Repository navigation
Expand file tree
/
Copy pathstrutil.cpp
More file actions
439 lines (363 loc) · 9.41 KB
/
Copy pathstrutil.cpp
File metadata and controls
439 lines (363 loc) · 9.41 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.h"
#include "strutil.h"
#include <algorithm>
#include <limits>
#include <algorithm>
#include <utility>
#include <functional>
#include <cctype>
#include "utf8cpp/utf8.h"
#include "memorystream.h"
namespace KBEngine{
namespace strutil {
int bytes2string(unsigned char *src, int srcsize, unsigned char *dst, int dstsize)
{
if (dst != NULL)
{
*dst = 0;
}
if (src == NULL || srcsize <= 0 || dst == NULL || dstsize <= srcsize * 2)
{
return 0;
}
const char szTable[] = "0123456789ABCDEF";
for(int i=0; i<srcsize; ++i)
{
*dst++ = szTable[src[i] >> 4];
*dst++ = szTable[src[i] & 0x0f];
}
*dst = 0;
return srcsize * 2;
}
int string2bytes(unsigned char* src, unsigned char* dst, int dstsize)
{
if(src == NULL)
return 0;
int iLen = (int)strlen((char *)src);
if (iLen <= 0 || iLen%2 != 0 || dst == NULL || dstsize < iLen/2)
{
return 0;
}
iLen /= 2;
str_toupper((char *)src);
for (int i=0; i<iLen; ++i)
{
int iVal = 0;
unsigned char *pSrcTemp = src + i*2;
sscanf((char *)pSrcTemp, "%02x", &iVal);
dst[i] = (unsigned char)iVal;
}
return iLen;
}
std::string toLower(const std::string& str) {
std::string t = str;
std::transform(t.begin(), t.end(), t.begin(), tolower);
return t;
}
std::string toUpper(const std::string& str) {
std::string t = str;
std::transform(t.begin(), t.end(), t.begin(), toupper);
return t;
}
std::string &kbe_ltrim(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;
}
std::string &kbe_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;
}
std::string kbe_trim(std::string s)
{
return kbe_ltrim(kbe_rtrim(s));
}
// ×Ö·û´®Ìæ»»
int kbe_replace(std::string& str, const std::string& pattern, const std::string& newpat)
{
int count = 0;
const size_t nsize = newpat.size();
const size_t psize = pattern.size();
for(size_t pos = str.find(pattern, 0);
pos != std::string::npos;
pos = str.find(pattern,pos + nsize))
{
str.replace(pos, psize, newpat);
count++;
}
return count;
}
int kbe_replace(std::wstring& str, const std::wstring& pattern, const std::wstring& newpat)
{
int count = 0;
const size_t nsize = newpat.size();
const size_t psize = pattern.size();
for(size_t pos = str.find(pattern, 0);
pos != std::wstring::npos;
pos = str.find(pattern,pos + nsize))
{
str.replace(pos, psize, newpat);
count++;
}
return count;
}
std::vector< std::string > kbe_splits(const std::string& s, const std::string& delim, const bool keep_empty)
{
std::vector< std::string > result;
if (delim.empty()) {
result.push_back(s);
return result;
}
std::string::const_iterator substart = s.begin(), subend;
while (true) {
subend = std::search(substart, s.end(), delim.begin(), delim.end());
std::string temp(substart, subend);
if (keep_empty || !temp.empty()) {
result.push_back(temp);
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
}
return result;
}
char* wchar2char(const wchar_t* ts, size_t* outlen)
{
int len = (int)((wcslen(ts) + 1) * sizeof(wchar_t));
char* ccattr =(char *)malloc(len);
memset(ccattr, 0, len);
size_t slen = wcstombs(ccattr, ts, len);
if (outlen)
{
if ((size_t)-1 != slen)
*outlen = slen;
else
*outlen = 0;
}
return ccattr;
};
void wchar2char(const wchar_t* ts, MemoryStream* pOutStream)
{
int len = (int)((wcslen(ts) + 1) * sizeof(wchar_t));
pOutStream->data_resize(pOutStream->wpos() + len);
size_t slen = wcstombs((char*)&pOutStream->data()[pOutStream->wpos()], ts, len);
if((size_t)-1 != slen)
{
pOutStream->wpos(pOutStream->wpos() + slen + 1);
pOutStream->data()[pOutStream->wpos() - 1] = 0;
}
};
wchar_t* char2wchar(const char* cs, size_t* outlen)
{
int len = (int)((strlen(cs) + 1) * sizeof(wchar_t));
wchar_t* ccattr =(wchar_t *)malloc(len);
memset(ccattr, 0, len);
size_t slen = mbstowcs(ccattr, cs, len);
if (outlen)
{
if ((size_t)-1 != slen)
*outlen = slen;
else
*outlen = 0;
}
return ccattr;
};
/*
int wchar2utf8(const wchar_t* in, int in_len, char* out, int out_max)
{
#if KBE_PLATFORM == PLATFORM_WIN32
BOOL use_def_char;
use_def_char = FALSE;
return ::WideCharToMultiByte(CP_UTF8, 0, in,in_len / sizeof(wchar_t), out, out_max, NULL, NULL);
#else
size_t result;
iconv_t env;
env = iconv_open("UTF8", "WCHAR_T");
result = iconv(env,(char**)&in,(size_t*)&in_len,(char**)&out,(size_t*)&out_max);
iconv_close(env);
return (int) result;
#endif
}
int wchar2utf8(const std::wstring& in, std::string& out)
{
int len = in.length() + 1;
int result;
char* pBuffer = new char[len * 4];
memset(pBuffer,0,len * 4);
result = wchar2utf8(in.c_str(), in.length() * sizeof(wchar_t), pBuffer,len * 4);
if(result >= 0)
{
out = pBuffer;
}
else
{
out = "";
}
delete[] pBuffer;
return result;
}
int utf82wchar(const char* in, int in_len, wchar_t* out, int out_max)
{
#if KBE_PLATFORM == PLATFORM_WIN32
return ::MultiByteToWideChar(CP_UTF8, 0, in, in_len, out, out_max);
#else
size_t result;
iconv_t env;
env = iconv_open("WCHAR_T", "UTF8");
result = iconv(env,(char**)&in, (size_t*)&in_len, (char**)&out,(size_t*)&out_max);
iconv_close(env);
return (int) result;
#endif
}
int utf82wchar(const std::string& in, std::wstring& out)
{
int len = in.length() + 1;
int result;
wchar_t* pBuffer = new wchar_t[len];
memset(pBuffer,0,len * sizeof(wchar_t));
result = utf82wchar(in.c_str(), in.length(), pBuffer, len*sizeof(wchar_t));
if(result >= 0)
{
out = pBuffer;
}
else
{
out.clear();
}
delete[] pBuffer;
return result;
}
*/
size_t utf8length(std::string& utf8str)
{
try
{
return utf8::distance(utf8str.c_str(),
utf8str.c_str() + utf8str.size());
}
catch (std::exception)
{
utf8str = "";
return 0;
}
}
void utf8truncate(std::string& utf8str, size_t len)
{
try
{
size_t wlen = utf8::distance(utf8str.c_str(),
utf8str.c_str() + utf8str.size());
if (wlen <= len)
return;
std::wstring wstr;
wstr.resize(wlen);
utf8::utf8to16(utf8str.c_str(), utf8str.c_str() +
utf8str.size(), &wstr[0]);
wstr.resize(len);
char* oend = utf8::utf16to8(wstr.c_str(),
wstr.c_str() + wstr.size(), &utf8str[0]);
utf8str.resize(oend - (&utf8str[0]));
}
catch (std::exception)
{
utf8str = "";
}
}
bool utf82wchar(char const* utf8str, size_t csize,
wchar_t* wstr, size_t& wsize)
{
try
{
size_t len = utf8::distance(utf8str, utf8str + csize);
if (len > wsize)
{
if (wsize > 0)
wstr[0] = L'\0';
wsize = 0;
return false;
}
wsize = len;
utf8::utf8to16(utf8str, utf8str + csize, wstr);
wstr[len] = L'\0';
}
catch (std::exception)
{
if (wsize > 0)
wstr[0] = L'\0';
wsize = 0;
return false;
}
return true;
}
bool utf82wchar(const std::string& utf8str, std::wstring& wstr)
{
try
{
size_t len = utf8::distance(utf8str.c_str(),
utf8str.c_str() + utf8str.size());
wstr.resize(len);
if (len)
utf8::utf8to16(utf8str.c_str(),
utf8str.c_str() + utf8str.size(), &wstr[0]);
}
catch (std::exception)
{
wstr = L"";
return false;
}
return true;
}
bool wchar2utf8(const wchar_t* wstr, size_t size, std::string& utf8str)
{
try
{
std::string utf8str2;
utf8str2.resize(size * 4); // allocate for most long case
char* oend = utf8::utf16to8(wstr, wstr + size, &utf8str2[0]);
utf8str2.resize(oend - (&utf8str2[0])); // remove unused tail
utf8str = utf8str2;
}
catch (std::exception)
{
utf8str = "";
return false;
}
return true;
}
bool wchar2utf8(const std::wstring& wstr, std::string& utf8str)
{
try
{
std::string utf8str2;
utf8str2.resize(wstr.size() * 4); // allocate for most long case
char* oend = utf8::utf16to8(wstr.c_str(),
wstr.c_str() + wstr.size(), &utf8str2[0]);
utf8str2.resize(oend - (&utf8str2[0])); // remove unused tail
utf8str = utf8str2;
}
catch (std::exception)
{
utf8str = "";
return false;
}
return true;
}
}
}