AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Read .INI file in one go !

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Smurth



Joined: 13 Dec 2006
Posts: 106

PostPosted: Sat Oct 11, 2008 4:16 am    Post subject: Read .INI file in one go ! Reply with quote

If you want to create lots of variable from .ini files, this function is for you.
It reads the whole file in one go and creates all the global variables.

Example:
Code:
[Test]
X=120
   Y=300

; comments
Title= [this works]

readIni() will create the following variables:
TestX := 120
TestY := 300
TestTitle := " [this works]"


Code:
ReadIni( filename = 0 )
; Read a whole .ini file and creates variables like this:
; %Section%%Key% = %value%
{
Local s, c, p, key, k

   if not filename
      filename := SubStr( A_ScriptName, 1, -3 ) . "ini"

   FileRead, s, %filename%

   Loop, Parse, s, `n`r, %A_Space%%A_Tab%
   {
      c := SubStr(A_LoopField, 1, 1)
      if (c="[")
         key := SubStr(A_LoopField, 2, -1)
      else if (c=";")
         continue
      else {
         p := InStr(A_LoopField, "=")
         if p {
            k := SubStr(A_LoopField, 1, p-1)
            %key%%k% := SubStr(A_LoopField, p+1)
         }
      }
   }
}


Last edited by Smurth on Sun Jul 05, 2009 11:00 pm; edited 5 times in total
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7746

PostPosted: Sat Oct 11, 2008 5:31 am    Post subject: Re: Read .INI file in one go ! Reply with quote

Smurth wrote:
- The variables _READINI, _READINI1 and _READINI2 will stay global (if someone know how to fix this)


Code:
Local s, sep, key, line, _READINI, _READINI1, _READINI2


Question Exclamation

Nice script, BTW Smile
_________________
Suresh Kumar A N
Back to top
View user's profile Send private message
Smurth



Joined: 13 Dec 2006
Posts: 106

PostPosted: Sat Oct 11, 2008 8:19 am    Post subject: Re: Read .INI file in one go ! Reply with quote

...

Last edited by Smurth on Sun Jul 05, 2009 10:57 pm; edited 1 time in total
Back to top
View user's profile Send private message
n-l-i-d
Guest





PostPosted: Sat Oct 11, 2008 10:53 pm    Post subject: Reply with quote

Nice. It would be even better if you could determine the name of the resulting variable from script (instead of from the ini section header), and not necessarily load the whole ini file, but a section, some sections, or all.
Code:
ReadIni( filename, section/sections/all, resultingvariablename/empty=section header name )

Just spitting some ideas...
Wink
Back to top
Smurth



Joined: 13 Dec 2006
Posts: 106

PostPosted: Sat Oct 11, 2008 11:05 pm    Post subject: Reply with quote

n-l-i-d wrote:
Nice. It would be even better if you could determine the name of the resulting variable from script (instead of from the ini section header), and not necessarily load the whole ini file, but a section, some sections, or all.


Perhaps someone else will do...

IMHO, .ini files are read at startup; there's no need to read one section and another one later. The script has been designed to do the work in one go; and fast.
If you want to use different variable names, just modify the .ini and reload your app.

One thing I should add, is a function to save a complete ini section. But I don't have time to do it now (even if it should be easy)
Back to top
View user's profile Send private message
freakkk



Joined: 29 Jul 2005
Posts: 179

PostPosted: Sun Oct 12, 2008 3:12 am    Post subject: Reply with quote

Isn't this kinda the same idea as [module] Ini 1.0b2 - ini-like handling of strings ? Also, that module includes many other flexable options-- in case you don't necessarily need keys stored as global vars, want to store sections in an array, or need to backup a section of keys.

I'm sorry if I missed something.. but could you explain the differences?

Thanks!
_________________
.o0[ corey ]0o.
Back to top
View user's profile Send private message
Smurth



Joined: 13 Dec 2006
Posts: 106

PostPosted: Sun Oct 12, 2008 4:09 am    Post subject: Reply with quote

freakkk wrote:
Isn't this kinda the same idea as [module] Ini 1.0b2 - ini-like handling of strings ?
I'm sorry if I missed something.. but could you explain the differences


