ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
Revision: 1.16
Committed: Wed Apr 30 06:40:28 2008 UTC (16 years ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_53
Changes since 1.15: +7 -7 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.11 /*
2 root 1.15 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.11 *
4 root 1.15 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.11 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992,2007 Frank Tore Johansen
7     *
8 root 1.15 * Deliantra is free software: you can redistribute it and/or modify
9 root 1.12 * 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 root 1.11 *
13 root 1.12 * 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 root 1.11 *
18 root 1.12 * 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 root 1.11 *
21 root 1.15 * The authors can be reached via e-mail to <support@deliantra.net>
22 root 1.11 */
23    
24 root 1.1 #ifndef DYNBUF_H__
25     #define DYNBUF_H__
26    
27     #include <cstring>
28     #include <cassert>
29    
30 root 1.8 #include "util.h"
31 root 1.4 #include "shstr.h"
32    
33 root 1.1 // this is a "buffer" that can grow fast
34     // and is still somewhat space-efficient.
35     // unlike obstacks or other data structures,
36     // it never moves data around. basically,
37     // this is a fast strstream without the overhead.
38    
39 root 1.4 struct dynbuf
40 root 1.1 {
41 root 1.4 protected:
42 root 1.1 struct chunk
43     {
44     chunk *next;
45 root 1.5 int alloc;
46 root 1.1 int size;
47     char data[0];
48     };
49    
50 root 1.6 char *ptr, *end;
51 root 1.1 int _size;
52    
53 root 1.13 int extend;
54 root 1.1 chunk *first, *last;
55    
56 root 1.13 void reserve (int size);
57     void init (int initial); // allocate sinitial chunk
58     void free (chunk *&chain); // free chain of chunks
59     char *_linearise ();
60     void finalise ();
61 root 1.1
62     public:
63    
64 root 1.13 // initial - the size of the initial chunk to be allocated
65     // extend - first incremental step when buffer size exceeded
66     dynbuf (int initial = 4096, int extend = 16384)
67     : extend (extend)
68     {
69     init (initial);
70     }
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 root 1.1
82 root 1.6 int size () const { return _size + (ptr - last->data); }
83     bool empty () const { return !size (); }
84 root 1.1
85     void linearise (void *data);
86 root 1.13 char *linearise () // does not 0-terminate(!)
87     {
88     return first->next ? _linearise () : first->data;
89     }
90 root 1.6
91     int room () const { return end - ptr; }
92 root 1.1
93     char *force (int size)
94     {
95 root 1.8 if (expect_false (ptr + size >= end))
96 root 1.13 reserve (size);
97 root 1.1
98     return ptr;
99     }
100    
101 root 1.10 char *falloc (int size)
102 root 1.1 {
103 root 1.10 char *res = ptr;
104 root 1.9 ptr += size;
105 root 1.1 return res;
106     }
107    
108 root 1.10 char *alloc (int size)
109     {
110     force (size);
111     return falloc (size);
112     }
113    
114 root 1.6 void fadd (char c) { *ptr++ = c; }
115 root 1.2 void fadd (unsigned char c) { fadd (char (c)); }
116 root 1.10 void fadd (const void *p, int len)
117     {
118     memcpy (falloc (len), p, len);
119     }
120 root 1.1
121     void add (const void *p, int len)
122     {
123 root 1.10 force (len);
124     fadd (p, len);
125 root 1.1 }
126    
127     void add (char c)
128     {
129 root 1.3 alloc (1)[0] = c;
130 root 1.1 }
131    
132     void add (const char *s)
133     {
134     add (s, strlen (s));
135     }
136    
137 root 1.4 void add (const shstr &s)
138     {
139     add (s.s, s.length ());
140     }
141 root 1.1
142     //TODO
143     //void add_destructive (dynbuf &buf);
144    
145     dynbuf &operator << (char c) { add (c); return *this; }
146     dynbuf &operator << (unsigned char c) { return *this << char (c); }
147     dynbuf &operator << (const char *s) { add (s); return *this; }
148 root 1.6 dynbuf &operator << (const shstr &s) { add (s); return *this; }
149 root 1.4 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 root 1.16 //TODO: should optimise the case printf "(name %+d)" as it comes up extremely often
165 root 1.13
166     using dynbuf::operator <<;
167 root 1.16 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 root 1.13
174 root 1.4 void printf (const char *format, ...);
175 root 1.14 void vprintf (const char *format, va_list ap);
176 root 1.13
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 * ();
182 root 1.1 };
183    
184     #endif
185 root 1.6