Imported from ../lua-3.2.tar.gz.
[lua.git] / src / lauxlib.c
blobdb929c4f90eea65ecdce1c7faedd6d98389917eb
1 /*
2 ** $Id: lauxlib.c,v 1.17 1999/03/11 18:59:19 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 function.
14 ** With care, these functions can be used by other libraries.
17 #include "lauxlib.h"
18 #include "lua.h"
19 #include "luadebug.h"
23 int luaL_findstring (char *name, char *list[]) {
24 int i;
25 for (i=0; list[i]; i++)
26 if (strcmp(list[i], name) == 0)
27 return i;
28 return -1; /* name not found */
31 void luaL_argerror (int numarg, char *extramsg) {
32 lua_Function f = lua_stackedfunction(0);
33 char *funcname;
34 lua_getobjname(f, &funcname);
35 numarg -= lua_nups(f);
36 if (funcname == NULL)
37 funcname = "?";
38 if (extramsg == NULL)
39 luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
40 else
41 luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
42 numarg, funcname, extramsg);
45 char *luaL_check_lstr (int numArg, long *len)
47 lua_Object o = lua_getparam(numArg);
48 luaL_arg_check(lua_isstring(o), numArg, "string expected");
49 if (len) *len = lua_strlen(o);
50 return lua_getstring(o);
53 char *luaL_opt_lstr (int numArg, char *def, long *len)
55 return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
56 luaL_check_lstr(numArg, len);
59 double luaL_check_number (int numArg)
61 lua_Object o = lua_getparam(numArg);
62 luaL_arg_check(lua_isnumber(o), numArg, "number expected");
63 return lua_getnumber(o);
67 double luaL_opt_number (int numArg, double def)
69 return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
70 luaL_check_number(numArg);
74 lua_Object luaL_tablearg (int arg)
76 lua_Object o = lua_getparam(arg);
77 luaL_arg_check(lua_istable(o), arg, "table expected");
78 return o;
81 lua_Object luaL_functionarg (int arg)
83 lua_Object o = lua_getparam(arg);
84 luaL_arg_check(lua_isfunction(o), arg, "function expected");
85 return o;
88 lua_Object luaL_nonnullarg (int numArg)
90 lua_Object o = lua_getparam(numArg);
91 luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
92 return o;
95 void luaL_openlib (struct luaL_reg *l, int n)
97 int i;
98 lua_open(); /* make sure lua is already open */
99 for (i=0; i<n; i++)
100 lua_register(l[i].name, l[i].func);
104 void luaL_verror (char *fmt, ...)
106 char buff[500];
107 va_list argp;
108 va_start(argp, fmt);
109 vsprintf(buff, fmt, argp);
110 va_end(argp);
111 lua_error(buff);
115 void luaL_chunkid (char *out, char *source, int len) {
116 len -= 13; /* 13 = strlen("string ''...\0") */
117 if (*source == '@')
118 sprintf(out, "file `%.*s'", len, source+1);
119 else if (*source == '(')
120 strcpy(out, "(C code)");
121 else {
122 char *b = strchr(source , '\n'); /* stop string at first new line */
123 int lim = (b && (b-source)<len) ? b-source : len;
124 sprintf(out, "string `%.*s'", lim, source);
125 strcpy(out+lim+(13-5), "...'"); /* 5 = strlen("...'\0") */
130 void luaL_filesource (char *out, char *filename, int len) {
131 if (filename == NULL) filename = "(stdin)";
132 sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */