-
Notifications
You must be signed in to change notification settings - Fork 4
/
Buffer.cpp
99 lines (56 loc) · 1.02 KB
/
Buffer.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
#include <string.h>
#include "Buffer.h"
#include <Arduino.h>
unsigned char * Buffer::getPayload() {
return data;
}
int Buffer::getLen() {
return len;
}
int Buffer::getFreeLen() {
return dlen - len;
}
bool Buffer::hasBytes(unsigned int bytes) {
if (len < bytes) {
return false;
}
return true;
}
unsigned char Buffer::getByte(unsigned int pos) {
if (pos >= len) {
return 0;
}
return data[pos];
}
void Buffer::addBytes(const unsigned char * bytes, int bLen) {
if ((bLen < 0) || ((len + bLen) < 0) || ((len + bLen) > dlen)) {
return;
}
memcpy(data + len, bytes, bLen);
len += bLen;
}
void Buffer::addByteCount(int bLen) {
len += bLen;
}
void Buffer::removeBytes(int bLen) {
if (bLen <= 0) {
return;
}
if (bLen >= len) {
len = 0;
return;
}
for (int i = 0; i < len - bLen; i++) {
data[i] = data[bLen + i];
}
len = len - bLen;
}
void Buffer::reset() {
len = 0;
}
Buffer::Buffer() {
len = 0;
dlen = sizeof(data);
}
Buffer::~Buffer() {
}