Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使LUA脚本读取我的配置文件_Lua_Config - Fatal编程技术网

如何使LUA脚本读取我的配置文件

如何使LUA脚本读取我的配置文件,lua,config,Lua,Config,我想让lua脚本读取自定义配置文件 personal.txt [user4] id=1234 code=9876 [user3] id=765 code=fh723 因此,我可以读取或写入数据要执行此操作,您应该将配置文件设置为Lua兼容格式: --personal.txt user4 = { id=1234, code=9876, } user3 = { id=765, code=fh723, } 然后,您可以使用加载文件并在自定义环境中传递,以将内容放入: loc

我想让lua脚本读取自定义配置文件

personal.txt

[user4]
id=1234
code=9876

[user3]
id=765
code=fh723

因此,我可以读取或写入数据要执行此操作,您应该将配置文件设置为Lua兼容格式:

--personal.txt

user4 = {
  id=1234,
  code=9876,
}

user3 = {
  id=765,
  code=fh723,
}
然后,您可以使用加载文件并在自定义环境中传递,以将内容放入:

local configEnv = {} -- to keep it separate from the global env
local f,err = loadfile("personal.txt", "t", configEnv)
if f then
   f() -- run the chunk
   -- now configEnv should contain your data
   print(configEnv.user4) -- table
else
   print(err)
end

当然,有多种方法可以做到这一点,这只是一种简单且相对安全的方法。

您可以使用lua模块进行配置:

-- config.lua

local _M = {}

_M.user3 = {
    id = 765,
    code = "fh723",
}

_M.user4 = {
    id = 1234,
    code = "9876",
}

return _M
然后,您可以选择模块,并根据需要使用模块表中的字段:

-- main.lua

local config = require "config"

print (config.user3.id)
print (config.user3.code)

print (config.user4.id)
print (config.user4.code)

-- Also you can edit the module table
config.user4.id = 12345
print (config.user4.id)
输出:

765
fh723
1234
9876
12345

这里有一种可能性。(警告:未进行100%测试,但似乎有效。) 您可以读取/写入任何此类文件,并根据哪些参数保留为零来添加、更新或删除条目

--==============================================================================

local
function file_exists(path)
  local f = io.open(path)
  if f == nil then return end
  f:close()
  return path
end

--------------------------------------------------------------------------------
-- Read the whole configuration in a table such that each section is a key to
-- key/value pair table containing the corresponding pairs from the file.

function read_config(filename)
  filename = filename or ''
  assert(type(filename) == 'string')
  local ans,u,k,v,temp = {}
  if not file_exists(filename) then return ans end
  for line in io.lines(filename) do
    temp = line:match('^%[(.+)%]$')
    if temp ~= nil and u ~= temp then u = temp end
    k,v = line:match('^([^=]+)=(.+)$')
    if u ~= nil then
      ans[u] = ans[u] or {}
      if k ~= nil then
        ans[u][k] = v
      end
    end
  end
  return ans
end

--------------------------------------------------------------------------------
-- When all three parametes are nil, no action at all.
-- When both key and value are nil but section is not, delete section.
-- When only value is nil, delete key value pair for given section.

function write_config(filename,section,key,value)
  filename = filename or ''
  assert(type(filename) == 'string')
  if section == nil and key == nil and value == nil then return end
  local t = read_config(filename)       -- read existing configuration, if any

  if section ~= nil and value == nil then
    if key == nil then
      t[section] = nil                  --eliminate whole section
    else
      t[section][key] = nil             --eliminate key/value pair
    end
    goto WriteFile
  end

  if key:match '=' then
    error('An equals sign is not expected inside key')
  end

  t[section] = t[section] or {}         --create section if not present
  t[section][key] = value               --update key value

::WriteFile::                           -- write to file
  local fo = io.open(filename,'w')
  for k,v in pairs(t) do
    fo:write('['..k..']\n')
    for k,v in pairs(v) do
      fo:write(k..'='..v..'\n')
    end
    fo:write('\n')
  end
  fo:close()

  return t                              --return updated configuration table
end

--==============================================================================

--------------------------------------------------------------------------------
-- Example use
--------------------------------------------------------------------------------

f = 'personal.txt'                                --file to use

write_config(f,'user2','id','my_id')              --update key value pair
write_config(f,'user2','name','noone')            --add key value pair
write_config(f,'user3','id','818')                --update key value pair
write_config(f,'user3','xxx','whatever')          --add key value pair
write_config(f,'newuser','id','54321')            --create new user
write_config(f,'newuser','xxx','54321')           --create new key/value pair
write_config(f,'newuser','xxx')                   --remove key/value pair
write_config(f,'newuser')                         --remove section

如何在personal.txt中写入、删除和替换?保存表比加载表更复杂。请查看如何添加、删除或替换用户?我需要在config.lua中填充大数据使用module和require而不是loadfile的缺点是require将值缓存在package.loaded表中。因此,如果config.lua发生更改,它将不会反映在对require的新调用(“config”)中。您可以将模块表编辑为lua普通表,请参阅一些。我在尝试加载config.lua*****1481970356.657114 lua:/home/user/app/main时出错。lua:115:未找到模块“config”:*