|
|
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> |
|
|
30 | |
|
|
31 | #include "util.h" |
|
|
32 | #include "shstr.h" |
6 | |
33 | |
7 | // this is a "buffer" that can grow fast |
34 | // this is a "buffer" that can grow fast |
8 | // and is still somewhat space-efficient. |
35 | // and is still somewhat space-efficient. |
9 | // unlike obstacks or other data structures, |
36 | // unlike obstacks or other data structures, |
10 | // it never moves data around. basically, |
37 | // it never moves data around. basically, |
11 | // this is a fast strstream without the overhead. |
38 | // this is a fast strstream without the overhead. |
12 | |
39 | |
13 | class dynbuf |
40 | struct dynbuf |
14 | { |
41 | { |
|
|
42 | protected: |
15 | struct chunk |
43 | struct chunk |
16 | { |
44 | { |
17 | chunk *next; |
45 | chunk *next; |
|
|
46 | int alloc; |
18 | int size; |
47 | int size; |
19 | char data[0]; |
48 | char data[0]; |
20 | }; |
49 | }; |
21 | |
50 | |
22 | char *ptr; |
51 | char *ptr, *end; |
23 | int room; |
|
|
24 | int ext; |
52 | int ext; |
25 | int _size; |
53 | int _size; |
26 | |
54 | |
27 | chunk *first, *last; |
55 | chunk *first, *last; |
28 | |
56 | |
29 | void _reserve (int size); |
57 | void _reserve (int size); |
|
|
58 | void _clear (); |
30 | void clear (); |
59 | void clear (); |
31 | void finish (); |
60 | void finish (); |
32 | |
61 | |
33 | public: |
62 | public: |
34 | |
63 | |
35 | dynbuf (int initial = 4096, int extend = 16384); |
64 | dynbuf (int initial = 4096, int extend = 16384); |
36 | ~dynbuf (); |
65 | ~dynbuf (); |
37 | |
66 | |
38 | int size () { return _size + (ptr - last->data); } |
67 | int size () const { return _size + (ptr - last->data); } |
|
|
68 | bool empty () const { return !size (); } |
39 | |
69 | |
40 | void linearise (void *data); |
70 | void linearise (void *data); |
41 | char *linearise (); |
71 | char *linearise (); // does not 0-terminate(!) |
|
|
72 | |
|
|
73 | int room () const { return end - ptr; } |
42 | |
74 | |
43 | char *force (int size) |
75 | char *force (int size) |
44 | { |
76 | { |
45 | if (room < size) |
77 | if (expect_false (ptr + size >= end)) |
46 | _reserve (size); |
78 | _reserve (size); |
47 | |
79 | |
48 | return ptr; |
80 | return ptr; |
49 | } |
81 | } |
50 | |
82 | |
51 | char *alloc (int size) |
83 | char *falloc (int size) |
52 | { |
84 | { |
53 | char *res = force (size); |
85 | char *res = ptr; |
54 | |
|
|
55 | room -= size; |
|
|
56 | ptr += size; |
86 | ptr += size; |
57 | |
|
|
58 | return res; |
87 | return res; |
59 | } |
88 | } |
60 | |
89 | |
|
|
90 | char *alloc (int size) |
|
|
91 | { |
|
|
92 | force (size); |
|
|
93 | return falloc (size); |
|
|
94 | } |
|
|
95 | |
61 | void fadd (char c) { --room; *ptr++ = c; } |
96 | void fadd (char c) { *ptr++ = c; } |
62 | 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 | } |
63 | |
102 | |
64 | void add (const void *p, int len) |
103 | void add (const void *p, int len) |
65 | { |
104 | { |
66 | memcpy (alloc (len), p, len); |
105 | force (len); |
|
|
106 | fadd (p, len); |
67 | } |
107 | } |
68 | |
108 | |
69 | void add (char c) |
109 | void add (char c) |
70 | { |
110 | { |
71 | if (room < 1) |
111 | alloc (1)[0] = c; |
72 | _reserve (1); |
|
|
73 | |
|
|
74 | room--; |
|
|
75 | *ptr++ = c; |
|
|
76 | } |
112 | } |
77 | |
113 | |
78 | void add (const char *s) |
114 | void add (const char *s) |
79 | { |
115 | { |
80 | add (s, strlen (s)); |
116 | add (s, strlen (s)); |
81 | } |
117 | } |
82 | |
118 | |
83 | static const int max_sint32_size = 11; |
119 | void add (const shstr &s) |
84 | static const int max_sint64_size = 20; |
120 | { |
85 | |
121 | add (s.s, s.length ()); |
86 | void add (sint32 i); |
122 | } |
87 | void add (sint64 i); |
|
|
88 | |
123 | |
89 | //TODO |
124 | //TODO |
90 | //void add_destructive (dynbuf &buf); |
125 | //void add_destructive (dynbuf &buf); |
91 | |
126 | |
92 | dynbuf &operator << (char c) { add (c); return *this; } |
127 | dynbuf &operator << (char c) { add (c); return *this; } |
93 | dynbuf &operator << (unsigned char c) { return *this << char (c); } |
128 | dynbuf &operator << (unsigned char c) { return *this << char (c); } |
94 | 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; } |
|
|
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, ...); |
95 | }; |
148 | }; |
96 | |
149 | |
97 | #endif |
150 | #endif |
|
|
151 | |