lauxlib.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. ** $Id: lauxlib.c,v 1.20 1999/10/05 18:33:43 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. #define LUA_REENTRANT
  14. #include "lauxlib.h"
  15. #include "lua.h"
  16. #include "luadebug.h"
  17. int luaL_findstring (const char *name, const char *const list[]) {
  18. int i;
  19. for (i=0; list[i]; i++)
  20. if (strcmp(list[i], name) == 0)
  21. return i;
  22. return -1; /* name not found */
  23. }
  24. void luaL_argerror (lua_State *L, int numarg, const char *extramsg) {
  25. lua_Function f = lua_stackedfunction(L, 0);
  26. const char *funcname;
  27. lua_getobjname(L, f, &funcname);
  28. numarg -= lua_nups(L, f);
  29. if (funcname == NULL)
  30. funcname = "?";
  31. luaL_verror(L, "bad argument #%d to function `%.50s' (%.100s)",
  32. numarg, funcname, extramsg);
  33. }
  34. static const char *checkstr (lua_State *L, lua_Object o, int n, long *len) {
  35. const char *s = lua_getstring(L, o);
  36. luaL_arg_check(L, s, n, "string expected");
  37. if (len) *len = lua_strlen(L, o);
  38. return s;
  39. }
  40. const char *luaL_check_lstr (lua_State *L, int n, long *len) {
  41. return checkstr(L, lua_getparam(L, n), n, len);
  42. }
  43. const char *luaL_opt_lstr (lua_State *L, int n, const char *def, long *len) {
  44. lua_Object o = lua_getparam(L, n);
  45. if (o == LUA_NOOBJECT) {
  46. if (len) *len = def ? strlen(def) : 0;
  47. return def;
  48. }
  49. else return checkstr(L, o, n, len);
  50. }
  51. double luaL_check_number (lua_State *L, int n) {
  52. lua_Object o = lua_getparam(L, n);
  53. luaL_arg_check(L, lua_isnumber(L, o), n, "number expected");
  54. return lua_getnumber(L, o);
  55. }
  56. double luaL_opt_number (lua_State *L, int n, double def) {
  57. lua_Object o = lua_getparam(L, n);
  58. if (o == LUA_NOOBJECT) return def;
  59. else {
  60. luaL_arg_check(L, lua_isnumber(L, o), n, "number expected");
  61. return lua_getnumber(L, o);
  62. }
  63. }
  64. lua_Object luaL_tablearg (lua_State *L, int arg) {
  65. lua_Object o = lua_getparam(L, arg);
  66. luaL_arg_check(L, lua_istable(L, o), arg, "table expected");
  67. return o;
  68. }
  69. lua_Object luaL_functionarg (lua_State *L, int arg) {
  70. lua_Object o = lua_getparam(L, arg);
  71. luaL_arg_check(L, lua_isfunction(L, o), arg, "function expected");
  72. return o;
  73. }
  74. lua_Object luaL_nonnullarg (lua_State *L, int n) {
  75. lua_Object o = lua_getparam(L, n);
  76. luaL_arg_check(L, o != LUA_NOOBJECT, n, "value expected");
  77. return o;
  78. }
  79. void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) {
  80. int i;
  81. for (i=0; i<n; i++)
  82. lua_register(L, l[i].name, l[i].func);
  83. }
  84. void luaL_verror (lua_State *L, const char *fmt, ...) {
  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(L, buff);
  91. }
  92. void luaL_chunkid (char *out, const char *source, int len) {
  93. len -= 13; /* 13 = strlen("string ''...\0") */
  94. if (*source == '@')
  95. sprintf(out, "file `%.*s'", len, source+1);
  96. else if (*source == '(')
  97. strcpy(out, "(C code)");
  98. else {
  99. const char *b = strchr(source , '\n'); /* stop string at first new line */
  100. int lim = (b && (b-source)<len) ? b-source : len;
  101. sprintf(out, "string `%.*s'", lim, source);
  102. strcpy(out+lim+(13-5), "...'"); /* 5 = strlen("...'\0") */
  103. }
  104. }
  105. void luaL_filesource (char *out, const char *filename, int len) {
  106. if (filename == NULL) filename = "(stdin)";
  107. sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */
  108. }