PostGIS  3.1.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 
133 lwvarlena_t *
135 {
136  size_t size = (s->str_end - s->str_start);
137  lwvarlena_t *output = (lwvarlena_t *)lwalloc(size + LWVARHDRSZ);
138  LWSIZE_SET(output->size, size + LWVARHDRSZ);
139 
140  memcpy(output->data, s->str_start, size);
141  return output;
142 }
143 
148 int
150 {
151  return (s->str_end - s->str_start);
152 }
153 
157 void
159 {
162 }
163 
167 void
169 {
171 }
172 
178 static int
179 stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap)
180 {
181  int maxlen = (s->capacity - (s->str_end - s->str_start));
182  int len = 0; /* Length of the output */
183  va_list ap2;
184 
185  /* Make a copy of the variadic arguments, in case we need to print twice */
186  /* Print to our buffer */
187  va_copy(ap2, ap);
188  len = vsnprintf(s->str_end, maxlen, fmt, ap2);
189  va_end(ap2);
190 
191  /* Propogate errors up */
192  if ( len < 0 )
193  #if defined(__MINGW64_VERSION_MAJOR)
194  len = _vscprintf(fmt, ap2);
195  #else
196  return len;
197  #endif
198 
199  /* We didn't have enough space! */
200  /* Either Unix vsnprint returned write length larger than our buffer */
201  /* or Windows vsnprintf returned an error code. */
202  if ( len >= maxlen )
203  {
204  stringbuffer_makeroom(s, len + 1);
205  maxlen = (s->capacity - (s->str_end - s->str_start));
206 
207  /* Try to print a second time */
208  len = vsnprintf(s->str_end, maxlen, fmt, ap);
209 
210  /* Printing error? Error! */
211  if ( len < 0 ) return len;
212  /* Too long still? Error! */
213  if ( len >= maxlen ) return -1;
214  }
215 
216  /* Move end pointer forward and return. */
217  s->str_end += len;
218  return len;
219 }
220 
227 int
229 {
230  int r;
231  va_list ap;
232  va_start(ap, fmt);
233  r = stringbuffer_avprintf(s, fmt, ap);
234  va_end(ap);
235  return r;
236 }
237 
242 int
244 {
245  char *ptr = s->str_end;
246  int dist = 0;
247 
248  /* Roll backwards until we hit a non-space. */
249  while( ptr > s->str_start )
250  {
251  ptr--;
252  if( (*ptr == ' ') || (*ptr == '\t') )
253  {
254  continue;
255  }
256  else
257  {
258  ptr++;
259  dist = s->str_end - ptr;
260  *ptr = '\0';
261  s->str_end = ptr;
262  return dist;
263  }
264  }
265  return dist;
266 }
267 
278 int
280 {
281  char *ptr = s->str_end;
282  char *decimal_ptr = NULL;
283  int dist;
284 
285  if ( s->str_end - s->str_start < 2)
286  return 0;
287 
288  /* Roll backwards to find the decimal for this number */
289  while( ptr > s->str_start )
290  {
291  ptr--;
292  if ( *ptr == '.' )
293  {
294  decimal_ptr = ptr;
295  break;
296  }
297  if ( (*ptr >= '0') && (*ptr <= '9' ) )
298  continue;
299  else
300  break;
301  }
302 
303  /* No decimal? Nothing to trim! */
304  if ( ! decimal_ptr )
305  return 0;
306 
307  ptr = s->str_end;
308 
309  /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
310  while( ptr >= decimal_ptr )
311  {
312  ptr--;
313  if ( *ptr == '0' )
314  continue;
315  else
316  break;
317  }
318 
319  /* Huh, we get anywhere. Must not have trimmed anything. */
320  if ( ptr == s->str_end )
321  return 0;
322 
323  /* If we stopped at the decimal, we want to null that out.
324  It we stopped on a numeral, we want to preserve that, so push the
325  pointer forward one space. */
326  if ( *ptr != '.' )
327  ptr++;
328 
329  /* Add null terminator re-set the end of the stringbuffer. */
330  *ptr = '\0';
331  dist = s->str_end - ptr;
332  s->str_end = ptr;
333  return dist;
334 }
335 
char * s
Definition: cu_in_wkt.c:23
char * r
Definition: cu_in_wkt.c:24
#define LWVARHDRSZ
Definition: liblwgeom.h:325
void lwfree(void *mem)
Definition: lwutil.c:242
#define LWSIZE_SET(varsize, len)
Definition: liblwgeom.h:339
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:228
lwvarlena_t * stringbuffer_getvarlenacopy(stringbuffer_t *s)
Definition: stringbuffer.c:134
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:158
int stringbuffer_trim_trailing_zeroes(stringbuffer_t *s)
Trims zeroes off the end of the last number in the stringbuffer.
Definition: stringbuffer.c:279
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:149
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:179
void stringbuffer_copy(stringbuffer_t *dst, stringbuffer_t *src)
Copy the contents of src into dst.
Definition: stringbuffer.c:168
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:243
#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:103
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:69
uint32_t size
Definition: liblwgeom.h:321
char data[]
Definition: liblwgeom.h:322