-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOffsetBuffer.cpp
108 lines (81 loc) · 2.98 KB
/
OffsetBuffer.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
#include <stdlib.h>
#include "OffsetBuffer.h"
OffsetBuffer::OffsetBuffer(Buffer *buffer,uint16_t offset, uint16_t length){
reinit(buffer,offset,length);
}
bool OffsetBuffer::reinit(Buffer *buffer,uint16_t offset, uint16_t length){
bool retval = true;
//configure the offset safely
if (offset > buffer->size()){
offset = buffer->size();
retval = false;
}
this->offset = offset;
//set the inner buffer
this->innerBuffer = buffer;
//set the length safely
this->bufferLength = buffer->size() - offset; //default
if (length > 0){ //if we have a custom length
if (length < this->bufferLength) //it must be shorter than the default
this->bufferLength = length;
else //otherwise, bad config
retval = false;
}
return (retval);
}
uint16_t OffsetBuffer::size(){
return this->bufferLength;
}
Buffer* OffsetBuffer::getRootBuffer(){
OffsetBuffer* b = this;
while (b->innerBuffer->getBufferType() == OffsetBuffer::BufferType)
b = static_cast<OffsetBuffer*>(b->innerBuffer);
return b->innerBuffer;
}
uint16_t OffsetBuffer::getRootBufferOffset(){
OffsetBuffer* b = this;
uint16_t offset = b->offset;
while (b->innerBuffer->getBufferType() == OffsetBuffer::BufferType){
b = static_cast<OffsetBuffer*>(b->innerBuffer);
offset += b->offset;
}
return offset;
}
#include <Arduino.h>
bool OffsetBuffer::write(uint16_t start, const void* data, uint16_t len){
//make sure we are within the bounds of the offset buffer
if (start+len > this->size()) return false;
return this->innerBuffer->write(this->offset+start,data,len);
}
bool OffsetBuffer::read(uint16_t start, void* data, uint16_t len){
//make sure we are within the bounds of the offset buffer
if (start+len > this->size()) return false;
return this->innerBuffer->read(this->offset+start,data,len);
}
uint8_t OffsetBuffer::getBufferType() { return OffsetBuffer::BufferType; }
bool OffsetBuffer::copyTo(Buffer* destination,uint16_t dest_start,
uint16_t src_start, uint16_t len){
if (len == 0)
len = size();
//make sure we are within the bounds of the offset buffer
if (len + src_start > this->size()) return false;
//if we are copying to an offset buffer, handle that special case
if (destination->getBufferType() == this->getBufferType()){
OffsetBuffer* destOffBuff = static_cast<OffsetBuffer*>(destination);
//make sure we are within the bounds of the offset buffers
if (len + dest_start > destination->size()) return false;
return this->getRootBuffer()->
copyTo(destOffBuff->getRootBuffer(),
dest_start + destOffBuff->getRootBufferOffset(),
src_start + this->getRootBufferOffset(),
len);
}
//otherwise...
return this->getRootBuffer()->copyTo(destination,dest_start,
src_start + this->getRootBufferOffset(),
len);
}//end copyTo
bool OffsetBuffer::copyFrom(Buffer* source, uint16_t dest_start,
uint16_t src_start, uint16_t len){
return source->copyTo(this,dest_start,src_start,len);
}//end copyFrom