Skip to content

Commit 6ad339d

Browse files
committed
Merge pull request geoffleyland#1 from kev82/openstring
Addition of csv.openstring plus other minor fixes
2 parents 1ed256e + f28dfe0 commit 6ad339d

File tree

6 files changed

+81
-8
lines changed

6 files changed

+81
-8
lines changed

AUTHORS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Leyland, Geoff
1+
Leyland, Geoff
2+
Martin, Kevin

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Copyright (c) 2013 Incremental IP Limited
2+
Copyright (c) 2014 Kevin Martin
23

34
Permission is hereby granted, free of charge, to any person obtaining a copy
45
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ controlling how the file is read:
5454
+ `buffer_size` controls the size of the blocks the file is read in. The
5555
default is 4096, which is what `pagesize` says on my system.
5656

57+
`csv.openstring` works exactly like `csv.open` except the first argument
58+
is the contents of the csv file. In this case `buffer_size` is set to
59+
the length of the string.
5760

5861
## 3. Requirements
5962

lua/csv.lua

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
-- likely gets something wrong.
1212

1313
-- (c) Copyright 2013 Incremental IP Limited.
14+
-- (c) Copyright 2014 Kevin Martin
1415
-- Available under the MIT licence. See LICENSE for more information.
1516

16-
local DEFAULT_BUFFER_SIZE = 1024
17+
local DEFAULT_BUFFER_SIZE = 4096
1718

1819

1920
------------------------------------------------------------------------------
@@ -322,6 +323,9 @@ local file_mt =
322323
close = function(t)
323324
t.file:close()
324325
end,
326+
name = function(t)
327+
return t.parameters.filename
328+
end,
325329
}
326330
file_mt.__index = file_mt
327331

@@ -349,6 +353,52 @@ end
349353

350354
------------------------------------------------------------------------------
351355

352-
return { open = open, use = use }
356+
local stringfh_mt =
357+
{
358+
read = function(self, bytes)
359+
if not self._string then return nil end
360+
361+
local read_rv
362+
read_rv, self._string =
363+
self._string:sub(1, bytes), self._string:sub(bytes+1)
364+
365+
if #self._string == 0 then
366+
self._string = nil
367+
end
368+
369+
return read_rv
370+
end,
371+
close = function()
372+
end
373+
}
374+
stringfh_mt.__index = stringfh_mt
375+
376+
--- Open a string for reading as a delimited file
377+
-- @return a file object
378+
local function openstring(
379+
filecontents, -- string: The contents of the delimited file
380+
parameters) -- ?table: parameters controlling reading the file.
381+
-- See README.md
382+
383+
parameters = parameters or {}
384+
385+
local function makename()
386+
local t = {}
387+
t[#t+1] = "<(String) "
388+
t[#t+1] = (filecontents:gmatch("[^\n]+")() or ""):sub(1,15)
389+
if #t[#t] > 14 then t[#t+1] = "..." end
390+
t[#t+1] = " >"
391+
return table.concat(t)
392+
end
393+
394+
parameters.filename = parameters.filename or makename()
395+
parameters.buffer_size = parameters.buffer_size or #filecontents
396+
local fileh = setmetatable({_string = filecontents}, stringfh_mt)
397+
return use(fileh, parameters)
398+
end
399+
400+
------------------------------------------------------------------------------
401+
402+
return { open = open, openstring = openstring, use = use }
353403

354404
------------------------------------------------------------------------------

lua/test.lua

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ local csv = require"csv"
33

44
local errors = 0
55

6-
local function test(filename, correct_result, parameters)
6+
local function testhandle(handle, correct_result)
77
local result = {}
8-
local f = csv.open(filename, parameters)
9-
for r in f:lines() do
8+
for r in handle:lines() do
109
if not r[1] then
1110
local r2 = {}
1211
for k, v in pairs(r) do r2[#r2+1] = k..":"..tostring(v) end
@@ -15,15 +14,33 @@ local function test(filename, correct_result, parameters)
1514
end
1615
result[#result+1] = table.concat(r, ",")
1716
end
17+
18+
handle:close()
19+
1820
result = table.concat(result, "\n")
1921
if result ~= correct_result then
2022
io.stderr:write(
2123
("Error reading '%s':\nExpected output:\n%s\n\nActual output:\n%s\n\n"):
22-
format(filename, correct_result, result))
24+
format(handle:name(), correct_result, result))
2325
errors = errors + 1
26+
return false
2427
end
28+
return true
2529
end
2630

31+
local function test(filename, correct_result, parameters)
32+
local f = csv.open(filename, parameters)
33+
local fileok = testhandle(f, correct_result)
34+
35+
if fileok then
36+
f = io.open(filename, "r")
37+
local data = f:read("*a")
38+
f:close()
39+
40+
f = csv.openstring(data, parameters)
41+
testhandle(f, correct_result)
42+
end
43+
end
2744

2845
test("../test-data/embedded-newlines.csv", [[
2946
embedded
@@ -62,4 +79,4 @@ else
6279
io.stdout:write(("%d errors\n"):format(errors))
6380
end
6481

65-
os.exit(errors)
82+
os.exit(errors)

test-data/blank-line.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
this,file,ends,with,a,blank,line
2+

0 commit comments

Comments
 (0)