1 | /* |
1 | /* |
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
8 | * Deliantra is free software: you can redistribute it and/or modify |
9 | * under the terms of the GNU General Public License as published by the Free |
9 | * it under the terms of the GNU General Public License as published by |
10 | * Software Foundation; either version 2 of the License, or (at your option) |
10 | * the Free Software Foundation, either version 3 of the License, or |
11 | * any later version. |
11 | * (at your option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, but |
13 | * This program is distributed in the hope that it will be useful, |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License along |
18 | * You should have received a copy of the GNU General Public License |
19 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
21 | * |
20 | * |
22 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
22 | */ |
24 | |
23 | |
25 | #ifndef DYNBUF_H__ |
24 | #ifndef DYNBUF_H__ |
26 | #define DYNBUF_H__ |
25 | #define DYNBUF_H__ |
27 | |
26 | |
… | |
… | |
47 | int size; |
46 | int size; |
48 | char data[0]; |
47 | char data[0]; |
49 | }; |
48 | }; |
50 | |
49 | |
51 | char *ptr, *end; |
50 | char *ptr, *end; |
52 | int ext; |
|
|
53 | int _size; |
51 | int _size; |
54 | |
52 | |
|
|
53 | int extend; |
55 | chunk *first, *last; |
54 | chunk *first, *last; |
56 | |
55 | |
57 | void _reserve (int size); |
56 | void reserve (int size); |
58 | void _clear (); |
57 | void init (int initial); // allocate sinitial chunk |
59 | void clear (); |
58 | void free (chunk *&chain); // free chain of chunks |
|
|
59 | char *_linearise (); |
60 | void finish (); |
60 | void finalise (); |
61 | |
61 | |
62 | public: |
62 | public: |
63 | |
63 | |
|
|
64 | // initial - the size of the initial chunk to be allocated |
|
|
65 | // extend - first incremental step when buffer size exceeded |
64 | dynbuf (int initial = 4096, int extend = 16384); |
66 | dynbuf (int initial = 4096, int extend = 16384) |
|
|
67 | : extend (extend) |
|
|
68 | { |
|
|
69 | init (initial); |
|
|
70 | } |
|
|
71 | |
65 | ~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 (); |
66 | |
81 | |
67 | int size () const { return _size + (ptr - last->data); } |
82 | int size () const { return _size + (ptr - last->data); } |
68 | bool empty () const { return !size (); } |
83 | bool empty () const { return !size (); } |
69 | |
84 | |
70 | void linearise (void *data); |
85 | void linearise (void *data); |
71 | char *linearise (); // does not 0-terminate(!) |
86 | char *linearise () // does not 0-terminate(!) |
|
|
87 | { |
|
|
88 | return first->next ? _linearise () : first->data; |
|
|
89 | } |
72 | |
90 | |
73 | int room () const { return end - ptr; } |
91 | int room () const { return end - ptr; } |
74 | |
92 | |
75 | char *force (int size) |
93 | char *force (int size) |
76 | { |
94 | { |
77 | if (expect_false (ptr + size >= end)) |
95 | if (expect_false (ptr + size >= end)) |
78 | _reserve (size); |
96 | reserve (size); |
79 | |
97 | |
80 | return ptr; |
98 | return ptr; |
81 | } |
99 | } |
82 | |
100 | |
83 | char *falloc (int size) |
101 | char *falloc (int size) |
… | |
… | |
138 | dynbuf_text (int initial = 4096, int extend = 16384) |
156 | dynbuf_text (int initial = 4096, int extend = 16384) |
139 | : dynbuf (initial, extend) |
157 | : dynbuf (initial, extend) |
140 | { } |
158 | { } |
141 | |
159 | |
142 | using dynbuf::add; |
160 | using dynbuf::add; |
143 | |
|
|
144 | void add (sint32 i); |
161 | void add (sint32 i); |
145 | void add (sint64 i); |
162 | void add (sint64 i); |
146 | |
163 | |
|
|
164 | //TODO: should optimise the case printf "(name %+d)" as it comes up extremely often |
|
|
165 | |
|
|
166 | using dynbuf::operator <<; |
|
|
167 | dynbuf_text &operator << (sint16 i) { add (sint32 (i)); return *this; } |
|
|
168 | dynbuf_text &operator << (uint16 i) { add (sint32 (i)); return *this; } |
|
|
169 | dynbuf_text &operator << (sint32 i) { add (sint32 (i)); return *this; } |
|
|
170 | dynbuf_text &operator << (sint64 i) { add (sint64 (i)); return *this; } |
|
|
171 | dynbuf_text &operator << (uint32 i) { add (sint64 (i)); return *this; } |
|
|
172 | dynbuf_text &operator << (uint64 i) { add (sint64 (i)); return *this; } |
|
|
173 | |
147 | void printf (const char *format, ...); |
174 | void printf (const char *format, ...); |
|
|
175 | void vprintf (const char *format, va_list ap); |
|
|
176 | |
|
|
177 | void add_abilities (const char *name, uint32 abilities); |
|
|
178 | void add_paths (const char *name, uint32 paths); |
|
|
179 | |
|
|
180 | // returns the string, linearised and with trailing \0 |
|
|
181 | operator const char * (); |
148 | }; |
182 | }; |
149 | |
183 | |
150 | #endif |
184 | #endif |
151 | |
185 | |