Menu

[r181]: / trunk / src / SerializeUtils.cpp  Maximize  Restore  History

Download this file

126 lines (115 with data), 2.7 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
#include "StdAfx.h"
#include "SerializeUtils.h"
#include <assert.h>
char * CSerializeUtils::buffer = NULL;
size_t CSerializeUtils::lenbuffer = 0;
CSerializeUtils::CSerializeUtils(void)
{
}
CSerializeUtils::~CSerializeUtils(void)
{
}
void CSerializeUtils::InitializeStatic()
{
buffer = new char[1024];
lenbuffer = 1024;
}
void CSerializeUtils::CleanupStatic()
{
if (buffer)
delete [] buffer;
lenbuffer = 0;
}
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::SaveString(FILE * hFile, string str)
{
SerializeTypes type = SerializeType_String;
if (fwrite(&type, sizeof(type), 1, hFile))
{
size_t length = str.size();
assert(length < (1024*100));
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::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 < (1024*100))
{
if (length)
{
if (length > lenbuffer)
{
delete [] buffer;
buffer = new char[length];
lenbuffer = length;
}
if (fread(buffer, sizeof(char), length, hFile))
{
str = string(buffer, length);
return true;
}
}
else
{
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;
}
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.