-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnsiString.cpp
596 lines (505 loc) · 12.6 KB
/
AnsiString.cpp
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
#include "AnsiString.h"
#ifdef USE_DEPRECATED
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#else
//#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#endif
#ifndef SEEK_SET
#define SEEK_SET 0 /* set file offset to offset */
#endif
#ifndef SEEK_CUR
#define SEEK_CUR 1 /* set file offset to current plus offset */
#endif
#ifndef SEEK_END
#define SEEK_END 2 /* set file offset to EOF plus offset */
#endif
#define MEMCPY memcpy
#ifdef _WIN32
wchar_t *wstr(const char *utf8);
#endif
void memcpy32(void *dest, void *src, int bcount) {
if (bcount < 4) {
memcpy(dest, src, bcount);
return;
}
int blocks = bcount / 4;
bcount %= 4;
long *ldest = (long *)dest;
long *lsrc = (long *)src;
void *end = lsrc + blocks;
label1:
*ldest++ = *lsrc++;
if (lsrc != end)
goto label1;
if (bcount)
memcpy(ldest, lsrc, bcount);
}
AnsiString::AnsiString() {
Data = NULL;
_DATA_SIZE = 0;
_LENGTH = 0;
}
AnsiString::AnsiString(const char *value) {
Data = NULL;
_LENGTH = 0;
_DATA_SIZE = 0;
operator=(value);
}
AnsiString::AnsiString(long i) {
Data = NULL;
_LENGTH = 0;
_DATA_SIZE = 0;
operator=(i);
}
AnsiString::AnsiString(int i) {
Data = NULL;
_LENGTH = 0;
_DATA_SIZE = 0;
operator=((long)i);
}
AnsiString::AnsiString(char c) {
Data = NULL;
_LENGTH = 0;
_DATA_SIZE = 0;
operator=(c);
}
AnsiString::AnsiString(double d) {
Data = NULL;
_LENGTH = 0;
_DATA_SIZE = 0;
operator=(d);
}
AnsiString::AnsiString(const AnsiString& S) {
Data = NULL;
_LENGTH = 0;
_DATA_SIZE = 0;
if (S._LENGTH)
this->LoadBuffer(S.Data, S._LENGTH);
}
void AnsiString::operator=(const char *value) {
size_t len;
if (Data)
Data[0] = 0;
_LENGTH = 0;
if (value) {
len = strlen(value);
_LENGTH = len;
if (len) {
if (len + 1 >= _DATA_SIZE) {
_DATA_SIZE = ((len + 1) / BLOCK_SIZE) * BLOCK_SIZE + BLOCK_SIZE;
Data = (char *)realloc(Data, _DATA_SIZE);
}
MEMCPY(Data, value, len + 1);
}
}
}
void AnsiString::operator=(char *value) {
operator=((const char *)value);
}
void AnsiString::operator=(const AnsiString& S) {
size_t len;
if (Data)
Data[0] = 0;
_LENGTH = 0;
char *other_data = S.Data;
len = S._LENGTH;
if (len) {
_LENGTH = len;
if (len + 1 >= _DATA_SIZE) {
if (Data)
free(Data);
_DATA_SIZE = ((len + 1) / BLOCK_SIZE) * BLOCK_SIZE + BLOCK_SIZE;
Data = (char *)malloc(_DATA_SIZE);
}
MEMCPY(Data, other_data, len + 1);
}
}
void AnsiString::operator=(char c) {
char buffer[2];
buffer[0] = c;
buffer[1] = 0;
operator=(buffer);
}
void AnsiString::operator=(long i) {
char buffer[MAX_DECIMALS];
//operator=(ltoa(i,buffer,10));
// pt linux !
sprintf(buffer, "%li", i);
operator=(buffer);
}
void AnsiString::operator=(double d) {
char buffer[MAX_DECIMALS];
sprintf(buffer, "%.30g", d);
//_gcvt(d,MAX_DECIMALS-10,buffer);
int len = strlen(buffer);
if (((len > 1) && ((buffer[len - 1] == '.') || (buffer[len - 1] == ','))))
buffer[len - 1] = 0;
operator=(buffer);
}
void AnsiString::operator=(int i) {
operator=((long)i);
}
int AnsiString::operator <(char *str) {
if ((Data) && (str)) {
if (strcmp(Data, str) == -1)
return 1;
else
return 0;
}
return 0;
}
int AnsiString::operator >(char *str) {
if ((Data) && (str)) {
if (strcmp(Data, str) == 1)
return 1;
else
return 0;
}
return 0;
}
int AnsiString::operator >=(char *str) {
if ((Data) && (str)) {
if (strcmp(Data, str) >= 0)
return 1;
else
return 0;
}
return 0;
}
int AnsiString::operator <=(char *str) {
if ((Data) && (str)) {
if (strcmp(Data, str) <= 0)
return 1;
else
return 0;
}
return 0;
}
int AnsiString::operator==(char *str) {
if ((Data) && (str)) {
if (strcmp(Data, str))
return 0;
else
return 1;
}
if ((!Data) && (str) && (!str[0]))
return 1;
if ((!str) && (Data) && (!Data[0]))
return 1;
if ((!Data) && (!str))
return 1;
return 0;
}
int AnsiString::operator !=(char *str) {
return !operator==(str);
}
int AnsiString::operator==(AnsiString& S) {
return operator==(S.c_str());
}
int AnsiString::operator!=(AnsiString& S) {
return operator!=(S.c_str());
}
int AnsiString::operator >(AnsiString& S) {
return operator >(S.c_str());
}
int AnsiString::operator <(AnsiString& S) {
return operator <(S.c_str());
}
int AnsiString::operator >=(AnsiString& S) {
return operator >=(S.c_str());
}
int AnsiString::operator <=(AnsiString& S) {
return operator <=(S.c_str());
}
AnsiString::~AnsiString(void) {
/*if (Data)
* delete[] Data;*/
if (Data)
free(Data);
}
AnsiString::operator char *() {
return c_str();
}
char *AnsiString::c_str() {
if ((!Data) || (!_LENGTH))
return NULL;
return Data;
}
void AnsiString::operator +=(char *value) {
operator+=((const char *)value);
}
void AnsiString::operator +=(const char *value) {
char *newdata;
size_t len;
size_t len_value;
if ((Data) && (value)) {
len = _LENGTH;
len_value = strlen(value);
_LENGTH = len + len_value;
if (_DATA_SIZE < _LENGTH + 1) {
_DATA_SIZE = ((_LENGTH + 1) / BLOCK_SIZE) * BLOCK_SIZE + BLOCK_SIZE;
Data = (char *)realloc(Data, _DATA_SIZE);
MEMCPY(Data + len, value, len_value + 1);
} else
MEMCPY(Data + len, value, len_value + 1);
} else if ((!_LENGTH) && (value))
operator=(value);
}
void AnsiString::operator +=(AnsiString& S) {
char *newdata;
size_t len;
size_t len_value = S.Length();
if (!len_value)
return;
if ((Data) && (len_value)) {
//len=strlen(Data);
len = _LENGTH;
//len_value=S._LENGTH;
_LENGTH = len + len_value;
if (_DATA_SIZE < _LENGTH + 1) {
_DATA_SIZE = ((_LENGTH + 1) / BLOCK_SIZE) * BLOCK_SIZE + BLOCK_SIZE;
/*newdata=new char[_DATA_SIZE];
* MEMCPY(newdata,Data,len);
* MEMCPY(newdata+len,S.Data,len_value+1);
* delete[] Data;
* Data=newdata;*/
Data = (char *)realloc(Data, _DATA_SIZE);
//MEMCPY(Data+len,S.Data,len_value+1);
} //else
MEMCPY(Data + len, S.c_str(), len_value + 1);
} else
operator=(S);
//operator+=(S.Data);
}
void AnsiString::operator +=(char c) {
/*char buffer[2];
* buffer[0]=c;
* buffer[1]=0;
* operator+=(buffer);*/
if (_LENGTH + 2 < _DATA_SIZE) {
Data[_LENGTH++] = c;
Data[_LENGTH] = 0;
} else {
_DATA_SIZE += BLOCK_SIZE;
/*char *newdata=new char[_DATA_SIZE];
* newdata[_LENGTH++] = c;
* newdata[_LENGTH] = 0;
* if (Data) {
* MEMCPY(newdata,Data,_LENGTH-1);
* delete[] Data;
* }
* Data=newdata;*/
Data = (char *)realloc(Data, _DATA_SIZE);
Data[_LENGTH++] = c;
Data[_LENGTH] = 0;
}
}
char AnsiString::operator [](unsigned long index) {
size_t len;
if (Data) {
//len=strlen(Data);
len = _LENGTH;
if (index < len)
return Data[index];
}
return 0;
}
char AnsiString::operator [](int index) {
size_t len;
if (Data) {
//len=strlen(Data);
len = _LENGTH;
if ((index < len) && (index >= 0))
return Data[index];
}
return 0;
}
long AnsiString::ToInt() {
if (Data)
return atol(Data);
/*long i=0;
* sscanf(Data, "%li", &i);
* return i;*/
return 0;
}
double AnsiString::ToFloat() {
if (Data)
return atof(Data);
return 0;
}
AnsiString AnsiString::operator +(AnsiString& S) {
//AnsiString ret(Data);
// bug corectat de pe 15 ianuarie 2006
// problema : Daca data arata de forma "abcde\0\0\0\0" si concatenam un sir ...
// nu era bine, ca-l punea imediat dupa e, in loc sa-l puna dupa al 4-lea 0
AnsiString ret = *this;
if (S._LENGTH)
ret += S;
return ret;
}
AnsiString AnsiString::operator+(char *value) {
if (_LENGTH) {
AnsiString ret = *this;
ret += value;
return ret;
} else
return AnsiString(value);
}
AnsiString AnsiString::operator+(const char *value) {
if (_LENGTH) {
AnsiString ret = *this;
ret += value;
return ret;
} else
return AnsiString(value);
}
int AnsiString::LoadFile(char *filename) {
FILE *in;
long size;
#ifdef _WIN32
wchar_t *fname = wstr(filename);
if (fname) {
in = _wfopen(fname, L"rb");
free(fname);
} else
in = fopen(filename, "rb");
#else
in = fopen(filename, "rb");
#endif
if (in) {
fseek(in, 0, SEEK_END);
size = ftell(in);
fseek(in, 0, SEEK_SET);
if (Data)
free(Data);
_DATA_SIZE = ((size + 1) / BLOCK_SIZE) * BLOCK_SIZE + BLOCK_SIZE;
Data = (char *)malloc(_DATA_SIZE);
Data[size] = 0;
fread(Data, size, 1, in);
fclose(in);
_LENGTH = size;
return 0;
}
return -1;
}
int AnsiString::SaveFile(char *filename) {
FILE *in;
size_t len;
int ret = -1;
#ifdef _WIN32
wchar_t *fname = wstr(filename);
if (fname) {
in = _wfopen(fname, L"wb");
free(fname);
} else
in = fopen(filename, "wb");
#else
in = fopen(filename, "wb");
#endif
if (in) {
if (Data) {
//len=strlen(Data);
len = _LENGTH;
if (fwrite(Data, len, 1, in) == len)
ret = 0;
} else
ret = 0;
fclose(in);
return ret;
}
return ret;
}
long AnsiString::Length() {
if (!Data)
return 0;
//return (long)strlen(Data);
return _LENGTH;
}
long AnsiString::Pos(AnsiString substr) {
long result = -1;
if ((!Data) || (!substr.Data))
return result;
char *ptr = strstr(Data, substr.c_str());
if (ptr)
result = ptr - Data + 1;
return result;
}
void AnsiString::LoadBuffer(const char *buffer, int size, int offset) {
if (Data)
free(Data);
_LENGTH = size;
_DATA_SIZE = ((size + 1) / BLOCK_SIZE) * BLOCK_SIZE + BLOCK_SIZE;
Data = (char *)malloc(_DATA_SIZE);
if (offset) {
for (int i = 0; i < size; i++)
Data[i] = buffer[offset + i];
} else
MEMCPY(Data, buffer, size);
Data[size] = 0;
}
int AnsiString::Serialize(FILE *out, int type) {
unsigned char uClen = (unsigned char)_LENGTH;
unsigned short uSlen = (unsigned short)_LENGTH;
switch (type) {
case SERIALIZE_8BIT_LENGTH:
fwrite(&uClen, sizeof(uClen), 1, out);
break;
case SERIALIZE_16BIT_LENGTH:
fwrite(&uSlen, sizeof(uSlen), 1, out);
break;
default:
fwrite(&_LENGTH, sizeof(_LENGTH), 1, out);
break;
}
fwrite(Data, _LENGTH, 1, out);
return 1;
}
int AnsiString::Unserialize(FILE *in, int type) {
long len;
unsigned char uClen = (unsigned char)_LENGTH;
unsigned short uSlen = (unsigned short)_LENGTH;
switch (type) {
case SERIALIZE_8BIT_LENGTH:
if (!fread(&uClen, sizeof(uClen), 1, in))
return -1;
len = uSlen;
break;
case SERIALIZE_16BIT_LENGTH:
if (!fread(&uSlen, sizeof(uSlen), 1, in))
return -1;
len = uSlen;
break;
default:
if (!fread(&len, sizeof(len), 1, in))
return -1;
break;
}
if (len) {
if (Data)
free(Data);
_LENGTH = len;
_DATA_SIZE = ((len + 1) / BLOCK_SIZE) * BLOCK_SIZE + BLOCK_SIZE;
Data = (char *)malloc(_DATA_SIZE);
fread(Data, len, 1, in);
Data[len] = 0;
}
return 1;
}
void AnsiString::LinkBuffer(char *buffer, int size) {
if (Data)
free(Data);
if (size < 0)
size = 0;
_LENGTH = size;
_DATA_SIZE = size;
if (size)
Data = buffer;
else
Data = 0;
}