forked from YeeYoungHan/cppftpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtility.cpp
More file actions
618 lines (526 loc) · 14.6 KB
/
StringUtility.cpp
File metadata and controls
618 lines (526 loc) · 14.6 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "StringUtility.h"
#include <string.h>
#include <stdlib.h>
#include "MemoryDebug.h"
/**
* @ingroup SipPlatform
* @brief 문자열에 포함된 문자열을 수정한다.
* @param strCallId 문자열
* @param pszBefore 수정 대상 문자열
* @param pszAfter 수정 대상 문자열을 수정할 문자열
*/
void ReplaceString( std::string & strCallId, const char * pszBefore, const char * pszAfter )
{
size_t iPos = strCallId.find( pszBefore );
size_t iBeforeLen = strlen( pszBefore );
size_t iAfterLen = strlen( pszAfter );
while( iPos < std::string::npos )
{
strCallId.replace( iPos, iBeforeLen, pszAfter );
iPos = strCallId.find( pszBefore, iPos + iAfterLen );
}
}
/**
* @ingroup SipPlatform
* @brief 문자열에 포함된 키의 값을 추출한다.
* "app=36;msg=36;hotline=46;presence=36; broadcast=46" 문자열에서
* app 의 값을 추출하고 싶으면 pszKey 에 "app=" 를 입력하고 cSep 에 ';' 를 입력하면 된다.
* @param strText 문자열
* @param pszKey 키
* @param cSep 구분자
* @param strValue 키의 값을 저장할 변수
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool SearchValue( std::string & strText, const char * pszKey, char cSep, std::string & strValue )
{
strValue.clear();
size_t iPos = strText.find( pszKey );
if( iPos < std::string::npos )
{
size_t iKeyLen = strlen( pszKey );
size_t iEndPos = strText.find( cSep, iPos + iKeyLen );
if( iEndPos < std::string::npos )
{
strValue = strText.substr( iPos + iKeyLen, iEndPos - ( iPos + iKeyLen ) );
}
else
{
strValue = strText.substr( iPos + iKeyLen );
}
return true;
}
return false;
}
/**
* @ingroup SipPlatform
* @brief 문자열에 포함된 키의 값을 추출한다.
* "app=36;msg=36;hotline=46;presence=36; broadcast=46" 문자열에서
* app 의 값을 추출하고 싶으면 pszKey 에 "app=" 를 입력하고 cSep 에 ';' 를 입력하면 된다.
* @param strText 문자열
* @param pszKey 키
* @param cSep 구분자
* @param iValue 키의 값을 저장할 변수
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool SearchValue( std::string & strText, const char * pszKey, char cSep, int & iValue )
{
std::string strValue;
if( SearchValue( strText, pszKey, cSep, strValue ) )
{
iValue = atoi( strValue.c_str() );
return true;
}
return false;
}
/**
* @ingroup SipPlatform
* @brief 검색 문자열이 문자열 리스트에 존재하는지 검사한다.
* @param clsList 문자열 리스트
* @param pszKey 검색 문자열
* @returns 검색 문자열이 문자열 리스트에 존재하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool SearchStringList( STRING_LIST & clsList, const char * pszKey )
{
STRING_LIST::iterator itList;
for( itList = clsList.begin(); itList != clsList.end(); ++itList )
{
if( !strcmp( pszKey, itList->c_str() ) )
{
return true;
}
}
return false;
}
/**
* @ingroup SipPlatform
* @brief 검색 문자열이 문자열 리스트에 존재하면 삭제한다.
* @param clsList 문자열 리스트
* @param pszKey 검색 문자열
* @returns 검색 문자열이 문자열 리스트에 존재하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool DeleteStringList( STRING_LIST & clsList, const char * pszKey )
{
STRING_LIST::iterator itList;
for( itList = clsList.begin(); itList != clsList.end(); ++itList )
{
if( !strcmp( pszKey, itList->c_str() ) )
{
clsList.erase( itList );
return true;
}
}
return false;
}
/**
* @ingroup SipPlatform
* @brief 문자열이 문자열 리스트에 존재하지 않으면 추가한다.
* @param clsList 문자열 리스트
* @param pszKey 문자열
*/
void InsertStringList( STRING_LIST & clsList, const char * pszKey )
{
STRING_LIST::iterator itList;
for( itList = clsList.begin(); itList != clsList.end(); ++itList )
{
if( !strcmp( pszKey, itList->c_str() ) )
{
return;
}
}
clsList.push_back( pszKey );
}
/**
* @ingroup SipPlatform
* @brief 입력 문자열 리스트의 각 문자열이 저장 문자열 리스트에 존재하지 않으면 저장 문자열 리스트에 저장한다.
* @param clsList 저장 문자열 리스트
* @param clsSrcList 입력 문자열 리스트
*/
void InsertStringList( STRING_LIST & clsList, STRING_LIST & clsSrcList )
{
STRING_LIST::iterator itList;
for( itList = clsSrcList.begin(); itList != clsSrcList.end(); ++itList )
{
InsertStringList( clsList, itList->c_str() );
}
}
/**
* @ingroup SipPlatform
* @brief 문자열 리스트를 로그로 출력한다.
* @param eLevel 로그 레벨
* @param pszName 로그 메시지 이름
* @param clsList 문자열 리스트
*/
void LogStringList( EnumLogLevel eLevel, const char * pszName, STRING_LIST & clsList )
{
if( CLog::IsPrintLogLevel( eLevel ) )
{
STRING_LIST::iterator itList;
std::string strData;
for( itList = clsList.begin(); itList != clsList.end(); ++itList )
{
if( strData.empty() == false ) strData.append( ", " );
strData.append( *itList );
}
CLog::Print( eLevel, "%s [%s]", pszName, strData.c_str() );
}
}
/**
* @ingroup SipPlatform
* @brief 문자열의 왼쪽 공백을 제거한다.
* @param strText 문자열
*/
void LeftTrimString( std::string & strText )
{
int iIndex;
int iLen = (int)strText.length();
for( iIndex = 0; iIndex < iLen; ++iIndex )
{
char c = strText.at(iIndex);
if( c == ' ' || c == '\t' ) continue;
strText.erase( 0, iIndex );
break;
}
if( iIndex == iLen )
{
strText.clear();
}
}
/**
* @ingroup SipPlatform
* @brief 문자열의 오른쪽 공백을 제거한다.
* @param strText 문자열
*/
void RightTrimString( std::string & strText )
{
int iIndex;
int iLen = (int)strText.length();
for( iIndex = iLen - 1; iIndex >= 0; --iIndex )
{
char c = strText.at(iIndex);
if( c == ' ' || c == '\t' ) continue;
if( iIndex != ( iLen - 1 ) )
{
strText.erase( iIndex + 1 );
}
break;
}
if( iIndex == -1 )
{
strText.clear();
}
}
/**
* @ingroup SipPlatform
* @brief 문자열의 왼쪽, 오른쪽 공백을 제거한다.
* @param strText 문자열
*/
void TrimString( std::string & strText )
{
LeftTrimString( strText );
RightTrimString( strText );
}
/**
* @ingroup SipPlatform
* @brief 입력 문자열을 구분자로 분리하여서 문자열 리스트에 저장한다.
* @param pszText 입력 문자열
* @param clsList 문자열 리스트
* @param cSep 구분자
*/
void SplitString( const char * pszText, STRING_LIST & clsList, char cSep )
{
int iStartPos = -1, i;
clsList.clear();
for( i = 0; pszText[i]; ++i )
{
if( pszText[i] == cSep )
{
if( iStartPos >= 0 && iStartPos != i )
{
std::string strTemp;
strTemp.append( pszText + iStartPos, i - iStartPos );
clsList.push_back( strTemp );
}
iStartPos = i + 1;
}
else if( i == 0 )
{
iStartPos = 0;
}
}
if( iStartPos >= 0 && iStartPos != i )
{
std::string strTemp;
strTemp.append( pszText + iStartPos, i - iStartPos );
clsList.push_back( strTemp );
}
}
/**
* @ingroup SipPlatform
* @brief 입력 문자열을 구분자로 분리하여서 문자열 리스트에 저장한다.
* @param pszText 입력 문자열
* @param clsList 문자열 리스트
* @param cSep 구분자
*/
void SplitString( const char * pszText, STRING_VECTOR & clsList, char cSep )
{
int iStartPos = -1, i;
clsList.clear();
for( i = 0; pszText[i]; ++i )
{
if( pszText[i] == cSep )
{
if( iStartPos >= 0 && iStartPos != i )
{
std::string strTemp;
strTemp.append( pszText + iStartPos, i - iStartPos );
clsList.push_back( strTemp );
}
iStartPos = i + 1;
}
else if( i == 0 )
{
iStartPos = 0;
}
}
if( iStartPos >= 0 && iStartPos != i )
{
std::string strTemp;
strTemp.append( pszText + iStartPos, i - iStartPos );
clsList.push_back( strTemp );
}
}
/**
* @ingroup SipPlatform
* @brief 문자열을 unsigned int 로 변환한다.
* @param pszText 문자열
* @returns unsigned int 를 리턴한다.
*/
uint32_t GetUInt32( const char * pszText )
{
if( pszText == NULL ) return 0;
return strtoul( pszText, NULL, 10 );
}
/**
* @ingroup SipPlatform
* @brief 문자열을 unsigned long long 으로 변환한다.
* @param pszText 문자열
* @returns unsigned long long 을 리턴한다.
*/
uint64_t GetUInt64( const char * pszText )
{
if( pszText == NULL ) return 0;
#ifdef WIN32
return _strtoui64( pszText, NULL, 10 );
#else
return strtoull( pszText, NULL, 10 );
#endif
}
/**
* @ingroup SipPlatform
* @brief 지정될 길이만큼의 문자열을 숫자로 변환한다.
* @param pszText 숫자 문자열
* @param iTextLen 문자열 길이
* @returns 성공하면 원하는 숫자가 리턴되고 실패하면 0 이 리턴된다.
*/
int GetInt( const char * pszText, int iTextLen )
{
char szNum[11];
if( iTextLen > 10 || iTextLen <= 0 ) return 0;
memcpy( szNum, pszText, iTextLen );
szNum[iTextLen] = '\0';
return atoi( szNum );
}
/**
* @ingroup SipPlatform
* @brief HEX 만 저장된 문자열을 숫자로 변환한 문자열로 변환한다.
* @param pszInput HEX 만 저장된 문자열
* @param strOutput [out] 숫자로 변환된 문자열
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool HexToString( const char * pszInput, std::string & strOutput )
{
int iLen = (int)strlen( pszInput );
int iValue;
strOutput.clear();
if( iLen >= 2 )
{
if( pszInput[0] == '0' && pszInput[1] == 'x' )
{
pszInput += 2;
iLen -= 2;
}
}
if( iLen == 0 || iLen % 2 == 1 ) return false;
for( int i = 0; i < iLen; i += 2 )
{
sscanf( pszInput + i, "%02x", &iValue );
strOutput.push_back( (char)iValue );
}
return true;
}
/**
* @ingroup SipPlatform
* @brief 숫자가 저장된 문자열을 HEX 만 저장된 문자열로 변환한다.
* @param pszInput 숫자가 저장된 문자열
* @param iInputLen pszInput 변수의 길이
* @param strOutput [out] HEX 만 저장된 문자열
*/
void StringToHex( const char * pszInput, int iInputLen, std::string & strOutput )
{
char szHex[3];
strOutput.clear();
for( int i = 0; i < iInputLen; ++i )
{
snprintf( szHex, sizeof(szHex), "%02x", (uint8_t)pszInput[i] );
strOutput.append( szHex );
}
}
/**
* @ingroup SipPlatform
* @brief 문자열이 출력 가능한지 검사한다.
* @param pszText 문자열
* @param iTextLen 문자열 길이
* @returns 문자열이 출력 가능하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool IsPrintString( const char * pszText, int iTextLen )
{
for( int i = 0; i < iTextLen; ++i )
{
if( isprint( (uint8_t)pszText[i] ) == 0 ) return false;
}
return true;
}
/**
* @ingroup SipPlatform
* @brief 입력된 문자열에서 " 를 제거한 출력 문자열을 저장한다.
* @param strInput 입력 문자열
* @param strOutput 출력 문자열
*/
void DeQuoteString( std::string & strInput, std::string & strOutput )
{
int iLen = (int)strInput.length();
strOutput.clear();
if( iLen > 0 )
{
if( strInput.at( 0 ) != '"' || strInput.at( iLen - 1 ) != '"' )
{
strOutput = strInput;
}
else
{
strOutput.append( strInput, 1, iLen - 2 );
}
}
}
#ifdef WIN32
#include <windows.h>
/**
* @ingroup SipPlatform
* @brief UTF8 문자열을 ANSI 문자열로 변환한다.
* @param pszUtf8 UTF8 문자열 (input)
* @param strOutput ANSI 문자열 (output)
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool Utf8ToAnsi( const char * pszUtf8, std::string & strOutput )
{
BSTR strWide = NULL;
char* pszAnsi = NULL;
int iLength;
int iUtf8Length = (int)strlen(pszUtf8) + 1;
iLength = MultiByteToWideChar( CP_UTF8, 0, pszUtf8, iUtf8Length, NULL, NULL );
if( iLength == 0 )
{
CLog::Print( LOG_ERROR, "%s MultiByteToWideChar error(%d)", __FUNCTION__, GetLastError() );
return false;
}
strWide = SysAllocStringLen( NULL, iLength );
if( strWide == NULL )
{
CLog::Print( LOG_ERROR, "%s SysAllocStringLen error(%d)", __FUNCTION__, GetLastError() );
return false;
}
MultiByteToWideChar( CP_UTF8, 0, pszUtf8, iUtf8Length, strWide, iLength );
iLength = WideCharToMultiByte( CP_ACP, 0, strWide, -1, NULL, 0, NULL, NULL );
if( iLength == 0 )
{
SysFreeString( strWide );
CLog::Print( LOG_ERROR, "%s WideCharToMultiByte error(%d)", __FUNCTION__, GetLastError() );
return false;
}
pszAnsi = new char[iLength];
if( pszAnsi == NULL )
{
SysFreeString( strWide );
CLog::Print( LOG_ERROR, "%s new error(%d)", __FUNCTION__, GetLastError() );
return false;
}
WideCharToMultiByte( CP_ACP, 0, strWide, -1, pszAnsi, iLength, NULL, NULL );
strOutput = pszAnsi;
SysFreeString( strWide );
delete [] pszAnsi;
return true;
}
/**
* @ingroup SipPlatform
* @brief ANSI 문자열을 UTF-8 문자열로 변환한다. EUC-KR 문자열을 UTF-8 문자열로 변환한다.
* @param pszAnsi ANSI 문자열
* @param strOutput UTF-8 문자열을 저장할 변수
* @returns 성공하면 true 를 리턴하고 실패하면 false 를 리턴한다.
*/
bool AnsiToUtf8( const char * pszAnsi, std::string & strOutput )
{
BSTR strWide = NULL;
char* pszUtf8 = NULL;
int iLength;
int iAnsiLength = (int)strlen(pszAnsi) + 1;
iLength = MultiByteToWideChar( CP_ACP, 0, pszAnsi, iAnsiLength, NULL, NULL );
if( iLength == 0 )
{
CLog::Print( LOG_ERROR, "%s MultiByteToWideChar error(%d)", __FUNCTION__, GetLastError() );
return false;
}
strWide = SysAllocStringLen( NULL, iLength );
if( strWide == NULL )
{
CLog::Print( LOG_ERROR, "%s SysAllocStringLen error(%d)", __FUNCTION__, GetLastError() );
return false;
}
MultiByteToWideChar( CP_ACP, 0, pszAnsi, iAnsiLength, strWide, iLength );
iLength = WideCharToMultiByte( CP_UTF8, 0, strWide, -1, NULL, 0, NULL, NULL );
if( iLength == 0 )
{
SysFreeString( strWide );
CLog::Print( LOG_ERROR, "%s WideCharToMultiByte error(%d)", __FUNCTION__, GetLastError() );
return false;
}
pszUtf8 = new char[iLength];
if( pszUtf8 == NULL )
{
SysFreeString( strWide );
CLog::Print( LOG_ERROR, "%s new error(%d)", __FUNCTION__, GetLastError() );
return false;
}
WideCharToMultiByte( CP_UTF8, 0, strWide, -1, pszUtf8, iLength, NULL, NULL );
strOutput = pszUtf8;
SysFreeString( strWide );
delete [] pszUtf8;
return true;
}
#endif