Some changes to the design of base-writer.hpp

This commit is contained in:
2023-09-11 11:58:26 -04:00
parent b7b215f79c
commit ff4ed323e0
3 changed files with 28 additions and 12 deletions

View File

@@ -235,8 +235,12 @@ class ReplayLogfile : public BaseReader<ReplayLogfile>, public std::ifstream {
using std::ifstream::ifstream; using std::ifstream::ifstream;
public: public:
using read_string_type = std::string; using read_string_type = std::string;
void read_bytes_into(char *n, size_t size) { read(n, size); } void read_bytes_into(char *n, size_t size) {
bool read_beyond_eof() { return !good(); } read(n, size);
if (!good()) {
memset(n, 0, size);
}
}
void raise_string_too_long() { void raise_string_too_long() {
fprintf(stderr, "string in logfile is too long"); fprintf(stderr, "string in logfile is too long");
std::abort(); std::abort();

View File

@@ -302,6 +302,8 @@ public:
// Copy bytes from the StreamBuffer into an external buffer. // Copy bytes from the StreamBuffer into an external buffer.
// //
// Returns true if the bytes were successfully read.
//
void read_bytes_into(char *target, int64_t len); void read_bytes_into(char *target, int64_t len);
// Read a string as a string_view. // Read a string as a string_view.
@@ -387,10 +389,6 @@ public:
void raise_truncated() { throw StreamCorruption(); } void raise_truncated() { throw StreamCorruption(); }
void raise_string_too_long() { throw StreamCorruption(); } void raise_string_too_long() { throw StreamCorruption(); }
// This is always false, because this module throws exceptions
// when reading beyond EOF.
bool read_beyond_eof() { return false; }
private: private:
// Start and end of the allocated block. // Start and end of the allocated block.
char *buf_lo_; char *buf_lo_;

View File

@@ -118,8 +118,21 @@ public:
// //
// using read_string_type = std::string; // or compatible // using read_string_type = std::string; // or compatible
// void read_bytes_into(char *n, size_t size) // void read_bytes_into(char *n, size_t size)
// void raise_string_too_long(); // void handle_string_too_long();
// bool read_beyond_eof(); //
// Error Handling:
//
// It is up to the derived class whether it wants
// to report errors using exceptions or flags.
//
// If read_bytes_into discovers there's not enough bytes,
// there are two valid options: throw an exception, OR,
// set an error flag and fill the buffer with zeros.
//
// If read_string discovers that the string is longer than
// the allowed limit, it will call handle_string_too_long.
// This function may either throw an exception, or set an
// error flag.
// //
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
@@ -131,7 +144,6 @@ protected:
T result; T result;
Derived *dthis = static_cast<Derived*>(this); Derived *dthis = static_cast<Derived*>(this);
dthis->read_bytes_into((char *)(&result), sizeof(result)); dthis->read_bytes_into((char *)(&result), sizeof(result));
if (dthis->read_beyond_eof()) result = 0;
return result; return result;
} }
@@ -146,7 +158,7 @@ public:
int32_t read_int32() { return read_value_core<int32_t>(); } int32_t read_int32() { return read_value_core<int32_t>(); }
int64_t read_int64() { return read_value_core<int64_t>(); } int64_t read_int64() { return read_value_core<int64_t>(); }
bool read_bool() { return read_uint8(); } bool read_bool() { return (bool)read_uint8(); }
char read_char() { return read_value_core<char>(); } char read_char() { return read_value_core<char>(); }
float read_float() { return read_value_core<float>(); } float read_float() { return read_value_core<float>(); }
double read_double() { return read_value_core<double>(); } double read_double() { return read_value_core<double>(); }
@@ -162,10 +174,12 @@ public:
auto read_string_limit(uint64_t limit) { auto read_string_limit(uint64_t limit) {
size_t len = read_length(); size_t len = read_length();
Derived *dthis = static_cast<Derived*>(this); Derived *dthis = static_cast<Derived*>(this);
if (len > limit) dthis->raise_string_too_long(); if (len > limit) {
dthis->raise_string_too_long();
len = 0;
}
typename Derived::read_string_type result(len, ' '); typename Derived::read_string_type result(len, ' ');
dthis->read_bytes_into(&(result[0]), len); dthis->read_bytes_into(&(result[0]), len);
if (dthis->read_beyond_eof()) result.clear();
return result; return result;
} }