PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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
39typedef struct
40{
41 size_t capacity;
42 char *str_end;
43 char *str_start;
44}
46
53extern void stringbuffer_clear(stringbuffer_t *sb);
54void stringbuffer_set(stringbuffer_t *sb, const char *s);
56extern int stringbuffer_aprintf(stringbuffer_t *sb, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
57extern const char *stringbuffer_getstring(stringbuffer_t *sb);
65
70static inline void
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
92inline static void
93stringbuffer_append_len(stringbuffer_t *s, const char *a, size_t alen)
94{
95 int alen0 = alen + 1; /* Length including null terminator */
97 memcpy(s->str_end, a, alen0);
98 s->str_end += alen;
99}
100
104inline static void
106{
107 int alen = strlen(a); /* Length of string to append */
108 stringbuffer_append_len(s, a, alen);
109}
110
111inline static void
117
118inline 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:242
#define __attribute__(x)
Definition liblwgeom.h:228
#define OUT_MAX_BYTES_DOUBLE
int lwprint_double(double d, int maxdd, char *buf)
Definition lwprint.c:463
int stringbuffer_getlength(stringbuffer_t *sb)
Returns the length of the current string, not including the null terminator (same behavior as strlen(...
static void stringbuffer_append_double(stringbuffer_t *s, double d, int precision)
stringbuffer_t * stringbuffer_create(void)
Allocate a new stringbuffer_t.
lwvarlena_t * stringbuffer_getvarlena(stringbuffer_t *s)
int stringbuffer_aprintf(stringbuffer_t *sb, const char *fmt,...) __attribute__((format(printf
void stringbuffer_release(stringbuffer_t *s)
static void stringbuffer_append(stringbuffer_t *s, const char *a)
Append the specified string to the stringbuffer_t.
static void stringbuffer_append_char(stringbuffer_t *s, char c)
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.
char stringbuffer_lastchar(stringbuffer_t *s)
Return the last character in the buffer.
int stringbuffer_trim_trailing_zeroes(stringbuffer_t *s)
Trims zeroes off the end of the last number in the stringbuffer.
lwvarlena_t * stringbuffer_getvarlenacopy(stringbuffer_t *s)
void stringbuffer_destroy(stringbuffer_t *sb)
Free the stringbuffer_t and all memory managed within it.
void stringbuffer_init(stringbuffer_t *s)
int const char * stringbuffer_getstring(stringbuffer_t *sb)
Returns a reference to the internal string being managed by the stringbuffer.
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.
void stringbuffer_copy(stringbuffer_t *sb, stringbuffer_t *src)
Copy the contents of src into dst.
void stringbuffer_set(stringbuffer_t *sb, const char *s)
Clear the stringbuffer_t and re-start it with the specified string.
stringbuffer_t * stringbuffer_create_with_size(size_t size)
Allocate a new stringbuffer_t.
void stringbuffer_clear(stringbuffer_t *sb)
Reset the stringbuffer_t.
void stringbuffer_init_varlena(stringbuffer_t *s)
int stringbuffer_trim_trailing_white(stringbuffer_t *s)
Trims whitespace off the end of the stringbuffer.
char * stringbuffer_getstringcopy(stringbuffer_t *sb)
Returns a newly allocated string large enough to contain the current state of the string.