From e8a7ecb982effdfc1148098b288c6ac7130f756e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 25 May 2005 10:21:26 -0300 Subject: luaL_findstring -> luaL_checkoption --- lauxlib.c | 13 ++++++++----- lauxlib.h | 5 +++-- lbaselib.c | 5 ++--- liolib.c | 8 +++----- loslib.c | 5 ++--- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 0c64f9e5..b8205191 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.132 2005/05/16 21:19:00 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.133 2005/05/17 19:49:15 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -95,12 +95,15 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { /* }====================================================== */ -LUALIB_API int luaL_findstring (const char *name, const char *const list[]) { +LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def, + const char *const lst[]) { + const char *name = (def) ? luaL_optstring(L, narg, def) : + luaL_checkstring(L, narg); int i; - for (i=0; list[i]; i++) - if (strcmp(list[i], name) == 0) + for (i=0; lst[i]; i++) + if (strcmp(lst[i], name) == 0) return i; - return -1; /* name not found */ + return luaL_error(L, "invalid option " LUA_QS, name); } diff --git a/lauxlib.h b/lauxlib.h index 323798bc..2fcb193c 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.75 2005/03/29 16:20:48 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.76 2005/05/20 19:09:05 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -59,7 +59,8 @@ LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname); LUALIB_API void (luaL_where) (lua_State *L, int lvl); 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_checkoption) (lua_State *L, int narg, const char *def, + const char *const lst[]); LUALIB_API const char *(luaL_searchpath) (lua_State *L, const char *name, const char *path); diff --git a/lbaselib.c b/lbaselib.c index 1fea255f..0eee03dd 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.176 2005/05/17 19:49:15 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.177 2005/05/20 15:53:42 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -192,9 +192,8 @@ static int luaB_collectgarbage (lua_State *L) { "count", "step", "setpause", "setstepmul", NULL}; static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; - int o = luaL_findstring(luaL_optstring(L, 1, "collect"), opts); + int o = luaL_checkoption(L, 1, "collect", opts); int ex = luaL_optinteger(L, 2, 0); - luaL_argcheck(L, o >= 0, 1, "invalid option"); lua_pushinteger(L, lua_gc(L, optsnum[o], ex)); return 1; } diff --git a/liolib.c b/liolib.c index f78f0245..692dd666 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 2.59 2005/03/18 18:01:14 roberto Exp roberto $ +** $Id: liolib.c,v 2.60 2005/05/16 21:19:00 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -400,9 +400,8 @@ static int f_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; FILE *f = tofile(L); - int op = luaL_findstring(luaL_optstring(L, 2, "cur"), modenames); + int op = luaL_checkoption(L, 2, "cur", modenames); lua_Integer offset = luaL_optinteger(L, 3, 0); - luaL_argcheck(L, op != -1, 2, "invalid mode"); op = fseek(f, offset, mode[op]); if (op) return pushresult(L, 0, NULL); /* error */ @@ -417,8 +416,7 @@ static int f_setvbuf (lua_State *L) { static const int mode[] = {_IONBF, _IOFBF, _IOLBF}; static const char *const modenames[] = {"no", "full", "line", NULL}; FILE *f = tofile(L); - int op = luaL_findstring(luaL_checkstring(L, 2), modenames); - luaL_argcheck(L, op != -1, 2, "invalid mode"); + int op = luaL_checkoption(L, 2, NULL, modenames); return pushresult(L, setvbuf(f, NULL, mode[op], 0) == 0, NULL); } diff --git a/loslib.c b/loslib.c index 0d3b7047..48dc9179 100644 --- a/loslib.c +++ b/loslib.c @@ -1,5 +1,5 @@ /* -** $Id: loslib.c,v 1.8 2005/05/16 21:19:00 roberto Exp roberto $ +** $Id: loslib.c,v 1.9 2005/05/17 19:49:15 roberto Exp roberto $ ** Standard Operating System library ** See Copyright Notice in lua.h */ @@ -197,9 +197,8 @@ static int io_setloc (lua_State *L) { static const char *const catnames[] = {"all", "collate", "ctype", "monetary", "numeric", "time", NULL}; const char *l = lua_tostring(L, 1); - int op = luaL_findstring(luaL_optstring(L, 2, "all"), catnames); + int op = luaL_checkoption(L, 2, "all", catnames); luaL_argcheck(L, l || lua_isnoneornil(L, 1), 1, "string expected"); - luaL_argcheck(L, op != -1, 2, "invalid option"); lua_pushstring(L, setlocale(cat[op], l)); return 1; } -- cgit v1.2.3-55-g6feb