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
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Thu Jan 24, 2008 9: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.

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 Sat Nov 27, 2010 9:33 pm; edited 7 times in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Fri Feb 13, 2009 8: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 Send e-mail Visit poster's website
fincs



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

PostPosted: Fri Feb 13, 2009 8: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
Get SciTE4AutoHotkey v3.0.00 (Release Candidate)
[My project list]
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 9: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: 296

PostPosted: Fri Feb 13, 2009 9: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: 1886
Location: Germany

PostPosted: Fri Feb 13, 2009 9: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 Send e-mail Visit poster's website
werD
Guest





PostPosted: Sat Feb 14, 2009 7: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: 1886
Location: Germany

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

Added links to download the source.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
BoBo³
Guest





PostPosted: Sun Feb 15, 2009 12: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: 1886
Location: Germany

PostPosted: Sun Feb 15, 2009 12: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 9:07 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
BoBo³
Guest





PostPosted: Sun Feb 15, 2009 1: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
Morpheus



Joined: 31 Jul 2008
Posts: 270

PostPosted: Fri Apr 01, 2011 11:57 pm    Post subject: Reply with quote

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. Very Happy

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
Back to top
View user's profile Send private message
sumon



Joined: 18 May 2010
Posts: 1011
Location: Sweden

PostPosted: Sat Apr 02, 2011 12:37 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
nimda



Joined: 26 Dec 2010
Posts: 3838
Location: Awesometown, USA

PostPosted: Sat Apr 02, 2011 2:22 am    Post subject: Reply with quote

Here's a tougher one: GlobalsToINI() Question
_________________
Spam. Autoclick. Rapidfire.Window Control ToolsLicense
Back to top
View user's profile Send private message
Morpheus



Joined: 31 Jul 2008
Posts: 270

PostPosted: Sat Apr 02, 2011 12:39 pm    Post subject: Reply with quote

nimda wrote:
Here's a tougher one: GlobalsToINI() Question


Like this?

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

I wish I had seen that sooner. Sad
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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