-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_formats.lua
109 lines (102 loc) · 3.1 KB
/
test_formats.lua
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
local t = require('tools')
local buffer = [[
# main comment 1
option1 = thisvalue # this is a comment
option2 = othervalue
option3 = val
#main comment 2
background = 0xFF00CC #also this one
#haratara ajhad = ahdflkjs adf df
foreground = 0x660033
block_size = 120
welcome_msg = "hello there how are you im fine thank you!"
]]
-- cfg format reader
local function cfg_parse(data)
local entry,err = string.gmatch(data,'(.-)\n')
if not entry and err then error(err) end
local get = entry()
if not get then error('Could not parse data') end
local cfgvalues = {}
repeat
get = string.match(' '..get,'(.+)#') or get
local tag,value = string.match(get,'([%w%p%s]+)=([%w%p%s]+)')
if tag and value then
cfgvalues[string.match(tag,'[%w%p]+')] = string.match(value,'[\"\']([%w%p%s]+)[\"\']') or string.match(value,'[%w%p]+')
end
get = entry()
until not get
return cfgvalues
end
local function cfg_make(data)
local retval = ''
for k,v in pairs(data) do
if string.find(v,'[%s]') then
retval = retval .. k .. ' = "' .. v .. '"\n'
else
retval = retval .. k .. ' = ' .. v .. '\n'
end
end
return retval
end
--print(t.serialize(cfg_parse(buffer)))
buffer = [[
!(cstate_version:2.0)
[statevars.system_sigroutines]
timequantum = 0.5
[statevars.system]
shellparams = "Default 160 50"
shelldir = A:/sys/os/intmgr.lua
path = A:/bin;A:/shared/bin;A:/sys
[users.debug]
homedir = A:/usr/debug
userdata = A:/usr/debug/_!usrdata
path = A:/usr/debug
]]
local function cstate_parse(data)
local entry,err = string.gmatch(data,'(.-)\n')
if not entry and err then error(err) end
local get = entry()
if not get then error('Could not parse data') end
-- version check
local version = string.match(get,'%!%(cstate_version:([%w%p]+)%)')
if version ~= '2.0' then return 'Incompatible version' end get = entry()
local cstatevalues = {}
local root,node = '',''
repeat
local r,n = string.match(get,'%[([%w%p]+)%.([%w%p]+)%]')
if (r and n) then
root,node = r,n
if not cstatevalues[root] then cstatevalues[root] = {} end
if not cstatevalues[root][node] then cstatevalues[root][node] = {} end
else
local tag,value = string.match(get,'([%w%p%s]+)=([%w%p%s]+)')
if tag and value then
cstatevalues[root][node][string.match(tag,'[%w%p]+')] = string.match(value,'[\"\']([%w%p%s]+)[\"\']') or string.match(value,'[%w%p]+')
else
return 'Error in parsing cstate data'
end
end
get = entry()
until not get
return cstatevalues
end
local function cstate_make(data)
local retval = '!(cstate_version:2.0)\n'
for k1,v1 in pairs(data) do
for k2,v2 in pairs(v1) do
retval = retval .. '[' .. k1 .. '.' .. k2 .. ']\n'
for k3,v3 in pairs(v2) do
if string.find(v3,'[%s]') then
retval = retval .. k3 .. ' = "' .. tostring(v3) .. '"\n'
else
retval = retval .. k3 .. ' = ' .. tostring(v3) .. '\n'
end
end
end
end
return retval
end
--print(t.serialize(cstate_parse(buffer)))
print(cstate_make(cstate_parse(buffer)))
print(t.serialize(cstate_parse(cstate_make(cstate_parse(buffer)))))