/src/postgis/liblwgeom/stringbuffer.h
Line | Count | Source |
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 | 0 | #define STRINGBUFFER_STARTSIZE 128 |
38 | | |
39 | | typedef struct |
40 | | { |
41 | | size_t capacity; |
42 | | char *str_end; |
43 | | char *str_start; |
44 | | } |
45 | | stringbuffer_t; |
46 | | |
47 | | extern stringbuffer_t *stringbuffer_create_with_size(size_t size); |
48 | | extern stringbuffer_t *stringbuffer_create(void); |
49 | | extern void stringbuffer_init(stringbuffer_t *s); |
50 | | extern void stringbuffer_init_varlena(stringbuffer_t *s); |
51 | | extern void stringbuffer_release(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); |
55 | | void stringbuffer_copy(stringbuffer_t *sb, stringbuffer_t *src); |
56 | | extern int stringbuffer_aprintf(stringbuffer_t *sb, const char *fmt, ...) __attribute__ ((format (printf, 2, 3))); |
57 | | extern const char *stringbuffer_getstring(stringbuffer_t *sb); |
58 | | extern char *stringbuffer_getstringcopy(stringbuffer_t *sb); |
59 | | extern lwvarlena_t *stringbuffer_getvarlenacopy(stringbuffer_t *s); |
60 | | extern lwvarlena_t * stringbuffer_getvarlena(stringbuffer_t *s); |
61 | | extern int stringbuffer_getlength(stringbuffer_t *sb); |
62 | | extern char stringbuffer_lastchar(stringbuffer_t *s); |
63 | | extern int stringbuffer_trim_trailing_white(stringbuffer_t *s); |
64 | | extern int stringbuffer_trim_trailing_zeroes(stringbuffer_t *s); |
65 | | |
66 | | /** |
67 | | * If necessary, expand the stringbuffer_t internal buffer to accommodate the |
68 | | * specified additional size. |
69 | | */ |
70 | | static inline void |
71 | | stringbuffer_makeroom(stringbuffer_t *s, size_t size_to_add) |
72 | 0 | { |
73 | 0 | size_t current_size = (s->str_end - s->str_start); |
74 | 0 | size_t capacity = s->capacity; |
75 | 0 | size_t required_size = current_size + size_to_add; |
76 | |
|
77 | 0 | while (capacity < required_size) |
78 | 0 | capacity *= 2; |
79 | |
|
80 | 0 | if (capacity > s->capacity) |
81 | 0 | { |
82 | 0 | s->str_start = lwrealloc(s->str_start, capacity); |
83 | 0 | s->capacity = capacity; |
84 | 0 | s->str_end = s->str_start + current_size; |
85 | 0 | } |
86 | 0 | } Unexecuted instantiation: lwout_wkt.c:stringbuffer_makeroom Unexecuted instantiation: lwutil.c:stringbuffer_makeroom Unexecuted instantiation: stringbuffer.c:stringbuffer_makeroom |
87 | | |
88 | | |
89 | | /** |
90 | | * Append the specified string to the stringbuffer_t using known length |
91 | | */ |
92 | | inline static void |
93 | | stringbuffer_append_len(stringbuffer_t *s, const char *a, size_t alen) |
94 | 0 | { |
95 | 0 | int alen0 = alen + 1; /* Length including null terminator */ |
96 | 0 | stringbuffer_makeroom(s, alen0); |
97 | 0 | memcpy(s->str_end, a, alen0); |
98 | 0 | s->str_end += alen; |
99 | 0 | } Unexecuted instantiation: lwout_wkt.c:stringbuffer_append_len Unexecuted instantiation: lwutil.c:stringbuffer_append_len Unexecuted instantiation: stringbuffer.c:stringbuffer_append_len |
100 | | |
101 | | /** |
102 | | * Append the specified string to the stringbuffer_t. |
103 | | */ |
104 | | inline static void |
105 | | stringbuffer_append(stringbuffer_t *s, const char *a) |
106 | 0 | { |
107 | 0 | int alen = strlen(a); /* Length of string to append */ |
108 | 0 | stringbuffer_append_len(s, a, alen); |
109 | 0 | } Unexecuted instantiation: lwout_wkt.c:stringbuffer_append Unexecuted instantiation: lwutil.c:stringbuffer_append Unexecuted instantiation: stringbuffer.c:stringbuffer_append |
110 | | |
111 | | inline static void |
112 | | stringbuffer_append_double(stringbuffer_t *s, double d, int precision) |
113 | 0 | { |
114 | 0 | stringbuffer_makeroom(s, OUT_MAX_BYTES_DOUBLE); |
115 | 0 | s->str_end += lwprint_double(d, precision, s->str_end); |
116 | 0 | } Unexecuted instantiation: lwout_wkt.c:stringbuffer_append_double Unexecuted instantiation: lwutil.c:stringbuffer_append_double Unexecuted instantiation: stringbuffer.c:stringbuffer_append_double |
117 | | |
118 | | inline static void |
119 | | stringbuffer_append_char(stringbuffer_t *s, char c) |
120 | 0 | { |
121 | 0 | stringbuffer_makeroom(s, 1); |
122 | 0 | *(s->str_end) = c; |
123 | 0 | s->str_end++; |
124 | 0 | } Unexecuted instantiation: lwout_wkt.c:stringbuffer_append_char Unexecuted instantiation: lwutil.c:stringbuffer_append_char Unexecuted instantiation: stringbuffer.c:stringbuffer_append_char |
125 | | |
126 | | |
127 | | #endif /* _STRINGBUFFER_H */ |