lauxlib.c 3.2 KB

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