lauxlib.c 5.2 KB

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