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