ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
Revision: 1.11
Committed: Mon May 28 21:15:56 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +24 -0 lines
Log Message:
- update copyrights in .h files, where applicable
- rename preprocess to genkeywords

File Contents

# Content
1 /*
2 * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game.
3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 *
8 * Crossfire TRT is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51
20 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 * The authors can be reached via e-mail to <crossfire@schmorp.de>
23 */
24
25 #ifndef DYNBUF_H__
26 #define DYNBUF_H__
27
28 #include <cstring>
29 #include <cassert>
30
31 #include "util.h"
32 #include "shstr.h"
33
34 // this is a "buffer" that can grow fast
35 // and is still somewhat space-efficient.
36 // unlike obstacks or other data structures,
37 // it never moves data around. basically,
38 // this is a fast strstream without the overhead.
39
40 struct dynbuf
41 {
42 protected:
43 struct chunk
44 {
45 chunk *next;
46 int alloc;
47 int size;
48 char data[0];
49 };
50
51 char *ptr, *end;
52 int ext;
53 int _size;
54
55 chunk *first, *last;
56
57 void _reserve (int size);
58 void _clear ();
59 void clear ();
60 void finish ();
61
62 public:
63
64 dynbuf (int initial = 4096, int extend = 16384);
65 ~dynbuf ();
66
67 int size () const { return _size + (ptr - last->data); }
68 bool empty () const { return !size (); }
69
70 void linearise (void *data);
71 char *linearise (); // does not 0-terminate(!)
72
73 int room () const { return end - ptr; }
74
75 char *force (int size)
76 {
77 if (expect_false (ptr + size >= end))
78 _reserve (size);
79
80 return ptr;
81 }
82
83 char *falloc (int size)
84 {
85 char *res = ptr;
86 ptr += size;
87 return res;
88 }
89
90 char *alloc (int size)
91 {
92 force (size);
93 return falloc (size);
94 }
95
96 void fadd (char c) { *ptr++ = c; }
97 void fadd (unsigned char c) { fadd (char (c)); }
98 void fadd (const void *p, int len)
99 {
100 memcpy (falloc (len), p, len);
101 }
102
103 void add (const void *p, int len)
104 {
105 force (len);
106 fadd (p, len);
107 }
108
109 void add (char c)
110 {
111 alloc (1)[0] = c;
112 }
113
114 void add (const char *s)
115 {
116 add (s, strlen (s));
117 }
118
119 void add (const shstr &s)
120 {
121 add (s.s, s.length ());
122 }
123
124 //TODO
125 //void add_destructive (dynbuf &buf);
126
127 dynbuf &operator << (char c) { add (c); return *this; }
128 dynbuf &operator << (unsigned char c) { return *this << char (c); }
129 dynbuf &operator << (const char *s) { add (s); return *this; }
130 dynbuf &operator << (const shstr &s) { add (s); return *this; }
131 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
132
133 operator std::string ();
134 };
135
136 struct dynbuf_text : dynbuf
137 {
138 dynbuf_text (int initial = 4096, int extend = 16384)
139 : dynbuf (initial, extend)
140 { }
141
142 using dynbuf::add;
143
144 void add (sint32 i);
145 void add (sint64 i);
146
147 void printf (const char *format, ...);
148 };
149
150 #endif
151