PostGIS  3.4.0dev-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 
59 void
61 {
63  /* Zero out LWVARHDRSZ bytes at the front of the buffer */
64  stringbuffer_append_len(s, "\0\0\0\0\0\0\0\0", LWVARHDRSZ);
65 }
66 
67 
73 {
75 
76  s = lwalloc(sizeof(stringbuffer_t));
78  return s;
79 }
80 
84 void
86 {
88  if ( s ) lwfree(s);
89 }
90 
96 void
98 {
99  s->str_start[0] = '\0';
100  s->str_end = s->str_start;
101 }
102 
106 char
108 {
109  if( s->str_end == s->str_start )
110  return 0;
111 
112  return *(s->str_end - 1);
113 }
114 
115 
121 const char*
123 {
124  return s->str_start;
125 }
126 
132 char*
134 {
135  size_t size = (s->str_end - s->str_start) + 1;
136  char *str = lwalloc(size);
137  memcpy(str, s->str_start, size);
138  str[size - 1] = '\0';
139  return str;
140 }
141 
142 lwvarlena_t *
144 {
145  lwvarlena_t *output = (lwvarlena_t *)(s->str_start);
146  LWSIZE_SET(output->size, (s->str_end - s->str_start));
147  return output;
148 }
149 
150 lwvarlena_t *
152 {
153  size_t size = (s->str_end - s->str_start);
154  lwvarlena_t *output = (lwvarlena_t *)lwalloc(size + LWVARHDRSZ);
155  LWSIZE_SET(output->size, size + LWVARHDRSZ);
156 
157  memcpy(output->data, s->str_start, size);
158  return output;
159 }
160 
165 int
167 {
168  return (s->str_end - s->str_start);
169 }
170 
174 void
176 {
179 }
180 
184 void
186 {
188 }
189 
195 static int
196 stringbuffer_avprintf(stringbuffer_t *s, const char *fmt, va_list ap)
197 {
198  int maxlen = (s->capacity - (s->str_end - s->str_start));
199  int len = 0; /* Length of the output */
200  va_list ap2;
201 
202  /* Make a copy of the variadic arguments, in case we need to print twice */
203  /* Print to our buffer */
204  va_copy(ap2, ap);
205  len = vsnprintf(s->str_end, maxlen, fmt, ap2);
206  va_end(ap2);
207 
208  /* Propogate errors up */
209  if ( len < 0 )
210  #if defined(__MINGW64_VERSION_MAJOR)
211  va_copy(ap2, ap);
212  len = _vscprintf(fmt, ap2);
213  va_end(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 
char * s
Definition: cu_in_wkt.c:23
char * r
Definition: cu_in_wkt.c:24
#define LWVARHDRSZ
Definition: liblwgeom.h:311
void lwfree(void *mem)
Definition: lwutil.c:242
#define LWSIZE_SET(varsize, len)
Definition: liblwgeom.h:325
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:72
void stringbuffer_release(stringbuffer_t *s)
Definition: stringbuffer.c:48
void stringbuffer_clear(stringbuffer_t *s)
Reset the stringbuffer_t.
Definition: stringbuffer.c:97
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
lwvarlena_t * stringbuffer_getvarlenacopy(stringbuffer_t *s)
Definition: stringbuffer.c:151
char stringbuffer_lastchar(stringbuffer_t *s)
Return the last character in the buffer.
Definition: stringbuffer.c:107
void stringbuffer_set(stringbuffer_t *s, const char *str)
Clear the stringbuffer_t and re-start it with the specified string.
Definition: stringbuffer.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
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:166
void stringbuffer_destroy(stringbuffer_t *s)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:85
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:133
lwvarlena_t * stringbuffer_getvarlena(stringbuffer_t *s)
Definition: stringbuffer.c:143
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:196
void stringbuffer_copy(stringbuffer_t *dst, stringbuffer_t *src)
Copy the contents of src into dst.
Definition: stringbuffer.c:185
const char * stringbuffer_getstring(stringbuffer_t *s)
Returns a reference to the internal string being managed by the stringbuffer.
Definition: stringbuffer.c:122
void stringbuffer_init_varlena(stringbuffer_t *s)
Definition: stringbuffer.c:60
int stringbuffer_trim_trailing_white(stringbuffer_t *s)
Trims whitespace off the end of the stringbuffer.
Definition: stringbuffer.c:262
#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:105
static void stringbuffer_append_len(stringbuffer_t *s, const char *a, size_t alen)
Append the specified string to the stringbuffer_t using known length.
Definition: stringbuffer.h:93
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:71
uint32_t size
Definition: liblwgeom.h:307
char data[]
Definition: liblwgeom.h:308