lbuffer.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. ** $Id: lbuffer.c,v 1.14 2000/06/12 13:52:05 roberto Exp roberto $
  3. ** Auxiliary functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include "lua.h"
  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 ((size_t)(size) > L->Mbuffsize-L->Mbuffnext) \
  19. Openspace(L, size)
  20. static void Openspace (lua_State *L, size_t size) {
  21. lint32 newsize = ((lint32)L->Mbuffnext+size+EXTRABUFF)*2;
  22. luaM_reallocvector(L, L->Mbuffer, newsize, char);
  23. L->Mbuffsize = newsize;
  24. }
  25. char *luaL_openspace (lua_State *L, size_t size) {
  26. openspace(L, size);
  27. return L->Mbuffer+L->Mbuffnext;
  28. }
  29. void luaL_addchar (lua_State *L, int c) {
  30. openspace(L, 1);
  31. L->Mbuffer[L->Mbuffnext++] = (char)c;
  32. }
  33. void luaL_resetbuffer (lua_State *L) {
  34. L->Mbuffnext = L->Mbuffbase;
  35. }
  36. void luaL_addsize (lua_State *L, size_t n) {
  37. L->Mbuffnext += n;
  38. }
  39. size_t luaL_getsize (lua_State *L) {
  40. return L->Mbuffnext-L->Mbuffbase;
  41. }
  42. size_t luaL_newbuffer (lua_State *L, size_t size) {
  43. size_t old = L->Mbuffbase;
  44. openspace(L, size);
  45. L->Mbuffbase = L->Mbuffnext;
  46. return old;
  47. }
  48. void luaL_oldbuffer (lua_State *L, size_t old) {
  49. L->Mbuffnext = L->Mbuffbase;
  50. L->Mbuffbase = old;
  51. }
  52. char *luaL_buffer (lua_State *L) {
  53. return L->Mbuffer+L->Mbuffbase;
  54. }