PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
stringbuffer.c
Go to the documentation of this file.
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
37
38static void
40{
41 s->str_start = lwalloc(size);
42 s->str_end = s->str_start;
43 s->capacity = size;
44 memset(s->str_start, 0, size);
45}
46
47void
49{
50 if ( s->str_start ) lwfree(s->str_start);
51}
52
53void
58
59void
61{
63 /* Zero out LWVARHDRSZ bytes at the front of the buffer */
64 stringbuffer_append_len(s, "\0\0\0\0\0\0\0\0", LWVARHDRSZ);
65}
66
67
73{
75
76 s = lwalloc(sizeof(stringbuffer_t));
78 return s;
79}
80
84void
86{
87 if ( s )
88 {
90 lwfree(s);
91 }
92}
93
99void
101{
102 s->str_start[0] = '\0';
103 s->str_end = s->str_start;
104}
105
109char
111{
112 if( s->str_end == s->str_start )
113 return 0;
114
115 return *(s->str_end - 1);
116}
117
118
124const char*
126{
127 return s->str_start;
128}
129
135char*
137{
138 size_t size = (s->str_end - s->str_start) + 1;
139 char *str = lwalloc(size);
140 memcpy(str, s->str_start, size);
141 str[size - 1] = '\0';
142 return str;
143}
144
147{
148 lwvarlena_t *output = (lwvarlena_t *)(s->str_start);
149 LWSIZE_SET(output->size, (s->str_end - s->str_start));
150 return output;
151}
152
155{
156 size_t size = (s->str_end - s->str_start);
157 lwvarlena_t *output = (lwvarlena_t *)lwalloc(size + LWVARHDRSZ);
158 LWSIZE_SET(output->size, size + LWVARHDRSZ);
159
160 memcpy(output->data, s->str_start, size);
161 return output;
162}
163
168int
170{
171 return (s->str_end - s->str_start);
172}
173
177void
183
187void
192
198static int
199stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap) __attribute__ ((format (printf, 2, 0)));
200static int
201stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap)
202{
203 int maxlen = (s->capacity - (s->str_end - s->str_start));
204 int len = 0; /* Length of the output */
205 va_list ap2;
206
207 /* Make a copy of the variadic arguments, in case we need to print twice */
208 /* Print to our buffer */
209 va_copy(ap2, ap);
210 len = vsnprintf(s->str_end, maxlen, fmt, ap2);
211 va_end(ap2);
212
213 /* Propagate errors up */
214 if ( len < 0 )
215 #if defined(__MINGW64_VERSION_MAJOR)
216 {
217 va_copy(ap2, ap);
218 len = _vscprintf(fmt, ap2);
219 va_end(ap2);
220 }
221 #else
222 return len;
223 #endif
224
225 /* We didn't have enough space! */
226 /* Either Unix vsnprint returned write length larger than our buffer */
227 /* or Windows vsnprintf returned an error code. */
228 if ( len >= maxlen )
229 {
230 stringbuffer_makeroom(s, len + 1);
231 maxlen = (s->capacity - (s->str_end - s->str_start));
232
233 /* Try to print a second time */
234 len = vsnprintf(s->str_end, maxlen, fmt, ap);
235
236 /* Printing error? Error! */
237 if ( len < 0 ) return len;
238 /* Too long still? Error! */
239 if ( len >= maxlen ) return -1;
240 }
241
242 /* Move end pointer forward and return. */
243 s->str_end += len;
244 return len;
245}
246
253int
254stringbuffer_aprintf(stringbuffer_t *s, const char *fmt, ...)
255{
256 int r;
257 va_list ap;
258 va_start(ap, fmt);
259 r = stringbuffer_avprintf(s, fmt, ap);
260 va_end(ap);
261 return r;
262}
263
268int
270{
271 char *ptr = s->str_end;
272 int dist = 0;
273
274 /* Roll backwards until we hit a non-space. */
275 while( ptr > s->str_start )
276 {
277 ptr--;
278 if( (*ptr == ' ') || (*ptr == '\t') )
279 {
280 continue;
281 }
282 else
283 {
284 ptr++;
285 dist = s->str_end - ptr;
286 *ptr = '\0';
287 s->str_end = ptr;
288 return dist;
289 }
290 }
291 return dist;
292}
293
304int
306{
307 char *ptr = s->str_end;
308 char *decimal_ptr = NULL;
309 int dist;
310
311 if ( s->str_end - s->str_start < 2)
312 return 0;
313
314 /* Roll backwards to find the decimal for this number */
315 while( ptr > s->str_start )
316 {
317 ptr--;
318 if ( *ptr == '.' )
319 {
320 decimal_ptr = ptr;
321 break;
322 }
323 if ( (*ptr >= '0') && (*ptr <= '9' ) )
324 continue;
325 else
326 break;
327 }
328
329 /* No decimal? Nothing to trim! */
330 if ( ! decimal_ptr )
331 return 0;
332
333 ptr = s->str_end;
334
335 /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
336 while( ptr >= decimal_ptr )
337 {
338 ptr--;
339 if ( *ptr == '0' )
340 continue;
341 else
342 break;
343 }
344
345 /* Huh, we get anywhere. Must not have trimmed anything. */
346 if ( ptr == s->str_end )
347 return 0;
348
349 /* If we stopped at the decimal, we want to null that out.
350 It we stopped on a numeral, we want to preserve that, so push the
351 pointer forward one space. */
352 if ( *ptr != '.' )
353 ptr++;
354
355 /* Add null terminator re-set the end of the stringbuffer. */
356 *ptr = '\0';
357 dist = s->str_end - ptr;
358 s->str_end = ptr;
359 return dist;
360}
361
char * s
Definition cu_in_wkt.c:23
char * r
Definition cu_in_wkt.c:24
#define LWVARHDRSZ
Definition liblwgeom.h:311
void * lwalloc(size_t size)
Definition lwutil.c:227
void lwfree(void *mem)
Definition lwutil.c:248
#define __attribute__(x)
Definition liblwgeom.h:228
#define LWSIZE_SET(varsize, len)
Definition liblwgeom.h:325
#define str(s)
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
lwvarlena_t * stringbuffer_getvarlena(stringbuffer_t *s)
void stringbuffer_release(stringbuffer_t *s)
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
int stringbuffer_aprintf(stringbuffer_t *s, const char *fmt,...)
Appends a formatted string to the current string buffer, using the format and argument list provided.
char stringbuffer_lastchar(stringbuffer_t *s)
Return the last character in the buffer.
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
void stringbuffer_set(stringbuffer_t *s, const char *str)
Clear the stringbuffer_t and re-start it with the specified string.
int stringbuffer_trim_trailing_zeroes(stringbuffer_t *s)
Trims zeroes off the end of the last number in the stringbuffer.
char * stringbuffer_getstringcopy(stringbuffer_t *s)
Returns a newly allocated string large enough to contain the current state of the string.
lwvarlena_t * stringbuffer_getvarlenacopy(stringbuffer_t *s)
static void stringbuffer_init_with_size(stringbuffer_t *s, size_t size)
int stringbuffer_getlength(stringbuffer_t *s)
Returns the length of the current string, not including the null terminator (same behavior as strlen(...
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
static int stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap) __attribute__((format(printf
Appends a formatted string to the current string buffer, using the format and argument list provided.
void stringbuffer_init(stringbuffer_t *s)
stringbuffer_t * stringbuffer_create_with_size(size_t size)
Allocate a new stringbuffer_t.
void stringbuffer_copy(stringbuffer_t *dst, stringbuffer_t *src)
Copy the contents of src into dst.
void stringbuffer_init_varlena(stringbuffer_t *s)
int stringbuffer_trim_trailing_white(stringbuffer_t *s)
Trims whitespace off the end of the stringbuffer.
#define STRINGBUFFER_STARTSIZE
static void stringbuffer_append(stringbuffer_t *s, const char *a)
Append the specified string to the stringbuffer_t.
static void stringbuffer_append_len(stringbuffer_t *s, const char *a, size_t alen)
Append the specified string to the stringbuffer_t using known length.
static void stringbuffer_makeroom(stringbuffer_t *s, size_t size_to_add)
If necessary, expand the stringbuffer_t internal buffer to accommodate the specified additional size.
uint32_t size
Definition liblwgeom.h:307
char data[]
Definition liblwgeom.h:308