mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 18:09:46 +00:00
Merge branch 'dev' of https://github.com/MihailRis/voxelcore into dev
This commit is contained in:
commit
369ba4d0be
12 changed files with 927 additions and 482 deletions
|
|
@ -14,8 +14,13 @@ std::filesystem::path io::MemoryDevice::resolve(std::string_view path) {
|
|||
|
||||
std::unique_ptr<std::ostream> io::MemoryDevice::write(std::string_view path) {
|
||||
std::string filePath = std::string(path);
|
||||
|
||||
auto memoryStream = std::make_unique<memory_ostream>();
|
||||
memoryStream->set_sync_callback([this, filePath](std::string_view data) {
|
||||
createFile(std::move(filePath), util::Buffer<char>(data.data(), data.size()));
|
||||
});
|
||||
return std::make_unique<finalizing_ostream>(
|
||||
std::make_unique<memory_ostream>(),
|
||||
std::move(memoryStream),
|
||||
[this, filePath](auto ostream) {
|
||||
auto& memoryStream = dynamic_cast<memory_ostream&>(*ostream);
|
||||
createFile(std::move(filePath), memoryStream.release());
|
||||
|
|
|
|||
|
|
@ -3,6 +3,9 @@
|
|||
#include <iostream>
|
||||
#include <streambuf>
|
||||
#include <cstring>
|
||||
#include <climits>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <string_view>
|
||||
|
||||
|
|
@ -13,40 +16,33 @@ public:
|
|||
explicit memory_buffer(size_t initial_size = 64)
|
||||
: buffer(std::make_unique<char[]>(initial_size)),
|
||||
capacity(initial_size) {
|
||||
setp(buffer.get(), buffer.get() + initial_size);
|
||||
setp(buffer.get(), buffer.get() + capacity);
|
||||
}
|
||||
|
||||
std::string_view view() const {
|
||||
return std::string_view(pbase(), pptr() - pbase());
|
||||
return std::string_view(pbase(), size());
|
||||
}
|
||||
|
||||
util::Buffer<char> release() {
|
||||
return {std::move(buffer), size()};
|
||||
size_t n = size();
|
||||
return {std::move(buffer), n};
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return pptr() - pbase();
|
||||
return std::max(length, position());
|
||||
}
|
||||
|
||||
void set_sync_callback(std::function<void(std::string_view)> callback) {
|
||||
on_sync = std::move(callback);
|
||||
}
|
||||
protected:
|
||||
int_type overflow(int_type c) override {
|
||||
if (c == traits_type::eof())
|
||||
return traits_type::eof();
|
||||
|
||||
const size_t data_size = pptr() - pbase();
|
||||
const size_t new_capacity = std::max(capacity * 2, data_size + 1);
|
||||
auto new_buffer = std::make_unique<char[]>(new_capacity);
|
||||
|
||||
std::memcpy(new_buffer.get(), pbase(), data_size);
|
||||
|
||||
buffer = std::move(new_buffer);
|
||||
capacity = new_capacity;
|
||||
|
||||
setp(buffer.get(), buffer.get() + new_capacity);
|
||||
pbump(data_size);
|
||||
|
||||
reserve(position() + 1);
|
||||
*pptr() = traits_type::to_char_type(c);
|
||||
pbump(1);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
|
@ -64,43 +60,87 @@ protected:
|
|||
}
|
||||
|
||||
std::streamsize xsputn(const char* s, std::streamsize count) override {
|
||||
const std::streamsize avail = epptr() - pptr();
|
||||
reserve(position() + count);
|
||||
std::memcpy(pptr(), s, count);
|
||||
advance(count);
|
||||
return count;
|
||||
}
|
||||
|
||||
if (avail >= count) {
|
||||
std::memcpy(pptr(), s, count);
|
||||
pbump(count);
|
||||
return count;
|
||||
pos_type seekoff(
|
||||
off_type off,
|
||||
std::ios_base::seekdir dir,
|
||||
std::ios_base::openmode which
|
||||
) override {
|
||||
if (!(which & std::ios_base::out)) {
|
||||
return pos_type(off_type(-1));
|
||||
}
|
||||
length = size();
|
||||
|
||||
off_type base;
|
||||
switch (dir) {
|
||||
case std::ios_base::beg: base = 0; break;
|
||||
case std::ios_base::cur: base = position(); break;
|
||||
case std::ios_base::end: base = length; break;
|
||||
default: return pos_type(off_type(-1));
|
||||
}
|
||||
|
||||
std::streamsize written = 0;
|
||||
if (avail > 0) {
|
||||
std::memcpy(pptr(), s, avail);
|
||||
written += avail;
|
||||
s += avail;
|
||||
count -= avail;
|
||||
pbump(avail);
|
||||
off_type target = base + off;
|
||||
if (target < 0 || target > static_cast<off_type>(length)) {
|
||||
return pos_type(off_type(-1));
|
||||
}
|
||||
set_position(target);
|
||||
return pos_type(target);
|
||||
}
|
||||
|
||||
const size_t data_size = pptr() - pbase();
|
||||
const size_t required_capacity = data_size + count;
|
||||
const size_t new_capacity = std::max(capacity * 2, required_capacity);
|
||||
auto new_buffer = std::make_unique<char[]>(new_capacity);
|
||||
|
||||
std::memcpy(new_buffer.get(), pbase(), data_size);
|
||||
std::memcpy(new_buffer.get() + data_size, s, count);
|
||||
|
||||
buffer = std::move(new_buffer);
|
||||
capacity = new_capacity;
|
||||
|
||||
setp(buffer.get(), buffer.get() + new_capacity);
|
||||
pbump(data_size + count);
|
||||
written += count;
|
||||
|
||||
return written;
|
||||
pos_type seekpos(pos_type pos, std::ios_base::openmode which) override {
|
||||
return seekoff(off_type(pos), std::ios_base::beg, which);
|
||||
}
|
||||
private:
|
||||
std::unique_ptr<char[]> buffer;
|
||||
size_t capacity;
|
||||
size_t length = 0;
|
||||
|
||||
size_t position() const {
|
||||
return pptr() - pbase();
|
||||
}
|
||||
|
||||
void advance(size_t n) {
|
||||
while (n > 0) {
|
||||
int step = static_cast<int>(std::min<size_t>(n, INT_MAX));
|
||||
pbump(step);
|
||||
n -= step;
|
||||
}
|
||||
}
|
||||
|
||||
void set_position(size_t pos) {
|
||||
setp(buffer.get(), buffer.get() + capacity);
|
||||
advance(pos);
|
||||
}
|
||||
|
||||
void reserve(size_t required) {
|
||||
if (required <= capacity) {
|
||||
return;
|
||||
}
|
||||
size_t pos = position();
|
||||
length = size();
|
||||
|
||||
size_t new_capacity = std::max(capacity * 2, required);
|
||||
auto new_buffer = std::make_unique<char[]>(new_capacity);
|
||||
std::memcpy(new_buffer.get(), buffer.get(), length);
|
||||
|
||||
buffer = std::move(new_buffer);
|
||||
capacity = new_capacity;
|
||||
set_position(pos);
|
||||
}
|
||||
|
||||
int sync() override {
|
||||
if (on_sync) {
|
||||
on_sync(view());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::function<void(std::string_view)> on_sync;
|
||||
};
|
||||
|
||||
class memory_ostream : public std::ostream {
|
||||
|
|
@ -115,6 +155,10 @@ public:
|
|||
util::Buffer<char> release() {
|
||||
return buffer.release();
|
||||
}
|
||||
|
||||
void set_sync_callback(std::function<void(std::string_view)> callback) {
|
||||
buffer.set_sync_callback(std::move(callback));
|
||||
}
|
||||
private:
|
||||
memory_buffer buffer;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -305,13 +305,14 @@ static int l_read_descriptor(lua::State* L) {
|
|||
int maxlen = lua::tointeger(L, 2);
|
||||
|
||||
auto& stream = io_descriptors::require_input(descriptor);
|
||||
if (stream.eof()) {
|
||||
stream.clear();
|
||||
}
|
||||
|
||||
util::Buffer<char> buffer(maxlen);
|
||||
stream.read(buffer.data(), maxlen);
|
||||
std::streamsize read_len = stream.gcount();
|
||||
std::streamsize read_len = stream.gcount();
|
||||
|
||||
if (stream.eof()) {
|
||||
stream.clear(stream.rdstate() & std::ios::badbit);
|
||||
}
|
||||
return lua::create_bytearray(L, buffer.data(), read_len);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue