1
2
3
4
5
6 #ifndef SOULNG_UTIL_BINARY_WRITER_INCLUDED
7 #define SOULNG_UTIL_BINARY_WRITER_INCLUDED
8 #include <soulng/util/FilePtr.hpp>
9 #include <string>
10 #include <boost/uuid/uuid.hpp>
11
12 namespace soulng { namespace util {
13
14 class BinaryWriter
15 {
16 public:
17 BinaryWriter(const std::string& fileName_);
18 virtual ~BinaryWriter();
19 void Write(bool x);
20 void Write(uint8_t x);
21 void Write(int8_t x);
22 void Write(uint16_t x);
23 void Write(int16_t x);
24 void Write(uint32_t x);
25 void Write(int32_t x);
26 void Write(uint64_t x);
27 void Write(int64_t x);
28 void Write(float x);
29 void Write(double x);
30 void Write(char x);
31 void Write(char16_t x);
32 void Write(char32_t x);
33 void Write(const std::string& s);
34 void Write(const std::string& s, bool writeNull);
35 void Write(const std::u16string& s);
36 void Write(const std::u32string& s);
37 void WriteULEB128UInt(uint32_t x);
38 void WriteULEB128ULong(uint64_t x);
39 void WriteSLEB128Int(int32_t x);
40 void WriteSLEB128Long(int64_t x);
41 void Write(const boost::uuids::uuid& uuid);
42 uint32_t Pos() const { return pos; }
43 void Seek(uint32_t pos_);
44 private:
45 static const int N = 8192;
46 std::string fileName;
47 FilePtr file;
48 uint8_t buffer[N];
49 uint8_t* bufp;
50 uint8_t* bufend;
51 uint32_t pos;
52 void BufferReset() { bufp = buffer; bufend = buffer + N; }
53 bool BufferFull() const { return bufp == bufend; }
54 void FlushBuffer();
55 };
56
57 } }
58
59 #endif // SOULNG_UTIL_BINARY_WRITER_INCLUDED