-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBuffer.cpp
More file actions
160 lines (105 loc) · 3.75 KB
/
Buffer.cpp
File metadata and controls
160 lines (105 loc) · 3.75 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
#include <cstring>
#include <stdexcept>
using namespace std;
#include "Buffer.hpp"
Buffer::Buffer(): size(0), buffer(nullptr), readPos(0), writePos(0){}
Buffer::Buffer(uint64_t size): size(size), buffer(new uint8_t[size]), readPos(0), writePos(0){}
Buffer::Buffer(const Buffer &buffer): size(buffer.getSize()), buffer(new uint8_t[size]), readPos(buffer.readPos), writePos(buffer.writePos){
memcpy(this->get(), buffer.get(), size);
}
Buffer::Buffer(Buffer &&buffer): size(buffer.getSize()), buffer(move(buffer.buffer)), readPos(buffer.readPos), writePos(buffer.writePos){}
const Buffer &Buffer::operator=(Buffer &&buffer){
this->buffer = move(buffer.buffer);
this->size = buffer.size;
this->readPos = buffer.readPos;
this->writePos = buffer.writePos;
return *this;
}
bool Buffer::operator==(const Buffer &buffer) const{
if(this->size != buffer.size)
return false;
for(uint64_t i = 0; i < this->size; ++i)
if(this->get(i) != buffer.get(i))
return false;
return true;
}
void Buffer::fill(uint8_t value){
for(uint64_t pos = 0; pos < size; ++pos)
get(pos) = value;
}
void Buffer::fill(const function<uint8_t()> &getValue){
for(uint64_t pos = 0; pos < size; ++pos)
get(pos) = getValue();
}
void Buffer::append(const Buffer &buffer){
uint64_t sumSize = this->getSize() + buffer.getSize();//для безопасости надо бы поставить ограничение макс. размера в 2^63
unique_ptr<uint8_t> newBlock(new uint8_t[sumSize]);
this->lowLevelRead(newBlock.get(), this->getSize());
buffer.lowLevelRead(newBlock.get() + this->getSize(), buffer.getSize());
this->buffer = move(newBlock);
this->size = sumSize;
if(this->getWriteSpace() > 0)
this->dropPointers();//если в конце 1-го блока было не инициализированное место, то нарушается логика, поэтому просто сбрасываем поинтеры
else
this->writePos += buffer.writePos;//если инициализированное пространство не разделено, то можно продолжать его читать.
}
void Buffer::lowLevelWrite(const void *data, uint64_t length, uint64_t from){
if(getSize() - from + 1 < length)
throw logic_error("Buffer out of range");
memcpy(buffer.get() + from, data, length);
}
void Buffer::lowLevelRead(void *dst, uint64_t length, uint64_t from) const{
if(getSize() - from + 1 < length)
throw logic_error("Buffer out of range");
memcpy(dst, buffer.get() + from, length);
}
void Buffer::dropPointers(){
readPos = 0;
writePos = 0;
}
void Buffer::write(const void *data, uint64_t length){
lowLevelWrite(data, length, writePos);
writePos += length;
}
void Buffer::write(istream &i, uint64_t length){
if(getWriteSpace() < length)
throw logic_error("Buffer overflow");
void *ptr = buffer.get() + writePos;
i.read(reinterpret_cast<char *>(ptr), length);
writePos += length;
}
uint64_t Buffer::getWriteSpace() const{
return getSize() - writePos;
}
uint64_t Buffer::getReadSpace() const{
return getSize() - readPos;
}
void Buffer::read(void *dst, uint64_t length){
lowLevelRead(dst, length, readPos);
readPos += length;
}
void Buffer::read(ostream &o, uint64_t length){
if(getReadSpace() < length)
throw logic_error("Buffer reading overflow");
void *ptr = buffer.get() + readPos;
o.write(reinterpret_cast<char *>(ptr), length);
readPos += length;
}
void *Buffer::get(){
return buffer.get();
}
const void *Buffer::get() const{
return buffer.get();
}
uint64_t Buffer::getSize() const{
return size;
}
string Buffer::toString() const{
string res;
for(size_t i = 0; i < size; ++i){
for(int bitRank = 7; bitRank >= 0; --bitRank)
res += get<uint8_t>(i) & 1 << bitRank ? "1" : "0";
res += " ";
}
return res;
}