IniSet 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 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| iniFile | Ini file path | | section | Section 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. | | prefix | Base name of the array to be used for global variables, by default “inis_” |
ReturnsIf 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. ParametersReturnsList 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| pIniFile | Ini file path. If you set “” here, next parameter will be seen as section string. | | section | By 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% | | prefix | Prefix for global variables created, or key/value delimiter when pInfo != 0 | | pInfo | Flag determining what to return. See return values of the function for details. | | filter | Only 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” | | reverse | Reverse 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) |
ExamplesIni_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"
MakeSectionMakes the section string using user global variables Parameters| prefix | Common prefix that all global variables have in their name. |
ReturnsSection string RemarksIf you defined more then around 16,350 variables (in entire script), this function may provide unreliable results. Examplea_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| pIniFile | Ini file name | | section | Section name | | data | Reference to the section data |
UpdateSection| Ini_UpdateSection( | ByRef | sSection, | | ByRef | data | ) |
|
Update section with new key values. Parameters| sSection | Reference to the section string | | data | Reference to the new section data |
ReturnsUpdated section string
GetVal| Ini_GetVal( | ByRef | sSection, | | | | | name, | | | | | def | = | "" | ) |
|
Get key value Parameters| sSection | Reference to the section string. | | name | name of the key | | def | default value |
ReturnsKey value if key is found or def
SetVal| Ini_SetVal( | ByRef | sSection, | | | | | name, | | | | | val | = | "" | ) |
|
Set the key value Parameters| sSection | Reference to the section string. | | name | Name of the key | | val | Value. 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| sSection | Reference to the section string. | | val | Key value |
ReturnsKey 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| sSection | Reference to the section string | | keys | List of keys to be deleted, separated by sep character | | rev | Reverse mode - keys in the list stay, all other keys are deleted | | sep | Key separator, by default “,” |
ReturnsNew section string
DelKeysRe| Ini_DelKeysRe( | ByRef | sSection, | | | re | ) |
|
Delete keys from the section using regular expression Parameters| sSection | Reference to the section string | | re | Regular expression specifying key names. You can use any valid AHK RE, including options and anchors. |
ReturnsNew section string Examples= ( 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| sSection | Section string | | pLine | Line to be added | | pMax | Maximum lines in the MRU list, by default 10 | | prefix | Prefix to use for key name generation. By default “m” |
ReturnsIf “line” was already in the MRU list, its previous MRU number, otherwise 1. RemarksThis 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.
|