1 #include "trace_file.hpp"
11 using namespace Trace;
13 File::File(const std::string &filename,
15 : m_filename(filename),
19 if (!m_filename.empty()) {
20 open(m_filename, m_mode);
30 bool File::isOpened() const
35 File::Mode File::mode() const
40 std::string File::filename() const
45 bool File::open(const std::string &filename, File::Mode mode)
50 m_isOpened = rawOpen(filename, mode);
55 bool File::write(const void *buffer, int length)
57 if (!m_isOpened || m_mode != File::Write) {
60 return rawWrite(buffer, length);
63 bool File::read(void *buffer, int length)
65 if (!m_isOpened || m_mode != File::Read) {
68 return rawRead(buffer, length);
86 if (!m_isOpened || m_mode != File::Read) {
92 bool File::isZLibCompressed(const std::string &filename)
94 std::fstream stream(filename.c_str(),
95 std::fstream::binary | std::fstream::in);
96 if (!stream.is_open())
99 unsigned char byte1, byte2;
104 return (byte1 == 0x1f && byte2 == 0x8b);
107 ZLibFile::ZLibFile(const std::string &filename,
109 : File(filename, mode),
114 ZLibFile::~ZLibFile()
118 bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode)
120 m_gzFile = gzopen(filename.c_str(),
121 (mode == File::Write) ? "wb" : "rb");
122 return m_gzFile != NULL;
125 bool ZLibFile::rawWrite(const void *buffer, int length)
127 return gzwrite(m_gzFile, buffer, length) != -1;
130 bool ZLibFile::rawRead(void *buffer, int length)
132 return gzread(m_gzFile, buffer, length) != -1;
135 int ZLibFile::rawGetc()
137 return gzgetc(m_gzFile);
140 void ZLibFile::rawClose()
148 void ZLibFile::rawFlush()
150 gzflush(m_gzFile, Z_SYNC_FLUSH);
153 SnappyFile::SnappyFile(const std::string &filename,
160 m_compressedCache = new char[SNAPPY_CHUNK_SIZE];
163 SnappyFile::~SnappyFile()
165 delete [] m_compressedCache;
168 bool SnappyFile::rawOpen(const std::string &filename, File::Mode mode)
170 std::ios_base::openmode fmode = std::fstream::binary;
171 if (mode == File::Write) {
172 fmode |= (std::fstream::out | std::fstream::trunc);
173 createCache(SNAPPY_CHUNK_SIZE);
174 } else if (mode == File::Read) {
175 fmode |= std::fstream::in;
178 m_stream.open(filename.c_str(), fmode);
180 //read in the initial buffer if we're reading
181 if (m_stream.is_open() && mode == File::Read) {
184 return m_stream.is_open();
187 bool SnappyFile::rawWrite(const void *buffer, int length)
189 if (freeCacheSize() > length) {
190 memcpy(m_cachePtr, buffer, length);
191 m_cachePtr += length;
192 } else if (freeCacheSize() == length) {
193 memcpy(m_cachePtr, buffer, length);
194 m_cachePtr += length;
197 int sizeToWrite = length;
199 while (sizeToWrite >= freeCacheSize()) {
200 int endSize = freeCacheSize();
201 int offset = length - sizeToWrite;
202 memcpy(m_cachePtr, (char*)buffer + offset, endSize);
203 sizeToWrite -= endSize;
204 m_cachePtr += endSize;
208 int offset = length - sizeToWrite;
209 memcpy(m_cachePtr, (char*)buffer + offset, sizeToWrite);
210 m_cachePtr += sizeToWrite;
217 bool SnappyFile::rawRead(void *buffer, int length)
219 if (m_stream.eof()) {
222 if (freeCacheSize() > length) {
223 memcpy(buffer, m_cachePtr, length);
224 m_cachePtr += length;
225 } else if (freeCacheSize() == length) {
226 memcpy(buffer, m_cachePtr, length);
227 m_cachePtr += length;
230 int sizeToRead = length;
233 int chunkSize = std::min(freeCacheSize(), sizeToRead);
234 offset = length - sizeToRead;
235 memcpy((char*)buffer + offset, m_cachePtr, chunkSize);
236 m_cachePtr += chunkSize;
237 sizeToRead -= chunkSize;
246 int SnappyFile::rawGetc()
254 void SnappyFile::rawClose()
263 void SnappyFile::rawFlush()
268 void SnappyFile::flushCache()
270 if (m_mode == File::Write) {
271 size_t compressedLength;
273 ::snappy::RawCompress(m_cache, SNAPPY_CHUNK_SIZE - freeCacheSize(),
274 m_compressedCache, &compressedLength);
276 m_stream << compressedLength;
277 m_stream.write(m_compressedCache, compressedLength);
278 m_cachePtr = m_cache;
279 } else if (m_mode == File::Read) {
282 //assert(m_cachePtr == m_cache + m_cacheSize);
283 size_t compressedLength;
284 m_stream >> compressedLength;
285 m_stream.read((char*)m_compressedCache, compressedLength);
286 ::snappy::GetUncompressedLength(m_compressedCache, compressedLength,
290 createCache(m_cacheSize);
291 ::snappy::RawUncompress(m_compressedCache, compressedLength,
296 void SnappyFile::createCache(size_t size)
298 m_cache = new char[size];
299 m_cachePtr = m_cache;