-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathystring.h
More file actions
520 lines (421 loc) · 11.8 KB
/
Copy pathystring.h
File metadata and controls
520 lines (421 loc) · 11.8 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
#ifndef _MB_YSTRING_H
#define _MB_YSTRING_H
/*
#ifdef _WIN32
extern "C"
// This function is a MSVC internal function. It is being used as there -
// is no vsscanf in MSVC 2003
//int __cdecl _input ( FILE *pStream, const char *format, va_list pArglist);
int __cdecl _input_l (FILE *infile, const char *format, _locale_t plocinfo, va_list arglist );
#endif //_WIN32
*/
#include "ydefines.h"
#include <stdlib.h>
#include <string.h>
#include <cstdarg>
#include <ctype.h>
class wyString {
public:
wyString(){
vInit();
}
wyString(size_t init_bufsize) {
vInit();
buf_size_ = init_bufsize;
data_ =(yChar*)calloc(buf_size_, 1);
}
wyString(yCChar * str) {
vInit();
Set(str, strlen(str));
}
wyString(yCWChar * str) {
vInit();
Set(str);
}
virtual ~wyString() {
FreeBuffer();
}
void FreeBuffer() {
if(data_) {
free(data_);
}
if(utf8_data_) {
free(utf8_data_);
}
if(wide_data_) {
free(wide_data_);
}
vInit();
}
// readers
size_t GetLen() {
return len_;
}
yChar* GetString() {
return (yChar *) (data_ ? data_ : "");
}
yCChar* GetString() const {
return (yCChar *) (data_ ? data_ : "");
}
yInt32 GetAsInt32() {
return atoi(GetString());
}
double GetAsDouble() {
return atof(GetString());
}
yInt64 GetAsInt64() {
return AToI64(GetString());
}
yUInt64 GetAsUInt64() {
return AToUI64();
}
yInt32 StrICmp(wyString * str) {
return wyString::StrICmp(this->GetString(), str->GetString());
}
yInt32 StrCmp(wyString * str) {
return wyString::StrCmp(this->GetString(), str->GetString());
}
yInt32 StrCmp(yChar * str) {
return wyString::StrCmp(this->GetString(), str);
}
yInt32 StrCmp(yCChar * str) {
return wyString::StrCmp(this->GetString(), str);
}
yInt32 StrICmp(yChar * str) {
return wyString::StrICmp(this->GetString(), str);
}
static yBool isControlCharacter(yChar ch) {
return ch > 0 && ch <= 0x1F;
}
static yInt32 StrICmp(yCChar* str1, yCChar* str2) {
#ifdef _WIN32
return stricmp (str1, str2);
#else
return strcasecmp (str1, str2);
#endif //_WIN32
}
static yInt32 StrCmp(yCChar* str1, yCChar* str2) {
return strcmp (str1, str2);
}
static yInt32 StrNCmp(yCChar* str1, yCChar* str2, yInt32 pLen) {
return strncmp (str1, str2, pLen);
}
static yInt32 StrNICmp(yCChar* str1, yCChar* str2, yInt32 pLen) {
#ifdef _WIN32
return strnicmp (str1, str2, pLen);
#else
return strncasecmp (str1, str2, pLen);
#endif //_WIN32
}
static yChar* StrCpy(yChar* dest, yCChar* src) {
//ASSERT(dest);
//ASSERT(src);
strcpy(dest, src);
return dest;
}
static yChar* StrNCpy(yChar* dest, yCChar* src, size_t size) {
//ASSERT(dest);
//ASSERT(src);
strncpy(dest, src, size);
return dest;
}
yInt32 StrNICmp(wyString* str, size_t count) {
return StrNICmp(this->GetString(), str->GetString(), count);
}
yInt32 StrNICmp(yChar* str, size_t count) {
return StrNICmp(this->GetString(), str, count);
}
static yInt32 StrNICmp(yCChar* str1, yCChar* str2, size_t count) {
#ifdef _WIN32
return strnicmp(str1, str2, count);
#else
return strncasecmp(str1, str2, count);
#endif //_WIN32
}
yBool IsEqual(yChar* str) {
return(StrICmp(str)== 0);
}
yBool IsEmpty() {
return(len_ == 0);
}
static yInt32 AToI32(const yChar* str) {
return atoi(str);
}
static yInt64 AToI64(const yChar* str) {
#ifdef _WIN32
return _atoi64(str);
#else
return atoll(str);
#endif //_WIN32
}
yInt64 AToI64() {
#ifdef _WIN32
return _atoi64(data_);
#else
return atoll(data_);
#endif //_WIN32
}
yUInt64 AToUI64() {
yUInt64 convval;
if (!data_) {
return 0;
}
#ifdef _WIN32
convval = _strtoui64(data_, NULL, 10);
#else
convval = strtoull(data_, NULL, 10);
#endif
return convval;
}
static double AToDouble(const yChar* str) {
return atof(str);
}
double AToDouble() {
return atof(data_);
}
// returns a char at a specified position
// Parameters
// index: char position
// pchar: chart at index[OUT]
// Returns
// TRUE if index is valid, FALSE otherwise
yBool GetCharAt(yUInt32 index, yChar *pchar) {
if(index > len_) {
return FALSE;
} else {
*pchar = data_[index];
return TRUE;
}
}
// returns a char at a specified position
// Parameters
// index: char position
// Returns
// char at index if index is valid, '\0' otherwise
yChar GetCharAt(yUInt32 index) {
if(index > len_) {
return YNULCHAR;
}
return data_[index];
}
yBool SetCharAt(yUInt32 index, yChar ch) {
if(index >= len_) {
return FALSE;
}
data_[index] = ch;
return TRUE;
}
yInt32 Strip(yInt32 size);
wyString* Dup();
//Using iconv to ignore wrong utf8 characters
void FixUTF8();;
//write formatted output using a pointer to a list of arguments.
static yInt32 VsnPrintf(yChar* str, size_t size, const yChar* format, va_list ap);
// writers
void SetEmpty() {
if(buf_size_) *data_ = 0;
len_ = 0;
}
void Set(yCChar* buf, size_t pLen = vAllBitsOne);
void Set(wyString* src);
void Set(const yWChar* src);
void Set(yInt64 int_data) {
SetEmpty();
Add(int_data);
}
void AddUInt64(yUInt64 int_data);
// adds a buffer to the existing buffer
// Parameters
// buf: buffer to add
// pLen: buffer len to add
// Returns
// void
void Add(yCChar * buf, size_t pLen = vAllBitsOne);
void Add(const yWChar * src);
static yBool IsValidUtf8Buffer(yCByte *bytes, size_t len=-1);
yBool IsValidUtf8Buffer();
// adds a string object to the existing buffer
// Parameters
// str: string object to add
// Returns
// void
void Add(wyString* str);
// Helps to add a integer value to the buffer
// Parameters
// int_data: int data
// Returns
// void
void Add(yInt32 int_data);
// Helps to add a integer value to the buffer
// Parameters
// int_data: int data
// Returns
// void
void Add(yInt64 int_data);
void Add(yULong int_data);
// Adds the buffer, here the parameter supports formatstring
// Parameters
// format_str: format string
// Returns
// void
void AddSprintf(yCChar* format_str, ...);
// Sets the buffer, here the parameter supports formatstring
// Parameters
// format_str: format string
// Returns
// void
void Sprintf(yCChar* format_str, ...);
// Copies a string of specidfied length from a string object
// Parameters
// str: the source string object
// pMax: length
// Returns
// void
void StrNCpy(wyString * str, size_t pMax);
void StrNCpy(yChar* str, size_t pMax);
// strips up to a token(including token)
// Parameters
// token: token to find and strip
// new_string: the string upto token[OUT]
// Returns
// TRUE if not empty, FALSE otherwise
yBool StripToken(yChar* token, wyString* new_string);
// skips the first white spaces and next token from the buffer
// Returns
// TRUE always
yBool SkipToken();
// helps to Escape a url
// Parameters
// preserve_reserved: reserved characters can only be used in certain places
// Returns
// void
void UnEscapeURL();
// helps to Escape a url
// Parameters
// preserve_reserved: reserved characters can only be used in certain places
// Returns
// void
void EscapeURL(yBool preserve_reserved = FALSE);
// fills with str bufffer repeat times
// Parametrs
// str: bufffer to fill
// prepeat: repeat count
// Returns
// Void
void Fill(yChar* str, yUInt32 repeat);
// finds a string with out considering the case
// Parameters
// pToFind: string to find
// Returns
// position if found, -1 otherwise
size_t FindI(yCChar* pToFind, yUInt32 pos = 0);
// finds a string by considering the case
// Parameters
// pToFind: string to find
// Returns
// position if found, -1 otherwise
size_t Find(yCChar* pToFind);
static size_t Find(yCChar * from_, yCChar* to_find);
// converts all buffer to lower case
void ToLower();
void ToUpper();
// Sets the buffer, here the parameter supports formatstring
// Parameters
// format_str: format string
// va_list: argument list
// Returns
// void
void vSprintf(yCChar* format_str, va_list va_list);
// sscanf with the variable argument list specified directly
yInt32 vSscanf(yCChar* format_str, va_list va_list);
// Read formatted data from a string
// Parameters
// format_str: format string
// Returns
// the number of fields successfully converted and assigned
yInt32 Sscanf(yCChar* format_str, ...);
// removes pchar(single/consecutive occurence) from the left of the buffer,
// Parameters
// pchar : Char to remove
// Returns
// void
void StripLChar(yChar pchar);
void StripLChar(yInt32 size);
void ReplaceTabWithSpace();
// strips left white space
void StripLWhiteSpace();
// strips right white space
void StripRWhiteSpace();
// strips white spaces from bith sides of the buffer
void StripWhiteSpace();
// helps to escape slash in the buffer
void EscapeSlash();
// helps to escape HTML entities in the buffer
void EscapeHTMLEntities();
void UnEscapeHTMLEntities();
//http://tools.ietf.org/html/rfc4627
// helps to escape as javascript escape
void JsonValueEscape();
// converts the buffer to utf8 and sends
yChar* GetAsUTF8(yCodePage in_codepage = WY_CODEPAGE_ASCII, yInt32 *length = NULL);
// wide char to test
yWChar* GetAsWideChar(yUInt32 *pLen = NULL);
yInt32 Replace(yInt32 startpos, yUInt32 originalstrlen, yCChar *replacestr, yUInt32 replacestrlen = 0);
yInt32 FindIAndReplace(yCChar* findstr, yCChar* replacestr);
//return the UTF8 equivalent of the string, if the platform is windows
yChar* IfWindowsGetAsUTF8();
// Specific to apple. Used to remove unwanted/corrupted chars from 'us-ascii' charset
void FixAscii();
static yCChar* StrIStr(yCChar* buffer, yCChar *to_find);
void TrimMatchingCharFromEnd(yCChar match);
yBool UrlDecode();
private:
void vInit() {
// should only be called from constructors
// otherwise will leak momory because its losing data_
buf_size_ = 0;
data_ = NULL;
utf8_data_ = NULL;
len_ = 0;
wide_data_ = NULL;
}
operator yChar*() {
return GetString();
}
yChar* data_; // internal data holding pointer
yChar* utf8_data_; // internal data holding pointer
size_t len_; // current lint in widechars(not size in bytes!)
size_t buf_size_; // in bytes
yWChar* wide_data_;
//checks char is always safe for putting in a URL:
// Parameters
// pchar: char to check
// Returns
// 1 if safe, 0 otherwise
yInt32 IsUrlSafe(yChar pchar);
// checks whether the char is included in the reserved list
// Parameters
// pchar: char to check
// Returns
// 1 if reserved, 0 otherwise
yInt32 IsUrlReservedSafe(yChar pchar);
// Helper function to URL encode the buffer
// Parameters
// pFrom: source
// pTo: result buffer[OUT]
// preserve_reserved: reserved characters can only be used in certain places
// Returns
// void
void UrlEncode(const yChar* pFrom, yChar* pTo, yBool preserve_reserved);
static const size_t vAllBitsOne;
// declare the default constructor which are not supported
// in the private section so we won't accidently use this
wyString(const wyString& p) {
UNREFERENCED_PARAMETER(p);
};
wyString& operator=(const wyString& p) {
UNREFERENCED_PARAMETER(p);
return *this;
}
};
#endif