forked from mattiasgustavsson/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini.h
1065 lines (817 loc) · 38.4 KB
/
ini.h
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
ini.h - v1.2 - Simple ini-file reader for C/C++.
Do this:
#define INI_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef ini_h
#define ini_h
#define INI_GLOBAL_SECTION ( 0 )
#define INI_NOT_FOUND ( -1 )
typedef struct ini_t ini_t;
ini_t* ini_create( void* memctx );
ini_t* ini_load( char const* data, void* memctx );
int ini_save( ini_t const* ini, char* data, int size );
void ini_destroy( ini_t* ini );
int ini_section_count( ini_t const* ini );
char const* ini_section_name( ini_t const* ini, int section );
int ini_property_count( ini_t const* ini, int section );
char const* ini_property_name( ini_t const* ini, int section, int property );
char const* ini_property_value( ini_t const* ini, int section, int property );
int ini_find_section( ini_t const* ini, char const* name, int name_length );
int ini_find_property( ini_t const* ini, int section, char const* name, int name_length );
int ini_section_add( ini_t* ini, char const* name, int length );
void ini_property_add( ini_t* ini, int section, char const* name, int name_length, char const* value, int value_length );
void ini_section_remove( ini_t* ini, int section );
void ini_property_remove( ini_t* ini, int section, int property );
void ini_section_name_set( ini_t* ini, int section, char const* name, int length );
void ini_property_name_set( ini_t* ini, int section, int property, char const* name, int length );
void ini_property_value_set( ini_t* ini, int section, int property, char const* value, int length );
#endif /* ini_h */
/**
ini.h
=====
Simple ini-file reader for C/C++.
Examples
--------
#### Loading an ini file and retrieving values
#define INI_IMPLEMENTATION
#include "ini.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE* fp = fopen( "test.ini", "r" );
fseek( fp, 0, SEEK_END );
int size = ftell( fp );
fseek( fp, 0, SEEK_SET );
char* data = (char*) malloc( size + 1 );
fread( data, 1, size, fp );
data[ size ] = '\0';
fclose( fp );
ini_t* ini = ini_load( data, NULL );
free( data );
int second_index = ini_find_property( ini, INI_GLOBAL_SECTION, "SecondSetting", 0 );
char const* second = ini_property_value( ini, INI_GLOBAL_SECTION, second_index );
printf( "%s=%s\n", "SecondSetting", second );
int section = ini_find_section( ini, "MySection", 0 );
int third_index = ini_find_property( ini, section, "ThirdSetting", 0 );
char const* third = ini_property_value( ini, section, third_index );
printf( "%s=%s\n", "ThirdSetting", third );
ini_destroy( ini );
return 0;
}
-----------------------------------------------------------------------------------------------
#### Creating a new ini file
#define INI_IMPLEMENTATION
#include "ini.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
ini_t* ini = ini_create( NULL );
ini_property_add( ini, INI_GLOBAL_SECTION, "FirstSetting", 0, "Test", 0 );
ini_property_add( ini, INI_GLOBAL_SECTION, "SecondSetting", 0, "2", 0 );
int section = ini_section_add( ini, "MySection", 0 );
ini_property_add( ini, section, "ThirdSetting", 0, "Three", 0 );
int size = ini_save( ini, NULL, 0 ); // Find the size needed
char* data = (char*) malloc( size );
size = ini_save( ini, data, size ); // Actually save the file
ini_destroy( ini );
FILE* fp = fopen( "test.ini", "w" );
fwrite( data, 1, size - 1, fp );
fclose( fp );
free( data );
return 0;
}
API Documentation
-----------------
ini.h is a small library for reading classic .ini files. It is a single-header library, and does not need any .lib files
or other binaries, or any build scripts. To use it, you just include ini.h to get the API declarations. To get the
definitions, you must include ini.h from *one* single C or C++ file, and #define the symbol `INI_IMPLEMENTATION` before
you do.
### Customization
There are a few different things in ini.h which are configurable by #defines. The customizations only affect the
implementation, so will only need to be defined in the file where you have the #define INI_IMPLEMENTATION.
Note that if all customizations are utilized, ini.h will include no external files whatsoever, which might be useful
if you need full control over what code is being built.
#### Custom memory allocators
To store the internal data structures, ini.h needs to do dynamic allocation by calling `malloc`. Programs might want to
keep track of allocations done, or use custom defined pools to allocate memory from. ini.h allows for specifying custom
memory allocation functions for `malloc` and `free`.
This is done with the following code:
#define INI_IMPLEMENTATION
#define INI_MALLOC( ctx, size ) ( my_custom_malloc( ctx, size ) )
#define INI_FREE( ctx, ptr ) ( my_custom_free( ctx, ptr ) )
#include "ini.h"
where `my_custom_malloc` and `my_custom_free` are your own memory allocation/deallocation functions. The `ctx` parameter
is an optional parameter of type `void*`. When `ini_create` or `ini_load` is called, you can pass in a `memctx`
parameter, which can be a pointer to anything you like, and which will be passed through as the `ctx` parameter to every
`INI_MALLOC`/`INI_FREE` call. For example, if you are doing memory tracking, you can pass a pointer to your tracking
data as `memctx`, and in your custom allocation/deallocation function, you can cast the `ctx` param back to the
right type, and access the tracking data.
If no custom allocator is defined, ini.h will default to `malloc` and `free` from the C runtime library.
#### Custom C runtime function
The library makes use of three additional functions from the C runtime library, and for full flexibility, it allows you
to substitute them for your own. Here's an example:
#define INI_IMPLEMENTATION
#define INI_MEMCPY( dst, src, cnt ) ( my_memcpy_func( dst, src, cnt ) )
#define INI_STRLEN( s ) ( my_strlen_func( s ) )
#define INI_STRNICMP( s1, s2, cnt ) ( my_strnicmp_func( s1, s2, cnt ) )
#include "ini.h"
If no custom function is defined, ini.h will default to the C runtime library equivalent.
ini_create
----------
ini_t* ini_create( void* memctx )
Instantiates a new, empty ini structure, which can be manipulated with other API calls, to fill it with data. To save it
out to an ini-file string, use `ini_save`. When no longer needed, it can be destroyed by calling `ini_destroy`.
`memctx` is a pointer to user defined data which will be passed through to the custom INI_MALLOC/INI_FREE calls. It can
be NULL if no user defined data is needed.
ini_load
--------
ini_t* ini_load( char const* data, void* memctx )
Parse the zero-terminated string `data` containing an ini-file, and create a new ini_t instance containing the data.
The instance can be manipulated with other API calls to enumerate sections/properties and retrieve values. When no
longer needed, it can be destroyed by calling `ini_destroy`. `memctx` is a pointer to user defined data which will be
passed through to the custom INI_MALLOC/INI_FREE calls. It can be NULL if no user defined data is needed.
ini_save
--------
int ini_save( ini_t const* ini, char* data, int size )
Saves an ini structure as a zero-terminated ini-file string, into the specified buffer. Returns the number of bytes
written, including the zero terminator. If `data` is NULL, nothing is written, but `ini_save` still returns the number
of bytes it would have written. If the size of `data`, as specified in the `size` parameter, is smaller than that
required, only part of the ini-file string will be written. `ini_save` still returns the number of bytes it would have
written had the buffer been large enough.
ini_destroy
-----------
void ini_destroy( ini_t* ini )
Destroy an `ini_t` instance created by calling `ini_load` or `ini_create`, releasing the memory allocated by it. No
further API calls are valid on an `ini_t` instance after calling `ini_destroy` on it.
ini_section_count
-----------------
int ini_section_count( ini_t const* ini )
Returns the number of sections in an ini file. There's at least one section in an ini file (the global section), but
there can be many more, each specified in the file by the section name wrapped in square brackets [ ].
ini_section_name
----------------
char const* ini_section_name( ini_t const* ini, int section )
Returns the name of the section with the specified index. `section` must be non-negative and less than the value
returned by `ini_section_count`, or `ini_section_name` will return NULL. The defined constant `INI_GLOBAL_SECTION` can
be used to indicate the global section.
ini_property_count
------------------
int ini_property_count( ini_t const* ini, int section )
Returns the number of properties belonging to the section with the specified index. `section` must be non-negative and
less than the value returned by `ini_section_count`, or `ini_section_name` will return 0. The defined constant
`INI_GLOBAL_SECTION` can be used to indicate the global section. Properties are declared in the ini-file on he format
`name=value`.
ini_property_name
-----------------
char const* ini_property_name( ini_t const* ini, int section, int property )
Returns the name of the property with the specified index `property` in the section with the specified index `section`.
`section` must be non-negative and less than the value returned by `ini_section_count`, and `property` must be
non-negative and less than the value returned by `ini_property_count`, or `ini_property_name` will return NULL. The
defined constant `INI_GLOBAL_SECTION` can be used to indicate the global section.
ini_property_value
------------------
char const* ini_property_value( ini_t const* ini, int section, int property )
Returns the value of the property with the specified index `property` in the section with the specified index `section`.
`section` must be non-negative and less than the value returned by `ini_section_count`, and `property` must be
non-negative and less than the value returned by `ini_property_count`, or `ini_property_value` will return NULL. The
defined constant `INI_GLOBAL_SECTION` can be used to indicate the global section.
ini_find_section
----------------
int ini_find_section( ini_t const* ini, char const* name, int name_length )
Finds the section with the specified name, and returns its index. `name_length` specifies the number of characters in
`name`, which does not have to be zero-terminated. If `name_length` is zero, the length is determined automatically, but
in this case `name` has to be zero-terminated. If no section with the specified name could be found, the value
`INI_NOT_FOUND` is returned.
ini_find_property
-----------------
int ini_find_property( ini_t const* ini, int section, char const* name, int name_length )
Finds the property with the specified name, within the section with the specified index, and returns the index of the
property. `name_length` specifies the number of characters in `name`, which does not have to be zero-terminated. If
`name_length` is zero, the length is determined automatically, but in this case `name` has to be zero-terminated. If no
property with the specified name could be found within the specified section, the value `INI_NOT_FOUND` is returned.
`section` must be non-negative and less than the value returned by `ini_section_count`, or `ini_find_property` will
return `INI_NOT_FOUND`. The defined constant `INI_GLOBAL_SECTION` can be used to indicate the global section.
ini_section_add
---------------
int ini_section_add( ini_t* ini, char const* name, int length )
Adds a section with the specified name, and returns the index it was added at. There is no check done to see if a
section with the specified name already exists - multiple sections of the same name are allowed. `length` specifies the
number of characters in `name`, which does not have to be zero-terminated. If `length` is zero, the length is determined
automatically, but in this case `name` has to be zero-terminated.
ini_property_add
----------------
void ini_property_add( ini_t* ini, int section, char const* name, int name_length, char const* value, int value_length )
Adds a property with the specified name and value to the specified section, and returns the index it was added at. There
is no check done to see if a property with the specified name already exists - multiple properties of the same name are
allowed. `name_length` and `value_length` specifies the number of characters in `name` and `value`, which does not have
to be zero-terminated. If `name_length` or `value_length` is zero, the length is determined automatically, but in this
case `name`/`value` has to be zero-terminated. `section` must be non-negative and less than the value returned by
`ini_section_count`, or the property will not be added. The defined constant `INI_GLOBAL_SECTION` can be used to
indicate the global section.
ini_section_remove
------------------
void ini_section_remove( ini_t* ini, int section )
Removes the section with the specified index, and all properties within it. `section` must be non-negative and less than
the value returned by `ini_section_count`. The defined constant `INI_GLOBAL_SECTION` can be used to indicate the global
section. Note that removing a section will shuffle section indices, so that section indices you may have stored will no
longer indicate the same section as it did before the remove. Use the find functions to update your indices.
ini_property_remove
-------------------
void ini_property_remove( ini_t* ini, int section, int property )
Removes the property with the specified index from the specified section. `section` must be non-negative and less than
the value returned by `ini_section_count`, and `property` must be non-negative and less than the value returned by
`ini_property_count`. The defined constant `INI_GLOBAL_SECTION` can be used to indicate the global section. Note that
removing a property will shuffle property indices within the specified section, so that property indices you may have
stored will no longer indicate the same property as it did before the remove. Use the find functions to update your
indices.
ini_section_name_set
--------------------
void ini_section_name_set( ini_t* ini, int section, char const* name, int length )
Change the name of the section with the specified index. `section` must be non-negative and less than the value returned
by `ini_section_count`. The defined constant `INI_GLOBAL_SECTION` can be used to indicate the global section. `length`
specifies the number of characters in `name`, which does not have to be zero-terminated. If `length` is zero, the length
is determined automatically, but in this case `name` has to be zero-terminated.
ini_property_name_set
---------------------
void ini_property_name_set( ini_t* ini, int section, int property, char const* name, int length )
Change the name of the property with the specified index in the specified section. `section` must be non-negative and
less than the value returned by `ini_section_count`, and `property` must be non-negative and less than the value
returned by `ini_property_count`. The defined constant `INI_GLOBAL_SECTION` can be used to indicate the global section.
`length` specifies the number of characters in `name`, which does not have to be zero-terminated. If `length` is zero,
the length is determined automatically, but in this case `name` has to be zero-terminated.
ini_property_value_set
----------------------
void ini_property_value_set( ini_t* ini, int section, int property, char const* value, int length )
Change the value of the property with the specified index in the specified section. `section` must be non-negative and
less than the value returned by `ini_section_count`, and `property` must be non-negative and less than the value
returned by `ini_property_count`. The defined constant `INI_GLOBAL_SECTION` can be used to indicate the global section.
`length` specifies the number of characters in `value`, which does not have to be zero-terminated. If `length` is zero,
the length is determined automatically, but in this case `value` has to be zero-terminated.
*/
/*
----------------------
IMPLEMENTATION
----------------------
*/
#ifdef INI_IMPLEMENTATION
#undef INI_IMPLEMENTATION
#define INITIAL_CAPACITY ( 256 )
#undef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#undef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include <stddef.h>
#ifndef INI_MALLOC
#include <stdlib.h>
#define INI_MALLOC( ctx, size ) ( malloc( size ) )
#define INI_FREE( ctx, ptr ) ( free( ptr ) )
#endif
#ifndef INI_MEMCPY
#include <string.h>
#define INI_MEMCPY( dst, src, cnt ) ( memcpy( dst, src, cnt ) )
#endif
#ifndef INI_STRLEN
#include <string.h>
#define INI_STRLEN( s ) ( strlen( s ) )
#endif
#ifndef INI_STRNICMP
#ifdef _WIN32
#include <string.h>
#define INI_STRNICMP( s1, s2, cnt ) ( strnicmp( s1, s2, cnt ) )
#else
#include <string.h>
#define INI_STRNICMP( s1, s2, cnt ) ( strncasecmp( s1, s2, cnt ) )
#endif
#endif
struct ini_internal_section_t
{
char name[ 32 ];
char* name_large;
};
struct ini_internal_property_t
{
int section;
char name[ 32 ];
char* name_large;
char value[ 64 ];
char* value_large;
};
struct ini_t
{
struct ini_internal_section_t* sections;
int section_capacity;
int section_count;
struct ini_internal_property_t* properties;
int property_capacity;
int property_count;
void* memctx;
};
static int ini_internal_property_index( ini_t const* ini, int section, int property )
{
int i;
int p;
if( ini && section >= 0 && section < ini->section_count )
{
p = 0;
for( i = 0; i < ini->property_count; ++i )
{
if( ini->properties[ i ].section == section )
{
if( p == property ) return i;
++p;
}
}
}
return INI_NOT_FOUND;
}
ini_t* ini_create( void* memctx )
{
ini_t* ini;
ini = (ini_t*) INI_MALLOC( memctx, sizeof( ini_t ) );
ini->memctx = memctx;
ini->sections = (struct ini_internal_section_t*) INI_MALLOC( ini->memctx, INITIAL_CAPACITY * sizeof( ini->sections[ 0 ] ) );
ini->section_capacity = INITIAL_CAPACITY;
ini->section_count = 1; /* global section */
ini->sections[ 0 ].name[ 0 ] = '\0';
ini->sections[ 0 ].name_large = 0;
ini->properties = (struct ini_internal_property_t*) INI_MALLOC( ini->memctx, INITIAL_CAPACITY * sizeof( ini->properties[ 0 ] ) );
ini->property_capacity = INITIAL_CAPACITY;
ini->property_count = 0;
return ini;
}
ini_t* ini_load( char const* data, void* memctx )
{
ini_t* ini;
char const* ptr;
int s;
char const* start;
char const* start2;
int l;
ini = ini_create( memctx );
ptr = data;
if( ptr )
{
s = 0;
while( *ptr )
{
/* trim leading whitespace */
while( *ptr && *ptr <=' ' )
++ptr;
/* done? */
if( !*ptr ) break;
/* comment */
else if( *ptr == ';' )
{
while( *ptr && *ptr !='\n' )
++ptr;
}
/* section */
else if( *ptr == '[' )
{
++ptr;
start = ptr;
while( *ptr && *ptr !=']' && *ptr != '\n' )
++ptr;
if( *ptr == ']' )
{
s = ini_section_add( ini, start, (int)( ptr - start) );
++ptr;
}
}
/* property */
else
{
start = ptr;
while( *ptr && *ptr !='=' && *ptr != '\n' )
++ptr;
if( *ptr == '=' )
{
l = (int)( ptr - start);
++ptr;
while( *ptr && *ptr <= ' ' && *ptr != '\n' )
ptr++;
start2 = ptr;
while( *ptr && *ptr != '\n' )
++ptr;
while( ptr >= start2 && *(--ptr) <= ' ' )
(void)ptr;
ptr++;
if( ptr == start2 )
ini_property_add( ini, s, start, l, "", 1 );
else
ini_property_add( ini, s, start, l, start2, (int)( ptr - start2 ) );
}
}
}
}
return ini;
}
int ini_save( ini_t const* ini, char* data, int size )
{
int s;
int p;
int i;
int l;
char* n;
int pos;
if( ini )
{
pos = 0;
for( s = 0; s < ini->section_count; ++s )
{
n = ini->sections[ s ].name_large ? ini->sections[ s ].name_large : ini->sections[ s ].name;
l = (int) INI_STRLEN( n );
if( l > 0 )
{
if( data && pos < size ) data[ pos ] = '[';
++pos;
for( i = 0; i < l; ++i )
{
if( data && pos < size ) data[ pos ] = n[ i ];
++pos;
}
if( data && pos < size ) data[ pos ] = ']';
++pos;
if( data && pos < size ) data[ pos ] = '\n';
++pos;
}
for( p = 0; p < ini->property_count; ++p )
{
if( ini->properties[ p ].section == s )
{
n = ini->properties[ p ].name_large ? ini->properties[ p ].name_large : ini->properties[ p ].name;
l = (int) INI_STRLEN( n );
for( i = 0; i < l; ++i )
{
if( data && pos < size ) data[ pos ] = n[ i ];
++pos;
}
if( data && pos < size ) data[ pos ] = '=';
++pos;
n = ini->properties[ p ].value_large ? ini->properties[ p ].value_large : ini->properties[ p ].value;
l = (int) INI_STRLEN( n );
for( i = 0; i < l; ++i )
{
if( data && pos < size ) data[ pos ] = n[ i ];
++pos;
}
if( data && pos < size ) data[ pos ] = '\n';
++pos;
}
}
if( pos > 0 )
{
if( data && pos < size ) data[ pos ] = '\n';
++pos;
}
}
if( data && pos < size ) data[ pos ] = '\0';
++pos;
return pos;
}
return 0;
}
void ini_destroy( ini_t* ini )
{
int i;
if( ini )
{
for( i = 0; i < ini->property_count; ++i )
{
if( ini->properties[ i ].value_large ) INI_FREE( ini->memctx, ini->properties[ i ].value_large );
if( ini->properties[ i ].name_large ) INI_FREE( ini->memctx, ini->properties[ i ].name_large );
}
for( i = 0; i < ini->section_count; ++i )
if( ini->sections[ i ].name_large ) INI_FREE( ini->memctx, ini->sections[ i ].name_large );
INI_FREE( ini->memctx, ini->properties );
INI_FREE( ini->memctx, ini->sections );
INI_FREE( ini->memctx, ini );
}
}
int ini_section_count( ini_t const* ini )
{
if( ini ) return ini->section_count;
return 0;
}
char const* ini_section_name( ini_t const* ini, int section )
{
if( ini && section >= 0 && section < ini->section_count )
return ini->sections[ section ].name_large ? ini->sections[ section ].name_large : ini->sections[ section ].name;
return NULL;
}
int ini_property_count( ini_t const* ini, int section )
{
int i;
int count;
if( ini )
{
count = 0;
for( i = 0; i < ini->property_count; ++i )
{
if( ini->properties[ i ].section == section ) ++count;
}
return count;
}
return 0;
}
char const* ini_property_name( ini_t const* ini, int section, int property )
{
int p;
if( ini && section >= 0 && section < ini->section_count )
{
p = ini_internal_property_index( ini, section, property );
if( p != INI_NOT_FOUND )
return ini->properties[ p ].name_large ? ini->properties[ p ].name_large : ini->properties[ p ].name;
}
return NULL;
}
char const* ini_property_value( ini_t const* ini, int section, int property )
{
int p;
if( ini && section >= 0 && section < ini->section_count )
{
p = ini_internal_property_index( ini, section, property );
if( p != INI_NOT_FOUND )
return ini->properties[ p ].value_large ? ini->properties[ p ].value_large : ini->properties[ p ].value;
}
return NULL;
}
int ini_find_section( ini_t const* ini, char const* name, int name_length )
{
int i;
if( ini && name )
{
if( name_length <= 0 ) name_length = (int) INI_STRLEN( name );
for( i = 0; i < ini->section_count; ++i )
{
char const* const other =
ini->sections[ i ].name_large ? ini->sections[ i ].name_large : ini->sections[ i ].name;
if( strlen( other ) == name_length && INI_STRNICMP( name, other, (size_t)name_length ) == 0 )
return i;
}
}
return INI_NOT_FOUND;
}
int ini_find_property( ini_t const* ini, int section, char const* name, int name_length )
{
int i;
int c;
if( ini && name && section >= 0 && section < ini->section_count)
{
if( name_length <= 0 ) name_length = (int) INI_STRLEN( name );
c = 0;
for( i = 0; i < ini->property_count; ++i )
{
if( ini->properties[ i ].section == section )
{
char const* const other =
ini->properties[ i ].name_large ? ini->properties[ i ].name_large : ini->properties[ i ].name;
if( strlen( other ) == name_length && INI_STRNICMP( name, other, (size_t) name_length ) == 0 )
return c;
++c;
}
}
}
return INI_NOT_FOUND;
}
int ini_section_add( ini_t* ini, char const* name, int length )
{
struct ini_internal_section_t* new_sections;
if( ini && name )
{
if( length <= 0 ) length = (int) INI_STRLEN( name );
if( ini->section_count >= ini->section_capacity )
{
ini->section_capacity *= 2;
new_sections = (struct ini_internal_section_t*) INI_MALLOC( ini->memctx,
ini->section_capacity * sizeof( ini->sections[ 0 ] ) );
INI_MEMCPY( new_sections, ini->sections, ini->section_count * sizeof( ini->sections[ 0 ] ) );
INI_FREE( ini->memctx, ini->sections );
ini->sections = new_sections;
}
ini->sections[ ini->section_count ].name_large = 0;
if( (size_t) length + 1 >= sizeof( ini->sections[ 0 ].name ) )
{
ini->sections[ ini->section_count ].name_large = (char*) INI_MALLOC( ini->memctx, (size_t) length + 1 );
INI_MEMCPY( ini->sections[ ini->section_count ].name_large, name, (size_t) length );
ini->sections[ ini->section_count ].name_large[ length ] = '\0';
}
else
{
INI_MEMCPY( ini->sections[ ini->section_count ].name, name, (size_t) length );
ini->sections[ ini->section_count ].name[ length ] = '\0';
}
return ini->section_count++;
}
return INI_NOT_FOUND;
}
void ini_property_add( ini_t* ini, int section, char const* name, int name_length, char const* value, int value_length )
{
struct ini_internal_property_t* new_properties;
if( ini && name && section >= 0 && section < ini->section_count )
{
if( name_length <= 0 ) name_length = (int) INI_STRLEN( name );
if( value_length <= 0 ) value_length = (int) INI_STRLEN( value );
if( ini->property_count >= ini->property_capacity )
{
ini->property_capacity *= 2;
new_properties = (struct ini_internal_property_t*) INI_MALLOC( ini->memctx,
ini->property_capacity * sizeof( ini->properties[ 0 ] ) );
INI_MEMCPY( new_properties, ini->properties, ini->property_count * sizeof( ini->properties[ 0 ] ) );
INI_FREE( ini->memctx, ini->properties );
ini->properties = new_properties;
}
ini->properties[ ini->property_count ].section = section;
ini->properties[ ini->property_count ].name_large = 0;
ini->properties[ ini->property_count ].value_large = 0;
if( (size_t) name_length + 1 >= sizeof( ini->properties[ 0 ].name ) )
{
ini->properties[ ini->property_count ].name_large = (char*) INI_MALLOC( ini->memctx, (size_t) name_length + 1 );
INI_MEMCPY( ini->properties[ ini->property_count ].name_large, name, (size_t) name_length );
ini->properties[ ini->property_count ].name_large[ name_length ] = '\0';
}
else
{
INI_MEMCPY( ini->properties[ ini->property_count ].name, name, (size_t) name_length );
ini->properties[ ini->property_count ].name[ name_length ] = '\0';
}
if( (size_t) value_length + 1 >= sizeof( ini->properties[ 0 ].value ) )
{
ini->properties[ ini->property_count ].value_large = (char*) INI_MALLOC( ini->memctx, (size_t) value_length + 1 );
INI_MEMCPY( ini->properties[ ini->property_count ].value_large, value, (size_t) value_length );
ini->properties[ ini->property_count ].value_large[ value_length ] = '\0';
}
else
{
INI_MEMCPY( ini->properties[ ini->property_count ].value, value, (size_t) value_length );
ini->properties[ ini->property_count ].value[ value_length ] = '\0';
}
++ini->property_count;
}
}
void ini_section_remove( ini_t* ini, int section )
{
int p;
if( ini && section >= 0 && section < ini->section_count )
{
if( ini->sections[ section ].name_large ) INI_FREE( ini->memctx, ini->sections[ section ].name_large );
for( p = ini->property_count - 1; p >= 0; --p )
{
if( ini->properties[ p ].section == section )
{
if( ini->properties[ p ].value_large ) INI_FREE( ini->memctx, ini->properties[ p ].value_large );
if( ini->properties[ p ].name_large ) INI_FREE( ini->memctx, ini->properties[ p ].name_large );
ini->properties[ p ] = ini->properties[ --ini->property_count ];
}
}
ini->sections[ section ] = ini->sections[ --ini->section_count ];
for( p = 0; p < ini->property_count; ++p )
{
if( ini->properties[ p ].section == ini->section_count )
ini->properties[ p ].section = section;
}
}
}
void ini_property_remove( ini_t* ini, int section, int property )
{
int p;
if( ini && section >= 0 && section < ini->section_count )
{
p = ini_internal_property_index( ini, section, property );
if( p != INI_NOT_FOUND )
{
if( ini->properties[ p ].value_large ) INI_FREE( ini->memctx, ini->properties[ p ].value_large );
if( ini->properties[ p ].name_large ) INI_FREE( ini->memctx, ini->properties[ p ].name_large );
ini->properties[ p ] = ini->properties[ --ini->property_count ];
return;
}
}
}
void ini_section_name_set( ini_t* ini, int section, char const* name, int length )
{
if( ini && name && section >= 0 && section < ini->section_count )
{
if( length <= 0 ) length = (int) INI_STRLEN( name );
if( ini->sections[ section ].name_large ) INI_FREE( ini->memctx, ini->sections[ section ].name_large );
ini->sections[ section ].name_large = 0;
if( (size_t) length + 1 >= sizeof( ini->sections[ 0 ].name ) )
{
ini->sections[ section ].name_large = (char*) INI_MALLOC( ini->memctx, (size_t) length + 1 );
INI_MEMCPY( ini->sections[ section ].name_large, name, (size_t) length );
ini->sections[ section ].name_large[ length ] = '\0';
}
else
{
INI_MEMCPY( ini->sections[ section ].name, name, (size_t) length );
ini->sections[ section ].name[ length ] = '\0';
}
}
}
void ini_property_name_set( ini_t* ini, int section, int property, char const* name, int length )
{
int p;
if( ini && name && section >= 0 && section < ini->section_count )
{
if( length <= 0 ) length = (int) INI_STRLEN( name );
p = ini_internal_property_index( ini, section, property );
if( p != INI_NOT_FOUND )
{
if( ini->properties[ p ].name_large ) INI_FREE( ini->memctx, ini->properties[ p ].name_large );
ini->properties[ ini->property_count ].name_large = 0;
if( (size_t) length + 1 >= sizeof( ini->properties[ 0 ].name ) )
{
ini->properties[ p ].name_large = (char*) INI_MALLOC( ini->memctx, (size_t) length + 1 );
INI_MEMCPY( ini->properties[ p ].name_large, name, (size_t) length );
ini->properties[ p ].name_large[ length ] = '\0';
}
else
{
INI_MEMCPY( ini->properties[ p ].name, name, (size_t) length );
ini->properties[ p ].name[ length ] = '\0';
}
}
}
}
void ini_property_value_set( ini_t* ini, int section, int property, char const* value, int length )
{
int p;
if( ini && value && section >= 0 && section < ini->section_count )
{
if( length <= 0 ) length = (int) INI_STRLEN( value );
p = ini_internal_property_index( ini, section, property );
if( p != INI_NOT_FOUND )
{
if( ini->properties[ p ].value_large ) INI_FREE( ini->memctx, ini->properties[ p ].value_large );
ini->properties[ ini->property_count ].value_large = 0;
if( (size_t) length + 1 >= sizeof( ini->properties[ 0 ].value ) )
{
ini->properties[ p ].value_large = (char*) INI_MALLOC( ini->memctx, (size_t) length + 1 );
INI_MEMCPY( ini->properties[ p ].value_large, value, (size_t) length );
ini->properties[ p ].value_large[ length ] = '\0';
}
else
{
INI_MEMCPY( ini->properties[ p ].value, value, (size_t) length );
ini->properties[ p ].value[ length ] = '\0';
}
}
}
}
#endif /* INI_IMPLEMENTATION */
/*
contributors: