-
Notifications
You must be signed in to change notification settings - Fork 5
/
StreamReader.cpp
155 lines (138 loc) · 2.91 KB
/
StreamReader.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
#include "StreamReader.hpp"
#include "InputStream.hpp"
#include "Exception.hpp"
#if defined(___INANITY_PLATFORM_LINUX)
#include <alloca.h>
#elif defined(___INANITY_PLATFORM_FREEBSD)
#include <stdlib.h>
#endif
BEGIN_INANITY
StreamReader::StreamReader(ptr<InputStream> stream) : stream(stream), read(0)
{
}
bigsize_t StreamReader::GetReadSize() const
{
return read;
}
size_t StreamReader::Read(void* data, size_t size)
{
if(stream->Read(data, size) != size)
THROW("Can't read data");
read += size;
return size;
}
String StreamReader::ReadString(size_t maximumLength)
{
//считать длину строки
size_t length = ReadShortly();
if(length > maximumLength)
THROW("String length is too big");
//считать строку
std::string r(length, ' ');
if(length)
Read(&*r.begin(), length);
return r;
}
size_t StreamReader::ReadShortly()
{
bigsize_t a = ReadShortlyBig();
size_t b = (size_t)a;
if(a != b)
THROW("Can't read shortly because number is too big");
return b;
}
/*
Формат сокращенных чисел, см. примечание к реализации StreamWriter::WriteShortly().
*/
bigsize_t StreamReader::ReadShortlyBig()
{
// read first byte
uint8_t first = Read<uint8_t>();
// determine additional length
int length;
if(!(first & 0x80))
//дополнительной части нет, можно сразу вернуть ответ
return first;
else if(!(first & 0x40))
{
length = 1;
first &= ~0x80;
}
else if(!(first & 0x20))
{
length = 2;
first &= ~0xC0;
}
else if(!(first & 0x10))
{
length = 3;
first &= ~0xE0;
}
else if(!(first & 0x08))
{
length = 4;
first &= ~0xF0;
}
else if(!(first & 0x04))
{
length = 5;
first &= ~0xF8;
}
else if(!(first & 0x02))
{
length = 6;
first &= ~0xFC;
}
else if(!(first & 0x01))
{
length = 7;
first &= ~0xFE;
}
else
{
length = 8;
first &= ~0xFF;
}
// read additional bytes
uint8_t bytes[8];
Read(bytes, length);
// calculate result
bigsize_t result = ((bigsize_t)first) << (length * 8);
for(int i = 0; i < length; ++i)
result |= ((bigsize_t)bytes[i]) << ((length - 1 - i) * 8);
return result;
}
void StreamReader::ReadGap(size_t alignment)
{
#ifdef _DEBUG
//проверить, что выравнивание - степень двойки
if(alignment & (alignment - 1))
THROW("Alignment must be power of two");
#endif
//вычислить количество байт
alignment = alignment - ((read - 1) & (alignment - 1)) - 1;
//считать их
if(alignment)
{
char* data = (char*)alloca(alignment);
Read(data, alignment);
}
}
void StreamReader::ReadEnd()
{
char c;
if(stream->Read(&c, 1) != 0)
THROW("Expected end of stream");
}
bigsize_t StreamReader::Skip(bigsize_t size)
{
bigsize_t skipped = stream->Skip(size);
if(skipped != size)
THROW("Not enough data to skip");
return skipped;
}
bool StreamReader::IsAtEnd() const
{
return stream->IsAtEnd();
}
END_INANITY