static int GLua_BitStream_Index(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	const char *key = lua_tostring(L,2);
	GLUA_UNUSED(stream);
	lua_getmetatable(L,1);
	lua_getfield(L,-1,key);
	return 1;
}
static int GLua_BitStream_ToString(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	if (stream->reading) {
		lua_pushstring(L,va("BitStream [ Reading - %i / %i bytes ]", stream->stream.readcount, stream->stream.maxsize ));
	} else {
		lua_pushstring(L,va("BitStream [ Writing - %i / %i bytes ]", stream->stream.cursize, stream->stream.maxsize ));
	}
	return 1;
}
Exemplo n.º 3
0
static int GLua_BitStream_GetData(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	
	if (stream->reading) {
		lua_pushlstring(L, stream->data, stream->stream.maxsize);
	} else {
		lua_pushlstring(L, stream->data, stream->stream.cursize);
	}
	return 1;
}
static int GLua_BitStream_Reset(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	if (stream->reading) {
		BitStream_BeginReading(&stream->stream);
	} else {
		stream->stream.cursize = 0;
		stream->stream.bit = 0;
	}
	return 0;
}
static int GLua_BitStream_GetLength(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	
	if (stream->reading) {
		lua_pushinteger(L, stream->stream.maxsize);
	} else {
		lua_pushinteger(L, stream->stream.cursize);
	}
	return 1;
}
static int GLua_BitStream_BytesRemaining(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	
	if (stream->reading) {
		lua_pushinteger(L, stream->stream.maxsize - stream->stream.readcount);
	} else {
		lua_pushinteger(L, stream->stream.maxsize - stream->stream.cursize);
	}
	return 1;
}
static int GLua_BitStream_GetData(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	
	if (stream->reading) {
		lua_pushlstring(L, reinterpret_cast<const char *>(stream->data), stream->stream.maxsize);
	} else {
		lua_pushlstring(L, reinterpret_cast<const char *>(stream->data), stream->stream.cursize);
	}
	return 1;
}
static int GLua_BitStream_WriteUInt(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	unsigned int val = luaL_checkinteger(L,2);
	
	if (stream->reading) {
		luaL_error(L, "Attempted to write to a read-only bitstream");
		return 0;
	}

	BitStream_WriteUInt(&stream->stream, val);
	
	return 0;
}
static int GLua_BitStream_WriteBool(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	int val = lua_toboolean(L,2);
	
	if (stream->reading) {
		luaL_error(L, "Attempted to write to a read-only bitstream");
		return 0;
	}

	BitStream_WriteBool(&stream->stream, val);
	
	return 0;
}
static int GLua_BitStream_WriteString(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	const char *val = luaL_checkstring(L,2);
	
	if (stream->reading) {
		luaL_error(L, "Attempted to write to a read-only bitstream");
		return 0;
	}

	BitStream_WriteString(&stream->stream, val);
	
	return 0;
}
static int GLua_BitStream_WriteDouble(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	double val = luaL_checknumber(L,2);
	
	if (stream->reading) {
		luaL_error(L, "Attempted to write to a read-only bitstream");
		return 0;
	}

	BitStream_WriteDouble(&stream->stream, val);
	
	return 0;
}
static int GLua_BitStream_ReadDouble(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	double val;
	
	if (!stream->reading) {
		luaL_error(L, "Attempted to read from a write-only bitstream");
		return 0;
	}

	val = BitStream_ReadDouble(&stream->stream);
	
	lua_pushnumber(L, val);
	return 1;
}
static int GLua_BitStream_ReadUInt(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	unsigned int val;
	
	if (!stream->reading) {
		luaL_error(L, "Attempted to read from a write-only bitstream");
		return 0;
	}

	val = BitStream_ReadUInt(&stream->stream);
	
	lua_pushinteger(L, val);
	return 1;
}
static int GLua_BitStream_ReadBool(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	int val;
	
	if (!stream->reading) {
		luaL_error(L, "Attempted to read from a write-only bitstream");
		return 0;
	}

	val = BitStream_ReadBool(&stream->stream);
	
	lua_pushboolean(L, val);
	return 1;
}
static int GLua_BitStream_ReadString(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	luaL_Buffer B;
	char ch;
	
	if (!stream->reading) {
		luaL_error(L, "Attempted to read from a write-only bitstream");
		return 0;
	}

	luaL_buffinit(L, &B);
	while (1) {
		ch = BitStream_ReadChar(&stream->stream);
		if (!ch) {
			break;
		}
		luaL_addchar(&B, ch);
	}
	luaL_pushresult(&B);
	return 1;
}
static int GLua_BitStream_WriteData(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	unsigned int length;
	unsigned int deslength;
	const char *val = luaL_checklstring(L,2, &length);

	if (!lua_isnoneornil(L,3)) {
		deslength = luaL_checkinteger(L,3);
		if (length < deslength) {
			luaL_error(L, "BitStream_WriteData: Specified length is less than data length");
		}
		length = deslength;
	}
	
	if (stream->reading) {
		luaL_error(L, "Attempted to write to a read-only bitstream");
		return 0;
	}

	BitStream_WriteData(&stream->stream, (const unsigned char *)val, length);
	
	return 0;
}
static int GLua_BitStream_GetMaxLength(lua_State *L) {
	GLua_Bitstream_t *stream = GLua_CheckBitStream(L, 1);
	
	lua_pushinteger(L, stream->stream.maxsize);
	return 1;
}