-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.cpp
More file actions
183 lines (166 loc) · 3.48 KB
/
Copy pathreader.cpp
File metadata and controls
183 lines (166 loc) · 3.48 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <ios>
#include <stdexcept>
#include <errno.h>
#include <unistd.h>
#include "reader.h"
Reader::Reader(const tstring& name, int flags) : fileName(name)
{
f = _topen(name.c_str(), flags);
if (f == -1)
{
throw std::ios_base::failure(std::string("Unable to open file \"") + to_string(name) + "\": " + strerror(errno));
}
}
Reader::~Reader()
{
close(f);
}
void Reader::readBuffer()
{
bufferOffset += dataSize;
ssize_t r = read(f, buffer, sizeof(buffer));
if (r == -1)
{
throw std::ios_base::failure(std::string("Read from \"") + to_string(fileName) + "\" failed: " + strerror(errno));
}
readPoint = buffer;
dataSize = r;
}
size_t Reader::dataLeft()
{
size_t result = dataSize - (readPoint - buffer);
if (result == 0)
{
readBuffer();
if (dataSize == 0)
{
throw std::ios_base::failure("Unexpected end of file");
}
result = dataSize;
}
return result;
}
void Reader::readBuffer(void* target, size_t size)
{
unsigned char* tgt = reinterpret_cast<unsigned char*>(target);
while (size > 0)
{
size_t chunk = std::min(size, dataLeft());
memcpy(tgt, readPoint, chunk);
readPoint += chunk;
tgt += chunk;
size -= chunk;
}
}
void Reader::skip(size_t size)
{
// Just like above but without target
while (size > 0)
{
size_t chunk = std::min(size, dataLeft());
readPoint += chunk;
size -= chunk;
}
}
void Reader::dumpTo(Dumper& dumper, size_t size)
{
while (size > 0)
{
size_t chunk = std::min(size, dataLeft());
dumper.dumpIt(readPoint, chunk);
readPoint += chunk;
size -= chunk;
}
}
uint8_t Reader::getInt8()
{
(void)dataLeft(); // Anything that is bigger than zero is fine and zero won't pass here
return *readPoint++;
}
uint16_t Reader::getInt16()
{
uint16_t result;
readBuffer(&result, sizeof(result));
if (endianness != __BYTE_ORDER__)
{
result = __builtin_bswap16(result);
}
return result;
}
uint32_t Reader::getInt32()
{
uint32_t result;
readBuffer(&result, sizeof(result));
if (endianness != __BYTE_ORDER__)
{
result = __builtin_bswap32(result);
}
return result;
}
uint64_t Reader::getInt64()
{
uint64_t result;
readBuffer(&result, sizeof(result));
if (endianness != __BYTE_ORDER__)
{
result = __builtin_bswap64(result);
}
return result;
}
uint16_t Reader::gatherInt16(const unsigned char* from) const
{
uint16_t result = *reinterpret_cast<const uint16_t*>(from);
if (endianness != __BYTE_ORDER__)
{
result = __builtin_bswap16(result);
}
return result;
}
uint32_t Reader::gatherInt32(const unsigned char* from) const
{
uint32_t result = *reinterpret_cast<const uint32_t*>(from);
if (endianness != __BYTE_ORDER__)
{
result = __builtin_bswap32(result);
}
return result;
}
uint64_t Reader::gatherInt64(const unsigned char* from) const
{
uint64_t result = *reinterpret_cast<const uint64_t*>(from);
if (endianness != __BYTE_ORDER__)
{
result = __builtin_bswap64(result);
}
return result;
}
void Reader::gatherInt128(const unsigned char* from, FB_I128& to) const
{
uint64_t t1 = *reinterpret_cast<const uint64_t*>(from);
uint64_t t2 = *reinterpret_cast<const uint64_t*>(from + sizeof(uint64_t));
if (endianness != __BYTE_ORDER__)
{
to.fb_data[0] = __builtin_bswap64(t2);
to.fb_data[1] = __builtin_bswap64(t1);
}
else
{
to.fb_data[0] = t1;
to.fb_data[1] = t2;
}
}
bool Reader::eof()
{
if (readPoint < buffer + dataSize)
{
// We surely have some data to read
return false;
}
readBuffer();
// read() returned 0, eof
return dataSize == 0;
}
size_t Reader::offset()
{
return bufferOffset + (readPoint - buffer);
}