AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

globalsFromIni() - creates globals from an Ini file

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Tuncay



Joined: 07 Nov 2006
Posts: 1081

PostPosted: Thu Jan 24, 2008 10:48 pm    Post subject: globalsFromIni() - creates globals from an Ini file Reply with quote

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.

Download Source

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:
Code:
globalsFromIni("test.ini")

would create from an INI file with this content:
Quote:
[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:
  • Search_In_Items_1 = All Documents
  • Search_In_Items_2 = All Inbound
  • Search_In_Items_3 = All Meters

  • Search_In_Restrictions_1 = Library\
  • Search_In_Restrictions_2 = Library\Inbound\
  • 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.

Code:
; 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.


Last edited by Tuncay on Mon Mar 29, 2010 10:06 pm; edited 6 times in total
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1081

PostPosted: Fri Feb 13, 2009 9:42 pm    Post subject: Reply with quote

It is about one year ago, I posted this little function. 2648 views and no reply?
Back to top
View user's profile Send private message
fincs



Joined: 05 May 2007
Posts: 648
Location: Seville, Spain

PostPosted: Fri Feb 13, 2009 9:46 pm    Post subject: Reply with quote

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

Calm down Wink
_________________
fincs
SciTE4AutoHotkey v2
[AutoHotkeyCE] [AutoHotkey_L] [AutoHotkey64]
Back to top
View user's profile Send private message
Hezzu



Joined: 08 Aug 2008
Posts: 117
Location: Raahe, Finland

PostPosted: Fri Feb 13, 2009 10:22 pm    Post subject: Reply with quote

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 Surprised

Good job and keep going!
_________________
Hezzu - excuse the english!
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vahju



Joined: 17 Feb 2008
Posts: 253

PostPosted: Fri Feb 13, 2009 10:33 pm    Post subject: Reply with quote

Tuncay wrote:
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.
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1081

PostPosted: Fri Feb 13, 2009 10:36 pm    Post subject: Reply with quote

Thx. I do not mean that this function is good or something. I am just wondering about the fact that so many people visiting this page without leaving any comment.
Back to top
View user's profile Send private message
werD
Guest





PostPosted: Sat Feb 14, 2009 8:09 pm    Post subject: thx Reply with quote

Thanks!
The great part abput sharing scripts is you don't have tp join or (think you have to join) to download a copy. Thats prolly y you get more views than replies. You want replies post broken script Wink you want views post a good one Very Happy
Back to top
Tuncay



Joined: 07 Nov 2006
Posts: 1081

PostPosted: Sat Feb 14, 2009 8:43 pm    Post subject: Reply with quote

Added links to download the source.
Back to top
View user's profile Send private message
BoBo³
Guest





PostPosted: Sun Feb 15, 2009 1:34 am    Post subject: Reply with quote

Quote:
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.
Well, for that reason I use AHKs #Include <filename> directive ...
so the scripts code looks like this:
Code:
#Include MySuperDuperExternalVariables.txt

while MySuperDuperExternalVariables.txt looks like this:
Quote:
MyNameVar = BoBo
DödeliWödeliVar = 0815
globalsFromIni()Dev := "Tuncay"


Btw, if you don't like to use an external INI, abuse your script to act as an INI-look-alike Wink

Code:
#SingleInstance, Force

/*
This is your secret 'embedded' INI section
[ForTheRecords]
   LastScriptUser =
*/

!y::INIWrite, %A_UserName%, %A_ScriptFullPath%, ForTheRecords, LastScriptUser ; press F5 to see the updated INI setting while the script is open in your editor
!a:: ; press ALT+a to see the scripts 'embedded' INI value
    INIRead, LastUser, %A_ScriptFullPath%, ForTheRecords, LastScriptUser
    MsgBox %LastUser%
    Return

Nevertheless, thanks for sharing your code with the community. Cool
Back to top
Tuncay



Joined: 07 Nov 2006
Posts: 1081

PostPosted: Sun Feb 15, 2009 1:49 am    Post subject: Reply with quote

Bobo wrote:
Btw, if you don't like to use an external INI, abuse your script to act as an INI-look-alike

Bobo I do the same with my scripts Very Happy Look in downloadable script source, there is a header which i did not include in the code section in posting.

Quote:
; 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=27928
Language = en
Name = globalsFromIni
Description = Creates global variables from an Ini file.
Revision = 1.0
Status = Final
Date = 2008-01-24 10:48
Author = Tuncay
License = Public Domain
Category = Misc.
Type = Library
Standalone = Yes
StdLibConform = No
*/


That why the script itself can be used like an ini file. But with attention... I do this with all my scripts. May be for future bye searching specific scripts or for categorizing reason ...

You see, I am also a bit bobolized.


Last edited by Tuncay on Mon Mar 29, 2010 10:07 pm; edited 1 time in total
Back to top
View user's profile Send private message
BoBo³
Guest





PostPosted: Sun Feb 15, 2009 2:00 am    Post subject: Reply with quote

Quote:
May be for future bye searching specific scripts or for categorizing reason ...
That's a great idea! Would be nice to have a tinytool/GUI to parse/filter existing scripts for such entries. Danke. Very Happy
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group