lauxlib.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. ** $Id: lauxlib.c,v 1.11 1998/06/18 16:57:03 roberto Exp roberto $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. /* Please Notice: This file uses only the official API of Lua
  10. ** Any function declared here could be written as an application
  11. ** function. With care, these functions can be used by other libraries.
  12. */
  13. #include "lauxlib.h"
  14. #include "lua.h"
  15. #include "luadebug.h"
  16. int luaL_findstring (char *name, char *list[]) {
  17. int i;
  18. for (i=0; list[i]; i++)
  19. if (strcmp(list[i], name) == 0)
  20. return i;
  21. return -1; /* name not found */
  22. }
  23. void luaL_argerror (int numarg, char *extramsg)
  24. {
  25. char *funcname;
  26. lua_getobjname(lua_stackedfunction(0), &funcname);
  27. if (funcname == NULL)
  28. funcname = "???";
  29. if (extramsg == NULL)
  30. luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
  31. else
  32. luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
  33. numarg, funcname, extramsg);
  34. }
  35. char *luaL_check_lstr (int numArg, long *len)
  36. {
  37. lua_Object o = lua_getparam(numArg);
  38. luaL_arg_check(lua_isstring(o), numArg, "string expected");
  39. if (len) *len = lua_strlen(o);
  40. return lua_getstring(o);
  41. }
  42. char *luaL_opt_lstr (int numArg, char *def, long *len)
  43. {
  44. return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
  45. luaL_check_lstr(numArg, len);
  46. }
  47. double luaL_check_number (int numArg)
  48. {
  49. lua_Object o = lua_getparam(numArg);
  50. luaL_arg_check(lua_isnumber(o), numArg, "number expected");
  51. return lua_getnumber(o);
  52. }
  53. double luaL_opt_number (int numArg, double def)
  54. {
  55. return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
  56. luaL_check_number(numArg);
  57. }
  58. lua_Object luaL_tablearg (int arg)
  59. {
  60. lua_Object o = lua_getparam(arg);
  61. luaL_arg_check(lua_istable(o), arg, "table expected");
  62. return o;
  63. }
  64. lua_Object luaL_functionarg (int arg)
  65. {
  66. lua_Object o = lua_getparam(arg);
  67. luaL_arg_check(lua_isfunction(o), arg, "function expected");
  68. return o;
  69. }
  70. lua_Object luaL_nonnullarg (int numArg)
  71. {
  72. lua_Object o = lua_getparam(numArg);
  73. luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
  74. return o;
  75. }
  76. void luaL_openlib (struct luaL_reg *l, int n)
  77. {
  78. int i;
  79. lua_open(); /* make sure lua is already open */
  80. for (i=0; i<n; i++)
  81. lua_register(l[i].name, l[i].func);
  82. }
  83. void luaL_verror (char *fmt, ...)
  84. {
  85. char buff[500];
  86. va_list argp;
  87. va_start(argp, fmt);
  88. vsprintf(buff, fmt, argp);
  89. va_end(argp);
  90. lua_error(buff);
  91. }