PostGIS  3.0.6dev-r@@SVN_REVISION@@
stringbuffer.h
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 
27 #ifndef _STRINGBUFFER_H
28 #define _STRINGBUFFER_H 1
29 
30 #include "liblwgeom_internal.h"
31 
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <stdio.h>
36 
37 #define STRINGBUFFER_STARTSIZE 128
38 
39 typedef struct
40 {
41  size_t capacity;
42  char *str_end;
43  char *str_start;
44 }
46 
49 extern void stringbuffer_init(stringbuffer_t *s);
51 extern void stringbuffer_destroy(stringbuffer_t *sb);
52 extern void stringbuffer_clear(stringbuffer_t *sb);
53 void stringbuffer_set(stringbuffer_t *sb, const char *s);
55 extern int stringbuffer_aprintf(stringbuffer_t *sb, const char *fmt, ...);
56 extern const char *stringbuffer_getstring(stringbuffer_t *sb);
62 
67 static inline void
68 stringbuffer_makeroom(stringbuffer_t *s, size_t size_to_add)
69 {
70  size_t current_size = (s->str_end - s->str_start);
71  size_t capacity = s->capacity;
72  size_t required_size = current_size + size_to_add;
73 
74  while (capacity < required_size)
75  capacity *= 2;
76 
77  if (capacity > s->capacity)
78  {
79  s->str_start = lwrealloc(s->str_start, capacity);
80  s->capacity = capacity;
81  s->str_end = s->str_start + current_size;
82  }
83 }
87 inline static void
89 {
90  int alen = strlen(a); /* Length of string to append */
91  int alen0 = alen + 1; /* Length including null terminator */
92  stringbuffer_makeroom(s, alen0);
93  memcpy(s->str_end, a, alen0);
94  s->str_end += alen;
95 }
96 #endif /* _STRINGBUFFER_H */
char * s
Definition: cu_in_wkt.c:23
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
def fmt
Definition: pixval.py:93
int stringbuffer_getlength(stringbuffer_t *sb)
Returns the length of the current string, not including the null terminator (same behavior as strlen(...
Definition: stringbuffer.c:138
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
static void stringbuffer_append(stringbuffer_t *s, const char *a)
Append the specified string to the stringbuffer_t.
Definition: stringbuffer.h:88
int stringbuffer_aprintf(stringbuffer_t *sb, 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
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
const char * stringbuffer_getstring(stringbuffer_t *sb)
Returns a reference to the internal string being managed by the stringbuffer.
Definition: stringbuffer.c:113
void stringbuffer_destroy(stringbuffer_t *sb)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:76
void stringbuffer_init(stringbuffer_t *s)
Definition: stringbuffer.c:54
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
void stringbuffer_copy(stringbuffer_t *sb, stringbuffer_t *src)
Copy the contents of src into dst.
Definition: stringbuffer.c:157
void stringbuffer_set(stringbuffer_t *sb, const char *s)
Clear the stringbuffer_t and re-start it with the specified string.
Definition: stringbuffer.c:147
char * stringbuffer_getstringcopy(stringbuffer_t *sb)
Returns a newly allocated string large enough to contain the current state of the string.
Definition: stringbuffer.c:124
void stringbuffer_clear(stringbuffer_t *sb)
Reset the stringbuffer_t.
Definition: stringbuffer.c:88
int stringbuffer_trim_trailing_white(stringbuffer_t *s)
Trims whitespace off the end of the stringbuffer.
Definition: stringbuffer.c:232
char * str_start
Definition: stringbuffer.h:43