AutoHotkey Community

It is currently May 26th, 2012, 7:25 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: January 4th, 2009, 5:26 am 
Offline

Joined: July 13th, 2008, 9:24 am
Posts: 7
Location: Australia
Just a quick lazy post while I'm on a break at work, just want to show you guys.

;;; ReadIni Function v1.1 ;;;
/*

ReadIni(Filename, Option [, Section])

Each parameter needs to be in quotes
e.g. ReadIni("H:\L2Hotkeys.ini", "keys", "Hotkey State")

Filename:
The name of the .ini file, which is assumed to be in
%A_WorkingDir% if an absolute path isn't specified.

Option:
Sections - Returns the number of Sections in Ini file. = Done
Section N - Returns the Name of Nth Section in Ini file.

Keys - Returns the number of Keys in Section. = Done
Key N - Returns the Name of Nth Key in Section.
Key - Returns the Value of Key in Section.

Section:
The section name in the .ini file, which is the heading phrase that appears
in square brackets (do not include the brackets in this parameter).

*/

Code:
ReadIni(ri_filename,ri_option,ri_section = 0)
{
   FileRead, ri_contents, %ri_filename%
   
   if ri_option = Sections
   {
      ri_scount = 0
      Loop, parse, ri_contents, `n, `r
      {
         StringLeft, 1stChar, A_LoopField, 1
         if 1stChar = [
            ri_scount+=1
      }
      return ri_scount
   }
   
   if ri_option = Keys
   {
      Loop, parse, ri_contents, `n, `r
      {
         if A_LoopField = [%ri_section%]
            Line1 = %A_Index%
         if (Line1 && !A_LoopField)
         {
            Line2 = %A_Index%
            kcount := Line2 - (Line1 + 1)
            return kcount
         }
      }
   }
   ri_contents =
}


Code:
/*
^!1::
   Count := ReadIni("H:\L2Hotkeys.ini", "sections")
   MsgBox, No. of Sections: %Count%
return

^!2::
   Count := ReadIni("H:\L2Hotkeys.ini", "keys", "Hotkey State")
   MsgBox, No. of Keys: %Count%
return
*/

_________________
I am awesome.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 27th, 2009, 2:26 pm 
Offline

Joined: March 27th, 2009, 12:46 pm
Posts: 76
Location: Dublin, IE
I've modified unexist03's code to give a list of sections, keys in a specified sections, or list of all keys in the ini file.

Usage:
Code:
ReadIni(Filename, Option, Option2 [, Section])

Filename:
The name of the .ini file, which is assumed to be in %A_WorkingDir% if an absolute path isn't specified.

Option:
Sections - Returns an array of all sections in the ini file or number of sections.
Keys - Returns an array or number of the keys in a section or an array or number all keys in the ini file.

Option2:
List - Returns an array of keys in a section, all keys in the file, or sections in the file.
Count - To be used to return the number of sections or keys.

Section:
The section name in the .ini file, which is the heading phrase that appears
in square brackets (do not include the brackets in this parameter). Leave blank if you want to return all keys or sections in the file.

Examples:

Get an array of all sections in test.ini
Code:
Array := ReadIni("test.ini","Sections","List")

Count the number of sections in test.ini
Code:
Array := ReadIni("test.ini","Sections","Count")

Get an array of all keys in test.ini
Code:
Array := ReadIni("test.ini","Keys","List")

Count the number of keys in test.ini
Code:
Array := ReadIni("test.ini","Keys","Count")

Get an array of all keys in the General section.
Code:
Array := ReadIni("test.ini","Keys","List", "General")

Count the number of keys in the General section.
Code:
Array := ReadIni("test.ini","Keys","Count", "General")

ReadIni.ahk
Code:
; ---Read INI file
ReadIni(ri_filename,ri_option,ri_option2,ri_section = 0)
{
   FileRead, ri_contents, %ri_filename%
   if ri_option = Sections
   {
      ri_scount = 0
      Loop, parse, ri_contents, `n, `r
      {
         StringLeft, 1stChar, A_LoopField, 1
         ; ---See if line is a section
         if 1stChar = [
         {
         if ri_option2 = count
            ri_scount+=1
         if ri_option2 = list
         {   
            ; ---Count all sections
            if (!ri_scount)
            {
               StringTrimLeft, var, A_LoopField, 1
               StringTrimright, ri_scount,var,1
            }
            ; ---Get array of all sections
            else
            {
               StringTrimLeft, var, A_LoopField, 1
               StringTrimright, var,var,1
               ri_scount=%ri_scount%,%var%
            }
         }
         }   
      }
      return ri_scount
   }
   
   if ri_option = Keys
   {
      kcount=0
      Loop, parse, ri_contents, `n, `r
      {
         StringLeft, 1stChar, A_LoopField, 1
         var := RegExReplace(A_LoopField,"\=.*")
         ; ---If line is a section
         if A_LoopField = [%ri_section%]
         {
            Line1 = %A_Index%
            i++
         }
         ; ---If line is empty and is after the specified section
         if (Line1 && !A_LoopField && ri_section!=0)
         {
            Line2 = %A_Index%
            kcount := Line2 - (Line1 + 1)
            if ri_option2 = count
               return kcount
            if ri_option2 = list
               return SecKeys
         }
         ; ---If line is after section and is a key
         if (i=1 && 1stChar <>"[")
         {
            ; ---First string in array
            If (!SecKeys)
               SecKeys=%var%
            ; ---All other strings in array
            else
               SecKeys=%SecKeys%,%var%
         }
         ; ---Get all keys and count them when sections variable is null
         if (ri_section=0 && 1stChar!="[" && 1stChar!=";" && A_LoopField)         
         {
            ; ---First string in array
            If !AllKeys
               AllKeys=%var%
            ; ---All other strings in array
            Else
               AllKeys=%AllKeys%,%var%
            kcount++
         }
      }
      ; ---Return list of all keys or number of all keys if sections var is null
      if ri_option2 = list
         Return, AllKeys
      if ri_option2 = count
         Return, kcount
   }
   ; ---Clear variable
   ri_contents =
}

_________________
My Scripts


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 29th, 2009, 6:39 am 
Offline

Joined: February 11th, 2009, 2:23 pm
Posts: 142
Location: India
pretty awesom

thanks I needed this ☺


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 2nd, 2009, 4:25 am 
Offline

Joined: July 13th, 2008, 9:24 am
Posts: 7
Location: Australia
Nice :)
I'll most likely read over it then update my own copy with yours :)
Should draw Chris' attention to this, he may be interested.

_________________
I am awesome.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Scratch and 23 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