-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathHelper.h
More file actions
261 lines (210 loc) · 6.93 KB
/
PathHelper.h
File metadata and controls
261 lines (210 loc) · 6.93 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#ifndef __PATH_HELPER_H__
#define __PATH_HELPER_H__
#include "Platform.h"
#include <cctype>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <assert.h>
#include <string.h>
#ifdef __PLATFORM_WINDOWS__
#define LAST_DURATION(Topic) \
static int nLast##Topic##Time_ltzhou; \
int nCur##Topic##Time_ltzhou = GetTickCount(); \
nLast##Topic##Time_ltzhou = nLast##Topic##Time_ltzhou ? nLast##Topic##Time_ltzhou : nCur##Topic##Time_ltzhou; \
char szBuf##Topic##Time_ltzhou[128];\
sprintf(szBuf##Topic##Time_ltzhou, "Topic:%s time-sep:%d\n", #Topic, nCur##Topic##Time_ltzhou - nLast##Topic##Time_ltzhou); \
DebugPrint(szBuf##Topic##Time_ltzhou);\
nLast##Topic##Time_ltzhou = nCur##Topic##Time_ltzhou;
#define TICK_BEGIN(Topic) \
int nBefore##Topic##Tick_ = GetTickCount();
#define TICK_END(Topic) \
int nAfter##Topic##Tick_ = GetTickCount(); \
DebugPrint("Topic:%s time-sep:%d\n", #Topic, nAfter##Topic##Tick_ - nBefore##Topic##Tick_);
std::string GetConfigDir();
std::string GetBasicFileName();
//支持相对路径文件夹的创建
void SafeCreateDir(const std::string& strDir);
//相对路径转换成绝对路径文件名
std::string GetFullPath(const std::string& strFile);
#ifdef _DEBUG
int __cdecl DebugPrint(const char* szFormat, ...);
#else
#define DebugPrint(...)
#endif
#else
#define LAST_DURATION(Topic)
#define TICK_BEGIN(Topic)
#define TICK_END(Topic)
#endif
struct DateTimeInfo
{
unsigned int nYear;
unsigned int nMonth;
unsigned int nDay;
unsigned int nHour;
unsigned int nMinute;
unsigned int nSecond;
unsigned int nMiniSecond;
DateTimeInfo()
{
GetCurDateTime();
}
void GetCurDateTime();
unsigned int ToYYMMDD();
long long ToTick();
};
#ifdef NDEBUG
#define AssertEx(expr) expr;
#else
#define AssertEx(expr) {int n = (int)(expr); assert(n);}
#endif
class PathHelper
{
public:
static inline std::string <rim(std::string &s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
// trim from end
static inline std::string &rtrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
static inline std::string &trim(std::string &s) {
return ltrim(rtrim(s));
}
static std::string fully_replace(const std::string& str, const std::string& sub_str, const std::string& new_str);
static void NormalizePath(std::string& strPath, bool isDir, char chSep='\\');
#ifdef __PLATFORM_WINDOWS__
static void GetFileList(std::vector<std::string>&vecFiles, const std::string& strDir, const std::string& strSuffix, bool bRecursive);
#endif
static bool HasSuffix(const std::string &lhs, const std::string& strSuffix);
static bool IFind(const std::string & strText, const std::string& strSub);
static bool StartsWith(const std::string& str, const std::string& strPrefix);
static int IFindStrInVec(const std::vector<std::string>& vecStrs, const std::string& str);
};
std::string& strUpper(std::string& str);
std::string& strLower(std::string& str);
char* strupr_n(char* src, int n);
char* strlwr_n(char* src, int n);
std::string int2str(int n);
template <class Container> void GetStrVecInStr(/*std::vector<std::string>&*/Container& vecResult, const char* szInput, const char* szDelim=";", bool bEraseSpace=true)
{
if (vecResult.size())
{
vecResult.clear();
}
if (!szInput)
{
return ;
}
unsigned int nLen = strlen(szInput);
if (nLen>0x7fffffff)
{
assert(0);
return ;
}
char* szCpy = new char[nLen+1];
if (!szCpy)
{
assert(0);
return;
}
memcpy(szCpy, szInput, nLen);
szCpy[nLen]=0;
char* pContext = NULL;
char* pToken = strtok_s(szCpy, szDelim, &pContext);
while(pToken)
{
std::string strToken(pToken);
if (bEraseSpace)
{
PathHelper::trim(strToken);
if (strToken.size())
{
vecResult.push_back(strToken);
}
}
else
{
vecResult.push_back(strToken);
}
pToken = strtok_s(NULL, szDelim, &pContext);
}
delete [] szCpy;
}
bool IsFileExist(const std::string& strFile);
int ErrPrint(const char* szFormat, ...);
//这种函数效率很低,bFullMath要求两者必须完全一样,否则vec2的为空可以匹配vec1
template <class StrContainer> StrContainer StrVecAnd(const StrContainer& vec1, const StrContainer& vec2)
{
StrContainer vecRes;
for (typename StrContainer::const_iterator it1 = vec1.begin(); it1 != vec1.end(); it1++)
{
const std::string& str1 = *it1;
for (typename StrContainer::const_iterator it2 =vec2.begin(); it2!=vec2.end(); it2++)
{
const std::string& str2 = *it2;
if (stricmp(str1.c_str(), str2.c_str()) == 0)
{
vecRes.push_back(str1);
break;
}
}
}
return vecRes;
}
template <class StrContainer> StrContainer StrVecOr(const StrContainer& vec1, const StrContainer& vec2)
{
StrContainer vecRes = vec1;
for (typename StrContainer::const_iterator it2 =vec2.begin(); it2!=vec2.end(); it2++)
{
const std::string& str2 = *it2;
bool bNotFound = true;
for (typename StrContainer::const_iterator it1 = vec1.begin(); it1 != vec1.end(); it1++)
{
const std::string& str1 = *it1;
if (stricmp(str1.c_str(), str2.c_str()) == 0)
{
bNotFound = false;
break;
}
}
if (bNotFound)
{
vecRes.push_back(str2);
}
}
return vecRes;
}
//vec1 -vec2即在vec1中不在vec2中的
template <class StrContainer> StrContainer StrVecSubstraction(const StrContainer& vec1, const StrContainer& vec2)
{
if (vec2.size() == 0)
{
return vec1;
}
StrContainer vecRes;
for (typename StrContainer::const_iterator it1 = vec1.begin(); it1 != vec1.end(); it1++)
{
const std::string& str1 = *it1;
bool bEqual = false;
for (typename StrContainer::const_iterator it2 =vec2.begin(); it2!=vec2.end(); it2++)
{
const std::string& str2 = *it2;
if (stricmp(str1.c_str(), str2.c_str()) == 0)
{
bEqual = true;
break;
}
}
if (!bEqual)
{
vecRes.push_back(str1);
}
}
return vecRes;
}
#endif