1997-12-23 22:24:36 +03:00
|
|
|
/*
|
2000-03-03 17:58:26 +03:00
|
|
|
** $Id: lbuffer.c,v 1.11 1999/11/22 13:12:07 roberto Exp roberto $
|
1998-06-19 20:14:09 +04:00
|
|
|
** Auxiliary functions for building Lua libraries
|
1997-12-23 22:24:36 +03:00
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
#define LUA_REENTRANT
|
|
|
|
|
1997-12-23 22:24:36 +03:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "lstate.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*-------------------------------------------------------
|
1998-06-19 20:14:09 +04:00
|
|
|
** Auxiliary buffer
|
1997-12-23 22:24:36 +03:00
|
|
|
-------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
2000-03-03 17:58:26 +03:00
|
|
|
/*
|
|
|
|
** amount of extra space (pre)allocated when buffer is reallocated
|
|
|
|
*/
|
1999-02-25 22:13:56 +03:00
|
|
|
#define EXTRABUFF 32
|
|
|
|
|
|
|
|
|
2000-03-03 17:58:26 +03:00
|
|
|
#define openspace(L, size) if (L->Mbuffnext+(size) > L->Mbuffsize) \
|
|
|
|
Openspace(L, size)
|
1997-12-23 22:24:36 +03:00
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
static void Openspace (lua_State *L, int size) {
|
|
|
|
L->Mbuffsize = (L->Mbuffnext+size+EXTRABUFF)*2;
|
|
|
|
luaM_reallocvector(L, L->Mbuffer, L->Mbuffsize, char);
|
1997-12-23 22:24:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
char *luaL_openspace (lua_State *L, int size) {
|
|
|
|
openspace(L, size);
|
1997-12-23 22:24:36 +03:00
|
|
|
return L->Mbuffer+L->Mbuffnext;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaL_addchar (lua_State *L, int c) {
|
|
|
|
openspace(L, 1);
|
1998-12-28 16:44:54 +03:00
|
|
|
L->Mbuffer[L->Mbuffnext++] = (char)c;
|
1997-12-23 22:24:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaL_resetbuffer (lua_State *L) {
|
1999-02-25 18:17:01 +03:00
|
|
|
L->Mbuffnext = L->Mbuffbase;
|
1997-12-23 22:24:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaL_addsize (lua_State *L, int n) {
|
1997-12-23 22:24:36 +03:00
|
|
|
L->Mbuffnext += n;
|
|
|
|
}
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int luaL_getsize (lua_State *L) {
|
1999-02-25 18:17:01 +03:00
|
|
|
return L->Mbuffnext-L->Mbuffbase;
|
1998-03-06 19:54:42 +03:00
|
|
|
}
|
1997-12-23 22:24:36 +03:00
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
int luaL_newbuffer (lua_State *L, int size) {
|
1999-02-25 18:17:01 +03:00
|
|
|
int old = L->Mbuffbase;
|
1999-11-22 16:12:07 +03:00
|
|
|
openspace(L, size);
|
1999-02-25 18:17:01 +03:00
|
|
|
L->Mbuffbase = L->Mbuffnext;
|
1997-12-23 22:24:36 +03:00
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
void luaL_oldbuffer (lua_State *L, int old) {
|
1999-02-25 18:17:01 +03:00
|
|
|
L->Mbuffnext = L->Mbuffbase;
|
|
|
|
L->Mbuffbase = old;
|
1997-12-23 22:24:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-22 16:12:07 +03:00
|
|
|
char *luaL_buffer (lua_State *L) {
|
1999-02-25 18:17:01 +03:00
|
|
|
return L->Mbuffer+L->Mbuffbase;
|
1997-12-23 22:24:36 +03:00
|
|
|
}
|
|
|
|
|