From: Michael Vrable Date: Sun, 24 Dec 2006 01:19:58 +0000 (-0800) Subject: Improve comments, and track number of bytes written to a stream. X-Git-Url: https://git.vrable.net/?a=commitdiff_plain;h=a81fd6242d4a4076b7c79213de8cc7e13a311e56;p=cumulus.git Improve comments, and track number of bytes written to a stream. --- diff --git a/store.cc b/store.cc index 5df4b77..6770e1c 100644 --- a/store.cc +++ b/store.cc @@ -11,6 +11,17 @@ using std::string; +OutputStream::OutputStream() + : bytes_written(0) +{ +} + +void OutputStream::write(const void *data, size_t len) +{ + write_internal(data, len); + bytes_written += len; +} + void OutputStream::write_u8(uint8_t val) { write(&val, 1); @@ -95,7 +106,7 @@ StringOutputStream::StringOutputStream() { } -void StringOutputStream::write(const void *data, size_t len) +void StringOutputStream::write_internal(const void *data, size_t len) { buf.write((const char *)data, len); if (!buf.good()) @@ -112,7 +123,7 @@ FileOutputStream::~FileOutputStream() fclose(f); } -void FileOutputStream::write(const void *data, size_t len) +void FileOutputStream::write_internal(const void *data, size_t len) { size_t res; diff --git a/store.h b/store.h index 7950297..3725a9f 100644 --- a/store.h +++ b/store.h @@ -15,8 +15,14 @@ #include #include +/* In memory datatype to represent key/value pairs of information, such as file + * metadata. Currently implemented as map. */ typedef std::map dictionary; +/* IOException will be thrown if an error occurs while reading or writing in + * one of the I/O wrappers. Depending upon the context; this may be fatal or + * not--typically, errors reading/writing the store will be serious, but errors + * reading an individual file are less so. */ class IOException : public std::exception { private: std::string error; @@ -26,13 +32,22 @@ public: std::string getError() const { return error; } }; +/* OutputStream is an abstract interface for writing data without seeking. + * Output could be to a file, to an object within a segment, or even to a + * memory buffer to help serialize data. */ class OutputStream { public: + OutputStream(); virtual ~OutputStream() { } - virtual void write(const void *data, size_t len) = 0; - /* Convenience functions for writing other data types. Values are always - * written out in little-endian order. */ + // Write the given data buffer + void write(const void *data, size_t len); + + // Return the total number of bytes written so far + int64_t get_pos() const { return bytes_written; } + + // Convenience functions for writing other data types. Values are always + // written out in little-endian order. void write_u8(uint8_t val); void write_u16(uint16_t val); void write_u32(uint32_t val); @@ -45,28 +60,45 @@ public: void write_string(const std::string &s); void write_dictionary(const dictionary &d); + +protected: + // Function which actually causes a write: must be overridden by + // implementation. + virtual void write_internal(const void *data, size_t len) = 0; + +private: + int64_t bytes_written; }; +/* An OutputStream implementation which writes data to memory and returns the + * result as a string. */ class StringOutputStream : public OutputStream { -private: - std::stringstream buf; public: StringOutputStream(); - - virtual void write(const void *data, size_t len); std::string contents() const { return buf.str(); } + +protected: + virtual void write_internal(const void *data, size_t len); + +private: + std::stringstream buf; }; +/* An OutputStream implementation which writes data via the C stdio layer. */ class FileOutputStream : public OutputStream { -private: - FILE *f; public: explicit FileOutputStream(FILE *file); virtual ~FileOutputStream(); - virtual void write(const void *data, size_t len); +protected: + virtual void write_internal(const void *data, size_t len); + +private: + FILE *f; }; +/* Simple wrappers that encode integers using a StringOutputStream and return + * the encoded result. */ std::string encode_u16(uint16_t val); std::string encode_u32(uint32_t val); std::string encode_u64(uint64_t val);