• Lua 如何读写ini文件


    常见的配置文件通常使用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))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
    
    --SaveIniFile
    function SaveIniFile(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);
        if file then
            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();
            return true
        else
            return false
        end
    end
    
    --ReadIni
    function ReadIni(IniPath, Section, Key)
        local data = LoadIniFile(IniPath)
        return data[Section][Key]
    end
    
    --WriteIni
    function WriteIni(IniPath, Section, Key, Value)
        local data = LoadIniFile(IniPath)
        data[Section][Key] = Value
        save(IniPath, data)
    end
    
    --初始化Ini文件
    function InitFile()
        local config = {
            ["section1"] = {
                key1 = "val1",
                key2 = "val2"
            },
            ["section2"] = {
                key3 = "val3",
                key4 = "val4"
            }
        }
        if SaveIniFile("./config.ini", config) then
            print("write ini success")
        else
            print("write ini failed")
        end
    end
    
    InitFile()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87

    C++调用ini.lua实现配置文件读写

    #include 
    #include 
    extern "C"{
    #include "lua.h"
    #include "lauxlib.h"
    #include "lualib.h"
    }
    
    lua_State *L;
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        printf("started\r\n");
    
        L =  luaL_newstate();//新建lua解释器
        luaL_openlibs(L);//载入lua基础库
    
    #if 1
        /* 通过lua读写ini文件 */
        luaL_dofile(L, "lua/ini.lua");
    
        /* 执行Lua脚本中的函数 */
        lua_getglobal(L, "ReadIni");
        lua_pushstring(L, "./config.ini");
        lua_pushstring(L, "section1");
        lua_pushstring(L, "key1");
        //
        lua_call(L, 3, 1);
        auto result = lua_tostring(L,-1);//-1表示最后一个返回值,因为lua的函数可以返回多个值
        printf("result=%s\n", result);
        //
        lua_pop(L, 1);
    #endif
    
        /*cleanup Lua*/
        lua_close(L);
    
        system("pause");
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    Linux网络命令使用简单说明
    一个新工具 nolyfill
    从零到一建设数据中台 - 数据可视化
    vue2给对象新增、删除属性,界面不刷新 。通过数组下标修改数组界面不会刷新
    判断两个二叉树是否相等
    pytorch collate_fn测试用例
    Linux 网络之netlink 简介
    浏览器消息通知代码
    LINUX漏洞复现篇之ShellShock漏洞
    C语言字符串查找函数和错误信息报告函数(strstr、strtok,strerror)
  • 原文地址:https://blog.csdn.net/hellokandy/article/details/136688463