ini和数组互操作函数及示例

FileSelectFile, file, , , 请选择ini文件, ini配置文件(*.ini) ;选择文件
if file=
	{
	MsgBox, 0, 错误, 您为选择任何文件
	Reload
	}
show_obj(newobj := ini2obj(file))
MsgBox, 64, 写入ini提示, % obj2ini(newobj,"test.ini")



ini2obj(file){
iniobj := {}
FileRead, filecontent, %file% ;加载文件到变量
StringReplace, filecontent, filecontent, `r, , All
StringSplit, line, filecontent, `n, , ;用函数分割变量为伪数组
Loop ;循环
{
if A_Index > %line0%
	Break
content = % line%A_Index% ;赋值当前行
FSection := RegExMatch(content, "[.*]") ;正则表达式匹配section
if FSection = 1 ;如果找到
	{
	TSection := RegExReplace(content, "[(.*)]", "$1") ;正则替换并赋值临时section $为向后引用
	iniobj[TSection] := {}
	}
Else
	{
	FKey := RegExMatch(content, "^.*=.*") ;正则表达式匹配key
	if FKey = 1
		{
		TKey := RegExReplace(content, "^(.*)=.*", "$1") ;正则替换并赋值临时key
		StringReplace, TKey, TKey, ., _, All
		TValue := RegExReplace(content, "^.*=(.*)", "$1") ;正则替换并赋值临时value
		iniobj[TSection][TKey] := TValue
		}
	}
}
Return iniobj
}

obj2ini(obj,file){
if (!isobject(obj) or !file)
	Return 0
for k,v in obj
{
	for key,value in v
	{
		IniWrite, %value%, %file%, %k%, %key%
	}
}
Return 1
}

show_obj(obj,menu_name:=""){
if menu_name =
    {
    main = 1
    Random, rand, 100000000, 999999999
    menu_name = %A_Now%%rand%
    }
Menu, % menu_name, add,
Menu, % menu_name, DeleteAll
for k,v in obj
{
if (IsObject(v))
	{
    Random, rand, 100000000, 999999999
	submenu_name = %A_Now%%rand%
    Menu, % submenu_name, add,
    Menu, % submenu_name, DeleteAll
	Menu, % menu_name, add, % k ? "【" k "】[obj]" : "", :%submenu_name%
    show_obj(v,submenu_name)
	}
Else
	{
	Menu, % menu_name, add, % k ? "【" k "】" v: "", MenuHandler
	}
}
if main = 1
    menu,% menu_name, show
}

MenuHandler:
return

给TA捐赠
共{{data.count}}人
人已捐赠
其他教程

IME2_AHK输入法切换的3个方案介绍

2020-3-30 23:04:12

其他案例

kcleaner_auto金山自动清理

2020-3-31 19:42:55

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索