--****************************************************************************** --* * --* File: LibrarySupport.lua Revision: 1.0 * --* * --* Purpose: provides a simple "library search and import" mechanism * --* * --* Creation: 13.03.2002 Last Modification: 13.03.2002 * --* * --* Platform: IBM-compatible PC running Windows 98SE * --* * --* Environment: Lua 4.0, TkLua 4.0 * --* * --* Author: Andreas Rozek Phone: ++49 (711) 6770682 * --* Kirschblütenweg 15 Fax: - * --* D-70569 Stuttgart EMail: Andreas.Rozek@T-Online.De * --* Germany * --* * --* URL: http://www.Andreas-Rozek.de/ * --* * --* Copyright: the software is published under the "GNU Lesser General Pub- * --* lic License" (see "http://www.fsf.org/copyleft/lesser.html" * --* for additional information) * --* * --* Comments: this file should be copied into a directory where it can be * --* found (by 'dofile') without the search mechanism implemented * --* below * --* * --* Known Bugs: absolute path names (of library files) are not yet handled * --* * --****************************************************************************** --**** do not execute this script more often than once **** if (type(import) == "function") then return nil; end; --**** retrieve some platform-specific settings for library imports **** LuaHome = getenv("LuaHome") or "."; -- retrieve Lua home directory LuaLibList = getenv("LuaLibList") or "."; -- retrieve library folder list if (tkeval ~= nil) then -- use Tk to determine the actual platform local Platform = tkeval("set tcl_platform(platform)")[1]; if (Platform == "windows") then LuaPathSeparator,LuaListSeparator = "\\",";"; elseif (Platform == "macintosh") then LuaPathSeparator,LuaListSeparator = ":",";"; else --(Platform == "unix") then LuaPathSeparator,LuaListSeparator = "/",";"; end; else -- read or "guess" platform-specific settings LuaPathSeparator = getenv("LuaPathSeparator"); if (LuaPathSeparator == nil) and (strfind(LuaHome..LuaLibList,"\\",1,1) ~= nil) then LuaPathSeparator = "\\"; end; LuaHome = gsub(gsub(LuaHome, "^ *",""), " *$",""); -- "trims" 'LuaHome' if (LuaPathSeparator == nil) and (strfind(LuaHome,":",3,1) ~= nil) then LuaPathSeparator = ":"; end; if (LuaPathSeparator == nil) and (strfind(LuaHome..LuaLibList,"/",1,1) ~= nil) then LuaPathSeparator = "/"; end; assert(strfind("\\:/",LuaPathSeparator,1,1),"invalid path separator \""..LuaPathSeparator.."\""); LuaListSeparator = getenv("LuaListSeparator") or ";"; assert(LuaListSeparator == ";", "invalid search list separator \""..LuaListSeparator.."\""); end; local LibList = {}; gsub(LuaLibList..LuaListSeparator, "([^"..LuaListSeparator.."]*)"..LuaListSeparator, -- tricky! function (Capture) Capture = gsub(gsub(Capture, "^ *",""), " *$",""); -- "trims" 'Capture' if (Capture == "") then return ""; end; -- "" is required by parser if (strsub(Capture,-1) ~= LuaPathSeparator) then Capture = Capture..LuaPathSeparator; end; tinsert(%LibList,Capture); end ); --****************************************************************************** --* * --* import looks for the given files in a directory list and executes them * --* * --****************************************************************************** function import (FileName, ...) local ArgCount = getn(arg); if (ArgCount > 0) then import(FileName); for i = 1,getn(arg) do import(arg[i]); end; end; FileName = gsub(gsub(FileName, "^ *",""), " *$",""); -- "trims" 'FileName' assert(FileName ~= "", "empty \"FileName\" given"); if (LuaPathSeparator ~= "/") then -- '/' is the "cross-platform" path sep. FileName = gsub(FileName, "/", LuaPathSeparator); end; for i = 1,getn(%LibList) do local FileHandle,ErrMsg = openfile(%LibList[i]..FileName,"r"); if (FileHandle == nil) and (strlower(strsub(FileName,-4)) ~= ".lua") then FileHandle,ErrMsg = openfile(%LibList[i]..FileName..".lua","r"); if (FileHandle ~= nil) then FileName = FileName..".lua"; end; end; if (FileHandle ~= nil) then closefile(FileHandle); dofile(%LibList[i]..FileName); return nil; -- 'nil' seems to be required by the parser end; end; error("Library file \""..FileName.."\" could not be found"); end;