PostGIS  3.0.6dev-r@@SVN_REVISION@@
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 
34 {
36 }
37 
38 static 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 
47 void
49 {
50  if ( s->str_start ) lwfree(s->str_start);
51 }
52 
53 void
55 {
57 }
58 
64 {
66 
67  s = lwalloc(sizeof(stringbuffer_t));
69  return s;
70 }
71 
75 void
77 {
79  if ( s ) lwfree(s);
80 }
81 
87 void
89 {
90  s->str_start[0] = '\0';
91  s->str_end = s->str_start;
92 }
93 
97 char
99 {
100  if( s->str_end == s->str_start )
101  return 0;
102 
103  return *(s->str_end - 1);
104 }
105 
106 
112 const char*
114 {
115  return s->str_start;
116 }
117 
123 char*
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 
137 int
139 {
140  return (s->str_end - s->str_start);
141 }
142 
146 void
148 {
151 }
152 
156 void
158 {
160 }
161 
167 static int
168 stringbuffer_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 
216 int
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 
231 int
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 
267 int
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 lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define str(s)
def fmt
Definition: pixval.py:93
stringbuffer_t * stringbuffer_create_with_size(size_t size)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:63
void stringbuffer_release(stringbuffer_t *s)
Definition: stringbuffer.c:48
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
Definition: stringbuffer.c:88
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:217
char stringbuffer_lastchar(stringbuffer_t *s)
Return the last character in the buffer.
Definition: stringbuffer.c:98
void stringbuffer_set(stringbuffer_t *s, const char *str)
Clear the stringbuffer_t and re-start it with the specified string.
Definition: stringbuffer.c:147
int stringbuffer_trim_trailing_zeroes(stringbuffer_t *s)
Trims zeroes off the end of the last number in the stringbuffer.
Definition: stringbuffer.c:268
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
Definition: stringbuffer.c:33
static void stringbuffer_init_with_size(stringbuffer_t *s, size_t size)
Definition: stringbuffer.c:39
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:138
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:76
void stringbuffer_init(stringbuffer_t *s)
Definition: stringbuffer.c:54
char * stringbuffer_getstringcopy(stringbuffer_t *s)
Returns a newly allocated string large enough to contain the current state of the string.
Definition: stringbuffer.c:124
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:168
void stringbuffer_copy(stringbuffer_t *dst, stringbuffer_t *src)
Copy the contents of src into dst.
Definition: stringbuffer.c:157
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
Definition: stringbuffer.c:113
int stringbuffer_trim_trailing_white(stringbuffer_t *s)
Trims whitespace off the end of the stringbuffer.
Definition: stringbuffer.c:232
#define STRINGBUFFER_STARTSIZE
Definition: stringbuffer.h:37
static void stringbuffer_append(stringbuffer_t *s, const char *a)
Append the specified string to the stringbuffer_t.
Definition: stringbuffer.h:88
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.
Definition: stringbuffer.h:68