Jump to content

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

globalsFromIni() - creates globals from an Ini file


  • Please log in to reply
6 replies to this topic
Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
globalsFromIni() Version 1.0

By reading an Ini file with the standard commands of AutoHotkey, the file would be reopened and closed after process on every single key. This could impact the performance of the script, if a large number of keys are to read, like in a loop command.

So its better to read the Ini file one time into an AutoHotkey variable and parse it with some string commands. This is super fast, in comparison to reading the contents of all keys with standard IniRead command.

In the forum, there are some solutions already. They may better, I don´t know. Here is my solution. It creates from an Ini file automatically global variables.

Example

Usage is very easy:
globalsFromIni("test.ini")
would create from an INI file with this content:

[Search_In_Items]
1=All Documents
2=All Inbound
3=All Meters

[Search_In_Restrictions]
1=Library\
2=Library\Inbound\
3=Library\Meters\

these global variables:
[*:339ieghk]Search_In_Items_1 = All Documents
[*:339ieghk]Search_In_Items_2 = All Inbound
[*:339ieghk]Search_In_Items_3 = All Meters

[*:339ieghk]Search_In_Restrictions_1 = Library\
[*:339ieghk]Search_In_Restrictions_2 = Library\Inbound\
[*:339ieghk]Search_In_Restrictions_3 = Library\Meters\
Source:

The function´s first parameter is the file path to the ini file to read. The second parameter is optional and should be better leaved by default "_"-character. This specifies the separator between section name and key name. All section names and key names are merged into single name.

; Creates global variables from an Ini file.
globalsFromIni(_SourcePath, _VarPrefixDelim = "_")
{
    ; Public Domain 2008 Tuncay
    Global
    Local FileContent, CurrentPrefix, CurrentVarName, CurrentVarContent, DelimPos
    FileRead, FileContent, %_SourcePath%
    If ErrorLevel = 0
    {
        Loop, Parse, FileContent, `n, `r%A_Tab%%A_Space%
        {
            If A_LoopField Is Not Space
            {
                If (SubStr(A_LoopField, 1, 1) = "[")
                {
                    StringTrimLeft, CurrentPrefix, A_LoopField, 1
                    StringTrimRight, CurrentPrefix, CurrentPrefix, 1
                }
                Else
                {
                    DelimPos := InStr(A_LoopField, "=")
                    StringLeft, CurrentVarName, A_LoopField, % DelimPos - 1
                    StringTrimLeft, CurrentVarContent, A_LoopField, %DelimPos%
                    CurrentVarName = %CurrentVarName%
                    %CurrentPrefix%%_VarPrefixDelim%%CurrentVarName% = %CurrentVarContent%
                }
            }
        }
    }
}

Like I said already, there are some already existing functions which does the same. But I could not find them again. Its public domain, do whatever you want with it.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
It is about one year ago, I posted this little function. 2648 views and no reply?

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007

It is about one year ago, I posted this little function. 2648 views and no reply?

Calm down ;)

Hezzu
  • Members
  • 117 posts
  • Last active: Mar 13 2014 11:46 AM
  • Joined: 08 Aug 2008
Everybody was so amazed that they went speechless

I think this would be very useful in some situations

But still amazing thing that there´s only 2 comments including mine on this script :O

Good job and keep going!

vahju
  • Members
  • 337 posts
  • Last active: Sep 21 2014 03:52 AM
  • Joined: 17 Feb 2008

It is about one year ago, I posted this little function. 2648 views and no reply?


You must learn the art of the "BUMP" young padawan.

aixylinux
  • Members
  • 1 posts
  • Last active: Dec 11 2014 06:15 PM
  • Joined: 11 Dec 2014

Thanks for this function.  I found a very good use for it today.



LarryC
  • Members
  • 68 posts
  • Last active: Aug 14 2016 07:53 PM
  • Joined: 28 Oct 2012

Hey, Tuncay, if you are still around.

 

Hey, Tuncay, if you are still around.
Maybe because, others, newbies, with no structured programming training, like myself, intuitively use FileRead over Ini.  Its just more common sense to someone who does not know any better. AHK caters to non-programmers. I abandoned Ini read almost from the beginning and assume so has everyone else. Your news may not be as earthshattering as you may have thought, Others may be already using their own (and simpler version). Just guessing.
 
Absolute credit must be given to Chris Mallot and his super extraordinary examples he supplies with every "FileEncoding, FileOpen/File Object, file-reading loop, FileReadLine, FileGetSize, FileAppend, IniRead, Sort, UrlDownloadToFile" command. That makes everything easier for non-trained programmers. May be the reason for yr statement "and no reply? "