1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
5 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
6 |
* Copyright (©) 2002 Mark Wedel & Crossfire Development Team |
7 |
* Copyright (©) 1992 Frank Tore Johansen |
8 |
* |
9 |
* Deliantra is free software: you can redistribute it and/or modify it under |
10 |
* the terms of the Affero GNU General Public License as published by the |
11 |
* Free Software Foundation, either version 3 of the License, or (at your |
12 |
* option) any later version. |
13 |
* |
14 |
* This program is distributed in the hope that it will be useful, |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* GNU General Public License for more details. |
18 |
* |
19 |
* You should have received a copy of the Affero GNU General Public License |
20 |
* and the GNU General Public License along with this program. If not, see |
21 |
* <http://www.gnu.org/licenses/>. |
22 |
* |
23 |
* The authors can be reached via e-mail to <support@deliantra.net> |
24 |
*/ |
25 |
|
26 |
#ifndef DYNBUF_H__ |
27 |
#define DYNBUF_H__ |
28 |
|
29 |
#include <cstring> |
30 |
#include <cassert> |
31 |
|
32 |
#include "ecb.h" |
33 |
#include "util.h" |
34 |
#include "shstr.h" |
35 |
|
36 |
// this is a "buffer" that can grow fast |
37 |
// and is still somewhat space-efficient. |
38 |
// unlike obstacks or other data structures, |
39 |
// it never moves data around. basically, |
40 |
// this is a fast strstream without the overhead. |
41 |
|
42 |
struct dynbuf |
43 |
{ |
44 |
protected: |
45 |
struct chunk |
46 |
{ |
47 |
chunk *next; |
48 |
int alloc; |
49 |
int size; |
50 |
char data[0]; |
51 |
}; |
52 |
|
53 |
char *ptr, *end; |
54 |
int _size; |
55 |
|
56 |
int extend, cextend; |
57 |
chunk *first, *last; |
58 |
|
59 |
void reserve (int size); |
60 |
void init (int initial); // allocate sinitial chunk |
61 |
void free (chunk *&chain); // free chain of chunks |
62 |
char *_linearise (int extra = 0); |
63 |
void finalise (); |
64 |
|
65 |
public: |
66 |
|
67 |
// initial - the size of the initial chunk to be allocated |
68 |
// extend - first incremental step when buffer size exceeded |
69 |
dynbuf (int initial = 4096, int extend = 16384) |
70 |
: extend (extend) |
71 |
{ |
72 |
init (initial); |
73 |
} |
74 |
|
75 |
~dynbuf () |
76 |
{ |
77 |
free (first); |
78 |
} |
79 |
|
80 |
// resets the dynbuf, but does not free the first chunk |
81 |
// which is either of size "initial" or the size of the last |
82 |
// linearise |
83 |
void clear (); |
84 |
|
85 |
int size () const { return _size + (ptr - last->data); } |
86 |
bool empty () const { return !size (); } |
87 |
|
88 |
void linearise (void *data); |
89 |
char *linearise () // does not 0-terminate(!) |
90 |
{ |
91 |
return first->next ? _linearise () : first->data; |
92 |
} |
93 |
|
94 |
int room () const { return end - ptr; } |
95 |
|
96 |
// make sure we have "size" extra room |
97 |
char *force (int size) |
98 |
{ |
99 |
if (ecb_expect_false (ptr + size > end)) |
100 |
reserve (size); |
101 |
|
102 |
ecb_assume (ptr + size <= end); |
103 |
|
104 |
return ptr; |
105 |
} |
106 |
|
107 |
// used for force + alloc combo |
108 |
void alloc (char *p) |
109 |
{ |
110 |
ptr = p; |
111 |
} |
112 |
|
113 |
// allocate size bytes and return pointer to them (caller must force()) |
114 |
char *falloc (int size) |
115 |
{ |
116 |
char *res = ptr; |
117 |
ptr += size; |
118 |
return res; |
119 |
} |
120 |
|
121 |
// allocate size bytes and return pointer to them |
122 |
char *alloc (int size) |
123 |
{ |
124 |
force (size); |
125 |
return falloc (size); |
126 |
} |
127 |
|
128 |
void fadd (char c) { *ptr++ = c; } |
129 |
void fadd (unsigned char c) { fadd (char (c)); } |
130 |
void fadd (const void *p, int len) |
131 |
{ |
132 |
memcpy (falloc (len), p, len); |
133 |
} |
134 |
|
135 |
void add (const void *p, int len) |
136 |
{ |
137 |
force (len); |
138 |
fadd (p, len); |
139 |
} |
140 |
|
141 |
void add (char c) |
142 |
{ |
143 |
alloc (1)[0] = c; |
144 |
} |
145 |
|
146 |
void add (const char *s) |
147 |
{ |
148 |
add (s, strlen (s)); |
149 |
} |
150 |
|
151 |
void add (shstr_tmp s) |
152 |
{ |
153 |
add (s.s, s.length ()); |
154 |
} |
155 |
|
156 |
// rather inefficient |
157 |
void splice (int offset, int olen, const char *s, int slen); |
158 |
void splice (int offset, int olen) { splice (offset, olen, "", 0); } |
159 |
|
160 |
//TODO |
161 |
//void add_destructive (dynbuf &buf); |
162 |
|
163 |
dynbuf &operator << (char c) { add (c); return *this; } |
164 |
dynbuf &operator << (unsigned char c) { return *this << char (c); } |
165 |
dynbuf &operator << (const char *s) { add (s); return *this; } |
166 |
dynbuf &operator << (shstr_tmp s) { add (s); return *this; } |
167 |
dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } |
168 |
|
169 |
operator std::string (); |
170 |
}; |
171 |
|
172 |
struct dynbuf_text : dynbuf |
173 |
{ |
174 |
dynbuf_text (int initial = 4096, int extend = 16384) |
175 |
: dynbuf (initial, extend) |
176 |
{ } |
177 |
|
178 |
using dynbuf::add; |
179 |
void add (sint32 i); |
180 |
void add (sint64 i); |
181 |
|
182 |
//TODO: should optimise the case printf "(name %+d)" as it comes up extremely often |
183 |
|
184 |
//using dynbuf::operator <<; // doesn't work, sometimes C++ just suxx |
185 |
// instead we use an ugly template function |
186 |
template<typename T> |
187 |
dynbuf_text &operator << (T c) { *(dynbuf *)this << c; return *this; } |
188 |
|
189 |
dynbuf_text &operator << (sint16 i) { add (sint32 (i)); return *this; } |
190 |
dynbuf_text &operator << (uint16 i) { add (sint32 (i)); return *this; } |
191 |
dynbuf_text &operator << (sint32 i) { add (sint32 (i)); return *this; } |
192 |
dynbuf_text &operator << (sint64 i) { add (sint64 (i)); return *this; } |
193 |
dynbuf_text &operator << (uint32 i) { add (sint64 (i)); return *this; } |
194 |
dynbuf_text &operator << (uint64 i) { add (sint64 (i)); return *this; } |
195 |
|
196 |
void printf (const char *format, ...) ecb_attribute ((format (printf, 2, 3))); |
197 |
void vprintf (const char *format, va_list ap); |
198 |
|
199 |
void add_abilities (const char *name, uint32 abilities); |
200 |
void add_paths (const char *name, uint32 paths); |
201 |
|
202 |
// we need to redefine, to keep the API :/ |
203 |
using dynbuf::splice; |
204 |
void splice (int offset, int olen, const char *s) |
205 |
{ |
206 |
dynbuf::splice (offset, olen, s, strlen (s)); |
207 |
} |
208 |
|
209 |
// returns the string, linearised and with trailing \0 |
210 |
operator char *(); |
211 |
}; |
212 |
|
213 |
#endif |
214 |
|