lbuffer.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. ** $Id: $
  3. ** Auxiliar functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include "lauxlib.h"
  8. #include "lmem.h"
  9. #include "lstate.h"
  10. /*-------------------------------------------------------
  11. ** Auxiliar buffer
  12. -------------------------------------------------------*/
  13. #define BUFF_STEP 32
  14. #define openspace(size) if (L->Mbuffnext+(size) > L->Mbuffsize) Openspace(size)
  15. static void Openspace (int size)
  16. {
  17. LState *l = L; /* to optimize */
  18. int base = l->Mbuffbase-l->Mbuffer;
  19. l->Mbuffsize *= 2;
  20. if (l->Mbuffnext+size > l->Mbuffsize) /* still not big enough? */
  21. l->Mbuffsize = l->Mbuffnext+size;
  22. l->Mbuffer = luaM_realloc(l->Mbuffer, l->Mbuffsize);
  23. l->Mbuffbase = l->Mbuffer+base;
  24. }
  25. char *luaL_openspace (int size)
  26. {
  27. openspace(size);
  28. return L->Mbuffer+L->Mbuffnext;
  29. }
  30. void luaL_addchar (int c)
  31. {
  32. openspace(BUFF_STEP);
  33. L->Mbuffer[L->Mbuffnext++] = c;
  34. }
  35. void luaL_resetbuffer (void)
  36. {
  37. L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
  38. }
  39. void luaL_addsize (int n)
  40. {
  41. L->Mbuffnext += n;
  42. }
  43. int luaL_newbuffer (int size)
  44. {
  45. int old = L->Mbuffbase-L->Mbuffer;
  46. openspace(size);
  47. L->Mbuffbase = L->Mbuffer+L->Mbuffnext;
  48. return old;
  49. }
  50. void luaL_oldbuffer (int old)
  51. {
  52. L->Mbuffnext = L->Mbuffbase-L->Mbuffer;
  53. L->Mbuffbase = L->Mbuffer+old;
  54. }
  55. char *luaL_buffer (void)
  56. {
  57. return L->Mbuffbase;
  58. }