PostGIS 3.0.6dev-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
64{
66
67 s = lwalloc(sizeof(stringbuffer_t));
69 return s;
70}
71
75void
81
87void
89{
90 s->str_start[0] = '\0';
91 s->str_end = s->str_start;
92}
93
97char
99{
100 if( s->str_end == s->str_start )
101 return 0;
102
103 return *(s->str_end - 1);
104}
105
106
112const char*
114{
115 return s->str_start;
116}
117
123char*
125{
126 size_t size = (s->str_end - s->str_start) + 1;
127 char *str = lwalloc(size);
128 memcpy(str, s->str_start, size);
129 str[size - 1] = '\0';
130 return str;
131}
132
137int
139{
140 return (s->str_end - s->str_start);
141}
142
146void
152
156void
161
167static int
168stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap)
169{
170 int maxlen = (s->capacity - (s->str_end - s->str_start));
171 int len = 0; /* Length of the output */
172 va_list ap2;
173
174 /* Make a copy of the variadic arguments, in case we need to print twice */
175 /* Print to our buffer */
176 va_copy(ap2, ap);
177 len = vsnprintf(s->str_end, maxlen, fmt, ap2);
178 va_end(ap2);
179
180 /* Propogate errors up */
181 if ( len < 0 )
182 #if defined(__MINGW64_VERSION_MAJOR)
183 len = _vscprintf(fmt, ap2);
184 #else
185 return len;
186 #endif
187
188 /* We didn't have enough space! */
189 /* Either Unix vsnprint returned write length larger than our buffer */
190 /* or Windows vsnprintf returned an error code. */
191 if ( len >= maxlen )
192 {
193 stringbuffer_makeroom(s, len + 1);
194 maxlen = (s->capacity - (s->str_end - s->str_start));
195
196 /* Try to print a second time */
197 len = vsnprintf(s->str_end, maxlen, fmt, ap);
198
199 /* Printing error? Error! */
200 if ( len < 0 ) return len;
201 /* Too long still? Error! */
202 if ( len >= maxlen ) return -1;
203 }
204
205 /* Move end pointer forward and return. */
206 s->str_end += len;
207 return len;
208}
209
216int
217stringbuffer_aprintf(stringbuffer_t *s, const char *fmt, ...)
218{
219 int r;
220 va_list ap;
221 va_start(ap, fmt);
222 r = stringbuffer_avprintf(s, fmt, ap);
223 va_end(ap);
224 return r;
225}
226
231int
233{
234 char *ptr = s->str_end;
235 int dist = 0;
236
237 /* Roll backwards until we hit a non-space. */
238 while( ptr > s->str_start )
239 {
240 ptr--;
241 if( (*ptr == ' ') || (*ptr == '\t') )
242 {
243 continue;
244 }
245 else
246 {
247 ptr++;
248 dist = s->str_end - ptr;
249 *ptr = '\0';
250 s->str_end = ptr;
251 return dist;
252 }
253 }
254 return dist;
255}
256
267int
269{
270 char *ptr = s->str_end;
271 char *decimal_ptr = NULL;
272 int dist;
273
274 if ( s->str_end - s->str_start < 2)
275 return 0;
276
277 /* Roll backwards to find the decimal for this number */
278 while( ptr > s->str_start )
279 {
280 ptr--;
281 if ( *ptr == '.' )
282 {
283 decimal_ptr = ptr;
284 break;
285 }
286 if ( (*ptr >= '0') && (*ptr <= '9' ) )
287 continue;
288 else
289 break;
290 }
291
292 /* No decimal? Nothing to trim! */
293 if ( ! decimal_ptr )
294 return 0;
295
296 ptr = s->str_end;
297
298 /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
299 while( ptr >= decimal_ptr )
300 {
301 ptr--;
302 if ( *ptr == '0' )
303 continue;
304 else
305 break;
306 }
307
308 /* Huh, we get anywhere. Must not have trimmed anything. */
309 if ( ptr == s->str_end )
310 return 0;
311
312 /* If we stopped at the decimal, we want to null that out.
313 It we stopped on a numeral, we want to preserve that, so push the
314 pointer forward one space. */
315 if ( *ptr != '.' )
316 ptr++;
317
318 /* Add null terminator re-set the end of the stringbuffer. */
319 *ptr = '\0';
320 dist = s->str_end - ptr;
321 s->str_end = ptr;
322 return dist;
323}
324
char * s
Definition cu_in_wkt.c:23
char * r
Definition cu_in_wkt.c:24
void * lwalloc(size_t size)
Definition lwutil.c:227
void lwfree(void *mem)
Definition lwutil.c:242
#define str(s)
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
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.
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.
void stringbuffer_init(stringbuffer_t *s)
static int stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap)
Appends a formatted string to the current string buffer, using the format and argument list provided.
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.
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_makeroom(stringbuffer_t *s, size_t size_to_add)
If necessary, expand the stringbuffer_t internal buffer to accommodate the specified additional size.