lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Sep 29, 2016 at 12:58 AM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> On Thu, Sep 29, 2016 at 7:19 AM, Russell Haley <russ.haley@gmail.com> wrote:
>> About the saving of values: yes, it is important not to molest the
>> original file. I would like to be able to work with conf files in
>> FreeBSD lik rc.conf and leave them intact.
>
> Ah, that suggests a simple solution:

Embarrassing. I couldn't see the forest through the trees (but Seans
solution is very cool. I can't wait to try it). I had tried something
like this with tables many moons ago and discovered something I
re-discovered last night: iterating through a table does not guarantee
the items are in the same order they are created? I ran your code
and... the items are in the same order? This code reads my items back
out of order:

--Create the item
if not value:find(",") then
    if removequotes == true then
        conf[option] = value:gsub("\"", "")
    else
        conf[option] = value
    end
else

...

if debug == true then
    for i, v in pairs(conf) do
        print(i, v)
    end
end
--Complete code is here:
--https://github.com/RussellHaley/LuaWebsocketTesting/blob/master/configuration.lua
-- Interesting that this link is in a directory called "blob" I'm
testing Jetbrains IDEA and using their built in Git functions. Could
be an artifact of the tool?

PIL online seems to indicate the difference is table.insert compared
to the implicit item creation using tablename[key] = new_value ? But I
can't create items using table.insert that use a non-integer key...
hmmm.

#client.conf file from cat:

russellh@prescott:~/IdeaProjects/LuaWebsocketTest% cat client.conf
server_port=8000
server_url = localhost
base_path = /usr/home/russellh/IdeaProjects/LuaWebsocketTest
data_dir_name = data

# Auto-Enabled NICs from pc-sysinstall
ifconfig_re0="DHCP"
ifconfig_ue0="DHCP"
ifconfig_ue1="DHCP"
# Auto-Enabled NICs from pc-sysinstall
ifconfig_re0_ipv6="inet6 accept_rtadv"
ifconfig_ue0_ipv6="inet6 accept_rtadv"
ifconfig_ue1_ipv6="inet6 accept_rtadv"
hostname="prescott.highfell.home"
pcsysconfig_enable="YES"
zfs_enable=YES
sshd_enable="YES"
vboxnet_enable="YES"
iocage_enable="YES"

nfs_server_enable="YES"
nfs_server_flags="-u -t -n 6"
rpcbind_enable="YES"
mountd_flags="-r"
mountd_enable="YES"

inetd_enable="YES"
nfs_client_enable="YES"
nfs_client_flags="-n 4"
rpc_lockd_enable="YES"
rpc_statd_enable="YES"

mongod_enable="YES"
mongod_config="/usr/local/etc/mongodb.conf"
mongod_dbpath="/var/db/mongodb"

#Output from my configuration reader...
russellh@prescott:~/IdeaProjects/LuaWebsocketTest% lua Debugtesting.lua
mountd_flags    "-r"
mongod_dbpath   "/var/db/mongodb"
hostname        "prescott.highfell.home"
rpcbind_enable  "YES"
:conf_file_path client.conf
ifconfig_ue0_ipv6       "inet6 accept_rtadv"
nfs_client_flags        "-n 4"
rpc_statd_enable        "YES"
rpc_lockd_enable        "YES"
iocage_enable   "YES"
inetd_enable    "YES"
server_port     8000
ifconfig_ue0    "DHCP"
nfs_server_enable       "YES"
mongod_enable   "YES"
nfs_client_enable       "YES"
ifconfig_re0_ipv6       "inet6 accept_rtadv"
mountd_enable   "YES"
mongod_config   "/usr/local/etc/mongodb.conf"
base_path       /usr/home/russellh/IdeaProjects/LuaWebsocketTest
ifconfig_re0    "DHCP"
ifconfig_ue1_ipv6       "inet6 accept_rtadv"
ifconfig_ue1    "DHCP"
data_dir_name   data
server_url      localhost
sshd_enable     "YES"
vboxnet_enable  "YES"
zfs_enable      YES
pcsysconfig_enable      "YES"
nfs_server_flags        "-u -t -n 6"

Russ


> (1) read all the lines into an array
> local f, e = io.open(file)
> -- check that e!
> local lines = {}
> for line in f:lines() do
>   table.insert(lines,line)
> end
> f:close()
>
> (2) process only non-blank or lines not starting with #
> local values = {}
> local value_lines = {}
> for i,line in ipairs(lines) do
>   if not (line:match '^%s*$' or line:match '^#') then
>     local var,val = line:match('^([^=]+)=(.+)')
>     values[var] = val
>     value_lines[var] = i
>   end
> end
>
> The values table is what you need.  Updating a var is like so
>
> (3)
> lines[value_lines[var]] = var..'='..new_value
>
> and then write out
> local f,e = io.open(file,'w');
> for i,line in ipairs(lines) do
>    f:write(line,'\n')
> end
> f:close()
>