/src/postgis/liblwgeom/stringbuffer.c
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * |
3 | | * PostGIS - Spatial Types for PostgreSQL |
4 | | * http://postgis.net |
5 | | * |
6 | | * PostGIS is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation, either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * PostGIS is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with PostGIS. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | ********************************************************************** |
20 | | * |
21 | | * Copyright 2002 Thamer Alharbash |
22 | | * Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca> |
23 | | * |
24 | | **********************************************************************/ |
25 | | |
26 | | #include "liblwgeom_internal.h" |
27 | | #include "stringbuffer.h" |
28 | | |
29 | | /** |
30 | | * Allocate a new stringbuffer_t. Use stringbuffer_destroy to free. |
31 | | */ |
32 | | stringbuffer_t* |
33 | | stringbuffer_create(void) |
34 | 0 | { |
35 | 0 | return stringbuffer_create_with_size(STRINGBUFFER_STARTSIZE); |
36 | 0 | } |
37 | | |
38 | | static void |
39 | | stringbuffer_init_with_size(stringbuffer_t *s, size_t size) |
40 | 0 | { |
41 | 0 | s->str_start = lwalloc(size); |
42 | 0 | s->str_end = s->str_start; |
43 | 0 | s->capacity = size; |
44 | 0 | s->success = true; |
45 | 0 | memset(s->str_start, 0, size); |
46 | 0 | } |
47 | | |
48 | | void |
49 | | stringbuffer_release(stringbuffer_t *s) |
50 | 0 | { |
51 | 0 | if ( s->str_start ) lwfree(s->str_start); |
52 | 0 | } |
53 | | |
54 | | void |
55 | | stringbuffer_init(stringbuffer_t *s) |
56 | 0 | { |
57 | 0 | stringbuffer_init_with_size(s, STRINGBUFFER_STARTSIZE); |
58 | 0 | } |
59 | | |
60 | | void |
61 | | stringbuffer_init_varlena(stringbuffer_t *s) |
62 | 0 | { |
63 | 0 | stringbuffer_init_with_size(s, STRINGBUFFER_STARTSIZE + LWVARHDRSZ); |
64 | | /* Zero out LWVARHDRSZ bytes at the front of the buffer */ |
65 | 0 | stringbuffer_append_len(s, "\0\0\0\0\0\0\0\0", LWVARHDRSZ); |
66 | 0 | } |
67 | | |
68 | | |
69 | | /** |
70 | | * Allocate a new stringbuffer_t. Use stringbuffer_destroy to free. |
71 | | */ |
72 | | stringbuffer_t* |
73 | | stringbuffer_create_with_size(size_t size) |
74 | 0 | { |
75 | 0 | stringbuffer_t *s; |
76 | 0 | if (size > LW_MAX_ALLOC_SIZE) |
77 | 0 | { |
78 | 0 | lwerror("buffer exceeds the maximum allocation size"); |
79 | 0 | return NULL; |
80 | 0 | } |
81 | | |
82 | 0 | s = lwalloc(sizeof(stringbuffer_t)); |
83 | 0 | stringbuffer_init_with_size(s, size); |
84 | 0 | return s; |
85 | 0 | } |
86 | | |
87 | | /** |
88 | | * Free the stringbuffer_t and all memory managed within it. |
89 | | */ |
90 | | void |
91 | | stringbuffer_destroy(stringbuffer_t *s) |
92 | 0 | { |
93 | 0 | if ( s ) |
94 | 0 | { |
95 | 0 | stringbuffer_release(s); |
96 | 0 | lwfree(s); |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | | /** |
101 | | * Reset the stringbuffer_t. Useful for starting a fresh string |
102 | | * without the expense of freeing and re-allocating a new |
103 | | * stringbuffer_t. |
104 | | */ |
105 | | void |
106 | | stringbuffer_clear(stringbuffer_t *s) |
107 | 0 | { |
108 | 0 | s->str_start[0] = '\0'; |
109 | 0 | s->str_end = s->str_start; |
110 | 0 | s->success = true; |
111 | 0 | } |
112 | | |
113 | | /** |
114 | | * Return the last character in the buffer. |
115 | | */ |
116 | | char |
117 | | stringbuffer_lastchar(stringbuffer_t *s) |
118 | 0 | { |
119 | 0 | if( s->str_end == s->str_start ) |
120 | 0 | return 0; |
121 | | |
122 | 0 | return *(s->str_end - 1); |
123 | 0 | } |
124 | | |
125 | | |
126 | | /** |
127 | | * Returns a reference to the internal string being managed by |
128 | | * the stringbuffer. The current string will be null-terminated |
129 | | * within the internal string. |
130 | | */ |
131 | | const char* |
132 | | stringbuffer_getstring(stringbuffer_t *s) |
133 | 0 | { |
134 | 0 | if (!s->success) |
135 | 0 | return NULL; |
136 | 0 | return s->str_start; |
137 | 0 | } |
138 | | |
139 | | /** |
140 | | * Returns a newly allocated string large enough to contain the |
141 | | * current state of the string. Caller is responsible for |
142 | | * freeing the return value. |
143 | | */ |
144 | | char* |
145 | | stringbuffer_getstringcopy(stringbuffer_t *s) |
146 | 0 | { |
147 | 0 | size_t size; |
148 | 0 | if (!s->success || !lw_size_add(s->str_end - s->str_start, 1, &size) || size > LW_MAX_ALLOC_SIZE) |
149 | 0 | { |
150 | 0 | lwerror("string exceeds the maximum allocation size"); |
151 | 0 | return NULL; |
152 | 0 | } |
153 | 0 | char *str = lwalloc(size); |
154 | 0 | memcpy(str, s->str_start, size); |
155 | 0 | str[size - 1] = '\0'; |
156 | 0 | return str; |
157 | 0 | } |
158 | | |
159 | | lwvarlena_t * |
160 | | stringbuffer_getvarlena(stringbuffer_t *s) |
161 | 0 | { |
162 | 0 | lwvarlena_t *output = (lwvarlena_t *)(s->str_start); |
163 | 0 | size_t size = s->str_end - s->str_start; |
164 | 0 | if (!s->success || size > LW_MAX_ALLOC_SIZE) |
165 | 0 | { |
166 | 0 | lwerror("string exceeds the maximum varlena size"); |
167 | 0 | return NULL; |
168 | 0 | } |
169 | 0 | LWSIZE_SET(output->size, size); |
170 | 0 | return output; |
171 | 0 | } |
172 | | |
173 | | lwvarlena_t * |
174 | | stringbuffer_getvarlenacopy(stringbuffer_t *s) |
175 | 0 | { |
176 | 0 | size_t size = (s->str_end - s->str_start); |
177 | 0 | if (!s->success || size > LW_MAX_ALLOC_SIZE - LWVARHDRSZ) |
178 | 0 | { |
179 | 0 | lwerror("string exceeds the maximum varlena size"); |
180 | 0 | return NULL; |
181 | 0 | } |
182 | 0 | lwvarlena_t *output = (lwvarlena_t *)lwalloc(size + LWVARHDRSZ); |
183 | 0 | LWSIZE_SET(output->size, size + LWVARHDRSZ); |
184 | |
|
185 | 0 | memcpy(output->data, s->str_start, size); |
186 | 0 | return output; |
187 | 0 | } |
188 | | |
189 | | /** |
190 | | * Returns the length of the current string, not including the |
191 | | * null terminator (same behavior as strlen()). |
192 | | */ |
193 | | int |
194 | | stringbuffer_getlength(stringbuffer_t *s) |
195 | 0 | { |
196 | 0 | return (s->str_end - s->str_start); |
197 | 0 | } |
198 | | |
199 | | /** |
200 | | * Clear the stringbuffer_t and re-start it with the specified string. |
201 | | */ |
202 | | void |
203 | | stringbuffer_set(stringbuffer_t *s, const char *str) |
204 | 0 | { |
205 | 0 | stringbuffer_clear(s); |
206 | 0 | stringbuffer_append(s, str); |
207 | 0 | } |
208 | | |
209 | | /** |
210 | | * Copy the contents of src into dst. |
211 | | */ |
212 | | void |
213 | | stringbuffer_copy(stringbuffer_t *dst, stringbuffer_t *src) |
214 | 0 | { |
215 | 0 | stringbuffer_set(dst, stringbuffer_getstring(src)); |
216 | 0 | } |
217 | | |
218 | | /** |
219 | | * Appends a formatted string to the current string buffer, |
220 | | * using the format and argument list provided. Returns -1 on error, |
221 | | * check errno for reasons, documented in the printf man page. |
222 | | */ |
223 | | static int |
224 | | stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap) __attribute__ ((format (printf, 2, 0))); |
225 | | static int |
226 | | stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap) |
227 | 0 | { |
228 | 0 | int maxlen = (s->capacity - (s->str_end - s->str_start)); |
229 | 0 | int len = 0; /* Length of the output */ |
230 | 0 | va_list ap2; |
231 | | |
232 | | /* Make a copy of the variadic arguments, in case we need to print twice */ |
233 | | /* Print to our buffer */ |
234 | 0 | va_copy(ap2, ap); |
235 | 0 | len = vsnprintf(s->str_end, maxlen, fmt, ap2); |
236 | 0 | va_end(ap2); |
237 | | |
238 | | /* Propagate errors up */ |
239 | 0 | if ( len < 0 ) |
240 | | #if defined(__MINGW64_VERSION_MAJOR) |
241 | | { |
242 | | va_copy(ap2, ap); |
243 | | len = _vscprintf(fmt, ap2);/**Assume windows flaky vsnprintf that returns -1 if initial buffer to small and add more space **/ |
244 | | va_end(ap2); |
245 | | } |
246 | | #else |
247 | 0 | return len; |
248 | 0 | #endif |
249 | | |
250 | | /* We didn't have enough space! */ |
251 | | /* Either Unix vsnprint returned write length larger than our buffer */ |
252 | | /* or Windows vsnprintf returned an error code. */ |
253 | 0 | if ( len >= maxlen ) |
254 | 0 | { |
255 | 0 | if (!stringbuffer_makeroom(s, (size_t)len + 1)) |
256 | 0 | return 0; /* The error was raised; append nothing if execution continues. */ |
257 | 0 | maxlen = (s->capacity - (s->str_end - s->str_start)); |
258 | | |
259 | | /* Try to print a second time */ |
260 | 0 | len = vsnprintf(s->str_end, maxlen, fmt, ap); |
261 | | |
262 | | /* Printing error? Error! */ |
263 | 0 | if ( len < 0 ) return len; |
264 | | /* Too long still? Error! */ |
265 | 0 | if ( len >= maxlen ) return -1; |
266 | 0 | } |
267 | | |
268 | | /* Move end pointer forward and return. */ |
269 | 0 | s->str_end += len; |
270 | 0 | return len; |
271 | 0 | } |
272 | | |
273 | | /** |
274 | | * Appends a formatted string to the current string buffer, |
275 | | * using the format and argument list provided. |
276 | | * Returns -1 on error, check errno for reasons, |
277 | | * as documented in the printf man page. |
278 | | */ |
279 | | int |
280 | | stringbuffer_aprintf(stringbuffer_t *s, const char *fmt, ...) |
281 | 0 | { |
282 | 0 | int r; |
283 | 0 | va_list ap; |
284 | 0 | va_start(ap, fmt); |
285 | 0 | r = stringbuffer_avprintf(s, fmt, ap); |
286 | 0 | va_end(ap); |
287 | 0 | return r; |
288 | 0 | } |
289 | | |
290 | | /** |
291 | | * Trims whitespace off the end of the stringbuffer. Returns |
292 | | * the number of characters trimmed. |
293 | | */ |
294 | | int |
295 | | stringbuffer_trim_trailing_white(stringbuffer_t *s) |
296 | 0 | { |
297 | 0 | char *ptr = s->str_end; |
298 | 0 | int dist = 0; |
299 | | |
300 | | /* Roll backwards until we hit a non-space. */ |
301 | 0 | while( ptr > s->str_start ) |
302 | 0 | { |
303 | 0 | ptr--; |
304 | 0 | if( (*ptr == ' ') || (*ptr == '\t') ) |
305 | 0 | { |
306 | 0 | continue; |
307 | 0 | } |
308 | 0 | else |
309 | 0 | { |
310 | 0 | ptr++; |
311 | 0 | dist = s->str_end - ptr; |
312 | 0 | *ptr = '\0'; |
313 | 0 | s->str_end = ptr; |
314 | 0 | return dist; |
315 | 0 | } |
316 | 0 | } |
317 | 0 | return dist; |
318 | 0 | } |
319 | | |
320 | | /** |
321 | | * Trims zeroes off the end of the last number in the stringbuffer. |
322 | | * The number has to be the very last thing in the buffer. Only the |
323 | | * last number will be trimmed. Returns the number of characters |
324 | | * trimmed. |
325 | | * |
326 | | * eg: 1.22000 -> 1.22 |
327 | | * 1.0 -> 1 |
328 | | * 0.0 -> 0 |
329 | | */ |
330 | | int |
331 | | stringbuffer_trim_trailing_zeroes(stringbuffer_t *s) |
332 | 0 | { |
333 | 0 | char *ptr = s->str_end; |
334 | 0 | char *decimal_ptr = NULL; |
335 | 0 | int dist; |
336 | |
|
337 | 0 | if ( s->str_end - s->str_start < 2) |
338 | 0 | return 0; |
339 | | |
340 | | /* Roll backwards to find the decimal for this number */ |
341 | 0 | while( ptr > s->str_start ) |
342 | 0 | { |
343 | 0 | ptr--; |
344 | 0 | if ( *ptr == '.' ) |
345 | 0 | { |
346 | 0 | decimal_ptr = ptr; |
347 | 0 | break; |
348 | 0 | } |
349 | 0 | if ( (*ptr >= '0') && (*ptr <= '9' ) ) |
350 | 0 | continue; |
351 | 0 | else |
352 | 0 | break; |
353 | 0 | } |
354 | | |
355 | | /* No decimal? Nothing to trim! */ |
356 | 0 | if ( ! decimal_ptr ) |
357 | 0 | return 0; |
358 | | |
359 | 0 | ptr = s->str_end; |
360 | | |
361 | | /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */ |
362 | 0 | while( ptr >= decimal_ptr ) |
363 | 0 | { |
364 | 0 | ptr--; |
365 | 0 | if ( *ptr == '0' ) |
366 | 0 | continue; |
367 | 0 | else |
368 | 0 | break; |
369 | 0 | } |
370 | | |
371 | | /* Huh, we get anywhere. Must not have trimmed anything. */ |
372 | 0 | if ( ptr == s->str_end ) |
373 | 0 | return 0; |
374 | | |
375 | | /* If we stopped at the decimal, we want to null that out. |
376 | | It we stopped on a numeral, we want to preserve that, so push the |
377 | | pointer forward one space. */ |
378 | 0 | if ( *ptr != '.' ) |
379 | 0 | ptr++; |
380 | | |
381 | | /* Add null terminator re-set the end of the stringbuffer. */ |
382 | 0 | *ptr = '\0'; |
383 | 0 | dist = s->str_end - ptr; |
384 | 0 | s->str_end = ptr; |
385 | 0 | return dist; |
386 | 0 | } |
387 | | |