1997-09-16 23:25:59 +04:00
|
|
|
/*
|
1999-03-05 00:23:39 +03:00
|
|
|
** $Id: lauxlib.c,v 1.14 1999/02/25 19:13:56 roberto Exp roberto $
|
1998-06-19 20:14:09 +04:00
|
|
|
** Auxiliary functions for building Lua libraries
|
1997-09-16 23:25:59 +04:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
1997-03-17 20:02:29 +03:00
|
|
|
|
1997-03-18 18:30:50 +03:00
|
|
|
#include <stdarg.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
#include <stdio.h>
|
1998-06-18 20:57:03 +04:00
|
|
|
#include <string.h>
|
1997-03-17 20:02:29 +03:00
|
|
|
|
1998-06-19 20:14:09 +04:00
|
|
|
/* Please Notice: This file uses only the official API of Lua
|
1999-02-26 00:07:26 +03:00
|
|
|
** Any function declared here could be written as an application function.
|
|
|
|
** With care, these functions can be used by other libraries.
|
1998-01-09 18:09:53 +03:00
|
|
|
*/
|
1999-02-26 00:07:26 +03:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
#include "lauxlib.h"
|
1997-03-17 20:02:29 +03:00
|
|
|
#include "lua.h"
|
1997-04-06 18:08:08 +04:00
|
|
|
#include "luadebug.h"
|
1997-03-17 20:02:29 +03:00
|
|
|
|
|
|
|
|
1997-04-07 18:48:53 +04:00
|
|
|
|
1998-06-18 20:57:03 +04:00
|
|
|
int luaL_findstring (char *name, char *list[]) {
|
|
|
|
int i;
|
|
|
|
for (i=0; list[i]; i++)
|
|
|
|
if (strcmp(list[i], name) == 0)
|
|
|
|
return i;
|
|
|
|
return -1; /* name not found */
|
|
|
|
}
|
|
|
|
|
1998-09-07 22:59:59 +04:00
|
|
|
void luaL_argerror (int numarg, char *extramsg) {
|
|
|
|
lua_Function f = lua_stackedfunction(0);
|
1998-01-09 18:09:53 +03:00
|
|
|
char *funcname;
|
1998-09-07 22:59:59 +04:00
|
|
|
lua_getobjname(f, &funcname);
|
|
|
|
numarg -= lua_nups(f);
|
1998-01-09 18:09:53 +03:00
|
|
|
if (funcname == NULL)
|
1999-03-05 00:23:39 +03:00
|
|
|
funcname = "?";
|
1998-01-09 18:09:53 +03:00
|
|
|
if (extramsg == NULL)
|
|
|
|
luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
|
|
|
|
else
|
|
|
|
luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
|
|
|
|
numarg, funcname, extramsg);
|
1997-03-17 20:02:29 +03:00
|
|
|
}
|
|
|
|
|
1998-03-06 19:54:42 +03:00
|
|
|
char *luaL_check_lstr (int numArg, long *len)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
1997-04-06 18:08:08 +04:00
|
|
|
luaL_arg_check(lua_isstring(o), numArg, "string expected");
|
1998-03-06 21:47:42 +03:00
|
|
|
if (len) *len = lua_strlen(o);
|
1997-03-17 20:02:29 +03:00
|
|
|
return lua_getstring(o);
|
|
|
|
}
|
|
|
|
|
1998-03-06 19:54:42 +03:00
|
|
|
char *luaL_opt_lstr (int numArg, char *def, long *len)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
1998-03-06 19:54:42 +03:00
|
|
|
luaL_check_lstr(numArg, len);
|
1997-03-17 20:02:29 +03:00
|
|
|
}
|
|
|
|
|
1997-04-06 18:08:08 +04:00
|
|
|
double luaL_check_number (int numArg)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
1997-04-06 18:08:08 +04:00
|
|
|
luaL_arg_check(lua_isnumber(o), numArg, "number expected");
|
1997-03-17 20:02:29 +03:00
|
|
|
return lua_getnumber(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-04-06 18:08:08 +04:00
|
|
|
double luaL_opt_number (int numArg, double def)
|
1997-03-17 20:02:29 +03:00
|
|
|
{
|
|
|
|
return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
|
1997-04-06 18:08:08 +04:00
|
|
|
luaL_check_number(numArg);
|
1997-12-09 16:50:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
lua_Object luaL_tablearg (int arg)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(arg);
|
|
|
|
luaL_arg_check(lua_istable(o), arg, "table expected");
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_Object luaL_functionarg (int arg)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(arg);
|
|
|
|
luaL_arg_check(lua_isfunction(o), arg, "function expected");
|
|
|
|
return o;
|
1997-03-17 20:02:29 +03:00
|
|
|
}
|
|
|
|
|
1997-09-26 19:02:26 +04:00
|
|
|
lua_Object luaL_nonnullarg (int numArg)
|
|
|
|
{
|
|
|
|
lua_Object o = lua_getparam(numArg);
|
|
|
|
luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
1997-03-18 18:30:50 +03:00
|
|
|
void luaL_openlib (struct luaL_reg *l, int n)
|
|
|
|
{
|
|
|
|
int i;
|
1997-11-04 18:27:53 +03:00
|
|
|
lua_open(); /* make sure lua is already open */
|
1997-03-18 18:30:50 +03:00
|
|
|
for (i=0; i<n; i++)
|
|
|
|
lua_register(l[i].name, l[i].func);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void luaL_verror (char *fmt, ...)
|
|
|
|
{
|
1997-09-16 23:25:59 +04:00
|
|
|
char buff[500];
|
1997-03-18 18:30:50 +03:00
|
|
|
va_list argp;
|
|
|
|
va_start(argp, fmt);
|
|
|
|
vsprintf(buff, fmt, argp);
|
|
|
|
va_end(argp);
|
|
|
|
lua_error(buff);
|
|
|
|
}
|
1997-09-26 19:02:26 +04:00
|
|
|
|
1999-03-05 00:23:39 +03:00
|
|
|
|
|
|
|
void luaL_chunkid (char *out, char *source, int len) {
|
|
|
|
len -= 13; /* 13 = strlen("string ''...\0") */
|
|
|
|
if (*source == '@')
|
|
|
|
sprintf(out, "file `%.*s'", len, source+1);
|
|
|
|
else if (*source == '(')
|
|
|
|
strcpy(out, "(C code)");
|
|
|
|
else {
|
|
|
|
char *b = strchr(source , '\n'); /* stop string at first new line */
|
|
|
|
int lim = (b && (b-source)<len) ? b-source : len;
|
|
|
|
sprintf(out, "string `%.*s'", lim, source);
|
|
|
|
strcpy(out+lim+(13-5), "...'"); /* 5 = strlen("...'\0") */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|