996
|
1 /*
|
|
2 This code was taken (and slightly modified to compile in cfunge) from:
|
|
3 CrossFire, A Multiplayer game for X-windows
|
|
4
|
|
5 Copyright (C) 2008 Crossfire Development Team
|
|
6
|
|
7 This program is free software; you can redistribute it and/or modify
|
|
8 it under the terms of the GNU General Public License as published by
|
|
9 the Free Software Foundation; either version 2 of the License, or
|
|
10 (at your option) any later version.
|
|
11
|
|
12 This program is distributed in the hope that it will be useful,
|
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15 GNU General Public License for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with this program; if not, write to the Free Software
|
|
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
20
|
|
21 The authors can be reached via e-mail at crossfire-devel@real-time.com
|
|
22 */
|
|
23
|
|
24 /*
|
|
25 * Modifications for cfunge:
|
|
26 * - Drop some crossfire specific includes and functions that depended on other
|
|
27 * parts of crossfire.
|
|
28 * - Adding stringbuffer_destroy
|
|
29 * - Adding GCC attributes.
|
|
30 *
|
|
31 */
|
|
32
|
|
33 #include <stdarg.h>
|
|
34 #include <stdlib.h>
|
|
35 #include <string.h>
|
|
36
|
|
37 #include "../../src/global.h"
|
|
38 #include <stdbool.h>
|
|
39 #include "stringbuffer.h"
|
|
40
|
|
41
|
|
42 struct StringBuffer {
|
|
43 /**
|
|
44 * The string buffer. The first {@link #pos} bytes contain the collected
|
|
45 * string. It's size is at least {@link #size} bytes.
|
|
46 */
|
|
47 char *buf;
|
|
48
|
|
49 /**
|
|
50 * The current length of {@link #buf}. The invariant <code>pos <
|
|
51 * size</code> always holds; this means there is always enough room to
|
|
52 * attach a trailing \0 character.
|
|
53 */
|
|
54 size_t pos;
|
|
55
|
|
56 /**
|
|
57 * The allocation size of {@link #buf}.
|
|
58 */
|
|
59 size_t size;
|
|
60 };
|
|
61
|
|
62
|
|
63 /**
|
|
64 * Make sure that at least <code>len</code> bytes are available in the passed
|
|
65 * string buffer.
|
|
66 *
|
|
67 * @param sb The string buffer to modify.
|
|
68 *
|
|
69 * @param len The number of bytes to allocate.
|
|
70 */
|
|
71 FUNGE_ATTR_FAST
|
|
72 static bool stringbuffer_ensure(StringBuffer *sb, size_t len);
|
|
73
|
|
74 FUNGE_ATTR_FAST
|
|
75 StringBuffer *stringbuffer_new(void)
|
|
76 {
|
|
77 StringBuffer *sb;
|
|
78
|
|
79 sb = malloc(sizeof(*sb));
|
|
80 if (sb == NULL) {
|
|
81 return NULL;
|
|
82 }
|
|
83
|
|
84 sb->size = 256;
|
|
85 sb->buf = malloc(sb->size);
|
|
86 sb->pos = 0;
|
|
87 return sb;
|
|
88 }
|
|
89
|
|
90 FUNGE_ATTR_FAST
|
|
91 void stringbuffer_destroy(StringBuffer *sb)
|
|
92 {
|
|
93 free(sb->buf);
|
|
94 free(sb);
|
|
95 }
|
|
96
|
|
97 FUNGE_ATTR_FAST
|
|
98 char *stringbuffer_finish(StringBuffer * restrict sb, size_t * restrict length)
|
|
99 {
|
|
100 char *result;
|
|
101
|
|
102 sb->buf[sb->pos] = '\0';
|
|
103 if (length)
|
|
104 *length = sb->pos;
|
|
105 result = sb->buf;
|
|
106 free(sb);
|
|
107 return result;
|
|
108 }
|
|
109
|
|
110 FUNGE_ATTR_FAST
|
|
111 void stringbuffer_append_char(StringBuffer *sb, const char c)
|
|
112 {
|
|
113 stringbuffer_ensure(sb, 2);
|
|
114 sb->buf[sb->pos] = c;
|
|
115 sb->pos += 1;
|
|
116 }
|
|
117
|
|
118 FUNGE_ATTR_FAST
|
|
119 void stringbuffer_append_string(StringBuffer *sb, const char *str)
|
|
120 {
|
|
121 size_t len;
|
|
122
|
|
123 len = strlen(str);
|
|
124 stringbuffer_ensure(sb, len+1);
|
|
125 memcpy(sb->buf+sb->pos, str, len);
|
|
126 sb->pos += len;
|
|
127 }
|
|
128
|
|
129 FUNGE_ATTR_FAST
|
|
130 void stringbuffer_append_printf(StringBuffer *sb, const char *format, ...)
|
|
131 {
|
|
132 size_t size;
|
|
133
|
|
134 size = 100; /* arbitrary guess */
|
|
135 for (;;) {
|
|
136 int n;
|
|
137 va_list arg;
|
|
138
|
|
139 stringbuffer_ensure(sb, size);
|
|
140
|
|
141 va_start(arg, format);
|
|
142 n = vsnprintf(sb->buf+sb->pos, size, format, arg);
|
|
143 va_end(arg);
|
|
144
|
|
145 if (n > -1 && (size_t)n < size) {
|
|
146 sb->pos += (size_t)n;
|
|
147 break;
|
|
148 }
|
|
149
|
|
150 if (n > -1) {
|
|
151 size = (size_t)(n+1); /* precisely what is needed */
|
|
152 } else {
|
|
153 size *= 2; /* twice the old size */
|
|
154 }
|
|
155 }
|
|
156 }
|
|
157
|
|
158 FUNGE_ATTR_FAST
|
|
159 void stringbuffer_append_stringbuffer(StringBuffer * restrict sb,
|
|
160 const StringBuffer * restrict sb2)
|
|
161 {
|
|
162 stringbuffer_ensure(sb, sb2->pos+1);
|
|
163 memcpy(sb->buf+sb->pos, sb2->buf, sb2->pos);
|
|
164 sb->pos += sb2->pos;
|
|
165 }
|
|
166
|
|
167 FUNGE_ATTR_FAST
|
|
168 static bool stringbuffer_ensure(StringBuffer *sb, size_t len)
|
|
169 {
|
|
170 char *tmp;
|
|
171 size_t new_size;
|
|
172
|
|
173 if (sb->pos+len <= sb->size) {
|
|
174 return true;
|
|
175 }
|
|
176
|
|
177 new_size = sb->pos+len+256;
|
|
178 tmp = realloc(sb->buf, new_size);
|
|
179 if (tmp == NULL) {
|
|
180 return false;
|
|
181 }
|
|
182 sb->buf = tmp;
|
|
183 sb->size = new_size;
|
|
184 return true;
|
|
185 }
|