1 |
root |
1.1 |
#include "global.h" |
2 |
|
|
|
3 |
|
|
#include <cstdio> |
4 |
|
|
|
5 |
|
|
dynbuf::dynbuf (int initial, int extend) |
6 |
|
|
{ |
7 |
|
|
_size = 0; |
8 |
root |
1.5 |
ext = extend; |
9 |
|
|
first = last = (chunk *) new char[sizeof (chunk) + initial]; |
10 |
|
|
|
11 |
root |
1.1 |
first->next = 0; |
12 |
root |
1.5 |
room = initial; |
13 |
|
|
ptr = first->data; |
14 |
root |
1.1 |
} |
15 |
|
|
|
16 |
|
|
dynbuf::~dynbuf () |
17 |
|
|
{ |
18 |
|
|
clear (); |
19 |
|
|
} |
20 |
|
|
|
21 |
root |
1.5 |
void |
22 |
|
|
dynbuf::clear () |
23 |
root |
1.1 |
{ |
24 |
|
|
while (first) |
25 |
|
|
{ |
26 |
|
|
chunk *next = first->next; |
27 |
root |
1.5 |
|
28 |
|
|
delete[](char *) first; |
29 |
root |
1.1 |
first = next; |
30 |
|
|
} |
31 |
root |
1.5 |
} |
32 |
root |
1.1 |
|
33 |
root |
1.5 |
void |
34 |
|
|
dynbuf::finish () |
35 |
root |
1.1 |
{ |
36 |
|
|
// finalise current chunk |
37 |
|
|
_size += last->size = ptr - last->data; |
38 |
|
|
} |
39 |
|
|
|
40 |
root |
1.5 |
void |
41 |
|
|
dynbuf::_reserve (int size) |
42 |
root |
1.1 |
{ |
43 |
|
|
finish (); |
44 |
|
|
|
45 |
|
|
do |
46 |
|
|
{ |
47 |
|
|
ext += ext >> 1; |
48 |
|
|
ext = (ext + 15) & ~15; |
49 |
|
|
} |
50 |
|
|
while (ext < size); |
51 |
|
|
|
52 |
root |
1.5 |
chunk *add = (chunk *) new char[sizeof (chunk) + ext]; |
53 |
|
|
|
54 |
root |
1.1 |
add->next = 0; |
55 |
|
|
|
56 |
|
|
last->next = add; |
57 |
|
|
last = add; |
58 |
|
|
|
59 |
|
|
room = ext; |
60 |
root |
1.5 |
ptr = last->data; |
61 |
root |
1.1 |
} |
62 |
|
|
|
63 |
root |
1.5 |
void |
64 |
|
|
dynbuf::linearise (void *data) |
65 |
root |
1.1 |
{ |
66 |
root |
1.5 |
char *p = (char *) data; |
67 |
root |
1.1 |
|
68 |
|
|
last->size = ptr - last->data; |
69 |
|
|
|
70 |
root |
1.5 |
for (chunk * c = first; c; c = c->next) |
71 |
root |
1.1 |
{ |
72 |
|
|
memcpy (p, c->data, c->size); |
73 |
|
|
p += c->size; |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
root |
1.5 |
char * |
78 |
|
|
dynbuf::linearise () |
79 |
root |
1.1 |
{ |
80 |
|
|
if (first->next) |
81 |
|
|
{ |
82 |
|
|
finish (); |
83 |
|
|
|
84 |
root |
1.5 |
chunk *add = (chunk *) new char[sizeof (chunk) + _size]; |
85 |
root |
1.1 |
|
86 |
|
|
add->next = 0; |
87 |
root |
1.5 |
linearise ((void *) add->data); |
88 |
root |
1.1 |
clear (); |
89 |
|
|
|
90 |
|
|
first = last = add; |
91 |
root |
1.5 |
ptr = last->data + _size; |
92 |
root |
1.1 |
_size = 0; |
93 |
root |
1.5 |
room = 0; |
94 |
root |
1.1 |
} |
95 |
|
|
|
96 |
|
|
return first->data; |
97 |
|
|
} |
98 |
|
|
|
99 |
root |
1.5 |
void |
100 |
|
|
dynbuf::add (sint32 i) |
101 |
root |
1.1 |
{ |
102 |
root |
1.5 |
char buf[max_sint32_size]; |
103 |
root |
1.1 |
char *p = buf + sizeof (buf); |
104 |
|
|
char neg; |
105 |
|
|
|
106 |
root |
1.4 |
uint32 val; |
107 |
|
|
|
108 |
root |
1.1 |
if (i < 0) |
109 |
|
|
{ |
110 |
|
|
neg = '-'; |
111 |
root |
1.4 |
val = -i; |
112 |
root |
1.1 |
} |
113 |
|
|
else |
114 |
root |
1.4 |
{ |
115 |
|
|
neg = 0; |
116 |
|
|
val = i; |
117 |
|
|
} |
118 |
root |
1.1 |
|
119 |
|
|
do |
120 |
|
|
{ |
121 |
|
|
uint32 div = val / 10; |
122 |
|
|
*--p = '0' + char (val - div * 10); |
123 |
root |
1.5 |
|
124 |
root |
1.1 |
val = div; |
125 |
|
|
} |
126 |
|
|
while (val); |
127 |
|
|
|
128 |
|
|
if (neg) |
129 |
|
|
*--p = neg; |
130 |
|
|
|
131 |
root |
1.5 |
add ((void *) p, buf + sizeof (buf) - p); |
132 |
root |
1.1 |
} |
133 |
|
|
|
134 |
root |
1.5 |
void |
135 |
|
|
dynbuf::add (sint64 i) |
136 |
root |
1.1 |
{ |
137 |
|
|
if (i > -10000000 && i < 10000000) |
138 |
|
|
{ |
139 |
|
|
add (sint32 (i)); |
140 |
|
|
return; |
141 |
|
|
} |
142 |
|
|
|
143 |
root |
1.5 |
char buf[max_sint64_size]; |
144 |
root |
1.4 |
char *p = buf + sizeof (buf); |
145 |
|
|
char neg; |
146 |
|
|
|
147 |
root |
1.2 |
uint64 val; |
148 |
|
|
|
149 |
root |
1.1 |
if (i < 0) |
150 |
|
|
{ |
151 |
root |
1.4 |
neg = '-'; |
152 |
root |
1.2 |
val = -i; |
153 |
root |
1.1 |
} |
154 |
root |
1.2 |
else |
155 |
root |
1.1 |
{ |
156 |
root |
1.4 |
neg = 0; |
157 |
|
|
val = i; |
158 |
root |
1.1 |
} |
159 |
|
|
|
160 |
root |
1.4 |
do |
161 |
root |
1.1 |
{ |
162 |
root |
1.4 |
uint64 div = val / 10; |
163 |
|
|
*--p = '0' + char (val - div * 10); |
164 |
root |
1.5 |
|
165 |
root |
1.4 |
val = div; |
166 |
root |
1.1 |
} |
167 |
root |
1.4 |
while (val); |
168 |
root |
1.1 |
|
169 |
root |
1.4 |
if (neg) |
170 |
|
|
*--p = neg; |
171 |
|
|
|
172 |
root |
1.5 |
add ((void *) p, buf + sizeof (buf) - p); |
173 |
root |
1.1 |
} |