-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsectionlist.go
1155 lines (999 loc) · 31.9 KB
/
sectionlist.go
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
/*
Copyright © 2019, 2024 M.Watermann, 10247 Berlin, Germany
All rights reserved
EMail : <[email protected]>
*/
package ini
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
//lint:file-ignore ST1017 - I prefer Yoda conditions
type (
// `tSections` is a list (map) of INI sections.
tSections map[string]*TSection
// A helper slice of strings (i.e. section names)
// used to preserve the order of INI sections.
tSectionOrder = []string
// `TSectionList` is a list of INI sections.
//
// This opaque data structure is filled by e.g. `load()`.
//
// For accessing the sections and key/value pairs it provides
// the appropriate methods.
TSectionList struct {
defSect string // name of default section
fName string // name of the INI file to use
secOrder tSectionOrder // slice containing the order of sections
sections tSections // map of INI sections
}
// `TIniWalkFunc()` is used by `Walk()` when visiting an entry
// in the INI list.
//
// see `Walk()`
TIniWalkFunc func(aSection, aKey, aVal string)
// A `TIniWalker` is used by `Walker()` when visiting an entry
// in the INI list.
//
// see `Walker()`
TIniWalker interface {
Walk(aSection, aKey, aVal string)
}
)
const (
// `DefSection` is the name of the default section in the INI file
// which is used when there are key/value pairs in the file
// without a preceding section header like `[SectionName]`.
DefSection = `Default`
// Default list capacity.
slDefCapacity = 16
)
// Regular expressions to identify certain parts of an INI file.
var (
// match: [section]
isSectionRE = regexp.MustCompile(`^\[\s*([^\]]*?)\s*]$`)
// match: key = val
isKeyValRE = regexp.MustCompile(`^([^=]+?)\s*=\s*(.*)$`)
// match: quoted ' " string " '
isQuotesRE = regexp.MustCompile(`^\s*(['"])\s*(.*?)\s*(['"])\s*$`)
)
// `removeQuotes()` returns a quoted string w/o the quote characters.
//
// Parameters:
// - `aString` The quoted string to process.
func removeQuotes(aString string) (rString string) {
// remove leading/trailing UTF whitespace:
rString = strings.TrimSpace(aString)
// get a slice of RegEx matches:
matches := isQuotesRE.FindStringSubmatch(rString)
// we expect: (1) leading quote, (2) text, (3) trailing quote
if (3 < len(matches)) && (matches[1] == matches[3]) {
rString = matches[2]
}
return
} // removeQuotes()
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// `addSection()` appends a new INI section returning `true` on success or
// `false` otherwise.
//
// Parameters:
// - `aSection` The name of the INI section to add.
//
// Returns:
// - bool: `true` if the section list was successfully updated,
// `false` otherwise.
func (sl *TSectionList) addSection(aSection string) (rOK bool) {
if _, rOK = sl.sections[aSection]; rOK {
return // already there: nothing more to do
}
sl.sections[aSection] = NewSection()
if _, rOK = sl.sections[aSection]; rOK {
// add new section name to order list
sl.secOrder = append(sl.secOrder, aSection)
// just to be safe:
_, rOK = sl.sections[aSection]
}
return
} // addSection()
// `AddSectionKey()` appends a new key/value pair to `aSection`
// returning `true` on success or `false` otherwise.
//
// Parameters:
// - `aSection` The name of the INI section to use.
// - `aKey` The key of the key/value pair to add.
// - `aValue` The value of the key/value pair to add.
//
// Returns:
// - `bool`: `true` on success, of `false` if either `aKey` is empty, or
// `aSection` can't be found or added.
func (sl *TSectionList) AddSectionKey(aSection, aKey, aValue string) (rOK bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if rOK = sl.addSection(aSection); !rOK {
return // can't find nor add the section
}
if kl, exists := sl.sections[aSection]; exists {
rOK = kl.AddKey(aKey, aValue)
}
return
} // AddSectionKey()
/*
* Public methods to return INI values from a section as a certain data type.
*/
// `AsBool()` returns the value of `aKey` in `aSection` as a boolean value.
//
// If the given aKey in `aSection` doesn't exist then the second (bool)
// return value will be `false`.
//
// `0`, `f`, `F`, `n`, and `N` are considered `false` while
// `1`, `t`, `T`, `y`, `Y`, `j`, `J`, `o`, `O` are considered `true`;
// these values will be given in the first result value.
// All other values will give `false` as the second result value.
//
// This method actually checks only the first character of the key's value
// so one can write e.g. "false" or "NO" (for a `false` result), or "True" or
// "yes" (for a `true` result).
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `bool`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsBool(aSection, aKey string) (bool, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return false, false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsBool(aKey)
}
return false, false
} // AsBool()
// Float
// `AsFloat32` returns the value of `aKey` in `aSection` as a 32bit
// floating point.
//
// If the given `aKey` in `aSection` doesn't exist then the second (bool)
// return value will be `false`.
//
// If the key's value is well-formed and near a valid floating point number,
// `AsFloat32` returns the nearest floating point number rounded using IEEE754
// unbiased rounding.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `float32`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsFloat32(aSection, aKey string) (float32, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return float32(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsFloat32(aKey)
}
return float32(0), false
} // AsFloat32()
// `AsFloat64` returns the value of `aKey` in `aSection` as a 64bit
// floating point.
//
// If the given `aKey` in `aSection` doesn't exist then the second (`rOK`)
// return value will be `false`.
//
// If the key's value is well-formed and near a valid floating point number,
// `AsFloat64` returns the nearest floating point number rounded using IEEE754
// unbiased rounding.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `float64`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsFloat64(aSection, aKey string) (float64, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return float64(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsFloat64(aKey)
}
return float64(0), false
} // AsFloat64()
// Int
// `AsInt()` returns the value of `aKey` in `aSection` as an integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `int`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsInt(aSection, aKey string) (int, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return int(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsInt(aKey)
}
return int(0), false
} // AsInt()
// `AsInt8()` returns the value of `aKey` in `aSection` as a 8bit integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `int8`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsInt8(aSection, aKey string) (int8, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return int8(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsInt8(aKey)
}
return int8(0), false
} // AsInt8()
// `AsInt16()` return the value of `aKey` in `aSection` as a 16bit integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `int16`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsInt16(aSection, aKey string) (int16, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return int16(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsInt16(aKey)
}
return int16(0), false
} // AsInt16()
// `AsInt32()` return the value of `aKey` in `aSection` as a 32bit integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `int32`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsInt32(aSection, aKey string) (int32, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return int32(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsInt32(aKey)
}
return int32(0), false
} // AsInt32()
// `AsInt64()` return the value of `aKey` in `aSection` as a 64bit integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second return
// value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `int64`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsInt64(aSection, aKey string) (int64, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return int64(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsInt64(aKey)
}
return int64(0), false
} // AsInt64()
//
// `AsString()` returns the value of `aKey` in `aSection` as a string.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `string`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsString(aSection, aKey string) (string, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return "", false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsString(aKey)
}
return "", false
} // AsString()
// Uint
// `AsUInt()` returns the value of `aKey` in `aSection` as an
// unsigned integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `uint`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsUInt(aSection, aKey string) (uint, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return uint(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsUInt(aKey)
}
return uint(0), false
} // AsUInt()
// `AsUInt8()` returns the value of `aKey` in `aSection` as an
// unsigned 8bit integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `uint8`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsUInt8(aSection, aKey string) (uint8, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return uint8(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsUInt8(aKey)
}
return uint8(0), false
} // AsUInt8()
// `AsUInt16()` return the value of `aKey` in `aSection` as an
// unsigned 16bit integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `uint16`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsUInt16(aSection, aKey string) (uint16, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return uint16(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsUInt16(aKey)
}
return uint16(0), false
} // AsUInt16()
// `AsUInt32()` return the value of `aKey` in `aSection` as an
// unsigned 32bit integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second
// return value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `uint32`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsUInt32(aSection, aKey string) (uint32, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return uint32(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsUInt32(aKey)
}
return uint32(0), false
} // AsUInt32()
// `AsUInt64()` return the value of `aKey` in `aSection` as an unsigned
// 64bit integer.
//
// If the given `aKey` in `aSection` doesn't exist then the second return
// value will be `false`.
//
// Parameters:
// - `aSection` the name of the INI section to lookup.
// - `aKey` The name of the key to lookup.
//
// Returns:
// - `uint64`: The value associated with `aKey`.
// - `bool`: `true` if `aKey` was found, or false otherwise.
func (sl *TSectionList) AsUInt64(aSection, aKey string) (uint64, bool) {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return uint64(0), false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.AsUInt64(aKey)
}
return uint64(0), false
} // AsUInt64()
//
// `Clear()` empties the internal data structures.
//
// This method can be called once the program has used the config values
// stored in the INI file to setup the application. Emptying these data
// structures should help the garbage collector to release the data not
// needed anymore.
//
// Returns:
// - `*TSectionList`: The return value is the cleared list.
func (sl *TSectionList) Clear() *TSectionList {
// we leave `defSect` alone for now
sl.secOrder = make(tSectionOrder, 0, slDefCapacity)
for name := range sl.sections {
if kl, exists := sl.sections[name]; exists {
kl.Clear()
}
delete(sl.sections, name)
}
sl.sections = make(tSections)
return sl
} // Clear()
// `CompareTo()` compares the current `TSectionList` with another
// `TSectionList`.
// It checks whether both lists have the same number of sections and
// whether each section in the current list has the same keys and values
// as in the other list.
//
// Parameters:
// - `aINI`: The `TSectionList` to compare with the current one.
//
// Returns:
// - `bool`: `true` if both lists are equal, `false` otherwise.
func (sl *TSectionList) CompareTo(aINI *TSectionList) bool {
// Check if both lists have the same number of sections
if len(sl.sections) != len(aINI.sections) {
return false
}
// Iterate over each section in the current list
for name, kl := range sl.sections {
// Check if the other list has the same section
section, exists := aINI.sections[name]
if !exists {
return false
}
// Compare the keys and values of the sections
if !kl.CompareTo(section) {
return false
}
}
// If all checks passed, the lists are equal
return true
} // CompareTo()
// `Filename()` returns the configured filename of the INI file.
func (sl *TSectionList) Filename() string {
return sl.fName
} // Filename()
// `GetSection()` returns the INI section named `aSection`, or an empty list
// if not found.
//
// Parameters:
// - `aSection` The name of the INI section to lookup.
//
// Returns:
// - `*TSection`: The requested section or an empty if not found.
func (sl *TSectionList) GetSection(aSection string) *TSection {
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if result, ok := sl.sections[aSection]; ok {
return result
}
return &TSection{}
} // GetSection()
// `HasSection()` checks whether the INI data contain `aSection`.
//
// Parameters:
// - `aSection` is the name of the INI section to lookup.
//
// Returns:
// - `bool`: `true` if `aSection` is found, or `false` otherwise.
func (sl *TSectionList) HasSection(aSection string) (rOK bool) {
if aSection = strings.TrimSpace(aSection); "" == aSection {
_, rOK = sl.sections[sl.defSect]
} else {
_, rOK = sl.sections[aSection]
}
return
} // HasSection()
// `HasSectionKey()` checks whether the INI data contain `aSection` with
// `aKey` returning whether it exists at all.
//
// Parameters:
// - `aSection` The INI section to lookup.
// - `aKey` The key name to lookup in `aSection`.
//
// Returns:
// - `bool`: `true` if `aKey` exists, or `false` otherwise.
func (sl *TSectionList) HasSectionKey(aSection, aKey string) bool {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return false
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, ok := sl.sections[aSection]; ok {
return kl.HasKey(aKey)
}
return false
} // HasSectionKey()
// `Len()` returns the number of INI sections.
//
// It is used to determine the size of the list of sections.
//
// Returns:
// - `int`: The number of sections in the INI file.
func (sl *TSectionList) Len() int {
return len(sl.sections)
} // Len()
// `load()` reads the configured filename returning the data structure
// read from the INI file and a possible error condition.
//
// This method reads one line at a time of the INI file skipping both
// empty lines and comments (identified by '#' or ';' at line start).
//
// Returns:
// - `*TSectionList`: The loaded INI list.
// - `error`: A possible error condition.
func (sl *TSectionList) load() (*TSectionList, error) {
file, rErr := os.Open(sl.fName)
if nil != rErr {
return sl, rErr
}
defer file.Close()
scanner := bufio.NewScanner(file)
_, err := sl.read(scanner)
return sl, err
} // load()
// `mergeWalker()` inserts the given key/value pair in `aSection`.
//
// This method is called by the `Merge()` method.
//
// Parameters:
// - `aSection`: The name of the INI section to lookup.
// - `aKey` The name of the key/value pair to use.
// - `aValue`: The value of the key/value pair to update.
func (sl *TSectionList) mergeWalker(aSection, aKey, aValue string) {
sl.AddSectionKey(aSection, aKey, aValue) // ignore the return value
} // mergeWalker()
// `Merge()` copies or merges all INI sections with all key/value pairs
// into this list.
//
// Parameters:
// - `aINI` The INI sections to merge with this list.
//
// Returns:
// - `TSectionList` This sections list merged with the other one.
func (sl *TSectionList) Merge(aINI *TSectionList) *TSectionList {
if nil != aINI {
aINI.Walk(sl.mergeWalker)
}
return sl
} // Merge()
// `read()` reads/parses the INI file data returning the number of bytes
// read and a possible error.
//
// This method reads one line of the INI file at a time skipping both
// empty lines and comments (identified by '#' or ';' at line start).
//
// The method updates the current section name and adds new key/value
// pairs to the list of sections.
//
// This method is called by the `load()` method.
//
// Parameters:
// - `aScanner`: A bufio.Scanner instance that reads from the INI file.
//
// Returns:
// - `int`: The number of bytes read from the INI file.
// - `error`: A possible error condition.
func (sl *TSectionList) read(aScanner *bufio.Scanner) (rRead int, rErr error) {
var lastLine string
section := sl.defSect
for lineRead := aScanner.Scan(); lineRead; lineRead = aScanner.Scan() {
line := aScanner.Text()
rRead += len(line) + 1 // add trailing LF
line = strings.TrimSpace(line)
lineLen := len(line)
if 0 == lineLen {
if "" == lastLine {
continue // Skip blank lines
}
line, lastLine = lastLine, ""
}
if ';' == line[0] || '#' == line[0] { // comment indicators
if "" == lastLine {
continue // Skip comment lines
}
line, lastLine = lastLine, ""
}
if '\\' == line[lineLen-1] { // possible value concatenation
if (1 < lineLen) && (' ' == line[lineLen-2]) {
lastLine += line[:lineLen-1]
} else {
lastLine += line[:lineLen-1] + " "
}
line = ``
continue // concatenation handled
}
if 0 < len(lastLine) {
line, lastLine = lastLine+line, ""
}
if matches := isSectionRE.FindStringSubmatch(line); nil != matches {
// update the current section name
section = strings.TrimSpace(matches[1])
} else if matches := isKeyValRE.FindStringSubmatch(line); nil != matches {
// get a slice of RegEx matches,
// we expect (1) key, (2) value
key := strings.TrimSpace(matches[1])
val := removeQuotes(matches[2])
sl.AddSectionKey(section, key, val) // ignore return value
} else {
line = "" // ignore broken lines
}
}
rErr = aScanner.Err()
return
} // read()
// `RemoveSection()` deletes `aSection` from the list of sections.
//
// Parameters:
// - `aSection` The name of the INI section to remove.
//
// Returns:
// - `bool`: `true` on success, `false` on failure.
func (sl *TSectionList) RemoveSection(aSection string) bool {
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if _, exists := sl.sections[aSection]; !exists {
// section doesn't exist which satisfies the removal request
return true
}
delete(sl.sections, aSection)
if _, exists := sl.sections[aSection]; exists {
return false // this should never happen!
}
// len - 1: because list is zero-based
oLen := len(sl.secOrder) - 1
if 0 > oLen {
// empty list
return true
}
// remove secOrder entry:
for idx, name := range sl.secOrder {
if name != aSection {
continue
}
switch idx {
case 0:
if 0 == oLen {
// the only list entry: replace by an empty list
sl.secOrder = make(tSectionOrder, 0, slDefCapacity)
} else {
// first list entry: move the remaining data
sl.secOrder = sl.secOrder[1:]
}
case oLen:
// last list entry
sl.secOrder = sl.secOrder[:idx]
default:
sl.secOrder = append(sl.secOrder[:idx], sl.secOrder[idx+1:]...)
}
return true
}
return false
} // RemoveSection()
// `RemoveSectionKey()` removes aKey from aSection.
//
// This method returns 'true' if either `aSection` or `aKey` doesn't exist
// or if `aKey` in `aSection` was successfully removed, or `false` otherwise.
//
// Parameters:
// - `aSection` is the name of the INI section to use.
// - `aKey` The name of the key/value pair to remove.
//
// Returns:
// - `bool`: `true` on success, `false` on failure.
func (sl *TSectionList) RemoveSectionKey(aSection, aKey string) bool {
if aKey = strings.TrimSpace(aKey); "" == aKey {
return true
}
if aSection = strings.TrimSpace(aSection); "" == aSection {
aSection = sl.defSect
}
if kl, exists := sl.sections[aSection]; exists {
return kl.RemoveKey(aKey)
}
// section or key doesn't exist: assume successful removal
return true
} // RemoveSectionKey()
// `Sections()` returns a list of section names in the order they
// appear in the INI file.
//
// The returned list is a slice of strings. The length of the slice
// is the number of sections in the INI file.
//
// Returns:
// - `[]string`: A list of section names
// - `int`: The number of sections in the returned list.
func (sl *TSectionList) Sections() ([]string, int) {
dest := make([]string, len(sl.secOrder))
len := copy(dest, sl.secOrder)
return dest, len
} // Sections()
// `SetFilename()` sets the filename of the INI file to use.
//
// Parameters:
// - `aFilename` The name to use for the INI file.
func (sl *TSectionList) SetFilename(aFilename string) *TSectionList {
sl.fName = strings.TrimSpace(aFilename)
return sl
} // SetFilename()
// `Sort()` sorts the sections in the order they appear in the INI file.
//
// This method sorts the key/value pairs in each section.
//
// Returns:
// - `*TSectionList`: The sorted instance of the `TSectionList`.
func (sl *TSectionList) Sort() *TSectionList {
// use the secOrder list to determine the order of sections
for _, name := range sl.secOrder {
if kl, exists := sl.sections[name]; exists {
sl.sections[name] = kl.Sort()
}
}
return sl
} // Sort()
// `Store()` writes all INI data to the configured filename.
//
// Returns:
// - `int`: The number of bytes written.
// - `error`: An possible error during writing the data to file.
func (sl *TSectionList) Store() (int, error) {
file, err := os.Create(sl.fName)
if err != nil {
return 0, err
}
defer file.Close()
return file.Write([]byte(sl.String()))
} // Store()
// `String()` returns a string representation of the INI section list.
//
// Returns:
// - `string`: The string representation of the INI section list.
func (sl *TSectionList) String() (rString string) {
// use the secOrder list to determine the order of sections
for _, name := range sl.secOrder {
if kl, exists := sl.sections[name]; exists {
// ensure that all sections are sorted internally
sl.sections[name] = kl.Sort()
rString += "\n[" + name + "]\n" + kl.String()
}
}
return
} // String()
// `updateSectKey()` updates the current value of `aKey` in `aSection`