 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Superfraggle
Joined: 02 Nov 2004 Posts: 1019 Location: London, UK
|
Posted: Fri Jul 20, 2007 10:30 pm Post subject: Auto read,load and save an INI file Updated.... |
|
|
Engunneer said something like this had been done before but I thought I would make my own attempt for practice.
We have three functions
| Code: | initializeini()
loadini()
saveini() |
initializeini will read the specified INI file and load all section and key names.
loadini will then load all the associated keys and assign them to variables of section_keyname.
Saveini will save all variables to the correct section & key.
You can either specify the name of the inifile in the function call, or specify it within the function (for lazy programmers)
This could be used as part of a template to help you along the way of creating more customizable apps.
Heres the code for the functions.
| Code: | initializeini(inifile = "ast.ini"){
global
local key,temp
inisections:=0
loop,read,%inifile%
{
if regexmatch(A_Loopreadline,"\[(.*)?]")
{
inisections+= 1
section%inisections%:=regexreplace(A_loopreadline,"(\[)(.*)?(])","$2")
section%inisections%_keys:=0
}
else if regexmatch(A_LoopReadLine,"(\w+)=(.*)")
{
section%inisections%_keys+= 1
key:=section%inisections%_keys
section%inisections%_key%key%:=regexreplace(A_LoopReadLine,"(\w+)=(.*)","$1")
}
}
}
loadini(inifile="ast.ini"){
global
local sec,var
loop,%inisections%
{
sec:=A_index
loop,% section%a_index%_keys
{
var:=section%sec% "_" section%sec%_key%A_index%,
Stringreplace,var,var,%a_space%,,All
iniread,%var%,%inifile%,% section%sec%,% section%sec%_key%A_index%
}
}
}
saveini(inifile="ast.ini"){
global
local sec,var
loop,%inisections%
{
sec:=A_index
loop,% section%a_index%_keys
{
var:=section%sec% "_" section%sec%_key%A_index%
Stringreplace,var,var,%a_space%,,All
var:=%var%
iniwrite,%var%,%inifile%,% section%sec%,% section%sec%_key%A_index%
}
}
}
|
As always comments and suggestions welcome
Update:
Code has been slightly updated to work with spaces in keys, and section names. _________________ Steve F AKA Superfraggle
http://r.yuwie.com/superfraggle
Last edited by Superfraggle on Sun Nov 04, 2007 10:17 pm; edited 4 times in total |
|
| Back to top |
|
 |
HuBa
Joined: 24 Feb 2007 Posts: 173 Location: Budapest, Hungary
|
Posted: Sun Jul 22, 2007 12:19 am Post subject: |
|
|
| Why is the filename (ast2.ini) different in save then in read? |
|
| Back to top |
|
 |
Superfraggle
Joined: 02 Nov 2004 Posts: 1019 Location: London, UK
|
Posted: Sun Jul 22, 2007 1:37 am Post subject: |
|
|
My bad that was used for testing, just in case it ddidn't work I didn't want to mess up my ini file.
They can be changed to whatever you want anyway, or specify the filename in the function call. _________________ Steve F AKA Superfraggle
http://r.yuwie.com/superfraggle |
|
| Back to top |
|
 |
Superfraggle
Joined: 02 Nov 2004 Posts: 1019 Location: London, UK
|
Posted: Mon Aug 27, 2007 9:48 pm Post subject: |
|
|
I've updated the code slightly as there was a bug that wasn't detected in my version of AHK. _________________ Steve F AKA Superfraggle
http://r.yuwie.com/superfraggle |
|
| Back to top |
|
 |
WankaUSR
Joined: 14 Aug 2007 Posts: 86
|
Posted: Fri Sep 07, 2007 2:55 pm Post subject: |
|
|
First of all i'm o newbie so don't laugh ...
How do i call this functions... for example if i want to make the call at the beginning of the script and save at exitapp or button press? |
|
| Back to top |
|
 |
