AutoHotkey Community

It is currently May 27th, 2012, 5:23 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: INIParser
PostPosted: December 22nd, 2005, 2:48 pm 
Hi. I want to parse an INI file to identify:
a) its keys (their number might vary)
b) get the keys values
c) set the detected keys as global variables with their assigned values

what I've got so far
My.ini
Quote:
[DEFAULTS]
MyFirstKey=A
MySecKey=B
MyThirdKey=C

Code:
Loop, Read, My.ini
{
   If A_LoopReadLine contains =
   {
      StringSplit, Field, A_LoopReadLine, =
      INIRead, Value, My.ini, DEFAULTS, %Field1%
                   ; No idea how to keep the detected key (Field1) as a global var and to assign its detected value. ByRef ?
                   MsgBox % Field1 "=" Value
      }
   }
Any idea? Thx for listening. 8)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2005, 2:52 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Code:
Loop, Read, My.ini
  {
    If A_LoopReadLine contains =
     {
        StringSplit, Field, A_LoopReadLine, =
        INIRead, %Field1%, My.ini, DEFAULTS, %Field1%
     }
   }
But you have to handle the sections as well.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2005, 2:55 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Do you create the ini file yourself?
You could have a key that contains the names of the other keys as a list. But then this key has to be updated and always available.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2005, 5:14 pm 
INIRead (as other AHK commands) rejects to set a variable referenced via a variable (illegal char % blabla) ...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2005, 5:39 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Ok then try
Code:
INIRead, Value , My.ini, DEFAULTS, %Field1%
%Field1% = %Value%

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2005, 5:53 pm 
I guess I've tested that already. Which is nice within a message box, but how to reference it as a global var ???

BTW: I've found a workarround --> #Include MyPseudo.ini
While MyPseudo.ini has to contain only key=var (without any standard INI-[Section]).
Nevertheless I would be interested to get the initial topic solved/discussed.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2005, 7:17 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
I do not know what you do not like about my code. This has been tested:
Code:
ListOfKeys =
Loop, Read, My.ini
  {
    If A_LoopReadLine contains =
      {
        StringSplit, Field, A_LoopReadLine, =
        INIRead, Value, My.ini, DEFAULTS, %Field1%
        %Field1% = %Value%
        ListOfKeys = %ListOfKeys%|%Field1%
      }
   }
