Coverage Report

Created: 2026-12-01 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
  bool success;
45
}
46
stringbuffer_t;
47
48
extern stringbuffer_t *stringbuffer_create_with_size(size_t size);
49
extern stringbuffer_t *stringbuffer_create(void);
50
extern void stringbuffer_init(stringbuffer_t *s);
51
extern void stringbuffer_init_varlena(stringbuffer_t *s);
52
extern void stringbuffer_release(stringbuffer_t *s);
53
extern void stringbuffer_destroy(stringbuffer_t *sb);
54
extern void stringbuffer_clear(stringbuffer_t *sb);
55
void stringbuffer_set(stringbuffer_t *sb, const char *s);
56
void stringbuffer_copy(stringbuffer_t *sb, stringbuffer_t *src);
57
extern int stringbuffer_aprintf(stringbuffer_t *sb, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
58
extern const char *stringbuffer_getstring(stringbuffer_t *sb);
59
extern char *stringbuffer_getstringcopy(stringbuffer_t *sb);
60
extern lwvarlena_t *stringbuffer_getvarlenacopy(stringbuffer_t *s);
61
extern lwvarlena_t * stringbuffer_getvarlena(stringbuffer_t *s);
62
extern int stringbuffer_getlength(stringbuffer_t *sb);
63
extern char stringbuffer_lastchar(stringbuffer_t *s);
64
extern int stringbuffer_trim_trailing_white(stringbuffer_t *s);
65
extern int stringbuffer_trim_trailing_zeroes(stringbuffer_t *s);
66
67
/**
68
 * If necessary, expand the stringbuffer_t internal buffer to accommodate the
69
 * specified additional size.
70
 */
71
static inline bool
72
stringbuffer_makeroom(stringbuffer_t *s, size_t size_to_add)
73
0
{
74
0
  size_t current_size = (s->str_end - s->str_start);
75
0
  size_t capacity = s->capacity;
76
0
  size_t required_size;
77
0
  if (!lw_size_add(current_size, size_to_add, &required_size) || required_size > LW_MAX_ALLOC_SIZE)
78
0
  {
79
0
    lwerror("buffer exceeds the maximum allocation size");
80
0
    s->success = false;
81
0
    return false;
82
0
  }
83
84
0
  while (capacity < required_size)
85
0
  {
86
0
    if (capacity > LW_MAX_ALLOC_SIZE / 2)
87
0
      capacity = required_size;
88
0
    else
89
0
      capacity *= 2;
90
0
  }
91
92
0
  if (capacity > s->capacity)
93
0
  {
94
0
    s->str_start = lwrealloc(s->str_start, capacity);
95
0
    s->capacity = capacity;
96
0
    s->str_end = s->str_start + current_size;
97
0
  }
98
0
  return true;
99
0
}
Unexecuted instantiation: lwout_wkt.c:stringbuffer_makeroom
Unexecuted instantiation: lwutil.c:stringbuffer_makeroom
Unexecuted instantiation: stringbuffer.c:stringbuffer_makeroom
100
101
102
/**
103
 * Append the specified string to the stringbuffer_t using known length
104
 */
105
inline static void
106
stringbuffer_append_len(stringbuffer_t *s, const char *a, size_t alen)
107
0
{
108
0
  size_t alen0;
109
0
  if (!lw_size_add(alen, 1, &alen0))
110
0
  {
111
0
    lwerror("string length overflow");
112
0
    s->success = false;
113
0
    return;
114
0
  }
115
0
  if (!stringbuffer_makeroom(s, alen0))
116
0
    return;
117
0
  memcpy(s->str_end, a, alen0);
118
0
  s->str_end += alen;
119
0
}
Unexecuted instantiation: lwout_wkt.c:stringbuffer_append_len
Unexecuted instantiation: lwutil.c:stringbuffer_append_len
Unexecuted instantiation: stringbuffer.c:stringbuffer_append_len
120
121
/**
122
 * Append the specified string to the stringbuffer_t.
123
 */
124
inline static void
125
stringbuffer_append(stringbuffer_t *s, const char *a)
126
0
{
127
0
  size_t alen = strlen(a); /* Length of string to append */
128
0
  stringbuffer_append_len(s, a, alen);
129
0
}
Unexecuted instantiation: lwout_wkt.c:stringbuffer_append
Unexecuted instantiation: lwutil.c:stringbuffer_append
Unexecuted instantiation: stringbuffer.c:stringbuffer_append
130
131
inline static void
132
stringbuffer_append_double(stringbuffer_t *s, double d, int precision)
133
0
{
134
0
  if (!stringbuffer_makeroom(s, OUT_MAX_BYTES_DOUBLE))
135
0
    return;
136
0
  s->str_end += lwprint_double(d, precision, s->str_end);
137
0
}
Unexecuted instantiation: lwout_wkt.c:stringbuffer_append_double
Unexecuted instantiation: lwutil.c:stringbuffer_append_double
Unexecuted instantiation: stringbuffer.c:stringbuffer_append_double
138
139
inline static void
140
stringbuffer_append_char(stringbuffer_t *s, char c)
141
0
{
142
0
  if (!stringbuffer_makeroom(s, 1))
143
0
    return;
144
0
  *(s->str_end) = c;
145
0
  s->str_end++;
146
0
}
Unexecuted instantiation: lwout_wkt.c:stringbuffer_append_char
Unexecuted instantiation: lwutil.c:stringbuffer_append_char
Unexecuted instantiation: stringbuffer.c:stringbuffer_append_char
147
148
149
#endif /* _STRINGBUFFER_H */