Added base-writer class

This commit is contained in:
2023-07-24 17:22:35 -04:00
parent ab005fc968
commit a38f40e388
3 changed files with 202 additions and 262 deletions

View File

@@ -145,41 +145,6 @@ eng::string StreamBuffer::readline() {
}
}
// These routines return true if you can losslessly cast the
// specified value to the specified type.
static inline bool safe_to_cast_to_int8(int64_t vv) {
return ((vv + 0x80LL) & 0xFFFFFFFFFFFFFF00LL) == 0;
}
static inline bool safe_to_cast_to_int16(int64_t vv) {
return ((vv + 0x8000LL) & 0xFFFFFFFFFFFF0000LL) == 0;
}
static inline bool safe_to_cast_to_int32(int64_t vv) {
return ((vv + 0x80000000LL) & 0xFFFFFFFF00000000LL) == 0;
}
static inline bool safe_to_cast_to_int64(int64_t vv) {
return true;
}
static inline bool safe_to_cast_to_uint8(uint64_t vv) {
return (vv & 0xFFFFFFFFFFFFFF00LL) == 0;
}
static inline bool safe_to_cast_to_uint16(uint64_t vv) {
return (vv & 0xFFFFFFFFFFFF0000LL) == 0;
}
static inline bool safe_to_cast_to_uint32(uint64_t vv) {
return (vv & 0xFFFFFFFF00000000LL) == 0;
}
static inline bool safe_to_cast_to_uint64(uint64_t vv) {
return true;
}
void StreamBuffer::write_bytes(const char *s, int64_t len) {
make_space(len);
memcpy(write_cursor_, s, len);
@@ -187,11 +152,10 @@ void StreamBuffer::write_bytes(const char *s, int64_t len) {
}
void StreamBuffer::write_bytes(std::string_view s) {
make_space(s.size());
memcpy(write_cursor_, s.data(), s.size());
write_cursor_ += s.size();
write_bytes(s.data(), s.size());
}
const char *StreamBuffer::read_bytes(int64_t bytes) {
check_available(bytes);
char *data = read_cursor_;
@@ -199,87 +163,12 @@ const char *StreamBuffer::read_bytes(int64_t bytes) {
return data;
}
void StreamBuffer::write_int8(int64_t vv) {
assert(safe_to_cast_to_int8(vv));
int8_t v = vv;
make_space(1);
memcpy(write_cursor_, &v, 1);
write_cursor_ += 1;
void StreamBuffer::read_bytes_into(char *data, int64_t size) {
check_available(size);
memcpy(data, read_cursor_, size);
read_cursor_ += size;
}
void StreamBuffer::write_int16(int64_t vv) {
assert(safe_to_cast_to_int16(vv));
int16_t v = vv;
make_space(2);
memcpy(write_cursor_, &v, 2);
write_cursor_ += 2;
}
void StreamBuffer::write_int32(int64_t vv) {
assert(safe_to_cast_to_int32(vv));
int32_t v = vv;
make_space(4);
memcpy(write_cursor_, &v, 4);
write_cursor_ += 4;
}
void StreamBuffer::write_int64(int64_t vv) {
assert(safe_to_cast_to_int64(vv));
int64_t v = vv;
make_space(8);
memcpy(write_cursor_, &v, 8);
write_cursor_ += 8;
}
void StreamBuffer::write_uint8(uint64_t vv) {
assert(safe_to_cast_to_uint8(vv));
uint8_t v = vv;
make_space(1);
memcpy(write_cursor_, &v, 1);
write_cursor_ += 1;
}
void StreamBuffer::write_uint16(uint64_t vv) {
assert(safe_to_cast_to_uint16(vv));
uint16_t v = vv;
make_space(2);
memcpy(write_cursor_, &v, 2);
write_cursor_ += 2;
}
void StreamBuffer::write_uint32(uint64_t vv) {
assert(safe_to_cast_to_uint32(vv));
uint32_t v = vv;
make_space(4);
memcpy(write_cursor_, &v, 4);
write_cursor_ += 4;
}
void StreamBuffer::write_uint64(uint64_t vv) {
assert(safe_to_cast_to_uint64(vv));
uint64_t v = vv;
make_space(8);
memcpy(write_cursor_, &v, 8);
write_cursor_ += 8;
}
void StreamBuffer::write_char(char c) {
make_space(1);
write_cursor_[0] = c;
write_cursor_ += 1;
}
void StreamBuffer::write_float(float f) {
make_space(4);
memcpy(write_cursor_, &f, 4);
write_cursor_ += 4;
}
void StreamBuffer::write_double(double d) {
make_space(8);
memcpy(write_cursor_, &d, 8);
write_cursor_ += 8;
}
void StreamBuffer::write_xyz(const util::XYZ &xyz) {
make_space(12);
@@ -301,61 +190,6 @@ void StreamBuffer::write_dxyz(const util::DXYZ &xyz) {
write_cursor_ += 8;
}
int8_t StreamBuffer::read_int8() {
check_available(1);
int8_t v;
memcpy(&v, read_cursor_, 1);
read_cursor_ += 1;
return v;
}
int16_t StreamBuffer::read_int16() {
check_available(2);
int16_t v;
memcpy(&v, read_cursor_, 2);
read_cursor_ += 2;
return v;
}
int32_t StreamBuffer::read_int32() {
check_available(4);
int32_t v;
memcpy(&v, read_cursor_, 4);
read_cursor_ += 4;
return v;
}
int64_t StreamBuffer::read_int64() {
check_available(8);
int64_t v;
memcpy(&v, read_cursor_, 8);
read_cursor_ += 8;
return v;
}
char StreamBuffer::read_char() {
check_available(1);
char c = read_cursor_[0];
read_cursor_ += 1;
return c;
}
float StreamBuffer::read_float() {
check_available(4);
float f;
memcpy(&f, read_cursor_, 4);
read_cursor_ += 4;
return f;
}
double StreamBuffer::read_double() {
check_available(8);
double d;
memcpy(&d, read_cursor_, 8);
read_cursor_ += 8;
return d;
}
util::XYZ StreamBuffer::read_xyz() {
check_available(12);
util::XYZ result;
@@ -380,45 +214,17 @@ util::DXYZ StreamBuffer::read_dxyz() {
return result;
}
void StreamBuffer::write_hashvalue(const util::HashValue &hv) {
write_uint64(hv.first);
write_uint64(hv.second);
}
void StreamBuffer::write_string(std::string_view s) {
if (s.size() >= 255) {
write_uint8(0xFF);
write_uint64(s.size());
write_bytes(s);
} else {
write_uint8(s.size());
write_bytes(s);
}
}
util::HashValue StreamBuffer::read_hashvalue() {
uint64_t f = read_uint64();
uint64_t s = read_uint64();
return util::HashValue(f,s);
}
eng::string StreamBuffer::read_string() {
return read_string_limit(0xFFFFFFF);
}
eng::string StreamBuffer::read_string_limit(int64_t max_allowed) {
int64_t len = read_uint8();
if (len == 255) {
len = read_int64();
}
if (len < 0) throw StreamCorruption();
if (len > max_allowed) throw StreamCorruption();
const char *bytes = read_bytes(len);
return eng::string(bytes, len);
}
eng::string StreamBuffer::read_entire_contents() {
eng::string result(read_cursor_, fill());
read_cursor_ = write_cursor_;
@@ -426,57 +232,57 @@ eng::string StreamBuffer::read_entire_contents() {
}
void StreamBuffer::overwrite_int8(int64_t write_count_after, int64_t vv) {
assert(safe_to_cast_to_int8(vv));
int8_t v = vv;
assert(int64_t(v) == vv);
char *target = get_overwrite(1, write_count_after);
memcpy(target, &v, 1);
}
void StreamBuffer::overwrite_int16(int64_t write_count_after, int64_t vv) {
assert(safe_to_cast_to_int16(vv));
int16_t v = vv;
assert(int64_t(v) == vv);
char *target = get_overwrite(2, write_count_after);
memcpy(target, &v, 2);
}
void StreamBuffer::overwrite_int32(int64_t write_count_after, int64_t vv) {
assert(safe_to_cast_to_int32(vv));
int32_t v = vv;
assert(int64_t(v) == vv);
char *target = get_overwrite(4, write_count_after);
memcpy(target, &v, 4);
}
void StreamBuffer::overwrite_int64(int64_t write_count_after, int64_t vv) {
assert(safe_to_cast_to_int64(vv));
int64_t v = vv;
assert(int64_t(v) == vv);
char *target = get_overwrite(8, write_count_after);
memcpy(target, &v, 8);
}
void StreamBuffer::overwrite_uint8(int64_t write_count_after, uint64_t vv) {
assert(safe_to_cast_to_uint8(vv));
uint8_t v = vv;
assert(uint64_t(v) == vv);
char *target = get_overwrite(1, write_count_after);
memcpy(target, &v, 1);
}
void StreamBuffer::overwrite_uint16(int64_t write_count_after, uint64_t vv) {
assert(safe_to_cast_to_uint16(vv));
uint16_t v = vv;
assert(uint64_t(v) == vv);
char *target = get_overwrite(2, write_count_after);
memcpy(target, &v, 2);
}
void StreamBuffer::overwrite_uint32(int64_t write_count_after, uint64_t vv) {
assert(safe_to_cast_to_uint32(vv));
uint32_t v = vv;
assert(uint64_t(v) == vv);
char *target = get_overwrite(4, write_count_after);
memcpy(target, &v, 4);
}
void StreamBuffer::overwrite_uint64(int64_t write_count_after, uint64_t vv) {
assert(safe_to_cast_to_uint64(vv));
uint64_t v = vv;
assert(uint64_t(v) == vv);
char *target = get_overwrite(8, write_count_after);
memcpy(target, &v, 8);
}