lauxlib.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. ** $Id: lauxlib.c,v 1.16 1999/03/10 14:19:41 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 function.
  11. ** 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. lua_Function f = lua_stackedfunction(0);
  25. char *funcname;
  26. lua_getobjname(f, &funcname);
  27. numarg -= lua_nups(f);
  28. if (funcname == NULL)
  29. funcname = "?";
  30. if (extramsg == NULL)
  31. luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
  32. else
  33. luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
  34. numarg, funcname, extramsg);
  35. }
  36. char *luaL_check_lstr (int numArg, long *len)
  37. {
  38. lua_Object o = lua_getparam(numArg);
  39. luaL_arg_check(lua_isstring(o), numArg, "string expected");
  40. if (len) *len = lua_strlen(o);
  41. return lua_getstring(o);
  42. }
  43. char *luaL_opt_lstr (int numArg, char *def, long *len)
  44. {
  45. return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
  46. luaL_check_lstr(numArg, len);
  47. }
  48. double luaL_check_number (int numArg)
  49. {
  50. lua_Object o = lua_getparam(numArg);
  51. luaL_arg_check(lua_isnumber(o), numArg, "number expected");
  52. return lua_getnumber(o);
  53. }
  54. double luaL_opt_number (int numArg, double def)
  55. {
  56. return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
  57. luaL_check_number(numArg);
  58. }
  59. lua_Object luaL_tablearg (int arg)
  60. {
  61. lua_Object o = lua_getparam(arg);
  62. luaL_arg_check(lua_istable(o), arg, "table expected");
  63. return o;
  64. }
  65. lua_Object luaL_functionarg (int arg)
  66. {
  67. lua_Object o = lua_getparam(arg);
  68. luaL_arg_check(lua_isfunction(o), arg, "function expected");
  69. return o;
  70. }
  71. lua_Object luaL_nonnullarg (int numArg)
  72. {
  73. lua_Object o = lua_getparam(numArg);
  74. luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
  75. return o;
  76. }
  77. void luaL_openlib (struct luaL_reg *l, int n)
  78. {
  79. int i;
  80. lua_open(); /* make sure lua is already open */
  81. for (i=0; i<n; i++)
  82. lua_register(l[i].name, l[i].func);
  83. }
  84. void luaL_verror (char *fmt, ...)
  85. {
  86. char buff[500];
  87. va_list argp;
  88. va_start(argp, fmt);
  89. vsprintf(buff, fmt, argp);
  90. va_end(argp);
  91. lua_error(buff);
  92. }
  93. void luaL_chunkid (char *out, char *source, int len) {
  94. len -= 13; /* 13 = strlen("string ''...\0") */
  95. if (*source == '@')
  96. sprintf(out, "file `%.*s'", len, source+1);
  97. else if (*source == '(')
  98. strcpy(out, "(C code)");
  99. else {
  100. char *b = strchr(source , '\n'); /* stop string at first new line */
  101. int lim = (b && (b-source)<len) ? b-source : len;
  102. sprintf(out, "string `%.*s'", lim, source);
  103. strcpy(out+lim+(13-5), "...'"); /* 5 = strlen("...'\0") */
  104. }
  105. }
  106. void luaL_filesource (char *out, char *filename, int len) {
  107. if (filename == NULL) filename = "(stdin)";
  108. sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
  109. }