lauxlib.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. ** $Id: lauxlib.c,v 1.50 2001/04/23 16:35:45 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. /* 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_PRIVATE
  14. #include "lua.h"
  15. #include "lauxlib.h"
  16. #include "luadebug.h"
  17. #include "lualib.h"
  18. LUALIB_API int luaL_findstring (const l_char *name, const l_char *const list[]) {
  19. int i;
  20. for (i=0; list[i]; i++)
  21. if (strcmp(list[i], name) == 0)
  22. return i;
  23. return -1; /* name not found */
  24. }
  25. LUALIB_API void luaL_argerror (lua_State *L, int narg, const l_char *extramsg) {
  26. lua_Debug ar;
  27. lua_getstack(L, 0, &ar);
  28. lua_getinfo(L, l_s("n"), &ar);
  29. if (ar.name == NULL)
  30. ar.name = l_s("?");
  31. luaL_verror(L, l_s("bad argument #%d to `%.50s' (%.100s)"),
  32. narg, ar.name, extramsg);
  33. }
  34. static void type_error (lua_State *L, int narg, const l_char *tname) {
  35. l_char buff[80];
  36. sprintf(buff, l_s("%.25s expected, got %.25s"), tname, lua_type(L,narg));
  37. luaL_argerror(L, narg, buff);
  38. }
  39. static void tag_error (lua_State *L, int narg, int tag) {
  40. type_error(L, narg, lua_typename(L, tag));
  41. }
  42. LUALIB_API void luaL_checkstack (lua_State *L, int space, const l_char *mes) {
  43. if (space > lua_stackspace(L))
  44. luaL_verror(L, l_s("stack overflow (%.30s)"), mes);
  45. }
  46. LUALIB_API void luaL_checktype(lua_State *L, int narg, int t) {
  47. if (lua_rawtag(L, narg) != t)
  48. tag_error(L, narg, t);
  49. }
  50. LUALIB_API void luaL_checkany (lua_State *L, int narg) {
  51. if (lua_rawtag(L, narg) == LUA_TNONE)
  52. luaL_argerror(L, narg, l_s("value expected"));
  53. }
  54. LUALIB_API void *luaL_check_userdata (lua_State *L, int narg,
  55. const l_char *name) {
  56. if (strcmp(lua_type(L, narg), name) != 0)
  57. type_error(L, narg, name);
  58. return lua_touserdata(L, narg);
  59. }
  60. LUALIB_API const l_char *luaL_check_lstr (lua_State *L, int narg, size_t *len) {
  61. const l_char *s = lua_tostring(L, narg);
  62. if (!s) tag_error(L, narg, LUA_TSTRING);
  63. if (len) *len = lua_strlen(L, narg);
  64. return s;
  65. }
  66. LUALIB_API const l_char *luaL_opt_lstr (lua_State *L, int narg, const l_char *def, size_t *len) {
  67. if (lua_isnull(L, narg)) {
  68. if (len)
  69. *len = (def ? strlen(def) : 0);
  70. return def;
  71. }
  72. else return luaL_check_lstr(L, narg, len);
  73. }
  74. LUALIB_API lua_Number luaL_check_number (lua_State *L, int narg) {
  75. lua_Number d = lua_tonumber(L, narg);
  76. if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */
  77. tag_error(L, narg, LUA_TNUMBER);
  78. return d;
  79. }
  80. LUALIB_API lua_Number luaL_opt_number (lua_State *L, int narg, lua_Number def) {
  81. if (lua_isnull(L, narg)) return def;
  82. else return luaL_check_number(L, narg);
  83. }
  84. LUALIB_API void luaL_openlib (lua_State *L, const luaL_reg *l, int n) {
  85. int i;
  86. for (i=0; i<n; i++)
  87. lua_register(L, l[i].name, l[i].func);
  88. }
  89. LUALIB_API void luaL_verror (lua_State *L, const l_char *fmt, ...) {
  90. l_char buff[500];
  91. va_list argp;
  92. va_start(argp, fmt);
  93. vsprintf(buff, fmt, argp);
  94. va_end(argp);
  95. lua_error(L, buff);
  96. }
  97. /*
  98. ** {======================================================
  99. ** Generic Buffer manipulation
  100. ** =======================================================
  101. */
  102. #define buffempty(B) ((B)->p == (B)->buffer)
  103. #define bufflen(B) ((B)->p - (B)->buffer)
  104. #define bufffree(B) ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
  105. #define LIMIT (LUA_MINSTACK/2)
  106. static int emptybuffer (luaL_Buffer *B) {
  107. size_t l = bufflen(B);
  108. if (l == 0) return 0; /* put nothing on stack */
  109. else {
  110. lua_pushlstring(B->L, B->buffer, l);
  111. B->p = B->buffer;
  112. B->level++;
  113. return 1;
  114. }
  115. }
  116. static void adjuststack (luaL_Buffer *B) {
  117. if (B->level > 1) {
  118. lua_State *L = B->L;
  119. int toget = 1; /* number of levels to concat */
  120. size_t toplen = lua_strlen(L, -1);
  121. do {
  122. size_t l = lua_strlen(L, -(toget+1));
  123. if (B->level - toget + 1 >= LIMIT || toplen > l) {
  124. toplen += l;
  125. toget++;
  126. }
  127. else break;
  128. } while (toget < B->level);
  129. lua_concat(L, toget);
  130. B->level = B->level - toget + 1;
  131. }
  132. }
  133. LUALIB_API l_char *luaL_prepbuffer (luaL_Buffer *B) {
  134. if (emptybuffer(B))
  135. adjuststack(B);
  136. return B->buffer;
  137. }
  138. LUALIB_API void luaL_addlstring (luaL_Buffer *B, const l_char *s, size_t l) {
  139. while (l--)
  140. luaL_putchar(B, *s++);
  141. }
  142. LUALIB_API void luaL_addstring (luaL_Buffer *B, const l_char *s) {
  143. luaL_addlstring(B, s, strlen(s));
  144. }
  145. LUALIB_API void luaL_pushresult (luaL_Buffer *B) {
  146. emptybuffer(B);
  147. lua_concat(B->L, B->level);
  148. B->level = 1;
  149. }
  150. LUALIB_API void luaL_addvalue (luaL_Buffer *B) {
  151. lua_State *L = B->L;
  152. size_t vl = lua_strlen(L, -1);
  153. if (vl <= bufffree(B)) { /* fit into buffer? */
  154. memcpy(B->p, lua_tostring(L, -1), vl); /* put it there */
  155. B->p += vl;
  156. lua_pop(L, 1); /* remove from stack */
  157. }
  158. else {
  159. if (emptybuffer(B))
  160. lua_insert(L, -2); /* put buffer before new value */
  161. B->level++; /* add new value into B stack */
  162. adjuststack(B);
  163. }
  164. }
  165. LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {
  166. B->L = L;
  167. B->p = B->buffer;
  168. B->level = 0;
  169. }
  170. /* }====================================================== */