Last active
September 20, 2016 01:44
-
-
Save tmplinshi/71301f2655a20750ae73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ============================================================================== | |
; 功能: 读取 ini 文件的 [Settings] 区段,脚本退出时自动保存 | |
; (ini 文件名与脚本文件同名) | |
; ============================================================================== | |
/* | |
; 示例: | |
ReadSettings( { x : "" | |
, y : "" | |
, key3: "默认值" } ) | |
Gui, Add, Edit, w300 vx , % x | |
Gui, Add, Edit, wp vy , % y | |
Gui, Add, Edit, wp vkey3, % key3 | |
Gui, Show | |
Return | |
GuiClose: | |
ExitApp | |
*/ | |
; ============================================================================== | |
ReadSettings(Obj_Default := "", IniFileExt := ".ini", SaveNow := false) { | |
global | |
local data, pos, k, v, m, m1, m2 | |
static obj_keyList := {} ; 只用来记录键名,不记录值 | |
static iniFile | |
If !iniFile | |
iniFile := SubStr(A_ScriptFullPath, 1, -4) . IniFileExt | |
; 脚本退出时自动保存 ini | |
static _ := { base: {__Delete: "ReadSettings"} } | |
If !_ || SaveNow { | |
Gui, Submit, NoHide | |
For k in obj_keyList { | |
data .= k "=" %k% "`r`n" | |
} | |
If data { | |
IniWrite, % data, % iniFile, Settings | |
} | |
Return | |
} | |
; 载入默认设置 | |
For k, v in Obj_Default { | |
%k% := v | |
obj_keyList[k] := "" | |
} | |
; 载入 ini 设置 | |
If FileExist(iniFile) { | |
IniRead, data, % iniFile, Settings | |
Loop, Parse, data, `n, `r | |
{ | |
if RegExMatch(A_LoopField, "(.*?)=(.*)", m) { | |
obj_keyList[m1] := "" | |
%m1% := m2 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment