[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Reading a conf file
- From: Russell Haley <russ.haley@...>
- Date: Wed, 28 Sep 2016 23:09:30 -0700
Hi Valentin,
I was unable to make this work at all. I kept getting "attempt to call
a nil value (global 'loadstring')" which PIL online tells me is normal
for a syntax error. I used assert but didn't get any extra
information.
conf file contents:
server_port=8000
server_url = localost
base_path = confFilePath
data_dir_name = data
In your parsing, you had left a trailing ',' that I thought was the
problem, I removed it with string.sub, but that did not fix the issue.
Here is the code:
local function loadConfFile (fn)
local f = io.open(fn, 'r')
if f==nil then return {} end
local str = f:read('*a')..'\n'
f:close()
local res = 'return {'
for line in str:gmatch("(.-)[\r\n]") do
line = line:gsub('^%s*(.-)%s*$"', '%1') -- trim line
-- ignore empty lines and comments
if line~='' and line:sub(1,1)~='#' then
line = line:gsub("'", "\\'") -- escape all '
line = line:gsub("=%s*", "='", 1)
res = res..line.."',"
end
end
res = res:sub(1, -2)
res = res..'}'
print(res)
-- return loadstring(res)
--return {server_port='8000',server_url ='localost',base_path
='confFilePath',data_dir_name ='data'}
--local t = assert(loadstring(res)())
return loadstring([[{"server_port"="8000","server_url"="localhost"}]])()
end
Note the various debugging attempts at the end. The output is a valid
table because I can manually return the table value from print(res).
hmmm...?
Russ
On Wed, Sep 28, 2016 at 3:55 PM, Valentin <vsl@dasdeck.com> wrote:
> Sean Conner wrote:
> [ snip ]
>
>> You already have a parser available to you---Lua. If the config file is
>> just what you said:
>>
>> a=series
>> of=key
>> value=pairs
>>
>> A small change:
>>
>> a='series'
>> of='key'
>> value='pairs'
>> and_a_number=3
>>
>> and that can be read in as:
>>
>> CONF = {} -- your data will end up here
>> f = loadfile("my-configfile.txt","t",CONF)
>> f()
>>
>> print(CONF.a,CONF.and_a_number)
>
> Based on the same idea, but without requiring to add quotes to the conf
> file, here a simple function for reading such "INI-like" conf files into
> a table. All table values will be strings, and have to be casted
> manually to other types if needed.
>
> function loadConfFile (fn)
> local f = io.open(fn, 'r')
> if f==nil then return {} end
> local str = f:read('*a')..'\n'
> f:close()
> local res = 'return {'
> for line in str:gmatch("(.-)[\r\n]") do
> line = line:gsub('^%s*(.-)%s*$"', '%1') -- trim line
> -- ignore empty lines and comments
> if line~='' and line:sub(1,1)~='#' then
> line = line:gsub("'", "\\'") -- escape all '
> line = line:gsub("=%s*", "='", 1)
> res = res..line.."',"
> end
> end
> res = res..'}'
> return loadstring(res)()
> end
>
>
> Usage:
> ======
>
> -- contents of test.conf
> a = 23
>
> b=hello 'world'
>
> # just a comment
> c = hello world
>
> -- /contents of test.conf
>
> local config = loadConfFile('test.conf')
> for k,v in pairs(conf) do
> print(k, v)
> end
>
> --> a 23
> --> c hello world
> --> b hello 'world'
>
> Valentin
>