Skip to content

Instantly share code, notes, and snippets.

@anonymous1184
Last active May 22, 2024 15:54
Show Gist options
  • Save anonymous1184/737749e83ade98c84cf619aabf66b063 to your computer and use it in GitHub Desktop.
Save anonymous1184/737749e83ade98c84cf619aabf66b063 to your computer and use it in GitHub Desktop.
Automatic configuration file handling.

; Version: 2023.04.20.1
; Usages and examples: https://redd.it/s1it4j
Ini(Path, Sync := true) {
return new Ini_File(Path, Sync)
}
class Ini_File {
;#region Public
Persist() {
local
IniRead buffer, % this.__path
sections := {}
for _, name in StrSplit(buffer, "`n")
sections[name] := true
for name in this.__data {
this[name].Persist()
sections.Delete(name)
}
for name in sections
IniDelete % this.__path, % name
}
Sync(Set := "") {
local
if (Set = "")
return this.__sync
Set := !!Set
for name in this
this[name].Sync(Set)
return this.__sync := Set
}
;#endregion
;#region Overload
Delete(Name) {
if (this.__sync)
IniDelete % this.__path, % Name
}
;#endregion
;#region Meta
__New(Path, Sync) {
local
ObjRawSet(this, "__path", Path)
ObjRawSet(this, "__sync", false)
IniRead buffer, % Path
for _, name in StrSplit(buffer, "`n") {
IniRead conts, % Path, % name
this[name] := new this.Ini_Section(Path, name, conts)
}
this.Sync(Sync)
}
__Set(Key, Value) {
local
isObj := IsObject(Value)
base := isObj ? ObjGetBase(Value) : false
if (isObj && !base)
|| (base && base.__Class != "Ini_File.Ini_Section") {
path := this.__path
sync := this.__sync
this[Key] := new Ini_File.Ini_Section(path, Key, Value, sync)
return obj ; Stop, hammer time!
}
}
;#endregion
#Include %A_LineFile%\..\Ini_Object.ahk
#Include %A_LineFile%\..\Ini_Section.ahk
}
;#region Auxiliary
Ini_ToObject(ByRef Data) {
local
info := Data, Data := {}
for _, pair in StrSplit(info, "`n") {
pair := StrSplit(pair, "=",, 2)
Data[pair[1]] := pair[2]
}
}
;#endregion

; Version: 2023.04.20.1
class Ini_Object {
;#region Public
Clone() {
local
name := ObjGetBase(this).__Class
clone := new %name%()
clone.__data := this.__data.Clone()
return clone
}
Count() {
return this.__data.Count()
}
Delete(Parameters*) {
return this.__data.Delete(Parameters*)
}
GetAddress(Key) {
return this.__data.GetAddress(Key)
}
GetCapacity(Parameters*) {
return this.__data.GetCapacity(Parameters*)
}
HasKey(Key) {
return this.__data.HasKey(Key)
}
Insert(_*) {
throw Exception("Deprecated.", -1, A_ThisFunc)
}
InsertAt(Parameters*) {
this.__data.InsertAt(Parameters*)
}
Length() {
return this.__data.Length()
}
MaxIndex() {
return this.__data.MaxIndex()
}
MinIndex() {
return this.__data.MinIndex()
}
Pop() {
return this.__data.Pop()
}
Push(Parameters*) {
return this.__data.Push(Parameters*)
}
Remove(_*) {
throw Exception("Deprecated.", -1, A_ThisFunc)
}
RemoveAt(Parameters*) {
return this.__data.RemoveAt(Parameters*)
}
SetCapacity(Parameters*) {
return this.__data.SetCapacity(Parameters*)
}
;#endregion
;#region Private
_NewEnum() {
return this.__data._NewEnum()
}
;#endregion
;#region Meta
__Get(Parameters*) { ; Key[, Key...]
return this.__data[Parameters*]
}
__Init() {
ObjRawSet(this, "__data", {})
}
__Set(Parameters*) { ; Key, Value[, Value...]
local
value := Parameters.Pop()
this.__data[Parameters*] := value
return value
}
;#endregion
}

; Version: 2023.04.20.1
class Ini_Section extends Ini_File.Ini_Object {
;#region Public
Persist() {
local
IniRead buffer, % this.__path, % this.__name
keys := {}
for _, key in StrSplit(buffer, "`n") {
key := StrSplit(key, "=")[1]
keys[key] := true
}
for key, value in this {
keys.Delete(key)
value := value = "" ? "" : " " value
IniWrite % value, % this.__path, % this.__name, % key
}
for key in keys
IniDelete % this.__path, % this.__name, % key
}
Sync(Set := "") {
if (Set = "")
return this.__sync
return this.__sync := !!Set
}
;#endregion
;#region Overload
Delete(Key) {
if (this.__sync)
IniDelete % this.__path, % this.__name, % Key
}
;#endregion
;#region Meta
__New(Path, Name, Data, Sync := false) {
local
ObjRawSet(this, "__path", Path)
ObjRawSet(this, "__name", Name)
ObjRawSet(this, "__sync", Sync)
if (!IsObject(Data))
Ini_ToObject(Data)
for key, value in Data
this[key] := value
}
__Set(Key, Value) {
if (this.__sync && Value != this.__data[Key]) {
Value := Value = "" ? "" : " " Value
IniWrite % Value, % this.__path, % this.__name, % Key
}
}
;#endregion
}
MIT License
Copyright (c) 2020 anonymous1184
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@Gewerd-Strauss
Copy link

Alright, thank you :)

@Gewerd-Strauss
Copy link

@anonymous1184 small bump, please update

(Sorry to keep pestering here, I'm just waiting for this to be updated before I can hand this script over.)

@anonymous1184
Copy link
Author

Done!

@Gewerd-Strauss
Copy link

Thank you kindly :)

@gaabora
Copy link

gaabora commented Feb 21, 2024

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment