Files
3dprinting/LuaLibs/CSVManager.lua
T
SaraP 37dd22aa3e 3dPrinting :
- riorganizzazione
- correzioni nel collegamento e nel riordino dei setti.
2022-06-29 11:34:46 +02:00

76 lines
2.1 KiB
Lua

-- CalcPaths.lua by Egaltech s.r.l. 2022/03/30
-- Calcolo percorsi di lavoro per Stampa 3d
-- Tabella per definizione modulo
local CSVManager = {}
-- Intestazioni
require( 'EgtBase')
EgtOutLog( ' CSVManager started', 1)
-- Dati
local AMD = require( 'AddManData')
---------------------------------------------------------------------
function toCSV (t)
local s = ""
for _,p in pairs(t) do
s = s .. "," .. escapeCSV(p)
end
return string.sub(s, 2) -- remove first comma
end
---------------------------------------------------------------------
function escapeCSV (s)
if string.find(s, '[,"]') then
ns = '"' .. string.gsub(s, '"', '""') .. '"'
end
return s
end
---------------------------------------------------------------------
local function fromCSV(s)
s = s .. ';' -- ending comma
local t = {} -- table to collect fields
local fieldstart = 1
repeat
-- next field is quoted? (start with `"'?)
if string.find(s, '^"', fieldstart) then
local a, c
local i = fieldstart
repeat
-- find closing quote
a, i, c = string.find(s, '"("?)', i+1)
until c ~= '"' -- quote not followed by quote?
if not i then error('unmatched "') end
local f = string.sub(s, fieldstart+1, i-1)
table.insert(t, (string.gsub(f, '""', '"')))
fieldstart = string.find(s, ';', i) + 1
else -- unquoted; find next comma
local nexti = string.find(s, ';', fieldstart)
table.insert(t, string.sub(s, fieldstart, nexti-1))
fieldstart = nexti + 1
end
until fieldstart > string.len(s)
return t
end
---------------------------------------------------------------------
function read_file( path)
local file = io.open( path, "r") -- r read mode and b binary mode
if not file then return nil end
local CSVTable = {}
for line in io.lines( path) do
CSVTable[#CSVTable + 1] = fromCSV( line)
end
file:close()
return CSVTable
end
---------------------------------------------------------------------
return CSVManager