This UDF contains functions that help managing the configuration of your scripts.
You can use either an *.ini file, or registry keys, the UDF manage for you all IniRead/IniWrite or RegRead/RegWrite calls.
First, you must call _AutoCfg_Init to setup the system, which method will be used to stor the config (ini/reg), the path (ini file/reg key) and various options (data encryption, ini file section).
After that, you must add the configuration entries that will be used, with their respective default value (use _AutoCfg_AddEntry).
Next, you must call _AutoCfg_Update to update every entry with either it’s corresponding value on the disk (ini/reg) or with it’s default value if no value is present on the disk.
Finally, you can use in your script the function CFG to get the current value of an entry, or an array of all the entries and their values and default values. And the function _AutoCfg_SetEntry the set the value of an entry or set it to it’s default value.
#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> #include <AutoConfig.au3> _AutoCfg_Init($ACFG_Ini, @ScriptDir & "\config.ini", "", "") _AutoCfg_AddEntry("nom", "Jean") ; on spécifie les paramètres _AutoCfg_AddEntry("prenom", "Dupon") ; avec leurs valeur par défaut _AutoCfg_AddEntry("age", "25") _AutoCfg_AddEntry("pass", "secret!") _AutoCfg_AddEntry("email", "rien@test.fr") _AutoCfg_AddEntry("pseudo", "matwachich") _AutoCfg_Update() #Region ### START Koda GUI section ### Form= $Form1 = GUICreate("Test", 272, 230, 444, 203) $Edit = GUICtrlCreateEdit("", 10, 10, 251, 176) GUICtrlSetData(-1, "") $B_go = GUICtrlCreateButton("Action!", 10, 195, 75, 25, $WS_GROUP) $B_cfg = GUICtrlCreateButton("Paramètres", 185, 195, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $B_cfg _cfg() Case $B_go ; Ici, on montre comment récupérer les paramètres dans le programme _cw("Configuration Actuelle") _cw(" - Nom: " & CFG("nom")) _cw(" - Prénom: " & CFG("prenom")) _cw(" - Age: " & CFG("age")) _cw(" - E-mail: " & CFG("email")) _cw(" - Pseudo: " & CFG("pseudo")) _cw(" - Pass: " & CFG("pass")) _cw(" ------------------------------") $a = CFG() _ArrayDisplay($a) EndSwitch WEnd Func _cfg() #Region ### START Koda GUI section ### Form= $Form2 = GUICreate("Paramètres", 257, 220, 378, 206) $Input1 = GUICtrlCreateInput(CFG("nom"), 100, 20, 121, 21) GUICtrlCreateLabel("Nom", 25, 23, 26, 17) $Input2 = GUICtrlCreateInput(CFG("prenom"), 100, 45, 121, 21) GUICtrlCreateLabel("Prenom", 25, 48, 40, 17) $Input3 = GUICtrlCreateInput(CFG("age"), 100, 70, 121, 21) GUICtrlCreateLabel("Age", 25, 73, 23, 17) $Input4 = GUICtrlCreateInput(CFG("email"), 100, 95, 121, 21) GUICtrlCreateLabel("E-mail", 25, 98, 42, 17) $Input5 = GUICtrlCreateInput(CFG("pseudo"), 100, 120, 121, 21) GUICtrlCreateLabel("Pseudo", 25, 123, 40, 17) $Input6 = GUICtrlCreateInput(CFG("pass"), 100, 145, 121, 21) GUICtrlCreateLabel("Mot de passe", 25, 148, 68, 17) $B_ok = GUICtrlCreateButton("Valider", 90, 180, 75, 25, $WS_GROUP) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE GUIDelete($Form2) ExitLoop Case $B_ok ; On enregistre tous les paramètres _AutoCfg_SetEntry("nom", GUICtrlRead($Input1)) _AutoCfg_SetEntry("prenom", GUICtrlRead($Input2)) _AutoCfg_SetEntry("age", GUICtrlRead($Input3)) _AutoCfg_SetEntry("email", GUICtrlRead($Input4)) _AutoCfg_SetEntry("pseudo", GUICtrlRead($Input5)) _AutoCfg_SetEntry("pass", GUICtrlRead($Input6)) GUIDelete($Form2) ExitLoop EndSwitch WEnd EndFunc Func _cw($data) GUICtrlSetData($Edit, "> " & $data & @CRLF, 1) EndFunc
AutoConfig | This UDF contains functions that help managing the configuration of your scripts. |
Functions | |
_AutoCfg_Init | Initiate the AutoConfig System. |
_AutoCfg_AddEntry | Add a configuration parameter (An entry), with it’s default value. |
_AutoCfg_Update | Update all/one entry(ies) with either it’s corresponding value on the disk (ini/reg), or with it’s default value. |
_AutoCfg_SetEntry | Set the value of an entry, or set it to it’s default value. |
CFG | Return the current value of an entry. |
Initiate the AutoConfig System.
_AutoCfg_Init($Type, $Path, $Section = "Auto_Cfg", $Pwd = "")
$Type | Type of data storage ($ACFG_INI for an \*.ini file, or $ACFG_REG for a registery key) |
$Path | Path of the *.ini file, or registery key |
$Section | Section of the *.ini file (ignored if registery key) |
$Pwd | If Empry, no then no data encryption will be used, if not empty, than the stored data will be encrypted using this password (RC4 Encryption) |
Succes | 1 |
Failed | 0 and set @error |
Update all/one entry(ies) with either it’s corresponding value on the disk (ini/reg), or with it’s default value.
_AutoCfg_Update($Name = Default)
$Name | Entry name to update (if default then all entries are updated) |
Succes | 1 |
Failed | 0 And set @error |
Set the value of an entry, or set it to it’s default value.
_AutoCfg_SetEntry($Name, $Value = Default)
$Name | Entry name |
$Value | Value to assign. If Default then the default value is assigned. |
Succes | 1 |
Failed | 0 And set @error |
Return the current value of an entry.
CFG($Name = Default)
$Name | Entry name. If Default then an array of all entries and their respective values and default values is returned (see Returns). |
Succes | Value of the entry or (if $Name = Default) |
$array[0][0] = Number of entries
$array[$i][0] = Entry name
$array[$i][1] = Entry current value
$array[$i][2] = Entry default value
Failed | 0 And set @error |