-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_to_csv.lua
69 lines (59 loc) · 1.44 KB
/
convert_to_csv.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
options = require("options")
-- Convert from table to CSV string
function toCSV (tt)
local s = ""
-- ChM 23.02.2014: changed pairs to ipairs
-- assumption is that fromCSV and toCSV maintain data as ordered array
for _,p in ipairs(tt) do
s = s .. "," .. p
end
return string.sub(s, 2) -- remove first comma
end
-- _G[varname]
--
fields = { "full_name", "type", "short_desc", "scope"}
-- print(toCSV(options.options))
header = ""
-- Used to escape "'s by toCSV
function escapeCSV (s)
if string.find(s, '[,"]') then
s = '"' .. string.gsub(s, '"', '""') .. '"'
end
return s
end
-- Convert from table to CSV string
function toCSV (tt)
local s = ""
-- ChM 23.02.2014: changed pairs to ipairs
-- assumption is that fromCSV and toCSV maintain data as ordered array
for _,p in ipairs(tt) do
s = s .. "," .. escapeCSV(p)
end
return string.sub(s, 2) -- remove first comma
end
-- dumpToCsv()
-- for _, name in ipairs(fields) do
-- header = header..","..name
-- end
-- header= header:sub(2, #header)
header = toCSV(fields)
print(header)
function dumpTable(opt)
local line = ""
for _, name in ipairs(fields) do
local append = ""
if name == "scope" then
-- print("SCOPE")
append = toCSV(opt[name])
else
append = opt[name] or ""
end
-- print(opt[name])
line = line..","..append
end
return line:sub(2,#line)
end
for i,opt in ipairs(options.options) do
-- s = s .. "," .. p
print(dumpTable(opt))
end