new module 'auxlib' centralizes functions to get/check parameters.

This commit is contained in:
Roberto Ierusalimschy 1997-03-17 14:02:29 -03:00
parent b6d91e24e2
commit eea734aa88
8 changed files with 146 additions and 157 deletions

47
auxlib.c Normal file
View File

@ -0,0 +1,47 @@
char *rcs_auxlib="$Id: $";
#include <stdio.h>
#include "lua.h"
void luaL_arg_check(int cond, char *funcname, int numarg, char *extramsg)
{
if (!cond) {
char buff[100];
if (extramsg == NULL)
sprintf(buff, "bad argument #%d to function `%s'", numarg, funcname);
else
sprintf(buff, "bad argument #%d to function `%s' (%s)",
numarg, funcname, extramsg);
lua_error(buff);
}
}
char *luaL_check_string (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
luaL_arg_check(lua_isstring(o), funcname, numArg, "string expected");
return lua_getstring(o);
}
char *luaL_opt_string (int numArg, char *def, char *funcname)
{
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
luaL_check_string(numArg, funcname);
}
double luaL_check_number (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
luaL_arg_check(lua_isnumber(o), funcname, numArg, "number expected");
return lua_getnumber(o);
}
double luaL_opt_number (int numArg, double def, char *funcname)
{
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
luaL_check_number(numArg, funcname);
}

61
inout.c
View File

