|
|
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, |
… | |
… | |
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 | |
… | |
… | |
46 | |
72 | |
47 | int room () const { return end - ptr; } |
73 | int room () const { return end - ptr; } |
48 | |
74 | |
49 | char *force (int size) |
75 | char *force (int size) |
50 | { |
76 | { |
51 | if (ptr + size >= end) |
77 | if (expect_false (ptr + size >= end)) |
52 | _reserve (size); |
78 | _reserve (size); |
53 | |
79 | |
54 | return ptr; |
80 | return ptr; |
55 | } |
81 | } |
56 | |
82 | |
|
|
83 | char *falloc (int size) |
|
|
84 | { |
|
|
85 | char *res = ptr; |
|
|
86 | ptr += size; |
|
|
87 | return res; |
|
|
88 | } |
|
|
89 | |
57 | char *alloc (int size) |
90 | char *alloc (int size) |
58 | { |
91 | { |
59 | char *res = force (size); |
92 | force (size); |
60 | ptr += size; |
93 | return falloc (size); |
61 | return res; |
|
|
62 | } |
94 | } |
63 | |
95 | |
64 | void fadd (char c) { *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; |
… | |
… | |
102 | : dynbuf (initial, extend) |
139 | : dynbuf (initial, extend) |
103 | { } |
140 | { } |
104 | |
141 | |
105 | using dynbuf::add; |
142 | using dynbuf::add; |
106 | |
143 | |
107 | static const int max_sint32_size = 11; |
|
|
108 | static const int max_sint64_size = 20; |
|
|
109 | |
|
|
110 | void add (sint32 i); |
144 | void add (sint32 i); |
111 | void add (sint64 i); |
145 | void add (sint64 i); |
112 | |
146 | |
113 | void printf (const char *format, ...); |
147 | void printf (const char *format, ...); |
114 | }; |
148 | }; |