-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.cpp
More file actions
137 lines (124 loc) · 2.44 KB
/
Copy pathreader.cpp
File metadata and controls
137 lines (124 loc) · 2.44 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
#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;
}
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);
}