Imported from ../lua-3.1.tar.gz.
[lua.git] / src / lauxlib.c
blob0a972af04ff7c7565f8d838e7dc8e2626452183a
1 /*
2 ** $Id: lauxlib.c,v 1.12 1998/06/19 16:14:09 roberto Exp $
3 ** Auxiliary functions for building Lua libraries
4 ** See Copyright Notice in lua.h
5 */
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <string.h>
12 /* Please Notice: This file uses only the official API of Lua
13 ** Any function declared here could be written as an application
14 ** function. With care, these functions can be used by other libraries.
16 #include "lauxlib.h"
17 #include "lua.h"
18 #include "luadebug.h"
22 int luaL_findstring (char *name, char *list[]) {
23 int i;
24 for (i=0; list[i]; i++)
25 if (strcmp(list[i], name) == 0)
26 return i;
27 return -1; /* name not found */
30 void luaL_argerror (int numarg, char *extramsg)
32 char *funcname;
33 lua_getobjname(lua_stackedfunction(0), &funcname);
34 if (funcname == NULL)
35 funcname = "???";
36 if (extramsg == NULL)
37 luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
38 else
39 luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
40 numarg, funcname, extramsg);
43 char *luaL_check_lstr (int numArg, long *len)
45 lua_Object o = lua_getparam(numArg);
46 luaL_arg_check(lua_isstring(o), numArg, "string expected");
47 if (len) *len = lua_strlen(o);
48 return lua_getstring(o);
51 char *luaL_opt_lstr (int numArg, char *def, long *len)
53 return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
54 luaL_check_lstr(numArg, len);
57 double luaL_check_number (int numArg)
59 lua_Object o = lua_getparam(numArg);
60 luaL_arg_check(lua_isnumber(o), numArg, "number expected");
61 return lua_getnumber(o);
65 double luaL_opt_number (int numArg, double def)
67 return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
68 luaL_check_number(numArg);
72 lua_Object luaL_tablearg (int arg)
74 lua_Object o = lua_getparam(arg);
75 luaL_arg_check(lua_istable(o), arg, "table expected");
76 return o;
79 lua_Object luaL_functionarg (int arg)
81 lua_Object o = lua_getparam(arg);
82 luaL_arg_check(lua_isfunction(o), arg, "function expected");
83 return o;
86 lua_Object luaL_nonnullarg (int numArg)
88 lua_Object o = lua_getparam(numArg);
89 luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
90 return o;
93 void luaL_openlib (struct luaL_reg *l, int n)
95 int i;
96 lua_open(); /* make sure lua is already open */
97 for (i=0; i<n; i++)
98 lua_register(l[i].name, l[i].func);
102 void luaL_verror (char *fmt, ...)
104 char buff[500];
105 va_list argp;
106 va_start(argp, fmt);
107 vsprintf(buff, fmt, argp);
108 va_end(argp);
109 lua_error(buff);