mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-23 02:49:46 +00:00
update memory_ostream
This commit is contained in:
parent
7994d41b19
commit
ec51848447
2 changed files with 110 additions and 45 deletions
|
|
@ -3,6 +3,8 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <streambuf>
|
#include <streambuf>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <climits>
|
||||||
|
#include <memory>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
|
|
||||||
|
|
@ -13,81 +15,118 @@ public:
|
||||||
explicit memory_buffer(size_t initial_size = 64)
|
explicit memory_buffer(size_t initial_size = 64)
|
||||||
: buffer(std::make_unique<char[]>(initial_size)),
|
: buffer(std::make_unique<char[]>(initial_size)),
|
||||||
capacity(initial_size) {
|
capacity(initial_size) {
|
||||||
setp(buffer.get(), buffer.get() + initial_size);
|
setp(buffer.get(), buffer.get() + capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view view() const {
|
std::string_view view() const {
|
||||||
return std::string_view(pbase(), pptr() - pbase());
|
return std::string_view(pbase(), size());
|
||||||
}
|
}
|
||||||
|
|
||||||
util::Buffer<char> release() {
|
util::Buffer<char> release() {
|
||||||
return {std::move(buffer), size()};
|
size_t n = size();
|
||||||
|
return {std::move(buffer), n};
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t size() const {
|
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:
|
protected:
|
||||||
int_type overflow(int_type c) override {
|
int_type overflow(int_type c) override {
|
||||||
if (c == traits_type::eof())
|
if (c == traits_type::eof())
|
||||||
return traits_type::eof();
|
return traits_type::eof();
|
||||||
|
|
||||||
const size_t data_size = pptr() - pbase();
|
reserve(position() + 1);
|
||||||
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);
|
|
||||||
|
|
||||||
*pptr() = traits_type::to_char_type(c);
|
*pptr() = traits_type::to_char_type(c);
|
||||||
pbump(1);
|
pbump(1);
|
||||||
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::streamsize xsputn(const char* s, std::streamsize count) override {
|
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) {
|
pos_type seekoff(
|
||||||
std::memcpy(pptr(), s, count);
|
off_type off,
|
||||||
pbump(count);
|
std::ios_base::seekdir dir,
|
||||||
return count;
|
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;
|
off_type target = base + off;
|
||||||
if (avail > 0) {
|
if (target < 0 || target > static_cast<off_type>(length)) {
|
||||||
std::memcpy(pptr(), s, avail);
|
return pos_type(off_type(-1));
|
||||||
written += avail;
|
|
||||||
s += avail;
|
|
||||||
count -= avail;
|
|
||||||
pbump(avail);
|
|
||||||
}
|
}
|
||||||
|
set_position(target);
|
||||||
|
return pos_type(target);
|
||||||
|
}
|
||||||
|
|
||||||
const size_t data_size = pptr() - pbase();
|
pos_type seekpos(pos_type pos, std::ios_base::openmode which) override {
|
||||||
const size_t required_capacity = data_size + count;
|
return seekoff(off_type(pos), std::ios_base::beg, which);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<char[]> buffer;
|
std::unique_ptr<char[]> buffer;
|
||||||
size_t capacity;
|
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 {
|
class memory_ostream : public std::ostream {
|
||||||
|
|
@ -102,6 +141,10 @@ public:
|
||||||
util::Buffer<char> release() {
|
util::Buffer<char> release() {
|
||||||
return buffer.release();
|
return buffer.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void set_sync_callback(std::function<void(std::string_view)> callback) {
|
||||||
|
buffer.set_sync_callback(std::move(callback));
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
memory_buffer buffer;
|
memory_buffer buffer;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
22
test/io/memory_ostream.cpp
Normal file
22
test/io/memory_ostream.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "io/memory_ostream.hpp"
|
||||||
|
|
||||||
|
TEST(io, memory_ostream) {
|
||||||
|
const char data[] = "Hello, world!";
|
||||||
|
const int n = std::strlen(data);
|
||||||
|
|
||||||
|
memory_ostream stream;
|
||||||
|
ASSERT_TRUE(stream.good());
|
||||||
|
|
||||||
|
stream.write(data, n);
|
||||||
|
ASSERT_EQ(std::string(stream.view()), std::string(data));
|
||||||
|
|
||||||
|
stream.seekp(0);
|
||||||
|
ASSERT_TRUE(stream.good());
|
||||||
|
stream.write("Remove", 6);
|
||||||
|
ASSERT_EQ(std::string(stream.view()), "Remove world!");
|
||||||
|
|
||||||
|
auto buffer = stream.release();
|
||||||
|
ASSERT_EQ(std::string(buffer.data(), buffer.size()), "Remove world!");
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue