PostGIS  3.3.9dev-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  va_copy(ap2, ap);
195  len = _vscprintf(fmt, ap2);
196  va_end(ap2);
197  #else
198  return len;
199  #endif
200 
201  /* We didn't have enough space! */
202  /* Either Unix vsnprint returned write length larger than our buffer */
203  /* or Windows vsnprintf returned an error code. */
204  if ( len >= maxlen )
205  {
206  stringbuffer_makeroom(s, len + 1);
207  maxlen = (s->capacity - (s->str_end - s->str_start));
208 
209  /* Try to print a second time */
210  len = vsnprintf(s->str_end, maxlen, fmt, ap);
211 
212  /* Printing error? Error! */
213  if ( len < 0 ) return len;
214  /* Too long still? Error! */
215  if ( len >= maxlen ) return -1;
216  }
217 
218  /* Move end pointer forward and return. */
219  s->str_end += len;
220  return len;
221 }
222 
229 int
231 {
232  int r;
233  va_list ap;
234  va_start(ap, fmt);
235  r = stringbuffer_avprintf(s, fmt, ap);
236  va_end(ap);
237  return r;
238 }
239 
244 int
246 {
247  char *ptr = s->str_end;
248  int dist = 0;
249 
250  /* Roll backwards until we hit a non-space. */
251  while( ptr > s->str_start )
252  {
253  ptr--;
254  if( (*ptr == ' ') || (*ptr == '\t') )
255  {
256  continue;
257  }
258  else
259  {
260  ptr++;
261  dist = s->str_end - ptr;
262  *ptr = '\0';
263  s->str_end = ptr;
264  return dist;
265  }
266  }
267  return dist;
268 }
269 
280 int
282 {
283  char *ptr = s->str_end;
284  char *decimal_ptr = NULL;
285  int dist;
286 
287  if ( s->str_end - s->str_start < 2)
288  return 0;
289 
290  /* Roll backwards to find the decimal for this number */
291  while( ptr > s->str_start )
292  {
293  ptr--;
294  if ( *ptr == '.' )
295  {
296  decimal_ptr = ptr;
297  break;
298  }
299  if ( (*ptr >= '0') && (*ptr <= '9' ) )
300  continue;
301  else
302  break;
303  }
304 
305  /* No decimal? Nothing to trim! */
306  if ( ! decimal_ptr )
307  return 0;
308 
309  ptr = s->str_end;
310 
311  /* Roll backwards again, with the decimal as stop point, trimming contiguous zeroes */
312  while( ptr >= decimal_ptr )
313  {
314  ptr--;
315  if ( *ptr == '0' )
316  continue;
317  else
318  break;
319  }
320 
321  /* Huh, we get anywhere. Must not have trimmed anything. */
322  if ( ptr == s->str_end )
323  return 0;
324 
325  /* If we stopped at the decimal, we want to null that out.
326  It we stopped on a numeral, we want to preserve that, so push the
327  pointer forward one space. */
328  if ( *ptr != '.' )
329  ptr++;
330 
331  /* Add null terminator re-set the end of the stringbuffer. */
332  *ptr = '\0';
333  dist = s->str_end - ptr;
334  s->str_end = ptr;
335  return dist;
336 }
337 
char * s
Definition: cu_in_wkt.c:23
char * r
Definition: cu_in_wkt.c:24
#define LWVARHDRSZ
Definition: liblwgeom.h:326
void lwfree(void *mem)
Definition: lwutil.c:242
#define LWSIZE_SET(varsize, len)
Definition: liblwgeom.h:340
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:230
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:281
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:245
#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:322
char data[]
Definition: liblwgeom.h:323