Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 1 | #ifndef TRACE_FILE_HPP |
| 2 | #define TRACE_FILE_HPP |
| 3 | |
| 4 | #include <string> |
Zack Rusin | a26cf3e | 2011-08-06 16:12:09 -0400 | [diff] [blame] | 5 | #include <fstream> |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 6 | |
| 7 | namespace Trace { |
| 8 | |
| 9 | class File { |
| 10 | public: |
| 11 | enum Mode { |
| 12 | Read, |
| 13 | Write |
| 14 | }; |
| 15 | public: |
Zack Rusin | 14b78f8 | 2011-08-06 19:26:46 -0400 | [diff] [blame] | 16 | static bool isZLibCompressed(const std::string &filename); |
Zack Rusin | 6dce37c | 2011-08-08 09:59:58 -0400 | [diff] [blame^] | 17 | static bool isSnappyCompressed(const std::string &filename); |
Zack Rusin | 14b78f8 | 2011-08-06 19:26:46 -0400 | [diff] [blame] | 18 | public: |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 19 | 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 Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 32 | int getc(); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 33 | |
| 34 | protected: |
| 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 Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 38 | virtual int rawGetc() = 0; |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 39 | virtual void rawClose() = 0; |
| 40 | virtual void rawFlush() = 0; |
| 41 | |
| 42 | protected: |
| 43 | std::string m_filename; |
| 44 | File::Mode m_mode; |
| 45 | bool m_isOpened; |
| 46 | }; |
| 47 | |
| 48 | class ZLibFile : public File { |
| 49 | public: |
| 50 | ZLibFile(const std::string &filename = std::string(), |
| 51 | File::Mode mode = File::Read); |
| 52 | virtual ~ZLibFile(); |
| 53 | |
| 54 | protected: |
| 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 Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 58 | virtual int rawGetc(); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 59 | virtual void rawClose(); |
| 60 | virtual void rawFlush(); |
| 61 | private: |
| 62 | void *m_gzFile; |
| 63 | }; |
| 64 | |
Zack Rusin | a26cf3e | 2011-08-06 16:12:09 -0400 | [diff] [blame] | 65 | namespace snappy { |
| 66 | class File; |
| 67 | } |
| 68 | |
| 69 | #define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024) |
| 70 | class SnappyFile : public File { |
| 71 | public: |
| 72 | SnappyFile(const std::string &filename = std::string(), |
| 73 | File::Mode mode = File::Read); |
| 74 | virtual ~SnappyFile(); |
| 75 | |
| 76 | protected: |
| 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 Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 80 | virtual int rawGetc(); |
Zack Rusin | a26cf3e | 2011-08-06 16:12:09 -0400 | [diff] [blame] | 81 | virtual void rawClose(); |
| 82 | virtual void rawFlush(); |
| 83 | |
| 84 | private: |
| 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); |
| 94 | private: |
| 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 Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | #endif |