diff options
Diffstat (limited to 'lauxlib.c')
| -rw-r--r-- | lauxlib.c | 49 |
1 files changed, 46 insertions, 3 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.123 2004/08/30 18:35:14 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.124 2004/09/03 13:17:14 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 | */ |
| @@ -352,8 +352,8 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { | |||
| 352 | } | 352 | } |
| 353 | 353 | ||
| 354 | 354 | ||
| 355 | static const char *luaL_gsub (lua_State *L, const char *s, | 355 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, |
| 356 | const char *p, const char *r) { | 356 | const char *r) { |
| 357 | const char *wild; | 357 | const char *wild; |
| 358 | int l = strlen(p); | 358 | int l = strlen(p); |
| 359 | luaL_Buffer b; | 359 | luaL_Buffer b; |
| @@ -391,6 +391,49 @@ LUALIB_API const char *luaL_searchpath (lua_State *L, const char *name, | |||
| 391 | } | 391 | } |
| 392 | 392 | ||
| 393 | 393 | ||
| 394 | LUALIB_API const char *luaL_getfield (lua_State *L, const char *fname) { | ||
| 395 | const char *e; | ||
| 396 | while ((e = strchr(fname, '.')) != NULL) { | ||
| 397 | lua_pushlstring(L, fname, e - fname); | ||
| 398 | lua_gettable(L, -2); | ||
| 399 | lua_remove(L, -2); /* remove previous table */ | ||
| 400 | fname = e + 1; | ||
| 401 | if (!lua_istable(L, -1)) return fname; | ||
| 402 | } | ||
| 403 | lua_getfield(L, -1, fname); /* get last field */ | ||
| 404 | lua_remove(L, -2); /* remove previous table */ | ||
| 405 | return NULL; | ||
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | LUALIB_API const char *luaL_setfield (lua_State *L, const char *fname) { | ||
| 410 | const char *e; | ||
| 411 | lua_insert(L, -2); /* move value to below table */ | ||
| 412 | while ((e = strchr(fname, '.')) != NULL) { | ||
| 413 | lua_pushlstring(L, fname, e - fname); | ||
| 414 | lua_gettable(L, -2); | ||
| 415 | if (lua_isnil(L, -1)) { /* no such field? */ | ||
| 416 | lua_pop(L, 1); /* remove this nil */ | ||
| 417 | lua_newtable(L); /* create a new table for field */ | ||
| 418 | lua_pushlstring(L, fname, e - fname); | ||
| 419 | lua_pushvalue(L, -2); | ||
| 420 | lua_settable(L, -4); /* set new table into field */ | ||
| 421 | } | ||
| 422 | lua_remove(L, -2); /* remove previous table */ | ||
| 423 | fname = e + 1; | ||
| 424 | if (!lua_istable(L, -1)) { | ||
| 425 | lua_pop(L, 2); /* remove table and value */ | ||
| 426 | return fname; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | lua_insert(L, -2); /* move table to below value */ | ||
| 430 | lua_setfield(L, -2, fname); /* set last field */ | ||
| 431 | lua_remove(L, -2); /* remove table */ | ||
| 432 | return NULL; | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | |||
| 394 | /* | 437 | /* |
| 395 | ** {====================================================== | 438 | ** {====================================================== |
| 396 | ** Generic Buffer manipulation | 439 | ** Generic Buffer manipulation |
