Ini

Set of functions for ini-like handling of strings

 

This set of functions allows you to handle strings that comply to the ini section format.  It makes ini section abstract, allowing you to store query and change related information as a whole instead having to take care about big number of variables.  You can create section string from real ini file or you can make it yourself.

The functions are designed to work with “worst possible” ini definition in mind, that is, white space in section or key definition may be present as much as empty sections, comments, empty lines etc.

There are 2 differences between this module and OS related functions.  First is that key values are not returned trimmed, so you can have values starting with white space.  The other one is that module generally doesn’t consider position of key in the section string as important or position of section in the ini file, and will not retain to change the position for the sake of speed.

Summary
Set of functions for ini-like handling of strings
Load one or all sections from the ini file.
Gets the names of all sections in the ini file.
Loads one or all keys from one or all sections.
Makes the section string using user global variables
Replace entire section in ini file.
Update section with new key values.
Get key value
Set the key value
Get name of the first key with given value
Delete one or more keys from the section
Delete keys from the section using regular expression
Add most recently used (MRU) item in the section string

LoadSection

Ini_LoadSection(pIniFile,  
pSection = "",
pPrefix = "inis_")

Load one or all sections from the ini file.

Parameters

iniFileIni file path
sectionSection to be returned by the function.  By default “”, which means all sections will be returned in array with the base name %prefix%.  Variable names will be constructed as %prefix%%sectionName%.  If section name contains characters illegal for AHK variable names, those will be removed.  If after removal of illegal characters section name is empty string it will be skipped.
prefixBase name of the array to be used for global variables, by default “inis_”

Returns

If section = “” list of sections separated by new lines otherwise requested section

GetSectionNames

Ini_GetSectionNames(pIniFile)

Gets the names of all sections in the ini file.

Parameters

iniFileIni file name

Returns

List of section names, each on new line

LoadKeys

Ini_LoadKeys(pIniFile,  
section =  "",
pInfo = 0,
prefix =  "",
filter = "",
reverse =  false)

Loads one or all keys from one or all sections.  Each key will be loaded into the global array with base name equal to its section name.

Parameters

pIniFileIni file path.  If you set “” here, next parameter will be seen as section string.
sectionBy default “”, means to load keys for all sections.  This will create array of global variables with the name %prefix%%section%_%key%.  If section is not empty, the function load keys only for that section.  If section is not empty and pIniFile is empty this parameter contains section string and since section name is not available, globals are created as %prefix%_%key%
prefixPrefix for global variables created, or key/value delimiter when pInfo != 0
pInfoFlag determining what to return.  See return values of the function for details.
filterOnly key names starting with this word will be retrieved.  For instance, if the section contain keys name1, def1, name2, def2, name3, def3.... you can set filter to “name” or to “def” for function to see only those keys.  To use regular expression, specify new line as first char, for instance “`ni)^abc”
reverseReverse filter operation, get only key names NOT fitting the filter.

Returns

pInfo=0 or pInfo=””number of variables created (default).
pInfo=1 or pInfo=”keys”function returns key names separated by prefix string (by default `n)
pInfo>1 or pInfo=”vals”function returns key values separated by prefix string (by default `n)

Examples

Ini_LoadKeys("settings.ini")                    ; load all keys from all sections, return number of vars
Ini_LoadKeys("application.ini", "window") ; load all keys under 'window' section, return number of vars
Ini_LoadKeys("test.ini", "", "", "cfg_") ; load all keys, use "cfg_" prefix
Ini_LoadKeys("test.ini, "Config", 1) ; return key names from "Config" section
Ini_LoadKeys("test.ini, "Config", "keys") ; the same as above
Ini_LoadKeys("test.ini, "Config", "vals") ; return key values from "Config" section
Ini_LoadKeys("", section, "", "cfg_") ; load section from string, use "cfg_" as base name
Ini_LoadKeys("", Presets, 2, "", "name") ; get only key values of keys that start with "name" word from Presets section string.
Ini_LoadKeys("", Presets, 2, "", "name", true) ; the same as above, but reverse filter.
Ini_LoadKeys("", Presets, 2, "", "`ni)^k") ; regular expression filter - get all keys starting with character "k" or "K"

MakeSection

