-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBuffer.cc
More file actions
115 lines (88 loc) · 2.25 KB
/
Buffer.cc
File metadata and controls
115 lines (88 loc) · 2.25 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
// Buffer object
#include "Buffer.h"
#include "misc.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdlib.h>
int Buffer::find_mem_size (int min_size) {
int i;
for (i = 4096 ; min_size > i; i *= 2)
;
return i ;
}
Buffer::Buffer(int min_size) {
size = find_mem_size(min_size);
data = new char[size];
overflowed = false;
len = 0;
data[0] = NUL;
}
bool Buffer::strcat (const char *text) {
return strncat(text, strlen(text));
}
void Buffer::need(int n) {
if (n > size) { /* expand? */
int new_size = find_mem_size (n);
assert (new_size >= 0);
char* new_data = new char[new_size];
memcpy (new_data, data, len);
delete[] data;
data = new_data;
size = new_size;
}
}
void Buffer::use (int count) {
assert(count+len <= size);
len += count;
}
char *Buffer::get (int count) {
need(count+len+1);
return data+len;
}
void Buffer::unshift(const char *s, int shifted_len) {
need(len+shifted_len+1);
memmove(data,data+len, shifted_len);
memcpy(data, s, shifted_len);
}
bool Buffer::strncat(const char *text, int text_len) {
if (overflowed) /* Do not attempt to add anymore if buffer is already overflowed */
return false;
if (!text) /* Adding NULL string ? */
return true;
if (text_len == 0) /* Adding empty string ? */
return true;
need(text_len+len+1);
/* Will the combined len of the added text and the current text exceed our buffer? */
memcpy (data + len, text, text_len); /* Start copying */
len += text_len; /* Adjust length */
data[len] = NUL; /* Null-terminate at new end */
return true;
}
void Buffer::clear() {
overflowed = false;
len = 0;
data[0] = NUL;
}
int Buffer::printf(const char *fmt, ...) {
char buf[16384];
va_list va;
int res;
va_start (va, fmt);
res = vsnprintf (buf, sizeof(buf), fmt, va);
va_end (va);
if (res >= (int)sizeof(buf)-2)
error("Buffer::printf: res > (int)sizeof(buf)-2\n");
else
strcat(buf);
return res;
}
void Buffer::shift (int count) {
assert (count <= len);
memmove (data, data+count, len-count);
data[len-count]=0;
len -= count;
}