exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / string-buffer.h
blob75fd7cbef8104528d0d7603440c2df95ce2f6bc2
1 /* A buffer that accumulates a string by piecewise concatenation.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation, either version 3 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2021. */
19 #ifndef _STRING_BUFFER_H
20 #define _STRING_BUFFER_H
22 /* This file uses _GL_ATTRIBUTE_MALLOC. */
23 #if !_GL_CONFIG_H_INCLUDED
24 #error "Please include config.h first."
25 #endif
27 #include <stdarg.h>
28 #include <stdlib.h>
30 #include "attribute.h"
32 /* A string buffer type. */
33 struct string_buffer
35 char *data;
36 size_t length; /* used bytes, <= allocated */
37 size_t allocated; /* allocated bytes */
38 bool error; /* true if there was an error */
39 char space[1024]; /* stack allocated space */
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
46 /* Initializes BUFFER to the empty string. */
47 extern void sb_init (struct string_buffer *buffer);
49 /* Appends the contents of STR to BUFFER.
50 Returns 0, or -1 in case of out-of-memory error. */
51 extern int sb_append (struct string_buffer *buffer, const char *str);
53 /* Appends the result of the printf-compatible FORMATSTRING with the argument
54 list LIST to BUFFER.
55 Returns 0, or -1 in case of error. */
56 extern int sb_appendvf (struct string_buffer *buffer,
57 const char *formatstring, va_list list)
58 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
59 ATTRIBUTE_FORMAT ((__gnu_printf__, 2, 0))
60 #else
61 ATTRIBUTE_FORMAT ((__printf__, 2, 0))
62 #endif
65 /* Appends the result of the printf-compatible FORMATSTRING with the following
66 arguments to BUFFER.
67 Returns 0, or -1 in case of error. */
68 extern int sb_appendf (struct string_buffer *buffer,
69 const char *formatstring, ...)
70 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4)
71 ATTRIBUTE_FORMAT ((__gnu_printf__, 2, 3))
72 #else
73 ATTRIBUTE_FORMAT ((__printf__, 2, 3))
74 #endif
77 /* Frees the memory held by BUFFER. */
78 extern void sb_free (struct string_buffer *buffer);
80 /* Returns the contents of BUFFER, and frees all other memory held
81 by BUFFER. Returns NULL upon failure or if there was an error earlier.
82 It is the responsibility of the caller to free() the result. */
83 extern char * sb_dupfree (struct string_buffer *buffer)
84 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
86 #ifdef __cplusplus
88 #endif
90 #endif /* _STRING_BUFFER_H */