forked from MrucznikRolePlay/Mrucznik-RP-Includes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble-o-files2.inc
1875 lines (1648 loc) · 49.5 KB
/
double-o-files2.inc
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
#if defined _dof2_included
#endinput
#endif
#define _dof2_included
#include <a_samp>
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
* This is a new version of the INI script Double-O-Files.
* However, it's has completely been rewritten and has now a much better performance.
* There is also the support for sections in the INI file. (But there is no support for comments.)
* Double-O-Files 2 is compatible with DUDB, DINI, Double-O-Files and possibly y_ini since it
* can handle sections and entry of the format "key = value", not only "key=value".
* The number of spaces between the equal sign and key and value can actually be arbitrary.
* I've added some comments below. You may see that I've mentioned the big-O-notation,
* 'n' always Entries.Count.
* Double-O-Files 2 should also be useful for Russian letter because I'm using
* the functions fgetchar and fputchar to write and read the files.
*
* There is another new feature which has been inspired by ZCMD and y_ini:
* The OnParseFile callbacks. To learn more about it, read the description in
* the SA-MP forums if you haven't already.
* THE END
*/
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*
native DOF2_SetFile(file[]);
native DOF2_LoadFile();
native DOF2_SaveFile();
native DOF2_ParseFile(file[],extraid,bool:callback=true);
native DOF2_ReparseFile(file[],extraid,bool:callback=true);
native DOF2_WriteFile();
native DOF2_PrintFile(comment[]="");
native DOF2_GetString(file[],key[],tag[]="");
native DOF2_GetStringEx(file[],key[],result[],size,tag[]="");
native Float:DOF2_GetFloat(file[],key[]);
native DOF2_GetInt(file[],key[],tag[]="");
native DOF2_GetHex(file[],key[],tag[]="");
native DOF2_GetBin(file[],key[],tag[]="");
native bool:DOF2_GetBool(file[],key[],tag[]="");
native DOF2_SetString(file[],key[],value[],tag[]="");
native DOF2_SetFloat(file[],key[],Float:value);
native DOF2_SetInt(file[],key[],value,tag[]="");
native DOF2_SetHex(file[],key[],value,tag[]="");
native DOF2_SetBin(file[],key[],value,tag[]="");
native DOF2_SetBool(file[],key[],bool:value,tag[]="");
native DOF2_IsSet(file[],key[],tag[]="");
native DOF2_Unset(file[],key[],tag[]="");
native DOF2_FileExists(file[]);
native DOF2_RemoveFile(file[]);
native DOF2_CreateFile(file[],password[]="");
native DOF2_RenameFile(oldfile[],newfile[]);
native DOF2_RenameKey(file[],oldkey[],newkey[],tag[]="");
native DOF2_CopyFile(filetocopy[],newfile[]);
native DOF2_CheckLogin(file[],password[]);
native DOF2_File(user[]);
native DOF2_ParseInt();
native DOF2_ParseFloat();
native DOF2_ParseBool();
native DOF2_ParseBin();
native DOF2_ParseHex();
native DOF2_SetUTF8(bool:set);
native bool:DOF2_GetUTF8();
native DOF2_GetFile();
native DOF2_MakeBackup(file[]);
native DOF2_RemoveSection (file [], tag []);
native DOF2_SectionExists (file [], tag []);
native DOF2_SortSection (file [], tag [], bool: ignorecase = true, bool: ascending = true);
native DOF2_SortAllSections (file [], bool: ignorecase = true, bool: ascending = true);
native DOF2_SetCaseSensitivity (bool: set);
native DOF2_GetCaseSensitivity ();
*/
#define DOF2_TagExists DOF2_SectionExists
#define DOF2_RemoveTag DOF2_RemoveSection
// OnParseFile <Tag><Key>(extraid, value [])
// OnParseFile <><Key>(extraid, value [])
// OnDefaultParseFile (extraid, value [], key [], tag [], file [])
// The arguments of your OnParseFile functions may have arbitrary names but must be an integer followed by a string.
// Function must return a value.
#define OnParseFile<%0><%1>(%2) \
forward _OnParseFile_%0_%1 (extraid, value []); \
public _OnParseFile_%0_%1 (extraid, value []) \
return __OnParseFile_%0_%1 (extraid, (value [0] == '\1' && value [1] == '\0') ? ("") : value); \
stock __OnParseFile_%0_%1 (%2)
// Also here: The argument names may be arbitrary but must be an integer followed by 4 strings.
// Function must return a value.
#define OnDefaultParseFile(%0) \
forward _OnDefaultParseFile (extraid, value [], key [], tag [], file []); \
public _OnDefaultParseFile (extraid, value [], key [], tag [], file []) \
return __OnDefaultParseFile (extraid, (value [0] == '\1' && value [1] == '\0') ? ("") : value, key, (tag [0] == '\1' && tag [1] == '\0') ? ("") : tag, file); \
stock __OnDefaultParseFile (%0)
#define DOF2_ParseBool() \
(strval (value) || (value [0] && !strcmp (value, "true", true)))
#define DOF2_ParseInt() \
(strval (value))
#define DOF2_ParseFloat() \
(floatstr (value))
#define DOF2_ParseBin() \
(DOF2_strbin (value))
#define DOF2_ParseHex() \
(DOF2_strhex (value))
#define DOF2_LoadFile() \
DOF2_ParseFile (CurrentFile, -1, false)
#define DOF2_SaveFile \
DOF2_WriteFile
#define DOF2_FileExists \
fexist
#define Sections. \
Sections_
#define Entries. \
Entries_
#define DOF2:: \
DOF2_
#if !defined private
#define private static stock
#endif
/*
#define MAX_SECTION_TAG (32)
#define MAX_LINE_SIZE (128)
#define MAX_SECTIONS (32)
#define MAX_ENTRIES (256)
#define MAX_FILE_SIZE (64)
#define USER_FILE_PATH "Users/%s.ini"
*/
// The maximum length of the name of a tag.
#if !defined MAX_SECTION_TAG
#define MAX_SECTION_TAG (32)
#endif
// The maximum length of a line (including key and value).
#if !defined MAX_LINE_SIZE
#define MAX_LINE_SIZE (128)
#endif
// The maximum number of sections which can be handled. Be careful: MUST NOT be higher than 255.
#if !defined MAX_SECTIONS
#define MAX_SECTIONS (32)
#endif
// The maximum number of entries which can be loaded into the cache.
#if !defined MAX_ENTRIES
#define MAX_ENTRIES (256)
#endif
// The maximum length of the name of a file.
#if !defined MAX_FILE_SIZE
#define MAX_FILE_SIZE (64)
#endif
/*
If PACK_CONTENT == true tag names and lines (key + value) will get stored in cache as packed strings.
The result is less memory usage. However, you won't be able to use special characters like russian or chinese ones.
*/
#if !defined PACK_CONTENT
#define PACK_CONTENT (false)
#endif
#define INVALID_ENTRY (-1)
#define INVALID_SECTION (-1)
// Do you want to emulate DUDB?
#if !defined DUDB_CONVERT && 0 // Change to 1 to enable.
#define DUDB_CONVERT
#endif
#if !defined USER_FILE_PATH
#if defined DUDB_CONVERT
#define USER_FILE_PATH "%s.dudb.sav"
#else
#define USER_FILE_PATH "%s.ini"
#endif
#endif
#if !defined USER_PW_HASH_KEY
#if defined DUDB_CONVERT
#define USER_PW_HASH_KEY "password_hash"
#else
#define USER_PW_HASH_KEY "password"
#endif
#endif
// Do you want to emulate DINI?
#if !defined DINI_CONVERT && 1 // Change to 1 to enable.
#define DINI_CONVERT
#endif
/*
#if MAX_SECTIONS >= 256
#error MAX_SECTIONS must not be greater than 255.
#endif
*/
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
private
bool: UseUTF8 = PACK_CONTENT,
bool: CaseSensitive = false,
CurrentFile [MAX_FILE_SIZE],
bool: FileChanged,
Sections.FirstEntry [MAX_SECTIONS] = {INVALID_ENTRY, ...},
Sections.LastEntry [MAX_SECTIONS] = {INVALID_ENTRY, ...},
Sections.Count,
#if PACK_CONTENT == true
Sections.Tag [MAX_SECTIONS][MAX_SECTION_TAG char],
Entries.Line [MAX_ENTRIES][MAX_LINE_SIZE char],
Entries.Tag [MAX_ENTRIES][MAX_SECTION_TAG char],
#else
Sections.Tag [MAX_SECTIONS][MAX_SECTION_TAG],
Entries.Line [MAX_ENTRIES][MAX_LINE_SIZE],
Entries.Tag [MAX_ENTRIES][MAX_SECTION_TAG],
#endif
#if MAX_SECTIONS >= 256
Entries.Section [MAX_ENTRIES],
#else
Entries.Section [MAX_ENTRIES char],
#endif
Entries.NextEntry [MAX_ENTRIES] = {INVALID_ENTRY, ...},
Entries.PreviousEntry [MAX_ENTRIES] = {INVALID_ENTRY, ...},
Entries.Count,
SortedEntryList [MAX_ENTRIES][2]; // Index 0: Hashcode, Index 1: EntryID
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
DOF2::Exit ()
DOF2::WriteFile ();
stock DOF2::SetUTF8 (bool: set)
UseUTF8 = set;
stock bool: DOF2::GetUTF8 ()
return UseUTF8;
stock bool: DOF2::SetCaseSensitivity (bool: set)
CaseSensitive = set;
stock bool: DOF2::GetCaseSensitivity ()
return CaseSensitive;
stock DOF2::SetFile (file [])
DOF2::strcpy (CurrentFile, file);
stock DOF2::GetFile ()
return CurrentFile;
stock DOF2::CreateFile (file [], password [] = "")
{
if (!DOF2::FileExists (file))
{
new File: f = fopen (file, io_append);
if (fclose (f))
{
if (password [0])
return DOF2::SetInt (file, USER_PW_HASH_KEY, DOF2::num_hash (password));
return 1;
}
}
return 0;
}
stock DOF2::RenameFile (oldfile [], newfile [])
{
if (!DOF2::FileExists (newfile))
{
// If 'CurrentFile' is 'oldfile', write it if it has been changed.
if (CurrentFile [0] && !strcmp (CurrentFile, oldfile) && FileChanged)
DOF2::WriteFile ();
else if (!DOF2::ParseFile (oldfile, -1, false)) // Otherwise parse 'oldfile'.
return 0;
DOF2::SetFile (newfile);
if (DOF2::WriteFile ())
return fremove (oldfile);
}
return 0;
}
stock DOF2::CopyFile (filetocopy [], newfile [])
{
if (!DOF2::FileExists (newfile))
{
if (CurrentFile [0] && !strcmp (CurrentFile, filetocopy) && FileChanged)
DOF2::WriteFile ();
else if(!DOF2::ParseFile (filetocopy, -1, false))
return 0;
DOF2::SetFile (newfile);
return DOF2::WriteFile ();
}
return 0;
}
stock DOF2::RemoveFile (file [])
{
if (file [0])
{
if (CurrentFile [0] && !strcmp (CurrentFile, file))
CurrentFile [0] = '\0';
return fremove (file);
}
return 0;
}
stock DOF2::MakeBackup (file [])
{
new
year,
month,
day,
hour,
minute,
second,
backupfile [MAX_FILE_SIZE];
getdate (year, month, day);
gettime (hour, minute, second);
format (backupfile, sizeof (backupfile), "%s.%02d_%02d_%02d.%02d_%02d_%02d_%02d.bak", CurrentFile, month, day, year, hour, minute, second, GetTickCount ());
return DOF2::CopyFile (CurrentFile, backupfile);
}
stock bool: DOF2::SectionExists (file [], tag [])
{
if (file [0]) // You can't remove the empty Sections.
{
if (!tag [0])
return true; // Emptry section always exists. In every file.
if (!CurrentFile [0] || strcmp (CurrentFile, file)) // No file in buffer or the file you want to read from is not the file in the buffer.
if (!DOF2::ParseFile (file, -1, false))
return false;
#if PACK_CONTENT == true
new buf [MAX_SECTION_TAG];
#endif
for (new i = 1; i < Sections.Count; ++i)
{
#if PACK_CONTENT == true
strunpack (buf, Sections.Tag [i]);
if (!strcmp (buf, tag, !CaseSensitive))
return true;
#else
if (!strcmp (Sections.Tag [i], tag, !CaseSensitive))
return true;
#endif
}
}
return false;
}
stock DOF2::RemoveSection (file [], tag [])
{
// Removes tag 'tag' with all it's entries.
if (file [0] && tag [0]) // You can't remove the empty Sections.
{
if (!CurrentFile [0] || strcmp (CurrentFile, file)) // No file in buffer or the file you want to read from is not the file in the buffer.
if (!DOF2::ParseFile (file, -1, false))
return 0;
new
#if PACK_CONTENT == true
line [MAX_LINE_SIZE],
#endif
buf [MAX_SECTION_TAG],
section = INVALID_SECTION,
entry,
key [MAX_LINE_SIZE];
for (new i = 1; i < Sections.Count; ++i)
{
#if PACK_CONTENT == true
strunpack (buf, Sections.Tag [i]);
if (!strcmp (buf, tag, !CaseSensitive))
{
section = i;
break;
}
#else
if (!strcmp (Sections.Tag [i], tag, !CaseSensitive))
{
section = i;
break;
}
#endif
}
if (section != INVALID_SECTION)
{
entry = Sections.FirstEntry [section];
while (entry != INVALID_ENTRY)
{
// Remove all entries under the current Sections.
#if PACK_CONTENT == true
strunpack (line, Entries.Line [entry]);
DOF2::ParseLine (line, key, buf);
#else
DOF2::ParseLine (Entries.Line [entry], key, buf);
#endif
DOF2::Unset (file, key, tag);
entry = Entries.NextEntry [entry];
}
// Move the last tag to the position of the current tag. Creates a little mess.
--Sections.Count;
Sections.Tag [section] = Sections.Tag [Sections.Count];
Sections.FirstEntry [section] = Sections.FirstEntry [Sections.Count];
Sections.LastEntry [section] = Sections.LastEntry [Sections.Count];
// Adjust the tag IDs of the entries.
entry = Sections.FirstEntry [section];
while (entry != INVALID_ENTRY)
{
#if MAX_SECTIONS >= 256
Entries.Section [entry] = section;
#else
Entries.Section {entry} = section;
#endif
entry = Entries.NextEntry [entry];
}
FileChanged = true;
return 1;
}
}
return 0;
}
private DOF2::SearchEntry (key [], tag [], keybuf [], valbuf [], &pos, keybufsize = sizeof (keybuf), valbufsize = sizeof (valbuf))
{
if (key [0] && Entries.Count)
{
new
entry = INVALID_ENTRY,
l,
m,
r,
h,
#if PACK_CONTENT == true
line [MAX_LINE_SIZE],
buf [MAX_SECTION_TAG],
#endif
i;
h = DOF2::HashKey (key);
l = 0;
r = Entries.Count - 1;
/*
* Binary search in a sorted list of entries in O(log n) time. This algorithm makes for example with 256 elements a maximum of ~8 steps until the entry is found if it exists.
* A sequential search would take up to 256 steps. That was the case in the first Double-O-Files script.
*/
while (l <= r)
{
if ((r - l) < 2)
{
if (h == SortedEntryList [l][0])
{
m = l;
entry = SortedEntryList [l][1];
}
else if (r > l && h == SortedEntryList [r][0])
{
m = r;
entry = SortedEntryList [r][1];
}
break;
}
else
{
m = l + (r - l) / 2;
if (h == SortedEntryList [m][0])
{
entry = SortedEntryList [m][1];
break;
}
else if (h > SortedEntryList [m][0])
l = m + 1;
else
r = m - 1;
}
}
// Candidate found?
if (entry != INVALID_ENTRY)
{
// Check if it's the entry we want.
#if PACK_CONTENT == true
strunpack (line, Entries.Line [entry]);
DOF2::ParseLine (line, keybuf, valbuf, keybufsize, valbufsize);
strunpack (buf, Entries.Tag [entry]);
if (!strcmp (keybuf, key, !CaseSensitive) && ((!tag [0] && !buf [0]) || (tag [0] && buf [0] && !strcmp (tag, buf, !CaseSensitive))))
#else
DOF2::ParseLine (Entries.Line [entry], keybuf, valbuf, keybufsize, valbufsize);
if (!strcmp (keybuf, key, !CaseSensitive) && ((!tag [0] && !Entries.Tag [entry][0]) || (tag [0] && Entries.Tag [entry][0] && !strcmp (tag, Entries.Tag [entry], !CaseSensitive))))
#endif
return (pos = m, entry);
else
{
// If not, look left and right in the list for entries with the same hash code. This can be collisions or entries with the same key from another section.
for (i = m - 1; i >= 0 && h == SortedEntryList [i][0]; --i)
{
entry = SortedEntryList [i][1];
#if PACK_CONTENT == true
strunpack (line, Entries.Line [entry]);
DOF2::ParseLine (line, keybuf, valbuf, keybufsize, valbufsize);
strunpack (buf, Entries.Tag [entry]);
if (!strcmp (keybuf, key, !CaseSensitive) && ((!tag [0] && !buf [0]) || (tag [0] && buf [0] && !strcmp (tag, buf, !CaseSensitive))))
#else
DOF2::ParseLine (Entries.Line [entry], keybuf, valbuf, keybufsize, valbufsize);
if (!strcmp (keybuf, key, !CaseSensitive) && ((!tag [0] && !Entries.Tag [entry][0]) || (tag [0] && Entries.Tag [entry][0] && !strcmp (tag, Entries.Tag [entry], !CaseSensitive))))
#endif
return (pos = i, entry);
}
for (i = m + 1; i < Entries.Count && h == SortedEntryList [i][0]; ++i)
{
entry = SortedEntryList [i][1];
#if PACK_CONTENT == true
strunpack (line, Entries.Line [entry]);
DOF2::ParseLine (line, keybuf, valbuf, keybufsize, valbufsize);
strunpack (buf, Entries.Tag [entry]);
if (!strcmp (keybuf, key, !CaseSensitive) && ((!tag [0] && !buf [0]) || (tag [0] && buf [0] && !strcmp (tag, buf, !CaseSensitive))))
#else
DOF2::ParseLine (Entries.Line [entry], keybuf, valbuf, keybufsize, valbufsize);
if (!strcmp (keybuf, key, !CaseSensitive) && ((!tag [0] && !Entries.Tag [entry][0]) || (tag [0] && Entries.Tag [entry][0] && !strcmp (tag, Entries.Tag [entry], !CaseSensitive))))
#endif
return (pos = i, entry);
}
}
}
}
keybuf [0] = valbuf [0] = '\0';
return INVALID_ENTRY;
}
stock DOF2::SetString (file [], key [], value [], tag [] = "")
{
if (file [0] && key [0])
{
new
entry,
pos,
section = INVALID_SECTION,
keybuf [MAX_LINE_SIZE],
valbuf [MAX_LINE_SIZE],
#if PACK_CONTENT == true
buf [MAX_SECTION_TAG],
line [MAX_LINE_SIZE],
#endif
i;
if (!CurrentFile [0] || strcmp (CurrentFile, file)) // No file in buffer or the file you want to read from is not the file in the buffer.
if (!DOF2::ParseFile (file, -1, false))
return 0;
entry = DOF2::SearchEntry (key, tag, keybuf, valbuf, pos);
// If the entry has been found, just change it's content.
if (entry != INVALID_ENTRY)
{
FileChanged = true;
#if PACK_CONTENT == true
format (line, sizeof (line), "%s = %s", key, value [0] ? value : ("(null)"));
return strpack (Entries.Line [entry], line);
#else
format (Entries.Line [entry], sizeof (Entries.Line []), "%s = %s", key, value [0] ? value : ("(null)"));
return 1;
#endif
}
if (Entries.Count >= MAX_ENTRIES)
return 0;
// Search for the section where the entry belongs.
if (!tag [0])
section = 0;
else
{
for (i = 1; i < Sections.Count; ++i)
{
#if PACK_CONTENT == true
strunpack (buf, Sections.Tag [i]);
if (buf [0] && !strcmp (tag, buf, !CaseSensitive))
{
section = i;
break;
}
#else
if (Sections.Tag [i][0] && !strcmp (tag, Sections.Tag [i], !CaseSensitive))
{
section = i;
break;
}
#endif
}
}
// Section we want does not exist, create new one if possible.
if (section == INVALID_SECTION)
{
if (Sections.Count >= MAX_SECTIONS)
return 0;
section = Sections.Count++;
#if PACK_CONTENT == true
strpack (Sections.Tag [section], tag);
#else
DOF2::strcpy (Sections.Tag [section], tag);
#endif
Sections.FirstEntry [section] = Sections.LastEntry [section] = INVALID_ENTRY;
}
// Add the entry to the section. Section's content is defined by a linear two way list.
#if PACK_CONTENT == true
format (line, sizeof (line), "%s = %s", key, value [0] ? value : ("(null)"));
strpack (Entries.Line [Entries.Count], line);
#else
format (Entries.Line [Entries.Count], sizeof (Entries.Line []), "%s = %s", key, value [0] ? value : ("(null)"));
#endif
Entries.Tag [Entries.Count] = Sections.Tag [section];
#if MAX_SECTIONS >= 256
Entries.Section [Entries.Count] = section;
#else
Entries.Section {Entries.Count} = section;
#endif
Entries.NextEntry [Entries.Count] = INVALID_ENTRY;
// Add entry to sorted list of entries and move to right correct position in O(n) time.
SortedEntryList [Entries.Count][0] = DOF2::HashKey (key);
SortedEntryList [Entries.Count][1] = Entries.Count;
i = Entries.Count - 1;
while (i >= 0 && SortedEntryList [i][0] > SortedEntryList [i + 1][0])
{
DOF2::SwapSortedEntries (SortedEntryList [i], SortedEntryList [i + 1]);
--i;
}
if (Sections.LastEntry [section] == INVALID_ENTRY) // No entry in this section.
{
Sections.FirstEntry [section] = Sections.LastEntry [section] = Entries.Count;
Entries.PreviousEntry [Entries.Count] = INVALID_ENTRY;
}
else
{
Entries.NextEntry [Sections.LastEntry [section]] = Entries.Count;
Entries.PreviousEntry [Entries.Count] = Sections.LastEntry [section];
Sections.LastEntry [section] = Entries.Count;
}
++Entries.Count;
FileChanged = true;
}
return 1;
}
stock DOF2::GetString (file [], key [], tag [] = "")
{
new buf [MAX_LINE_SIZE];
DOF2::GetStringEx (file, key, buf, sizeof (buf), tag);
return buf;
}
stock DOF2::GetStringEx (file [], key [], result [], size, tag [] = "")
{
if (file [0] && key [0])
{
new
pos,
keybuf [MAX_LINE_SIZE];
if (!CurrentFile [0] || strcmp (CurrentFile, file))
{
if (!DOF2::ParseFile (file, -1, false))
{
result [0] = '\0';
return 0;
}
}
// Find entry and assign the result with it's value.
return (DOF2::SearchEntry (key, tag, keybuf, result, pos, sizeof (keybuf), size) != INVALID_ENTRY);
}
return 0;
}
stock DOF2::Unset (file [], key [], tag [] = "")
{
if (file [0] && key [0])
{
new
entry,
pos,
keybuf [MAX_LINE_SIZE],
valbuf [MAX_LINE_SIZE];
if (!CurrentFile [0] || strcmp (CurrentFile, file))
if (!DOF2::ParseFile (file, -1, false))
return 0;
if ((entry = DOF2::SearchEntry (key, tag, keybuf, valbuf, pos)) != INVALID_ENTRY)
{
// Remove entry from it's section.
#if MAX_SECTIONS >= 256
if (Sections.FirstEntry [Entries.Section [entry]] == entry) // Is the entry the first entry in the section? Make it's next entry the first entry.
#else
if (Sections.FirstEntry [Entries.Section {entry}] == entry)
#endif
{
#if MAX_SECTIONS >= 256
Sections.FirstEntry [Entries.Section [entry]] = Entries.NextEntry [entry];
#else
Sections.FirstEntry [Entries.Section {entry}] = Entries.NextEntry [entry];
#endif
if (Entries.NextEntry [entry] != INVALID_ENTRY)
Entries.PreviousEntry [Entries.NextEntry [entry]] = INVALID_ENTRY;
}
else
{
Entries.NextEntry [Entries.PreviousEntry [entry]] = Entries.NextEntry [entry];
if (Entries.NextEntry [entry] != INVALID_ENTRY)
Entries.PreviousEntry [Entries.NextEntry [entry]] = Entries.PreviousEntry [entry];
}
#if MAX_SECTIONS >= 256
if (Sections.LastEntry [Entries.Section [entry]] == entry)
#else
if (Sections.LastEntry [Entries.Section {entry}] == entry)
#endif
{
#if MAX_SECTIONS >= 256
Sections.LastEntry [Entries.Section [entry]] = Entries.PreviousEntry [entry];
#else
Sections.LastEntry [Entries.Section {entry}] = Entries.PreviousEntry [entry];
#endif
if (Entries.PreviousEntry [entry] != INVALID_ENTRY)
Entries.NextEntry [Entries.PreviousEntry [entry]] = INVALID_ENTRY;
}
else
{
Entries.PreviousEntry [Entries.NextEntry [entry]] = Entries.PreviousEntry [entry];
if (Entries.PreviousEntry [entry] != INVALID_ENTRY)
Entries.NextEntry [Entries.PreviousEntry [entry]] = Entries.NextEntry [entry];
}
// Move the entry to the end of the sorted list and decrement Entries.Count to forget about the unset Entries.
while (pos < (Entries.Count - 1))
{
DOF2::SwapSortedEntries (SortedEntryList [pos], SortedEntryList [pos + 1]);
++pos;
}
--Entries.Count;
FileChanged = true;
return 1;
}
}
return 0;
}
stock DOF2::RenameKey (file [], oldkey [], newkey [], tag [] = "")
{
if (file [0] && oldkey [0])
{
new
entry,
pos,
#if PACK_CONTENT == true
line [MAX_LINE_SIZE],
#endif
keybuf [MAX_LINE_SIZE],
valbuf [MAX_LINE_SIZE];
if (!CurrentFile [0] || strcmp (CurrentFile, file))
if (!DOF2::ParseFile (file, -1, false))
return 0;
if ((entry = DOF2::SearchEntry (oldkey, tag, keybuf, valbuf, pos)) != INVALID_ENTRY)
{
// Change content of Entries.
#if PACK_CONTENT == true
format (line, sizeof (line), "%s = %s", newkey, valbuf [0] ? valbuf : ("(null)"));
strpack (Entries.Line [entry], line);
#else
format (Entries.Line [entry], sizeof (Entries.Line []), "%s = %s", newkey, valbuf [0] ? valbuf : ("(null)"));
#endif
// Because the hashcode has been changed, the entry has to move in the list.
SortedEntryList [pos][0] = DOF2::HashKey (newkey);
if (pos < (MAX_ENTRIES - 1) && SortedEntryList [pos][0] > SortedEntryList [pos + 1][0])
{
// Hash value of key is greater than the hash value of it's right neighbor, move to the right by swapping the 2 entries.
while (pos < (MAX_ENTRIES - 1) && SortedEntryList [pos][0] > SortedEntryList [pos + 1][0])
{
DOF2::SwapSortedEntries (SortedEntryList [pos], SortedEntryList [pos + 1]);
++pos;
}
}
else if (pos > 0 && SortedEntryList [pos][0] < SortedEntryList [pos + 1][0])
{
// Hash value of key is smaller than the hash value of it' left neighbor, move to the left by swapping the 2 entries.
while (pos > 0 && SortedEntryList [pos][0] < SortedEntryList [pos - 1][0])
{
DOF2::SwapSortedEntries (SortedEntryList [pos], SortedEntryList [pos - 1]);
--pos;
}
}
FileChanged = true;
return 1;
}
}
return 0;
}
stock bool: DOF2::IsSet (file [], key [], tag [] = "")
{
new
pos,
keybuf [MAX_LINE_SIZE],
valbuf [MAX_LINE_SIZE];
if (!CurrentFile [0] || strcmp (CurrentFile, file))
if (!DOF2::ParseFile (file, -1, false))
return false;
// Try to find the Entries.
return (DOF2::SearchEntry (key, tag, keybuf, valbuf, pos) != INVALID_ENTRY);
}
stock DOF2::SetInt (file [], key [], value, tag [] = "")
{
new buf [16];
format (buf, sizeof (buf), "%d", value);
return DOF2::SetString (file, key, buf, tag);
}
stock DOF2::GetInt (file [], key [], tag [] = "")
{
new buf [16];
DOF2::GetStringEx (file, key, buf, sizeof (buf), tag);
return strval (buf);
}
stock DOF2::SetHex (file [], key [], value, tag [] = "")
{
new buf [16];
DOF2::hexstr (value, buf);
return DOF2::SetString (file, key, buf, tag);
}
stock DOF2::GetHex (file [], key [], tag [] = "")
{
new buf [16];
DOF2::GetStringEx (file, key, buf, sizeof (buf), tag);
return DOF2::strhex (buf);
}
stock DOF2::SetBin (file [], key [], value, tag [] = "")
{
new buf [35];
DOF2::binstr (value, buf);
return DOF2::SetString (file, key, buf, tag);
}
stock DOF2::GetBin (file [], key [], tag [] = "")
{
new buf [35];
DOF2::GetStringEx (file, key, buf, sizeof (buf), tag);
return DOF2::strbin (buf);
}
stock DOF2::SetFloat (file [], key [], Float: value, tag [] = "")
{
new buf [32];
format (buf, sizeof (buf), "%.8f", value);
return DOF2::SetString (file, key, buf, tag);
}
stock Float: DOF2::GetFloat (file [], key [], tag [] = "")
{
new buf [32];
DOF2::GetStringEx (file, key, buf, sizeof (buf), tag);
return floatstr (buf);
}
stock bool: DOF2::GetBool (file [], key [], tag [] = "")
{
new buf [16];
DOF2::GetStringEx (file, key, buf, sizeof (buf), tag);
return (strval (buf) || (buf [0] && !strcmp (buf, "true", true)));
}
stock DOF2::SetBool (file [], key [], bool: value, tag [] = "")
return DOF2::SetString (file, key, value ? ("true") : ("false"), tag);
/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
stock DOF2::PrintFile (comment [] = "")
{
if (CurrentFile [0])
{
new
bool: firstline = true,
entry,
#if PACK_CONTENT == true
buf [MAX_LINE_SIZE],
#endif
entries,
i;
printf ("[DOF] Current file: %s", CurrentFile);
for ( ; i < Sections.Count; ++i)
{
if (i)
{
if (!firstline)
print (" ");
else
firstline = false;
#if PACK_CONTENT == true
strunpack (buf, Sections.Tag [i]);
printf ("[%s]", buf);
#else
printf ("[%s]", Sections.Tag [i]);
#endif
}
entry = Sections.FirstEntry [i];
while (entry != INVALID_ENTRY)
{
#if PACK_CONTENT == true
strunpack (buf, Entries.Line [entry]);
print (buf);
#else
print (Entries.Line [entry]);
#endif
entry = Entries.NextEntry [entry];
firstline = false;
++entries;
}
}
printf ("* %d sections, %d entries", i, entries);
if (comment [0])
printf ("* Comment: %s", comment);
return 1;
}
return 0;
}
stock DOF2::WriteFile ()
{
if (CurrentFile [0])
{
new
File: f = fopen (CurrentFile, io_write),
bool: firstline = true,
entry;
if (f)
{
for (new i; i < Sections.Count; ++i)
{
if (Sections.FirstEntry [i] != INVALID_ENTRY) // Do not write when empty.
{
if (i)
{
if (!firstline)
{
fputchar (f, '\r', UseUTF8);
fputchar (f, '\n', UseUTF8);
}
else
firstline = false;
fputchar (f, '[', UseUTF8);
fwritechars (f, Sections.Tag [i]);
fputchar (f, ']', UseUTF8);
fputchar (f, '\r', UseUTF8);
fputchar (f, '\n', UseUTF8);