PageRenderTime 37ms CodeModel.GetById 34ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ini.ahk

http://github.com/Skiouros/Macro
AutoHotKey | 90 lines | 77 code | 7 blank | 6 comment | 10 complexity | 270745e1ec8d2a6df9a27b40a3348377 MD5 | raw file
 1class Ini
 2{
 3        ; Loads ini file.
 4    __New(File, Default = "") {
 5        If (FileExist(File)) and (RegExMatch(File, "\.ini$"))
 6            FileRead, Info, % File
 7        Else
 8            Info := File
 9        Loop, Parse, Info, `n, `r
10        {
11            RegExMatch(A_LoopField, "^\[(.+?)\]$", Section) ; Get the section name.
12            RegExMatch(A_LoopField, "(.+?)=(.*)", Key) ; Get the key and its value.
13            If (Section1)
14                Saved_Section := Trim(Section1), this[Saved_Section] := { }
15            Key2 := (Key2) ? Key2 : Default
16            If (Key1) and (Saved_Section)
17                this[Saved_Section].Insert(Key1, Key2) ; Set the section name withs its keys and values.
18        }
19    }
20
21    __Get(Section) {
22        If (!this.HasKey(Section)) and (Section)
23            this[Section] := { }
24    }
25
26    ; Renames an entire section or just an individual key.
27    Rename(Section, NewName, KeyName = "") { ; If KeyName is omited, rename the seciton, else rename key.
28        Sections := this.Sections(",")
29        If Section not in %Sections%
30            Return 1
31        else if ((this.HasKey(NewName)) and (!KeyName)) ; If the new section already exists.
32            Return 1
33        else if ((this[Section].HasKey(NewName)) and (KeyName)) ; If the section already contains the new key name.
34            Return 1
35        else if (!this[Section].HasKey(KeyName) and (KeyName)) ; If the section doesn't have the key to rename.
36            Return 1
37        else If (!KeyName)
38        {
39            this[NewName] := { }
40            for key, value in this[Section]
41                this[NewName].Insert(Key, Value)
42            this.Remove(Section)
43        }
44        Else
45        {
46            KeyValue := this[Section][KeyName]
47            this[Section].Insert(NewName, KeyValue)
48            this[Section].Remove(KeyName)
49        }
50        Return 0
51    }
52
53    ; Delete a whole section or just a specific key within a section.
54    Delete(Section, Key = "") { ; Omit "Key" to delete the whole section.
55        If (Key)
56            this[Section].Remove(Key)
57        Else
58            this.Remove(Section)
59    }
60
61    ; Returns a list of sections in the ini.
62    Sections(Delimiter = "`n") {
63        for Section, in this
64            List .= (this.Keys(Section)) ? Section . Delimiter : ""
65        Return SubStr(List, 1, -1)
66    }
67
68    ; Get all of the keys in the entire ini or just one section.
69    Keys(Section = "") { ; Leave blank to retrieve all keys or specify a seciton to retrieve all of its keys.
70        Sections := Section ? Section : this.Sections()
71        Loop, Parse, Sections, `n
72            for key, in this[A_LoopField]
73                keys .= key . "`n"
74        Return SubStr(keys, 1, -1)
75    }
76
77    ; Saves everything to a file.
78    Save(File) {
79        Sections := this.Sections()
80        loop, Parse, Sections, `n
81        {
82            NewIni .= (A_LoopField) ? "[" . A_LoopField . "]`n" : ""
83            For key, value in this[A_LoopField]
84                NewIni .= key . "=" . value . "`n"
85        }
86        FileDelete, % File
87        FileAppend, % SubStr(NewIni, 1, -1), % File
88    }
89
90}