ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/dynbuf.h
Revision: 1.12
Committed: Sun Jul 1 05:00:18 2007 UTC (16 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.11: +11 -12 lines
Log Message:
- upgrade crossfire trt to the GPL version 3 (hopefully correctly).
- add a single file covered by the GNU Affero General Public License
  (which is not yet released, so I used the current draft, which is
  legally a bit wavy, but its likely better than nothing as it expresses
  direct intent by the authors, and we can upgrade as soon as it has been
  released).
  * this should ensure availability of source code for the server at least
    and hopefully also archetypes and maps even when modified versions
    are not being distributed, in accordance of section 13 of the agplv3.

File Contents

# User Rev Content
1 root 1.11 /*
2 root 1.12 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG.
3 root 1.11 *
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 root 1.12 * Crossfire TRT 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 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     * The authors can be reached via e-mail to <crossfire@schmorp.de>
22     */
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 ext;
52     int _size;
53    
54     chunk *first, *last;
55    
56     void _reserve (int size);
57 root 1.7 void _clear ();
58 root 1.1 void clear ();
59     void finish ();
60    
61     public:
62    
63 root 1.2 dynbuf (int initial = 4096, int extend = 16384);
64 root 1.1 ~dynbuf ();
65    
66 root 1.6 int size () const { return _size + (ptr - last->data); }
67     bool empty () const { return !size (); }
68 root 1.1
69     void linearise (void *data);
70 root 1.6 char *linearise (); // does not 0-terminate(!)
71    
72     int room () const { return end - ptr; }
73 root 1.1
74     char *force (int size)
75     {
76 root 1.8 if (expect_false (ptr + size >= end))
77 root 1.1 _reserve (size);
78    
79     return ptr;
80     }
81    
82 root 1.10 char *falloc (int size)
83 root 1.1 {
84 root 1.10 char *res = ptr;
85 root 1.9 ptr += size;
86 root 1.1 return res;
87     }
88    
89 root 1.10 char *alloc (int size)
90     {
91     force (size);
92     return falloc (size);
93     }
94    
95 root 1.6 void fadd (char c) { *ptr++ = c; }
96 root 1.2 void fadd (unsigned char c) { fadd (char (c)); }
97 root 1.10 void fadd (const void *p, int len)
98     {
99     memcpy (falloc (len), p, len);
100     }
101 root 1.1
102     void add (const void *p, int len)
103     {
104 root 1.10 force (len);
105     fadd (p, len);
106 root 1.1 }
107    
108     void add (char c)
109     {
110 root 1.3 alloc (1)[0] = c;
111 root 1.1 }
112    
113     void add (const char *s)
114     {
115     add (s, strlen (s));
116     }
117    
118 root 1.4 void add (const shstr &s)
119     {
120     add (s.s, s.length ());
121     }
122 root 1.1
123     //TODO
124     //void add_destructive (dynbuf &buf);
125    
126     dynbuf &operator << (char c) { add (c); return *this; }
127     dynbuf &operator << (unsigned char c) { return *this << char (c); }
128     dynbuf &operator << (const char *s) { add (s); return *this; }
129 root 1.6 dynbuf &operator << (const shstr &s) { add (s); return *this; }
130 root 1.4 dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; }
131    
132     operator std::string ();
133     };
134    
135     struct dynbuf_text : dynbuf
136     {
137     dynbuf_text (int initial = 4096, int extend = 16384)
138     : dynbuf (initial, extend)
139     { }
140    
141     using dynbuf::add;
142    
143     void add (sint32 i);
144     void add (sint64 i);
145    
146     void printf (const char *format, ...);
147 root 1.1 };
148    
149     #endif
150 root 1.6