#include "trace_file.hpp"
+#include "trace_snappyfile.hpp"
+
+#include <assert.h>
+#include <string.h>
+
#include <zlib.h>
+#include "os.hpp"
+
+#include <iostream>
+
using namespace Trace;
+
File::File(const std::string &filename,
File::Mode mode)
: m_filename(filename),
}
}
+
File::~File()
{
close();
close();
}
m_isOpened = rawOpen(filename, mode);
+ m_mode = mode;
+
return m_isOpened;
}
}
}
-void File::flush()
+void File::flush(FlushType type)
{
- rawFlush();
+ rawFlush(type);
}
-char File::getc()
+int File::getc()
{
if (!m_isOpened || m_mode != File::Read) {
return 0;
return rawGetc();
}
+bool File::isZLibCompressed(const std::string &filename)
+{
+ std::fstream stream(filename.c_str(),
+ std::fstream::binary | std::fstream::in);
+ if (!stream.is_open())
+ return false;
+
+ unsigned char byte1, byte2;
+ stream >> byte1;
+ stream >> byte2;
+ stream.close();
+
+ return (byte1 == 0x1f && byte2 == 0x8b);
+}
+
+
+bool File::isSnappyCompressed(const std::string &filename)
+{
+ std::fstream stream(filename.c_str(),
+ std::fstream::binary | std::fstream::in);
+ if (!stream.is_open())
+ return false;
+
+ unsigned char byte1, byte2;
+ stream >> byte1;
+ stream >> byte2;
+ stream.close();
+
+ return (byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
+}
+
+
ZLibFile::ZLibFile(const std::string &filename,
File::Mode mode)
: File(filename, mode),
bool ZLibFile::rawWrite(const void *buffer, int length)
{
- return gzwrite(m_gzFile, buffer, length);
+ return gzwrite(m_gzFile, buffer, length) != -1;
}
bool ZLibFile::rawRead(void *buffer, int length)
{
- return gzread(m_gzFile, buffer, length);
+ return gzread(m_gzFile, buffer, length) != -1;
}
-char ZLibFile::rawGetc()
+int ZLibFile::rawGetc()
{
return gzgetc(m_gzFile);
}
}
}
-void ZLibFile::rawFlush()
+void ZLibFile::rawFlush(FlushType type)
{
gzflush(m_gzFile, Z_SYNC_FLUSH);
}