lauxlib.c 5.0 KB

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