Warning: this is an htmlized version!
The original is here, and
the conversion rules are here.
-- This file:
--   http://angg.twu.net/LUA/Path.lua.html
--   http://angg.twu.net/LUA/Path.lua
--           (find-angg "LUA/Path.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
--
-- Also here: (find-angg "LUA/lua50init.lua" "Path")
-- See: (find-lua51manual "#pdf-package.path")
--      (find-lua51manual "#pdf-package.cpath")

Path = Class {
  type = "Path",
  from = function (field) return Path {field = field} end,
  prepend = function (field, fname) return Path.from(field):prepend(fname) end,
  prependtopath  = function (fname) return Path.prepend("path",  fname) end,
  prependtocpath = function (fname) return Path.prepend("cpath", fname) end,
  find = function (field, modulename)
      local modulepath = modulename:gsub("%.", "/")
      for pathentry in package[field]:gmatch("([^;]+)") do
        local filename = pathentry:gsub("%?", modulepath)
        local file = io.open(filename, "rb")
        if file then
          file:close()
          return filename
        end
      end
    end,
  __tostring = function (p) return p:tostring() end,
  __index = {
    get = function (p) return package[p.field] end,
    set = function (p, newvalue) package[p.field] = newvalue end,
    tostring = function (p, sep)
        return format("package.%s = %s", p.field, p:tostring0(sep))
      end,
    tostring0 = function (p, sep)
        return (p:get():gsub(";", sep or "\n ;"))
      end,
    toset = function (p)
        return Set.from(split(p:get(), "([^;]+)"))
      end,
    has = function (p, fname)
        return p:toset():has(fname)
      end,
    prepend0 = function (p, fname)
        p:set(ee_expand(fname)..";"..p:get())
      end,
    prepend = function (p, fname)
        if not p:has(ee_expand(fname)) then
	  p:prepend0(fname)
	end
        return p
      end
  },
}



--[[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
dofile "Path.lua"

p = Path.from "path"
= p:tostring(";")
= p:tostring()
= p
= p:toset():ksc()

= p:prepend("~/bread/?.lua")  -- add bread
= p:prepend("~/cheese/?.lua") -- add cheese
= p:prepend("~/bread/?.lua")  -- bread is not added again

= Path.find("cpath", "lpeg")
= Path.find("path", "re")

--]]




-- Local Variables:
-- coding:  utf-8-unix
-- End: