#include <string>
#include <fstream>
+#include <stdint.h>
namespace Trace {
Read,
Write
};
+ struct Offset {
+ Offset(uint64_t _chunk = 0, uint32_t _offsetInChunk = 0)
+ : chunk(_chunk),
+ offsetInChunk(_offsetInChunk)
+ {}
+ uint64_t chunk;
+ uint32_t offsetInChunk;
+ };
+
public:
static bool isZLibCompressed(const std::string &filename);
static bool isSnappyCompressed(const std::string &filename);
bool isOpened() const;
File::Mode mode() const;
- std::string filename() const;
-
bool open(const std::string &filename, File::Mode mode);
bool write(const void *buffer, size_t length);
bool read(void *buffer, size_t length);
void close();
void flush(void);
int getc();
+ bool skip(size_t length);
+ int percentRead();
+ virtual bool supportsOffsets() const = 0;
+ virtual File::Offset currentOffset() = 0;
+ virtual void setCurrentOffset(const File::Offset &offset);
protected:
virtual bool rawOpen(const std::string &filename, File::Mode mode) = 0;
virtual bool rawWrite(const void *buffer, size_t length) = 0;
virtual int rawGetc() = 0;
virtual void rawClose() = 0;
virtual void rawFlush() = 0;
+ virtual bool rawSkip(size_t length) = 0;
+ virtual int rawPercentRead() = 0;
protected:
- std::string m_filename;
File::Mode m_mode;
bool m_isOpened;
};
return m_mode;
}
-inline std::string File::filename() const
-{
- return m_filename;
-}
-
inline bool File::open(const std::string &filename, File::Mode mode)
{
if (m_isOpened) {
return rawRead(buffer, length);
}
+inline int File::percentRead()
+{
+ if (!m_isOpened || m_mode != File::Read) {
+ return 0;
+ }
+ return rawPercentRead();
+}
+
inline void File::close()
{
if (m_isOpened) {
inline void File::flush(void)
{
- rawFlush();
+ if (m_mode == File::Write) {
+ rawFlush();
+ }
}
inline int File::getc()
return rawGetc();
}
+inline bool File::skip(size_t length)
+{
+ if (!m_isOpened || m_mode != File::Read) {
+ return false;
+ }
+ return rawSkip(length);
+}
+
class ZLibFile : public File {
public:
ZLibFile(const std::string &filename = std::string(),
File::Mode mode = File::Read);
virtual ~ZLibFile();
+
+ virtual bool supportsOffsets() const;
+ virtual File::Offset currentOffset();
protected:
virtual bool rawOpen(const std::string &filename, File::Mode mode);
virtual bool rawWrite(const void *buffer, size_t length);
virtual int rawGetc();
virtual void rawClose();
virtual void rawFlush();
+ virtual bool rawSkip(size_t length);
+ virtual int rawPercentRead();
private:
void *m_gzFile;
+ double m_endOffset;
};
+inline bool
+operator<(const File::Offset &one, const File::Offset &two)
+{
+ return one.chunk < two.chunk ||
+ (one.chunk == two.chunk && one.offsetInChunk < two.offsetInChunk);
+}
+
+inline bool
+operator==(const File::Offset &one, const File::Offset &two)
+{
+ return one.chunk == two.chunk &&
+ one.offsetInChunk == two.offsetInChunk;
+}
+
+inline bool
+operator>=(const File::Offset &one, const File::Offset &two)
+{
+ return one.chunk > two.chunk ||
+ (one.chunk == two.chunk && one.offsetInChunk >= two.offsetInChunk);
+}
+
+inline bool
+operator>(const File::Offset &one, const File::Offset &two)
+{
+ return two < one;
+}
+
+inline bool
+operator<=(const File::Offset &one, const File::Offset &two)
+{
+ return two >= one;
+}
+
+
}
#endif