-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathstringtools.h
343 lines (318 loc) · 8.72 KB
/
stringtools.h
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*************************************************************************
* UrBackup - Client/Server backup system
* Copyright (C) 2011 Martin Raiber
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#ifndef STRINGTOOLS_H
#define STRINGTOOLS_H
#include <string>
#include <vector>
#include <map>
#include "types.h"
std::string getafter(const std::string &str,const std::string &data);
std::string getafterinc(const std::string &str,const std::string &data);
std::wstring getafter(const std::wstring &str,const std::wstring &data);
std::wstring getafterinc(const std::wstring &str,const std::wstring &data);
std::string getbetween(std::string s1,std::string s2,std::string data);
std::string strdelete(std::string str,std::string data);
void writestring(std::string str,std::string file);
void writestring(char *str, unsigned int len,std::string file);
std::string getuntil(std::string str,std::string data);
std::wstring getuntil(std::wstring str,std::wstring data);
std::string getuntilinc(std::string str,std::string data);
std::string getline(int line, const std::string &str);
int linecount(const std::string &str);
std::string getFile(std::string filename);
#ifdef _WIN32
std::string getFile(std::wstring filename);
#endif
std::wstring getFileUTF8(std::string filename);
std::string ExtractFileName(std::string fulln);
std::wstring ExtractFileName(std::wstring fulln);
std::string ExtractFilePath(std::string fulln);
std::wstring ExtractFilePath(std::wstring fulln);
std::wstring convert(bool pBool);
std::wstring convert(int i);
std::wstring convert(float f);
std::wstring convert(long long int i);
std::wstring convert(size_t i);
std::wstring convert(unsigned long long int i);
std::wstring convert(unsigned int i);
std::string nconvert(bool pBool);
std::string nconvert(int i);
std::string nconvert(long int i);
std::string nconvert(long long int i);
std::string nconvert(size_t i);
std::string nconvert(unsigned long long int i);
std::string nconvert(unsigned int i);
std::string nconvert(float f);
std::string findextension(const std::string& pString);
std::string wnarrow(const std::wstring& pStr);
std::wstring widen(std::string tw);
std::string replaceonce(std::string tor, std::string tin, std::string data);
std::wstring replaceonce(std::wstring tor, std::wstring tin, std::wstring data);
void Tokenize(const std::string& str, std::vector<std::string> &tokens, std::string seps);
void Tokenize(const std::wstring& str, std::vector<std::wstring> &tokens, std::wstring seps);
void TokenizeMail(const std::string& str, std::vector<std::string> &tokens, std::string seps);
bool isnumber(char ch);
bool isletter(char ch);
bool isnumber(wchar_t ch);
bool isletter(wchar_t ch);
void strupper(std::string *pStr);
void strupper(std::wstring *pStr);
std::string greplace(std::string tor, std::string tin, std::string data);
std::wstring greplace(std::wstring tor, std::wstring tin, std::wstring data);
int getNextNumber(const std::string &pStr, int *read=NULL);
std::string strlower(const std::string &str);
bool next(const std::string &pData, const size_t & doff, const std::string &pStr);
bool next(const std::wstring &pData, const size_t & doff, const std::wstring &pStr);
char getRandomChar(void);
std::string getRandomNumber(void);
void transformHTML(std::string &str);
void EscapeSQLString(std::string &pStr);
void EscapeSQLString(std::wstring &pStr);
void EscapeCh(std::string &pStr, char ch='\\');
void EscapeCh(std::wstring &pStr, wchar_t ch);
std::string UnescapeSQLString(std::string pStr);
std::wstring UnescapeSQLString(std::wstring pStr);
void ParseParamStrHttp(const std::string &pStr, std::map<std::string, std::string> *pMap);
std::string FormatTime(int timeins);
bool IsHex(const std::string &str);
std::string byteToHex(unsigned char ch);
std::string bytesToHex(const unsigned char *b, size_t bsize);
std::string bytesToHex(const std::string& data);
unsigned long hexToULong(const std::string &data);
std::string htmldecode(std::string str, bool html=true, char xc='%');
bool checkhtml(const std::string &str);
std::string nl2br(std::string str);
bool FileExists(std::string pFile);
bool checkStringHTML(const std::string &str);
std::string ReplaceChar(std::string str, char tr, char ch);
std::wstring ReplaceChar(std::wstring str, wchar_t tr, wchar_t ch);
std::string striptags(std::string html);
std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_encode_dash(const std::string& data);
std::string base64_decode(std::string const& s);
bool CheckForIllegalChars(const std::string &str);
int watoi(std::wstring str);
std::wstring strlower(const std::wstring &str);
std::string trim(const std::string &str);
void replaceNonAlphaNumeric(std::string &str, char rch);
std::string conv_filename(std::string fn);
std::string EscapeParamString(const std::string &pStr);
std::string EscapePathParamString(const std::string &pStr);
std::string UnescapeParamString(const std::string & str);
namespace
{
bool is_big_endian(void)
{
union {
unsigned int i;
char c[4];
} bint = { 0x01020304 };
return bint.c[0] == 1;
}
unsigned int endian_swap(unsigned int x)
{
return (x >> 24) |
((x << 8) & 0x00FF0000) |
((x >> 8) & 0x0000FF00) |
(x << 24);
}
unsigned short endian_swap(unsigned short x)
{
return x = (x >> 8) |
(x << 8);
}
std::string endian_swap_utf16(std::string str)
{
for (size_t i = 0; i<str.size(); i += 2)
{
unsigned short *t = (unsigned short*)&str[i];
*t = endian_swap(*t);
}
return str;
}
unsigned long long int endian_swap(unsigned long long int x)
{
#ifdef _WIN32
return (x >> 56) |
((x << 40) & 0x00FF000000000000) |
((x << 24) & 0x0000FF0000000000) |
((x << 8) & 0x000000FF00000000) |
((x >> 8) & 0x00000000FF000000) |
((x >> 24) & 0x0000000000FF0000) |
((x >> 40) & 0x000000000000FF00) |
(x << 56);
#else
return (x >> 56) |
((x << 40) & 0x00FF000000000000LLU) |
((x << 24) & 0x0000FF0000000000LLU) |
((x << 8) & 0x000000FF00000000LLU) |
((x >> 8) & 0x00000000FF000000LLU) |
((x >> 24) & 0x0000000000FF0000LLU) |
((x >> 40) & 0x000000000000FF00LLU) |
(x << 56);
#endif
}
unsigned int little_endian(unsigned int x)
{
if (is_big_endian())
{
return endian_swap(x);
}
else
{
return x;
}
}
unsigned short little_endian(unsigned short x)
{
if (is_big_endian())
{
return endian_swap(x);
}
else
{
return x;
}
}
int little_endian(int x)
{
if (is_big_endian())
{
return static_cast<int>(endian_swap(static_cast<unsigned int>(x)));
}
else
{
return x;
}
}
uint64 little_endian(uint64 x)
{
if (is_big_endian())
{
return endian_swap(x);
}
else
{
return x;
}
}
int64 little_endian(int64 x)
{
if (is_big_endian())
{
return static_cast<int64>(endian_swap(static_cast<uint64>(x)));
}
else
{
return x;
}
}
float little_endian(float x)
{
if (is_big_endian())
{
unsigned int* ptr = reinterpret_cast<unsigned int*>(&x);
unsigned int ret = endian_swap(*ptr);
return *reinterpret_cast<float*>(&ret);
}
else
{
return x;
}
}
unsigned int big_endian(unsigned int x)
{
if (!is_big_endian())
{
return endian_swap(x);
}
else
{
return x;
}
}
unsigned short big_endian(unsigned short x)
{
if (!is_big_endian())
{
return endian_swap(x);
}
else
{
return x;
}
}
int big_endian(int x)
{
if (!is_big_endian())
{
return static_cast<int>(endian_swap(static_cast<unsigned int>(x)));
}
else
{
return x;
}
}
uint64 big_endian(uint64 x)
{
if (!is_big_endian())
{
return endian_swap(x);
}
else
{
return x;
}
}
int64 big_endian(int64 x)
{
if (!is_big_endian())
{
return static_cast<int64>(endian_swap(static_cast<uint64>(x)));
}
else
{
return x;
}
}
float big_endian(float x)
{
if (!is_big_endian())
{
unsigned int* ptr = reinterpret_cast<unsigned int*>(&x);
unsigned int ret = endian_swap(*ptr);
return *reinterpret_cast<float*>(&ret);
}
else
{
return x;
}
}
std::string big_endian_utf16(std::string str)
{
if (!is_big_endian())
{
return endian_swap_utf16(str);
}
else
{
return str;
}
}
}
#endif