Coverage Report

Created: 2025-07-18 06:04

/src/libconfig/lib/strbuf.c
Line
Count
Source
1
/* ----------------------------------------------------------------------------
2
   libconfig - A library for processing structured configuration files
3
   Copyright (C) 2005-2025  Mark A Lindner
4
5
   This file is part of libconfig.
6
7
   This library is free software; you can redistribute it and/or
8
   modify it under the terms of the GNU Lesser General Public License
9
   as published by the Free Software Foundation; either version 2.1 of
10
   the License, or (at your option) any later version.
11
12
   This library is distributed in the hope that it will be useful, but
13
   WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
   Lesser General Public License for more details.
16
17
   You should have received a copy of the GNU Library General Public
18
   License along with this library; if not, see
19
   <http://www.gnu.org/licenses/>.
20
   ----------------------------------------------------------------------------
21
*/
22
23
#include "strbuf.h"
24
#include "util.h"
25
26
#include <string.h>
27
#include <stdlib.h>
28
29
18.2k
#define STRING_BLOCK_SIZE 64
30
31
/* ------------------------------------------------------------------------- */
32
33
void libconfig_strbuf_ensure_capacity(strbuf_t *buf, size_t len)
34
14.4k
{
35
14.4k
  static const size_t mask = ~(STRING_BLOCK_SIZE - 1);
36
37
14.4k
  size_t newlen = buf->length + len + 1; /* add 1 for NUL */
38
14.4k
  if(newlen > buf->capacity)
39
3.76k
  {
40
3.76k
    buf->capacity = (newlen + (STRING_BLOCK_SIZE - 1)) & mask;
41
3.76k
    buf->string = (char *)libconfig_realloc(buf->string, buf->capacity);
42
3.76k
  }
43
14.4k
}
44
45
/* ------------------------------------------------------------------------- */
46
47
char *libconfig_strbuf_release(strbuf_t *buf)
48
9.95k
{
49
9.95k
  char *r = buf->string;
50
9.95k
  __zero(buf);
51
9.95k
  return(r);
52
9.95k
}
53
54
/* ------------------------------------------------------------------------- */
55
56
void libconfig_strbuf_append_string(strbuf_t *buf, const char *s)
57
7.98k
{
58
7.98k
  size_t len = strlen(s);
59
7.98k
  libconfig_strbuf_ensure_capacity(buf, len);
60
7.98k
  strcpy(buf->string + buf->length, s);
61
7.98k
  buf->length += len;
62
7.98k
}
63
64
/* ------------------------------------------------------------------------- */
65
66
void libconfig_strbuf_append_char(strbuf_t *buf, char c)
67
6.50k
{
68
6.50k
  libconfig_strbuf_ensure_capacity(buf, 1);
69
6.50k
  *(buf->string + buf->length) = c;
70
6.50k
  ++(buf->length);
71
6.50k
  *(buf->string + buf->length) = '\0';
72
6.50k
}
73
74
/* ------------------------------------------------------------------------- */