lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 753625c3f377b013d00a8bd20f4492338e2d87b8
parent 42b74ccf1d8c2bb181be6900b068a3a011e106a9
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Tue, 29 Jun 2004 13:57:34 -0300

new interface for search-path function

Diffstat:
Mlauxlib.c | 55++++++++++++++++++++++++++++---------------------------
Mlauxlib.h | 8++++----
Mluaconf.h | 4++--
3 files changed, 34 insertions(+), 33 deletions(-)

diff --git a/lauxlib.c b/lauxlib.c @@ -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 ** 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) { - const char *tem = lua_tostring(L, -1); +static const char *gsub (lua_State *L, const char *s, const char *p, + const char *r) { const char *wild; - int n = 1; - while ((wild = strchr(tem, LUA_PATH_MARK)) != NULL) { - /* is there stack space for prefix, name, and eventual last suffix? */ - luaL_checkstack(L, 3, "too many marks in a path component"); - lua_pushlstring(L, tem, wild - tem); /* push prefix */ - lua_pushvalue(L, 1); /* push package name (in place of MARK) */ - tem = wild + 1; /* continue after MARK */ - n += 2; + int l = strlen(p); + luaL_Buffer b; + luaL_buffinit(L, &b); + while ((wild = strstr(s, p)) != NULL) { + luaL_addlstring(&b, s, wild - s); /* push prefix */ + luaL_addstring(&b, r); /* push replacement in place of pattern */ + s = wild + l; /* continue after p */ } - lua_pushstring(L, tem); /* push last suffix (`n' already includes this) */ - lua_concat(L, n); + luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */ + luaL_pushresult(&b); + return lua_tostring(L, -1); } -LUALIB_API int luaL_searchpath (lua_State *L, const char *name, - const char *path, luaL_Loader f, void *data) { - int status; +LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, + const char *path) { + FILE *f; const char *p = path; - int top = lua_gettop(L); - do { - lua_settop(L, top); + for (;;) { + const char *fname; if ((p = pushnexttemplate(L, p)) == NULL) { - lua_pushfstring(L, "cound not find `%s' in path `%s'", name, path); - return LUA_ERRFILE; + lua_pushfstring(L, "no readable `%s' in path `%s'", name, path); + return NULL; } - pushcomposename(L); + fname = gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); lua_remove(L, -2); /* remove path template */ - lua_assert(lua_gettop(L) == top + 1); - status = f(data, lua_tostring(L, -1)); /* try to load it */ - lua_remove(L, top+1); /* remove file name */ - } while (status == LUA_ERRFILE); - return status; + f = fopen(fname, "r"); /* try to read it */ + if (f) { + fclose(f); + return fname; + } + lua_pop(L, 1); /* remove file name */ + } } diff --git a/lauxlib.h b/lauxlib.h @@ -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 ** See Copyright Notice in lua.h */ @@ -25,7 +25,7 @@ typedef struct 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, 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_searchpath (lua_State *L, const char *name, - const char *path, luaL_Loader f, void *data); +LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, + const char *path); LUALIB_API int luaL_ref (lua_State *L, int t); LUALIB_API void luaL_unref (lua_State *L, int t, int ref); diff --git a/luaconf.h b/luaconf.h @@ -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 ** See Copyright Notice in lua.h */ @@ -271,7 +271,7 @@ #define LUA_PATH_SEP ';' /* wild char in each template */ -#define LUA_PATH_MARK '?' +#define LUA_PATH_MARK "?" /* default path */ #define LUA_PATH_DEFAULT "?;?.lua"