ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
(Generate patch)

Comparing deliantra/server/include/dynbuf.h (file contents):
Revision 1.20 by root, Mon Sep 15 00:16:19 2008 UTC vs.
Revision 1.40 by root, Wed Dec 5 21:18:37 2018 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / 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
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 6 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 7 * Copyright (©) 1992 Frank Tore Johansen
7 * 8 *
8 * Deliantra is free software: you can redistribute it and/or modify 9 * Deliantra is free software: you can redistribute it and/or modify it under
9 * it under the terms of the GNU General Public License as published by 10 * the terms of the Affero GNU General Public License as published by the
10 * the Free Software Foundation, either version 3 of the License, or 11 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your option) any later version. 12 * option) any later version.
12 * 13 *
13 * This program is distributed in the hope that it will be useful, 14 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 17 * GNU General Public License for more details.
17 * 18 *
18 * You should have received a copy of the GNU General Public License 19 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 20 * and the GNU General Public License along with this program. If not, see
21 * <http://www.gnu.org/licenses/>.
20 * 22 *
21 * The authors can be reached via e-mail to <support@deliantra.net> 23 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 24 */
23 25
24#ifndef DYNBUF_H__ 26#ifndef DYNBUF_H__
25#define DYNBUF_H__ 27#define DYNBUF_H__
26 28
27#include <cstring> 29#include <cstring>
28#include <cassert> 30#include <cassert>
29 31
32#include "ecb.h"
30#include "util.h" 33#include "util.h"
31#include "shstr.h" 34#include "shstr.h"
32 35
33// this is a "buffer" that can grow fast 36// this is a "buffer" that can grow fast
34// and is still somewhat space-efficient. 37// and is still somewhat space-efficient.
54 chunk *first, *last; 57 chunk *first, *last;
55 58
56 void reserve (int size); 59 void reserve (int size);
57 void init (int initial); // allocate sinitial chunk 60 void init (int initial); // allocate sinitial chunk
58 void free (chunk *&chain); // free chain of chunks 61 void free (chunk *&chain); // free chain of chunks
59 char *_linearise (); 62 char *_linearise (int extra = 0);
60 void finalise (); 63 void finalise ();
61 64
62public: 65public:
63 66
64 // initial - the size of the initial chunk to be allocated 67 // initial - the size of the initial chunk to be allocated
82 int size () const { return _size + (ptr - last->data); } 85 int size () const { return _size + (ptr - last->data); }
83 bool empty () const { return !size (); } 86 bool empty () const { return !size (); }
84 87
85 void linearise (void *data); 88 void linearise (void *data);
86 char *linearise () // does not 0-terminate(!) 89 char *linearise () // does not 0-terminate(!)
87 { 90 {
88 return first->next ? _linearise () : first->data; 91 return first->next ? _linearise () : first->data;
89 } 92 }
90 93
91 int room () const { return end - ptr; } 94 int room () const { return end - ptr; }
92 95
96 // make sure we have "size" extra room
93 char *force (int size) 97 char *force (int size)
94 { 98 {
95 if (expect_false (ptr + size >= end)) 99 if (ecb_expect_false (ptr + size > end))
96 reserve (size); 100 reserve (size);
97 101
102 ecb_assume (ptr + size <= end);
103
98 return ptr; 104 return ptr;
99 } 105 }
100 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())
101 char *falloc (int size) 114 char *falloc (int size)
102 { 115 {
103 char *res = ptr; 116 char *res = ptr;
104 ptr += size; 117 ptr += size;
105 return res; 118 return res;
106 } 119 }
107 120
121 // allocate size bytes and return pointer to them
108 char *alloc (int size) 122 char *alloc (int size)
109 { 123 {
110 force (size); 124 force (size);
111 return falloc (size); 125 return falloc (size);
112 } 126 }
132 void add (const char *s) 146 void add (const char *s)
133 { 147 {
134 add (s, strlen (s)); 148 add (s, strlen (s));
135 } 149 }
136 150
137 void add (const shstr &s) 151 void add (shstr_tmp s)
138 { 152 {
139 add (s.s, s.length ()); 153 add (s.s, s.length ());
140 } 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); }
141 159
142 //TODO 160 //TODO
143 //void add_destructive (dynbuf &buf); 161 //void add_destructive (dynbuf &buf);
144 162
145 dynbuf &operator << (char c) { add (c); return *this; } 163 dynbuf &operator << (char c) { add (c); return *this; }
146 dynbuf &operator << (unsigned char c) { return *this << char (c); } 164 dynbuf &operator << (unsigned char c) { return *this << char (c); }
147 dynbuf &operator << (const char *s) { add (s); return *this; } 165 dynbuf &operator << (const char *s) { add (s); return *this; }
148 dynbuf &operator << (const shstr &s) { add (s); return *this; } 166 dynbuf &operator << (shstr_tmp s) { add (s); return *this; }
149 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } 167 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
150 168
151 operator std::string (); 169 operator std::string ();
152}; 170};
153 171
173 dynbuf_text &operator << (sint32 i) { add (sint32 (i)); return *this; } 191 dynbuf_text &operator << (sint32 i) { add (sint32 (i)); return *this; }
174 dynbuf_text &operator << (sint64 i) { add (sint64 (i)); return *this; } 192 dynbuf_text &operator << (sint64 i) { add (sint64 (i)); return *this; }
175 dynbuf_text &operator << (uint32 i) { add (sint64 (i)); return *this; } 193 dynbuf_text &operator << (uint32 i) { add (sint64 (i)); return *this; }
176 dynbuf_text &operator << (uint64 i) { add (sint64 (i)); return *this; } 194 dynbuf_text &operator << (uint64 i) { add (sint64 (i)); return *this; }
177 195
178 void printf (const char *format, ...); 196 void printf (const char *format, ...) ecb_attribute ((format (printf, 2, 3)));
179 void vprintf (const char *format, va_list ap); 197 void vprintf (const char *format, va_list ap);
180 198
181 void add_abilities (const char *name, uint32 abilities); 199 void add_abilities (const char *name, uint32 abilities);
182 void add_paths (const char *name, uint32 paths); 200 void add_paths (const char *name, uint32 paths);
183 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
184 // returns the string, linearised and with trailing \0 209 // returns the string, linearised and with trailing \0
185 operator const char * (); 210 operator char *();
186}; 211};
187 212
188#endif 213#endif
189 214

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines