I've been meaning to posta again in this topic anyway. I have changed the function names so they can be put in my user library as INI.ahk
/*
INI_Init(inifile) ;prepares the global variables to be populated
INI_Load(inifile) ;Reads all the settings into the global variables from the file
INI_Save(inifile) ;Saves all the settings from the global variables into the file
INI_ReadAll(inifile) ;Synonym for INI_Load
INI_WriteAll(inifile) ;Synonym for INI_Save
*/
INI_Init(inifile = "inifile.ini"){
global
local key
inisections:=0
loop,read,%inifile%
{
if regexmatch(A_Loopreadline,"\[(\w+)]")
{
inisections+= 1
section%inisections%:=regexreplace(A_loopreadline,"(\[)(\w+)(])","$2")
section%inisections%_keys:=0
}
else if regexmatch(A_LoopReadLine,"(\w+)=(\w+)")
{
section%inisections%_keys+= 1
key:=section%inisections%_keys
section%inisections%_key%key%:=regexreplace(A_LoopReadLine,"(\w+)=(.*)","$1")
}
}
}
INI_readAll(inifile="inifile.ini"){
INI_load(inifile)
}
INI_load(inifile="inifile.ini"){
global
local sec,var
loop,%inisections%
{
sec:=A_index
loop,% section%a_index%_keys
{
var:=section%sec% "_" section%sec%_key%A_index%
iniread,%var%,%inifile%,% section%sec%,% section%sec%_key%A_index%
}
}
}
INI_writeAll(inifile="inifile.ini"){
INI_Save(inifile)
}
INI_Save(inifile="inifile.ini"){
global
local sec,var
loop,%inisections%
{
sec:=A_index
loop,% section%a_index%_keys
{
var:=section%sec% "_" section%sec%_key%A_index%,var:=%var%
iniwrite,%var%,%inifile%,% section%sec%,% section%sec%_key%A_index%
}
}
}
I also added two functions which are just aliases.
To use these functions, consider this example ini file.
inifile.ini
[Options]
font=Veranda
size=12
[users]
bsmith=Bob Smith
jdoe=Jane Doe
and now run this AHK file - the ini file will be created for you
inifile = inifile.ini
;first we make the example ini file for you - your script will not normally include this.
FileDelete, %inifile%
FileAppend,
(
[Options]
font=Veranda
size=12
[Users]
bsmith=Bob Smith
jdoe=Jane Doe
), %inifile%
;here is where we use our new user library functions.
INI_Init(inifile)
Listvars
Msgbox, First, we will look at the results of INI_Init.`n Close Listvars and Click OK when you have looked at the listvars window
INI_Load(inifile)
Listvars
Msgbox, Now we look at the results of reading the ini file.`nNotice how the variables are made as Section_variable.`n Close Listvars and Click OK when you see this.
InputBox, myname, , Now we will add a new variable to the Users section - Enter your Name
;since we are making a new value in the ini file, we use INIWrite
IniWrite, %myname%, %inifile%, Users, %A_UserName%
;since we have changed the file, we should Init and read again.
INI_Init(inifile)
INI_Load(inifile)
Listvars
Msgbox, Now we have your username as an option.
Msgbox, Now we will loop through all users to say hello to you
;first we need to find out what section number is Users. We /could/ hardcode this, but that's no fun
;then we find out how many keys are in that section
Loop, %inisections%
If (Section%A_index% = "Users")
{
UsersSection := A_index
numberOfUsers := Section%A_index%_keys
}
;now we can loop through each user
Loop, %numberOfUsers%
{
This_username := Section%UsersSection%_key%A_index%
This_realName := Users_%This_UserName%
If (This_username = A_UserName)
Msgbox, Hello, %This_Realname%
else
Msgbox, You are not %This_RealName% (%This_UserName%)
}
;if you only need to do something with your user, then you can do the much simpler
YourRealName := Users_%A_UserName%
Msgbox, Hello, %YourRealName%
;Now we will write a value to the ini file
Inputbox, Options_Font, , Type in the name of a font that you want to use `n(Any text is fine for this example)
Msgbox, the result (%Options_Font%) is now saved in Options_Font
INI_Save(inifile)
Msgbox, the ini file has been updated with your new text automatically.`n now imagine writing an INIWrite command for 50 or 60 different options.`nINI_Save takes care of them all for you.
Run, edit, %inifile%
If this is not helpful, I can share an application I made for work (minus a bit of stuff), so you can see how simple it is for basic ini usage.