Reputation: 113
I want make lua script read custom config file
personal.txt
[user4]
id=1234
code=9876
[user3]
id=765
code=fh723
so i can read or write data
Upvotes: 4
Views: 12929
Reputation: 3225
Here's one possibility. (WARNING: Not tested 100% but seems to work.) You can read/write any such file and add, update, or delete entries depending on which parameters are left nil.
--==============================================================================
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
Upvotes: 0
Reputation: 69
You can use lua module to make your config:
-- config.lua
local _M = {}
_M.user3 = {
id = 765,
code = "fh723",
}
_M.user4 = {
id = 1234,
code = "9876",
}
return _M
Then you can require the module, and use the field in module table as you like:
-- 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)
Output:
765
fh723
1234
9876
12345
Upvotes: 6
Reputation: 3611
To do this, you should make the config file a Lua compatible format:
--personal.txt
user4 = {
id=1234,
code=9876,
}
user3 = {
id=765,
code=fh723,
}
Then you can load the file using loadfile
and pass in a custom environment to place the contents into:
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
Of course, there are multiple ways to do this, this is just a simple, and relatively safe way.
Upvotes: 8