diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-06-21 17:05:29 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2004-06-21 17:05:29 -0300 |
commit | 0c49857c1ab08a5adfe1e21e5370e8aa132e4da1 (patch) | |
tree | 293301c3531f061c3a8bf7b7da898309dd3af21e /lauxlib.c | |
parent | 6b41e84da577b2d31cc659255b036bd0c2d6483f (diff) | |
download | lua-0c49857c1ab08a5adfe1e21e5370e8aa132e4da1.tar.gz lua-0c49857c1ab08a5adfe1e21e5370e8aa132e4da1.tar.bz2 lua-0c49857c1ab08a5adfe1e21e5370e8aa132e4da1.zip |
new scheme to search paths
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 52 |
1 files changed, 51 insertions, 1 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.115 2004/06/02 19:06:14 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.116 2004/06/17 14:06:52 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 | */ |
@@ -342,6 +342,56 @@ LUALIB_API int luaL_getn (lua_State *L, int t) { | |||
342 | 342 | ||
343 | 343 | ||
344 | 344 | ||
345 | |||
346 | static const char *pushnexttemplate (lua_State *L, const char *path) { | ||
347 | const char *l; | ||
348 | if (*path == '\0') return NULL; /* no more templates */ | ||
349 | if (*path == LUA_PATH_SEP) path++; /* skip separator */ | ||
350 | l = strchr(path, LUA_PATH_SEP); /* find next separator */ | ||
351 | if (l == NULL) l = path+strlen(path); | ||
352 | lua_pushlstring(L, path, l - path); /* template */ | ||
353 | return l; | ||
354 | } | ||
355 | |||
356 | |||
357 | static void pushcomposename (lua_State *L) { | ||
358 | const char *tem = lua_tostring(L, -1); | ||
359 | const char *wild; | ||
360 | int n = 1; | ||
361 | while ((wild = strchr(tem, LUA_PATH_MARK)) != NULL) { | ||
362 | /* is there stack space for prefix, name, and eventual last suffix? */ | ||
363 | luaL_checkstack(L, 3, "too many marks in a path component"); | ||
364 | lua_pushlstring(L, tem, wild - tem); /* push prefix */ | ||
365 | lua_pushvalue(L, 1); /* push package name (in place of MARK) */ | ||
366 | tem = wild + 1; /* continue after MARK */ | ||
367 | n += 2; | ||
368 | } | ||
369 | lua_pushstring(L, tem); /* push last suffix (`n' already includes this) */ | ||
370 | lua_concat(L, n); | ||
371 | } | ||
372 | |||
373 | |||
374 | LUALIB_API int luaL_searchpath (lua_State *L, const char *name, | ||
375 | const char *path, luaL_Loader f, void *data) { | ||
376 | int status; | ||
377 | const char *p = path; | ||
378 | int top = lua_gettop(L); | ||
379 | do { | ||
380 | lua_settop(L, top); | ||
381 | if ((p = pushnexttemplate(L, p)) == NULL) { | ||
382 | lua_pushfstring(L, "cound not find `%s' in path `%s'", name, path); | ||
383 | return LUA_ERRFILE; | ||
384 | } | ||
385 | pushcomposename(L); | ||
386 | lua_remove(L, -2); /* remove path template */ | ||
387 | lua_assert(lua_gettop(L) == top + 1); | ||
388 | status = f(data, lua_tostring(L, -1)); /* try to load it */ | ||
389 | lua_remove(L, top+1); /* remove file name */ | ||
390 | } while (status == LUA_ERRFILE); | ||
391 | return status; | ||
392 | } | ||
393 | |||
394 | |||
345 | /* | 395 | /* |
346 | ** {====================================================== | 396 | ** {====================================================== |
347 | ** Generic Buffer manipulation | 397 | ** Generic Buffer manipulation |