-
Notifications
You must be signed in to change notification settings - Fork 71
/
RIni.ahk
2298 lines (2012 loc) · 63.5 KB
/
RIni.ahk
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
;Robert's INI library
;Version 1.7
#noenv ;Increases the speed of the library due to the large amount of dynamic variables.
SetBatchLines -1 ;Increases the overall speed of the script.
;-1 : Error, INI format is wrong
;-2 : Error, Sec not found
;-3 : Error, Key not found
;-4 : Error, Invalid optional paramater
;-5 : Error, Sec already exists
;-6 : Error, Key already exists
;-9 : Error, Reference number is already set
;-10 : Error, Reference number is invalid
;-11 : Error, Unable to read ini file
;-12 : Error, Unable to write ini file
;-13 : Error, Unable to delete existing ini file
;-14 : Error, Unable to rename temp ini file
;-15 : Error, Ini already exists
;Full function list at bottom
RIni_Read(1, A_ScriptDir "\test.ini")
RIni_Write(1, A_ScriptDir "\out test.ini")
RIni_Create(RVar, Correct_Errors=1)
{
Global
If (RVar = "")
Return -10
If (RIni_%RVar%_Is_Set != "")
Return -9
If (Correct_Errors = 1)
RIni_%RVar%_Fix_Errors := 1
Else If (Correct_Errors != 0)
Return -4
RIni_%RVar%_Is_Set := 1
, RIni_Unicode_Modifier := A_IsUnicode ? 2 : 1
, RIni_%RVar%_Section_Number := 1
}
RIni_Shutdown(RVar)
{
Global
Local Sec
If (RIni_%RVar%_Is_Set = "")
Return -10
If %RVar%_First_Comments
VarSetCapacity(%RVar%_First_Comments, 0)
Loop, % RIni_%RVar%_Section_Number
{
If (RIni_%RVar%_%A_Index% != ""){
Sec := RIni_CalcMD5(RIni_%RVar%_%A_Index%)
, VarSetCapacity(RIni_%RVar%_%A_Index%, 0)
, VarSetCapacity(RIni_%RVar%_%Sec%_Number, 0)
, VarSetCapacity(RIni_%RVar%_%Sec%_Is_Set, 0)
If %RVar%_%Sec%_Lone_Line_Comments
VarSetCapacity(%RVar%_%Sec%_Lone_Line_Comments, 0)
If %RVar%_%Sec%_Comment
VarSetCapacity(%RVar%_%Sec%_Comment, 0)
If (%RVar%_All_%Sec%_Keys){
Loop, Parse, %RVar%_All_%Sec%_Keys, `n
{
If A_Loopfield =
Continue
If (%RVar%_%Sec%_%A_LoopField%_Name != "")
VarSetCapacity(%RVar%_%Sec%_%A_LoopField%_Name, 0)
If (%RVar%_%Sec%_%A_LoopField%_Value != "")
VarSetCapacity(%RVar%_%Sec%_%A_LoopField%_Value, 0)
If %RVar%_%Sec%_%A_LoopField%_Comment
VarSetCapacity(%RVar%_%Sec%_%A_LoopField%_Comment, 0)
}
VarSetCapacity(%RVar%_All_%Sec%_Keys, 0)
}
}
}
If RIni_%RVar%_Fix_Errors
VarSetCapacity(RIni_%RVar%_Fix_Errors, 0)
VarSetCapacity(RIni_%RVar%_Is_Set, 0)
, VarSetCapacity(RIni_%RVar%_Section_Number, 0)
}
RIni_Read(RVar, File, Correct_Errors=1, Remove_Inline_Key_Comments=0, Remove_Lone_Line_Comments=0, Remove_Inline_Section_Comments=0, Treat_Duplicate_Sections=1, Treat_Duplicate_Keys=1, Read_Blank_Sections=1, Read_Blank_Keys=1, Trim_Spaces_From_Values=0, ByRef RIni_Read_Var = "")
{
;Treat_Duplicate_Sections
;1 - Skip
;2 - Append
;3 - Replace
;4 - Add new keys
;Treat_Duplicate_Keys
;1 - Skip
;2 - Append
;3 - Replace
Global
Local Has_Equal, Sec, Key, P_1, P_2, Section_Skip, C_Pos, Section_Append, T_Sections, T_Section, E, T_LoopField, Errored_Lines, T_Value
Local T_Section_Number, TSec, TKey, T_Section_Name
If (RVar = "")
Return -10
If (RIni_%RVar%_Is_Set != "")
Return -9
If (RIni_Read_Var = "")
{
FileRead, RIni_Read_Var, %File%
If Errorlevel
Return -11
}
If (Correct_Errors = 1)
RIni_%RVar%_Fix_Errors := 1
Else If (Correct_Errors != 0)
Return -4
RIni_Unicode_Modifier := A_IsUnicode ? 2 : 1
, RIni_%RVar%_Is_Set := 1
, RIni_%RVar%_Section_Number := 1
Loop, Parse, RIni_Read_Var, `n, `r
{
If A_LoopField =
Continue
T_LoopField = %A_LoopField%
If (SubStr(T_Loopfield, 1, 1) = ";"){
If !Section_Skip
{
If !Remove_Lone_Line_Comments
{
If Sec
%RVar%_%Sec%_Lone_Line_Comments .= A_LoopField "`n"
Else
%RVar%_First_Comments .= A_LoopField "`n"
}
}
Continue
}
Has_Equal := InStr(A_Loopfield, "=")
If (!Has_Equal and InStr(A_LoopField, "[") and InStr(A_LoopField, "]")){
Section_Skip := 0
, Section_Append := 0
, P_1 := InStr(A_LoopField, "[")
, P_2 := Instr(A_LoopField, "]")
, T_Section := Sec
, T_Section_Name := TSec
, TSec := SubStr(A_LoopField, P_1+1, P_2-P_1-1)
, Sec := RIni_CalcMD5(TSec)
If (T_Section)
If (T_Section != Sec)
If (!Read_Blank_Sections and !%RVar%_%T_Section%_Lone_Line_Comments and !%RVar%_%T_Section%_Comment and !%RVar%_All_%T_Section%_Keys)
RIni_DeleteSection(RVar, T_Section_Name)
If (RIni_%RVar%_%Sec%_Is_Set){
If (Treat_Duplicate_Sections = 1)
Section_Skip := 1
Else If (Treat_Duplicate_Sections = 2){
Section_Append := 1
If InStr(A_LoopField, ";")
If !Remove_Inline_Section_Comments
%RVar%_%Sec%_Comment .= SubStr(A_Loopfield, P_2+1)
} Else If (Treat_Duplicate_Sections = 3){
If (E := RIni_DeleteSection(RVar, TSec))
Return E
RIni_AddSection(RVar, TSec)
If InStr(A_LoopField, ";")
If !Remove_Inline_Section_Comments
%RVar%_%Sec%_Comment := SubStr(A_Loopfield, P_2+1)
} Else If (Treat_Duplicate_Sections = 4)
Section_Append := 2
Continue
} Else {
If InStr(A_LoopField, ";")
If !Remove_Inline_Section_Comments
%RVar%_%Sec%_Comment := SubStr(A_Loopfield, P_2+1)
RIni_AddSection(RVar, TSec)
}
Continue
}
If Has_Equal
{
If (!Sec)
{
If (RIni_%RVar%_Fix_Errors)
{
Errored_Lines .= (Errored_Lines = "" ? "" : ",") A_Index
Continue
}
VarSetCapacity(RIni_%RVar%_Fix_Errors, 0)
, VarSetCapacity(RIni_Unicode_Modifier, 0)
, VarSetCapacity(RIni_%RVar%_Is_Set, 0)
Return -1
}
If Section_Skip
Continue
TKey := SubStr(A_LoopField, 1, Has_Equal-1)
, Key := RIni_CalcMD5(TKey)
If (InStr("`n" %RVar%_All_%Sec%_Keys, "`n" Key "`n"))
{
If (Section_Append = 2)
Continue
Else If (Section_Append = 1){
C_Pos := InStr(A_LoopField, ";")
If (C_Pos){
If !Remove_Inline_Key_Comments
%RVar%_%Sec%_%Key%_Comment .= SubStr(A_LoopField, C_Pos)
%RVar%_%Sec%_%Key%_Value .= SubStr(A_LoopField, Has_Equal+1, C_Pos-Has_Equal-1)
} Else
%RVar%_%Sec%_%Key%_Value .= SubStr(A_Loopfield, Has_Equal+1)
} Else If (Treat_Duplicate_Keys = 1)
Continue
Else If (Treat_Duplicate_Keys = 2){
C_Pos := InStr(A_LoopField, ";")
If (C_Pos){
If !Remove_Inline_Key_Comments
%RVar%_%Sec%_%Key%_Comment .= SubStr(A_LoopField, C_Pos)
%RVar%_%Sec%_%Key%_Value .= SubStr(A_LoopField, Has_Equal+1, C_Pos-Has_Equal-1)
} Else
%RVar%_%Sec%_%Key%_Value .= SubStr(A_Loopfield, Has_Equal+1)
} Else If (Treat_Duplicate_Keys = 3){
RIni_DeleteKey(RVar, TSec, TKey)
, %RVar%_All_%Sec%_Keys .= Key "`n"
, C_Pos := InStr(A_LoopField, ";")
If (C_Pos){
If !Remove_Inline_Key_Comments
%RVar%_%Sec%_%Key%_Comment := SubStr(A_LoopField, C_Pos)
%RVar%_%Sec%_%Key%_Value := SubStr(A_LoopField, Has_Equal+1, C_Pos-Has_Equal-1)
} Else
%RVar%_%Sec%_%Key%_Value := SubStr(A_Loopfield, Has_Equal+1)
%RVar%_%Sec%_%Key%_Name := TKey
}
If (Trim_Spaces_From_Values)
T_Value := %RVar%_%Sec%_%Key%_Value
, %RVar%_%Sec%_%Key%_Value = %T_Value%
} Else {
C_Pos := InStr(A_LoopField, ";")
If (C_Pos){
If !Remove_Inline_Key_Comments
%RVar%_%Sec%_%Key%_Comment := SubStr(A_LoopField, C_Pos)
%RVar%_%Sec%_%Key%_Value := SubStr(A_LoopField, Has_Equal+1, C_Pos-Has_Equal-1)
} Else {
%RVar%_%Sec%_%Key%_Value := SubStr(A_Loopfield, Has_Equal+1)
If (!Read_Blank_Keys and %RVar%_%Sec%_%Key%_Value != "")
Continue
}
If (Trim_Spaces_From_Values)
T_Value := %RVar%_%Sec%_%Key%_Value
, %RVar%_%Sec%_%Key%_Value = %T_Value%
%RVar%_All_%Sec%_Keys .= Key "`n"
, %RVar%_%Sec%_%Key%_Name := TKey
}
Continue
}
If (RIni_%RVar%_Fix_Errors)
Errored_Lines .= (Errored_Lines = "" ? "" : ",") A_Index
}
VarSetCapacity(RIni_Read_Var, 0)
If Errored_Lines
Return Errored_Lines
}
RIni_Write(RVar, File, Newline="`r`n", Write_Blank_Sections=1, Write_Blank_Keys=1, Space_Sections=1, Space_Keys=0, Remove_Valuewlines=1, Overwrite_If_Exists=1, Addwline_At_End=0)
{
Global
Local Write_Ini, Sec, Length, Temp_Write_Ini, T_Time, T_Section, T_Key, T_Value, T_Size, E, T_Write_Section
If !RIni_%RVar%_Is_Set
Return -10
If (Newline != "`n" and Newline != "`r" and Newline != "`r`n" and Newline != "`n`r")
Return -4
T_Size := RIni_GetTotalSize(RVar, Newline)
If (T_Size < 0)
Return T_Size
If Space_Sections
T_Size += 1*1024*1024
If Space_Keys
T_Size += 1*1024*1024
VarSetCapacity(Write_Ini, T_Size)
If (%RVar%_First_Comments){
Loop, parse, %RVar%_First_Comments, `n, `r
{
If A_LoopField =
Continue
Write_Ini .= A_LoopField Newline
}
}
Loop, % RIni_%RVar%_Section_Number
{
If (RIni_%RVar%_%A_Index% != ""){
If (T_Write_Section != ""){
If Space_Sections
Write_Ini .= Newline
}
T_Write_Section := RIni_%RVar%_%A_Index%
T_Section := RIni_CalcMD5(T_Write_Section)
If %RVar%_%T_Section%_Comment
Write_Ini .= "[" T_Write_Section "]" %RVar%_%T_Section%_Comment Newline
Else {
If (!Write_Blank_Sections and !%RVar%_All_%T_Section%_Keys and !%RVar%_%T_Section%_Lone_Line_Comments)
Continue
Write_Ini .= "[" T_Write_Section "]" Newline
}
If (%RVar%_All_%T_Section%_Keys){
Loop, Parse, %RVar%_All_%T_Section%_Keys, `n
{
If A_LoopField =
Continue
If (T_Key){
If Space_Keys
Write_Ini .= Newline
}
T_Key := %RVar%_%T_Section%_%A_LoopField%_Name
T_Value := %RVar%_%T_Section%_%A_LoopField%_Value
If (Remove_Valuewlines){
If InStr(T_Value, "`n")
StringReplace, T_Value, T_Value, `n, ,A
If InStr(T_Value, "`r")
StringReplace, T_Value, T_Value, `r, ,A
}
If %RVar%_%T_Section%_%A_LoopField%_Comment
Write_Ini .= T_Key "=" T_Value %RVar%_%T_Section%_%A_LoopField%_Comment Newline
Else {
If (!Write_Blank_Keys and T_Value = "")
Continue
Write_Ini .= T_Key "=" T_Value Newline
}
}
}
If (%RVar%_%T_Section%_Lone_Line_Comments){
Loop, parse, %RVar%_%T_Section%_Lone_Line_Comments, `n, `r
{
If A_LoopField =
Continue
Write_Ini .= A_LoopField Newline
}
}
}
}
If (!Addwline_At_End and StrLen(Write_Ini) < (63 * 1024 * 1024))
Write_Ini := SubStr(Write_Ini, 1, StrLen(Write_Ini)-StrLen(Newline))
IfExist, %File%
{
If !Overwrite_If_Exists
Return -15
T_Time := A_Now
If A_IsUnicode
FileAppend, %Write_Ini%, %A_Temp%\%T_Time%.ini, UTF-8
Else
FileAppend, %Write_Ini%, %A_Temp%\%T_Time%.ini
If ErrorLevel
Return -12
FileDelete, %File%
If ErrorLevel
Return -13
FileMove, %A_Temp%\%T_Time%.ini, %File%
If ErrorLevel
Return -14
} Else {
If A_IsUnicode
FileAppend, %Write_Ini%, %File%, UTF-8
Else
FileAppend, %Write_Ini%, %File%
If ErrorLevel
Return -12
}
Write_Ini := ""
}
RIni_AddSection(RVar, Sec)
{
Global
Local T_Section_Number, TSec
If !RIni_%RVar%_Is_Set
Return -10
TSec := RIni_CalcMD5(Sec)
If RIni_%RVar%_%TSec%_Is_Set
Return -5
RIni_%RVar%_%TSec%_Is_Set := 1
T_Section_Number := RIni_%RVar%_Section_Number
RIni_%RVar%_%TSec%_Number := T_Section_Number
RIni_%RVar%_%T_Section_Number% := Sec
RIni_%RVar%_Section_Number ++
}
RIni_AddKey(RVar, Sec, Key)
{
Global
Local E, TKey, TSec
If !RIni_%RVar%_Is_Set
Return -10
TSec := Sec
Sec := RIni_CalcMD5(TSec)
If (!RIni_%RVar%_%Sec%_Is_Set){
If !RIni_%RVar%_Fix_Errors
Return -2
RIni_AddSection(RVar, TSec)
}
TKey := Key
Key := RIni_CalcMD5(TKey)
If (InStr("`n" %RVar%_All_%Sec%_Keys, "`n" Key "`n"))
Return -6
%RVar%_All_%Sec%_Keys .= Key "`n"
%RVar%_%Sec%_%Key%_Name := TKey
}
RIni_AppendValue(RVar, Sec, Key, Value, Trim_Spaces_From_Value=0, Removewlines=1)
{
Global
Static TSec, TKey, MD5Sec, MD5Key
Local E
If !RIni_%RVar%_Is_Set
Return -10
If (TSec != Sec)
TSec := Sec
, MD5Sec := RIni_CalcMD5(TSec)
If (!RIni_%RVar%_%MD5Sec%_Is_Set){
If !RIni_%RVar%_Fix_Errors
Return -2
RIni_AddSection(RVar, TSec)
}
If (TKey != Key)
TKey := Key
, MD5Key := RIni_CalcMD5(TKey)
If (!InStr("`n" %RVar%_All_%MD5Sec%_Keys, "`n" MD5Key "`n")){
If !RIni_%RVar%_Fix_Errors
Return -3
%RVar%_All_%MD5Sec%_Keys .= MD5Key "`n"
%RVar%_%MD5Sec%_%MD5Key%_Name := TKey
}
If (Removewlines){
If InStr(Value, "`n")
StringReplace, Value, Value, `n, ,A
If InStr(Value, "`r")
StringReplace, Value, Value, `r, ,A
}
If Trim_Spaces_From_Value
Value = %Value%
%RVar%_%MD5Sec%_%MD5Key%_Value .= Value
}
RIni_ExpandSectionKeys(RVar, Sec, Amount=1)
{
Global
Local Temp_All_Section_Keys, Length, E, TSec
If !RIni_%RVar%_Is_Set
Return -10
TSec := Sec
Sec := RIni_CalcMD5(TSec)
If (!RIni_%RVar%_%Sec%_Is_Set){
If !RIni_%RVar%_Fix_Errors
Return -2
RIni_AddSection(RVar, TSec)
}
Length := StrLen(%RVar%_All_%Sec%_Keys)
VarSetCapacity(Temp_All_Section_Keys, RIni_Unicode_Modifier*Length)
Temp_All_Section_Keys .= %RVar%_All_%Sec%_Keys
VarSetCapacity(%RVar%_All_%Sec%_Keys, Round(RIni_Unicode_Modifier*(Length+Amount*(1*1024*1024))))
%RVar%_All_%Sec%_Keys .= Temp_All_Section_Keys
}
RIni_ContractSectionKeys(RVar, Sec)
{
Global
Local Temp_All_Section_Keys, Length
Sec := RIni_CalcMD5(Sec)
Length := StrLen(%RVar%_All_%Sec%_Keys)
VarSetCapacity(Temp_All_Section_Keys, RIni_Unicode_Modifier*Length)
Temp_All_Section_Keys .= %RVar%_All_%Sec%_Keys
VarSetCapacity(%RVar%_All_%Sec%_Keys, 0)
VarSetCapacity(%RVar%_All_%Sec%_Keys, RIni_Unicode_Modifier*Length)
%RVar%_All_%Sec%_Keys .= Temp_All_Section_Keys
}
RIni_ExpandKeyValue(RVar, Sec, Key, Amount=1)
{
Global
Local Temp_Key_value, Length, E, TSec, TKey
If !RIni_%RVar%_Is_Set
Return -10
TSec := Sec
Sec := RIni_CalcMD5(TSec)
If (!RIni_%RVar%_%Sec%_Is_Set){
If !RIni_%RVar%_Fix_Errors
Return -2
RIni_AddSection(RVar, Sec)
}
TKey := Key
Key := RIni_CalcMD5(TKey)
If (!InStr("`n" %RVar%_All_%Sec%_Keys, "`n" Key "`n")){
If !RIni_%RVar%_Fix_Errors
Return -3
%RVar%_All_%Sec%_Keys .= Key "`n"
%RVar%_%Sec%_%Key%_Name := TKey
}
If (%RVar%_%Sec%_%Key%_Value = "")
VarSetCapacity(%RVar%_%Sec%_%Key%_Value, Round(RIni_Unicode_Modifier*Amount*(1*1024*1024)))
Else {
Length := StrLen(%RVar%_%Sec%_%Key%_Value)
VarSetCapacity(Temp_Key_value, RIni_Unicode_Modifier*Length)
Temp_Key_value .= %RVar%_%Sec%_%Key%_Value
varSetCapacity(%RVar%_%Sec%_%Key%_Value, Round(RIni_Unicode_Modifier*(Length+Amount*(1*1024*1024))))
%RVar%_%Sec%_%Key%_Value .= Temp_Key_value
}
}
RIni_ContractKeyValue(RVar, Sec, Key)
{
Global
Local Temp_Key_value, Length, TSec, TKey
TSec := Sec
Sec := RIni_CalcMD5(TSec)
TKey := Key
Key := RIni_CalcMD5(TKey)
If (%RVar%_%Sec%_%Key%_Value = "")
VarSetCapacity(%RVar%_%Sec%_%Key%_Value, 0)
Else {
Length := StrLen(%RVar%_%Sec%_%Key%_Value)
VarSetCapacity(Temp_Key_value, RIni_Unicode_Modifier*Length)
Temp_Key_value .= %RVar%_%Sec%_%Key%_Value
VarSetCapacity(%RVar%_%Sec%_%Key%_Value, 0)
varSetCapacity(%RVar%_%Sec%_%Key%_Value, RIni_Unicode_Modifier*Length)
%RVar%_%Sec%_%Key%_Value .= Temp_Key_value
}
}
RIni_SetKeyValue(RVar, Sec, Key, Value, Trim_Spaces_From_Value=0, Removewlines=1)
{
Global
Local E, TSec, TKey
If !RIni_%RVar%_Is_Set
Return -10
TSec := Sec
Sec := RIni_CalcMD5(TSec)
If (!RIni_%RVar%_%Sec%_Is_Set){
If !RIni_%RVar%_Fix_Errors
Return -2
RIni_AddSection(RVar, TSec)
}
TKey := Key
Key := RIni_CalcMD5(TKey)
If (!InStr("`n" %RVar%_All_%Sec%_Keys, "`n" Key "`n")){
If !RIni_%RVar%_Fix_Errors
Return -3
%RVar%_All_%Sec%_Keys .= Key "`n"
%RVar%_%Sec%_%Key%_Name := TKey
}
If (Removewlines){
If InStr(Value, "`n")
StringReplace, Value, Value, `n, ,A
If InStr(Value, "`r")
StringReplace, Value, Value, `r, ,A
}
If Trim_Spaces_From_Value
Value = %Value%
%RVar%_%Sec%_%Key%_Value := Value
}
RIni_DeleteSection(RVar, Sec)
{
Global
Local Position, T_Section_Number
If !RIni_%RVar%_Is_Set
Return -10
Sec := RIni_CalcMD5(Sec)
If (RIni_%RVar%_%Sec%_Is_Set){
T_Section_Number := RIni_%RVar%_%Sec%_Number
VarSetCapacity(RIni_%RVar%_%T_Section_Number%, 0)
VarSetCapacity(RIni_%RVar%_%Sec%_Is_Set, 0)
VarSetCapacity(RIni_%RVar%_%Sec%_Number, 0)
If (%RVar%_All_%Sec%_Keys){
Loop, Parse, %RVar%_All_%Sec%_Keys, `n
{
If A_LoopField =
Continue
VarSetCapacity(%RVar%_%Sec%_%A_LoopField%_Name, 0)
If (%RVar%_%Sec%_%A_LoopField%_Value != "")
VarSetCapacity(%RVar%_%Sec%_%A_LoopField%_Value, 0)
If %RVar%_%Sec%_%A_LoopField%_Comment
VarSetCapacity(%RVar%_%Sec%_%A_LoopField%_Comment, 0)
}
VarSetCapacity(%RVar%_All_%Sec%_Keys, 0)
}
If %RVar%_%Sec%_Comment
VarSetCapacity(%RVar%_%Sec%_Comment, 0)
If %RVar%_%Sec%_Lone_Line_Comments
VarSetCapacity(%RVar%_%Sec%_Lone_Line_Comments, 0)
} Else
Return -2
}
RIni_DeleteKey(RVar, Sec, Key)
{
Global
Local Position, TSec, TKey
If !RIni_%RVar%_Is_Set
Return -10
TSec := Sec
Sec := RIni_CalcMD5(TSec)
If !RIni_%RVar%_%Sec%_Is_Set
Return -2
TKey := Key
Key := RIni_CalcMD5(TKey)
If (%RVar%_All_%Sec%_Keys){
If (Key = SubStr(%RVar%_All_%Sec%_Keys, 1, Instr(%RVar%_All_%Sec%_Keys, "`n")-1)){
%RVar%_All_%Sec%_Keys := SubStr(%RVar%_All_%Sec%_Keys, InStr(%RVar%_All_%Sec%_Keys, "`n")+1)
} Else {
Position := InStr(%RVar%_All_%Sec%_Keys, "`n" Key "`n")
If !Position
Return -3
%RVar%_All_%Sec%_Keys := SubStr(%RVar%_All_%Sec%_Keys, 1, Position) SubStr(%RVar%_All_%Sec%_Keys, Position+2+StrLen(Key))
If Errorlevel
Return -3
}
VarSetCapacity(%RVar%_%Sec%_%Key%_Name, 0)
If (%RVar%_%Sec%_%Key%_Value != "")
VarSetCapacity(%RVar%_%Sec%_%Key%_Value, 0)
If %RVar%_%Sec%_%Key%_Comment
VarSetCapacity(%RVar%_%Sec%_%Key%_Comment, 0)
} Else
Return -3
}
RIni_GetSections(RVar, Delimiter=",")
{
Global
Local T_Sections
If !RIni_%RVar%_Is_Set
Return -10
Loop, % RIni_%RVar%_Section_Number
{
If (RIni_%RVar%_%A_Index%){
If T_Sections
T_Sections .= Delimiter RIni_%RVar%_%A_Index%
Else
T_Sections := RIni_%RVar%_%A_Index%
}
}
Return T_Sections
}
RIni_GetSectionKeys(RVar, Sec, Delimiter=",")
{
Global
Local T_Section_Keys
If !RIni_%RVar%_Is_Set
Return -10
Sec := RIni_CalcMD5(Sec)
If !RIni_%RVar%_%Sec%_Is_Set
Return -2
If (%RVar%_All_%Sec%_Keys){
Loop, Parse, %RVar%_All_%Sec%_Keys, `n
{
If A_LoopField =
Continue
T_Section_Keys .= (A_Index = 1 ? "" : Delimiter) %RVar%_%Sec%_%A_LoopField%_Name
}
Return T_Section_Keys
}
}
RIni_GetKeyValue(RVar, Sec, Key, Default_Return="")
{
Global
If (!RIni_%RVar%_Is_Set)
Return Default_Return = "" ? -10 : Default_Return
Sec := RIni_CalcMD5(Sec)
If !RIni_%RVar%_%Sec%_Is_Set
Return Default_Return = "" ? -2 : Default_Return
Key := RIni_CalcMD5(Key)
If (%RVar%_All_%Sec%_Keys){
If (!InStr("`n" %RVar%_All_%Sec%_Keys, "`n" Key "`n"))
Return Default_Return = "" ? -3 : Default_Return
If (%RVar%_%Sec%_%Key%_Value != "")
Return %RVar%_%Sec%_%Key%_Value
Else
Return Default_Return
} Else
Return Default_Return = "" ? -3 : Default_Return
}
RIni_CopyKeys(From_RVar, To_RVar, From_Section, To_Section, Treat_Duplicate_Keys=2, Copy_Key_Values=1, Copy_Key_Comments=1, Copy_Blank_Keys=1)
{
;Treat_Duplicate_? = 1 : Skip
;Treat_Duplicate_? = 2 : Append
;Treat_Duplicate_? = 3 : Replace
Global
Local E, TTo_Section
If (From_RVar = "" Or !RIni_%From_RVar%_Is_Set)
Return -10
If (To_RVar = "")
Return -10
If (!RIni_%To_RVar%_Is_Set){
If !RIni_%From_RVar%_Fix_Errors
Return -10
RIni_Create(To_RVar)
}
If (Treat_Duplicate_Keys != 1 and Treat_Duplicate_Keys != 2 and Treat_Duplicate_Keys != 3)
Return -4
From_Section := RIni_CalcMD5(From_Section)
TTo_Section := To_Section
To_Section := RIni_CalcMD5(TTo_Section)
If !RIni_%From_RVar%_%From_Section%_Is_Set
Return -2
If (!RIni_%To_RVar%_%To_Section%_Is_Set){
If !RIni_%To_RVar%_Fix_Errors
Return -2
RIni_AddSection(To_RVar, TTo_Section)
}
If (%From_RVar%_All_%From_Section%_Keys){
Loop, Parse, %From_RVar%_All_%From_Section%_Keys, `n
{
If A_Loopfield =
Continue
If (!Copy_Blank_Keys and %From_RVar%_%From_Section%_%A_Loopfield%_Value != 0 and !%From_RVar%_%From_Section%_%A_Loopfield%_Value and !%From_RVar%_%From_Section%_%A_Loopfield%_Comment)
Continue
If (!InStr("`n" %To_RVar%_All_%To_Section%_Keys, "`n" A_LoopField "`n")){
%To_RVar%_All_%To_Section%_Keys .= A_LoopField "`n"
%To_RVar%_%To_Section%_%A_LoopField%_Name := %From_RVar%_%From_Section%_%A_LoopField%_Name
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "" and Copy_Key_Values)
%To_RVar%_%To_Section%_%A_Loopfield%_Value := %From_RVar%_%From_Section%_%A_Loopfield%_Value
If (%From_RVar%_%From_Section%_%A_Loopfield%_Comment and Copy_Key_Comments)
%To_RVar%_%To_Section%_%A_Loopfield%_Comment := %From_RVar%_%From_Section%_%A_Loopfield%_Comment
} Else {
If (Treat_Duplicate_Keys = 1)
Continue
If (Treat_Duplicate_Keys = 2){
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "" and Copy_Key_Values)
%To_RVar%_%To_Section%_%A_Loopfield%_Value .= %From_RVar%_%From_Section%_%A_Loopfield%_Value
If (%From_RVar%_%From_Section%_%A_Loopfield%_Comment and Copy_Key_Comments)
%To_RVar%_%To_Section%_%A_Loopfield%_Comment .= %From_RVar%_%From_Section%_%A_Loopfield%_Comment
}
If (Treat_Duplicate_Keys = 3){
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "" and Copy_Key_Values)
%To_RVar%_%To_Section%_%A_Loopfield%_Value := %From_RVar%_%From_Section%_%A_Loopfield%_Value
If (%From_RVar%_%From_Section%_%A_Loopfield%_Comment and Copy_Key_Comments)
%To_RVar%_%To_Section%_%A_Loopfield%_Comment := %From_RVar%_%From_Section%_%A_Loopfield%_Comment
}
}
}
}
}
RIni_Merge(From_RVar, To_RVar, Treat_Duplicate_Sections=1, Treat_Duplicate_Keys=2, Merge_Blank_Sections=1, Merge_Blank_Keys=1)
{
;Treat_Duplicate_? = 1 : Skip
;Treat_Duplicate_? = 2 : Append
;Treat_Duplicate_? = 3 : Replace
Global
Local From_Section, E, T_Section_Number, TFrom_Section
If (From_RVar = "" Or !RIni_%From_RVar%_Is_Set)
Return -10
If (To_RVar = "")
Return -10
If (!RIni_%To_RVar%_Is_Set){
If !RIni_%From_RVar%_Fix_Errors
Return -10
RIni_Create(To_RVar)
}
If (Treat_Duplicate_Sections != 1 and Treat_Duplicate_Sections != 2 and Treat_Duplicate_Sections != 3)
Return -4
If (Treat_Duplicate_Keys != 1 and Treat_Duplicate_Keys != 2 and Treat_Duplicate_Keys != 3)
Return -4
If %From_RVar%_First_Comments
%To_RVar%_First_Comments .= %From_RVar%_First_Comments
Loop, % RIni_%From_RVar%_Section_Number
{
If (RIni_%From_RVar%_%A_Index% != ""){
TFrom_Section := RIni_%From_RVar%_%A_Index%
From_Section := RIni_CalcMD5(TFrom_Section)
If (!Merge_Blank_Sections and !%From_RVar%_%From_Section%_Lone_Line_Comments and !%From_RVar%_%From_Section%_Comment and !%From_RVar%_All_%From_Section%_Keys)
Continue
If (!RIni_%To_RVar%_%From_Section%_Is_Set){
RIni_AddSection(To_RVar, TFrom_Section)
If %From_RVar%_%From_Section%_Comment
%To_RVar%_%From_Section%_Comment := %From_RVar%_%From_Section%_Comment
If %From_RVar%_%From_Section%_Lone_Line_Comments
%To_RVar%_%From_Section%_Lone_Line_Comments := %From_RVar%_%From_Section%_Lone_Line_Comments
If (%From_RVar%_All_%From_Section%_Keys){
Loop, Parse, %From_RVar%_All_%From_Section%_Keys, `n
{
If A_Loopfield =
Continue
If (!Merge_Blank_Keys and %From_RVar%_%From_Section%_%A_Loopfield%_Value = "" !%From_RVar%_%From_Section%_%A_LoopField%_Comment)
Continue
If (!InStr("`n" %To_RVar%_All_%From_Section%_Keys, "`n" A_LoopField "`n")){
%To_RVar%_All_%From_Section%_Keys .= A_LoopField "`n"
%To_RVar%_%From_Section%_%A_LoopField%_Name := %From_RVar%_%From_Section%_%A_Loopfield%_Name
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "")
%To_RVar%_%From_Section%_%A_Loopfield%_Value := %From_RVar%_%From_Section%_%A_Loopfield%_Value
If %From_RVar%_%From_Section%_%A_LoopField%_Comment
%To_RVar%_%From_Section%_%A_LoopField%_Comment := %From_RVar%_%From_Section%_%A_LoopField%_Comment
} Else {
If (Treat_Duplicate_Keys = 1)
Continue
If (Treat_Duplicate_Keys = 2){
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "")
%To_RVar%_%From_Section%_%A_Loopfield%_Value .= %From_RVar%_%From_Section%_%A_Loopfield%_Value
If %From_RVar%_%From_Section%_%A_LoopField%_Comment
%To_RVar%_%From_Section%_%A_LoopField%_Comment .= %From_RVar%_%From_Section%_%A_LoopField%_Comment
}
If (Treat_Duplicate_Keys = 3){
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "")
%To_RVar%_%From_Section%_%A_Loopfield%_Value := %From_RVar%_%From_Section%_%A_Loopfield%_Value
If %From_RVar%_%From_Section%_%A_LoopField%_Comment
%To_RVar%_%From_Section%_%A_LoopField%_Comment := %From_RVar%_%From_Section%_%A_LoopField%_Comment
}
}
}
}
} Else {
If (Treat_Duplicate_Sections = 1)
Continue
If (Treat_Duplicate_Sections = 2){
If %From_RVar%_%From_Section%_Comment
%To_RVar%_%From_Section%_Comment .= %From_RVar%_%From_Section%_Comment
If %From_RVar%_%From_Section%_Lone_Line_Comments
%To_RVar%_%From_Section%_Lone_Line_Comments .= %From_RVar%_%From_Section%_Lone_Line_Comments
If (%From_RVar%_All_%From_Section%_Keys){
Loop, Parse, %From_RVar%_All_%From_Section%_Keys, `n
{
If A_Loopfield =
Continue
If (!Merge_Blank_Keys and %From_RVar%_%From_Section%_%A_Loopfield%_Value != 0 and !%From_RVar%_%From_Section%_%A_Loopfield%_Value and !%From_RVar%_%From_Section%_%A_LoopField%_Comment)
Continue
If (!InStr("`n" %To_RVar%_All_%From_Section%_Keys, "`n" A_LoopField "`n")){
%To_RVar%_All_%From_Section%_Keys .= A_LoopField "`n"
%To_RVar%_%From_Section%_%A_LoopField%_Name := %From_RVar%_%From_Section%_%A_Loopfield%_Name
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "")
%To_RVar%_%From_Section%_%A_Loopfield%_Value := %From_RVar%_%From_Section%_%A_Loopfield%_Value
If %From_RVar%_%From_Section%_%A_LoopField%_Comment
%To_RVar%_%From_Section%_%A_LoopField%_Comment := %From_RVar%_%From_Section%_%A_LoopField%_Comment
} Else {
If (Treat_Duplicate_Keys = 1)
Continue
If (Treat_Duplicate_Keys = 2){
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "")
%To_RVar%_%From_Section%_%A_Loopfield%_Value .= %From_RVar%_%From_Section%_%A_Loopfield%_Value
If %From_RVar%_%From_Section%_%A_LoopField%_Comment
%To_RVar%_%From_Section%_%A_LoopField%_Comment .= %From_RVar%_%From_Section%_%A_LoopField%_Comment
}
If (Treat_Duplicate_Keys = 3){
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "")
%To_RVar%_%From_Section%_%A_Loopfield%_Value := %From_RVar%_%From_Section%_%A_Loopfield%_Value
If %From_RVar%_%From_Section%_%A_LoopField%_Comment
%To_RVar%_%From_Section%_%A_LoopField%_Comment := %From_RVar%_%From_Section%_%A_LoopField%_Comment
}
}
}
}
}
If (Treat_Duplicate_Sections = 3){
If (E := RIni_DeleteSection(To_RVar, TFrom_Section))
Return E
RIni_AddSection(To_RVar, TFrom_Section)
If %From_RVar%_%From_Section%_Comment
%To_RVar%_%From_Section%_Comment := %From_RVar%_%From_Section%_Comment
If %From_RVar%_%From_Section%_Lone_Line_Comments
%To_RVar%_%From_Section%_Lone_Line_Comments := %From_RVar%_%From_Section%_Lone_Line_Comments
If (%From_RVar%_All_%From_Section%_Keys){
%To_RVar%_All_%From_Section%_Keys := %From_RVar%_All_%From_Section%_Keys
Loop, Parse, %From_RVar%_All_%From_Section%_Keys, `n
{
If A_Loopfield =
Continue
If (!Merge_Blank_Keys and %From_RVar%_%From_Section%_%A_Loopfield%_Value = "" !%From_RVar%_%From_Section%_%A_LoopField%_Comment)
Continue
%To_RVar%_%From_Section%_%A_LoopField%_Name := %From_RVar%_%From_Section%_%A_Loopfield%_Name
If (%From_RVar%_%From_Section%_%A_Loopfield%_Value != "")
%To_RVar%_%From_Section%_%A_Loopfield%_Value := %From_RVar%_%From_Section%_%A_Loopfield%_Value
If %From_RVar%_%From_Section%_%A_LoopField%_Comment
%To_RVar%_%From_Section%_%A_LoopField%_Comment := %From_RVar%_%From_Section%_%A_LoopField%_Comment
}
}
}
}
}
}
}
RIni_ToVariable(RVar, ByRef Variable, Newline="`r`n", Add_Blank_Sections=1, Add_Blank_Keys=1, Space_Sections=0, Space_Keys=0, Remove_Valuewlines=1)
{
Global
Local Sec, Length, Key, Value, T_Section, TKey, T_Value
If (!RIni_%RVar%_Is_Set)
Return -10
If (Newline != "`n" and Newline != "`r" and Newline != "`r`n" and Newline != "`n`r")
Return -4
If (%RVar%_First_Comments){
Loop, parse, %RVar%_First_Comments, `n, `r
{
If A_LoopField =
Continue
Variable .= A_LoopField Newline
}
}
Loop, % RIni_%RVar%_Section_Number
{
If (RIni_%RVar%_%A_Index% != ""){
If (Sec){
If Space_Sections
Variable .= Newline
}
T_Section := RIni_%RVar%_%A_Index%
Sec := RIni_CalcMD5(T_Section)
If (%RVar%_%Sec%_Comment)
Variable .= "[" T_Section "]" %RVar%_%Sec%_Comment Newline
Else {
If (!Add_Blank_Sections And !%RVar%_All_%Sec%_Keys And !%RVar%_%Sec%_Lone_Line_Comments)
Continue
Variable .= "[" T_Section "]" Newline
}
Loop, Parse, %RVar%_All_%Sec%_Keys, `n
{
If A_LoopField =
Continue
If (TKey){
If Space_Keys
Variable .= Newline
}
TKey := %RVar%_%Sec%_%A_LoopField%_Name
T_Value := ""
If (%RVar%_%Sec%_%A_LoopField%_Value != ""){
T_Value := %RVar%_%Sec%_%A_LoopField%_Value
If (Remove_Valuewlines){
If InStr(T_Value, "`n")