Repository navigation
Expand file tree
/
Copy pathcsvfile.lua
More file actions
431 lines (306 loc) · 9.83 KB
/
Copy pathcsvfile.lua
File metadata and controls
431 lines (306 loc) · 9.83 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
--[[#######################################################################
## csvfile.lua - Read/Write/Update csv format file ##
## ##
## Author: BeGab ##
## ##
## URL : https://github.com/Be-Gab/BattUse ##
## ##
## Copyright (C) "BeGab" ##
## ##
###########################################################################
## License GNU General Public License v3.0 ##
#########################################################################]]
local app_ver = "0.6"
local fileRW = {}
local string_gsub = string.gsub
local string_gmatch = string.gmatch
local _tostring = tostring
local fileName -- readed csv filename (full path)
local fileLines = {} -- readed csv file records
local LF = "\n" -- LineFeed
local fieldNames = {} -- fieldnames in indexed table
local fieldPos = {} -- fieldnames in associated table
local fieldType = {} -- field types, string or numeric
local NUMERIC = 1
local STRING = 2
--#########################################################################--
function fileRW.version()
return app_ver
end
function fileRW.log(s)
-- return;
print("BattUse: fileRW : " .. s)
end
-- 0 - numeric, 1 - string
function fileRW.setFieldDataType( fieldPs, fieldTy )
-- fileRW.log("Ps, Ty : " .. fieldPs .. ", " .. fieldTy )
fieldType[ fieldPs ] = fieldTy
end
function fileRW.readCsv(readFileName)
local itemID = 0
local buffer
fileName = readFileName
-- fileRW.log( fileName )
-- Ha nem létezik a file
if fstat( fileName ) == nil then
print("fileRW.readCSV Error: File Open Error! File not exist? (" .. fileName .. ")" )
return nil
end
-- fileRW.log( "fileName:" ..fileName )
local file = io.open(fileName, "r")
if file == nil then
fileRW.log("Hiba a fájl megnyitásakor : " .. fileName )
return nil
end
buffer = io.read(file, 2048 * 32)
-- Process lines
fileLines = {}
for line in string.gmatch(buffer, "(.-)\n") do
-- Read Heading
if itemID == 0 then
local cnt = 1
for val in string_gmatch( line.."," , "([^,]*)," ) do
fieldNames[cnt] = val
fieldPos[ val ] = cnt
-- fileRW.log( "Head: " .. cnt .. " : " .. val .. " => " .. fieldNames[cnt] .. " #: " .. #fieldNames )
cnt = cnt + 1
end
itemID = 1
else
-- Read DataLines
-- fileRW.log( "itemId: " .. itemID )
local cnt = 1
local fields = {}
for v in string.gmatch( line.."," , "([^,]*)," ) do
val = string.gsub( v , '^["]', '')
val = string.gsub( val , '["]$', '')
-- fileRW.log( "readCsv :: Line/field A: " .. itemID .. "/" .. cnt .. ":" .. v .. "<>" .. val )
if itemID == 1 and fieldType[ cnt ] == nil then
if val ~= v then
fieldType[ cnt ] = STRING
else
fieldType[ cnt ] = NUMERIC
end
end
-- if val == nil then
-- print( "fileRW.readCsv: Not numeric! (field:" , fieldNames[cnt] , " value: [" , v , "] )" )
-- end
if fieldType[ cnt ] == NUMERIC then
val = tonumber( val )
end
fields[ fieldNames[cnt] ] = val
-- fileRW.log( "Line/field B: " .. itemID .. "/" .. cnt .. " : " .. fieldNames[cnt] .. " => " .. tostring( val ) .. "/" .. v )
cnt = cnt + 1
end
fileLines[ itemID ] = fields
itemID = itemID + 1
end
end
io.close(file)
return fileLines
end
-- write the [saveFileName] csv file. Have to define with full path
-- if missing, overwrite the readed file
function fileRW.writeCsv( saveFileName )
local file, saveFN
local tableData
if saveFileName == nil then
saveFN = fileName
else
saveFN = saveFileName
end
-- fileRW.log( "WriteCSV : Before - txtTableData" )
-- Create before write, if error occure, the file is unhurt
tableData = fileRW.txtTableData()
file = io.open(saveFN, "w")
-- fileRW.log( "WriteCSV : " .. saveFN )
if not file then
print("fileRW.writeCSV Error: File Open Error! (" .. saveFN .. ")" )
return false
end
io.write( file, tableData )
io.close( file )
return true
end
-- get copy(!) of the full csv file
function fileRW.getTable()
local retTbl = {}
local rowTable = {}
for key , row in ipairs(fileLines) do
-- fileRW.log( "GetTable sor k:" .. key .. " row : " .. #row )
rowTable = {}
-- getFields order by heads order :: KIvenni a mezők értékeit a fejléc sorrendjében
for k , v in ipairs(fieldNames) do
rowTable[v] = row[ v ]
-- fileRW.log( row[ v ] .. " k:" .. k .. " #rowT:" .. #rowTable )
end
retTbl[ key ] = rowTable;
-- txtData = txtData .. table.concat( rowTable , ";" ) .. LF
-- fileRW.log( txtData )
end
return retTbl
end
-- get record id (record number) by field name and value
function fileRW.getRowIdByField( fieldName, value )
local rowID
local lExists , fieldPos = fileRW.isFieldExists( fieldName )
if not lExists then
return nil
end
for key , row in ipairs(fileLines) do
if row[ fieldName ] == value then
return key
end
end
return nil
end
-- get record copy(!), in an associated table
function fileRW.getRow( rowID )
local tRow = {}
local ertek
if fileLines[ rowID ] == nil then
print("fileRW.getROW Error: ID not exist! (" .. rowID .. ")" )
return false
end
-- KIvenni a mezők értékeit a fejléc sorrendjében
-- get fields order by original head order
for k , v in ipairs(fieldNames) do
tRow[ v ] = fileLines[ rowID ][ v ]
end
return tRow
end
-- update full record with fields in [newRow]
function fileRW.updateRow( rowID , newRow )
if fileLines[ rowID ] == nil then
print("fileRW.updateROW Error: ID not exist! (" .. rowID .. ")" )
return false
else
-- csak a newRow ban érkezett mezőket frissíteni, ha van olyan mező...
for k , v in pairs(newRow) do
fileRW.setField( rowID, k, v )
end
end
return true
end
-- Csak azok a mezők kerülnek be, amik szerepelnek a fejlécben
-- create new record filled with fields in [newRow] (can't create new field )
function fileRW.addRow( newRow )
local newRowID = #fileLines + 1
fileLines[ newRowID ] = {}
-- Kivenni a mezőket az új sorból
for k , v in pairs( newRow ) do
-- Ellenőrizni van e ilyen mező név
if fileRW.isFieldExists( k ) == nil then
print("fileRW.addROW Error: field not exist! (" .. k .. ")" )
else
fileLines[ newRowID ][ k ] = v
end
end
return true
end
-- delete the record, result the deleted record.
function fileRW.delRow( rowID )
if fileLines[ rowID ] == nil then
print( "fileRW.delRow::Record Not Exist! (" .. rowID .. ")" )
return false
else
return table.remove( fileLines, rowID );
end
end
-- delete all record without heading
function fileRW.clearData()
fileLines = {}
end
-- get fields value of the specified record in [rowID]
function fileRW.getField( rowID, fieldName )
if fileLines[ rowID ] == nil then
fileRW.log( "getField::Record Not Exist :" .. rowID .. "/" .. fieldName )
return false
elseif fileRW.isFieldExists( fieldName ) then
return fileLines[ rowID ][fieldName]
else
fileRW.log( "getField : Field Not Exist :" .. rowID .. "/" .. fieldName )
return false
end
end
-- set field with value of the specified record in [rowID]
function fileRW.setField( rowID, fieldName, value )
if fileLines[ rowID ] == nil then
fileRW.log( "setField::Record Not Exist :" .. rowID .. "/" .. fieldName )
return false
elseif fileRW.isFieldExists( fieldName ) then
fileLines[ rowID ][ fieldName ] = value
return true
else
fileRW.log( "setField::Field Not Exist :" .. rowID .. "/" .. fieldName )
return false
end
end
-- checking field exist?
function fileRW.isFieldExists( fieldName )
if fieldPos[ fieldName ] == nil then
for k,v in pairs( fieldPos ) do
if fieldName ~= k then
if fieldName == v then
stat = "=="
else
stat = " Not =="
end
fileRW.log( "isFieldExists() NotExists: " .. v .. "=" .. k .. "?=" .. fieldName .. ":" .. stat )
end
end
return false, 0
else
return true, fieldPos[ fieldName ]
end
end
-- return the full csv file in one string
function fileRW.txtTableData()
local txtData
local rowVal
-- Head
-- fileRW.log( table.concat(fieldNames, ";") )
txtData = table.concat(fieldNames, ",") .. LF
-- Data
for key , row in ipairs(fileLines) do
-- fileRW.log( " sor k:" .. key .. " row : " .. #row )
-- fileRW.log( "fileRW.txtTableData, Rec:" .. key .. " #row : " .. #row )
local rowTable = {}
-- KIvenni a mezők értékeit a fejléc sorrendjében
for k , v in ipairs(fieldNames) do
-- fileRW.log( "fileRW.txtTableData, Fields k:" .. k .. " row : " .. v )
if v == nil or row[ v ] == nil then -- Field exists but do not have value
rowVal = ""
else
rowVal = row[ v ]
end
-- fileRW.log( "rowVal:" .. rowVal )
if fieldType[ k ] == STRING then
rowVal = "\"" .. rowVal .. "\""
else
rowVal = tonumber( rowVal )
if rowVal == nil then
print( "fileRW.txtTableData: Not numeric! (" , k , "/" , v , ")" )
-- Not numeric!
rowVal = ""
end
end
rowTable[k] = rowVal
-- jó ::fileRW.log( row[ v ] .. " k:" .. k .. " #rowT:" .. #rowTable )
end
txtData = txtData .. table.concat( rowTable , "," ) .. LF
end
return txtData
end
-- print full csv file to debug
function fileRW.logTableData()
local slines, line
local cnt = 0
-- The Debug window unable to process linefeeds
slines = fileRW.txtTableData()
for line in string.gmatch(slines, "([^\n]+)\n") do
print( "fileRW :: [Line " .. cnt .. "]" .. line )
cnt = cnt + 1
end
end
return fileRW