xref: /third_party/mesa3d/src/util/string_buffer.h (revision bf215546)
1/*
2 * Copyright © 2017 Thomas Helland
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24#ifndef _STRING_BUFFER_H
25#define _STRING_BUFFER_H
26
27#include "ralloc.h"
28#include "u_string.h"
29#include <stdbool.h>
30#include <stdint.h>
31#include <stdio.h>
32#include <string.h>
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
38struct _mesa_string_buffer {
39   char *buf;
40   uint32_t length;
41   uint32_t capacity;
42};
43
44struct _mesa_string_buffer *
45_mesa_string_buffer_create(void *mem_ctx, uint32_t initial_capacity);
46
47static inline void
48_mesa_string_buffer_destroy(struct _mesa_string_buffer *str)
49{
50   ralloc_free(str);
51}
52
53bool
54_mesa_string_buffer_append_all(struct _mesa_string_buffer *str,
55                               uint32_t num_args, ...);
56bool
57_mesa_string_buffer_append_len(struct _mesa_string_buffer *str,
58                               const char *c, uint32_t len);
59
60static inline bool
61_mesa_string_buffer_append_char(struct _mesa_string_buffer *str, char c)
62{
63   return _mesa_string_buffer_append_len(str, &c, 1);
64}
65
66static inline bool
67_mesa_string_buffer_append(struct _mesa_string_buffer *str, const char *c)
68{
69   return _mesa_string_buffer_append_len(str, c, strlen(c));
70}
71
72static inline void
73_mesa_string_buffer_clear(struct _mesa_string_buffer *str)
74{
75   str->length = 0;
76   str->buf[str->length] = '\0';
77}
78
79static inline void
80_mesa_string_buffer_crimp_to_fit(struct _mesa_string_buffer *str)
81{
82    char *crimped =
83       (char *) reralloc_array_size(str, str->buf, sizeof(char),
84                                    str->capacity);
85    if (!crimped)
86       return;
87
88    str->capacity = str->length + 1;
89    str->buf = crimped;
90}
91
92bool
93_mesa_string_buffer_vprintf(struct _mesa_string_buffer *str,
94                            const char *format, va_list args);
95
96bool
97_mesa_string_buffer_printf(struct _mesa_string_buffer *str,
98                            const char *format, ...);
99
100#ifdef __cplusplus
101} /* extern "C" */
102#endif
103
104#endif /* _STRING_BUFFER_H */
105