]> git.cworth.org Git - apitrace/blob - trace_file.hpp
Abstract file writing operation into a class of its own.
[apitrace] / trace_file.hpp
1 #ifndef TRACE_FILE_HPP
2 #define TRACE_FILE_HPP
3
4 #include <string>
5
6 namespace Trace {
7
8 class File {
9 public:
10     enum Mode {
11         Read,
12         Write
13     };
14 public:
15     File(const std::string &filename = std::string(),
16          File::Mode mode = File::Read);
17     virtual ~File();
18
19     bool isOpened() const;
20     File::Mode mode() const;
21     std::string filename() const;
22
23     bool open(const std::string &filename, File::Mode mode);
24     bool write(const void *buffer, int length);
25     bool read(void *buffer, int length);
26     void close();
27     void flush();
28     char getc();
29
30 protected:
31     virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
32     virtual bool rawWrite(const void *buffer, int length) = 0;
33     virtual bool rawRead(void *buffer, int length) = 0;
34     virtual char rawGetc() = 0;
35     virtual void rawClose() = 0;
36     virtual void rawFlush() = 0;
37
38 protected:
39     std::string m_filename;
40     File::Mode m_mode;
41     bool m_isOpened;
42 };
43
44 class ZLibFile : public File {
45 public:
46     ZLibFile(const std::string &filename = std::string(),
47              File::Mode mode = File::Read);
48     virtual ~ZLibFile();
49
50 protected:
51     virtual bool rawOpen(const std::string &filename, File::Mode mode);
52     virtual bool rawWrite(const void *buffer, int length);
53     virtual bool rawRead(void *buffer, int length);
54     virtual char rawGetc();
55     virtual void rawClose();
56     virtual void rawFlush();
57 private:
58     void *m_gzFile;
59 };
60
61 }
62
63 #endif