1 #include "trace_file.hpp"
11 using namespace Trace;
13 #define SNAPPY_BYTE1 'a'
14 #define SNAPPY_BYTE2 't'
16 File::File(const std::string &filename,
18 : m_filename(filename),
22 if (!m_filename.empty()) {
23 open(m_filename, m_mode);
33 bool File::isOpened() const
38 File::Mode File::mode() const
43 std::string File::filename() const
48 bool File::open(const std::string &filename, File::Mode mode)
53 m_isOpened = rawOpen(filename, mode);
58 bool File::write(const void *buffer, int length)
60 if (!m_isOpened || m_mode != File::Write) {
63 return rawWrite(buffer, length);
66 bool File::read(void *buffer, int length)
68 if (!m_isOpened || m_mode != File::Read) {
71 return rawRead(buffer, length);
89 if (!m_isOpened || m_mode != File::Read) {
95 bool File::isZLibCompressed(const std::string &filename)
97 std::fstream stream(filename.c_str(),
98 std::fstream::binary | std::fstream::in);
99 if (!stream.is_open())
102 unsigned char byte1, byte2;
107 return (byte1 == 0x1f && byte2 == 0x8b);
111 bool File::isSnappyCompressed(const std::string &filename)
113 std::fstream stream(filename.c_str(),
114 std::fstream::binary | std::fstream::in);
115 if (!stream.is_open())
118 unsigned char byte1, byte2;
123 return (byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
127 ZLibFile::ZLibFile(const std::string &filename,
129 : File(filename, mode),
134 ZLibFile::~ZLibFile()
138 bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode)
140 m_gzFile = gzopen(filename.c_str(),
141 (mode == File::Write) ? "wb" : "rb");
142 return m_gzFile != NULL;
145 bool ZLibFile::rawWrite(const void *buffer, int length)
147 return gzwrite(m_gzFile, buffer, length) != -1;
150 bool ZLibFile::rawRead(void *buffer, int length)
152 return gzread(m_gzFile, buffer, length) != -1;
155 int ZLibFile::rawGetc()
157 return gzgetc(m_gzFile);
160 void ZLibFile::rawClose()
168 void ZLibFile::rawFlush()
170 gzflush(m_gzFile, Z_SYNC_FLUSH);
173 SnappyFile::SnappyFile(const std::string &filename,
180 m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
183 SnappyFile::~SnappyFile()
185 delete [] m_compressedCache;
188 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
190 std::ios_base::openmode fmode = std::fstream::binary;
191 if (mode == File::Write) {
192 fmode |= (std::fstream::out | std::fstream::trunc);
193 createCache(SNAPPY_CHUNK_SIZE);
194 } else if (mode == File::Read) {
195 fmode |= std::fstream::in;
198 m_stream.open(filename.c_str(), fmode);
200 //read in the initial buffer if we're reading
201 if (m_stream.is_open() && mode == File::Read) {
202 // read the snappy file identifier
203 unsigned char byte1, byte2;
206 assert(byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
209 } else if (m_stream.is_open() && mode == File::Write) {
210 // write the snappy file identifier
211 m_stream << SNAPPY_BYTE1;
212 m_stream << SNAPPY_BYTE2;
214 return m_stream.is_open();
217 bool SnappyFile::rawWrite(const void *buffer, int length)
219 if (freeCacheSize() > length) {
220 memcpy(m_cachePtr, buffer, length);
221 m_cachePtr += length;
222 } else if (freeCacheSize() == length) {
223 memcpy(m_cachePtr, buffer, length);
224 m_cachePtr += length;
227 int sizeToWrite = length;
229 while (sizeToWrite >= freeCacheSize()) {
230 int endSize = freeCacheSize();
231 int offset = length - sizeToWrite;
232 memcpy(m_cachePtr, (char*)buffer + offset, endSize);
233 sizeToWrite -= endSize;
234 m_cachePtr += endSize;
238 int offset = length - sizeToWrite;
239 memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
240 m_cachePtr += sizeToWrite;
247 bool SnappyFile::rawRead(void *buffer, int length)
249 if (m_stream.eof()) {
252 if (freeCacheSize() > length) {
253 memcpy(buffer, m_cachePtr, length);
254 m_cachePtr += length;
255 } else if (freeCacheSize() == length) {
256 memcpy(buffer, m_cachePtr, length);
257 m_cachePtr += length;
260 int sizeToRead = length;
263 int chunkSize = std::min(freeCacheSize(), sizeToRead);
264 offset = length - sizeToRead;
265 memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
266 m_cachePtr += chunkSize;
267 sizeToRead -= chunkSize;
276 int SnappyFile::rawGetc()
284 void SnappyFile::rawClose()
293 void SnappyFile::rawFlush()
298 void SnappyFile::flushCache()
300 if (m_mode == File::Write) {
301 size_t compressedLength;
303 ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
304 m_compressedCache, &compressedLength);
306 m_stream << compressedLength;
307 m_stream.write(m_compressedCache, compressedLength);
308 m_cachePtr = m_cache;
309 } else if (m_mode == File::Read) {
312 //assert(m_cachePtr == m_cache + m_cacheSize);
313 size_t compressedLength;
314 m_stream >> compressedLength;
315 m_stream.read((char*)m_compressedCache, compressedLength);
316 ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
320 createCache(m_cacheSize);
321 ::snappy::RawUncompress(m_compressedCache, compressedLength,
326 void SnappyFile::createCache(size_t size)
328 m_cache = new char[size];
329 m_cachePtr = m_cache;