AutoHotkey Community

It is currently May 26th, 2012, 9:54 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
PostPosted: October 11th, 2008, 4:16 am 
Offline

Joined: December 13th, 2006, 7:10 am
Posts: 118
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 July 5th, 2009, 11:00 pm, edited 5 times in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: October 11th, 2008, 5:31 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
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


:?: :!:

Nice script, BTW :)

_________________
URLGet - Internet Explorer based Downloader
StartEx - Portable Shortcut Link


Report this post
Top
 Profile  
Reply with quote  
PostPosted: October 11th, 2008, 8:19 am 
Offline

Joined: December 13th, 2006, 7:10 am
Posts: 118
...


Last edited by Smurth on July 5th, 2009, 10:57 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 11th, 2008, 10:53 pm 
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:


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 11th, 2008, 11:05 pm 
Offline

Joined: December 13th, 2006, 7:10 am
Posts: 118
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 12th, 2008, 3:12 am 
Offline

Joined: July 29th, 2005, 5:32 pm
Posts: 179
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 12th, 2008, 4:09 am 
Offline

Joined: December 13th, 2006, 7:10 am
Posts: 118
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)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 14th, 2008, 5:24 am 
Who's the man
you are the man :D

Just the thing i've been looking for


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: July 5th, 2009, 10:55 pm 
Offline

Joined: December 13th, 2006, 7:10 am
Posts: 118
Update: variation without regex for greater performances


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 6th, 2009, 10:44 pm 
Offline

Joined: January 22nd, 2009, 3:43 pm
Posts: 84
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 September 7th, 2009, 12:24 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
PostPosted: September 6th, 2009, 11:06 pm 
Offline

Joined: January 22nd, 2009, 3:43 pm
Posts: 84
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 26th, 2009, 11:58 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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).


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 22nd, 2009, 7:24 pm 
Offline

Joined: October 21st, 2009, 6:55 pm
Posts: 48
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group