PostGIS  2.1.10dev-r@@SVN_REVISION@@
stringbuffer.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: stringbuffer.c 11218 2013-03-28 13:32:44Z robe $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * Copyright 2002 Thamer Alharbash
6  * Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
7  *
8  * Redistribution and use in source and binary forms, with or
9  * without modification, are permitted provided that the following
10  * conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * The name of the author may not be used to endorse or promote
21  * products derived from this software without specific prior
22  * written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
25  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
28  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35  * THE POSSIBILITY OF SUCH DAMAGE.
36  *
37  **********************************************************************/
38 
39 
40 #include "liblwgeom_internal.h"
41 #include "stringbuffer.h"
42 
48 {
50 }
51 
57 {
59 
60  s = lwalloc(sizeof(stringbuffer_t));
61  s->str_start = lwalloc(size);
62  s->str_end = s->str_start;
63  s->capacity = size;
64  memset(s->str_start,0,size);
65  return s;
66 }
67 
71 void
73 {
74  if ( s->str_start ) lwfree(s->str_start);
75  if ( s ) lwfree(s);
76 }
77 
83 void
85 {
86  s->str_start[0] = '\0';
87  s->str_end = s->str_start;
88 }
89 
94 static inline void
95 stringbuffer_makeroom(stringbuffer_t *s, size_t size_to_add)
96 {
97  size_t current_size = (s->str_end - s->str_start);
98  size_t capacity = s->capacity;
99  size_t required_size = current_size + size_to_add;
100 
101  while (capacity < required_size)
102  capacity *= 2;
103 
104  if ( capacity > s->capacity )
105  {
106  s->str_start = lwrealloc(s->str_start, capacity);
107  s->capacity = capacity;
108  s->str_end = s->str_start + current_size;
109  }
110 }
111 
115 char
117 {
118  if( s->str_end == s->str_start )
119  return 0;
120 
121  return *(s->str_end - 1);
122 }
123 
127 void
129 {
130  int alen = strlen(a); /* Length of string to append */
131  int alen0 = alen + 1; /* Length including null terminator */
132  stringbuffer_makeroom(s, alen0);
133  memcpy(s->str_end, a, alen0);
134  s->str_end += alen;
135 }
136 
142 const char*
144 {
145  return s->str_start;
146 }
147 
153 char*
155 {
156  size_t size = (s->str_end - s->str_start) + 1;
157  char *str = lwalloc(size);
158  memcpy(str, s->str_start, size);
159  str[size - 1] = '\0';
160  return str;
161 }
162 
167 int
169 {
170  return (s->str_end - s->str_start);
171 }
172 
176 void
178 {
180  stringbuffer_append(s, str);
181 }
182 
186 void
188 {
190 }
191 
197 static int
198 stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap)
199 {
200  int maxlen = (s->capacity - (s->str_end - s->str_start));
201  int len = 0; /* Length of the output */
202  va_list ap2;
203 
204  /* Make a copy of the variadic arguments, in case we need to print twice */
205  /* Print to our buffer */
206  va_copy(ap2, ap);
207  len = vsnprintf(s->str_end, maxlen, fmt, ap2);
208  va_end(ap2);
209 
210  /* Propogate errors up */
211  if ( len < 0 )
212  #if defined(__MINGW64_VERSION_MAJOR)
213  len = _vscprintf(fmt, ap2);
214  #else
215  return len;
216  #endif
217 
218  /* We didn't have enough space! */
219  /* Either Unix vsnprint returned write length larger than our buffer */
220  /* or Windows vsnprintf returned an error code. */
221  if ( len >= maxlen )
222  {
223  stringbuffer_makeroom(s, len + 1);
224  maxlen = (s->capacity - (s->str_end - s->str_start));
225 
226  /* Try to print a second time */
227  len = vsnprintf(s->str_end, maxlen, fmt, ap);
228 
229  /* Printing error? Error! */
230  if ( len < 0 ) return len;
231  /* Too long still? Error! */
232  if ( len >= maxlen ) return -1;
233  }
234 
235  /* Move end pointer forward and return. */
236  s->str_end += len;
237  return len;
238 }
239 
246 int
248 {
249  int r;
250  va_list ap;
251  va_start(ap, fmt);
252  r = stringbuffer_avprintf(s, fmt, ap);
253  va_end(ap);
254  return r;
255 }
256 
261 int
263 {
264  char *ptr = s->str_end;
265  int dist = 0;
266 
267  /* Roll backwards until we hit a non-space. */
268  while( ptr > s->str_start )
269  {
270  ptr--;
271  if( (*ptr == ' ') || (*ptr == '\t') )
272  {
273  continue;
274  }
275  else
276  {
277  ptr++;
278  dist = s->str_end - ptr;
279  *ptr = '\0';
280  s->str_end = ptr;
281  return dist;
282  }
283  }
284  return dist;
285 }
286 
297 int
299 {
300  char *ptr = s->str_end;
301  char *decimal_ptr = NULL;
302  int dist;
303 
304  if ( s->str_end - s->str_start < 2)
305  return 0;
306 
307  /* Roll backwards to find the decimal for this number */
308  while( ptr > s->str_start )
309  {
310  ptr--;
311  if ( *ptr == '.' )
312  {
313  decimal_ptr = ptr;
314  break;
315  }
316  if ( (*ptr >= '0') && (*ptr <= '9' ) )
317  continue;
318  else
319  break;
320  }
321 
322  /* No decimal? Nothing to trim! */
323  if ( ! decimal_ptr )
324  return 0;
325 
326  ptr = s->str_end;
327 
328  /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
329  while( ptr >= decimal_ptr )
330  {
331  ptr--;
332  if ( *ptr == '0' )
333  continue;
334  else
335  break;
336  }
337 
338  /* Huh, we get anywhere. Must not have trimmed anything. */
339  if ( ptr == s->str_end )
340  return 0;
341 
342  /* If we stopped at the decimal, we want to null that out.
343  It we stopped on a numeral, we want to preserve that, so push the
344  pointer forward one space. */
345  if ( *ptr != '.' )
346  ptr++;
347 
348  /* Add null terminator re-set the end of the stringbuffer. */
349  *ptr = '\0';
350  dist = s->str_end - ptr;
351  s->str_end = ptr;
352  return dist;
353 }
354 
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:47
char * r
Definition: cu_in_wkt.c:25
void lwfree(void *mem)
Definition: lwutil.c:190
void stringbuffer_set(stringbuffer_t *s, const char *str)
Clear the stringbuffer_t and re-start it with the specified string.
Definition: stringbuffer.c:177
char * stringbuffer_getstringcopy(stringbuffer_t *s)
Returns a newly allocated string large enough to contain the current state of the string...
Definition: stringbuffer.c:154
tuple fmt
Definition: pixval.py:92
char * str_start
Definition: stringbuffer.h:53
stringbuffer_t * stringbuffer_create_with_size(size_t size)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:56
int stringbuffer_trim_trailing_white(stringbuffer_t *s)
Trims whitespace off the end of the stringbuffer.
Definition: stringbuffer.c:262
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...
Definition: stringbuffer.c:247
int stringbuffer_getlength(stringbuffer_t *s)
Returns the length of the current string, not including the null terminator (same behavior as strlen(...
Definition: stringbuffer.c:168
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
Definition: stringbuffer.c:84
char * s
Definition: cu_in_wkt.c:24
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...
Definition: stringbuffer.c:198
void stringbuffer_append(stringbuffer_t *s, const char *a)
Append the specified string to the stringbuffer_t.
Definition: stringbuffer.c:128
#define STRINGBUFFER_STARTSIZE
Definition: stringbuffer.h:47
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:72
static void stringbuffer_makeroom(stringbuffer_t *s, size_t size_to_add)
If necessary, expand the stringbuffer_t internal buffer to accomodate the specified additional size...
Definition: stringbuffer.c:95
void stringbuffer_copy(stringbuffer_t *dst, stringbuffer_t *src)
Copy the contents of src into dst.
Definition: stringbuffer.c:187
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:183
#define va_copy(d, s)
Definition: vsprintf.c:42
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
Definition: stringbuffer.c:143
char stringbuffer_lastchar(stringbuffer_t *s)
Return the last character in the buffer.
Definition: stringbuffer.c:116
void * lwalloc(size_t size)
Definition: lwutil.c:175
int stringbuffer_trim_trailing_zeroes(stringbuffer_t *s)
Trims zeroes off the end of the last number in the stringbuffer.
Definition: stringbuffer.c:298