Here is my last version, that has some extra interesting features:
- Added ReadObject and WriteObject method to read and write objects (section or ini) (in case you want to read/Write all the variables at once)
- Accepts now maps and arrays as variables
- Allows `n and `r to be in variables
For the maps, arrays and `n and `r, this class shoud be used to read and write it.
Feel free to give some advice, improvements or corrections to make it cleaner. I love to learn.
It looks and works already great, and I was missing some functionalities.
(I know Regex would be shorter to write, but choose to use SubStr as it is normally faster.)
Code: Select all
#Requires AutoHotkey v2.0-beta.3
#SingleInstance force
MyIni := Ini()
;Example writing a map
mTest := Map("first",55,"Second",66)
MyIni.Map.Test := mTest
;Example Writing an double object to an ini file
oTest := Object()
oTest.Pos := Object()
oTest.Pos.x := atest
oTest.Pos.y := 2
path := 'test.ini'
oResult := MyIni.WriteObject(oTest)
; Writing an object as a section
MyIni.Pos2.WriteObject(oTest.pos)
;Reading an object as a section
oPos := MyIni.Pos.ReadObject()
;MsgBox(oPos.x)
; Example reading and writing in the file
MyIni.section.key := 'value'
MsgBox(MyIni.section.key)
MsgBox(MyIni.section.key2['default value'])
MyIni.section.key2 := 'value2'
MsgBox(MyIni.section2.key2['default value'])
class Ini {
__New(FileName:="") => this.__path__ := (FileName="") ? RegExReplace(A_ScriptName, "(.*\.)(.*)", "$1ini") : FileName
__Get(sectionName, *) => Ini.Section(this.__path__, sectionName)
class Section {
__New(FileName, sectionName) {
this.DefineProp('__path__', { get: (*) => FileName })
this.DefineProp('__name__', { get: (*) => sectionName })
}
__Get(keyName, default) => this.StrToAhk(IniRead(this.__path__, this.__name__, keyName, default.Has(1) ? default[1] : ''))
__Set(keyName,_, value) => IniWrite(this.AhkToStr(value), this.__path__, this.__name__, keyName)
; Read the section as an object
ReadObject(){
oResult := Object()
For VariableLine in StrSplit(IniRead(this.__path__,this.__name__), "`n", "`r"){
FoundPos := InStr(VariableLine, "=")
KeyName := SubStr(VariableLine,1,FoundPos-1)
oResult.%KeyName% := this.StrToAhk(SubStr(VariableLine,FoundPos+1))
}
return oResult
}
; Write an object as a Section
WriteObject(oSection){
if !IsObject(oSection){
throw ValueError("Object was required", -1, oSection)
}
For keyName, value in oSection.OwnProps(){
IniWrite(this.AhkToStr(value), this.__path__, this.__name__, keyName)
}
}
StrToAhk(var){
var := StrReplace(StrReplace(var, "``r","`r"), "``n","`n")
if (SubStr(var,1,6)="Array:"){
return StrSplit(SubStr(var,7),"¢")
} else if (SubStr(var,1,4)="Map:"){
aTemp := StrSplit(SubStr(var,5),"¢")
mVar := Map()
for Value in aTemp{
aValue := StrSplit(Value,"¤")
mVar[aValue[1]] := aValue[2]
}
Return mVar
}
Return var
}
AhkToStr(var){
result := ""
if (Type(Var)="Array"){
for Value in Var{
result .= (result="") ? "Array:" Value : "¢" Value
}
} else if (Type(Var)="Map"){
for key, Value in Var{
result .= (result="") ? "Map:" key "¤" Value : "¢" key "¤" Value
}
} else{
result := var
}
result := StrReplace(StrReplace(result, "`r","``r"), "`n","``n")
Return result
}
}
; Write a double Object in the ini file
WriteObject(oIni){
if !IsObject(oIni){
throw ValueError("Object was required", -1, oIni)
}
For SectionName, oSection in oIni.OwnProps()
{
if !IsObject(oSection){
throw ValueError("Object of Objects was required", -1, oIni)
}
For keyName, value in oSection.OwnProps(){
IniWrite(this.Section.AhkToStr(value), this.__path__, SectionName, keyName)
}
}
}
; Read the ini file as a double Object
ReadObject(){
oResult := Object()
For SectionName in StrSplit(IniRead(this.__path__), "`n", "`r"){
oResult.%SectionName% := Object()
For VariableLine in StrSplit(IniRead(this.__path__,SectionName), "`n", "`r"){
FoundPos := InStr(VariableLine, "=")
KeyName := SubStr(VariableLine,1,FoundPos-1)
oResult.%SectionName%.%KeyName% := this.Section.StrToAhk(SubStr(VariableLine,FoundPos+1))
}
}
return oResult
}
}