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

# User Rev Content
1 root 1.11 /*
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 root 1.1 #ifndef DYNBUF_H__
26     #define DYNBUF_H__
27    
28     #include <cstring>
29     #include <cassert>
30    
31 root 1.8 #include "util.h"
32 root 1.4 #include "shstr.h"
33    
34 root 1.1 // 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 root 1.4 struct dynbuf
41 root 1.1 {
42 root 1.4 protected:
43 root 1.1 struct chunk
44     {
45     chunk *next;
46 root 1.5 int alloc;
47 root 1.1 int size;
48     char data[0];
49     };
50    
51 root 1.6 char *ptr, *end;
52 root 1.1 int ext;
53     int _size;
54    
55     chunk *first, *last;
56    
57     void _reserve (int size);
58 root 1.7 void _clear ();
59 root 1.1 void clear ();
60     void finish ();
61    
62     public:
63    
64 root 1.2 dynbuf (int initial = 4096, int extend = 16384);
65 root 1.1 ~dynbuf ();
66    
67 root 1.6 int size () const { return _size + (ptr - last->data); }
68     bool empty () const { return !size (); }
69 root 1.1
70     void linearise (void *data);
71 root 1.6 char *linearise (); // does not 0-terminate(!)
72    
73     int room () const { return end - ptr; }
74 root 1.1
75     char *force (int size)
76     {
77 root 1.8 if (expect_false (ptr + size >= end))
78 root 1.1 _reserve (size);
79    
80     return ptr;
81     }
82    
83 root 1.10 char *falloc (int size)
84 root 1.1 {
85 root 1.10 char *res = ptr;
86 root 1.9 ptr += size;
87 root 1.1 return res;
88     }
89    
90 root 1.10 char *alloc (int size)
91     {
92     force (size);
93     return falloc (size);
94     }
95    
96 root 1.6 void fadd (char c) { *ptr++ = c; }
97 root 1.2 void fadd (unsigned char c) { fadd (char (c)); }
98 root 1.10 void fadd (const void *p, int len)
99     {
100     memcpy (falloc (len), p, len);
101     }
102 root 1.1
103     void add (const void *p, int len)
104     {
105 root 1.10 force (len);
106     fadd (p, len);
107 root 1.1 }
108    
109     void add (char c)
110     {
111 root 1.3 alloc (1)[0] = c;
112 root 1.1 }
113    
114     void add (const char *s)
115     {
116     add (s, strlen (s));
117     }
118    
119 root 1.4 void add (const shstr &s)
120     {
121     add (s.s, s.length ());
122     }
123 root 1.1
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 root 1.6 dynbuf &operator << (const shstr &s) { add (s); return *this; }
131 root 1.4 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 root 1.1 };
149    
150     #endif
151 root 1.6