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); |
| 17 | public: |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 18 | 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 Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 31 | int getc(); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 32 | |
| 33 | protected: |
| 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 Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 37 | virtual int rawGetc() = 0; |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 38 | virtual void rawClose() = 0; |
| 39 | virtual void rawFlush() = 0; |
| 40 | |
| 41 | protected: |
| 42 | std::string m_filename; |
| 43 | File::Mode m_mode; |
| 44 | bool m_isOpened; |
| 45 | }; |
| 46 | |
| 47 | class ZLibFile : public File { |
| 48 | public: |
| 49 | ZLibFile(const std::string &filename = std::string(), |
| 50 | File::Mode mode = File::Read); |
| 51 | virtual ~ZLibFile(); |
| 52 | |
| 53 | protected: |
| 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 Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 57 | virtual int rawGetc(); |
Zack Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 58 | virtual void rawClose(); |
| 59 | virtual void rawFlush(); |
| 60 | private: |
| 61 | void *m_gzFile; |
| 62 | }; |
| 63 | |
Zack Rusin | a26cf3e | 2011-08-06 16:12:09 -0400 | [diff] [blame] | 64 | namespace snappy { |
| 65 | class File; |
| 66 | } |
| 67 | |
| 68 | #define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024) |
| 69 | class SnappyFile : public File { |
| 70 | public: |
| 71 | SnappyFile(const std::string &filename = std::string(), |
| 72 | File::Mode mode = File::Read); |
| 73 | virtual ~SnappyFile(); |
| 74 | |
| 75 | protected: |
| 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 Rusin | bb130e5 | 2011-08-06 18:58:39 -0400 | [diff] [blame] | 79 | virtual int rawGetc(); |
Zack Rusin | a26cf3e | 2011-08-06 16:12:09 -0400 | [diff] [blame] | 80 | virtual void rawClose(); |
| 81 | virtual void rawFlush(); |
| 82 | |
| 83 | private: |
| 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); |
| 93 | private: |
| 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 Rusin | 5ce45e7 | 2011-08-05 13:43:46 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #endif |