forked from popacai/seekgzip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_cpp.cpp
168 lines (153 loc) · 3.76 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <string>
#include <stdio.h>
#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_buffer = new char[BUFFER_SIZE + 1];
offset = BUFFER_SIZE + 1;
}
reader::~reader()
{
delete [] reader_buffer;
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
);
}
}
void reader::seek(long long offset, int whence)
{
long long file_length;
file_length = seekgzip_unpacked_length(reinterpret_cast<seekgzip_t*>(m_obj));
//seek to the end
if (whence == 2) {
offset = file_length - offset;
} else if (whence == 1) {
offset = tell() + offset;
} else if (whence == 0) {
// pass
} else {
throw std::invalid_argument(error_string(SEEKGZIP_ERROR));
}
if (offset < 0 || offset > file_length) {
throw std::invalid_argument(error_string(SEEKGZIP_READERROR));
}
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)
{
static char* buffer = new char[1024];
static size_t buffer_size = 1024;
std::string ret;
if (m_obj != NULL) {
if(buffer_size < size) {
delete[] buffer;
buffer_size = size;
buffer = new char[buffer_size];
}
int n = seekgzip_read(
reinterpret_cast<seekgzip_t*>(m_obj),
buffer,
size
);
if(n < size) {
size = n;
}
ret.assign(buffer, size);
}
return ret;
}
std::string reader::readline()
{
off_t old_offset;
int found;
std::string ret = "";
while (1) {
old_offset = offset;
found = 0;
for (; offset < BUFFER_SIZE; offset++) {
if (reader_buffer[offset] == '\n') {
found = 1;
break;
}
}
reader_buffer[offset] = 0;
ret += (char*) (reader_buffer + old_offset);
if (found) {
offset++;
return ret;
} else {
if (m_obj != NULL) {
int n = seekgzip_read(
reinterpret_cast<seekgzip_t*>(m_obj),
reader_buffer,
BUFFER_SIZE
);
if (n == 0) {
return ret;
}
reader_buffer[n] = 0;
offset = 0; //reset offset
}
}
}
}