常见的配置文件通常使用ini文件来存储,读写ini文件的方式也有很多。本文想要实现的是:ini文件的读写由Lua实现,C只负责调用Lua来实现ini文件的读写功能。那么如何在C代码中调用Lua来实现ini文件的读写?
ini.lua
--LoadIniFile
function LoadIniFile(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))th