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