new interface for search-path function

This commit is contained in:
Roberto Ierusalimschy 2004-06-29 13:57:56 -03:00
parent 42b74ccf1d
commit 753625c3f3
3 changed files with 34 additions and 33 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.c,v 1.116 2004/06/17 14:06:52 roberto Exp roberto $ ** $Id: lauxlib.c,v 1.117 2004/06/21 20:05:29 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -354,41 +354,42 @@ static const char *pushnexttemplate (lua_State *L, const char *path) {
} }
static void pushcomposename (lua_State *L) { static const char *gsub (lua_State *L, const char *s, const char *p,
const char *tem = lua_tostring(L, -1); const char *r) {
const char *wild; const char *wild;
int n = 1; int l = strlen(p);
while ((wild = strchr(tem, LUA_PATH_MARK)) != NULL) { luaL_Buffer b;
/* is there stack space for prefix, name, and eventual last suffix? */ luaL_buffinit(L, &b);
luaL_checkstack(L, 3, "too many marks in a path component"); while ((wild = strstr(s, p)) != NULL) {
lua_pushlstring(L, tem, wild - tem); /* push prefix */ luaL_addlstring(&b, s, wild - s); /* push prefix */
lua_pushvalue(L, 1); /* push package name (in place of MARK) */ luaL_addstring(&b, r); /* push replacement in place of pattern */
tem = wild + 1; /* continue after MARK */ s = wild + l; /* continue after p */
n += 2;
} }
lua_pushstring(L, tem); /* push last suffix (`n' already includes this) */ luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */
lua_concat(L, n); luaL_pushresult(&b);
return lua_tostring(L, -1);
} }
LUALIB_API int luaL_searchpath (lua_State *L, const char *name, LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
const char *path, luaL_Loader f, void *data) { const char *path) {
int status; FILE *f;
const char *p = path; const char *p = path;
int top = lua_gettop(L); for (;;) {
do { const char *fname;
lua_settop(L, top);
if ((p = pushnexttemplate(L, p)) == NULL) { if ((p = pushnexttemplate(L, p)) == NULL) {
lua_pushfstring(L, "cound not find `%s' in path `%s'", name, path); lua_pushfstring(L, "no readable `%s' in path `%s'", name, path);
return LUA_ERRFILE; return NULL;
} }
pushcomposename(L); fname = gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name);
lua_remove(L, -2); /* remove path template */ lua_remove(L, -2); /* remove path template */
lua_assert(lua_gettop(L) == top + 1); f = fopen(fname, "r"); /* try to read it */
status = f(data, lua_tostring(L, -1)); /* try to load it */ if (f) {
lua_remove(L, top+1); /* remove file name */ fclose(f);
} while (status == LUA_ERRFILE); return fname;
return status; }
lua_pop(L, 1); /* remove file name */
}
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lauxlib.h,v 1.66 2004/06/02 17:37:03 roberto Exp roberto $ ** $Id: lauxlib.h,v 1.67 2004/06/21 20:05:29 roberto Exp roberto $
** Auxiliary functions for building Lua libraries ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -25,7 +25,7 @@ typedef struct luaL_reg {
} luaL_reg; } luaL_reg;
typedef int (*luaL_Loader)(void *data, const char *name); typedef int (*luaL_Loader)(lua_State *L, const char *name);
LUALIB_API void luaL_openlib (lua_State *L, const char *libname, LUALIB_API void luaL_openlib (lua_State *L, const char *libname,
const luaL_reg *l, int nup); const luaL_reg *l, int nup);
@ -56,8 +56,8 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...);
LUALIB_API int luaL_findstring (const char *st, const char *const lst[]); LUALIB_API int luaL_findstring (const char *st, const char *const lst[]);
LUALIB_API int luaL_searchpath (lua_State *L, const char *name, LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name,
const char *path, luaL_Loader f, void *data); const char *path);
LUALIB_API int luaL_ref (lua_State *L, int t); LUALIB_API int luaL_ref (lua_State *L, int t);
LUALIB_API void luaL_unref (lua_State *L, int t, int ref); LUALIB_API void luaL_unref (lua_State *L, int t, int ref);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: luaconf.h,v 1.6 2004/06/02 19:07:55 roberto Exp roberto $ ** $Id: luaconf.h,v 1.7 2004/06/23 15:57:29 roberto Exp roberto $
** Configuration file for Lua ** Configuration file for Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -271,7 +271,7 @@
#define LUA_PATH_SEP ';' #define LUA_PATH_SEP ';'
/* wild char in each template */ /* wild char in each template */
#define LUA_PATH_MARK '?' #define LUA_PATH_MARK "?"
/* default path */ /* default path */
#define LUA_PATH_DEFAULT "?;?.lua" #define LUA_PATH_DEFAULT "?;?.lua"