summaryrefslogtreecommitdiff
path: root/tex/context/base/l-io.lua
diff options
context:
space:
mode:
authorHans Hagen <pragma@wxs.nl>2012-06-05 09:16:00 +0200
committerHans Hagen <pragma@wxs.nl>2012-06-05 09:16:00 +0200
commit1cbef76ff4aa6cc1f7dc1d38c4b0514e94ad59a5 (patch)
treef825c9515b1d51e48dddd9a3bc0d0c51c6b407ed /tex/context/base/l-io.lua
parentc3efc6042c5a5a4d0f1a80bc3a097f0ae2963f7c (diff)
downloadcontext-1cbef76ff4aa6cc1f7dc1d38c4b0514e94ad59a5.tar.gz
beta 2012.06.05 09:16
Diffstat (limited to 'tex/context/base/l-io.lua')
-rw-r--r--tex/context/base/l-io.lua47
1 files changed, 43 insertions, 4 deletions
diff --git a/tex/context/base/l-io.lua b/tex/context/base/l-io.lua
index 4f27dc1dc..657b755b8 100644
--- a/tex/context/base/l-io.lua
+++ b/tex/context/base/l-io.lua
@@ -17,14 +17,14 @@ else
io.fileseparator, io.pathseparator = "/" , ":"
end
-function io.loaddata(filename,textmode)
+function io.loaddata(filename,textmode) -- return nil if empty
local f = io.open(filename,(textmode and 'r') or 'rb')
if f then
local data = f:read('*all')
f:close()
- return data
- else
- return nil
+ if #data > 0 then
+ return data
+ end
end
end
@@ -46,6 +46,45 @@ function io.savedata(filename,data,joiner)
end
end
+function io.loadlines(filename,n) -- return nil if empty
+ local f = io.open(filename,'r')
+ if f then
+ if n then
+ local lines = { }
+ for i=1,n do
+ local line = f:read("*lines")
+ if line then
+ lines[#lines+1] = line
+ else
+ break
+ end
+ end
+ f:close()
+ lines = concat(lines,"\n")
+ if #lines > 0 then
+ return lines
+ end
+ else
+ local line = f:read("*line") or ""
+ assert(f:close())
+ if #line > 0 then
+ return line
+ end
+ end
+ end
+end
+
+function io.loadchunk(filename,n)
+ local f = io.open(filename,'rb')
+ if f then
+ local data = f:read(n or 1024)
+ f:close()
+ if #data > 0 then
+ return data
+ end
+ end
+end
+
function io.exists(filename)
local f = io.open(filename)
if f == nil then