Menu

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

Download this file

102 lines (95 with data), 2.1 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
#include "StdAfx.h"
#include "SerializeUtils.h"
#include <assert.h>
CSerializeUtils::CSerializeUtils(void)
{
}
CSerializeUtils::~CSerializeUtils(void)
{
}
bool CSerializeUtils::SaveNumber(HANDLE hFile, unsigned __int64 value)
{
DWORD written = 0;
SerializeTypes type = SerializeType_Number;
if (WriteFile(hFile, &type, sizeof(type), &written, NULL))
{
if (WriteFile(hFile, &value, sizeof(value), &written, NULL))
{
return true;
}
}
return false;
}
bool CSerializeUtils::LoadNumber(HANDLE hFile, unsigned __int64& value)
{
DWORD read = 0;
SerializeTypes type;
if (ReadFile(hFile, &type, sizeof(type), &read, NULL))
{
if (type == SerializeType_Number)
{
if (ReadFile(hFile, &value, sizeof(value), &read, NULL))
{
return true;
}
}
}
return false;
}
bool CSerializeUtils::SaveString(HANDLE hFile, string str)
{
DWORD written = 0;
SerializeTypes type = SerializeType_String;
if (WriteFile(hFile, &type, sizeof(type), &written, NULL))
{
size_t length = str.size();
assert(length < (1024*100));
if (WriteFile(hFile, &length, sizeof(length), &written, NULL))
{
if (WriteFile(hFile, str.c_str(), length, &written, NULL))
return true;
}
}
return false;
}
bool CSerializeUtils::SaveString(HANDLE hFile, wstring str)
{
return SaveString(hFile, CUnicodeUtils::StdGetUTF8(str));
}
bool CSerializeUtils::LoadString(HANDLE hFile, std::string &str)
{
DWORD read = 0;
SerializeTypes type;
if (ReadFile(hFile, &type, sizeof(type), &read, NULL))
{
if (type == SerializeType_String)
{
size_t length = 0;
if (ReadFile(hFile, &length, sizeof(length), &read, NULL))
{
if (length < (1024*100))
{
char * buffer = new char[length];
if (ReadFile(hFile, buffer, length, &read, NULL))
{
str = string(buffer, length);
delete [] buffer;
return true;
}
delete [] buffer;
}
}
}
}
return false;
}
bool CSerializeUtils::LoadString(HANDLE 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.