lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I use that on Lua2SC (only open, read, close but seek should be easy)

-- function takes a string that behaves like a file to be read
function StrFileOpen(strg)
   local strptr = 1
   local str = strg
   local strfile = {}
   function strfile:read(len)
       local oldstrptr = strptr
       strptr = strptr + len
       return str:sub(oldstrptr,strptr-1)
   end
   function strfile:close(len)
       self = nil
       return true
   end
   return strfile
end

----- Original Message ----- From: "Dirk Laurie" <dirk.laurie@gmail.com>
To: "Lua mailing list" <lua-l@lists.lua.org>
Sent: Tuesday, June 03, 2014 9:11 AM
Subject: File-like string scanning


I find myself writing my own code for functions string.lines(...) and
string.read(...) so that str:lines and str:read have the same semantics
as file:lines and file:read, except that they are use-once since strings
can't store position.

I can't believe that I'm the first person who has though of doing this.
Any pointer to a module that does it nicely will be appreciated.