lbuffer.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. ** $Id: lbuffer.c,v 1.11 1999/11/22 13:12:07 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. /*
  15. ** amount of extra space (pre)allocated when buffer is reallocated
  16. */
  17. #define EXTRABUFF 32
  18. #define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) \
  19. Openspace(L, size)
  20. static void Openspace (lua_State *L, int size) {
  21. L->Mbuffsize = (L->Mbuffnext+size+EXTRABUFF)*2;
  22. luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
  23. }
  24. char *luaL_openspace (lua_State *L, int size) {
  25. openspace(L, size);
  26. return L->Mbuffer+L->Mbuffnext;
  27. }
  28. void luaL_addchar (lua_State *L, int c) {
  29. openspace(L, 1);
  30. L->Mbuffer[L->Mbuffnext++] = (char)c;
  31. }
  32. void luaL_resetbuffer (lua_State *L) {
  33. L->Mbuffnext = L->Mbuffbase;
  34. }
  35. void luaL_addsize (lua_State *L, int n) {
  36. L->Mbuffnext += n;
  37. }
  38. int luaL_getsize (lua_State *L) {
  39. return L->Mbuffnext-L->Mbuffbase;
  40. }
  41. int luaL_newbuffer (lua_State *L, int size) {
  42. int old = L->Mbuffbase;
  43. openspace(L, size);
  44. L->Mbuffbase = L->Mbuffnext;
  45. return old;
  46. }
  47. void luaL_oldbuffer (lua_State *L, int old) {
  48. L->Mbuffnext = L->Mbuffbase;
  49. L->Mbuffbase = old;
  50. }
  51. char *luaL_buffer (lua_State *L) {
  52. return L->Mbuffer+L->Mbuffbase;
  53. }