add bytearray:append(b)

This commit is contained in:
MihailRis 2024-06-16 21:00:50 +03:00
parent 22328a9f64
commit 20ef11e5a2
6 changed files with 42 additions and 13 deletions

View File

@ -78,6 +78,21 @@ std::unique_ptr<ubyte[]> files::read_bytes(const fs::path& filename, size_t& len
return data;
}
std::vector<ubyte> files::read_bytes(const fs::path& filename) {
std::ifstream input(filename, std::ios::binary);
if (!input.is_open())
return {};
input.seekg(0, std::ios_base::end);
size_t length = input.tellg();
input.seekg(0, std::ios_base::beg);
std::vector<ubyte> data(length);
data.resize(length);
input.read((char*)data.data(), length);
input.close();
return data;
}
std::string files::read_string(const fs::path& filename) {
size_t size;
std::unique_ptr<ubyte[]> bytes (read_bytes(filename, size));

View File

@ -57,6 +57,7 @@ namespace files {
bool read(const fs::path&, char* data, size_t size);
std::unique_ptr<ubyte[]> read_bytes(const fs::path&, size_t& length);
std::vector<ubyte> read_bytes(const fs::path&);
std::string read_string(const fs::path& filename);
/// @brief Read JSON or BJSON file

View File

@ -109,18 +109,8 @@ static int l_file_mkdirs(lua::State* L) {
static int l_file_read_bytes(lua::State* L) {
fs::path path = resolve_path(lua::require_string(L, 1));
if (fs::is_regular_file(path)) {
size_t length = static_cast<size_t>(fs::file_size(path));
auto bytes = files::read_bytes(path, length);
lua::createtable(L, length, 0);
int newTable = lua::gettop(L);
for(size_t i = 0; i < length; i++) {
lua::pushinteger(L, bytes[i]);
lua::rawseti(L, i+1, newTable);
}
return 1;
auto bytes = files::read_bytes(path);
return lua::newuserdata<lua::Bytearray>(L, std::move(bytes));
}
throw std::runtime_error("file does not exists "+util::quote(path.u8string()));
}

View File

@ -18,6 +18,14 @@ Bytearray::Bytearray(std::vector<ubyte> buffer) : buffer(std::move(buffer)) {
Bytearray::~Bytearray() {
}
static int l_bytearray_append(lua::State* L) {
if (auto buffer = touserdata<Bytearray>(L, 1)) {
auto value = tointeger(L, 2);
buffer->append(static_cast<ubyte>(value));
}
return 0;
}
static int l_bytearray_meta_meta_call(lua::State* L) {
auto size = tointeger(L, 2);
if (size < 0) {
@ -28,6 +36,12 @@ static int l_bytearray_meta_meta_call(lua::State* L) {
static int l_bytearray_meta_index(lua::State* L) {
auto buffer = touserdata<Bytearray>(L, 1);
if (isstring(L, 2)) {
std::string member = tostring(L, 2);
if (member == "append") {
return pushcfunction(L, l_bytearray_append);
}
}
auto index = tointeger(L, 2)-1;
if (buffer == nullptr || static_cast<size_t>(index) > buffer->size()) {
return 0;
@ -55,7 +69,7 @@ static int l_bytearray_meta_len(lua::State* L) {
static int l_bytearray_meta_tostring(lua::State* L) {
auto& buffer = *touserdata<Bytearray>(L, 1);
if (buffer.size() > 128) {
if (buffer.size() > 512) {
return pushstring(L, "bytearray["+std::to_string(buffer.size())+"]{...}");
} else {
std::stringstream ss;

View File

@ -31,6 +31,11 @@ namespace lua {
inline size_t size() const {
return buffer.size();
}
inline void append(ubyte b) {
buffer.push_back(b);
}
static int createMetatable(lua::State*);
inline static std::string TYPENAME = "bytearray";
};

View File

@ -49,6 +49,10 @@ namespace lua {
inline const char* type_name(lua::State* L, int idx) {
return lua_typename(L, idx);
}
inline int rawget(lua::State* L, int idx=-2) {
lua_rawget(L, idx);
return 1;
}
inline int rawgeti(lua::State* L, int n, int idx=-1) {
lua_rawgeti(L, idx, n);
return 1;