The differences: mine is simple, fast and sweet my need (I don't need a complete library to do the job)
Back to top
View user's profile Send private message
GHezzu
Guest





PostPosted: Tue Oct 14, 2008 5:24 am    Post subject: Reply with quote

Who's the man
you are the man Very Happy

Just the thing i've been looking for
Back to top
Smurth



Joined: 13 Dec 2006
Posts: 106

PostPosted: Sun Jul 05, 2009 10:55 pm    Post subject: Reply with quote

Update: variation without regex for greater performances
Back to top
View user's profile Send private message
Scratch



Joined: 22 Jan 2009
Posts: 72

PostPosted: Sun Sep 06, 2009 10:44 pm    Post subject: Reply with quote

Great Stuff, with only a minor Tweak to your code I made a complementary writeini function as well...

Code:

WriteIni( filename = 0 )
; updates a whole .ini file
; %Section%%Key% = %value%
{
Local s, c, p, key, k, write

   if not filename
      filename := SubStr( A_ScriptName, 1, -3 ) . "ini"

   FileRead, s, %filename%

   Loop, Parse, s, `n`r, %A_Space%%A_Tab%
   {
      c := SubStr(A_LoopField, 1, 1)
      if (c="[")
         key := SubStr(A_LoopField, 2, -1)
      else if (c=";")
         continue
      else {
         p := InStr(A_LoopField, "=")
         if p {
            k := SubStr(A_LoopField, 1, p-1)
       write := %key%%k%
       IniWrite, %write%, %filename%, %key%, %k%
         }
      }
   }
}



I included a little test.ahk and a test.ini to see it work

Code:

#include readini.ahk
#include writeini.ahk
;
;
readini(test)
msgbox posx := %posx% , posy := %posy% , posz := %posz%
;
; Now we change the variables and write/update the ini
;
posx := posx * 2
posy := posy * 2
posz := posz * 2
writeini(test)
Msgbox Updated variables written...
;
; To confirm the INI is correctly updated, we read out the INI again
;
readini(test)
msgbox UPDATED >>> posx := %posx% , posy := %posy% , posz := %posz%


TEST.INI

Code:

[pos]
x=100
y=300
Z=450


Last edited by Scratch on Mon Sep 07, 2009 12:24 am; edited 2 times in total
Back to top
View user's profile Send private message
Scratch



Joined: 22 Jan 2009
Posts: 72

PostPosted: Sun Sep 06, 2009 11:06 pm    Post subject: combining the iniread/write function into one function ? Reply with quote

Since the write/update whole INI function is merely a minor tweaked copy of your read whole INI function it took little effort to combine them in one function:

Code:

update( filename = 0, updatemode = 0 )
;
; updates From/To a whole .ini file
;
; By default the update mode is set to 0 (Read)
; and creates variables like this:
; %Section%%Key% = %value%
;
; You don't have to state the updatemode when reading, just use
;
; update(filename)
;
; The function can be called to write back updated variables to
; the .ini by setting the updatemode to 1, like this:
;
; update(filename, 1)
;
{
Local s, c, p, key, k, write

   if not filename
      filename := SubStr( A_ScriptName, 1, -3 ) . "ini"

   FileRead, s, %filename%

   Loop, Parse, s, `n`r, %A_Space%%A_Tab%
   {
      c := SubStr(A_LoopField, 1, 1)
      if (c="[")
         key := SubStr(A_LoopField, 2, -1)
      else if (c=";")
         continue
      else {
         p := InStr(A_LoopField, "=")
         if p {
         k := SubStr(A_LoopField, 1, p-1)
       if updatemode=0
          %key%%k% := SubStr(A_LoopField, p+1)
       if updatemode=1
       {
          write := %key%%k%
          IniWrite, %write%, %filename%, %key%, %k%    
       }
         }
      }
   }
}


Here is the testprogram:

Code:

#include update.ahk
;
;
update(test)
msgbox posx := %posx% , posy := %posy% , posz := %posz%
;
; Now we change the variables and write/update the ini
;
posx := posx * 2
posy := posy * 2
posz := posz * 2
update(test, 1)
Msgbox Updated variables written...
;
; To confirm the INI is correctly updated, we read out the INI again
;
update(test)
msgbox UPDATED >>> posx := %posx% , posy := %posy% , posz := %posz%


and the test.ini

Code:

[pos]
x=100
y=300
Z=450
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1081

PostPosted: Sat Sep 26, 2009 11:58 am    Post subject: Reply with quote

There is a little problem: Whitespaces around the key or section lead to illegal character in variable name.
Code:
[pos]
x =100
y=300
Z=450


Here is the test file and function in one file together. Now you just need to start the script to test. Ini file is created from scratch and saved onto disk, every time you start the script.
Code:
/*
; ATTENTION! THIS COMMENT BLOCK IS IN INI FORMAT AND COULD BE USED AS INI FILE.
[META]
Source =            http://www.autohotkey.com/forum/viewtopic.php?t=36601
Language =              en
Description =           If you want to create lots of variable from .ini files, this function is for you. It reads the whole file in one go and creates all the global variables.
Date =                  2009-09-07
Author =                Scratch
Category =              File Management
Type =                  Library
*/

#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

; .....................................................
;                    TEST SECTION
; .....................................................

iniFile := SubStr( A_ScriptName, 1, -3 ) . "ini"
iniContent =
(
[pos]
x =100
y=300
Z=450
)
replaceFile(iniFile, iniContent)

ini(test)
msgbox posx := %posx% , posy := %posy% , posz := %posz%
;
; Now we change the variables and write/update the ini
;
posx := posx * 2
posy := posy * 2
posz := posz * 2
ini(test, 1)
Msgbox Updated variables written...
;
; To confirm the INI is correctly updated, we read out the INI again
;
ini(test)
msgbox UPDATED >>> posx := %posx% , posy := %posy% , posz := %posz%


RETURN ; END OF Auto-execution section


replaceFile(File, Content)
{
   FileDelete, %File%
   FileAppend, %Content%, %File%
}
Return

; .....................................................
;               END OF TEST SECTION
; .....................................................


ini( filename = 0, updatemode = 0 )
;
; updates From/To a whole .ini file
;
; By default the update mode is set to 0 (Read)
; and creates variables like this:
; %Section%%Key% = %value%
;
; You don't have to state the updatemode when reading, just use
;
; update(filename)
;
; The function can be called to write back updated variables to
; the .ini by setting the updatemode to 1, like this:
;
; update(filename, 1)
;
{
Local s, c, p, key, k, write

   if not filename
      filename := SubStr( A_ScriptName, 1, -3 ) . "ini"

   FileRead, s, %filename%

   Loop, Parse, s, `n`r, %A_Space%%A_Tab%
   {
      c := SubStr(A_LoopField, 1, 1)
      if (c="[")
         key := SubStr(A_LoopField, 2, -1)
      else if (c=";")
         continue
      else {
         p := InStr(A_LoopField, "=")
         if p {
         k := SubStr(A_LoopField, 1, p-1)
       if updatemode=0
          %key%%k% := SubStr(A_LoopField, p+1)
       if updatemode=1
       {
          write := %key%%k%
          IniWrite, %write%, %filename%, %key%, %k%   
       }
         }
      }
   }
}


Play with some changes on iniContent variable to see what works.

The function globalsFromIni() is similair to this, but it does not have a write feature. May be there is some line of code you are interested in it.

I am also working on something which works with strings formatted like an ini (seen several times, but I want to have my own solution).
Back to top
View user's profile Send private message
hapyman



Joined: 21 Oct 2009
Posts: 37

PostPosted: Thu Oct 22, 2009 7:24 pm    Post subject: Reply with quote

Hey guys,

I am trying to customize this code to suite my needs but I can't even test drive it as I keep getting an error.


Code:
readini.ahk (4) : ==> A label must not point to a function.


Not sure what this means as I copied it word for word so I could fiddle with it. I am new to AHK and am using this as a learning tool. I am making a script and hit a brick wall. If you're interested you can see what I am trying to do here: http://www.autohotkey.com/forum/viewtopic.php?p=304828#304828

Thanks a bunch. Appreciate any help.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group