Repository navigation
Expand file tree
/
Copy pathUtil.cpp
More file actions
298 lines (264 loc) · 5.18 KB
/
Copy pathUtil.cpp
File metadata and controls
298 lines (264 loc) · 5.18 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "utils/Util.h"
#include <locale.h>
#include <string>
#include <cstdlib>
using namespace std;
#include <fcntl.h>
#include "errno.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/timeb.h>
#ifdef WIN32
#define snprintf _snprintf
#endif
string int2string(int number)
{
char s[16];
snprintf(s,sizeof(s) - 1,"%d",number);
return (string)s;
}
int string2int(string str)
{
return atoi(str.c_str());
}
string llong2string(long long number)
{
char s[20];
snprintf(s,sizeof(s) - 1,"%lld",number);
return (string)s;
}
string lowToUpper(string str)
{
int i=0;
while (i<str.size())
{
if(str[i] >= 'a' && str[i] <= 'z')
str[i] -= 0x20;
i++;
}
return str;
}
double random(double start, double end)
{
return start+(end-start)*rand()/(RAND_MAX + 1.0);
}
string time2str(time_t const timer)
{
struct tm *l=localtime(&timer);
if (l == NULL)
{
return string("");
}
char buf[128];
snprintf(buf,sizeof(buf),"%04d-%02d-%02d %02d:%02d:%02d",l->tm_year+1900,l->tm_mon+1,l->tm_mday,l->tm_hour,l->tm_min,l->tm_sec);
string s(buf);
return s;
}
time_t gettime()
{
return time(0);
}
string getHourMinute() //获取时分
{
string ret = time2str(gettime());
return ret.substr(11, 5);
}
string getData() //获取日期
{
string ret = time2str(gettime());
return ret.substr(0, 10);
}
long long getCurrentTime() //获得当前时间,ms级
{
timeb tp;
ftime(&tp);
return ((long long)(tp.time))*1000+tp.millitm;
}
int bytesOfWord(const char firByte)
{
//结果返回
int ret = 0;
int ast = 0;
//单字节字符首位是0
if( (firByte & (0x80)) == 0 )
{
return 1;
}else
{
//计算字节头连续1的个数
for(ast = 0; ast <= 7; ast++)
{
if( (firByte & (0x80 >> ast) ) != 0)
{
ret++;
}else
{
break;
}
}
return ret;
}
return 1;
}
//获取指定长度的字符串(半角算半个长度)
// pSrc: 源字符串
// pOut: 输出字符串
//outLen: 输出字符串的长度
// words: 多少个字位后插入分隔符(以全角字符算,半角字符算半个长度)
void splitLongString(const char* pSrc, char *pOut, const int outLen, const int words, const char* split )
{
memset(pOut, 0, outLen);
char c = 0;
float currWords = 0;
int wordBytes = 0;
char *pOutCurr = pOut;
const char* splitor = split;
int splitor_len = strlen(splitor);
for(unsigned int i = 0; i < strlen(pSrc);)
{
c = *(pSrc + i);
wordBytes = bytesOfWord(c);
if((currWords + 0.5) >= words)
{
if( (pOutCurr - pOut) + splitor_len > outLen)
{
break;
}
memcpy(pOutCurr,splitor, splitor_len);
pOutCurr += splitor_len;
currWords = 0;
}
if( (pOutCurr - pOut) + wordBytes > outLen)
{
break;
}
memcpy(pOutCurr, pSrc + i, wordBytes);
wordBytes >= 2 ? currWords += 1 : currWords += 0.5;
pOutCurr += wordBytes;
i += wordBytes;
}
}
void splitToVector(const char* pSrc, vector<std::string>& vec, const char* pTok)
{
int tokenLen = strlen(pTok);
const char* pHead = NULL;
const char* pNextPos = NULL;
pHead = pSrc;
char temp[256] = {0};
while(1)
{
memset(temp, 0, sizeof(temp));
pNextPos = strstr(pHead, pTok);
//到达结尾
if(pNextPos == NULL)
{
strcpy(temp, pHead);
vec.push_back(std::string(temp));
break;
}
//未到结尾
else
{
memcpy(temp, pHead, pNextPos - pHead);
vec.push_back(std::string(temp));
pHead = pNextPos + tokenLen;
}
}
}
string splitHeadString(const char *src, unsigned int head_len, const char *endStr)
{
if(src == NULL) return " ";
string retName = " ";
char splitedStr[128] = {0};
splitLongString(src, splitedStr, 128, head_len);
vector<string> splitedVec;
splitToVector(splitedStr, splitedVec);
if(splitedVec.size() > 1)
{
retName = splitedVec.at(0) + endStr;
}else
{
retName = string(src);
}
return retName;
}
string ltrim(string str) //去掉首空格
{
int endIndex = str.find_first_not_of(' ');
if (endIndex>=0 && endIndex<str.length())
{
return str.substr(endIndex, str.length()-endIndex);
}
return str;
}
string rtrim(string str) //去掉尾空格
{
int endIndex = str.find_last_not_of(' ');
if (endIndex>=0 && endIndex<str.length())
{
return str.substr(0, str.length()-endIndex);
}
return str;
}
string int2Coin(int coin)
{
string ret = "";
vector<int> numDatas;
if (coin<1000)
{
numDatas.push_back(coin);
}
else
{
while (coin/1000 > 0)
{
numDatas.push_back(coin%1000);
coin /= 1000;
}
numDatas.push_back(coin);
}
for (int j=numDatas.size()-1;j>=0;j--)
{
if (j == numDatas.size()-1)
{
ret.append(int2string(numDatas[j]).c_str());
}
else
{
char toStr[8] = {0};
if (numDatas[j]<10)
{
snprintf(toStr, sizeof(toStr)-1, "00%d", numDatas[j]);
}
else if (numDatas[j]<100)
{
snprintf(toStr, sizeof(toStr)-1, "0%d", numDatas[j]);
}
else
{
snprintf(toStr, sizeof(toStr)-1, "%d", numDatas[j]);
}
ret.append(",");
ret.append(toStr);
}
}
return ret;
}
std::string splitLongStringWithMoveLine( const char *src, unsigned int head_len /*= 4*/ )
{
if(src == NULL) return "";
string retName = "";
char splitedStr[255] = {0};
splitLongString(src, splitedStr, 255, head_len);
vector<string> splitedVec;
splitToVector(splitedStr, splitedVec);
for (int i=0;i<splitedVec.size();i++)
{
retName+=splitedVec.at(i);
if (i !=splitedVec.size()-1)
{
retName+="\n";
}
}
return retName;
}