-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuffer.cpp
52 lines (43 loc) · 1.13 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
#include "Buffer.h"
#include <math.h>
#include <string.h>
Buffer::Buffer(const unsigned int size) {
this.data = (char *) malloc(sizeof(char) * size);
this.size = size;
this.len = 0;
}
Buffer::~Buffer() {
free(data);
}
static int Buffer::memCeil(unsigned float v) {
return (ceil(v) - v < epsilon) ? ceil(v) : ceil(++v);
}
void Buffer::dynamicCpy(char *newData) {
for (unsigned int i = 0; i < len; i++) newData[i] = data[i];
free(data);
data = newData;
}
void Buffer::handleOverflow(const unsigned int entrySize) {
const int scalar = entrySize > size ? memCeil((entrySize + len) / size) : 2;
char *newData = (char *) malloc(sizeof(char) * scalar);
dynamicCpy(newData);
}
void Buffer::empty() {
memset(data, 0, sizeof data);
}
void Buffer::addChar(const char c) {
if (len == size) handleOverflow();
data[len] = c;
}
void Buffer::addString(const char *str) {
if (len + sizeof(str) > size) handleOverflow();
for (unsigned int i = 0; i < strlen(str); i++) {
data[len] = str[i];
len++;
}
}
char *getData() {
data[len] = '\0'; //null terminate
++len;
return data;
}