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