lauxlib.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. ** $Id: lauxlib.c,v 1.19 1999/09/06 13:13: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 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 (const char *name, const char *const 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, const char *extramsg) {
  24. lua_Function f = lua_stackedfunction(0);
  25. const char *funcname;
  26. lua_getobjname(f, &funcname);
  27. numarg -= lua_nups(f);
  28. if (funcname == NULL)
  29. funcname = "?";
  30. luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
  31. numarg, funcname, extramsg);
  32. }
  33. static const char *checkstr (lua_Object o, int numArg, long *len) {
  34. const char *s = lua_getstring(o);
  35. luaL_arg_check(s, numArg, "string expected");
  36. if (len) *len = lua_strlen(o);
  37. return s;
  38. }
  39. const char *luaL_check_lstr (int numArg, long *len) {
  40. return checkstr(lua_getparam(numArg), numArg, len);
  41. }
  42. const char *luaL_opt_lstr (int numArg, const char *def, long *len) {
  43. lua_Object o = lua_getparam(numArg);
  44. if (o == LUA_NOOBJECT) {
  45. if (len) *len = def ? strlen(def) : 0;
  46. return def;
  47. }
  48. else return checkstr(o, numArg, len);
  49. }
  50. double luaL_check_number (int numArg) {
  51. lua_Object o = lua_getparam(numArg);
  52. luaL_arg_check(lua_isnumber(o), numArg, "number expected");
  53. return lua_getnumber(o);
  54. }
  55. double luaL_opt_number (int numArg, double def) {
  56. lua_Object o = lua_getparam(numArg);
  57. if (o == LUA_NOOBJECT) return def;
  58. else {
  59. luaL_arg_check(lua_isnumber(o), numArg, "number expected");
  60. return lua_getnumber(o);
  61. }
  62. }
  63. lua_Object luaL_tablearg (int arg) {
  64. lua_Object o = lua_getparam(arg);
  65. luaL_arg_check(lua_istable(o), arg, "table expected");
  66. return o;
  67. }
  68. lua_Object luaL_functionarg (int arg) {
  69. lua_Object o = lua_getparam(arg);
  70. luaL_arg_check(lua_isfunction(o), arg, "function expected");
  71. return o;
  72. }
  73. lua_Object luaL_nonnullarg (int numArg) {
  74. lua_Object o = lua_getparam(numArg);
  75. luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
  76. return o;
  77. }
  78. void luaL_openlib (const struct luaL_reg *l, int n) {
  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 (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(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. }