… | |
… | |
46 | int size; |
46 | int size; |
47 | char data[0]; |
47 | char data[0]; |
48 | }; |
48 | }; |
49 | |
49 | |
50 | char *ptr, *end; |
50 | char *ptr, *end; |
51 | int ext; |
|
|
52 | int _size; |
51 | int _size; |
53 | |
52 | |
|
|
53 | int extend; |
54 | chunk *first, *last; |
54 | chunk *first, *last; |
55 | |
55 | |
56 | void _reserve (int size); |
56 | void reserve (int size); |
57 | void _clear (); |
57 | void init (int initial); // allocate sinitial chunk |
58 | void clear (); |
58 | void free (chunk *&chain); // free chain of chunks |
|
|
59 | char *_linearise (); |
59 | void finish (); |
60 | void finalise (); |
60 | |
61 | |
61 | public: |
62 | public: |
62 | |
63 | |
|
|
64 | // initial - the size of the initial chunk to be allocated |
|
|
65 | // extend - first incremental step when buffer size exceeded |
63 | dynbuf (int initial = 4096, int extend = 16384); |
66 | dynbuf (int initial = 4096, int extend = 16384) |
|
|
67 | : extend (extend) |
|
|
68 | { |
|
|
69 | init (initial); |
|
|
70 | } |
|
|
71 | |
64 | ~dynbuf (); |
72 | ~dynbuf () |
|
|
73 | { |
|
|
74 | free (first); |
|
|
75 | } |
|
|
76 | |
|
|
77 | // resets the dynbuf, but does not free the first chunk |
|
|
78 | // which is either of size "initial" or the size of the last |
|
|
79 | // linearise |
|
|
80 | void clear (); |
65 | |
81 | |
66 | int size () const { return _size + (ptr - last->data); } |
82 | int size () const { return _size + (ptr - last->data); } |
67 | bool empty () const { return !size (); } |
83 | bool empty () const { return !size (); } |
68 | |
84 | |
69 | void linearise (void *data); |
85 | void linearise (void *data); |
70 | char *linearise (); // does not 0-terminate(!) |
86 | char *linearise () // does not 0-terminate(!) |
|
|
87 | { |
|
|
88 | return first->next ? _linearise () : first->data; |
|
|
89 | } |
71 | |
90 | |
72 | int room () const { return end - ptr; } |
91 | int room () const { return end - ptr; } |
73 | |
92 | |
74 | char *force (int size) |
93 | char *force (int size) |
75 | { |
94 | { |
76 | if (expect_false (ptr + size >= end)) |
95 | if (expect_false (ptr + size >= end)) |
77 | _reserve (size); |
96 | reserve (size); |
78 | |
97 | |
79 | return ptr; |
98 | return ptr; |
80 | } |
99 | } |
81 | |
100 | |
82 | char *falloc (int size) |
101 | char *falloc (int size) |
… | |
… | |
137 | dynbuf_text (int initial = 4096, int extend = 16384) |
156 | dynbuf_text (int initial = 4096, int extend = 16384) |
138 | : dynbuf (initial, extend) |
157 | : dynbuf (initial, extend) |
139 | { } |
158 | { } |
140 | |
159 | |
141 | using dynbuf::add; |
160 | using dynbuf::add; |
142 | |
|
|
143 | void add (sint32 i); |
161 | void add (sint32 i); |
144 | void add (sint64 i); |
162 | void add (sint64 i); |
145 | |
163 | |
|
|
164 | //TODO: should optimise the case printf "(name %+d)" as it comes up extreemly often |
|
|
165 | |
|
|
166 | using dynbuf::operator <<; |
|
|
167 | dynbuf &operator << (sint16 i) { add (sint32 (i)); return *this; } |
|
|
168 | dynbuf &operator << (uint16 i) { add (sint32 (i)); return *this; } |
|
|
169 | dynbuf &operator << (sint32 i) { add (sint32 (i)); return *this; } |
|
|
170 | dynbuf &operator << (sint64 i) { add (sint64 (i)); return *this; } |
|
|
171 | dynbuf &operator << (uint32 i) { add (sint64 (i)); return *this; } |
|
|
172 | dynbuf &operator << (uint64 i) { add (sint64 (i)); return *this; } |
|
|
173 | |
146 | void printf (const char *format, ...); |
174 | void printf (const char *format, ...); |
|
|
175 | |
|
|
176 | void add_abilities (const char *name, uint32 abilities); |
|
|
177 | void add_paths (const char *name, uint32 paths); |
|
|
178 | |
|
|
179 | // returns the string, linearised and with trailing \0 |
|
|
180 | operator const char * (); |
147 | }; |
181 | }; |
148 | |
182 | |
149 | #endif |
183 | #endif |
150 | |
184 | |