StringTrimLeft,ListOfKeys,ListOfKeys,1
StringSplit, Key ,ListOfKeys,|
String =
Loop, %Key0%
  {
    Name := Key%A_index%
    Value := %Name%
    String = %String%%Name% = %Value%`n
  }
MsgBox, %String%
It works well.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2005, 7:19 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
And to be really sure that the values are set correctly, you can add this line at the end
Code:
MsgBox, MyFirstKey=%MyFirstKey%`nMySecKey=%MySecKey%`nMyThirdKey=%MyThirdKey%

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 22nd, 2005, 7:21 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
If you have other sections in your ini as well, you have to chnage the code to this
Code:
      {
        StringSplit, Field, A_LoopReadLine, =
        INIRead, Value, My.ini, DEFAULTS, %Field1%
        If Value <> ERROR
           {
              %Field1% = %Value%
               ListOfKeys = %ListOfKeys%|%Field1%
            }
      }

_________________
Ciao
toralf
Image


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

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
BoBo wrote:
but how to reference it as a global var ???
Maybe I do not understand what you want to achive here. Could you please clarify? Why do you wan to define it as global? For a function? Why? What is the purpose/reason/objective/functionality?

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 23rd, 2005, 8:44 am 
Quote:
Why do you wan to define it as global? For a function?
Yes, indeed. :)

@ toralf
Es soll eine zentrale Steuerdatei geben, welche sämtliche Defaults (Server, UNC Zielpfade, Dateifilterangaben etc.) beinhaltet. Diese wird bei Initialisierung des Prozesses eingelesen und soll die darin enthaltenen Variablen+Werte allen angetriggerten Funktionen bereitstellen. Schwierigkeit (war) hierbei, eine externe Zeichenkette/Namen als internen Variablennamen zu definieren um eine anschließende Wertzuweisung vorzunehmen (muss deine Beispiele noch testen, allein die Zeit wird knapp --> Urlaub :D). Grazie für deine Bemühungen. Und schöne Weihnachtstage ->(hier wackelt der ultimative x-mas smiley)<-


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 26th, 2005, 10:36 pm 
Hi BoBo,

I can read your text, but it doesn't make sense. You want to have a main ini file that holds any kind and number of data. And you want to provide this data to the called functions.
But functions have to have all their vars defined when they are coded, they do not accept any kind and number of vars. The only exception is the data they are working on. E.g. you hand over an array of data that get's split inside the function, or you can call the function multiple times (e.g. in a loop).
I do not know what your functions should do. Write me an email if I can help you on details/design.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 28th, 2005, 7:15 pm 
This?

inifile (name it 'testini.ini' and save it in the same folder as the script):

Code:
[dummies]
dummy1=error
dummy2=error

[variables]
var1=xxx
var2=yyy
var3=zzz

[dummies]
dummy1=error
dummy2=error

[more variables]
var4=111
var5=222
var6=333

[dummies]
dummy1=error
dummy2=error


Code:
/* ---
- Will load all keys as variables of a particular section of an ini file

loadVarsFromIni(iniFile,iniSections)

iniFile = Set to the path to the iniFile
iniSections = Set to the section(s) of the iniFile to load.
            Seperate multiple sections with commas
            (If this variable is empty, the whole iniFile will be loaded)
*/

; Examples

iniFile = testini.ini

loadVarsFromIni(iniFile,"variables")

msgbox,
(LTrim
Example 1: Loading section "variables"

variables:
var1 = %var1%
var2 = %var2%
var3 = %var3%

var4 = %var4%
var5 = %var5%
var6 = %var6%

dummies:
dummy1 = %dummy1%
dummy2 = %dummy2%
)

loadVarsFromIni(iniFile,"variables,more variables")

msgbox,
(LTrim
Example 2: Loading sections "variables" and "more variables"

variables:
var1 = %var1%
var2 = %var2%
var3 = %var3%

var4 = %var4%
var5 = %var5%
var6 = %var6%

dummies:
dummy1 = %dummy1%
dummy2 = %dummy2%
)

loadVarsFromIni(iniFile,"")

msgbox,
(LTrim
Example 3: Load all sections

variables:
var1 = %var1%
var2 = %var2%
var3 = %var3%

var4 = %var4%
var5 = %var5%
var6 = %var6%

dummies:
dummy1 = %dummy1%
dummy2 = %dummy2%
)

; Function

loadVarsFromIni(iniFile,iniSectionsToLoad)
{
   loadEm = 0
   Loop, Read, %iniFile%
   {
      ; Scan for section, if the line is a section, get section name,
      ; compare to iniSectionsToLoad, set loadEm flag
      If A_LoopReadLine contains [
      {
         If A_LoopReadLine contains ]
         {
            StringMid, anIniSection, A_LoopReadLine, InStr(A_LoopReadLine,"]")-1, StrLen(A_LoopReadLine)-2, L ; Get section name
            If anIniSection in %iniSectionsToLoad% ; is it in our matchlist?
               loadEm = 1
            Else
               loadEm = 0
            if iniSectionsToLoad = ; is it empty? -> get all
               loadEm = 1
         }
      }
      ; Set key and value of the line to a variable named like the key
      ; and with the value of the key
      If loadEm = 1
      {
         If A_LoopReadLine contains =
         {
            StringSplit, Field, A_LoopReadLine, =
            %Field1% := Field2
         }
      }
   }
}


Report this post
Top
  
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: Exabot [Bot], HotkeyStick, rbrtryn, XstatyK and 86 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