blob: 5a460f79b0c13e1027970d89c6b281757878f389 [file] [log] [blame]
Zack Rusin5ce45e72011-08-05 13:43:46 -04001#ifndef TRACE_FILE_HPP
2#define TRACE_FILE_HPP
3
4#include <string>
Zack Rusina26cf3e2011-08-06 16:12:09 -04005#include <fstream>
Zack Rusin5ce45e72011-08-05 13:43:46 -04006
7namespace Trace {
8
9class File {
10public:
11 enum Mode {
12 Read,
13 Write
14 };
15public:
16 File(const std::string &filename = std::string(),
17 File::Mode mode = File::Read);
18 virtual ~File();
19
20 bool isOpened() const;
21 File::Mode mode() const;
22 std::string filename() const;
23
24 bool open(const std::string &filename, File::Mode mode);
25 bool write(const void *buffer, int length);
26 bool read(void *buffer, int length);
27 void close();
28 void flush();
29 char getc();
30
31protected:
32 virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
33 virtual bool rawWrite(const void *buffer, int length) = 0;
34 virtual bool rawRead(void *buffer, int length) = 0;
35 virtual char rawGetc() = 0;
36 virtual void rawClose() = 0;
37 virtual void rawFlush() = 0;
38
39protected:
40 std::string m_filename;
41 File::Mode m_mode;
42 bool m_isOpened;
43};
44
45class ZLibFile : public File {
46public:
47 ZLibFile(const std::string &filename = std::string(),
48 File::Mode mode = File::Read);
49 virtual ~ZLibFile();
50
51protected:
52 virtual bool rawOpen(const std::string &filename, File::Mode mode);
53 virtual bool rawWrite(const void *buffer, int length);
54 virtual bool rawRead(void *buffer, int length);
55 virtual char rawGetc();
56 virtual void rawClose();
57 virtual void rawFlush();
58private:
59 void *m_gzFile;
60};
61
Zack Rusina26cf3e2011-08-06 16:12:09 -040062namespace snappy {
63 class File;
64}
65
66#define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024)
67class SnappyFile : public File {
68public:
69 SnappyFile(const std::string &filename = std::string(),
70 File::Mode mode = File::Read);
71 virtual ~SnappyFile();
72
73protected:
74 virtual bool rawOpen(const std::string &filename, File::Mode mode);
75 virtual bool rawWrite(const void *buffer, int length);
76 virtual bool rawRead(void *buffer, int length);
77 virtual char rawGetc();
78 virtual void rawClose();
79 virtual void rawFlush();
80
81private:
82 inline int freeCacheSize() const
83 {
84 if (m_cacheSize > 0)
85 return m_cacheSize - (m_cachePtr - m_cache);
86 else
87 return 0;
88 }
89 void flushCache();
90 void createCache(size_t size);
91private:
92 std::fstream m_stream;
93 char *m_cache;
94 char *m_cachePtr;
95 size_t m_cacheSize;
96
97 char *m_compressedCache;
98};
99
100
Zack Rusin5ce45e72011-08-05 13:43:46 -0400101}
102
103#endif