Add virtual io support for accessing resources

Sun, 26 Jul 2009 00:56:13 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Sun, 26 Jul 2009 00:56:13 +0100
changeset 19
f72a0f535301
parent 18
c005ff9eef47
child 20
1766b5a287e1

Add virtual io support for accessing resources

Makefile file | annotate | diff | comparison | revisions
squish.lua file | annotate | diff | comparison | revisions
squishy file | annotate | diff | comparison | revisions
vio/vio.lua file | annotate | diff | comparison | revisions
--- 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)
--- 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
 
--- 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
--- /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
+

mercurial