|
|
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 | |
1 | #ifndef DYNBUF_H__ |
25 | #ifndef DYNBUF_H__ |
2 | #define DYNBUF_H__ |
26 | #define DYNBUF_H__ |
3 | |
27 | |
4 | #include <cstring> |
28 | #include <cstring> |
5 | #include <cassert> |
29 | #include <cassert> |
6 | |
30 | |
|
|
31 | #include "util.h" |
7 | #include "shstr.h" |
32 | #include "shstr.h" |
8 | |
33 | |
9 | // this is a "buffer" that can grow fast |
34 | // this is a "buffer" that can grow fast |
10 | // and is still somewhat space-efficient. |
35 | // and is still somewhat space-efficient. |
11 | // unlike obstacks or other data structures, |
36 | // unlike obstacks or other data structures, |
… | |
… | |
16 | { |
41 | { |
17 | protected: |
42 | protected: |
18 | struct chunk |
43 | struct chunk |
19 | { |
44 | { |
20 | chunk *next; |
45 | chunk *next; |
|
|
46 | int alloc; |
21 | int size; |
47 | int size; |
22 | char data[0]; |
48 | char data[0]; |
23 | }; |
49 | }; |
24 | |
50 | |
25 | char *ptr; |
51 | char *ptr, *end; |
26 | int room; |
|
|
27 | int ext; |
52 | int ext; |
28 | int _size; |
53 | int _size; |
29 | |
54 | |
30 | chunk *first, *last; |
55 | chunk *first, *last; |
31 | |
56 | |
32 | void _reserve (int size); |
57 | void _reserve (int size); |
|
|
58 | void _clear (); |
33 | void clear (); |
59 | void clear (); |
34 | void finish (); |
60 | void finish (); |
35 | |
61 | |
36 | public: |
62 | public: |
37 | |
63 | |
38 | dynbuf (int initial = 4096, int extend = 16384); |
64 | dynbuf (int initial = 4096, int extend = 16384); |
39 | ~dynbuf (); |
65 | ~dynbuf (); |
40 | |
66 | |
41 | int size () { return _size + (ptr - last->data); } |
67 | int size () const { return _size + (ptr - last->data); } |
|
|
68 | bool empty () const { return !size (); } |
42 | |
69 | |
43 | void linearise (void *data); |
70 | void linearise (void *data); |
44 | char *linearise (); |
71 | char *linearise (); // does not 0-terminate(!) |
|
|
72 | |
|
|
73 | int room () const { return end - ptr; } |
45 | |
74 | |
46 | char *force (int size) |
75 | char *force (int size) |
47 | { |
76 | { |
48 | if (room < size) |
77 | if (expect_false (ptr + size >= end)) |
49 | _reserve (size); |
78 | _reserve (size); |
50 | |
79 | |
51 | return ptr; |
80 | return ptr; |
52 | } |
81 | } |
53 | |
82 | |
54 | char *alloc (int size) |
83 | char *falloc (int size) |
55 | { |
84 | { |
56 | char *res = force (size); |
85 | char *res = ptr; |
57 | |
|
|
58 | room -= size; |
|
|
59 | ptr += size; |
86 | ptr += size; |
60 | |
|
|
61 | return res; |
87 | return res; |
62 | } |
88 | } |
63 | |
89 | |
|
|
90 | char *alloc (int size) |
|
|
91 | { |
|
|
92 | force (size); |
|
|
93 | return falloc (size); |
|
|
94 | } |
|
|
95 | |
64 | void fadd (char c) { --room; *ptr++ = c; } |
96 | void fadd (char c) { *ptr++ = c; } |
65 | void fadd (unsigned char c) { fadd (char (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 | } |
66 | |
102 | |
67 | void add (const void *p, int len) |
103 | void add (const void *p, int len) |
68 | { |
104 | { |
69 | memcpy (alloc (len), p, len); |
105 | force (len); |
|
|
106 | fadd (p, len); |
70 | } |
107 | } |
71 | |
108 | |
72 | void add (char c) |
109 | void add (char c) |
73 | { |
110 | { |
74 | alloc (1)[0] = c; |
111 | alloc (1)[0] = c; |
… | |
… | |
88 | //void add_destructive (dynbuf &buf); |
125 | //void add_destructive (dynbuf &buf); |
89 | |
126 | |
90 | dynbuf &operator << (char c) { add (c); return *this; } |
127 | dynbuf &operator << (char c) { add (c); return *this; } |
91 | dynbuf &operator << (unsigned char c) { return *this << char (c); } |
128 | dynbuf &operator << (unsigned char c) { return *this << char (c); } |
92 | dynbuf &operator << (const char *s) { add (s); return *this; } |
129 | dynbuf &operator << (const char *s) { add (s); return *this; } |
|
|
130 | dynbuf &operator << (const shstr &s) { add (s); return *this; } |
93 | dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } |
131 | dynbuf &operator << (const std::string &s) { add (s.data(), s.size ()); return *this; } |
94 | |
132 | |
95 | operator std::string (); |
133 | operator std::string (); |
96 | }; |
134 | }; |
97 | |
135 | |
… | |
… | |
101 | : dynbuf (initial, extend) |
139 | : dynbuf (initial, extend) |
102 | { } |
140 | { } |
103 | |
141 | |
104 | using dynbuf::add; |
142 | using dynbuf::add; |
105 | |
143 | |
106 | static const int max_sint32_size = 11; |
|
|
107 | static const int max_sint64_size = 20; |
|
|
108 | |
|
|
109 | void add (sint32 i); |
144 | void add (sint32 i); |
110 | void add (sint64 i); |
145 | void add (sint64 i); |
111 | |
146 | |
112 | void printf (const char *format, ...); |
147 | void printf (const char *format, ...); |
113 | }; |
148 | }; |
114 | |
149 | |
115 | #endif |
150 | #endif |
|
|
151 | |