forked from chokkan/seekgzip
-
Notifications
You must be signed in to change notification settings - Fork 2
/
export_cpp.cpp
91 lines (83 loc) · 1.86 KB
/
export_cpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <string>
#include <stdexcept>
#include "seekgzip.h"
#include "export_cpp.h"
static std::string error_string(int errorcode)
{
switch (errorcode) {
case SEEKGZIP_SUCCESS:
return "";
case SEEKGZIP_OPENERROR:
return "Failed to open a file";
case SEEKGZIP_READERROR:
return "Failed to read from a file";
case SEEKGZIP_WRITEERROR:
return "Failed to write to a file";
case SEEKGZIP_DATAERROR:
return "Data error";
case SEEKGZIP_OUTOFMEMORY:
return "Out of memory";
case SEEKGZIP_IMCOMPATIBLE:
return "Imcompatible data format";
case SEEKGZIP_ZLIBERROR:
return "ZLIB error";
default:
case SEEKGZIP_ERROR:
return "Unknown error";
}
}
reader::reader(const char *filename)
{
int err = 0;
seekgzip_t* sgz = seekgzip_open(filename, 0);
m_obj = sgz;
if ( (err = seekgzip_error(sgz)) != SEEKGZIP_SUCCESS){
throw std::invalid_argument(error_string(err));
}
}
reader::~reader()
{
this->close();
}
void reader::close()
{
if (m_obj != NULL) {
seekgzip_close(reinterpret_cast<seekgzip_t*>(m_obj));
m_obj = NULL;
}
}
void reader::seek(long long offset)
{
if (m_obj != NULL) {
seekgzip_seek(
reinterpret_cast<seekgzip_t*>(m_obj),
offset
);
}
}
long long reader::tell()
{
if (m_obj != NULL) {
return seekgzip_tell(
reinterpret_cast<seekgzip_t*>(m_obj)
);
} else {
return -1;
}
}
std::string reader::read(int size)
{
std::string ret;
if (m_obj != NULL) {
char *buffer = new char[size+1];
int n = seekgzip_read(
reinterpret_cast<seekgzip_t*>(m_obj),
buffer,
size
);
buffer[n] = 0;
ret = buffer;
delete[] buffer;
}
return ret;
}