Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Getting Info from Ini files


  • Please log in to reply
5 replies to this topic
Invalid User
  • Members
  • 447 posts
  • Last active: Mar 27 2012 01:04 PM
  • Joined: 14 Feb 2005
;IniSectionGet Fuction
;Get All sections names from Said file and store in said array
IniSectionGet(File,Array)
{
	i = 0 ;Index used for array element number
	Loop, Read, %File%
	{
		StringLeft, L, A_LoopReadLine, 1
		;Possible Section name, so check right side
		If L = [
		{
			StringRight, R, A_LoopReadLine, 1
			;If its a right bracket Section found
			If R = ]
			{
				i++
				;Econt = Element Contents
				ECont = %A_LoopReadLine%
				StringTrimLeft, ECont, ECont, 1
				StringTrimRight, ECont, ECont, 1
				%Array%%i% = %ECont%
			}
		}
	}
	%Array%0 = %i%
}

The above funtion syntax is:
IniSectionGet(File,Array)
File is the Ini file to be checked
Array is the name of the array for info to be stored in.

Array0 will contain how many sections were found in the file and the Array Elements will contain the names of the sections.

Hardcore testing welcome ...as well as optimization (Laszlo???)

Sect = Section2
Array = SampleArray
File = %A_ScriptDir%\TestIni.ini
IniKeyGet(File,Sect,Array)
F9::Reload
;IniKeyGet Function
;Gets all key names from said section of said file and store in said array
;Array0 stores how many elements are in said array.
IniKeyGet(File,Sect,Array)
{
	i = 0
	LineNum = 0
	Loop, Read, %File%
	{
		LineNum ++
		Section = [%Sect%]
		If A_LoopReadLine = %Section% 
		{
			LineNum++
			Loop
			{
				FileReadLine, LineCont, %File%, %LineNum%
				;Check is line may be a section, not a key name
				StringLeft, LL, LineCont, 1
				If LL = [
				{
					;Declare Array0 Value
					%Array%0 = %i%
					ListVars
					Return
				}
				;Line not a key
				If LineCont Not Contains =
				{	
					LineNum++
					Continue
				}
				i++
				StringSplit, KeyName, LineCont, =
				%Array%%i% = %KeyName1%
				LineNum++
			}
		}
	}
}

Syntax for IniKeyGet
IniKeyGet(File,Section,Array)
File To anylize
Section to anylize
Name of array to be created

Array0 will contain the amount of keys in the section and each element will contain the name of the key

Again, Testing, comments, feedback, and optimization all very welcome

Edit: New code submitted, less tested
Edit: KeyGet Fn futher optimized. From 68 line to 45 lines
my lame sig :)

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
For the IniSectionGet Function:

the following lines are not neccessary:
...
Local i
...
Else ;Not a section name
Continue
...


You could make sure to eliminate all whitespaces and beginning and end of each line before testing if section, to make it robust against human modified ini files. maybe also check if contents has a space or "=". But I'm not sure, since I do not know if this is allowed in INI file
[Toralf=DAU]
[Toralf = DAU]
Are these sections or keys?
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
And this is more straight forward:
If R = ] 
         { 
            i++ 
            ;Econt = Element Contents 
            ECont = %A_LoopReadLine% 
            StringTrimLeft, ECont, ECont, 1 
            StringTrimRight, ECont, ECont, 1 
            %Array%%i% = %ECont% 
         } 

Just remove the "<>" and remove
Continue 
         ;Else, section name 
         Else 

Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
To the IniKeyGet function:

Why do your search this complex? You know that the sectionname is "[" + Section + "]". So could just search for that line as a whole string.

And you can simplify the script similiar to the previous function
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

Invalid User
  • Members
  • 447 posts
  • Last active: Mar 27 2012 01:04 PM
  • Joined: 14 Feb 2005
[Text=moreText]
is seen as a section and can be read from

KeyNames that start with [ or ; Return ERROR

Comments made in Ini file can be:

;Commented with a leading semi-colon
Or be like this line where it is just text, but must not contain an equal sign
So a line that = something like this one would not work
;Because there is really a key named "So a line that" in this section

I may use Autotrim to do as you said, if I understand it correctly. but I would do that later as I am very tired.
my lame sig :)

TheIrishThug
  • Members
  • 419 posts
  • Last active: Jan 18 2012 02:51 PM
  • Joined: 19 Mar 2006
The code posted above for IniKeyGet allows for an endless loop.
Loop
{
   FileReadLine, LineCont, %File%, %LineNum%
   ;Check is line may be a section, not a key name
   If LL = [
   {
      ;Declare Array0 Value
      %Array%0 = %i%
      ListVars
      Return
   }
   ...
   LineNum++
}
The loop will only break if the new line is the beginning of a new section. However, if the section you start in is the last section of the file, FileReadLine will contine to read past the end of the file testing non-existant lines to see if they start with [.

The following corrects that bug by checking ErrorLevel after FileReadLine. There are also settings for global and local variables so the array can be accessed outside of the function.

IniKeyGet(File,Sect,Array)
{
  Global
  Local i := 0, LineNum := 0, KeyName0, KeyName1, KeyName2, Section, LineCont, ErrLvl
  Loop, Read, %File%
  {
    LineNum ++
    Section = [%Sect%]
    If A_LoopReadLine = %Section%
    {
      LineNum++
      Loop
      {
        FileReadLine, LineCont, %File%, %LineNum%
        ;Check is line may be a section, not a key name
        If (ErrorLevel OR SubStr(LineCont, 1, 1) = "[")
        {
          ;Declare Array0 Value
          %Array%0 := i
          Return i
        }
        ;Line not a key
        If LineCont Not Contains =
        {   
          LineNum++
          Continue
        }
        i++
        StringSplit, KeyName, LineCont, =
        %Array%%i% := KeyName1
        LineNum++
      }
    }
  }
}