luaL_findstring -> luaL_checkoption

This commit is contained in:
Roberto Ierusalimschy 2005-05-25 10:21:26 -03:00
parent 38da9d568a
commit e8a7ecb982
5 changed files with 18 additions and 18 deletions

View File

@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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; int i;
for (i=0; list[i]; i++) for (i=0; lst[i]; i++)
if (strcmp(list[i], name) == 0) if (strcmp(lst[i], name) == 0)
return i; return i;
return -1; /* name not found */ return luaL_error(L, "invalid option " LUA_QS, name);
} }

View File

@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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 void (luaL_where) (lua_State *L, int lvl);
LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); 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, LUALIB_API const char *(luaL_searchpath) (lua_State *L, const char *name,
const char *path); const char *path);

View File

@ -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 ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -192,9 +192,8 @@ static int luaB_collectgarbage (lua_State *L) {
"count", "step", "setpause", "setstepmul", NULL}; "count", "step", "setpause", "setstepmul", NULL};
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT, static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL}; 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); int ex = luaL_optinteger(L, 2, 0);
luaL_argcheck(L, o >= 0, 1, "invalid option");
lua_pushinteger(L, lua_gc(L, optsnum[o], ex)); lua_pushinteger(L, lua_gc(L, optsnum[o], ex));
return 1; return 1;
} }

View File

@ -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 ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** 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 int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
static const char *const modenames[] = {"set", "cur", "end", NULL}; static const char *const modenames[] = {"set", "cur", "end", NULL};
FILE *f = tofile(L); 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); lua_Integer offset = luaL_optinteger(L, 3, 0);
luaL_argcheck(L, op != -1, 2, "invalid mode");
op = fseek(f, offset, mode[op]); op = fseek(f, offset, mode[op]);
if (op) if (op)
return pushresult(L, 0, NULL); /* error */ 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 int mode[] = {_IONBF, _IOFBF, _IOLBF};
static const char *const modenames[] = {"no", "full", "line", NULL}; static const char *const modenames[] = {"no", "full", "line", NULL};
FILE *f = tofile(L); FILE *f = tofile(L);
int op = luaL_findstring(luaL_checkstring(L, 2), modenames); int op = luaL_checkoption(L, 2, NULL, modenames);
luaL_argcheck(L, op != -1, 2, "invalid mode");
return pushresult(L, setvbuf(f, NULL, mode[op], 0) == 0, NULL); return pushresult(L, setvbuf(f, NULL, mode[op], 0) == 0, NULL);
} }

View File

@ -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 ** Standard Operating System library
** See Copyright Notice in lua.h ** 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", static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
"numeric", "time", NULL}; "numeric", "time", NULL};
const char *l = lua_tostring(L, 1); 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, l || lua_isnoneornil(L, 1), 1, "string expected");
luaL_argcheck(L, op != -1, 2, "invalid option");
lua_pushstring(L, setlocale(cat[op], l)); lua_pushstring(L, setlocale(cat[op], l));
return 1; return 1;
} }