lbuffer.c 1.3 KB

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