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