-
Notifications
You must be signed in to change notification settings - Fork 0
/
inifile.c
1633 lines (1478 loc) · 53.5 KB
/
inifile.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>
#include <limits.h>
#include "inifile.h"
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* >= C99 */
#define INLINE_FCT inline
#define INLINE_PROT static inline
#else /* No inline available from C Standard */
#define INLINE_FCT static
#define INLINE_PROT static
#endif /* __STDC_VERSION__ >= C99 */
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h> /* For DeleteFile() function */
#define STRCMP_CIS(str1,str2) _stricmp(str1,str2)
#elif __linux__
#include <unistd.h> /* For unlink() function */
#if defined (_POSIX_C_SOURCE) && (_POSIX_C_SOURCE>=200112L)
#include <strings.h>
#define STRCMP_CIS(str1,str2) strcasecmp(str1,str2)
#else
#define STRCMP_CIS(str1,str2) Ini_stricmp(str1,str2)
#endif
#else
#error Platform not implemented
#endif /* _WIN32 */
#define STRCMP_CS(str1,str2) strcmp(str1,str2)
#define IS_OCT_DIGIT(ch) ((isdigit(ch)) && (ch<'8'))
#define KEYS_REALLOC(ptr,newCount) realloc((ptr),(newCount)*sizeof(struct TagIniKey))
#define SECTIONS_REALLOC(ptr,newCount) realloc((ptr),(newCount)*sizeof(struct TagIniSection))
#define SECTIONS_NEED_REALLOC(file) (((file)->uiSectionsCount<(file)->uiSectionsMax)?0:1)
#define KEYS_NEED_REALLOC(section) (((section)->uiKeysCount<(section)->uiKeysMax)?0:1)
#define SECTION_CALC_INDEX(file) (((file)->ptagCurrSection-(file)->ptagSections)/sizeof(struct TagIniSection*))
#define KEY_CALC_INDEX(file) (((file)->ptagCurrSection->ptagCurrKey-(file)->ptagCurrSection->ptagKeys)/sizeof(struct TagIniKey*))
#define CHECK_SECTIONNAME_VALID(str) Ini_CheckNameValid_m(str,MAX_SECTION_LENGTH-1)
#define CHECK_KEYNAME_VALID(str) Ini_CheckNameValid_m(str,MAX_KEYNAME_LENGTH-1)
#define OPTION_CHK(ini,opt) ((ini)->uiOptions&opt)
#define INI_STRCMP(ini,str1,str2) (OPTION_CHK(ini,INI_OPT_CASE_SENSITIVE)?STRCMP_CS(str1,str2):STRCMP_CIS(str1,str2))
#ifdef INIFILE_PRINT_ERRORS
#define ERR_PRINT(str) fputs("INI Error: " str "\n",stderr)
#define ERR_PRINT_VARGS(str,...) fprintf(stderr,"INI Error: " str "\n",__VA_ARGS__)
#else
#define ERR_PRINT(str)
#define ERR_PRINT_VARGS(str,...)
#endif /* INIFILE_PRINT_ERRORS */
enum
{
INITIAL_SECTION_COUNT = 10,
INITIAL_KEYS_PER_SECTION_COUNT = 10,
MAX_SECTION_LENGTH = 80,
MAX_KEYNAME_LENGTH = MAX_SECTION_LENGTH,
MAX_KEYVAL_LENGTH = 300,
INI_MAX_PATHLEN = 260,
/* Internal used return values */
INI_CONTINUE_ON_NEXT_LINE = 100,
INI_ERR_MEMORY_ALLOC,
INI_ERR_FOPEN,
INI_ERR_READ_QUOTES_INVALID,
INI_ERR_ESC_SEQ_INVALID,
INI_ERR_SECTIONNAME_DUPLICATE,
INI_ERR_SECTIONNAME_INVALID,
INI_ERR_KEYNAME_DUPLICATE,
INI_ERR_KEYNAME_INVALID,
INI_ERR_KEYVAL_INVALID,
INI_ERR_DATATYPE_UNKNOWN,
INI_ERR_BUFFER_TOO_SMALL,
};
enum chars
{
SECTION_NAME_BEGIN = '[',
SECTION_NAME_END = ']',
COMMENT_INDICATOR_1 = ';',
COMMENT_INDICATOR_2 = '#',
CHAR_EQUAL1 = '=',
CHAR_EQUAL2 = ':',
CHAR_NEWLINE_1 = '\n',
CHAR_NEWLINE_2 = '\r',
CHAR_A = 'a', /* 'a', Bell (alert) */
CHAR_B = 'b', /* 'b', backspace */
CHAR_F = 'f', /* 'f', Form feed */
CHAR_N = 'n', /* 'n', new line */
CHAR_R = 'r', /* 'r', carriage return */
CHAR_T = 't', /* 't', horizontal tab */
CHAR_V = 'v', /* 'v', vertical tab */
CHAR_SQ = '\'', /* '\'', Single quotation mark */
CHAR_DQ = '\"', /* '\"', Double quotation mark */
CHAR_BS = '\\', /* '\\', Backslash */
CHAR_QUES = '?', /* '?', Question mark */
CHAR_O = 'o', /* 'o', ASCII octal notation */
CHAR_X = 'x', /* 'x', ASCII Hexadecimal notation */
CHAR_0 = '\0', /* '\0' continue on next line */
ESC_SEQ_CHAR_A = '\a',
ESC_SEQ_CHAR_B = '\b',
ESC_SEQ_CHAR_F = '\f',
ESC_SEQ_CHAR_N = '\n',
ESC_SEQ_CHAR_R = '\r',
ESC_SEQ_CHAR_T = '\t',
ESC_SEQ_CHAR_V = '\v',
ESC_SEQ_CHAR_SQ = '\'',
ESC_SEQ_CHAR_DQ = '\"',
ESC_SEQ_CHAR_BS = '\\',
ESC_SEQ_CHAR_QUES = '\?',
ESC_SEQ_CHAR_0 = '\0',
};
struct TagIniKey
{
char caKeyName[MAX_KEYNAME_LENGTH+1];
unsigned char ucaData[MAX_KEYVAL_LENGTH];
unsigned int uiDataSize;
EDataType type;
};
struct TagIniSection
{
unsigned int uiKeysCount;
unsigned int uiKeysMax;
char caSectionName[MAX_SECTION_LENGTH+1];
struct TagIniKey *ptagKeys;
struct TagIniKey *ptagCurrKey;
};
struct TagIniFile
{
char caPath[INI_MAX_PATHLEN+1];
char *caTmpBuffer;
unsigned int uiBufSize;
unsigned int uiOptions;
unsigned int uiSectionsCount;
unsigned int uiSectionsMax;
struct TagIniSection *ptagSections;
struct TagIniSection *ptagCurrSection;
};
INLINE_PROT void Ini_ResetContent_m(Inifile file);
INLINE_PROT int Ini_OptionsValid_m(unsigned int uiOptions);
INLINE_PROT int Ini_stricmp(const char *pcStr1,
const char *pcStr2);
INLINE_PROT void Ini_RemoveLeadingWhitespaces_m(char **ppcString);
INLINE_PROT unsigned int Ini_RemoveFollowingWhitespaces_m(char *pcString,
unsigned int uiLength);
INLINE_PROT char *Ini_FindEndOfLine_m(char **ppcLine,
unsigned int *puiLength);
INLINE_PROT int Ini_UnEscapeString_m(const char *pcStrIn,
unsigned char *pucStrOut,
unsigned int *puiDataSize,
EDataType *pDataType);
INLINE_PROT int Ini_EscapeData_m(const unsigned char *pucDataIn,
unsigned int uiDataInSize,
char *pcOutText,
unsigned int *puiOutBufSize,
EDataType dataType);
INLINE_PROT int Ini_CheckNameValid_m(const char *pcName,
unsigned int uiMaxLen);
INLINE_PROT int Ini_CreateNewSection_m(Inifile tagfile,
const char *pcSection);
INLINE_PROT int Ini_CreateNewKey_m(Inifile file,
const char *pcKey);
INLINE_PROT int Ini_KeySetData_m(Inifile file,
const TagData *ptagData);
INLINE_PROT int Ini_FindSection_m(Inifile file,
const char *pcSection);
INLINE_PROT int Ini_FindKey_m(Inifile file,
const char *pcKey);
INLINE_PROT int Ini_Read_Section_m(char *pcLine,
unsigned int uiLength);
INLINE_PROT int Ini_Read_Key_m(Inifile tagfile,
char **ppcNextLine,
char *pcLine,
unsigned int uiLength);
INLINE_PROT int Ini_Read_Comment_m(char *pcLine);
INLINE_PROT int Ini_ConvertValue_m(IniKey ptagKey,
TagData *ptagData);
static const char *pcaBooleanStringsTrue_m[]= {"true", "enabled", "1","y","t"};
static const char *pcaBooleanStringsFalse_m[]={"false","disabled","0","n","f"};
int IniFile_New(Inifile *newFile,
const char *path,
unsigned int options)
{
unsigned int uiLen=strlen(path)+1;
if((Ini_OptionsValid_m(options)) || (uiLen > INI_MAX_PATHLEN))
return(INI_ERR_PARAM);
if(!(*newFile=malloc(sizeof(struct TagIniFile))))
return(INI_ERR_INTERNAL);
(*newFile)->uiBufSize=(MAX_KEYNAME_LENGTH+MAX_KEYVAL_LENGTH)*5;
if(!((*newFile)->caTmpBuffer=malloc((*newFile)->uiBufSize)))
{
free(*newFile);
return(INI_ERR_INTERNAL);
}
if(!((*newFile)->ptagSections=SECTIONS_REALLOC(NULL,INITIAL_SECTION_COUNT)))
{
free((*newFile)->caTmpBuffer);
free(*newFile);
return(INI_ERR_INTERNAL);
}
if(!((*newFile)->ptagSections[0].ptagKeys=KEYS_REALLOC(NULL,INITIAL_KEYS_PER_SECTION_COUNT)))
{
free((*newFile)->ptagSections);
free((*newFile)->caTmpBuffer);
free(*newFile);
return(INI_ERR_INTERNAL);
}
(*newFile)->uiOptions=options;
(*newFile)->uiSectionsCount=1;
(*newFile)->uiSectionsMax=INITIAL_SECTION_COUNT;
(*newFile)->ptagCurrSection=(*newFile)->ptagSections;
(*newFile)->ptagSections[0].caSectionName[0]='\0';
(*newFile)->ptagSections[0].uiKeysCount=0;
(*newFile)->ptagSections[0].uiKeysMax=INITIAL_KEYS_PER_SECTION_COUNT;
(*newFile)->ptagSections[0].ptagCurrKey=(*newFile)->ptagSections[0].ptagKeys;
memcpy((*newFile)->caPath,path,uiLen);
return(INI_ERR_NONE);
}
void IniFile_Dispose(Inifile file)
{
unsigned int uiIndexSection;
if(!file) /* Validate param for not being NULL */
return;
for(uiIndexSection=0;uiIndexSection < file->uiSectionsCount;++uiIndexSection)
free(file->ptagSections[uiIndexSection].ptagKeys);
free(file->ptagSections);
free(file->caTmpBuffer);
free(file);
}
int Inifile_SetPath(Inifile file,
const char *path)
{
unsigned int uiLen=strlen(path)+1;
if((!file) || (uiLen > INI_MAX_PATHLEN))
return(INI_ERR_PARAM);
memcpy(file->caPath,path,uiLen);
return(INI_ERR_NONE);
}
int IniFile_Read(Inifile file)
{
FILE *fp;
char *pcNextLine;
char *pcLine;
char *pcTmp;
int iRc=INI_ERR_NONE;
unsigned int uiLength;
if(!file) /* Validate params for not being NULL */
return(INI_ERR_PARAM);
errno=0;
/* Open files as binary, to stay compatible with all platforms */
if(!(fp=fopen(file->caPath,"rb")))
{
ERR_PRINT_VARGS("fopen() failed for file \"%s\" (%d): %s",file->caPath,errno,strerror(errno));
return(INI_ERR_IO);
}
fseek(fp,0,SEEK_END);
uiLength=ftell(fp)+1;
rewind(fp);
if(uiLength < 2) /* Empty file, okay, don't read it then */
{
fclose(fp);
return(INI_ERR_NONE);
}
if(uiLength>file->uiBufSize)
{
if(!(pcTmp=realloc(file->caTmpBuffer,uiLength)))
{
fclose(fp);
return(INI_ERR_INTERNAL);
}
file->uiBufSize=uiLength;
file->caTmpBuffer=pcTmp;
}
/* Read whole file at once */
uiLength=fread(file->caTmpBuffer,1,uiLength,fp)+1;
if(!feof(fp)) /* If EOF not reached, I/O Error occured */
{
fclose(fp);
return(INI_ERR_IO);
}
file->caTmpBuffer[uiLength-1]='\0';
fclose(fp);
pcLine=file->caTmpBuffer;
while(1)
{
pcNextLine=Ini_FindEndOfLine_m(&pcLine,&uiLength);
switch(pcLine[0])
{
case SECTION_NAME_BEGIN:
if((iRc=Ini_Read_Section_m(++pcLine,--uiLength)) != INI_ERR_NONE)
break;
iRc=Ini_CreateNewSection_m(file,pcLine);
break;
case COMMENT_INDICATOR_1:
case COMMENT_INDICATOR_2:
iRc=Ini_Read_Comment_m(pcLine);
break;
default:
if((iRc=Ini_Read_Key_m(file,&pcNextLine,pcLine,uiLength)) != INI_ERR_NONE)
break;
}
switch(iRc)
{
case INI_ERR_NONE: /* All okay, continue reading */
case INI_CONTINUE_ON_NEXT_LINE:
iRc=INI_ERR_NONE;
break;
case INI_ERR_MALFORMED:
case INI_ERR_READ_QUOTES_INVALID:
case INI_ERR_ESC_SEQ_INVALID:
case INI_ERR_SECTIONNAME_INVALID:
case INI_ERR_KEYNAME_INVALID:
case INI_ERR_KEYVAL_INVALID:
if(!OPTION_CHK(file,INI_OPT_IGNORE_MALFORMED_LINES))
{
iRc=INI_ERR_MALFORMED;
pcNextLine=NULL; /* Will quit loop below */
Ini_ResetContent_m(file);
}
else
iRc=INI_ERR_NONE;
break;
default: /* Fatal error, can't be ignored */
iRc=INI_ERR_INTERNAL;
Ini_ResetContent_m(file);
pcNextLine=NULL;
break;
}
if(!pcNextLine) /* No more data */
break;
pcLine=pcNextLine;
}
return(iRc);
}
int IniFile_Write(Inifile file)
{
FILE *fp;
unsigned int uiSize;
unsigned int uiIndexSection;
unsigned int uiIndexKey;
if(!file) /* Validate params for not being NULL */
return(INI_ERR_PARAM);
errno=0;
if(!(fp=fopen(file->caPath,"w")))
{
ERR_PRINT_VARGS("fopen() failed for file \"%s\" (%d): %s",file->caPath,errno,strerror(errno));
return(INI_ERR_IO);
}
for(uiIndexSection=0;uiIndexSection < file->uiSectionsCount;++uiIndexSection)
{
if(uiIndexSection) /* First section is unnamed */
{
/* Check return val for possible I/O Error */
if(fprintf(fp,"[%s]\n",file->ptagSections[uiIndexSection].caSectionName) < 1)
{
fclose(fp);
return(INI_ERR_IO);
}
}
for(uiIndexKey=0;uiIndexKey < file->ptagSections[uiIndexSection].uiKeysCount;++uiIndexKey)
{
uiSize=file->uiBufSize;
if(!file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].uiDataSize)
file->caTmpBuffer[0]='\0';
else if(Ini_EscapeData_m(file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].ucaData,
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].uiDataSize,
file->caTmpBuffer,
&uiSize,
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].type) != INI_ERR_NONE)
{
ERR_PRINT("Ini_EscapeData_m() failed, buffer too small, quit writing");
fclose(fp);
return(INI_ERR_INTERNAL);
}
/* Check return val for possible I/O Error */
if(fprintf(fp,
"%s=%s\n",
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].caKeyName,
file->caTmpBuffer) < 1)
{
fclose(fp);
return(INI_ERR_IO);
}
}
}
fclose(fp);
return(INI_ERR_NONE);
}
void IniFile_Clean(Inifile file)
{
Ini_ResetContent_m(file);
}
int IniFile_Remove(Inifile file)
{
errno=0;
/* If file was removed successfully or doesn't exist, return no error */
#ifdef _WIN32
if((DeleteFile(file->caPath)) || ((errno=GetLastError()) == ERROR_FILE_NOT_FOUND))
return(INI_ERR_NONE);
ERR_PRINT_VARGS("DeleteFile(\"%s\") failed (%d): %s",file->caPath,errno,strerror(errno));
#elif __linux__
if((unlink(file->caPath) == 0) || (errno == ENOENT))
return(INI_ERR_NONE);
ERR_PRINT_VARGS("unlink(\"%s\") failed (%d): %s",file->caPath,errno,strerror(errno));
#endif
return(INI_ERR_IO);
}
int IniFile_FindEntry_GetValue(Inifile file,
const char *section,
const char *key,
TagData *ptagData)
{
if((!file) || (!key) || (!ptagData)) /* Validate params for not being NULL */
return(INI_ERR_PARAM);
if(!Ini_FindSection_m(file,section))
return(INI_ERR_FIND_NONE);
if(!Ini_FindKey_m(file,key))
return(INI_ERR_FIND_SECTION);
return(IniFile_Iterator_KeyGetValue(file,ptagData));
}
int IniFile_CreateEntry_SetValue(Inifile file,
const char *section,
const char *key,
const TagData *ptagData)
{
int iRc;
if(!file) /* Validate params for not being NULL */
return(INI_ERR_PARAM);
/* Empty section: Always exists as first */
if((!section) || (section[0]=='\0'))
file->ptagCurrSection=&file->ptagSections[0];
else
{
/* Check if section exist or create new if not */
if((iRc=IniFile_Iterator_CreateSection(file,section)) != INI_ERR_NONE)
return(iRc);
}
/* Check if key exists, create if not */
if((iRc=IniFile_Iterator_CreateKey(file,key)) != INI_ERR_NONE)
return(iRc);
return(IniFile_Iterator_KeySetValue(file,ptagData));
}
int IniFile_StringCompare(const Inifile file,
const char *str1,
const char *str2)
{
return(INI_STRCMP(file,str1,str2));
}
const char *IniFile_Iterator_SetSectionIndex(Inifile file,
unsigned int position)
{
file->ptagCurrSection=file->ptagSections +
((position < file->uiSectionsCount)?position:file->uiSectionsCount-1);
return(file->ptagCurrSection->caSectionName);
}
const char *IniFile_Iterator_NextSection(Inifile file)
{
if(file->ptagCurrSection < file->ptagSections+file->uiSectionsCount-1)
{
++file->ptagCurrSection;
return(file->ptagCurrSection->caSectionName);
}
return(NULL);
}
const char *IniFile_Iterator_SetKeyIndex(Inifile file,
unsigned int position)
{
file->ptagCurrSection->ptagCurrKey=file->ptagCurrSection->ptagKeys +
((position < file->ptagCurrSection->uiKeysCount)?position:file->ptagCurrSection->uiKeysCount-1);
return((file->ptagCurrSection->uiKeysCount)?file->ptagCurrSection->ptagCurrKey->caKeyName:NULL);
}
const char *IniFile_Iterator_NextKey(Inifile file)
{
if(file->ptagCurrSection->ptagCurrKey < file->ptagCurrSection->ptagKeys+file->ptagCurrSection->uiKeysCount-1)
{
++file->ptagCurrSection->ptagCurrKey;
return(file->ptagCurrSection->ptagCurrKey->caKeyName);
}
return(NULL);
}
int IniFile_Iterator_FindSection(Inifile file,
const char *section)
{
if(!file)
return(INI_ERR_PARAM);
return((Ini_FindSection_m(file,section))?INI_ERR_NONE:INI_ERR_FIND_NONE);
}
int IniFile_Iterator_FindKey(Inifile file,
const char *key)
{
if((!file) || (!key))
return(INI_ERR_PARAM);
return((Ini_FindKey_m(file,key))?INI_ERR_NONE:INI_ERR_FIND_SECTION);
}
int IniFile_Iterator_KeySetValue(Inifile file,
const TagData *ptagData)
{
int iRc;
if(!file) /* Validate parameter, data may be NULL */
return(INI_ERR_PARAM);
switch((iRc=Ini_KeySetData_m(file,ptagData)))
{
case INI_ERR_NONE:
break;
case INI_ERR_CONVERSION:
case INI_ERR_BUFFER_TOO_SMALL:
case INI_ERR_DATATYPE_UNKNOWN:
ERR_PRINT_VARGS("Ini_KeySetData_m() failed (%d): %s",iRc,IniFile_GetErrorText(iRc));
iRc=INI_ERR_PARAM;
break;
default:
ERR_PRINT_VARGS("Ini_KeySetData_m() failed (%d): %s",iRc,IniFile_GetErrorText(iRc));
return(INI_ERR_INTERNAL);
}
return(iRc);
}
int IniFile_Iterator_KeyGetValue(const Inifile file,
TagData *ptagData)
{
int iRc;
if((!file) || (!ptagData))
return(INI_ERR_PARAM);
if(((iRc=Ini_ConvertValue_m(file->ptagCurrSection->ptagCurrKey,ptagData)) != INI_ERR_NONE) &&
(iRc != INI_ERR_DATA_EMPTY))
{
ERR_PRINT_VARGS("Ini_ConvertValue_m() failed (%d): %s",iRc,IniFile_GetErrorText(iRc));
switch(iRc)
{
case INI_ERR_NONE:
case INI_ERR_DATA_EMPTY:
case INI_ERR_CONVERSION:
break;
case INI_ERR_BUFFER_TOO_SMALL:
case INI_ERR_DATATYPE_UNKNOWN:
iRc=INI_ERR_PARAM;
break;
default:
iRc=INI_ERR_INTERNAL;
}
}
return(iRc);
}
int IniFile_Iterator_CreateSection(Inifile file,
const char *section)
{
int iRc;
if((!file) || (!section))
return(INI_ERR_PARAM);
switch((iRc=Ini_CreateNewSection_m(file,section)))
{
case INI_ERR_NONE:
case INI_ERR_SECTIONNAME_DUPLICATE: /* Section already exists, ok */
break;
case INI_ERR_SECTIONNAME_INVALID:
ERR_PRINT_VARGS("Ini_CreateNewSection_m() failed (%d): %s",iRc,IniFile_GetErrorText(iRc));
return(INI_ERR_PARAM);
case INI_ERR_MEMORY_ALLOC:
default: /* Other error occured */
ERR_PRINT_VARGS("Ini_CreateNewSection_m() failed (%d): %s",iRc,IniFile_GetErrorText(iRc));
return(INI_ERR_INTERNAL);
}
return(INI_ERR_NONE);
}
int IniFile_Iterator_CreateKey(Inifile file,
const char *key)
{
int iRc;
if((!file) || (!key))
return(INI_ERR_PARAM);
switch((iRc=Ini_CreateNewKey_m(file,key)))
{
case INI_ERR_NONE:
case INI_ERR_KEYNAME_DUPLICATE: /* Key already exists, ok */
break;
case INI_ERR_KEYNAME_INVALID:
ERR_PRINT_VARGS("Ini_CreateNewKey_m() failed (%d): %s",iRc,IniFile_GetErrorText(iRc));
return(INI_ERR_PARAM);
case INI_ERR_MEMORY_ALLOC:
default:
ERR_PRINT_VARGS("Ini_CreateNewKey_m() failed (%d): %s",iRc,IniFile_GetErrorText(iRc));
return(INI_ERR_INTERNAL);
}
return(INI_ERR_NONE);
}
void IniFile_Iterator_DeleteSection(Inifile file)
{
unsigned int uiSectionIndex;
/* Check if first section */
if(file->ptagCurrSection == file->ptagSections)
{
file->ptagCurrSection->uiKeysCount=0;
return;
}
uiSectionIndex=SECTION_CALC_INDEX(file);
/* free Keys for the section */
free(file->ptagCurrSection->ptagKeys);
--file->uiSectionsCount;
if(uiSectionIndex < file->uiSectionsCount-1)
{
memmove(file->ptagCurrSection,
file->ptagCurrSection+1,
(file->uiSectionsCount-uiSectionIndex)*sizeof(struct TagIniSection));
}
--file->ptagCurrSection;
}
void IniFile_Iterator_DeleteKey(Inifile file)
{
unsigned int uiKeyIndex;
/* Delete Key+value */
uiKeyIndex=KEY_CALC_INDEX(file);
--file->ptagCurrSection->uiKeysCount;
if(uiKeyIndex < file->ptagCurrSection->uiKeysCount)
{
memmove(file->ptagCurrSection->ptagCurrKey,
file->ptagCurrSection->ptagCurrKey+1,
(file->ptagCurrSection->uiKeysCount-uiKeyIndex)*sizeof(struct TagIniKey));
}
if(file->ptagCurrSection->ptagCurrKey != file->ptagCurrSection->ptagKeys)
--file->ptagCurrSection->ptagCurrKey;
}
int IniFile_DeleteEntry_Key(Inifile file,
const char *section,
const char *key)
{
if((!file) || (!key)) /* Validate params for not being NULL */
return(INI_ERR_PARAM);
if(!Ini_FindSection_m(file,section))
return(INI_ERR_FIND_NONE);
if(!Ini_FindKey_m(file,key))
return(INI_ERR_FIND_SECTION);
IniFile_Iterator_DeleteKey(file);
return(INI_ERR_NONE);
}
int IniFile_DeleteEntry_Section(Inifile file,
const char *section)
{
if(!file) /* Validate params for not being NULL */
return(INI_ERR_PARAM);
if(!Ini_FindSection_m(file,section))
return(INI_ERR_FIND_NONE);
IniFile_Iterator_DeleteSection(file);
return(INI_ERR_NONE);
}
const char *IniFile_GetErrorText(int error)
{
switch(error)
{
/* External available error codes */
case INI_ERR_NONE:
return("No Error");
case INI_ERR_PARAM:
return("Invalid Parameter");
case INI_ERR_DATA_EMPTY:
return("Can't get Data, Key is empty");
case INI_ERR_MALFORMED:
return("File is malformed, can't read");
case INI_ERR_IO:
return("I/O Error occured");
case INI_ERR_INTERNAL:
return("Internal Error occured");
case INI_ERR_FIND_NONE:
return("Nor Section nor Keyname found");
case INI_ERR_FIND_SECTION:
return("Only Section found, but not Keyname");
/* Internal error codes */
case INI_ERR_MEMORY_ALLOC:
return("Failed to allocate requested memory");
case INI_ERR_FOPEN:
return("Failed to open file");
case INI_ERR_READ_QUOTES_INVALID:
return("Invalid quotes in line");
case INI_ERR_ESC_SEQ_INVALID:
return("Invalid Escape sequence in line");
case INI_ERR_SECTIONNAME_DUPLICATE:
return("Duplicate Section, already exists");
case INI_ERR_SECTIONNAME_INVALID:
return("Invalid Sectionname");
case INI_ERR_KEYNAME_DUPLICATE:
return("Duplicate Keyname, already exists");
case INI_ERR_KEYNAME_INVALID:
return("Invalid Keyname");
case INI_ERR_KEYVAL_INVALID:
return("Invalid Keyvalue");
case INI_ERR_DATATYPE_UNKNOWN:
return("Unknown Datatype specified");
case INI_ERR_BUFFER_TOO_SMALL:
return("Buffer is too small");
case INI_ERR_CONVERSION:
return("Conversion to specified type failed");
default:
return("Unknown Error");
}
}
void IniFile_DumpContent(const Inifile file,
FILE *fp)
{
unsigned int uiSize;
unsigned int uiIndexSection;
unsigned int uiIndexKey;
fputs("***Start Dumping Contents of Inifile***\n",fp);
for(uiIndexSection=0;uiIndexSection < file->uiSectionsCount;++uiIndexSection)
{
if(uiIndexSection)
fprintf(fp,
"0x%p [%s]\n",
file->ptagSections[uiIndexSection].caSectionName,
file->ptagSections[uiIndexSection].caSectionName);
for(uiIndexKey=0;uiIndexKey < file->ptagSections[uiIndexSection].uiKeysCount;++uiIndexKey)
{
uiSize=file->uiBufSize;
/* Check if key is empty */
if(!file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].uiDataSize)
strcpy(file->caTmpBuffer,"(empty)");
else if(Ini_EscapeData_m(file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].ucaData,
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].uiDataSize,
file->caTmpBuffer,
&uiSize,
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].type) != INI_ERR_NONE)
return;
fprintf(fp,
"0x%p=0x%p %s=%s (types: 0x%.4X)\n",
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].caKeyName,
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].ucaData,
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].caKeyName,
file->caTmpBuffer,
file->ptagSections[uiIndexSection].ptagKeys[uiIndexKey].type);
}
}
fputs("***End Dumping Contents of Inifile***\n",fp);
}
INLINE_FCT int Ini_UnEscapeString_m(const char *pcStrIn,
unsigned char *pucStrOut,
unsigned int *puiDataSize,
EDataType *pType)
{
unsigned int uiIndexOut=0;
char caTmp[4];
if(!*puiDataSize)
return((pcStrIn[0] == '\0')?INI_ERR_NONE:INI_ERR_BUFFER_TOO_SMALL);
*pType |= eDataType_Binary;
while(*pcStrIn != '\0')
{
if(uiIndexOut > (*puiDataSize)-2)
return(INI_ERR_BUFFER_TOO_SMALL);
if(*pcStrIn != CHAR_BS)
{
*pType &= ~(eDataType_Binary);
pucStrOut[uiIndexOut++]=*pcStrIn;
++pcStrIn;
continue;
}
++pcStrIn;
/* Evaluate Escape sequence character */
switch(*pcStrIn)
{
case CHAR_A: /* 'a', Bell (alert) */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_A;
break;
case CHAR_B: /* 'b', Backspace */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_B;
break;
case CHAR_F: /* 'f', Form feed */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_F;
break;
case CHAR_N: /* 'n', New line */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_N;
break;
case CHAR_R: /* 'r', Carriage return */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_R;
break;
case CHAR_T: /* 't', Horizontal Tab */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_T;
break;
case CHAR_V: /* 'v', Vertical Tab */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_V;
break;
case CHAR_SQ: /* '\'', Single quotation mark */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_SQ;
break;
case CHAR_DQ: /* '\"', Double quotation mark */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_DQ;
break;
case CHAR_BS: /* '\\', Backslash */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_BS;
break;
case CHAR_QUES: /* '?', Question mark */
pucStrOut[uiIndexOut++]=ESC_SEQ_CHAR_QUES;
break;
case CHAR_O: /* 'o', ASCII octal notation */
++pcStrIn;
if(!IS_OCT_DIGIT(*pcStrIn)) /* If 1st char isn't octal, escape sequence is invalid */
return(INI_ERR_ESC_SEQ_INVALID);
caTmp[3]='\0';
if(*pcStrIn > '3')
return(INI_ERR_ESC_SEQ_INVALID);
/* Next 2 bytes also must be a valid octal number */
if((!IS_OCT_DIGIT(*(pcStrIn+1))) ||
(!IS_OCT_DIGIT(*(pcStrIn+2))))
return(INI_ERR_ESC_SEQ_INVALID);
caTmp[0]=*pcStrIn++;
caTmp[1]=*pcStrIn++;
caTmp[2]=*pcStrIn++;
if(uiIndexOut > (*puiDataSize)-2)
return(INI_ERR_BUFFER_TOO_SMALL);
pucStrOut[uiIndexOut++]=strtol(caTmp,NULL,8);
continue; /* Continue, to keep the binary datatype flag */
case CHAR_X: /* 'x', ASCII Hexadecimal notation */
++pcStrIn;
if(!isxdigit(*pcStrIn)) /* If 1st char isn't hexadecimal, escape sequence is invalid */
return(INI_ERR_ESC_SEQ_INVALID);
caTmp[2]='\0';
/* Next byte also must be a valid hexadecimal number */
if(!isxdigit(*(pcStrIn+1)))
return(INI_ERR_ESC_SEQ_INVALID);
caTmp[0]=*pcStrIn++;
caTmp[1]=*pcStrIn++;
if(uiIndexOut > (*puiDataSize)-2)
return(INI_ERR_BUFFER_TOO_SMALL);
pucStrOut[uiIndexOut++]=strtol(caTmp,NULL,16);
continue; /* Continue, to keep the binary datatype flag */
case CHAR_0: /* '\0' continue on next line, return directly (line ended) */
pucStrOut[uiIndexOut]='\0';
*puiDataSize=uiIndexOut+1;
return(INI_CONTINUE_ON_NEXT_LINE);
default: /* Escape sequence unknown */
return(INI_ERR_ESC_SEQ_INVALID);
}
*pType &= ~(eDataType_Binary);
++pcStrIn;
}
if(*pType != eDataType_Binary) /* If nonbinary data, termination is added */
pucStrOut[uiIndexOut++]='\0';
*puiDataSize=uiIndexOut;
return(INI_ERR_NONE);
}
INLINE_FCT int Ini_EscapeData_m(const unsigned char *pucDataIn,
unsigned int uiDataInSize,
char *pcOutText,
unsigned int *puiOutBufSize,
EDataType dataType)
{
unsigned int uiIndex;
unsigned int uiIndexOut=0;
/* Empty data, this is valid, just return empty string then */
if(!uiDataInSize)
{
pcOutText[0]='\0';
*puiOutBufSize=0;
return(INI_ERR_NONE);
}
/* If type is string, escape with quotes */
if((dataType & eDataType_String) &&
((dataType & (eDataType_Int | eDataType_Uint | eDataType_Double | eDataType_Boolean)) == 0))
pcOutText[uiIndexOut++]='\"';
if(dataType == eDataType_Binary)
{/* If type is just binary, escape all bytes as binary */
for(uiIndex=0;uiIndex < uiDataInSize;++uiIndex)
{
if(uiIndexOut > (*puiOutBufSize)-5)
return(INI_ERR_BUFFER_TOO_SMALL);
pcOutText[uiIndexOut++]=CHAR_BS;
pcOutText[uiIndexOut++]=CHAR_X;
pcOutText[uiIndexOut++]=(pucDataIn[uiIndex]>>4)+(((pucDataIn[uiIndex]>>4) > 9)?'A'-10:'0');
pcOutText[uiIndexOut++]=(pucDataIn[uiIndex] & 0xF)+(((pucDataIn[uiIndex] & 0xF) > 9)?'A'-10:'0');
}
pcOutText[uiIndexOut++]='\0';
*puiOutBufSize=uiIndexOut;
return(INI_ERR_NONE);
}
for(uiIndex=0;uiIndex < uiDataInSize-1;++uiIndex)
{
if(uiIndexOut+2 > *puiOutBufSize) /* Buffer too small */
return(INI_ERR_BUFFER_TOO_SMALL);
switch(pucDataIn[uiIndex])
{
case ESC_SEQ_CHAR_A:
pcOutText[uiIndexOut++]='\\';
pcOutText[uiIndexOut++]=CHAR_A;
break;
case ESC_SEQ_CHAR_B:
pcOutText[uiIndexOut++]='\\';
pcOutText[uiIndexOut++]=CHAR_B;
break;
case ESC_SEQ_CHAR_F:
pcOutText[uiIndexOut++]='\\';
pcOutText[uiIndexOut++]=CHAR_F;
break;
case ESC_SEQ_CHAR_N:
pcOutText[uiIndexOut++]='\\';
pcOutText[uiIndexOut++]=CHAR_N;
break;
case ESC_SEQ_CHAR_R:
pcOutText[uiIndexOut++]='\\';