PostGIS  3.4.0dev-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);
52 extern void stringbuffer_destroy(stringbuffer_t *sb);
53 extern void stringbuffer_clear(stringbuffer_t *sb);
54 void stringbuffer_set(stringbuffer_t *sb, const char *s);
56 extern int stringbuffer_aprintf(stringbuffer_t *sb, const char *fmt, ...);
57 extern const char *stringbuffer_getstring(stringbuffer_t *sb);
65 
70 static inline void
71 stringbuffer_makeroom(stringbuffer_t *s, size_t size_to_add)
72 {
73  size_t current_size = (s->str_end - s->str_start);
74  size_t capacity = s->capacity;
75  size_t required_size = current_size + size_to_add;
76 
77  while (capacity < required_size)
78  capacity *= 2;
79 
80  if (capacity > s->capacity)
81  {
82  s->str_start = lwrealloc(s->str_start, capacity);
83  s->capacity = capacity;
84  s->str_end = s->str_start + current_size;
85  }
86 }
87 
88 
92 inline static void
93 stringbuffer_append_len(stringbuffer_t *s, const char *a, size_t alen)
94 {
95  int alen0 = alen + 1; /* Length including null terminator */
96  stringbuffer_makeroom(s, alen0);
97  memcpy(s->str_end, a, alen0);
98  s->str_end += alen;
99 }
100 
104 inline static void
106 {
107  int alen = strlen(a); /* Length of string to append */
108  stringbuffer_append_len(s, a, alen);
109 }
110 
111 inline static void
113 {
115  s->str_end += lwprint_double(d, precision, s->str_end);
116 }
117 
118 inline static void
120 {
122  *(s->str_end) = c;
123  s->str_end++;
124 }
125 
126 
127 #endif /* _STRINGBUFFER_H */
static uint8_t precision
Definition: cu_in_twkb.c:25
char * s
Definition: cu_in_wkt.c:23
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
#define OUT_MAX_BYTES_DOUBLE
int lwprint_double(double d, int maxdd, char *buf)
Definition: lwprint.c:457
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:166
static void stringbuffer_append_double(stringbuffer_t *s, double d, int precision)
Definition: stringbuffer.h:112
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
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_char(stringbuffer_t *s, char c)
Definition: stringbuffer.h:119
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
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: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
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
const char * stringbuffer_getstring(stringbuffer_t *sb)
Returns a reference to the internal string being managed by the stringbuffer.
Definition: stringbuffer.c:122
void stringbuffer_destroy(stringbuffer_t *sb)
Free the stringbuffer_t and all memory managed within it.
Definition: stringbuffer.c:85
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:71
void stringbuffer_copy(stringbuffer_t *sb, stringbuffer_t *src)
Copy the contents of src into dst.
Definition: stringbuffer.c:185
lwvarlena_t * stringbuffer_getvarlena(stringbuffer_t *s)
Definition: stringbuffer.c:143
void stringbuffer_set(stringbuffer_t *sb, const char *s)
Clear the stringbuffer_t and re-start it with the specified string.
Definition: stringbuffer.c:175
char * stringbuffer_getstringcopy(stringbuffer_t *sb)
Returns a newly allocated string large enough to contain the current state of the string.
Definition: stringbuffer.c:133
void stringbuffer_clear(stringbuffer_t *sb)
Reset the stringbuffer_t.
Definition: stringbuffer.c:97
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
char * str_start
Definition: stringbuffer.h:43