-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbuffer.cpp
More file actions
44 lines (37 loc) · 723 Bytes
/
buffer.cpp
File metadata and controls
44 lines (37 loc) · 723 Bytes
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
#include "buffer.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
buffer::buffer() {
_ptr = (char *)calloc(1, BUFSIZE);
_size = BUFSIZE;
_len = 0;
}
buffer::~buffer() {
free(_ptr);
_ptr = NULL;
}
unsigned int buffer::length() {
return _len;
}
char *buffer::ptr() {
return _ptr;
}
void buffer::push_back(char *buf, int len) {
if (len + _len > 2 * _size / 3) {
if (!expand()) {
// log error.
printf("expand error\n");
return ;
}
}
memcpy(_ptr + _len, buf, len);
}
bool buffer::expand() {
char *new_ptr = (char *)calloc(1, 2 * _size);
_size *= 2;
memcpy(new_ptr, _ptr, _len);
free(_ptr);
_ptr = new_ptr;
return true;
}