lbuffer.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. ** $Id: lbuffer.c,v 1.10 1999/11/10 15:40:46 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 "lauxlib.h"
  9. #include "lmem.h"
  10. #include "lstate.h"
  11. /*-------------------------------------------------------
  12. ** Auxiliary buffer
  13. -------------------------------------------------------*/
  14. #define EXTRABUFF 32
  15. #define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(L, size)
  16. static void Openspace (lua_State *L, int size) {
  17. L->Mbuffsize = (L->Mbuffnext+size+EXTRABUFF)*2;
  18. luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
  19. }
  20. char *luaL_openspace (lua_State *L, int size) {
  21. openspace(L, size);
  22. return L->Mbuffer+L->Mbuffnext;
  23. }
  24. void luaL_addchar (lua_State *L, int c) {
  25. openspace(L, 1);
  26. L->Mbuffer[L->Mbuffnext++] = (char)c;
  27. }
  28. void luaL_resetbuffer (lua_State *L) {
  29. L->Mbuffnext = L->Mbuffbase;
  30. }
  31. void luaL_addsize (lua_State *L, int n) {
  32. L->Mbuffnext += n;
  33. }
  34. int luaL_getsize (lua_State *L) {
  35. return L->Mbuffnext-L->Mbuffbase;
  36. }
  37. int luaL_newbuffer (lua_State *L, int size) {
  38. int old = L->Mbuffbase;
  39. openspace(L, size);
  40. L->Mbuffbase = L->Mbuffnext;
  41. return old;
  42. }
  43. void luaL_oldbuffer (lua_State *L, int old) {
  44. L->Mbuffnext = L->Mbuffbase;
  45. L->Mbuffbase = old;
  46. }
  47. char *luaL_buffer (lua_State *L) {
  48. return L->Mbuffer+L->Mbuffbase;
  49. }