@ -5,7 +5,7 @@
** Also provides some predefined lua functions. ** Also provides some predefined lua functions.
*/ */
char *rcs_inout="$Id: inout.c,v 2.44 1997/02/26 17:38:41 roberto Unstable roberto $"; char *rcs_inout="$Id: inout.c,v 2.45 1997/03/11 18:44:28 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -101,32 +101,6 @@ void lua_closestring (void)
} }
static void check_arg (int cond, char *func)
{
if (!cond)
{
char buff[100];
sprintf(buff, "incorrect argument to function `%s'", func);
lua_error(buff);
}
}
static char *check_string (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
check_arg(lua_isstring(o), funcname);
return lua_getstring(o);
}
static int check_number (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
check_arg(lua_isnumber(o), funcname);
return (int)lua_getnumber(o);
}
static int passresults (void) static int passresults (void)
{ {
int arg = 0; int arg = 0;
@ -141,7 +115,7 @@ static int passresults (void)
*/ */
static void lua_internaldostring (void) static void lua_internaldostring (void)
{ {
if (lua_dostring(check_string(1, "dostring")) == 0) if (lua_dostring(luaL_check_string(1, "dostring")) == 0)
if (passresults() == 0) if (passresults() == 0)
lua_pushuserdata(NULL); /* at least one result to signal no errors */ lua_pushuserdata(NULL); /* at least one result to signal no errors */
} }
@ -151,13 +125,7 @@ static void lua_internaldostring (void)
*/ */
static void lua_internaldofile (void) static void lua_internaldofile (void)
{ {
lua_Object obj = lua_getparam (1); char *fname = luaL_opt_string(1, NULL, "dofile");
char *fname = NULL;
if (lua_isstring(obj))
fname = lua_getstring(obj);
else if (obj != LUA_NOOBJECT)
lua_error("invalid argument to function `dofile'");
/* else fname = NULL */
if (lua_dofile(fname) == 0) if (lua_dofile(fname) == 0)
if (passresults() == 0) if (passresults() == 0)
lua_pushuserdata(NULL); /* at least one result to signal no errors */ lua_pushuserdata(NULL); /* at least one result to signal no errors */
@ -247,15 +215,15 @@ static void luaI_assert (void)
static void luaI_setglobal (void) static void luaI_setglobal (void)
{ {
lua_Object value = lua_getparam(2); lua_Object value = lua_getparam(2);
check_arg(value != LUA_NOOBJECT, "setglobal"); luaL_arg_check(value != LUA_NOOBJECT, "setglobal", 2, NULL);
lua_pushobject(value); lua_pushobject(value);
lua_storeglobal(check_string(1, "setglobal")); lua_storeglobal(luaL_check_string(1, "setglobal"));
lua_pushobject(value); /* return given value */ lua_pushobject(value); /* return given value */
} }
static void luaI_getglobal (void) static void luaI_getglobal (void)
{ {
lua_pushobject(lua_getglobal(check_string(1, "getglobal"))); lua_pushobject(lua_getglobal(luaL_check_string(1, "getglobal")));
} }
#define MAXPARAMS 256 #define MAXPARAMS 256
@ -265,8 +233,8 @@ static void luaI_call (void)
lua_Object arg = lua_getparam(2); lua_Object arg = lua_getparam(2);
lua_Object temp, params[MAXPARAMS]; lua_Object temp, params[MAXPARAMS];
int narg, i; int narg, i;
check_arg(lua_istable(arg), "call"); luaL_arg_check(lua_isfunction(f), "call", 1, "function expected");
check_arg(lua_isfunction(f), "call"); luaL_arg_check(lua_istable(arg), "call", 2, "table expected");
/* narg = arg.n */ /* narg = arg.n */
lua_pushobject(arg); lua_pushobject(arg);
lua_pushstring("n"); lua_pushstring("n");
@ -296,21 +264,22 @@ static void luaI_call (void)
static void luaIl_settag (void) static void luaIl_settag (void)
{ {
lua_Object o = lua_getparam(1); lua_Object o = lua_getparam(1);
check_arg(o != LUA_NOOBJECT, "settag"); luaL_arg_check(o != LUA_NOOBJECT, "settag", 1, NULL);
lua_pushobject(o); lua_pushobject(o);
lua_settag(check_number(2, "settag")); lua_settag(luaL_check_number(2, "settag"));
} }
static void luaIl_newtag (void) static void luaIl_newtag (void)
{ {
lua_pushnumber(lua_newtag(check_string(1, "newtag"))); lua_pushnumber(lua_newtag(luaL_check_string(1, "newtag")));
} }
static void basicindex (void) static void basicindex (void)
{ {
lua_Object t = lua_getparam(1); lua_Object t = lua_getparam(1);
lua_Object i = lua_getparam(2); lua_Object i = lua_getparam(2);
check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT, "basicindex"); luaL_arg_check(t != LUA_NOOBJECT, "basicindex", 1, NULL);
luaL_arg_check(i != LUA_NOOBJECT, "basicindex", 2, NULL);
lua_pushobject(t); lua_pushobject(t);
lua_pushobject(i); lua_pushobject(i);
lua_pushobject(lua_basicindex()); lua_pushobject(lua_basicindex());
@ -321,8 +290,8 @@ static void basicstoreindex (void)
lua_Object t = lua_getparam(1); lua_Object t = lua_getparam(1);
lua_Object i = lua_getparam(2); lua_Object i = lua_getparam(2);
lua_Object v = lua_getparam(3); lua_Object v = lua_getparam(3);
check_arg(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT, luaL_arg_check(t != LUA_NOOBJECT && i != LUA_NOOBJECT && v != LUA_NOOBJECT,
"basicindex"); "basicindex", 0, NULL);
lua_pushobject(t); lua_pushobject(t);
lua_pushobject(i); lua_pushobject(i);
lua_pushobject(v); lua_pushobject(v);

24
iolib.c
View File

@ -58,7 +58,7 @@ static void io_readfrom (void)
else if (lua_isuserdata(f)) else if (lua_isuserdata(f))
lua_infile = lua_getuserdata(f); lua_infile = lua_getuserdata(f);
else { else {
char *s = lua_check_string(1, "readfrom"); char *s = luaL_check_string(1, "readfrom");
FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r"); FILE *fp = (*s == '|') ? popen(s+1, "r") : fopen(s, "r");
if (fp) if (fp)
lua_infile = fp; lua_infile = fp;
@ -79,7 +79,7 @@ static void io_writeto (void)
else if (lua_isuserdata(f)) else if (lua_isuserdata(f))
lua_outfile = lua_getuserdata(f); lua_outfile = lua_getuserdata(f);
else { else {
char *s = lua_check_string(1, "writeto"); char *s = luaL_check_string(1, "writeto");
FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w"); FILE *fp = (*s == '|') ? popen(s+1,"w") : fopen(s,"w");
if (fp) if (fp)
lua_outfile = fp; lua_outfile = fp;
@ -94,7 +94,7 @@ static void io_writeto (void)
static void io_appendto (void) static void io_appendto (void)
{ {
char *s = lua_check_string(1, "appendto"); char *s = luaL_check_string(1, "appendto");
FILE *fp = fopen (s, "a"); FILE *fp = fopen (s, "a");
if (fp != NULL) { if (fp != NULL) {
lua_outfile = fp; lua_outfile = fp;
@ -110,7 +110,7 @@ static void io_appendto (void)
static void io_read (void) static void io_read (void)
{ {
char *buff; char *buff;
char *p = lua_opt_string(1, "[^\n]*{\n}", "read"); char *p = luaL_opt_string(1, "[^\n]*{\n}", "read");
int inskip = 0; /* to control {skips} */ int inskip = 0; /* to control {skips} */
int c = NEED_OTHER; int c = NEED_OTHER;
luaI_addchar(0); luaI_addchar(0);
@ -161,7 +161,7 @@ static void io_write (void)
int arg = 1; int arg = 1;
int status = 1; int status = 1;
char *s; char *s;
while ((s = lua_opt_string(arg++, NULL, "write")) != NULL) while ((s = luaL_opt_string(arg++, NULL, "write")) != NULL)
status = status && (fputs(s, lua_outfile) != EOF); status = status && (fputs(s, lua_outfile) != EOF);
pushresult(status); pushresult(status);
} }
@ -169,20 +169,20 @@ static void io_write (void)
static void io_execute (void) static void io_execute (void)
{ {
lua_pushnumber(system(lua_check_string(1, "execute"))); lua_pushnumber(system(luaL_check_string(1, "execute")));
} }
static void io_remove (void) static void io_remove (void)
{ {
pushresult(remove(lua_check_string(1, "remove")) == 0); pushresult(remove(luaL_check_string(1, "remove")) == 0);
} }
static void io_rename (void) static void io_rename (void)
{ {
pushresult(rename(lua_check_string(1, "rename"), pushresult(rename(luaL_check_string(1, "rename"),
lua_check_string(2, "rename")) == 0); luaL_check_string(2, "rename")) == 0);
} }
@ -195,7 +195,7 @@ static void io_tmpname (void)
static void io_getenv (void) static void io_getenv (void)
{ {
lua_pushstring(getenv(lua_check_string(1, "getenv"))); /* if NULL push nil */ lua_pushstring(getenv(luaL_check_string(1, "getenv"))); /* if NULL push nil */
} }
@ -203,7 +203,7 @@ static void io_date (void)
{ {
time_t t; time_t t;
struct tm *tm; struct tm *tm;
char *s = lua_opt_string(1, "%c", "date"); char *s = luaL_opt_string(1, "%c", "date");
char b[BUFSIZ]; char b[BUFSIZ];
time(&t); tm = localtime(&t); time(&t); tm = localtime(&t);
if (strftime(b,sizeof(b),s,tm)) if (strftime(b,sizeof(b),s,tm))
@ -269,7 +269,7 @@ static void lua_printstack (FILE *f)
static void errorfb (void) static void errorfb (void)
{ {
char *s = lua_opt_string(1, "(no messsage)", NULL); char *s = luaL_opt_string(1, "(no messsage)", NULL);
fprintf(stderr, "lua: %s\n", s); fprintf(stderr, "lua: %s\n", s);
lua_printstack(stderr); lua_printstack(stderr);
} }

13
lua.h
View File

@ -2,7 +2,7 @@
** LUA - Linguagem para Usuarios de Aplicacao ** LUA - Linguagem para Usuarios de Aplicacao
** Grupo de Tecnologia em Computacao Grafica ** Grupo de Tecnologia em Computacao Grafica
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: lua.h,v 3.34 1997/02/20 15:51:14 roberto Exp roberto $ ** $Id: lua.h,v 3.35 1997/02/26 17:38:41 roberto Unstable roberto $
*/ */
@ -80,6 +80,7 @@ void lua_unref (int ref);
lua_Object lua_createtable (void); lua_Object lua_createtable (void);
/* =============================================================== */
/* some useful macros */ /* some useful macros */
#define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l)) #define lua_refobject(o,l) (lua_pushobject(o), lua_ref(l))
@ -89,7 +90,17 @@ lua_Object lua_createtable (void);
#define lua_pushuserdata(u) lua_pushusertag(u, 0) #define lua_pushuserdata(u) lua_pushusertag(u, 0)
/* =============================================================== */
/* Auxiliar functions for libraries */
void luaL_arg_check(int cond, char *funcname, int numarg, char *extramsg);
char *luaL_check_string (int numArg, char *funcname);
char *luaL_opt_string (int numArg, char *def, char *funcname);
double luaL_check_number (int numArg, char *funcname);
double luaL_opt_number (int numArg, double def, char *funcname);
/* =============================================================== */
/* for compatibility with old versions. Avoid using these macros */ /* for compatibility with old versions. Avoid using these macros */
#define lua_type(o) (lua_tag(o)) #define lua_type(o) (lua_tag(o))

View File

@ -2,7 +2,7 @@
** Libraries to be used in LUA programs ** Libraries to be used in LUA programs
** Grupo de Tecnologia em Computacao Grafica ** Grupo de Tecnologia em Computacao Grafica
** TeCGraf - PUC-Rio ** TeCGraf - PUC-Rio
** $Id: lualib.h,v 1.9 1996/08/01 14:55:33 roberto Exp roberto $ ** $Id: lualib.h,v 1.10 1996/08/05 20:55:24 roberto Exp roberto $
*/ */
#ifndef lualib_h #ifndef lualib_h
@ -23,11 +23,6 @@ struct lua_reg {
}; };
void luaI_openlib (struct lua_reg *l, int n); void luaI_openlib (struct lua_reg *l, int n);
void lua_arg_check(int cond, char *funcname);
char *lua_check_string (int numArg, char *funcname);
char *lua_opt_string (int numArg, char *def, char *funcname);
double lua_check_number (int numArg, char *funcname);
long lua_opt_number (int numArg, long def, char *funcname);
char *luaI_addchar (int c); char *luaI_addchar (int c);
void luaI_addquoted (char *s); void luaI_addquoted (char *s);

View File

@ -1,4 +1,4 @@
# $Id: makefile,v 1.27 1996/11/06 20:48:03 roberto Exp roberto $ # $Id: makefile,v 1.28 1997/03/05 13:37:04 roberto Exp roberto $
#configuration #configuration
@ -30,7 +30,8 @@ LUAOBJS = \
fallback.o \ fallback.o \
mem.o \ mem.o \
func.o \ func.o \
undump.o undump.o \
auxlib.o
LIBOBJS = \ LIBOBJS = \
iolib.o \ iolib.o \
@ -75,6 +76,7 @@ clear :
co $@ co $@
auxlib.o: auxlib.c lua.h
fallback.o: fallback.c mem.h fallback.h lua.h opcode.h types.h tree.h \ fallback.o: fallback.c mem.h fallback.h lua.h opcode.h types.h tree.h \
func.h table.h hash.h func.h table.h hash.h
func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \ func.o: func.c luadebug.h lua.h table.h tree.h types.h opcode.h func.h \
@ -97,6 +99,6 @@ strlib.o: strlib.c lua.h lualib.h
table.o: table.c mem.h opcode.h lua.h types.h tree.h func.h hash.h \ table.o: table.c mem.h opcode.h lua.h types.h tree.h func.h hash.h \
table.h inout.h fallback.h luadebug.h table.h inout.h fallback.h luadebug.h
tree.o: tree.c mem.h lua.h tree.h types.h lex.h hash.h opcode.h func.h \ tree.o: tree.c mem.h lua.h tree.h types.h lex.h hash.h opcode.h func.h \
table.h table.h fallback.h
undump.o: undump.c opcode.h lua.h types.h tree.h func.h mem.h table.h \ undump.o: undump.c opcode.h lua.h types.h tree.h func.h mem.h table.h \
undump.h undump.h

View File

@ -3,7 +3,7 @@
** Mathematics library to LUA ** Mathematics library to LUA
*/ */
char *rcs_mathlib="$Id: mathlib.c,v 1.17 1996/04/30 21:13:55 roberto Exp roberto $"; char *rcs_mathlib="$Id: mathlib.c,v 1.18 1996/08/01 14:55:33 roberto Exp roberto $";
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
@ -19,7 +19,7 @@ char *rcs_mathlib="$Id: mathlib.c,v 1.17 1996/04/30 21:13:55 roberto Exp roberto
static void math_abs (void) static void math_abs (void)
{ {
double d = lua_check_number(1, "abs"); double d = luaL_check_number(1, "abs");
if (d < 0) d = -d; if (d < 0) d = -d;
lua_pushnumber (d); lua_pushnumber (d);
} }
@ -27,7 +27,7 @@ static void math_abs (void)
static void math_sin (void) static void math_sin (void)
{ {
double d = lua_check_number(1, "sin"); double d = luaL_check_number(1, "sin");
lua_pushnumber (sin(TORAD(d))); lua_pushnumber (sin(TORAD(d)));
} }
@ -35,7 +35,7 @@ static void math_sin (void)
static void math_cos (void) static void math_cos (void)
{ {
double d = lua_check_number(1, "cos"); double d = luaL_check_number(1, "cos");
lua_pushnumber (cos(TORAD(d))); lua_pushnumber (cos(TORAD(d)));
} }
@ -43,64 +43,64 @@ static void math_cos (void)
static void math_tan (void) static void math_tan (void)
{ {
double d = lua_check_number(1, "tan"); double d = luaL_check_number(1, "tan");
lua_pushnumber (tan(TORAD(d))); lua_pushnumber (tan(TORAD(d)));
} }
static void math_asin (void) static void math_asin (void)
{ {
double d = lua_check_number(1, "asin"); double d = luaL_check_number(1, "asin");
lua_pushnumber (TODEGREE(asin(d))); lua_pushnumber (TODEGREE(asin(d)));
} }
static void math_acos (void) static void math_acos (void)
{ {
double d = lua_check_number(1, "acos"); double d = luaL_check_number(1, "acos");
lua_pushnumber (TODEGREE(acos(d))); lua_pushnumber (TODEGREE(acos(d)));
} }
static void math_atan (void) static void math_atan (void)
{ {
double d = lua_check_number(1, "atan"); double d = luaL_check_number(1, "atan");
lua_pushnumber (TODEGREE(atan(d))); lua_pushnumber (TODEGREE(atan(d)));
} }
static void math_atan2 (void) static void math_atan2 (void)
{ {
double d1 = lua_check_number(1, "atan2"); double d1 = luaL_check_number(1, "atan2");
double d2 = lua_check_number(2, "atan2"); double d2 = luaL_check_number(2, "atan2");
lua_pushnumber (TODEGREE(atan2(d1, d2))); lua_pushnumber (TODEGREE(atan2(d1, d2)));
} }
static void math_ceil (void) static void math_ceil (void)
{ {
double d = lua_check_number(1, "ceil"); double d = luaL_check_number(1, "ceil");
lua_pushnumber (ceil(d)); lua_pushnumber (ceil(d));
} }
static void math_floor (void) static void math_floor (void)
{ {
double d = lua_check_number(1, "floor"); double d = luaL_check_number(1, "floor");
lua_pushnumber (floor(d)); lua_pushnumber (floor(d));
} }
static void math_mod (void) static void math_mod (void)
{ {
float x = lua_check_number(1, "mod"); float x = luaL_check_number(1, "mod");
float y = lua_check_number(2, "mod"); float y = luaL_check_number(2, "mod");
lua_pushnumber(fmod(x, y)); lua_pushnumber(fmod(x, y));
} }
static void math_sqrt (void) static void math_sqrt (void)
{ {
double d = lua_check_number(1, "sqrt"); double d = luaL_check_number(1, "sqrt");
lua_pushnumber (sqrt(d)); lua_pushnumber (sqrt(d));
} }
@ -131,10 +131,10 @@ static void math_pow (void)
static void math_min (void) static void math_min (void)
{ {
int i=1; int i=1;
double dmin = lua_check_number(i, "min"); double dmin = luaL_check_number(i, "min");
while (lua_getparam(++i) != LUA_NOOBJECT) while (lua_getparam(++i) != LUA_NOOBJECT)
{ {
double d = lua_check_number(i, "min"); double d = luaL_check_number(i, "min");
if (d < dmin) dmin = d; if (d < dmin) dmin = d;
} }
lua_pushnumber (dmin); lua_pushnumber (dmin);
@ -143,10 +143,10 @@ static void math_min (void)
static void math_max (void) static void math_max (void)
{ {
int i=1; int i=1;
double dmax = lua_check_number(i, "max"); double dmax = luaL_check_number(i, "max");
while (lua_getparam(++i) != LUA_NOOBJECT) while (lua_getparam(++i) != LUA_NOOBJECT)
{ {
double d = lua_check_number(i, "max"); double d = luaL_check_number(i, "max");
if (d > dmax) dmax = d; if (d > dmax) dmax = d;
} }
lua_pushnumber (dmax); lua_pushnumber (dmax);
@ -154,33 +154,33 @@ static void math_max (void)
static void math_log (void) static void math_log (void)
{ {
double d = lua_check_number(1, "log"); double d = luaL_check_number(1, "log");
lua_pushnumber (log(d)); lua_pushnumber (log(d));
} }
static void math_log10 (void) static void math_log10 (void)
{ {
double d = lua_check_number(1, "log10"); double d = luaL_check_number(1, "log10");
lua_pushnumber (log10(d)); lua_pushnumber (log10(d));
} }
static void math_exp (void) static void math_exp (void)
{ {
double d = lua_check_number(1, "exp"); double d = luaL_check_number(1, "exp");
lua_pushnumber (exp(d)); lua_pushnumber (exp(d));
} }
static void math_deg (void) static void math_deg (void)
{ {
float d = lua_check_number(1, "deg"); float d = luaL_check_number(1, "deg");
lua_pushnumber (d*180./PI); lua_pushnumber (d*180./PI);
} }
static void math_rad (void) static void math_rad (void)
{ {
float d = lua_check_number(1, "rad"); float d = luaL_check_number(1, "rad");
lua_pushnumber (d/180.*PI); lua_pushnumber (d/180.*PI);
} }
@ -191,7 +191,7 @@ static void math_random (void)
static void math_randomseed (void) static void math_randomseed (void)
{ {
srand(lua_check_number(1, "randomseed")); srand(luaL_check_number(1, "randomseed"));
} }

View File

@ -3,7 +3,7 @@
** String library to LUA ** String library to LUA
*/ */
char *rcs_strlib="$Id: strlib.c,v 1.34 1996/11/22 13:08:02 roberto Exp roberto $"; char *rcs_strlib="$Id: strlib.c,v 1.35 1997/02/21 15:21:34 roberto Exp roberto $";
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
@ -42,41 +42,6 @@ static char *openspace (unsigned long size)
return buff+lbuffer.size; return buff+lbuffer.size;
} }
void lua_arg_check(int cond, char *funcname)
{
if (!cond) {
char buff[100];
sprintf(buff, "incorrect argument to function `%s'", funcname);
lua_error(buff);
}
}
char *lua_check_string (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
lua_arg_check(lua_isstring(o), funcname);
return lua_getstring(o);
}
char *lua_opt_string (int numArg, char *def, char *funcname)
{
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
lua_check_string(numArg, funcname);
}
double lua_check_number (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
lua_arg_check(lua_isnumber(o), funcname);
return lua_getnumber(o);
}
long lua_opt_number (int numArg, long def, char *funcname)
{
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
(long)lua_check_number(numArg, funcname);
}
char *luaI_addchar (int c) char *luaI_addchar (int c)
{ {
if (lbuffer.size >= lbuffer.max) if (lbuffer.size >= lbuffer.max)
@ -104,8 +69,8 @@ static void addstr (char *s)
*/ */
static void str_tok (void) static void str_tok (void)
{ {
char *s1 = lua_check_string(1, "strtok"); char *s1 = luaL_check_string(1, "strtok");
char *del = lua_check_string(2, "strtok"); char *del = luaL_check_string(2, "strtok");
lua_Object t = lua_createtable(); lua_Object t = lua_createtable();
int i = 1; int i = 1;
/* As strtok changes s1, and s1 is "constant", make a copy of it */ /* As strtok changes s1, and s1 is "constant", make a copy of it */
@ -127,7 +92,7 @@ static void str_tok (void)
*/ */
static void str_len (void) static void str_len (void)
{ {
lua_pushnumber(strlen(lua_check_string(1, "strlen"))); lua_pushnumber(strlen(luaL_check_string(1, "strlen")));
} }
/* /*
@ -135,9 +100,9 @@ static void str_len (void)
*/ */
static void str_sub (void) static void str_sub (void)
{ {
char *s = lua_check_string(1, "strsub"); char *s = luaL_check_string(1, "strsub");
long start = (long)lua_check_number(2, "strsub"); long start = (long)luaL_check_number(2, "strsub");
long end = lua_opt_number(3, strlen(s), "strsub"); long end = (long)luaL_opt_number(3, strlen(s), "strsub");
if (1 <= start && start <= end && end <= strlen(s)) { if (1 <= start && start <= end && end <= strlen(s)) {
luaI_addchar(0); luaI_addchar(0);
addnchar(s+start-1, end-start+1); addnchar(s+start-1, end-start+1);
@ -151,7 +116,7 @@ static void str_sub (void)
*/ */
static void str_lower (void) static void str_lower (void)
{ {
char *s = lua_check_string(1, "strlower"); char *s = luaL_check_string(1, "strlower");
luaI_addchar(0); luaI_addchar(0);
while (*s) while (*s)
luaI_addchar(tolower((unsigned char)*s++)); luaI_addchar(tolower((unsigned char)*s++));
@ -163,7 +128,7 @@ static void str_lower (void)
*/ */
static void str_upper (void) static void str_upper (void)
{ {
char *s = lua_check_string(1, "strupper"); char *s = luaL_check_string(1, "strupper");
luaI_addchar(0); luaI_addchar(0);
while (*s) while (*s)
luaI_addchar(toupper((unsigned char)*s++)); luaI_addchar(toupper((unsigned char)*s++));
@ -172,8 +137,8 @@ static void str_upper (void)
static void str_rep (void) static void str_rep (void)
{ {
char *s = lua_check_string(1, "strrep"); char *s = luaL_check_string(1, "strrep");
int n = (int)lua_check_number(2, "strrep"); int n = (int)luaL_check_number(2, "strrep");
luaI_addchar(0); luaI_addchar(0);
while (n-- > 0) while (n-- > 0)
addstr(s); addstr(s);
@ -185,9 +150,9 @@ static void str_rep (void)
*/ */
static void str_ascii (void) static void str_ascii (void)
{ {
char *s = lua_check_string(1, "ascii"); char *s = luaL_check_string(1, "ascii");
long pos = lua_opt_number(2, 1, "ascii") - 1; long pos = (long)luaL_opt_number(2, 1, "ascii") - 1;
lua_arg_check(0<=pos && pos<strlen(s), "ascii"); luaL_arg_check(0<=pos && pos<strlen(s), "ascii", 2, "out of range");
lua_pushnumber((unsigned char)s[pos]); lua_pushnumber((unsigned char)s[pos]);
} }
@ -394,10 +359,10 @@ static char *match (char *s, char *p, int level)
static void str_find (void) static void str_find (void)
{ {
char *s = lua_check_string(1, "strfind"); char *s = luaL_check_string(1, "strfind");
char *p = lua_check_string(2, "strfind"); char *p = luaL_check_string(2, "strfind");
long init = lua_opt_number(3, 1, "strfind") - 1; long init = (long)luaL_opt_number(3, 1, "strfind") - 1;
lua_arg_check(0 <= init && init <= strlen(s), "strfind"); luaL_arg_check(0 <= init && init <= strlen(s), "strfind", 3, "out of range");
if (lua_getparam(4) != LUA_NOOBJECT || if (lua_getparam(4) != LUA_NOOBJECT ||
strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */ strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */
char *s2 = strstr(s+init, p); char *s2 = strstr(s+init, p);
@ -450,15 +415,15 @@ static void add_s (lua_Object newp)
addstr(lua_isstring(res) ? lua_getstring(res) : ""); addstr(lua_isstring(res) ? lua_getstring(res) : "");
lua_endblock(); lua_endblock();
} }
else lua_arg_check(0, "gsub"); else luaL_arg_check(0, "gsub", 3, NULL);
} }
static void str_gsub (void) static void str_gsub (void)
{ {
char *src = lua_check_string(1, "gsub"); char *src = luaL_check_string(1, "gsub");
char *p = lua_check_string(2, "gsub"); char *p = luaL_check_string(2, "gsub");
lua_Object newp = lua_getparam(3); lua_Object newp = lua_getparam(3);
int max_s = lua_opt_number(4, strlen(src)+1, "gsub"); int max_s = (int)luaL_opt_number(4, strlen(src)+1, "gsub");
int anchor = (*p == '^') ? (p++, 1) : 0; int anchor = (*p == '^') ? (p++, 1) : 0;
int n = 0; int n = 0;
luaI_addchar(0); luaI_addchar(0);
@ -482,9 +447,9 @@ static void str_gsub (void)
static void str_set (void) static void str_set (void)
{ {
char *item = lua_check_string(1, "strset"); char *item = luaL_check_string(1, "strset");
int i; int i;
lua_arg_check(*item_end(item) == 0, "strset"); luaL_arg_check(*item_end(item) == 0, "strset", 1, "wrong format");
luaI_addchar(0); luaI_addchar(0);
for (i=1; i<256; i++) /* 0 cannot be part of a set */ for (i=1; i<256; i++) /* 0 cannot be part of a set */
if (singlematch(i, item)) if (singlematch(i, item))
@ -509,7 +474,7 @@ void luaI_addquoted (char *s)
static void str_format (void) static void str_format (void)
{ {
int arg = 1; int arg = 1;
char *strfrmt = lua_check_string(arg++, "format"); char *strfrmt = luaL_check_string(arg++, "format");
luaI_addchar(0); /* initialize */ luaI_addchar(0); /* initialize */
while (*strfrmt) { while (*strfrmt) {
if (*strfrmt != '%') if (*strfrmt != '%')
@ -528,20 +493,20 @@ static void str_format (void)
buff = openspace(1000); /* to store the formated value */ buff = openspace(1000); /* to store the formated value */
switch (*strfrmt++) { switch (*strfrmt++) {
case 'q': case 'q':
luaI_addquoted(lua_check_string(arg++, "format")); luaI_addquoted(luaL_check_string(arg++, "format"));
continue; continue;
case 's': { case 's': {
char *s = lua_check_string(arg++, "format"); char *s = luaL_check_string(arg++, "format");
buff = openspace(strlen(s)); buff = openspace(strlen(s));
sprintf(buff, form, s); sprintf(buff, form, s);
break; break;
} }
case 'c': case 'd': case 'i': case 'o': case 'c': case 'd': case 'i': case 'o':
case 'u': case 'x': case 'X': case 'u': case 'x': case 'X':
sprintf(buff, form, (int)lua_check_number(arg++, "format")); sprintf(buff, form, (int)luaL_check_number(arg++, "format"));
break; break;
case 'e': case 'E': case 'f': case 'g': case 'e': case 'E': case 'f': case 'g':
sprintf(buff, form, lua_check_number(arg++, "format")); sprintf(buff, form, luaL_check_number(arg++, "format"));
break; break;
default: /* also treat cases 'pnLlh' */ default: /* also treat cases 'pnLlh' */
lua_error("invalid format option in function `format'"); lua_error("invalid format option in function `format'");