]> git.cworth.org Git - apitrace/blob - trace_file.cpp
Export SnappyFile class to its own file.
[apitrace] / trace_file.cpp
1 #include "trace_file.hpp"
2
3 #include "trace_snappyfile.hpp"
4
5 #include <assert.h>
6 #include <string.h>
7
8 #include <zlib.h>
9
10 #include "os.hpp"
11
12 #include <iostream>
13
14 using namespace Trace;
15
16
17 File::File(const std::string &filename,
18            File::Mode mode)
19     : m_filename(filename),
20       m_mode(mode),
21       m_isOpened(false)
22 {
23     if (!m_filename.empty()) {
24         open(m_filename, m_mode);
25     }
26 }
27
28
29 File::~File()
30 {
31     close();
32 }
33
34 bool File::isOpened() const
35 {
36     return m_isOpened;
37 }
38
39 File::Mode File::mode() const
40 {
41     return m_mode;
42 }
43
44 std::string File::filename() const
45 {
46     return m_filename;
47 }
48
49 bool File::open(const std::string &filename, File::Mode mode)
50 {
51     if (m_isOpened) {
52         close();
53     }
54     m_isOpened = rawOpen(filename, mode);
55     m_mode = mode;
56
57     return m_isOpened;
58 }
59
60 bool File::write(const void *buffer, int length)
61 {
62     if (!m_isOpened || m_mode != File::Write) {
63         return false;
64     }
65     return rawWrite(buffer, length);
66 }
67
68 bool File::read(void *buffer, int length)
69 {
70     if (!m_isOpened || m_mode != File::Read) {
71         return false;
72     }
73     return rawRead(buffer, length);
74 }
75
76 void File::close()
77 {
78     if (m_isOpened) {
79         rawClose();
80         m_isOpened = false;
81     }
82 }
83
84 void File::flush(FlushType type)
85 {
86     rawFlush(type);
87 }
88
89 int File::getc()
90 {
91     if (!m_isOpened || m_mode != File::Read) {
92         return 0;
93     }
94     return rawGetc();
95 }
96
97 bool File::isZLibCompressed(const std::string &filename)
98 {
99     std::fstream stream(filename.c_str(),
100                         std::fstream::binary | std::fstream::in);
101     if (!stream.is_open())
102         return false;
103
104     unsigned char byte1, byte2;
105     stream >> byte1;
106     stream >> byte2;
107     stream.close();
108
109     return (byte1 == 0x1f && byte2 == 0x8b);
110 }
111
112
113 bool File::isSnappyCompressed(const std::string &filename)
114 {
115     std::fstream stream(filename.c_str(),
116                         std::fstream::binary | std::fstream::in);
117     if (!stream.is_open())
118         return false;
119
120     unsigned char byte1, byte2;
121     stream >> byte1;
122     stream >> byte2;
123     stream.close();
124
125     return (byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
126 }
127
128
129 ZLibFile::ZLibFile(const std::string &filename,
130                    File::Mode mode)
131     : File(filename, mode),
132       m_gzFile(NULL)
133 {
134 }
135
136 ZLibFile::~ZLibFile()
137 {
138 }
139
140 bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode)
141 {
142     m_gzFile = gzopen(filename.c_str(),
143                       (mode == File::Write) ? "wb" : "rb");
144     return m_gzFile != NULL;
145 }
146
147 bool ZLibFile::rawWrite(const void *buffer, int length)
148 {
149     return gzwrite(m_gzFile, buffer, length) != -1;
150 }
151
152 bool ZLibFile::rawRead(void *buffer, int length)
153 {
154     return gzread(m_gzFile, buffer, length) != -1;
155 }
156
157 int ZLibFile::rawGetc()
158 {
159     return gzgetc(m_gzFile);
160 }
161
162 void ZLibFile::rawClose()
163 {
164     if (m_gzFile) {
165         gzclose(m_gzFile);
166         m_gzFile = NULL;
167     }
168 }
169
170 void ZLibFile::rawFlush(FlushType type)
171 {
172     gzflush(m_gzFile, Z_SYNC_FLUSH);
173 }