A little function to read all variables from ini file
Posted: 19 Apr 2022, 22:10
Hello this is a function I have been using for years now.
It is very simple, it reads and ini file and it create variables with it in the format of
SECTION_key := value
so for example, if you have an ini like this
The function will create the variables:
SYSTEM_name
SYSTEM_version
SYSTEM_lang
OPTIONS_rootdir
and save the ini values in each of them
This is the function:
Just read the comments for usage and instructions
Edit: Edited to change my global variable CONFIGURATION_FILE to function local _SourcePath
It is very simple, it reads and ini file and it create variables with it in the format of
SECTION_key := value
so for example, if you have an ini like this
Code: Select all
[SYSTEM]
name=Applecation Name
version=1.0.0.4
lang=english
[OPTIONS]
rootdir=C:\PathToApp
SYSTEM_name
SYSTEM_version
SYSTEM_lang
OPTIONS_rootdir
and save the ini values in each of them
This is the function:
Just read the comments for usage and instructions
Code: Select all
; Created by Cristófano Varacolaci
; For ObsessedDesigns Studios™, Inc.
; Version 1.2.00.00
; Build 16:30 2022.04.19
; ==================================================================================================================================
; Function: variablesFromIni
; Description read all variables in an ini and store in variables
; Usage: variablesFromIni(_SourcePath [, _ValueDelim = "=", _VarPrefixDelim = "_"])
; Parameters:
; _SourcePath - path to the ini file ["path\file.ini"]
; _ValueDelim - This is the delimitator used for key 'delim' value function
; _VarPrefixDelim - This specifies the separator between section name and key name.
; All section names and key names are merged into single name.
; Return values:
; Variables from the ini, named after SECTION Delimiter KEY
; Change history:
; 1.2.00.00/2022.04.19
; Updated to use SubStr() ad regExMatch() instead of deprecated functions
; Remarks:
; Change history:
; 1.1.00.00/2022.03.30
; Added _ValueDelim
; corrected an error, now it is working again
; Remarks:
; Change history:
; 1.1.00.00/2021.12.05
; Added _ValueDelim
; Remarks:
variablesFromIni(_SourcePath, _ValueDelim = "=", _VarPrefixDelim = "_")
{
Global
if !FileExist(_SourcePath){
MsgBox, 16, % "Error", % "The file " . _SourcePath . " does not esxist"
} else {
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) = "[")
{
RegExMatch(A_LoopField, "\[(.*)\]", ini_Section)
CurrentPrefix := ini_Section1
}
Else
{
DelimPos := InStr(A_LoopField, _ValueDelim)
CurrentVarName := SubStr(A_LoopField, 1, DelimPos - 1)
CurrentVarContent := SubStr(A_LoopField, DelimPos + 1)
%CurrentPrefix%%_VarPrefixDelim%%CurrentVarName% = %CurrentVarContent%
;---- [Uncomment for variable name and value display]
;MsgBox, , Title, %CurrentPrefix%%_VarPrefixDelim%%CurrentVarName% = %CurrentVarContent%
}
}
}
}
}
}