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);
{
}
-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())
fclose(f);
}
-void FileOutputStream::write(const void *data, size_t len)
+void FileOutputStream::write_internal(const void *data, size_t len)
{
size_t res;
#include <string>
#include <sstream>
+/* In memory datatype to represent key/value pairs of information, such as file
+ * metadata. Currently implemented as map<string, string>. */
typedef std::map<std::string, std::string> 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;
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);
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);