blob: 3afa7096553c97a2ee7281862e5eaa85db0590f8 [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:
Zack Rusin14b78f82011-08-06 19:26:46 -040016 static bool isZLibCompressed(const std::string &filename);
17public:
Zack Rusin5ce45e72011-08-05 13:43:46 -040018 File(const std::string &filename = std::string(),
19 File::Mode mode = File::Read);
20 virtual ~File();
21
22 bool isOpened() const;
23 File::Mode mode() const;
24 std::string filename() const;
25
26 bool open(const std::string &filename, File::Mode mode);
27 bool write(const void *buffer, int length);
28 bool read(void *buffer, int length);
29 void close();
30 void flush();
Zack Rusinbb130e52011-08-06 18:58:39 -040031 int getc();
Zack Rusin5ce45e72011-08-05 13:43:46 -040032
33protected:
34 virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
35 virtual bool rawWrite(const void *buffer, int length) = 0;
36 virtual bool rawRead(void *buffer, int length) = 0;
Zack Rusinbb130e52011-08-06 18:58:39 -040037 virtual int rawGetc() = 0;
Zack Rusin5ce45e72011-08-05 13:43:46 -040038 virtual void rawClose() = 0;
39 virtual void rawFlush() = 0;
40
41protected:
42 std::string m_filename;
43 File::Mode m_mode;
44 bool m_isOpened;
45};
46
47class ZLibFile : public File {
48public:
49 ZLibFile(const std::string &filename = std::string(),
50 File::Mode mode = File::Read);
51 virtual ~ZLibFile();
52
53protected:
54 virtual bool rawOpen(const std::string &filename, File::Mode mode);
55 virtual bool rawWrite(const void *buffer, int length);
56 virtual bool rawRead(void *buffer, int length);
Zack Rusinbb130e52011-08-06 18:58:39 -040057 virtual int rawGetc();
Zack Rusin5ce45e72011-08-05 13:43:46 -040058 virtual void rawClose();
59 virtual void rawFlush();
60private:
61 void *m_gzFile;
62};
63
Zack Rusina26cf3e2011-08-06 16:12:09 -040064namespace snappy {
65 class File;
66}
67
68#define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024)
69class SnappyFile : public File {
70public:
71 SnappyFile(const std::string &filename = std::string(),
72 File::Mode mode = File::Read);
73 virtual ~SnappyFile();
74
75protected:
76 virtual bool rawOpen(const std::string &filename, File::Mode mode);
77 virtual bool rawWrite(const void *buffer, int length);
78 virtual bool rawRead(void *buffer, int length);
Zack Rusinbb130e52011-08-06 18:58:39 -040079 virtual int rawGetc();
Zack Rusina26cf3e2011-08-06 16:12:09 -040080 virtual void rawClose();
81 virtual void rawFlush();
82
83private:
84 inline int freeCacheSize() const
85 {
86 if (m_cacheSize > 0)
87 return m_cacheSize - (m_cachePtr - m_cache);
88 else
89 return 0;
90 }
91 void flushCache();
92 void createCache(size_t size);
93private:
94 std::fstream m_stream;
95 char *m_cache;
96 char *m_cachePtr;
97 size_t m_cacheSize;
98
99 char *m_compressedCache;
100};
101
102
Zack Rusin5ce45e72011-08-05 13:43:46 -0400103}
104
105#endif