summaryrefslogtreecommitdiff
path: root/src/util/string_buffer.h
blob: eaaf5f33dfd2854905b95fc82ad3780dbb869bb2 (plain)
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
/*
 * Copyright © 2017 Thomas Helland
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */
#ifndef _STRING_BUFFER_H
#define _STRING_BUFFER_H

#include "ralloc.h"
#include "u_string.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

struct _mesa_string_buffer {
   char *buf;
   uint32_t length;
   uint32_t capacity;
};

struct _mesa_string_buffer *
_mesa_string_buffer_create(void *mem_ctx, uint32_t initial_capacity);

static inline void
_mesa_string_buffer_destroy(struct _mesa_string_buffer *str)
{
   ralloc_free(str);
}

bool
_mesa_string_buffer_append_all(struct _mesa_string_buffer *str,
                               uint32_t num_args, ...);
bool
_mesa_string_buffer_append_len(struct _mesa_string_buffer *str,
                               const char *c, uint32_t len);

static inline bool
_mesa_string_buffer_append_char(struct _mesa_string_buffer *str, char c)
{
   return _mesa_string_buffer_append_len(str, &c, 1);
}

static inline bool
_mesa_string_buffer_append(struct _mesa_string_buffer *str, const char *c)
{
   return _mesa_string_buffer_append_len(str, c, strlen(c));
}

static inline void
_mesa_string_buffer_clear(struct _mesa_string_buffer *str)
{
   str->length = 0;
   str->buf[str->length] = '\0';
}

static inline void
_mesa_string_buffer_crimp_to_fit(struct _mesa_string_buffer *str)
{
    char *crimped =
       (char *) reralloc_array_size(str, str->buf, sizeof(char),
                                    str->capacity);
    if (!crimped)
       return;

    str->capacity = str->length + 1;
    str->buf = crimped;
}

bool
_mesa_string_buffer_vprintf(struct _mesa_string_buffer *str,
                            const char *format, va_list args);

bool
_mesa_string_buffer_printf(struct _mesa_string_buffer *str,
                            const char *format, ...);

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif /* _STRING_BUFFER_H */