-
Notifications
You must be signed in to change notification settings - Fork 13
/
common.cpp
77 lines (71 loc) · 1.77 KB
/
common.cpp
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
#include "stdafx.h"
#include "common.h"
std::string IntToString(int iValue)
{
std::stringstream intString;
intString << iValue;
return intString.str();
}
std::string FloatToString(double f)
{
if (f == 0) return "0.0";
char szBuf[128] = {0};
sprintf(szBuf, "%.20f", f);
const char * pszDot = strrchr(szBuf, '.');
if (pszDot != NULL) {
size_t iLen = strlen(szBuf);
char * pEnd = szBuf + iLen - 1;
while (*pEnd == '0') {
pEnd--;
}
if (pEnd == pszDot) {
*pEnd = 0;
} else {
*(pEnd + 1) = 0;
}
}
return szBuf;
}
#include "utf8/unchecked.h"
std::wstring ConvertToWString(const std::string & szStr)
{
std::wstring utf16result;
utf8::unchecked::utf8to16(szStr.begin(), szStr.end(), back_inserter(utf16result));
return utf16result;
}
std::string ConvertToAString(const std::wstring & szStr)
{
std::string utf8result;
utf8::unchecked::utf16to8(szStr.begin(), szStr.end(), back_inserter(utf8result));
return utf8result;
}
void * LoadResourceFromFile(const char *szFilename, unsigned long *pSize)
{
unsigned char * pBuffer = NULL;
FILE * fp = fopen(szFilename, "rb");
if (fp)
{
fseek(fp,0,SEEK_END);
*pSize = ftell(fp);
fseek(fp,0,SEEK_SET);
pBuffer = new unsigned char[*pSize+1];
*pSize = (unsigned long)fread(pBuffer,sizeof(unsigned char), *pSize,fp);
pBuffer[*pSize] = 0;
fclose(fp);
}
return pBuffer;
}
bool LoadXMLFile(const char * szFileName, TiXmlDocument & doc)
{
unsigned long size = 0;
void * pData = LoadResourceFromFile(szFileName, &size);
if (pData == NULL) return false;
doc.Parse((char*)pData);
return true;
}
bool SaveXMLFile(const char * szFileName, TiXmlDocument & doc)
{
char szFullPath[_MAX_PATH];
sprintf(szFullPath, "%s", szFileName);
return doc.SaveFile(szFullPath);
}