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