Menu

[r568]: / branches / multiSCCS / src / SerializeUtils.cpp  Maximize  Restore  History

Download this file

238 lines (216 with data), 6.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// CommitMonitor - simple checker for new commits in svn repositories
// Copyright (C) 2007 - Stefan Kueng
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "StdAfx.h"
#include "SerializeUtils.h"
#include <assert.h>
char CSerializeUtils::buffer[SERIALIZEBUFFERSIZE] = {0};
wchar_t CSerializeUtils::wbuffer[SERIALIZEBUFFERSIZE] = {0};
CSerializeUtils::CSerializeUtils(void)
{
}
CSerializeUtils::~CSerializeUtils(void)
{
}
bool CSerializeUtils::SaveNumber(FILE * hFile, unsigned __int64 value)
{
SerializeTypes type = SerializeType_Number;
if (fwrite(&type, sizeof(type), 1, hFile))
{
if (fwrite(&value, sizeof(value), 1, hFile))
{
return true;
}
}
return false;
}
bool CSerializeUtils::LoadNumber(FILE * hFile, unsigned __int64& value)
{
SerializeTypes type;
if (fread(&type, sizeof(type), 1, hFile))
{
if (type == SerializeType_Number)
{
if (fread(&value, sizeof(value), 1, hFile))
{
return true;
}
}
}
return false;
}
bool CSerializeUtils::LoadNumber(const unsigned char *& buf, unsigned __int64& value)
{
SerializeTypes type = *((SerializeTypes*)buf);
buf += sizeof(SerializeTypes);
if (type == SerializeType_Number)
{
value = *((unsigned __int64 *)buf);
buf += sizeof(unsigned __int64);
return true;
}
return false;
}
bool CSerializeUtils::SaveString(FILE * hFile, string str)
{
SerializeTypes type = SerializeType_String;
if (fwrite(&type, sizeof(type), 1, hFile))
{
size_t length = str.size();
if (fwrite(&length, sizeof(length), 1, hFile))
{
if (fwrite(str.c_str(), sizeof(char), length, hFile)>=0)
return true;
}
}
return false;
}
bool CSerializeUtils::SaveString(FILE * hFile, wstring str)
{
return SaveString(hFile, CUnicodeUtils::StdGetUTF8(str));
}
bool CSerializeUtils::SaveBuffer(FILE * hFile, BYTE * pbData, size_t len)
{
SerializeTypes type = SerializeType_Buffer;
if (fwrite(&type, sizeof(type), 1, hFile))
{
if (fwrite(&len, sizeof(len), 1, hFile))
{
if (fwrite(pbData, sizeof(BYTE), len, hFile)>=0)
return true;
}
}
return false;
}
bool CSerializeUtils::LoadString(FILE * hFile, std::string &str)
{
SerializeTypes type;
if (fread(&type, sizeof(type), 1, hFile))
{
if (type == SerializeType_String)
{
size_t length = 0;
if (fread(&length, sizeof(length), 1, hFile))
{
if (length)
{
if (length < SERIALIZEBUFFERSIZE)
{
if (fread(buffer, sizeof(char), length, hFile))
{
str = string(buffer, length);
return true;
}
}
char * pBuffer = new char[length];
if (fread(pBuffer, sizeof(char), length, hFile))
{
str = string(pBuffer, length);
delete [] pBuffer;
return true;
}
delete [] pBuffer;
}
else
{
str = string("");
return true;
}
}
}
}
return false;
}
bool CSerializeUtils::LoadString(const unsigned char *& buf, std::string &str)
{
SerializeTypes type = *((SerializeTypes*)buf);
buf += sizeof(SerializeTypes);
if (type == SerializeType_String)
{
size_t length = *((size_t *)buf);
buf += sizeof(size_t);
if (length)
{
str = string((const char *)buf, length);
buf += length;
return true;
}
str = string("");
return true;
}
return false;
}
bool CSerializeUtils::LoadString(FILE * hFile, wstring& str)
{
string tempstr;
if (LoadString(hFile, tempstr))
{
str = CUnicodeUtils::StdGetUnicode(tempstr);
return true;
}
return false;
}
bool CSerializeUtils::LoadString(const unsigned char *& buf, wstring& str)
{
SerializeTypes type = *((SerializeTypes*)buf);
buf += sizeof(SerializeTypes);
if (type == SerializeType_String)
{
size_t length = *((size_t *)buf);
buf += sizeof(size_t);
if (length)
{
int size = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)buf, length, NULL, 0);
if (size < SERIALIZEBUFFERSIZE)
{
int ret = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)buf, length, wbuffer, size);
str = wstring(wbuffer, ret);
}
else
{
wchar_t * wide = new wchar_t[size+1];
int ret = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)buf, length, wide, size);
str = wstring(wide, ret);
delete [] wide;
}
buf += length;
return true;
}
str = wstring(_T(""));
return true;
}
return false;
}
bool CSerializeUtils::LoadBuffer(const unsigned char *& buf, BYTE *& pbData, size_t & len)
{
SerializeTypes type = *((SerializeTypes*)buf);
buf += sizeof(SerializeTypes);
if (type == SerializeType_Buffer)
{
size_t length = *((size_t *)buf);
buf += sizeof(size_t);
if (length)
{
pbData = new BYTE[length];
memcpy(pbData, buf, length);
len = length;
buf += length;
return true;
}
len = 0;
pbData = NULL;
return true;
}
return false;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.