summaryrefslogtreecommitdiff
path: root/src/fontloader/misc/fontloader-l-io.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/fontloader/misc/fontloader-l-io.lua')
-rw-r--r--src/fontloader/misc/fontloader-l-io.lua95
1 files changed, 72 insertions, 23 deletions
diff --git a/src/fontloader/misc/fontloader-l-io.lua b/src/fontloader/misc/fontloader-l-io.lua
index a91d44d..2039017 100644
--- a/src/fontloader/misc/fontloader-l-io.lua
+++ b/src/fontloader/misc/fontloader-l-io.lua
@@ -7,6 +7,7 @@ if not modules then modules = { } end modules ['l-io'] = {
}
local io = io
+local open, flush, write, read = io.open, io.flush, io.write, io.read
local byte, find, gsub, format = string.byte, string.find, string.gsub, string.format
local concat = table.concat
local floor = math.floor
@@ -30,15 +31,13 @@ local function readall(f)
local size = f:seek("end")
if size == 0 then
return ""
- elseif size < 1024*1024 then
- f:seek("set",0)
+ end
+ f:seek("set",0)
+ if size < 1024*1024 then
return f:read('*all')
else
- local done = f:seek("set",0)
local step
- if size < 1024*1024 then
- step = 1024 * 1024
- elseif size > 16*1024*1024 then
+ if size > 16*1024*1024 then
step = 16*1024*1024
else
step = floor(size/(1024*1024)) * 1024 * 1024 / 8
@@ -58,9 +57,8 @@ end
io.readall = readall
function io.loaddata(filename,textmode) -- return nil if empty
- local f = io.open(filename,(textmode and 'r') or 'rb')
+ local f = open(filename,(textmode and 'r') or 'rb')
if f then
- -- local data = f:read('*all')
local data = readall(f)
f:close()
if #data > 0 then
@@ -69,8 +67,55 @@ function io.loaddata(filename,textmode) -- return nil if empty
end
end
+function io.copydata(source,target,action)
+ local f = open(source,"rb")
+ if f then
+ local g = open(target,"wb")
+ if g then
+ local size = f:seek("end")
+ if size == 0 then
+ -- empty
+ else
+ f:seek("set",0)
+ if size < 1024*1024 then
+ local data = f:read('*all')
+ if action then
+ data = action(data)
+ end
+ if data then
+ g:write(data)
+ end
+ else
+ local step
+ if size > 16*1024*1024 then
+ step = 16*1024*1024
+ else
+ step = floor(size/(1024*1024)) * 1024 * 1024 / 8
+ end
+ while true do
+ local data = f:read(step)
+ if data then
+ if action then
+ data = action(data)
+ end
+ if data then
+ g:write(data)
+ end
+ else
+ break
+ end
+ end
+ end
+ end
+ g:close()
+ end
+ f:close()
+ flush()
+ end
+end
+
function io.savedata(filename,data,joiner)
- local f = io.open(filename,"wb")
+ local f = open(filename,"wb")
if f then
if type(data) == "table" then
f:write(concat(data,joiner or ""))
@@ -80,7 +125,7 @@ function io.savedata(filename,data,joiner)
f:write(data or "")
end
f:close()
- io.flush()
+ flush()
return true
else
return false
@@ -90,7 +135,7 @@ end
-- we can also chunk this one if needed: io.lines(filename,chunksize,"*l")
function io.loadlines(filename,n) -- return nil if empty
- local f = io.open(filename,'r')
+ local f = open(filename,'r')
if not f then
-- no file
elseif n then
@@ -118,7 +163,7 @@ function io.loadlines(filename,n) -- return nil if empty
end
function io.loadchunk(filename,n)
- local f = io.open(filename,'rb')
+ local f = open(filename,'rb')
if f then
local data = f:read(n or 1024)
f:close()
@@ -129,7 +174,7 @@ function io.loadchunk(filename,n)
end
function io.exists(filename)
- local f = io.open(filename)
+ local f = open(filename)
if f == nil then
return false
else
@@ -139,7 +184,7 @@ function io.exists(filename)
end
function io.size(filename)
- local f = io.open(filename)
+ local f = open(filename)
if f == nil then
return 0
else
@@ -149,11 +194,11 @@ function io.size(filename)
end
end
-function io.noflines(f)
+local function noflines(f)
if type(f) == "string" then
- local f = io.open(filename)
+ local f = open(filename)
if f then
- local n = f and io.noflines(f) or 0
+ local n = f and noflines(f) or 0
f:close()
return n
else
@@ -169,6 +214,10 @@ function io.noflines(f)
end
end
+io.noflines = noflines
+
+-- inlined is faster
+
local nextchar = {
[ 4] = function(f)
return f:read(1,1,1,1)
@@ -250,16 +299,16 @@ end
function io.ask(question,default,options)
while true do
- io.write(question)
+ write(question)
if options then
- io.write(format(" [%s]",concat(options,"|")))
+ write(format(" [%s]",concat(options,"|")))
end
if default then
- io.write(format(" [%s]",default))
+ write(format(" [%s]",default))
end
- io.write(format(" "))
- io.flush()
- local answer = io.read()
+ write(format(" "))
+ flush()
+ local answer = read()
answer = gsub(answer,"^%s*(.*)%s*$","%1")
if answer == "" and default then
return default