]> git.cworth.org Git - apitrace/blob - trace_file.cpp
Inline some File methods.
[apitrace] / trace_file.cpp
1 #include "trace_file.hpp"
2
3 #include "trace_snappyfile.hpp"
4
5 #include <assert.h>
6 #include <string.h>
7
8 #include <zlib.h>
9
10 #include "os.hpp"
11
12 #include <iostream>
13
14 using namespace Trace;
15
16
17 File::File(const std::string &filename,
18            File::Mode mode)
19     : m_filename(filename),
20       m_mode(mode),
21       m_isOpened(false)
22 {
23     if (!m_filename.empty()) {
24         open(m_filename, m_mode);
25     }
26 }
27
28 File::~File()
29 {
30     close();
31 }
32
33 bool File::isZLibCompressed(const std::string &filename)
34 {
35     std::fstream stream(filename.c_str(),
36                         std::fstream::binary | std::fstream::in);
37     if (!stream.is_open())
38         return false;
39
40     unsigned char byte1, byte2;
41     stream >> byte1;
42     stream >> byte2;
43     stream.close();
44
45     return (byte1 == 0x1f && byte2 == 0x8b);
46 }
47
48
49 bool File::isSnappyCompressed(const std::string &filename)
50 {
51     std::fstream stream(filename.c_str(),
52                         std::fstream::binary | std::fstream::in);
53     if (!stream.is_open())
54         return false;
55
56     unsigned char byte1, byte2;
57     stream >> byte1;
58     stream >> byte2;
59     stream.close();
60
61     return (byte1 == SNAPPY_BYTE1 && byte2 == SNAPPY_BYTE2);
62 }
63
64
65 ZLibFile::ZLibFile(const std::string &filename,
66                    File::Mode mode)
67     : File(filename, mode),
68       m_gzFile(NULL)
69 {
70 }
71
72 ZLibFile::~ZLibFile()
73 {
74 }
75
76 bool ZLibFile::rawOpen(const std::string &filename, File::Mode mode)
77 {
78     m_gzFile = gzopen(filename.c_str(),
79                       (mode == File::Write) ? "wb" : "rb");
80     return m_gzFile != NULL;
81 }
82
83 bool ZLibFile::rawWrite(const void *buffer, int length)
84 {
85     return gzwrite(m_gzFile, buffer, length) != -1;
86 }
87
88 bool ZLibFile::rawRead(void *buffer, int length)
89 {
90     return gzread(m_gzFile, buffer, length) != -1;
91 }
92
93 int ZLibFile::rawGetc()
94 {
95     return gzgetc(m_gzFile);
96 }
97
98 void ZLibFile::rawClose()
99 {
100     if (m_gzFile) {
101         gzclose(m_gzFile);
102         m_gzFile = NULL;
103     }
104 }
105
106 void ZLibFile::rawFlush(FlushType type)
107 {
108     gzflush(m_gzFile, Z_SYNC_FLUSH);
109 }