LUA读写ini配置
--读全部 function load(fileName) assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.'); local file = assert(io.open(fileName, 'r'), 'Error loading file : ' .. fileName); local data = {}; local section; for line in file:lines() do local tempSection = line:match('^%[([^%[%]]+)%]$'); if(tempSection)then section = tonumber(tempSection) and tonumber(tempSection) or tempSection; data[section] = data[section] or {}; end local param, value = line:match('^([%w|_]+)%s-=%s-(.+)$'); if(param and value ~= nil)then if(tonumber(value))then value = tonumber(value); elseif(value == 'true')then value = true; elseif(value == 'false')then value = false; end if(tonumber(param))then param = tonumber(param); end data[section][param] = value; end end file:close(); return data; end --写全部 function save(fileName, data) assert(type(fileName) == 'string', 'Parameter "fileName" must be a string.'); assert(type(data) == 'table', 'Parameter "data" must be a table.'); local file = assert(io.open(fileName, 'w+b'), 'Error loading file :' .. fileName); local contents = ''; for section, param in pairs(data) do contents = contents .. ('[%s]\n'):format(section); for key, value in pairs(param) do contents = contents .. ('%s=%s\n'):format(key, tostring(value)); end contents = contents .. '\n'; end file:write(contents); file:close(); end --读单条 function ReadIni(IniPath,Section,Key) local data=load(IniPath) return data[Section][Key] end --写单条 function WriteIni(IniPath,Section,Key,Value) local data=load(IniPath) data[Section][Key]=Value save(IniPath, data) end
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步