Superfraggle
Joined: 02 Nov 2004 Posts: 1019 Location: London, UK
|
Posted: Fri Sep 07, 2007 5:09 pm Post subject: |
|
|
We don't laugh at new people.
To call the function on exit best to use the following.
| Code: | onexit, close ; Put this at the very start of the script.
; put this after the autoexecute section of the script.
Close:
saveini()
exitapp
return |
if you have a gui with a save button, then it would be something like
| Code: | buttonsave:
saveini()
return |
Be sure to change the name of the inifile you are actually using in the original function. _________________ Steve F AKA Superfraggle
http://r.yuwie.com/superfraggle |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Sep 07, 2007 6:04 pm Post subject: |
|
|
| thanks this helped me a lot by reducing the script size for reading and writing inis |
|
| Back to top |
|
 |
WankaUSR
Joined: 14 Aug 2007 Posts: 86
|
Posted: Fri Sep 07, 2007 6:06 pm Post subject: |
|
|
This is what i've really needed thanks an keep up the good work
So after a little study of the code i still can't find the variable where the read sections are stored i thought it was %var% but i can not seem to use it...
My ideea was to use it in a textedit for example a path would be shown there and stored in ini.ini at settings/path but i don't understand enough the code you wrote to do this |
|
| Back to top |
|
 |
djeaton3162
Joined: 16 Sep 2006 Posts: 27
|
Posted: Fri Sep 21, 2007 5:05 pm Post subject: |
|
|
What you guys are doing is way over my head...but I need it. I am setting some environment variables in a script, but need to read them from a ini file that others can edit and add to. The ini file would contain "set variable value" statements and I need to load them and set them in the program. Could one of you super-smart and really kind people dumb this down a bit for a newbie and tell me how I could use the above code to do something like this? I'm just not grasping how this code works or how to call the function when I don't know how many variables will be set in the INI file.
Daniel |
|
| Back to top |
|
 |
haichen
Joined: 05 Feb 2007 Posts: 182 Location: Osnabrück, Germany
|
Posted: Fri Sep 21, 2007 5:35 pm Post subject: |
|
|
| have a look at VisualINI from Titan |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 7703 Location: Madison, WI
|
Posted: Fri Sep 21, 2007 5:38 pm Post subject: |
|
|
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
| Code: |
/*
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
| Code: |
[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
| Code: |
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. _________________
Unless noted, all code is UNTESTED.
Answers Here: 1.(Loops, Viruses, etc.) 2.Search 3.RTFM 4.Ask for Help.
PMs will be ignored unless you are hiring me. |
|
| Back to top |
|
 |
djeaton3162
Joined: 16 Sep 2006 Posts: 27
|
Posted: Fri Sep 21, 2007 6:32 pm Post subject: |
|
|
Here is where I am confused. If your script of functions isn't inside my own script or "included", how does it find it? I love what you are doing with the sections as well. I have other things I want to add to the ini file later.
D. |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 7703 Location: Madison, WI
|
Posted: Fri Sep 21, 2007 6:37 pm Post subject: |
|
|
please read about the user library and the Standard Library. It let you "extend" the AHK language by Auto-including needed functions from %A_MyDocuments%\Autohotkey\Lib and from the Autohotkey library in Program Files. _________________
Unless noted, all code is UNTESTED.
Answers Here: 1.(Loops, Viruses, etc.) 2.Search 3.RTFM 4.Ask for Help.
PMs will be ignored unless you are hiring me. |
|
| Back to top |
|
 |
djeaton3162
Joined: 16 Sep 2006 Posts: 27
|
Posted: Fri Sep 21, 2007 6:59 pm Post subject: |
|
|
| engunneer wrote: | | please read about the user library and the Standard Library. It let you "extend" the AHK language by Auto-including needed functions from %A_MyDocuments%\Autohotkey\Lib and from the Autohotkey library in Program Files. | Thanks. The help file is good, but only if you know what to search on. This helps me a lot.
D. |
|
| Back to top |
|
 |
tic
Joined: 22 Apr 2007 Posts: 1520
|
Posted: Thu Sep 27, 2007 3:13 pm Post subject: |
|
|
just a thought as ive seen this happen before in inis:
| Code: | Loop, 2
{
If A_Index = 1
String := "[hello]"
Else
String := " [hello]"
Regexmatch(String,"\[(\w+)]")
yours := regexreplace(String,"(\[)(\w+)(])","$2")
RegExMatch(String,"\[.*\]", MyMatch)
StringMid, mine, MyMatch, 2, % StrLen(MyMatch)-2
MsgBox, % "Your Match: " . yours . "`nMy Match: " . mine
} |
|
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|