00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "IClipboard.h"
00016 #include "stdvector.h"
00017
00018
00019
00020
00021
00022 void
00023 IClipboard::unmarshall(IClipboard* clipboard, const CString& data, Time time)
00024 {
00025 assert(clipboard != NULL);
00026
00027 const char* index = data.data();
00028
00029
00030 clipboard->open(time);
00031 clipboard->empty();
00032
00033
00034 const UInt32 numFormats = readUInt32(index);
00035 index += 4;
00036
00037
00038 for (UInt32 i = 0; i < numFormats; ++i) {
00039
00040 IClipboard::EFormat format =
00041 static_cast<IClipboard::EFormat>(readUInt32(index));
00042 index += 4;
00043
00044
00045 UInt32 size = readUInt32(index);
00046 index += 4;
00047
00048
00049
00050
00051 if (format <IClipboard::kNumFormats) {
00052 clipboard->add(format, CString(index, size));
00053 }
00054 index += size;
00055 }
00056
00057
00058 clipboard->close();
00059 }
00060
00061 CString
00062 IClipboard::marshall(const IClipboard* clipboard)
00063 {
00064 assert(clipboard != NULL);
00065
00066 CString data;
00067
00068 std::vector<CString> formatData;
00069 formatData.resize(IClipboard::kNumFormats);
00070
00071 clipboard->open(0);
00072
00073
00074 UInt32 size = 4;
00075 UInt32 numFormats = 0;
00076 for (UInt32 format = 0; format != IClipboard::kNumFormats; ++format) {
00077 if (clipboard->has(static_cast<IClipboard::EFormat>(format))) {
00078 ++numFormats;
00079 formatData[format] =
00080 clipboard->get(static_cast<IClipboard::EFormat>(format));
00081 size += 4 + 4 + formatData[format].size();
00082 }
00083 }
00084
00085
00086 data.reserve(size);
00087
00088
00089 writeUInt32(&data, numFormats);
00090 for (UInt32 format = 0; format != IClipboard::kNumFormats; ++format) {
00091 if (clipboard->has(static_cast<IClipboard::EFormat>(format))) {
00092 writeUInt32(&data, format);
00093 writeUInt32(&data, formatData[format].size());
00094 data += formatData[format];
00095 }
00096 }
00097 clipboard->close();
00098
00099 return data;
00100 }
00101
00102 bool
00103 IClipboard::copy(IClipboard* dst, const IClipboard* src)
00104 {
00105 assert(dst != NULL);
00106 assert(src != NULL);
00107
00108 return copy(dst, src, src->getTime());
00109 }
00110
00111 bool
00112 IClipboard::copy(IClipboard* dst, const IClipboard* src, Time time)
00113 {
00114 assert(dst != NULL);
00115 assert(src != NULL);
00116
00117 bool success = false;
00118 if (src->open(time)) {
00119 if (dst->open(time)) {
00120 if (dst->empty()) {
00121 for (SInt32 format = 0;
00122 format != IClipboard::kNumFormats; ++format) {
00123 IClipboard::EFormat eFormat = (IClipboard::EFormat)format;
00124 if (src->has(eFormat)) {
00125 dst->add(eFormat, src->get(eFormat));
00126 }
00127 }
00128 success = true;
00129 }
00130 dst->close();
00131 }
00132 src->close();
00133 }
00134
00135 return success;
00136 }
00137
00138 UInt32
00139 IClipboard::readUInt32(const char* buf)
00140 {
00141 const unsigned char* ubuf = reinterpret_cast<const unsigned char*>(buf);
00142 return (static_cast<UInt32>(ubuf[0]) << 24) |
00143 (static_cast<UInt32>(ubuf[1]) << 16) |
00144 (static_cast<UInt32>(ubuf[2]) << 8) |
00145 static_cast<UInt32>(ubuf[3]);
00146 }
00147
00148 void
00149 IClipboard::writeUInt32(CString* buf, UInt32 v)
00150 {
00151 *buf += static_cast<UInt8>((v >> 24) & 0xff);
00152 *buf += static_cast<UInt8>((v >> 16) & 0xff);
00153 *buf += static_cast<UInt8>((v >> 8) & 0xff);
00154 *buf += static_cast<UInt8>( v & 0xff);
00155 }