# HG changeset patch # User Matthew Wild # Date 1248566173 -3600 # Node ID f72a0f535301818928acd9c67900fec87cc3aff5 # Parent c005ff9eef4738ef645722c072cb3af4baeee21e Add virtual io support for accessing resources diff -r c005ff9eef47 -r f72a0f535301 Makefile --- a/Makefile Sun Jul 26 00:51:10 2009 +0100 +++ b/Makefile Sun Jul 26 00:56:13 2009 +0100 @@ -1,5 +1,5 @@ -OPTIONS=--with-minify --with-uglify --with-compile +OPTIONS=--with-minify --with-uglify --with-compile --with-virtual-io squish: squish.lua squishy ./squish.lua $(OPTIONS) diff -r c005ff9eef47 -r f72a0f535301 squish.lua --- a/squish.lua Sun Jul 26 00:51:10 2009 +0100 +++ b/squish.lua Sun Jul 26 00:56:13 2009 +0100 @@ -193,6 +193,22 @@ f:write(data); f:write("]", string.rep("=", maxequals+1), "];"); end + if opts.enable_virtual_io then + local vio = require_resource("vio"); + if not vio then + print_err("Virtual IO requested but is not enabled in this build of squish"); + else + -- Insert vio library + f:write(vio, "\n") + -- Override io.open to use vio if opening a resource + f:write[[local io_open = io.open; function io.open(fn, mode) + if not resources[fn] then + return io_open(fn, mode); + else + return vio.open(resources[fn]); + end end ]] + end + end f:write[[function require_resource(name) return resources[name] or error("resource '"..tostring(name).."' not found"); end end ]] end diff -r c005ff9eef47 -r f72a0f535301 squishy --- a/squishy Sun Jul 26 00:51:10 2009 +0100 +++ b/squishy Sun Jul 26 00:56:13 2009 +0100 @@ -31,3 +31,7 @@ if GetOption "with-compile" then Main "compile/squish.compile.lua" end + +if GetOption "with-virtual-io" then + Resource "vio" "vio/vio.lua" +end diff -r c005ff9eef47 -r f72a0f535301 vio/vio.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vio/vio.lua Sun Jul 26 00:56:13 2009 +0100 @@ -0,0 +1,61 @@ +local vio = {}; +vio.__index = vio; + +function vio.open(string) + return setmetatable({ pos = 1, data = string }, vio); +end + +function vio:read(format, ...) + if self.pos >= #self.data then return; end + if format == "*a" then + local oldpos = self.pos; + self.pos = #self.data; + return self.data:sub(oldpos, self.pos); + elseif format == "*l" then + local data; + data, self.pos = self.data:match("([^\r\n]*)\r?\n?()", self.pos) + return data; + elseif format == "*n" then + local data; + data, self.pos = self.data:match("(%d+)()", self.pos) + return tonumber(data); + elseif type(format) == "number" then + local oldpos = self.pos; + self.pos = self.pos + format; + return self.data:sub(oldpos, self.pos-1); + end +end + +function vio:seek(whence, offset) + if type(whence) == "number" then + whence, offset = "cur", whence; + end + offset = offset or 0; + + if whence == "cur" then + self.pos = self.pos + offset; + elseif whence == "set" then + self.pos = offset + 1; + elseif whence == "end" then + self.pos = #self.data - offset; + end + + return self.pos; +end + +local function _readline(f) return f:read("*l"); end +function vio:lines() + return _readline, self; +end + +function vio:write(...) + for i=1,select('#', ...) do + local dat = tostring(select(i, ...)); + self.data = self.data:sub(1, self.pos-1)..dat..self.data:sub(self.pos+#dat, -1); + end +end + +function vio:close() + self.pos, self.data = nil, nil; +end +