Ini_MakeSection(prefix)

Makes the section string using user global variables

Parameters

prefixCommon prefix that all global variables have in their name.

Returns

Section string

Remarks

If you defined more then around 16,350 variables (in entire script), this function may provide unreliable results.

Example

a_key1          = my key
a_SomeOtherKey = 1
a_configuration = bla bla
somevar = dumy

s := Ini_MakeSection("a_")
Ini_ReplaceSection("test.ini", "config", s)

ReplaceSection

Ini_ReplaceSection( pIniFile,  
 section,  
ByRef data = "")

Replace entire section in ini file.

Parameters

pIniFileIni file name
sectionSection name
dataReference to the section data

UpdateSection

Ini_UpdateSection(ByRef sSection,
ByRef data)

Update section with new key values.

Parameters

sSectionReference to the section string
dataReference to the new section data

Returns

Updated section string

GetVal

Ini_GetVal(ByRef sSection,  
 name,  
 def = "")

Get key value

Parameters

sSectionReference to the section string.
namename of the key
defdefault value

Returns

Key value if key is found or def

SetVal

Ini_SetVal(ByRef sSection,  
 name,  
 val = "")

Set the key value

Parameters

sSectionReference to the section string.
nameName of the key
valValue.  New line char (“`n”) is not removed from the value.

GetKeyName

Ini_GetKeyName(ByRef sSection,
 val)

Get name of the first key with given value

Parameters

sSectionReference to the section string.
valKey value

Returns

Key name if value is found or empty string

DelKeys

Ini_DelKeys(ByRef sSection,  
 keys,  
 rev = false,
 sep = ",")

Delete one or more keys from the section

Parameters

sSectionReference to the section string
keysList of keys to be deleted, separated by sep character
revReverse mode - keys in the list stay, all other keys are deleted
sepKey separator, by default “,”

Returns

New section string

DelKeysRe

Ini_DelKeysRe(ByRef sSection,
 re)

Delete keys from the section using regular expression

Parameters

sSectionReference to the section string
reRegular expression specifying key names.  You can use any valid AHK RE, including options and anchors.

Returns

New section string

Example

s=
(
Evocation=1
levitation=2
somekey=blah
)

Ini_DelKeysRe( s, "i)^ev") ;remove only "Evocation" key

AddMRU

Ini_AddMRU(ByRef sSection,  
 pLine,  
 pMax = 10,
 prefix = "m")

Add most recently used (MRU) item in the section string

Parameters

sSectionSection string
pLineLine to be added
pMaxMaximum lines in the MRU list, by default 10
prefixPrefix to use for key name generation.  By default “m”

Returns

If “line” was already in the MRU list, its previous MRU number, otherwise 1.

Remarks

This will create array of keys, by default m1, m2... mN.  Each key represents one MRU line.  “Line” will be added to the top of the MRU list.  If it is already in the list, it will first be moved to top and its old position will be returned.

About

Ini_LoadSection(pIniFile,  
pSection = "",
pPrefix = "inis_")
Load one or all sections from the ini file.
Ini_GetSectionNames(pIniFile)
Gets the names of all sections in the ini file.
Ini_LoadKeys(pIniFile,  
section =  "",
pInfo = 0,
prefix =  "",
filter = "",
reverse =  false)
Loads one or all keys from one or all sections.
Ini_MakeSection(prefix)
Makes the section string using user global variables
Ini_ReplaceSection( pIniFile,  
 section,  
ByRef data = "")
Replace entire section in ini file.
Ini_UpdateSection(ByRef sSection,
ByRef data)
Update section with new key values.
Ini_GetVal(ByRef sSection,  
 name,  
 def = "")
Get key value
Ini_SetVal(ByRef sSection,  
 name,  
 val = "")
Set the key value
Ini_GetKeyName(ByRef sSection,
 val)
Get name of the first key with given value
Ini_DelKeys(ByRef sSection,  
 keys,  
 rev = false,
 sep = ",")
Delete one or more keys from the section
Ini_DelKeysRe(ByRef sSection,
 re)
Delete keys from the section using regular expression
Ini_AddMRU(ByRef sSection,  
 pLine,  
 pMax = 10,
 prefix = "m")
Add most recently used (MRU) item in the section string