-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCommon.cpp
159 lines (140 loc) · 3.42 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
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
#include "stdafx.h"
#include "Common.h"
static const char * gLogFile = "ApiDebugger.log";
wstring getAppNameW()
{
wstring appName;
wchar_t szPathName[_MAX_PATH];
memset(szPathName, 0, sizeof(szPathName));
GetModuleFileNameW(NULL, szPathName, _MAX_PATH);
appName = szPathName;
return appName;
}
wstring getAppPath()
{
wstring appName = getAppNameW();
int pos = (int)appName.find_last_of(L"\\/");
return appName.substr(0, pos+1);
}
string getModuleNameA(HMODULE hModule)
{
string appName;
char szPathName[_MAX_PATH];
memset(szPathName, 0, sizeof(szPathName));
GetModuleFileNameA(hModule, szPathName, _MAX_PATH);
appName = szPathName;
return appName;
}
string getAppNameA()
{
return getModuleNameA(NULL);
}
string getAppPathA()
{
string appName = getAppNameA();
int pos = (int)appName.find_last_of("\\/");
return appName.substr(0, pos+1);
}
string getFileName(const string &filePath)
{
int pos = (int)filePath.find_last_of("\\/");
if(pos > 0)
{
return filePath.substr(pos+1);
}
return filePath;
}
string formatString(const char *format, ...)
{
const int BUFFER_SIZE = 4097;
char strbuf[BUFFER_SIZE];
memset(strbuf, 0, sizeof(strbuf));
va_list ap;
va_start (ap, format);
int result = _vsnprintf(strbuf, BUFFER_SIZE-1, format, ap);
va_end (ap);
return string(strbuf, result);
}
string toHexString(const char *data, int size)
{
string result(size*2, 0);
static char *chars = "0123456789ABCDEF";
for(int i = 0; i < size; i ++)
{
char ch = data[i];
result[i*2+0] = chars[(ch & 0xF0) >> 4];
result[i*2+1] = chars[(ch & 0x0F)];
}
return result;
}
string getLogString()
{
SYSTEMTIME sysTime = { 0 };
GetLocalTime(&sysTime);
return formatString("[% 6d]%02d:%02d:%02d.%03d", (int)GetCurrentThreadId(),
(int)sysTime.wHour, (int)sysTime.wMinute, (int)sysTime.wSecond, (int)sysTime.wMilliseconds);
}
int writeDataToFile(const char *fileName, void *buffer, int size)
{
string filePath = getAppPathA() + fileName;
FILE *pFile = fopen(filePath.c_str(), "wb");
if(pFile != NULL)
{
int ret = fwrite(buffer, 1, size, pFile);
fclose(pFile);
pFile = NULL;
return ret;
}
return -1;
}
int readDataFromFile(const char *fileName, void *buffer, int size)
{
string filePath = getAppPathA() + fileName;
FILE *pFile = fopen(filePath.c_str(), "rb");
if(pFile != NULL)
{
int ret = fread(buffer, 1, size, pFile);
fclose(pFile);
pFile = NULL;
return ret;
}
return -1;
}
int appendDataToFile(const char *fileName, void *buffer, int size)
{
string filePath = getAppPathA() + fileName;
FILE *pFile = fopen(filePath.c_str(), "ab");
if(pFile != NULL)
{
fseek(pFile, 0, SEEK_END);
int ret = fwrite(buffer, 1, size, pFile);
fclose(pFile);
pFile = NULL;
return ret;
}
return -1;
}
int logOutput(const char *text)
{
if(!gEnableLogOutput)
{
return false;
}
string filePath = getAppPathA() + gLogFile;
FILE *pFile = fopen(filePath.c_str(), "ab");
if(pFile != NULL)
{
fseek(pFile, 0, SEEK_END);
string tstr = getLogString() + " - ";
int ret = fwrite(tstr.c_str(), 1, tstr.length(), pFile);
ret = fwrite(text, 1, strlen(text), pFile);
fclose(pFile);
pFile = NULL;
return ret;
}
return -1;
}
int logOutput(const string &text)
{
return logOutput(text.c_str());
}