/lib/ini.ahk

http://github.com/Skiouros/Macro · AutoHotKey · 90 lines · 77 code · 7 blank · 6 comment · 10 complexity · 270745e1ec8d2a6df9a27b40a3348377 MD5 · raw file

  1. class 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. __Get(Section) {
  21. If (!this.HasKey(Section)) and (Section)
  22. this[Section] := { }
  23. }
  24. ; Renames an entire section or just an individual key.
  25. Rename(Section, NewName, KeyName = "") { ; If KeyName is omited, rename the seciton, else rename key.
  26. Sections := this.Sections(",")
  27. If Section not in %Sections%
  28. Return 1
  29. else if ((this.HasKey(NewName)) and (!KeyName)) ; If the new section already exists.
  30. Return 1
  31. else if ((this[Section].HasKey(NewName)) and (KeyName)) ; If the section already contains the new key name.
  32. Return 1
  33. else if (!this[Section].HasKey(KeyName) and (KeyName)) ; If the section doesn't have the key to rename.
  34. Return 1
  35. else If (!KeyName)
  36. {
  37. this[NewName] := { }
  38. for key, value in this[Section]
  39. this[NewName].Insert(Key, Value)
  40. this.Remove(Section)
  41. }
  42. Else
  43. {
  44. KeyValue := this[Section][KeyName]
  45. this[Section].Insert(NewName, KeyValue)
  46. this[Section].Remove(KeyName)
  47. }
  48. Return 0
  49. }
  50. ; Delete a whole section or just a specific key within a section.
  51. Delete(Section, Key = "") { ; Omit "Key" to delete the whole section.
  52. If (Key)
  53. this[Section].Remove(Key)
  54. Else
  55. this.Remove(Section)
  56. }
  57. ; Returns a list of sections in the ini.
  58. Sections(Delimiter = "`n") {
  59. for Section, in this
  60. List .= (this.Keys(Section)) ? Section . Delimiter : ""
  61. Return SubStr(List, 1, -1)
  62. }
  63. ; Get all of the keys in the entire ini or just one section.
  64. Keys(Section = "") { ; Leave blank to retrieve all keys or specify a seciton to retrieve all of its keys.
  65. Sections := Section ? Section : this.Sections()
  66. Loop, Parse, Sections, `n
  67. for key, in this[A_LoopField]
  68. keys .= key . "`n"
  69. Return SubStr(keys, 1, -1)
  70. }
  71. ; Saves everything to a file.
  72. Save(File) {
  73. Sections := this.Sections()
  74. loop, Parse, Sections, `n
  75. {
  76. NewIni .= (A_LoopField) ? "[" . A_LoopField . "]`n" : ""
  77. For key, value in this[A_LoopField]
  78. NewIni .= key . "=" . value . "`n"
  79. }
  80. FileDelete, % File
  81. FileAppend, % SubStr(NewIni, 1, -1), % File
  82. }
  83. }