add new Bytearray:append overloads
This commit is contained in:
parent
e5b533f65f
commit
4ce2f3edca
@ -149,27 +149,6 @@ static int l_read_bytes(lua::State* L) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_bytes_from_table(
|
|
||||||
lua::State* L, int tableIndex, std::vector<ubyte>& bytes
|
|
||||||
) {
|
|
||||||
if (!lua::istable(L, tableIndex)) {
|
|
||||||
throw std::runtime_error("table expected");
|
|
||||||
} else {
|
|
||||||
size_t size = lua::objlen(L, tableIndex);
|
|
||||||
for (size_t i = 0; i < size; i++) {
|
|
||||||
lua::rawgeti(L, i + 1, tableIndex);
|
|
||||||
const int byte = lua::tointeger(L, -1);
|
|
||||||
lua::pop(L);
|
|
||||||
if (byte < 0 || byte > 255) {
|
|
||||||
throw std::runtime_error(
|
|
||||||
"invalid byte '" + std::to_string(byte) + "'"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
bytes.push_back(byte);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_write_bytes(lua::State* L) {
|
static int l_write_bytes(lua::State* L) {
|
||||||
fs::path path = get_writeable_path(L);
|
fs::path path = get_writeable_path(L);
|
||||||
|
|
||||||
@ -181,7 +160,7 @@ static int l_write_bytes(lua::State* L) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ubyte> bytes;
|
std::vector<ubyte> bytes;
|
||||||
read_bytes_from_table(L, 2, bytes);
|
lua::read_bytes_from_table(L, 2, bytes);
|
||||||
return lua::pushboolean(
|
return lua::pushboolean(
|
||||||
L, files::write_bytes(path, bytes.data(), bytes.size())
|
L, files::write_bytes(path, bytes.data(), bytes.size())
|
||||||
);
|
);
|
||||||
@ -223,7 +202,7 @@ static int l_list(lua::State* L) {
|
|||||||
static int l_gzip_compress(lua::State* L) {
|
static int l_gzip_compress(lua::State* L) {
|
||||||
std::vector<ubyte> bytes;
|
std::vector<ubyte> bytes;
|
||||||
|
|
||||||
read_bytes_from_table(L, 1, bytes);
|
lua::read_bytes_from_table(L, 1, bytes);
|
||||||
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
|
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
|
||||||
int newTable = lua::gettop(L);
|
int newTable = lua::gettop(L);
|
||||||
|
|
||||||
@ -237,7 +216,7 @@ static int l_gzip_compress(lua::State* L) {
|
|||||||
static int l_gzip_decompress(lua::State* L) {
|
static int l_gzip_decompress(lua::State* L) {
|
||||||
std::vector<ubyte> bytes;
|
std::vector<ubyte> bytes;
|
||||||
|
|
||||||
read_bytes_from_table(L, 1, bytes);
|
lua::read_bytes_from_table(L, 1, bytes);
|
||||||
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
|
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
|
||||||
int newTable = lua::gettop(L);
|
int newTable = lua::gettop(L);
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <typeindex>
|
#include <typeindex>
|
||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
|
#include <stdexcept>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "data/dv.hpp"
|
#include "data/dv.hpp"
|
||||||
@ -698,4 +699,25 @@ namespace lua {
|
|||||||
}
|
}
|
||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void read_bytes_from_table(
|
||||||
|
lua::State* L, int tableIndex, std::vector<ubyte>& bytes
|
||||||
|
) {
|
||||||
|
if (!lua::istable(L, tableIndex)) {
|
||||||
|
throw std::runtime_error("table expected");
|
||||||
|
} else {
|
||||||
|
size_t size = lua::objlen(L, tableIndex);
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
lua::rawgeti(L, i + 1, tableIndex);
|
||||||
|
const int byte = lua::tointeger(L, -1);
|
||||||
|
lua::pop(L);
|
||||||
|
if (byte < 0 || byte > 255) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"invalid byte '" + std::to_string(byte) + "'"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
bytes.push_back(byte);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "util/listutil.hpp"
|
||||||
#include "../lua_util.hpp"
|
#include "../lua_util.hpp"
|
||||||
|
|
||||||
using namespace lua;
|
using namespace lua;
|
||||||
@ -18,8 +19,16 @@ LuaBytearray::~LuaBytearray() {
|
|||||||
|
|
||||||
static int l_append(lua::State* L) {
|
static int l_append(lua::State* L) {
|
||||||
if (auto buffer = touserdata<LuaBytearray>(L, 1)) {
|
if (auto buffer = touserdata<LuaBytearray>(L, 1)) {
|
||||||
|
if (lua::isnumber(L, 2)) {
|
||||||
auto value = tointeger(L, 2);
|
auto value = tointeger(L, 2);
|
||||||
buffer->data().push_back(static_cast<ubyte>(value));
|
buffer->data().push_back(static_cast<ubyte>(value));
|
||||||
|
} else if (lua::istable(L, 2)) {
|
||||||
|
lua::read_bytes_from_table(L, 2, buffer->data());
|
||||||
|
} else if (auto extension = lua::touserdata<LuaBytearray>(L, 2)) {
|
||||||
|
util::concat(buffer->data(), extension->data());
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error("integer/table/Bytearray expected");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user