1 #include "trace_file.hpp"
13 using namespace Trace;
15 #define SNAPPY_BYTE1 'a'
16 #define SNAPPY_BYTE2 't'
18 File::File(const std::string &filename,
20 : m_filename(filename),
24 if (!m_filename.empty()) {
25 open(m_filename, m_mode);
35 bool File::isOpened() const
40 File::Mode File::mode() const
45 std::string File::filename() const
50 bool File::open(const std::string &filename, File::Mode mode)
55 m_isOpened = rawOpen(filename, mode);
61 bool File::write(const void *buffer, int length)
63 if (!m_isOpened || m_mode != File::Write) {
66 return rawWrite(buffer, length);
69 bool File::read(void *buffer, int length)
71 if (!m_isOpened || m_mode != File::Read) {
74 return rawRead(buffer, length);
85 void File::flush(FlushType type)
92 if (!m_isOpened || m_mode != File::Read) {
98 bool File::isZLibCompressed(const std::string &filename)
100 std::fstream stream(filename.c_str(),
101 std::fstream::binary | std::fstream::in);
102 if (!stream.is_open())
105 unsigned char byte1, byte2;
110 return (byte1 == 0x1f && byte2 == 0x8b);
114 bool File::isSnappyCompressed(const std::string &filename)
116 std::fstream stream(filename.c_str(),
117 std::fstream::binary | std::fstream::in);
118 if (!stream.is_open())
121 unsigned char byte1, byte2;
126 return (byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
130 ZLibFile::ZLibFile(const std::string &filename,
132 : File(filename, mode),
137 ZLibFile::~ZLibFile()
141 bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode)
143 m_gzFile = gzopen(filename.c_str(),
144 (mode == File::Write) ? "wb" : "rb");
145 return m_gzFile != NULL;
148 bool ZLibFile::rawWrite(const void *buffer, int length)
150 return gzwrite(m_gzFile, buffer, length) != -1;
153 bool ZLibFile::rawRead(void *buffer, int length)
155 return gzread(m_gzFile, buffer, length) != -1;
158 int ZLibFile::rawGetc()
160 return gzgetc(m_gzFile);
163 void ZLibFile::rawClose()
171 void ZLibFile::rawFlush(FlushType type)
173 gzflush(m_gzFile, Z_SYNC_FLUSH);
176 SnappyFile::SnappyFile(const std::string &filename,
183 m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
186 SnappyFile::~SnappyFile()
188 delete [] m_compressedCache;
191 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
193 std::ios_base::openmode fmode = std::fstream::binary;
194 if (mode == File::Write) {
195 fmode |= (std::fstream::out | std::fstream::trunc);
196 createCache(SNAPPY_CHUNK_SIZE);
197 } else if (mode == File::Read) {
198 fmode |= std::fstream::in;
201 m_stream.open(filename.c_str(), fmode);
203 //read in the initial buffer if we're reading
204 if (m_stream.is_open() && mode == File::Read) {
205 // read the snappy file identifier
206 unsigned char byte1, byte2;
209 assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
212 } else if (m_stream.is_open() && mode == File::Write) {
213 // write the snappy file identifier
214 m_stream << SNAPPY_BYTE1;
215 m_stream << SNAPPY_BYTE2;
217 return m_stream.is_open();
220 bool SnappyFile::rawWrite(const void *buffer, int length)
222 if (freeCacheSize() > length) {
223 memcpy(m_cachePtr, buffer, length);
224 m_cachePtr += length;
225 } else if (freeCacheSize() == length) {
226 memcpy(m_cachePtr, buffer, length);
227 m_cachePtr += length;
230 int sizeToWrite = length;
232 while (sizeToWrite >= freeCacheSize()) {
233 int endSize = freeCacheSize();
234 int offset = length - sizeToWrite;
235 memcpy(m_cachePtr, (char*)buffer + offset, endSize);
236 sizeToWrite -= endSize;
237 m_cachePtr += endSize;
241 int offset = length - sizeToWrite;
242 memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
243 m_cachePtr += sizeToWrite;
250 bool SnappyFile::rawRead(void *buffer, int length)
252 if (m_stream.eof()) {
255 if (freeCacheSize() > length) {
256 memcpy(buffer, m_cachePtr, length);
257 m_cachePtr += length;
258 } else if (freeCacheSize() == length) {
259 memcpy(buffer, m_cachePtr, length);
260 m_cachePtr += length;
263 int sizeToRead = length;
266 int chunkSize = std::min(freeCacheSize(), sizeToRead);
267 offset = length - sizeToRead;
268 memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
269 m_cachePtr += chunkSize;
270 sizeToRead -= chunkSize;
281 int SnappyFile::rawGetc()
289 void SnappyFile::rawClose()
298 void SnappyFile::rawFlush(FlushType type)
300 if (type == FlushDeep) {
306 void SnappyFile::flushCache()
308 if (m_mode == File::Write) {
309 size_t compressedLength;
311 ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
312 m_compressedCache, &compressedLength);
314 m_stream << compressedLength;
315 m_stream.write(m_compressedCache, compressedLength);
316 m_cachePtr = m_cache;
317 } else if (m_mode == File::Read) {
320 //assert(m_cachePtr == m_cache + m_cacheSize);
321 size_t compressedLength;
322 m_stream >> compressedLength;
323 m_stream.read((char*)m_compressedCache, compressedLength);
324 ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
328 createCache(m_cacheSize);
329 ::snappy::RawUncompress(m_compressedCache, compressedLength,
334 void SnappyFile::createCache(size_t size)
336 m_cache = new char[size];
337 m_cachePtr = m_cache;