mirror of
https://github.com/FourierTransformer/ftcsv.git
synced 2024-11-19 19:54:23 +00:00
code cleanup and release version
This commit is contained in:
parent
e34c08b772
commit
db9941aee8
@ -1,9 +1,9 @@
|
|||||||
package = "ftcsv"
|
package = "ftcsv"
|
||||||
version = "1.0.1-1"
|
version = "1.0.2-1"
|
||||||
|
|
||||||
source = {
|
source = {
|
||||||
url = "git://github.com/FourierTransformer/ftcsv.git",
|
url = "git://github.com/FourierTransformer/ftcsv.git",
|
||||||
tag = "1.0.1"
|
tag = "1.0.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
description = {
|
description = {
|
142
ftcsv.lua
142
ftcsv.lua
@ -1,5 +1,5 @@
|
|||||||
local ftcsv = {
|
local ftcsv = {
|
||||||
_VERSION = 'ftcsv 1.0.1',
|
_VERSION = 'ftcsv 1.0.2',
|
||||||
_DESCRIPTION = 'CSV library for Lua',
|
_DESCRIPTION = 'CSV library for Lua',
|
||||||
_URL = 'https://github.com/FourierTransformer/ftcsv',
|
_URL = 'https://github.com/FourierTransformer/ftcsv',
|
||||||
_LICENSE = [[
|
_LICENSE = [[
|
||||||
@ -204,95 +204,91 @@ function ftcsv.parse(inputFile, delimiter, options)
|
|||||||
local currentChar, nextChar = string.byte(inputString, i), nil
|
local currentChar, nextChar = string.byte(inputString, i), nil
|
||||||
|
|
||||||
while i <= inputLength do
|
while i <= inputLength do
|
||||||
-- go by two chars at a time!
|
-- go by two chars at a time! currentChar is set at the bottom.
|
||||||
-- currentChar = string.byte(inputString, i)
|
-- currentChar = string.byte(inputString, i)
|
||||||
nextChar = string.byte(inputString, i+1)
|
nextChar = string.byte(inputString, i+1)
|
||||||
-- print(i, string.char(currentChar), string.char(nextChar))
|
-- print(i, string.char(currentChar), string.char(nextChar))
|
||||||
|
|
||||||
-- keeps track of characters to "skip" while going through the encoding process
|
-- empty string
|
||||||
-- if skipChar == 0 then
|
if currentChar == quote and nextChar == quote then
|
||||||
|
-- print("EMPTY STRING")
|
||||||
|
skipChar = 1
|
||||||
|
fieldStart = i + 2
|
||||||
|
-- print("fs+2:", fieldStart)
|
||||||
|
|
||||||
-- empty string
|
-- identifies the escape toggle
|
||||||
if currentChar == quote and nextChar == quote then
|
elseif currentChar == quote and nextChar ~= quote then
|
||||||
-- print("EMPTY STRING")
|
-- print("ESCAPE TOGGLE")
|
||||||
skipChar = 1
|
fieldStart = i + 1
|
||||||
fieldStart = i + 2
|
i, doubleQuoteEscape = M.findClosingQuote(i+1, inputLength, inputString, quote, doubleQuoteEscape)
|
||||||
-- print("fs+2:", fieldStart)
|
-- print("I VALUE", i, doubleQuoteEscape)
|
||||||
|
skipChar = 1
|
||||||
|
|
||||||
-- identifies the escape toggle
|
-- create some fields if we can!
|
||||||
elseif currentChar == quote and nextChar ~= quote then
|
elseif currentChar == delimiterByte then
|
||||||
-- print("ESCAPE TOGGLE")
|
-- for that first field
|
||||||
fieldStart = i + 1
|
if not headerSet and lineNum == 1 then
|
||||||
i, doubleQuoteEscape = M.findClosingQuote(i+1, inputLength, inputString, quote, doubleQuoteEscape)
|
headerField[fieldNum] = fieldNum
|
||||||
-- print("I VALUE", i, doubleQuoteEscape)
|
end
|
||||||
skipChar = 1
|
-- create the new field
|
||||||
-- end
|
-- print(headerField[fieldNum])
|
||||||
|
doubleQuoteEscape = createNewField(inputString, quote, fieldStart, i, outResults[lineNum], headerField[fieldNum], doubleQuoteEscape, fieldsToKeep)
|
||||||
|
|
||||||
-- create some fields if we can!
|
fieldNum = fieldNum + 1
|
||||||
elseif currentChar == delimiterByte then
|
fieldStart = i + 1
|
||||||
-- for that first field
|
-- print("fs+1:", fieldStart)
|
||||||
if not headerSet and lineNum == 1 then
|
-- end
|
||||||
headerField[fieldNum] = fieldNum
|
|
||||||
end
|
|
||||||
-- create the new field
|
|
||||||
-- print(headerField[fieldNum])
|
|
||||||
doubleQuoteEscape = createNewField(inputString, quote, fieldStart, i, outResults[lineNum], headerField[fieldNum], doubleQuoteEscape, fieldsToKeep)
|
|
||||||
|
|
||||||
fieldNum = fieldNum + 1
|
-- newline?!
|
||||||
fieldStart = i + 1
|
elseif ((currentChar == CR and nextChar == LF) or currentChar == LF) then
|
||||||
-- print("fs+1:", fieldStart)
|
-- keep track of headers
|
||||||
-- end
|
if not headerSet and lineNum == 1 then
|
||||||
|
headerField[fieldNum] = fieldNum
|
||||||
|
end
|
||||||
|
|
||||||
-- newline?!
|
-- create the new field
|
||||||
elseif ((currentChar == CR and nextChar == LF) or currentChar == LF) then
|
doubleQuoteEscape = createNewField(inputString, quote, fieldStart, i, outResults[lineNum], headerField[fieldNum], doubleQuoteEscape, fieldsToKeep)
|
||||||
-- keep track of headers
|
|
||||||
if not headerSet and lineNum == 1 then
|
|
||||||
headerField[fieldNum] = fieldNum
|
|
||||||
end
|
|
||||||
|
|
||||||
-- create the new field
|
-- if we have headers then we gotta do something about it
|
||||||
doubleQuoteEscape = createNewField(inputString, quote, fieldStart, i, outResults[lineNum], headerField[fieldNum], doubleQuoteEscape, fieldsToKeep)
|
if lineNum == 1 and not headerSet then
|
||||||
|
if ofieldsToKeep ~= nil then
|
||||||
-- if we have headers then we gotta do something about it
|
fieldsToKeep = {}
|
||||||
if lineNum == 1 and not headerSet then
|
for j = 1, #ofieldsToKeep do
|
||||||
if ofieldsToKeep ~= nil then
|
fieldsToKeep[ofieldsToKeep[j]] = true
|
||||||
fieldsToKeep = {}
|
|
||||||
for j = 1, #ofieldsToKeep do
|
|
||||||
fieldsToKeep[ofieldsToKeep[j]] = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if header then
|
end
|
||||||
headerField, lineNum, headerSet = createHeaders(outResults[lineNum], rename)
|
if header then
|
||||||
else
|
headerField, lineNum, headerSet = createHeaders(outResults[lineNum], rename)
|
||||||
-- files without headers, but with a rename need to be handled too!
|
else
|
||||||
if #rename > 0 then
|
-- files without headers, but with a rename need to be handled too!
|
||||||
for j = 1, math.max(#rename, #headerField) do
|
if #rename > 0 then
|
||||||
headerField[j] = rename[j]
|
for j = 1, math.max(#rename, #headerField) do
|
||||||
-- this is an odd case of where there are certain fields to be kept
|
headerField[j] = rename[j]
|
||||||
if fieldsToKeep == nil or fieldsToKeep[rename[j]] then
|
-- this is an odd case of where there are certain fields to be kept
|
||||||
outResults[1][rename[j]] = outResults[1][j]
|
if fieldsToKeep == nil or fieldsToKeep[rename[j]] then
|
||||||
end
|
outResults[1][rename[j]] = outResults[1][j]
|
||||||
-- print("J", j)
|
|
||||||
outResults[1][j] = nil
|
|
||||||
end
|
end
|
||||||
|
-- print("J", j)
|
||||||
|
outResults[1][j] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- incrememnt for new line
|
|
||||||
lineNum = lineNum + 1
|
|
||||||
outResults[lineNum] = {}
|
|
||||||
fieldNum = 1
|
|
||||||
fieldStart = i + 1
|
|
||||||
-- print("fs:", fieldStart)
|
|
||||||
if (currentChar == CR and nextChar == LF) then
|
|
||||||
-- print("CRLF DETECTED")
|
|
||||||
skipChar = 1
|
|
||||||
fieldStart = fieldStart + 1
|
|
||||||
-- print("fs:", fieldStart)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- incrememnt for new line
|
||||||
|
lineNum = lineNum + 1
|
||||||
|
outResults[lineNum] = {}
|
||||||
|
fieldNum = 1
|
||||||
|
fieldStart = i + 1
|
||||||
|
-- print("fs:", fieldStart)
|
||||||
|
if (currentChar == CR and nextChar == LF) then
|
||||||
|
-- print("CRLF DETECTED")
|
||||||
|
skipChar = 1
|
||||||
|
fieldStart = fieldStart + 1
|
||||||
|
-- print("fs:", fieldStart)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
i = i + 1 + skipChar
|
i = i + 1 + skipChar
|
||||||
if (skipChar > 0) then
|
if (skipChar > 0) then
|
||||||
currentChar = string.byte(inputString, i)
|
currentChar = string.byte(inputString, i)
|
||||||
|
Loading…
Reference in New Issue
Block a user