diff options
Diffstat (limited to 'lauxlib.c')
| -rw-r--r-- | lauxlib.c | 55 |
1 files changed, 28 insertions, 27 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.116 2004/06/17 14:06:52 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.117 2004/06/21 20:05:29 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -354,41 +354,42 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { | |||
| 354 | } | 354 | } |
| 355 | 355 | ||
| 356 | 356 | ||
| 357 | static void pushcomposename (lua_State *L) { | 357 | static const char *gsub (lua_State *L, const char *s, const char *p, |
| 358 | const char *tem = lua_tostring(L, -1); | 358 | const char *r) { |
| 359 | const char *wild; | 359 | const char *wild; |
| 360 | int n = 1; | 360 | int l = strlen(p); |
| 361 | while ((wild = strchr(tem, LUA_PATH_MARK)) != NULL) { | 361 | luaL_Buffer b; |
| 362 | /* is there stack space for prefix, name, and eventual last suffix? */ | 362 | luaL_buffinit(L, &b); |
| 363 | luaL_checkstack(L, 3, "too many marks in a path component"); | 363 | while ((wild = strstr(s, p)) != NULL) { |
| 364 | lua_pushlstring(L, tem, wild - tem); /* push prefix */ | 364 | luaL_addlstring(&b, s, wild - s); /* push prefix */ |
| 365 | lua_pushvalue(L, 1); /* push package name (in place of MARK) */ | 365 | luaL_addstring(&b, r); /* push replacement in place of pattern */ |
| 366 | tem = wild + 1; /* continue after MARK */ | 366 | s = wild + l; /* continue after p */ |
| 367 | n += 2; | ||
| 368 | } | 367 | } |
| 369 | lua_pushstring(L, tem); /* push last suffix (`n' already includes this) */ | 368 | luaL_addstring(&b, s); /* push last suffix (`n' already includes this) */ |
| 370 | lua_concat(L, n); | 369 | luaL_pushresult(&b); |
| 370 | return lua_tostring(L, -1); | ||
| 371 | } | 371 | } |
| 372 | 372 | ||
| 373 | 373 | ||
| 374 | LUALIB_API int luaL_searchpath (lua_State *L, const char *name, | 374 | LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, |
| 375 | const char *path, luaL_Loader f, void *data) { | 375 | const char *path) { |
| 376 | int status; | 376 | FILE *f; |
| 377 | const char *p = path; | 377 | const char *p = path; |
| 378 | int top = lua_gettop(L); | 378 | for (;;) { |
| 379 | do { | 379 | const char *fname; |
| 380 | lua_settop(L, top); | ||
| 381 | if ((p = pushnexttemplate(L, p)) == NULL) { | 380 | if ((p = pushnexttemplate(L, p)) == NULL) { |
| 382 | lua_pushfstring(L, "cound not find `%s' in path `%s'", name, path); | 381 | lua_pushfstring(L, "no readable `%s' in path `%s'", name, path); |
| 383 | return LUA_ERRFILE; | 382 | return NULL; |
| 384 | } | 383 | } |
| 385 | pushcomposename(L); | 384 | fname = gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); |
| 386 | lua_remove(L, -2); /* remove path template */ | 385 | lua_remove(L, -2); /* remove path template */ |
| 387 | lua_assert(lua_gettop(L) == top + 1); | 386 | f = fopen(fname, "r"); /* try to read it */ |
| 388 | status = f(data, lua_tostring(L, -1)); /* try to load it */ | 387 | if (f) { |
| 389 | lua_remove(L, top+1); /* remove file name */ | 388 | fclose(f); |
| 390 | } while (status == LUA_ERRFILE); | 389 | return fname; |
| 391 | return status; | 390 | } |
| 391 | lua_pop(L, 1); /* remove file name */ | ||
| 392 | } | ||
| 392 | } | 393 | } |
| 393 | 394 | ||
| 394 | 395 | ||
