]> git.cworth.org Git - apitrace/blob - trace_snappyfile.hpp
601bc12d0154ebab6a8b13b248adc3c8d6b91775
[apitrace] / trace_snappyfile.hpp
1 #ifndef TRACE_SNAPPYFILE_HPP
2 #define TRACE_SNAPPYFILE_HPP
3
4 #include "trace_file.hpp"
5
6 #include <string>
7 #include <fstream>
8
9 #include <stdint.h>
10
11 namespace snappy {
12     class File;
13 }
14
15 namespace Trace {
16
17 #define SNAPPY_CHUNK_SIZE (1 * 1024 * 1024)
18
19 #define SNAPPY_BYTE1 'a'
20 #define SNAPPY_BYTE2 't'
21
22
23 class SnappyFile : public File {
24 public:
25     SnappyFile(const std::string &filename = std::string(),
26                File::Mode mode = File::Read);
27     virtual ~SnappyFile();
28
29 protected:
30     virtual bool rawOpen(const std::string &filename, File::Mode mode);
31     virtual bool rawWrite(const void *buffer, int length);
32     virtual bool rawRead(void *buffer, int length);
33     virtual int rawGetc();
34     virtual void rawClose();
35     virtual void rawFlush(FlushType type);
36
37 private:
38     inline int freeCacheSize() const
39     {
40         if (m_cacheSize > 0)
41             return m_cacheSize - (m_cachePtr - m_cache);
42         else
43             return 0;
44     }
45     void flushCache();
46     void createCache(size_t size);
47     void writeCompressedLength(uint32_t  num);
48     uint32_t readCompressedLength();
49 private:
50     std::fstream m_stream;
51     char *m_cache;
52     char *m_cachePtr;
53     size_t m_cacheSize;
54
55     char *m_compressedCache;
56 };
57
58 }
59
60 #endif // TRACE_SNAPPYFILE_HPP