lbuffer.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. ** $Id: lbuffer.c,v 1.13 2000/05/24 13:54:49 roberto Exp roberto $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #define LUA_REENTRANT
  8. #include "lua.h"
  9. #include "lauxlib.h"
  10. #include "lmem.h"
  11. #include "lstate.h"
  12. /*-------------------------------------------------------
  13. ** Auxiliary buffer
  14. -------------------------------------------------------*/
  15. /*
  16. ** amount of extra space (pre)allocated when buffer is reallocated
  17. */
  18. #define EXTRABUFF 32
  19. #define openspace(L, size) if ((size_t)(size) > L->Mbuffsize-L->Mbuffnext) \
  20. Openspace(L, size)
  21. static void Openspace (lua_State *L, size_t size) {
  22. lint32 newsize = ((lint32)L->Mbuffnext+size+EXTRABUFF)*2;
  23. luaM_reallocvector(L, L->Mbuffer, newsize, char);
  24. L->Mbuffsize = newsize;
  25. }
  26. char *luaL_openspace (lua_State *L, size_t size) {
  27. openspace(L, size);
  28. return L->Mbuffer+L->Mbuffnext;
  29. }
  30. void luaL_addchar (lua_State *L, int c) {
  31. openspace(L, 1);
  32. L->Mbuffer[L->Mbuffnext++] = (char)c;
  33. }
  34. void luaL_resetbuffer (lua_State *L) {
  35. L->Mbuffnext = L->Mbuffbase;
  36. }
  37. void luaL_addsize (lua_State *L, size_t n) {
  38. L->Mbuffnext += n;
  39. }
  40. size_t luaL_getsize (lua_State *L) {
  41. return L->Mbuffnext-L->Mbuffbase;
  42. }
  43. size_t luaL_newbuffer (lua_State *L, size_t size) {
  44. size_t old = L->Mbuffbase;
  45. openspace(L, size);
  46. L->Mbuffbase = L->Mbuffnext;
  47. return old;
  48. }
  49. void luaL_oldbuffer (lua_State *L, size_t old) {
  50. L->Mbuffnext = L->Mbuffbase;
  51. L->Mbuffbase = old;
  52. }
  53. char *luaL_buffer (lua_State *L) {
  54. return L->Mbuffer+L->Mbuffbase;
  55. }