1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
/* A buffer that accumulates a string by piecewise concatenation.
Copyright (C) 2021-2024 Free Software Foundation, Inc.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* Written by Bruno Haible <bruno@clisp.org>, 2021. */
#include <config.h>
/* Specification. */
#include "string-buffer.h"
/* Undocumented. */
extern int sb_ensure_more_bytes (struct string_buffer *buffer,
size_t increment);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* The warnings about memory resource 'buffer->data' in this file are not
relevant. Silence them. */
#if __clang_major__ >= 3
# pragma clang diagnostic ignored "-Wthread-safety"
#endif
void
sb_init (struct string_buffer *buffer)
{
buffer->data = buffer->space;
buffer->length = 0;
buffer->allocated = sizeof (buffer->space);
buffer->error = false;
}
/* Ensures that INCREMENT bytes are available beyond the current used length
of BUFFER.
Returns 0, or -1 in case of out-of-memory error. */
int
sb_ensure_more_bytes (struct string_buffer *buffer, size_t increment)
{
size_t incremented_length = buffer->length + increment;
if (incremented_length < increment)
/* Overflow. */
return -1;
if (buffer->allocated < incremented_length)
{
size_t new_allocated = 2 * buffer->allocated;
if (new_allocated < buffer->allocated)
/* Overflow. */
return -1;
if (new_allocated < incremented_length)
new_allocated = incremented_length;
char *new_data;
if (buffer->data == buffer->space)
{
new_data = (char *) malloc (new_allocated);
if (new_data == NULL)
/* Out-of-memory. */
return -1;
memcpy (new_data, buffer->data, buffer->length);
}
else
{
new_data = (char *) realloc (buffer->data, new_allocated);
if (new_data == NULL)
/* Out-of-memory. */
return -1;
}
buffer->data = new_data;
buffer->allocated = new_allocated;
}
return 0;
}
int
sb_append1 (struct string_buffer *buffer, char c)
{
if (sb_ensure_more_bytes (buffer, 1) < 0)
{
buffer->error = true;
return -1;
}
buffer->data[buffer->length++] = c;
return 0;
}
int
sb_append_desc (struct string_buffer *buffer, string_desc_t s)
{
size_t len = string_desc_length (s);
if (sb_ensure_more_bytes (buffer, len) < 0)
{
buffer->error = true;
return -1;
}
memcpy (buffer->data + buffer->length, string_desc_data (s), len);
buffer->length += len;
return 0;
}
int
sb_append_c (struct string_buffer *buffer, const char *str)
{
size_t len = strlen (str);
if (sb_ensure_more_bytes (buffer, len) < 0)
{
buffer->error = true;
return -1;
}
memcpy (buffer->data + buffer->length, str, len);
buffer->length += len;
return 0;
}
void
sb_free (struct string_buffer *buffer)
{
if (buffer->data != buffer->space)
free (buffer->data);
}
string_desc_t
sb_contents (struct string_buffer *buffer)
{
return string_desc_new_addr (buffer->length, buffer->data);
}
const char *
sb_contents_c (struct string_buffer *buffer)
{
if (sb_ensure_more_bytes (buffer, 1) < 0)
return NULL;
buffer->data[buffer->length] = '\0';
return buffer->data;
}
string_desc_t
sb_dupfree (struct string_buffer *buffer)
{
if (buffer->error)
goto fail;
size_t length = buffer->length;
if (buffer->data == buffer->space)
{
char *copy = (char *) malloc (length > 0 ? length : 1);
if (copy == NULL)
goto fail;
memcpy (copy, buffer->data, length);
return string_desc_new_addr (length, copy);
}
else
{
/* Shrink the string before returning it. */
char *contents = buffer->data;
if (length < buffer->allocated)
{
contents = realloc (contents, length > 0 ? length : 1);
if (contents == NULL)
goto fail;
}
return string_desc_new_addr (length, contents);
}
fail:
sb_free (buffer);
return string_desc_new_addr (0, NULL);
}
char *
sb_dupfree_c (struct string_buffer *buffer)
{
if (buffer->error)
goto fail;
if (sb_ensure_more_bytes (buffer, 1) < 0)
goto fail;
buffer->data[buffer->length] = '\0';
buffer->length++;
size_t length = buffer->length;
if (buffer->data == buffer->space)
{
char *copy = (char *) malloc (length);
if (copy == NULL)
goto fail;
memcpy (copy, buffer->data, length);
return copy;
}
else
{
/* Shrink the string before returning it. */
char *contents = buffer->data;
if (length < buffer->allocated)
{
contents = realloc (contents, length);
if (contents == NULL)
goto fail;
}
return contents;
}
fail:
sb_free (buffer);
return NULL;
}
|