Some changes to the design of base-writer.hpp
This commit is contained in:
@@ -235,8 +235,12 @@ class ReplayLogfile : public BaseReader<ReplayLogfile>, public std::ifstream {
|
||||
using std::ifstream::ifstream;
|
||||
public:
|
||||
using read_string_type = std::string;
|
||||
void read_bytes_into(char *n, size_t size) { read(n, size); }
|
||||
bool read_beyond_eof() { return !good(); }
|
||||
void read_bytes_into(char *n, size_t size) {
|
||||
read(n, size);
|
||||
if (!good()) {
|
||||
memset(n, 0, size);
|
||||
}
|
||||
}
|
||||
void raise_string_too_long() {
|
||||
fprintf(stderr, "string in logfile is too long");
|
||||
std::abort();
|
||||
|
||||
@@ -302,6 +302,8 @@ public:
|
||||
|
||||
// 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);
|
||||
|
||||
// Read a string as a string_view.
|
||||
@@ -387,10 +389,6 @@ public:
|
||||
void raise_truncated() { 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:
|
||||
// Start and end of the allocated block.
|
||||
char *buf_lo_;
|
||||
|
||||
@@ -118,8 +118,21 @@ public:
|
||||
//
|
||||
// using read_string_type = std::string; // or compatible
|
||||
// void read_bytes_into(char *n, size_t size)
|
||||
// void raise_string_too_long();
|
||||
// bool read_beyond_eof();
|
||||
// void handle_string_too_long();
|
||||
//
|
||||
// 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;
|
||||
Derived *dthis = static_cast<Derived*>(this);
|
||||
dthis->read_bytes_into((char *)(&result), sizeof(result));
|
||||
if (dthis->read_beyond_eof()) result = 0;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -146,7 +158,7 @@ public:
|
||||
int32_t read_int32() { return read_value_core<int32_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>(); }
|
||||
float read_float() { return read_value_core<float>(); }
|
||||
double read_double() { return read_value_core<double>(); }
|
||||
@@ -162,10 +174,12 @@ public:
|
||||
auto read_string_limit(uint64_t limit) {
|
||||
size_t len = read_length();
|
||||
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, ' ');
|
||||
dthis->read_bytes_into(&(result[0]), len);
|
||||
if (dthis->read_beyond_eof()) result.clear();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user