1 /* LBS: An LFS-inspired filesystem backup system
2 * Copyright (C) 2006 Michael Vrable
4 * Backup data is stored in a collection of objects, which are grouped together
5 * into segments for storage purposes. This file provides interfaces for
6 * reading and writing objects and segments. */
14 void OutputStream::write_u8(uint8_t val)
19 void OutputStream::write_u16(uint16_t val)
24 buf[1] = (val >> 8) & 0xff;
28 void OutputStream::write_u32(uint32_t val)
33 buf[1] = (val >> 8) & 0xff;
34 buf[2] = (val >> 16) & 0xff;
35 buf[3] = (val >> 24) & 0xff;
39 void OutputStream::write_u64(uint64_t val)
44 buf[1] = (val >> 8) & 0xff;
45 buf[2] = (val >> 16) & 0xff;
46 buf[3] = (val >> 24) & 0xff;
47 buf[4] = (val >> 32) & 0xff;
48 buf[5] = (val >> 40) & 0xff;
49 buf[6] = (val >> 48) & 0xff;
50 buf[7] = (val >> 56) & 0xff;
54 /* Writes an integer to an output stream using a variable-sized representation:
55 * seven bits are written at a time (little-endian), and the eigth bit of each
56 * byte is set if more data follows. */
57 void OutputStream::write_varint(uint64_t val)
60 uint8_t remainder = (val & 0x7f);
68 /* Write an arbitrary string by first writing out the length, followed by the
70 void OutputStream::write_string(const string &s)
72 size_t len = s.length();
77 void OutputStream::write_dictionary(const dictionary &d)
79 size_t size = d.size();
84 for (dictionary::const_iterator i = d.begin(); i != d.end(); ++i) {
85 write_string(i->first);
86 write_string(i->second);
90 assert(written == size);
93 StringOutputStream::StringOutputStream()
94 : buf(std::ios_base::out)
98 void StringOutputStream::write(const void *data, size_t len)
100 buf.write((const char *)data, len);
102 throw IOException("error writing to StringOutputStream");
105 FileOutputStream::FileOutputStream(FILE *file)
110 FileOutputStream::~FileOutputStream()
115 void FileOutputStream::write(const void *data, size_t len)
119 res = fwrite(data, 1, len, f);
121 throw IOException("write error");
125 /* Utility functions, for encoding data types to strings. */
126 string encode_u16(uint16_t val)
128 StringOutputStream s;
133 string encode_u32(uint32_t val)
135 StringOutputStream s;
140 string encode_u64(uint64_t val)
142 StringOutputStream s;