-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiniparse.c
More file actions
executable file
·1192 lines (994 loc) · 38.6 KB
/
Copy pathiniparse.c
File metadata and controls
executable file
·1192 lines (994 loc) · 38.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
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
/*****************************************************************************\
* *
* FILENAME : iniparse.c *
* *
* --------------------------------------------------------------------------- *
* *
* DESCRIPTION : Contains diverse character buffer parsing functions *
* for a buffer that uses the data composition format. *
* *
* --------------------------------------------------------------------------- *
* *
* COPYRIGHT : (c) 2026 Dipl.-Ing. Klaus Lux (Aachen, Germany) *
* *
* --------------------------------------------------------------------------- *
* *
* ORIGIN : https://github.com/klux21/composition_parser *
* *
* --------------------------------------------------------------------------- *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgment in the product documentation would be *
* appreciated but is not required. *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
* *
\*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#pragma warning( disable : 4996)
#include <io.h>
#include <malloc.h>
#define strncasecmp strnicmp
#define stat _stat64
#define fstat _fstat64
#define lseek _lseeki64
#else /* ! _WIN32 */
#include <unistd.h>
#ifndef _cdecl
#define _cdecl
#endif
#ifndef O_BINARY
#define O_BINARY 0
#endif
#endif /* #ifdef _WIN32 */
#include <stdint.h>
#include <iniparse.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#endif
#define vPrintLog(...)
static const uint8_t ini_char_type[256] = { 0x08,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x02,0x02,0x02, 0x02,0x02,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* ! " # $ % & ' ( ) * + , - . / */
0x03,0x00,0x10,0x04, 0x00,0x00,0x00,0x10, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x04, 0x00,0x01,0x00,0x00,
/* @ A B C D E F G H I J K L M N O */
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* P Q R S T U V W X Y Z [ \ ] ^ _ */
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x08, 0x10,0x08,0x00,0x00,
/* ` a b c d e f g h i j k l m n o */
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
/* p q r s t u v w x y z { | } ~ */
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01, 0x00,0x08,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00};
#define IS_INI_CHAR(x) (!ini_char_type[(uint8_t)(x)]) /* characters without any special function */
#define IS_INI_ARG(x) (ini_char_type[(uint8_t) (x)] & 0x01) /* entry argument = { */
#define IS_INI_BLANK(x) (ini_char_type[(uint8_t) (x)] & 0x02) /* white spaces */
#define IS_INI_COMMENT(x) (ini_char_type[(uint8_t) (x)] & 0x04) /* comment start markers ; # */
#define IS_INI_SECT_END(x) (ini_char_type[(uint8_t) (x)] & 0x08) /* section end indicators [ ] } '\0' */
#define IS_INI_QUOTE(x) (ini_char_type[(uint8_t) (x)] & 0x10) /* backslash \ or quote ' " */
#define IS_INI_SPECIAL(x) (ini_char_type[(uint8_t) (x)] & 0x0f) /* terminates unquoted strings '\0',' ','\t','\n','\f','\v','\r','#',';','[',']','{','}','=' */
#define IS_INI_BLANK_OR_COMMENT(x) (ini_char_type[(uint8_t) (x)] & 0x06) /* whitespace or start of a comment */
/* table for fast decoding ascii encoded numbers i.e. if (Digit[c] < 10) i = Digit[c]; */
static const uint8_t digit_value[256] = { 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64,
/* ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,64,64, 64,64,64,64,
/* @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ */
64,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,64, 64,64,64,64,
/* ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ */
64,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,64, 64,64,64,64,
64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64,
64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64,
64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64,
64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64, 64,64,64,64 };
/* ------------------------------------------------------------------------- *\
strmemcmp compares a string content with a buffer content of a given size
and returns like strcmp if any differences are found.
(A strncmp() succeeds if the string is longer than the size.)
\* ------------------------------------------------------------------------- */
int strmemcmp(const char * pStr, const void * pBuffer, size_t BufferSize)
{
const char * pb = (const char*) pBuffer;
const char * ps = pStr ? pStr : "";
while(BufferSize && *ps && (*ps == *pb))
{
--BufferSize;
++ps;
++pb;
}
if(!BufferSize)
pb = "";
return (((int) (unsigned char) * ps) - (unsigned char) * pb);
}/* int strmemcmp(...) */
/* ------------------------------------------------------------------------- *\
Extracts a C style character from a string and moves the string pointer to
the end of the C style character definition. It does not move the pointer
behind a string terminating '\0'.
A '\u' is ignored and the backslash returned as the backslash for keeping
'\u'. In case of other unknown sequences like '\#', '\[', '\]', '\{', '\}',
'\*', '\;' or '\ ' the backslash is will be skipped and the subsequent
character returned only.
\* ------------------------------------------------------------------------- */
char get_C_char(const char ** ppSrc)
{
const uint8_t * ps;
uint8_t c = 0;
uint8_t d;
if(!ppSrc || !*ppSrc)
goto Exit;
ps = *(uint8_t **) ppSrc;
c = *ps;
if(c == '\\')
{
ps++;
c = (uint8_t) *ps++;
switch(c) /* check character case insensitive */
{
case '\0': --ps; break; /* string terminator */
case 'a' : c = '\a'; break; /* alert */
case 'b' : c = '\b'; break; /* back space */
case 'd' : /* decimal byte value */
d = digit_value[*ps];
if(d < 10)
{
c = d;
d = digit_value[*(++ps)];
if(d < 10)
{
c = (c * 10) + d;
++ps;
if(c <= 25)
{
d = digit_value[*ps];
if(d <= 5)
{
c = (c * 10) + d;
++ps;
}
}
}
}
break;
case 'e' : c = 0x1b; break; /* escape */
case 'f' : c = '\f'; break; /* formfeed */
case 'n' : c = '\n'; break; /* new line */
case 'r' : c = '\r'; break; /* carriage return */
case 't' : c = '\t'; break; /* horizontal tab */
case 'u' : --ps; c = '\\'; break; /* \u must be kept */
case 'v' : c = '\v'; break; /* vertical tab */
case 'x' : /* hexadecimal byte value */
d = digit_value[*ps];
if(d < 16)
{
c = d;
d = digit_value[*(++ps)];
if(d < 16)
{
c = (c << 4) + d;
++ps;
}
}
break;
default : if((uint8_t) (c - '0') < 8)
{ /* octal byte value */
c -= '0';
d = digit_value[*ps];
if(d < 8)
{
++ps;
c = (c << 3) + d;
if(c < 32)
{
d = digit_value[*ps];
if(d < 8)
{
++ps;
c = (c << 3) + d;
}
}
}
}
break; /* return the character */
}
}
else if(c) /* prevent moving the pointer behind the end of string */
{
++ps;
}
*ppSrc = (char *) ps;
Exit:;
return (c);
}/* get_C_char(char ** pSrc) */
/* ------------------------------------------------------------------------- *\
pIniFindCommentEnd returns a pointer to the first character after
a comment if pb points to the first character of a comment.
\* ------------------------------------------------------------------------- */
static char * pIniFindCommentEnd(const char * pb)
{
char s = *pb; /* comment start character */
if(!IS_INI_COMMENT(s))
goto Exit;
if(*(++pb) == '*')
{ /* a block comment starts with #* (or ;*) ends with *# (or *;) or at the end of the data */
do
{
if(*(++pb) == '*')
{ /* end of comment? */
if(*(++pb) == s)
{ /* end of comment found */
++pb;
break;
}
}
} while(*pb);
}
else
{ /* line comment starts with #* (or ;*) and ends at the end of the current line */
while (*pb && (*pb++ != '\n'))
{};
}
Exit:;
return ((char *) pb);
}/* const char * pIniFindCommentEnd(const char * pb) */
/* ------------------------------------------------------------------------- *\
pSkipBlanksAndComments: helper for ignoring blanks and comments
\* ------------------------------------------------------------------------- */
static char * pSkipBlanksAndComments(const char * pb)
{
if(!pb)
goto Exit;
/* ignore tabs and spaces and other separating blanks */
while(IS_INI_BLANK(*pb)) /* " \t\n\f\v\r" are treated as entry separating whitespaces */
++pb;
while(IS_INI_COMMENT(*pb))
{
pb = pIniFindCommentEnd(pb);
if(!*pb)
goto Exit; /* end of document */
while(IS_INI_BLANK(*pb)) /* " \t\n\f\v\r" are treated as entry separating whitespaces */
++pb;
}
Exit:;
return ((char *) pb);
}/* const char * pSkipBlanksAndComments(const char * pb) */
/* ------------------------------------------------------------------------- *\
pFindBlockEnd returns a pointer to the first character after the block
that pb points to. pb must not point to the inner of a quoted string,
a comment or a header.
\* ------------------------------------------------------------------------- */
char * pIniFindBlockEnd (const char * pb)
{
size_t BlockDepth = 1;
if(!pb)
goto Exit;
while (1)
{
while (IS_INI_CHAR(*pb))
++pb; /* ignore normal characters */
if(IS_INI_BLANK_OR_COMMENT(*pb))
{
pb = pSkipBlanksAndComments(pb);
continue;
}
if(*pb == '\\')
{ /* ignore characters that are escaped except '\0' */
if(! *(++pb))
goto Exit; /* end of document */
}
else if((*pb == '\"') || (*pb == '\''))
{/* ignore characters inside of quoted strings */
char c = *pb;
while(*(++pb) != c)
{
if(*pb == '\\')
++pb;
if(!*pb)
goto Exit; /* end of document */
}
}
else if(*pb == '}')
{
if(!-- BlockDepth)
{
++pb;
goto Exit;
}
}
else if(*pb == '{')
{
++BlockDepth;
}
else if(*pb == '[')
{ /* Let's skip sections headers. */
if(IS_INI_BLANK_OR_COMMENT(*(++pb)))
pb = pSkipBlanksAndComments(pb);
/* let's find the end of the section header */
while(*pb != ']')
{
if(*pb == '\\')
{ /* ignore all characters that are escaped except '\0' */
if(!*(++pb))
goto Exit; /* unexpected end of document */
}
else if((*pb == '\"') || (*pb == '\''))
{/* a part of the section name is quoted and must be ignored */
char c = *pb;
while(*(++pb) != c)
{
if(*pb == '\\')
++pb;
if(!*pb)
goto Exit; /* unexpected end of document */
}
}
else if(!*pb)
goto Exit; /* unexpected end of document */
++pb;
if(IS_INI_BLANK_OR_COMMENT(*pb))
pb = pSkipBlanksAndComments(pb);
}
}
else if(!*pb)
goto Exit; /* end of document */
++pb;
}
Exit:;
return((char *) pb);
} /* char * pIniFindBlockEnd (const char * pBlock) */
/* ------------------------------------------------------------------------- *\
bIniEntryFind searches the next entry in a section and returns nonzero if
an entry was found. * ppData will be set to to the begin of the next
section entry or to the character that terminates the section.
Argument strings to entries which contain spaces have to be enclosed in
"" or '' in the section.
Name or argument embracing quotes or braces will be not removed.
\* ------------------------------------------------------------------------- */
int bIniEntryFind(char ** ppData, /* section data pointer */
char ** ppName, /* returned pointer to entry name */
size_t * pNameSize, /* will be set to the length of the name */
char ** ppArg, /* pointer to the entries parameter string */
size_t * pArgSize, /* length of parameter string */
int bFindBlockEnd) /* whether to find the end of a block or just the begin */
{
int bRet = 0;
char * pd = NULL; /* data pointer for temporary usage */
char c = 0; /* char for temporary usage */
char * pName = NULL; /* pointer to name */
char * pArg = NULL; /* pointer to argument string */
size_t NameSize = 0; /* length of name */
size_t ArgSize = 0; /* argument length */
if(!ppData || !*ppData)
goto Exit; /* invalid argument pointer */
/* let's find begin of next entry */
pd = *ppData;
if(IS_INI_BLANK_OR_COMMENT(*pd))
pd = pSkipBlanksAndComments(pd);
if(IS_INI_SECT_END(*pd)) /* [ ] } '\0' */
goto Exit; /* end of section found */
pName = pd; /* begin of entry name found, remember pointer */
/* find end of the name */
while(!IS_INI_SPECIAL(*pd))
{/* find end of entry name */
if(*pd == '\\')
{
if(!*(++pd)) /* ignore c style characters */
break;
}
else if((*pd == '\"') || (*pd == '\''))
{/* entry name given in quotes */
c = *pd;
while(*(++pd) != c)
{
if(*pd == '\\')
++pd;
if(!*pd)
break; /* end of document */
}
}
++pd; /* add character to string */
}
NameSize = (size_t)(pd - pName); /* calculate name length */
/* end of name, find argument */
if(IS_INI_BLANK_OR_COMMENT(*pd))
pd = pSkipBlanksAndComments(pd);
if(*pd == '=')
{/* argument string found */
if(IS_INI_BLANK_OR_COMMENT(*(++pd)))
pd = pSkipBlanksAndComments(pd); /* skip '=' and subsequent blanks */
if (*pd == '{')
{ /* subblock found */
pArg = pd++;
if(!bFindBlockEnd)
{
ArgSize = 1;
}
else
{
pd = pIniFindBlockEnd(pd);
ArgSize = (size_t)(pd - pArg); /* calculate length but treat the block terminating character as part of the argument */
if(*pd)
pd = pSkipBlanksAndComments(pd); /* skip subsequent blanks */
else
++ArgSize; /* add the terminating '\0' as the block termination */
}
}
else
{
pArg = pd;
while(!IS_INI_SPECIAL(*pd))
{/* find end of entries argument string */
if(*pd == '\\')
{
if(!*(++pd)) /* ignore c style characters */
break;
}
else if((*pd == '\"') || (*pd == '\''))
{/* entry name given in quotes */
c = *pd;
while(*(++pd) != c)
{
if(*pd == '\\')
++pd;
if(!*pd)
break; /* end of document */
}
if(!*pd)
break; /* end of document */
}
++pd;
}
ArgSize = (size_t)(pd - pArg); /* calculate length of argument string */
pd = pSkipBlanksAndComments(pd); /* skip subsequent blanks */
}
}/* argument string found */
else if ((*pd == '{') && !NameSize)
{ /* subblock without name found */
pArg = pd++;
if(!bFindBlockEnd)
{
ArgSize = 1;
}
else
{
pd = pIniFindBlockEnd(pd);
ArgSize = (size_t)(pd - pArg); /* calculate length but treat the block terminating character as part of the argument */
if(*pd)
pd = pSkipBlanksAndComments(pd); /* skip subsequent blanks */
else
++ArgSize; /* add the terminating '\0' as the block termination */
}
}
if(NameSize || ArgSize)
{
bRet = 1;
if(ppName)
*ppName = pName;
if(pNameSize)
*pNameSize = NameSize;
if(ppArg)
*ppArg = pArg;
if(pArgSize)
*pArgSize = ArgSize;
if(*ppData) /* let's find begin of next entry or next section and store it into pData */
*ppData = pd;
vPrintLog(DFL_DBG,"Found entry: name[%lu] = \'%.*s\' arg[%lu] = \'%.*s\'",
(unsigned long) NameSize, (int) NameSize, pName, (unsigned long) ArgSize, (int) ArgSize, pArg);
}
Exit:;
if(!bRet && pd)
{ /* move the iterating data pointer to the terminating character */
if(*ppData)
*ppData = pd;
}
return (bRet);
}/* int bIniEntryFind(...) */
/* ------------------------------------------------------------------------- *\
pIniFindNextSection searches the next section in given INI file buffer.
It returns a pointer to the begin of the sections data or NULL if there
doesn't exist any section within the data.
The iterator ppData point to will be set to the begin of the returned
section or the character that stops the scan.
\* ------------------------------------------------------------------------- */
char * pIniFindNextSection(char ** ppData, /* INI file data buffer */
char ** ppSectionName, /* pointer to section name */
size_t * pSectionNameSize) /* pointer to section name length */
{
char * pd = NULL;
char * pSectionName = NULL;
size_t SectionNameSize = 0;
/* vPrintLog(DFL_ERR,"Searching section \"%s\"!", Name); */
if(!ppData)
goto Exit;
pd = *ppData;
while(bIniEntryFind(&pd, NULL, NULL, NULL, NULL, 1))
{ /* skip all INI file entries */
}
if(*pd != '[')
{
*ppData = pd;
pd = NULL;
goto Exit; /* unexpected end of document */
}
/* Found a section header. Let's find the name and the end of it... */
if(IS_INI_BLANK_OR_COMMENT(*(++pd)))
pd = pSkipBlanksAndComments(pd);
pSectionName = pd;
/* let's find the end of the section header */
while(*pd != ']')
{
if(*pd == '\\')
{ /* we must ignore nonzero characters after a backslash */
if(!*(++pd))
{ /* unexpected end of document */
*ppData = pd;
pd = NULL;
goto Exit; /* unexpected end of document */
}
}
else if((*pd == '\"') || (*pd == '\''))
{/* a part of the section name was given in quotes */
char c = *pd;
while(*(++pd) != c)
{
if(*pd == '\\')
++pd;
if(!*pd)
{ /* unexpected end of document */
*ppData = pd;
pd = NULL;
goto Exit;
}
}
}
else if(!*pd)
{ /* unexpected end of document */
*ppData = pd;
pd = NULL;
goto Exit;
}
++pd;
SectionNameSize = pd - pSectionName;
if(IS_INI_BLANK_OR_COMMENT(*pd))
pd = pSkipBlanksAndComments(pd);
}
/* Skip section header terminating ']' found. Skip all blanks and comments at begin of that section. */
/* pcRet points to the begin of the first entry or end of section afterwards. */
pd = pSkipBlanksAndComments(pd + 1);
*ppData = pd;
if(ppSectionName)
*ppSectionName = pSectionName;
if(pSectionNameSize)
*pSectionNameSize = SectionNameSize;
Exit:;
return (pd);
}/* pIniFindNextSection(char ** pData, char** ppSectionName, size_t * pSectionNameSize) */
/* ------------------------------------------------------------------------- *\
pIniFindSection searches a section in a given string. It returns a pointer
to the begin of the sections data or NULL is the section doesn't exist.
\* ------------------------------------------------------------------------- */
char * pIniFindSection(const char * pData, /* INI file data buffer */
const char * pName) /* section name */
{
const char * pRet = NULL;
size_t NameSize = 0;
char * FoundName = NULL;
size_t FoundLen = 0;
if(!pData || !*pData || !pName)
goto Exit;
/* vPrintLog(DFL_ERR,"Searching section \"%s\"!", Name); */
if(!*pName)
{
pRet = pData;
goto Exit;
}
NameSize = strlen(pName);
while(pData && *pData)
{
pData = (const char *) pIniFindNextSection((char**)&pData, &FoundName, &FoundLen);
/* vPrintLog(DFL_DBG,"### section [%.*s]: '%.20s ...'",
(int)FoundLen, FoundName, pData); */
if(NameSize == FoundLen)
{
if(!strncasecmp(pName, FoundName, FoundLen))
{
pRet = pData;
goto Exit; /* section found */
}
}
else if(FoundLen > NameSize)
{ /* convert special characters and ignore quotation marks in the found string */
const char * pn = pName;
const char * ps = FoundName;
char quote = '\0';
while((size_t)(ps - FoundName) < FoundLen)
{
if(quote && (*ps == quote))
{/* end of quotation found, remember it and ignore the character */
++ps;
quote = '\0';
}
else if (!quote && ((*ps == '\"') || (*ps == '\'')))
{/* begin of quotation found, remember it and ignore the character */
quote = *ps++;
}
else
{
if((size_t)(pn - pName) >= NameSize)
break; /* Searched name is shorter then the found one */
if(*pn++ != get_C_char(&ps))
break;
}
}
if((pn == (pName + NameSize)) &&
(ps == (FoundName + FoundLen)))
{ /* searched section found */
pRet = pData;
break;
}
/* vPrintLog(DFL_DBG,"!!! Section [%.*s|%.*s] != [%.*s|%.*s]",
pn - pName, pName,(int)(NameSize - (pn - pName)), pn,
ps - FoundName, FoundName, (int)(FoundLen - (ps - FoundName)), ps); */
}/* if(FoundLen > NameSize) */
}/* while(*pData) */
Exit:;
if(pRet)
vPrintLog(DFL_DBG,"Found section [%s]", pName ? pName : "");
else
vPrintLog(DFL_DBG,"Section [%s] not found!", pName ? pName : "");
return ((char *) pRet);
}/* pIniFindSection(char * pData, char * pName) */
/* ------------------------------------------------------------------------- *\
lIniGetStringValue converts an unterminated C style escaped string from
INI file buffer to it's unescaped and unqoted value. DstLen has to be at
maximum as large as SrcLen + 1 for a terminating '\0' character.
The function returns the number of written bytes.
Source and Destination buffer may overlap. If the destination is NULL than
the required buffer size is returned only.
\* ------------------------------------------------------------------------- */
size_t lIniGetStringValue(char * pDst, /* pointer to destination buffer */
const char * pSrc, /* pointer to source buffer */
size_t SrcLen) /* length of the source buffer */
{
const char * ps = pSrc;
char * pd = pDst;
char q = '\0';
if(!pSrc)
{
if(pDst)
*pd++='\0';
else
++pd;
}
else if(!pDst)
{
while(*ps && ((size_t)(ps - pSrc) < SrcLen))
{
if(*ps == '\\')
{
get_C_char(&ps); /* get C style char */
++pd;
}
else if ((*ps == '\"') || (*ps == '\''))
{ /* remove quotes */
if(q)
{/* trailing quote? */
if(q == *ps)
{
q = '\0';
++ps;
}
else
{ /* it's the other type of quote */
++ps;
++pd;
}
}
else
{/* leading quote found */
q = *ps++;
}
}
else
{
++ps;
++pd;
}
}
++pd;
}
else
{
while(*ps && ((size_t)(ps - pSrc) < SrcLen))
{
if(*ps == '\\')
{
*pd++ = get_C_char(&ps); /* get C style char */
}
else if ((*ps == '\"') || (*ps == '\''))
{ /* remove quotes */
if(q)
{/* trailing quote? */
if(q == *ps)
{
q = '\0';
++ps;
}
else
{ /* it's the other type of quote */
*pd++ = *ps++;
}
}
else
{/* leading quote found */
q = *ps++;
}
}
else
{
*pd++ = *ps++;
}
}
*pd++ = '\0';
}
return (size_t)(pd - pDst); /* written size */
}/* lIniGetStringValue */
/* ------------------------------------------------------------------------- *\
lIniRemoveQuotes removes the quotes from a value read from INI file without
replacing C like escaped characters. The destination buffer length has to
be at least as large as SrcLen + 1 (for a terminating '\0' character).
The function returns the number of bytes written to the destination string
including the terminating '\0'. Source and destination buffer may overlap.
If the destination is NULL than the required buffer size is returned only.
\* ------------------------------------------------------------------------- */
size_t lIniRemoveQuotes(char * pDst, /* pointer to destination buffer */
const char * pSrc, /* pointer to source buffer */
size_t SrcLen) /* length of source buffer */
{
const char * ps = pSrc;
char * pd = pDst;
char q = '\0';
if(!pSrc)
{
if(pDst)
*pd++='\0';
else
++pd;
}
else if(!pDst)
{
while(*ps && SrcLen--)
{
if ((*ps == '\"') || (*ps == '\''))
{ /* remove quotes */
if(q)
{/* trailing quote? */
if(q == *ps)
{
q = '\0';
++ps;
}
else
{ /* it's the other type of quote */
++ps;
++pd;
}
}
else
{/* leading quote found */
q = *ps++;
}
}
else
{
++ps;
++pd;
}
}
++pd;
}
else
{
while(*ps && SrcLen--)
{
if ((*ps == '\"') || (*ps == '\''))
{ /* remove quotes */
if(q)
{/* trailing quote? */
if(q == *ps)
{
q = '\0';
++ps;
}
else
{ /* it's the other type of quote */
*pd++ = *ps++;
}
}
else
{/* leading quote found */
q = *ps++;
}
}
else
{
*pd++ = *ps++;
}
}
*pd++ = '\0';
}
return ((size_t)(pd - pDst)); /* written size */