AutoHotkey Community

It is currently May 26th, 2012, 6:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: January 24th, 2008, 10:48 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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:
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 November 27th, 2010, 10:33 pm, edited 7 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2009, 9:42 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
It is about one year ago, I posted this little function. 2648 views and no reply?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2009, 9:46 pm 
Offline
User avatar

Joined: May 5th, 2007, 7:24 pm
Posts: 1240
Location: Seville, Spain
Tuncay wrote:
It is about one year ago, I posted this little function. 2648 views and no reply?

Calm down ;)

_________________
fincs
Highly recommended: AutoHotkey_L (see why) (all my code snippets require it)
Formal request to polyethene - I support the unity of the AutoHotkey community
Get SciTE4AutoHotkey v3.0.00 (Release Candidate)
[My project list]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2009, 10:22 pm 
Offline

Joined: August 8th, 2008, 7:26 pm
Posts: 117
Location: Raahe, Finland
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!

_________________
Hezzu - excuse the english!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2009, 10:33 pm 
Offline

Joined: February 17th, 2008, 8:52 pm
Posts: 314
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 13th, 2009, 10:36 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: thx
PostPosted: February 14th, 2009, 8:09 pm 
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 ;) you want views post a good one :D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 14th, 2009, 8:43 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
Added links to download the source.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2009, 1:34 am 
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. 8)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2009, 1:49 am 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1934
Location: Germany
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 :D 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 March 29th, 2010, 10:07 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2009, 2:00 am 
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. :D


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2011, 12:57 am 
Offline

Joined: July 31st, 2008, 10:27 pm
Posts: 336
Tuncay wrote:
It is about one year ago, I posted this little function. 2648 views and no reply?


I found a use for this today, so thanks Tuncay. :D

I have some strange characters in my ini file, so I changed the function to this:

Code:
globalsFromIni( FileContent, _VarPrefixDelim = "_")
{
    Global
    Local CurrentPrefix, CurrentVarName, CurrentVarContent, DelimPos
    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
                CurrentPrefix := Varize(CurrentPrefix)
            }
            Else
            {
                DelimPos := InStr(A_LoopField, "=")
                StringLeft, CurrentVarName, A_LoopField, % DelimPos - 1
                CurrentVarName := Varize(CurrentVarName)
                StringTrimLeft, CurrentVarContent, A_LoopField, %DelimPos%
                %CurrentPrefix%%_VarPrefixDelim%%CurrentVarName% = %CurrentVarContent%
            }
        }
    }
}

Varize(Name)
{
 Name = %Name%
 SetFormat, Integer, hex
 Loop, Parse, Name,
 {
  If A_LoopField is alnum
     NewName .= A_LoopField
  Else
  {
   Num := Asc(A_LoopField)
   Num += 0
   NewName .= "¬" Num
  }
 }
 SetFormat, Integer, d
 Return NewName
}


The basis for the varize() function comes from PhilHo's post in this thread:
http://www.autohotkey.com/forum/topic10358.html


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2011, 1:37 am 
Offline
User avatar

Joined: May 18th, 2010, 3:10 pm
Posts: 1179
Location: Sweden
I used something like this a while ago, and while I must say that I like some of the other solutions around, this is probably the easiest way to do it. Really simple and scriper-friendly.

_________________
~sumon Appifyer AHK Nova halted Recommended: AHK_L (Why?)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2011, 3:22 am 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
Here's a tougher one: GlobalsToINI() :?:

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 2nd, 2011, 1:39 pm 
Offline

Joined: July 31st, 2008, 10:27 pm
Posts: 336
nimda wrote:
Here's a tougher one: GlobalsToINI() :?:


Like this?

http://www.autohotkey.com/forum/topic21346.html

I wish I had seen that sooner. :(


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 10 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