forked from librae8226/gmsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
1759 lines (1575 loc) · 46.3 KB
/
util.c
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "version.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include "common.h"
#include "util.h"
#include "configfile.h"
#define IS_2BYTEWORD( _a_ ) ( (char)(0x80) <= (_a_) && (_a_) <= (char)(0xFF) )
//整个模块需要内联汇编优化
/*-----------------------------------------
仃勾及荼垫毛仇欠允
娄醒“ 手午及 侬 [ 涛 凳今木月冗
windows , macintosh , unix及升及垫手覆杀匹五月[
windows : \r\n
macintosh : \n\r
unix : \n
分匀凶井卅
macintosh : \r
匹允[
公木午仇及皿夫弘仿丞分午仃勾及 侬元扎卅仁化手
荼垫壅允仃升中中氏匹允井
-----------------------------------------*/
void chompex( char *buf )
{
while( *buf ){
if( *buf == '\r' || *buf == '\n' ){
*buf='\0';
}
buf++;
}
}
/*----------------------------------------
* 仃勾及1 侬毛潸曰轮仁
* 娄醒
* src 凳允月 侬
----------------------------------------*/
void chop( char* src )
{
/*
* 凶分凶氏卞 及 侬毛潸曰轮仁
*/
int length = strlen( src );
if( length == 0 ) return;
src[length-1] = '\0';
}
/*----------------------------------------
* 仃勾及 侬毛潸月[公及 侬毛 醒隙烂匹五月[
* 娄醒
* src 葭及 侬
* delim 绰轮允月 侬及 [ 反NULL 侬匹蔽匀化中月仪
----------------------------------------*/
void dchop( char* src , char* del)
{
int dellen, srclen;
int i;
BOOL delete=FALSE;
/*
* src及 欠井日譬屯化中仁[1 侬卞勾五 del 允屯化毛伙□皿仄化
* 域谯允木壬壅允[公仄化戚及 侬卞中仁[
*/
srclen = strlen( src );
dellen = strlen( del );
if( srclen == 0 || dellen == 0 ) return;
for( i = 0 ; i < dellen ; i ++ ){
if( src[srclen-1] == del[i] ){
delete = TRUE;
break;
}
}
/* 濠曰潸曰 */
if( delete )src[srclen - 1] = '\0';
}
/*----------------------------------------
* dchop及菅[ 赓及 侬毛潸月[公及 侬毛 醒隙烂匹五月[
* 娄醒
* src 葭及 侬
* delim 绰轮允月 侬及 [ 反NULL 侬匹蔽匀化中月仪
----------------------------------------*/
void pohcd( char* src , char* del)
{
int dellen, srclen;
int i;
BOOL delete=FALSE;
/*
* src及蟆井日譬屯化中仁[1 侬卞勾五 del 允屯化毛伙□皿仄化
* 域谯允木壬壅允[公仄化戚及 侬卞中仁[
*/
srclen = strlen( src );
dellen = strlen( del );
if( srclen == 0 || dellen == 0 ) return;
for( i = 0 ; i < dellen ; i ++ ){
if( src[0] == del[i] ){
delete = TRUE;
break;
}
}
/* 绰轮 */
if( delete )
/* 仇及戊疋□匹 NULL 侬手戊疋□今木月 */
for( i = 0 ; i < srclen ; i ++ )
src[i] = src[i+1];
}
/*----------------------------------------
* 侬 卞 侬互殖引木化中月井升丹井譬屯月[
* 娄醒
* src char* 譬屯月 侬
* include char* 殖引木化中月井升丹井及 侬 [
* 忒曰袄
* 岳 赓卞殖引木化中凶src及桦赭
* 撩 -1
----------------------------------------*/
int charInclude( char* src, char* include )
{
int i,j;
int srclen,includelen;
srclen = strlen( src );
includelen = strlen( include );
for( i = 0 ; i < srclen ; i ++ ){
for( j = 0 ; j < includelen ; j ++ )
if( src[i] == include[j] )
return i;
}
return -1;
}
/*------------------------------------------------------------
* EUC及2田奶玄 分仃毛戊疋□仄卅中strncpy
* freeBSD 及仿奶皮仿伉井日 strncpy 毛潸匀化五化}
* 公木毛手午卞荼瞻[*dest++ 午井银中凶中仃升}瑛宁赐辆卞愤褥互
* 卅中及匹}银匀化中卅中[井歹曰卞 分仃升}禾奶件正手银匀化化
* 井卅曰 卅皿夫弘仿丞
* 娄醒
* dest char* 戊疋□燮
* src char* 末□旦
* n size_t 赢今
* 忒曰袄
* dest
------------------------------------------------------------*/
char* strncpy2( char* dest, const char* src, size_t n )
{
if( n > 0 ){
char* d = dest;
const char* s = src;
int i;
for( i=0; i<n ; i++ ){
if( *(s+i) == 0 ){
/* 戊疋□仄云歹匀凶日 NULL 侬毛 木月 */
*(d+i) = '\0';
return dest;
}
if( *(s+i) & 0x80 ){
*(d+i) = *(s+i);
i++;
if( i>=n ){
*(d+i-1)='\0';
break;
}
*(d+i) = *(s+i);
}else
*(d+i) = *(s+i);
}
}
return dest;
}
/*----------------------------------------
* src 井日 dest 卞戊疋□允月[ 戊疋□燮及扔奶术毛譬屯月[
* 娄醒
* dest 戊疋□燮
* n 扔奶术
* src 葭及 侬
----------------------------------------*/
void strcpysafe( char* dest ,size_t n ,const char* src )
{
/*
* src 井日 dest 尺戊疋□允月.
* strcpy, strncpy 匹反 dest 方曰 戊疋□允月汹互
* 五中凛卞裟少午,丢乒伉陆失弁本旦互粟月.
* 仇木毛 什啃卞, strncpy 互丐月互 strlen( src ) 互 n 方曰
* 五中凛卞反, dest 及 互 NULL 侬午反卅日卅中.
*
* 仄凶互匀化 dest 及 五今方曰 src 及幻丹互赢中凛卞反
* n-1 匹 strncpy 毛允月. 凝今中凛反公及引引戊疋□允月
*
* n 互 及凛反云井仄仁卅月及匹 及凛反 窒手仄卅中[
*
*/
// Nuke +1 (08/25): Danger if src=0
if (!src) {
*dest = '\0';
return;
}
if( n <= 0 ) /* 窒手仄卅中 */
return;
/* 仇及凛鳔匹} n >= 1 动晓互瑁烂 */
/* NULL 侬毛哔 仄化 胜允月 */
else if( n < strlen( src ) + 1 ){
/*
* 田永白央互箫曰卅中及匹 n - 1(NULL 侬)
* 匹 strncpy 毛裟少
*/
strncpy2( dest , src , n-1 );
dest[n-1]='\0';
}else
strcpy( dest , src );
}
/*----------------------------------------
* src 井日 length 掏允[ 戊疋□燮及 五今毛譬屯月[
* 娄醒
* dest 戊疋□燮
* n 戊疋□燮及扔奶术
* src 戊疋□葭
* length 窒 侬戊疋□允月井
----------------------------------------*/
void strncpysafe( char* dest , const size_t n ,
const char* src ,const int length )
{
/*
* src 井日 dest 卞 length 戊疋□允月
* strcpy, strncpy 匹反 dest 方曰 戊疋□允月汹互
* 五中凛卞裟少午,丢乒伉陆失弁本旦互粟月.
* 仇及楮醒匹反}strlen( src ) 午 length 及凝今中幻丹
* ( 端卞戊疋□允月汹) 午 dest 及扔奶术毛 屯化}
* strcpysafe 午 元仪毛允月[
*/
int Short;
Short = min( strlen( src ) , length );
/* NULL 侬 毛哔 仄凶 胜 */
if( n < Short + 1 ){
/*
* 田永白央互箫曰卅中及匹 n - 1(NULL 侬)
* 匹 strncpy 毛裟少
*/
strncpy2( dest , src , n-1 );
dest[n-1]='\0';
}else if( n <= 0 ){
return;
}else{
/*
* 田永白央反蜗坌卞丐月及匹 Short 匹strncpy毛裟少
* 卅云 src 卞反 Short 及赢今 卞 NULL 互卅中及匹}
* dest 卞反 馨笛仄化云仁[
*/
strncpy2( dest , src , Short );
dest[Short]= '\0';
}
}
/*------------------------------------------------------------
* strcat毛兜蝈卞 蜇允月[赞中及匹} 卅桦宁毛轮中化银歹卅中仪[
* 娄醒
* src char* 葭卞卅月 侬
* size int src 及扔奶术( 欠卞升木分仃酸匀化中月井匹反
* 卅仁化田永白央蝈 )
* ap char* 尥仃笛尹月
* 忒曰袄
* src ( 木阂迕)
------------------------------------------------------------*/
char* strcatsafe( char* src, int size, char* ap )
{
strcpysafe( src + strlen( src ) , size - strlen(src), ap );
return src;
}
/*----------------------------------------
* 侬 及 毛 胜允月[ 元井升丹井仄井 烂仄卅中 by ringo
* char *s1 , *s2 : 胜允月 侬
* 忒曰袄
* 0 “ 云卅元
* 1 : 仇午卅月
* 仇木反NPC及 平旦玄荸 卞支仁分勾[
----------------------------------------*/
//待优化函数吉仔ADD
int strcmptail( char *s1 , char *s2 )
{
int i;
int len1 = strlen( s1 );
int len2 = strlen( s2 );
int ind1,ind2;
for(i=0;;i++){
ind1 = len1 - 1 - i;
ind2 = len2 - 1 - i;
if( ind1 < 0 || ind2 < 0 ) return 0;
if( s1[ind1] != s2[ind2] ) return 1;
}
}
/*----------------------------------------
* 侬 及 卞踏井木化中月帮醒毛必永玄允月[
* NPC及 平旦玄荸 卞支仁分勾[
* 凶分仄 卞中日氏 侬互丐月桦宁 骰允月[
* 耨痊互 切卅中失伙打伉术丞卞 凳[
* 娄醒
* hoge char* 荸 允月 侬
* 忒曰袄
* 荸 仄凶帮醒[
*
* “
* "ringo ( 吧噗吧噗星人 ) : 100"
* 及桦宁反100互井尹月[
* "ringo ( 吧噗吧噗星人 ) : 是100"
* 手100卞卅月[幻氏引卞NPC迕卅曰友[
----------------------------------------*/
#define DECIMALCHARNUM 10
#define FIGURECHARNUM 3
#define SPECIALCHARNUM 2
char *UTIL_decimalchar[DECIMALCHARNUM] = {"0","1","2",
"3", "4","5",
"6","7","8","9"};
char *UTIL_figurechar[FIGURECHARNUM] = {"","",""};
char *UTIL_specialchar[SPECIALCHARNUM] = {"",""};
/*
* 侬 井日}醒袄毛 允午 歹木月 坌毛 五请允[燮 及幻丹井日
* 茧仄化 五请允[euc迕[
*
* : in "这本书七万册300美金吗?"
* out "7万"
* 娄醒
* char *in: 侬
* char *out:请 侬
* int outsiz:请 侬 及田永白央扔奶术
* 忒曰袄
* 醒袄毛 允 坌互丐匀凶日公及 侬 及赢今}卅井匀凶日ㄟ
*/
static int findNumberString( char *in, char *out, int outsiz )
{
int len,i,j,k, findflag, numstrflag;
char tmp[3];
len = strlen(in);
if( len == 0 ) return 0;
numstrflag=0;
findflag = 0;
for( i=0,j=0;in[i]!='\0'&& j<outsiz-2;i++ ){
findflag = 0;
if( in[i] & 0x80 ){
/* EUC及桦宁 */
tmp[0] = in[i];
tmp[1] = in[i+1];
tmp[2] = '\0';
}else{
/* ASCII及桦宁 */
tmp[0] = in[i];
tmp[1] = '\0';
}
for( k=0;k<DECIMALCHARNUM;k++ ){
if( strstr( UTIL_decimalchar[k], tmp ) ){
numstrflag = 1;
findflag = 1;
}
}
for( k=0;k<FIGURECHARNUM;k++ ){
if( strstr( UTIL_figurechar[k], tmp ) ){
numstrflag = 1;
findflag = 1;
}
}
for( k=0;k<SPECIALCHARNUM;k++ ){
if( strstr( UTIL_specialchar[k], tmp ) ){
numstrflag = 1;
findflag = 1;
}
}
if( findflag ){
if( in[i] & 0x80 ){
out[j] = tmp[0];
out[j+1] = tmp[1];
j+=2;
i++;
}else{
out[j] = tmp[0];
j+=1;
}
continue;
}
if( numstrflag ){
out[j] = '\0';
return j;
}
}
if( findflag ){
out[j] = '\0';
return j;
}else{
return 0;
}
}
/*
* 侬 毛int卞 晶[
*
*/
int numstrIsKanji( char *str );
int decimalstrToInt( char *str );
int kanjistrToInt( char *str );
int textToInt( char *str )
{
char numstr[256];
if( !findNumberString(str,numstr,sizeof(numstr)) ) return 0;
if( numstrIsKanji(numstr) ) return kanjistrToInt(numstr);
else return decimalstrToInt(numstr);
}
/*
* 醒袄毛 允 侬 互} 贿挚卅及井壑钗挚卅及井毛
* 娄醒 允月 侬 [醒侬毛 允 侬 动陆反殖氏匹反中仃卅中[
* 忒曰袄 贿挚卅日ㄠ}壑钗挚卅日ㄟ
* str 互"一亿四千万。"及午五{忒曰袄1
* str 互"123"及午五{忒曰袄ㄟ
*
*/
int numstrIsKanji( char *str )
{
int i,j;
char tmp[3];
for( i=0;str[i]!='\0';i++ ){
if( str[i] & 0x80 ){
tmp[0] = str[i];
tmp[1] = str[i+1];
tmp[2] = '\0';
i++;
}else{
tmp[0] = str[i];
tmp[1] = '\0';
}
for( j=0;j<FIGURECHARNUM;j++ ){
if( strstr(UTIL_figurechar[j],tmp) ){
return 1;
}
}
for( j=0;j<SPECIALCHARNUM;j++ ){
if( strstr(UTIL_specialchar[j],tmp) ){
return 1;
}
}
}
return 0;
}
/*
* 蜗褡芊匹醒袄毛 仄凶 侬 毛int卞 晶
* char *str 侬
* 忒曰袄 侬 互 允醒袄
*/
int decimalstrToInt( char *str )
{
double val;
char tmp[3];
int i,j;
val = 0;
for( i=0;str[i]!='\0';i++ ){
if( str[i] & 0x80 ){
tmp[0] = str[i];
tmp[1] = str[i+1];
tmp[2] = '\0';
i++;
}else{
tmp[0] = str[i];
tmp[1] = '\0';
}
for( j=0;j<DECIMALCHARNUM;j++ ){
if( strstr(UTIL_decimalchar[j],tmp) ){
val = val*10+j;
}
}
}
if( val > 0x7fffffff ) return 0x7fffffff;
else return (int)val;
}
/*
* 挚匹 仄凶醒袄毛帮醒卞 晶
* char *str 醒袄毛 允 侬
* 忒曰袄 醒袄
*
* str互"一亿4千万?"及午五忒曰袄 140000000
*/
int kanjistrToInt( char *str )
{
double val,tmpval;
char tmp[3];
int num,i,j,numflag;
numflag = 0;
num = 1;
tmpval = 0;
val = 0;
for( i=0;str[i]!='\0';i++ ){
if( str[i] & 0x80 ){
tmp[0] = str[i];
tmp[1] = str[i+1];
tmp[2] = '\0';
i++;
}else{
tmp[0] = str[i];
tmp[1] = '\0';
}
for( j=0;j<SPECIALCHARNUM;j++ ){
if( strstr(UTIL_specialchar[j],tmp ) ){
if( numflag == 1 ) tmpval += num;
if( j == 0 ) val += tmpval*10000;
else if( j == 1 ) val += tmpval*100000000;
num = 1;
numflag = 0;
tmpval = 0;
goto nextchar;
}
}
for( j=0;j<FIGURECHARNUM;j++ ){
if( strstr(UTIL_figurechar[j],tmp) ){
if( j == 0 ) tmpval += num*10;
else if( j == 1 ) tmpval += num*100;
else if( j == 2 ) tmpval += num*1000;
num = 1;
numflag = 0;
goto nextchar;
}
}
for( j=0;j<DECIMALCHARNUM;j++ ){
if( strstr(UTIL_decimalchar[j],tmp) ){
num = j;
numflag = 1;
goto nextchar;
}
}
nextchar:
continue;
}
if( numflag == 1 ) tmpval += num;
val += tmpval;
if( val > 0x7fffffff ) return 0x7fffffff;
else return (int)val;
}
/*----------------------------------------
* 侬 及 卞踏井木化中月帮醒毛必永玄允月[
* NPC及 平旦玄荸 卞支仁分勾[
* 凶分仄 卞中日氏 侬互丐月桦宁 骰允月[
* 耨痊互 切卅中失伙打伉术丞卞 凳[
* 娄醒
* hoge char* 荸 允月 侬
* 忒曰袄
* 荸 仄凶帮醒[
*
* “
* "ringo ( 吧噗吧噗星人 ) : 100"
* 及桦宁反100互井尹月[
* "ringo ( 吧噗吧噗星人 ) : 是100"
* 手100卞卅月[幻氏引卞NPC迕卅曰友[
----------------------------------------*/
int texttoiTail( char *hoge)
{
return 0; /* 壬什匀化凶及匹仇丹仄凶[公及丹切楮醒仍午壅允[ */
#if 0
int len = strlen(hoge);
if( len == 0 )return atoi(hoge);
/* 井日茧仄化 赓卞醒侬毛苇勾仃月 */
while(--len && len >= 0){
if( isdigit(hoge[len]) )
break;
}
while( --len && len >= 0 ){
if( !isdigit(hoge[len]) )
break;
if( hoge[len]=='+' || hoge[len]=='-')
break;
}
return atoi(hoge+len+1);
#endif
#if 0
int f = 0;
for(i=len-1;i>=0;i--){
if( isdigit( hoge[i] ) ) f = 1;
if( (f == 1)
&& !( isdigit(hoge[i]) || hoge[i]=='+' || hoge[i]=='-') ){
return atoi( hoge + i + 1 );
}
}
return atoi(hoge);
#endif
}
/*----------------------------------------
* 侬 毛醒侬井升丹井民尼永弁仄化}醒侬分匀凶日
* 娄醒卞 木化忒允
* 娄醒
* arg 晶葭
* number 晶 毛医 允月赭
* base 湘醒
* type 娄醒及滇( CTYPE 反 common.h 卞烛蜕今木化中月 )
* 忒曰袄
* TRUE(1) 岳
* FALSE(0) strtol互敦僬卞反岳 仄卅井匀凶
* 仇及凛及number反strtol及忒曰袄匹丐月
----------------------------------------*/
BOOL strtolchecknum( char* arg , void* number,int base ,CTYPE type)
{
char* buf;
int num;
num = strtol( arg, &buf, base);
switch( type ){
case CHAR:
*(char*)number = (char)num;
break;
case SHORT:
*(short*)number = (short)num;
break;
case INT:
*(int*)number = num;
break;
case DOUBLE:
*(double*)number = (double)num;
break;
default:
break;
}
if( strlen( buf ) >= 1 )
/*
* 酸曰 侬互丐月午中丹仪反 晶仄五木化卅中午中丹仪卅及分[
*/
return FALSE;
return TRUE;
}
/*----------------------------------------
* src 井日 dels 匹隙烂仄凶 侬毛潸曰轮仁
* 田永弁旦仿永扑亘反巨旦弗□皿匹丐月仪卞镗啦[
* 勾引曰田永弁旦仿永扑亘反绰轮匹五卅中[
* 娄醒
* src 凳今木月 侬
* char* 绰轮允月 侬( 侬 匹隙烂第)
* 忒曰袄
* 卅仄
----------------------------------------*/
void deleteCharFromString( char* src , char* dels )
{
int index=0; /* 匹五丐互曰及 侬 匹及 index */
int delength; /* dels 及赢今毛涩烂允月(剂仄反簧蘸失永皿及啃 */
int i=0,j;/* i 反 src 毛伙□皿允月 醒 j 反 dels 毛伙□皿允月 醒 */
delength= strlen( dels );
while( src[i] != '\0' ){
if( src[i] == BACKSLASH )
/*
* 田永弁旦仿永扑亘卅及匹巨旦弗□皿允月
* 勾引曰戚及 侬反 椭瘀卞啖 允月
*/
src[index++] = src[++i]; /* substitute next char */
else{
for( j = 0 ; j < delength ; j ++ ){
if( src[i] == dels[j] )
/*
* 绰轮允月 侬分[方匀化 i 卞 1 毛凶允
*/
goto incrementi;
}
/*
* 壅允 卞隙烂今木化中卅中及匹戊疋□允月[
*/
src[index++] = src[i];
}
incrementi:
i++;
}
src[index] = '\0';
}
/*----------------------------------------
* src 井日 dels 匹隙烂仄凶 侬毛潸曰轮仁
* 巨旦弗□皿反卅中
* 娄醒
* src 凳今木月 侬
* char* 绰轮允月 侬( 侬 匹隙烂第)
* 忒曰袄
* 卅仄
----------------------------------------*/
void deleteCharFromStringNoEscape( char* src , char* dels )
{
int index=0; /* 匹五丐互曰及 侬 匹及 index */
int delength; /* dels 及赢今毛涩烂允月(剂仄反簧蘸失永皿及啃 */
int i=0,j;/* i 反 src 毛伙□皿允月 醒 j 反 dels 毛伙□皿允月 醒 */
delength= strlen( dels );
while( src[i] != '\0' ){
for( j = 0 ; j < delength ; j ++ ){
if( src[i] == dels[j] )
/*
* 绰轮允月 侬分[方匀化 i 卞 1 毛凶允
*/
goto incrementi;
}
/*
* 壅允 卞隙烂今木化中卅中及匹戊疋□允月[
*/
src[index++] = src[i];
incrementi:
i++;
}
src[index] = '\0';
}
/*------------------------------------------------------------
* 隙烂今木凶 侬毛隙烂今木凶 侬匹 五井尹月
* 娄醒
* src char* 凳今木月 侬
* oldc char 凳今木月 侬
* newc char 凳允月 侬
* 忒曰袄
* src
------------------------------------------------------------*/
char* replaceString( char* src, char oldc ,char newc )
{
char* cp=src;
do{
if( *cp == oldc ) *cp=newc;
}while( *cp++ );
return src;
}
typedef struct tagEscapeChar
{
char escapechar;
char escapedchar;
}EscapeChar;
static EscapeChar escapeChar[]=
{
{ '\n', 'n' },
{ ',', 'c' },
{ '|', 'z' },
{ '\\', 'y' },
};
char makeCharFromEscaped( char c )//add this function,because the second had it
{
int i;
for( i = 0; i < sizeof( escapeChar )/sizeof( escapeChar[0] ); i++ )
{
if( escapeChar[i].escapedchar == c )
{
c = escapeChar[i].escapechar;
break;
}
}
return c;
}
/*----------------------------------------
* makeEscapeString匹综岳今木凶 侬 毛葭卞 允
* 娄醒
* src char* 葭卞卅月 侬 [仇木互踏五赘月[
* 忒曰袄
* src 毛忒允[(楮醒毛 木仇卞仄支允中方丹卞)
----------------------------------------*/
char *makeStringFromEscaped( char* src )
{//ttom this function all change,beside copy from the second
int i;
// CoolFish: Fix bug 2001/10/13
// int srclen = strlen( src );
int srclen = 0;
int searchindex=0;
// CoolFish: Fix bug 2001/10/13
if (!src) return NULL;
srclen = strlen(src);
for( i = 0; i < srclen; i ++ )
{
// for 2Byte Word
if( IS_2BYTEWORD( src[i] ) ){
src[searchindex++] = src[i++];
src[searchindex++] = src[i];
}else
if( src[i] == '\\' )
{
// 戚及 侬卞垫仁
i++;
src[searchindex++] = makeCharFromEscaped( src[i] );
}
else
{
src[searchindex++] = src[i];
}
}
src[searchindex] = '\0';
return src;
}
char* makeEscapeString( char* src , char* dest, int sizeofdest)
{ //ttom this function all change, copy from the second
int i;
int srclen = 0;
int destindex=0;
// CoolFish: Fix bug 2001/10/13
if (!src) return NULL;
srclen = strlen(src);
BOOL dirty;
int j;
char escapechar='\0';
for( i = 0 ; i < srclen ; i ++ ){
dirty=FALSE;
if( destindex + 1 >= sizeofdest )break;
if( IS_2BYTEWORD( src[i] ) ){
if( destindex + 2 >= sizeofdest )break;
dest[destindex] = src[i];
dest[destindex+1] = src[i+1];
destindex += 2;
i ++;
continue;
}
for( j = 0; j<sizeof(escapeChar)/sizeof(escapeChar[0]); j++){
if( src[i] == escapeChar[j].escapechar ){
dirty=TRUE;
escapechar= escapeChar[j].escapedchar;
break;
}
}
if( dirty == TRUE ){
if( destindex + 2 < sizeofdest ){
dest[destindex] = '\\';
dest[destindex+1] = escapechar;
destindex+=2;
dirty=TRUE;
continue;
}else{
dest[destindex] = '\0';
return dest;
}
}else{
dest[destindex] = src[i];
destindex++;
}
}
dest[destindex] = '\0';
return dest;
}
//this function copy all from the second
char * ScanOneByte( char *src, char delim ){
// Nuke
if (!src) return NULL;
// 侬 互卅仁卅月引匹腹绸
for( ;src[0] != '\0'; src ++ ){
// 蝈剩及ㄠ田奶玄 井升丹井毛民尼永弁
if( IS_2BYTEWORD( src[0] ) ){
// 蝈剩分[公及桦宁反ㄠ田奶玄芴坌卞褡引六月[
// 凶分仄ㄠ田奶玄仄井卅中桦宁反公丹仄卅中
if( src[1] != 0 ){
src ++;
}
continue;
}
// 剩分匀凶[仇仇匹覆擂及 侬午 胜
if( src[0] == delim ){
return src;
}
}
// 伙□皿 仃凶日苇勾井日卅井匀凶[
return NULL;
}
/*----------------------------------------
* delim 匹隙烂今木凶 侬 毛嗉濠曰 侬午仄化
* index 及 毛 月[index反1匹反元引月[
* 侬 酷 [
* 娄醒
* src 葭及 侬
* delim 犯伉立正午卅月 侬 [ 反NULL 侬匹蔽匀化中月仪
* index 窒 及 毛潸月井
* buf 瑛绊 侬 毛伞 允月桦赭尺及禾奶件正□
* buflen 瑛绊 侬 毛伞 允月桦赭及扔奶术
* 忒曰袄
* 丐匀凶日 TRUE(1);
* 卅井匀凶日 FALSE(0);
* ex
* getStringFromIndexWithDelim( "aho=hoge","=",1,buf,sizeof(buf) );
* buf ... aho
*
* getStringFromIndexWithDelim( "aho=hoge","=",2,buf,sizeof(buf) );
* buf ... hoge
* 仇木手 户日木月[
*
* getStringFromIndexWithDelim( "aho=hoge","=",3,buf,sizeof(buf) );
* 忒曰袄 FALSE
----------------------------------------*/
BOOL getStringFromIndexWithDelim_body( char* src ,char* delim ,int index,
char* buf , int buflen ,
char *file, int line )
{//ttom this function all change,copy from the second
int i; /* 伙□皿 醒 */
int length =0; /* 潸曰请仄凶 侬 及赢今 */
int addlen=0; /* 箫今木月赢今 */
int oneByteMode = 0; /* ㄠ田奶玄乒□玉井" */
char* last;
if( strlen( delim ) == 1 ){ // 腹绸互ㄠ田奶玄卅日ㄠ田奶玄乒□玉卞允月
oneByteMode = 1;// 公及端ㄡ田奶玄 侬反民尼永弁仄卅中
}
for( i = 0 ; i < index ; i ++ ){
src += addlen;/* 心勾井匀凶赢今毛箫允 */
if( oneByteMode ){
// ㄠ田奶玄乒□玉分匀凶日仇切日匹腹绸