1b8e80941Smrg/*
2b8e80941Smrg * Copyright © 2017 Thomas Helland
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20b8e80941Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21b8e80941Smrg * IN THE SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg */
24b8e80941Smrg
25b8e80941Smrg#include "string_buffer.h"
26b8e80941Smrg
27b8e80941Smrgstatic bool
28b8e80941Smrgensure_capacity(struct _mesa_string_buffer *str, uint32_t needed_capacity)
29b8e80941Smrg{
30b8e80941Smrg   if (needed_capacity <= str->capacity)
31b8e80941Smrg      return true;
32b8e80941Smrg
33b8e80941Smrg   /* Too small, double until we can fit the new string */
34b8e80941Smrg   uint32_t new_capacity = str->capacity * 2;
35b8e80941Smrg   while (needed_capacity > new_capacity)
36b8e80941Smrg      new_capacity *= 2;
37b8e80941Smrg
38b8e80941Smrg   str->buf = reralloc_array_size(str, str->buf, sizeof(char), new_capacity);
39b8e80941Smrg   if (str->buf == NULL)
40b8e80941Smrg      return false;
41b8e80941Smrg
42b8e80941Smrg   str->capacity = new_capacity;
43b8e80941Smrg   return true;
44b8e80941Smrg}
45b8e80941Smrg
46b8e80941Smrgstruct _mesa_string_buffer *
47b8e80941Smrg_mesa_string_buffer_create(void *mem_ctx, uint32_t initial_capacity)
48b8e80941Smrg{
49b8e80941Smrg   struct _mesa_string_buffer *str;
50b8e80941Smrg   str = ralloc(mem_ctx, struct _mesa_string_buffer);
51b8e80941Smrg
52b8e80941Smrg   if (str == NULL)
53b8e80941Smrg      return NULL;
54b8e80941Smrg
55b8e80941Smrg   /* If no initial capacity is set then set it to something */
56b8e80941Smrg   str->capacity = initial_capacity ? initial_capacity : 32;
57b8e80941Smrg   str->buf = ralloc_array(str, char, str->capacity);
58b8e80941Smrg
59b8e80941Smrg   if (!str->buf) {
60b8e80941Smrg      ralloc_free(str);
61b8e80941Smrg      return NULL;
62b8e80941Smrg   }
63b8e80941Smrg
64b8e80941Smrg   str->length = 0;
65b8e80941Smrg   str->buf[str->length] = '\0';
66b8e80941Smrg   return str;
67b8e80941Smrg}
68b8e80941Smrg
69b8e80941Smrgbool
70b8e80941Smrg_mesa_string_buffer_append_all(struct _mesa_string_buffer *str,
71b8e80941Smrg                               uint32_t num_args, ...)
72b8e80941Smrg{
73b8e80941Smrg   int i;
74b8e80941Smrg   char* s;
75b8e80941Smrg   va_list args;
76b8e80941Smrg   va_start(args, num_args);
77b8e80941Smrg   for (i = 0; i < num_args; i++) {
78b8e80941Smrg      s = va_arg(args, char*);
79b8e80941Smrg      if (!_mesa_string_buffer_append_len(str, s, strlen(s))) {
80b8e80941Smrg         va_end(args);
81b8e80941Smrg         return false;
82b8e80941Smrg      }
83b8e80941Smrg   }
84b8e80941Smrg   va_end(args);
85b8e80941Smrg   return true;
86b8e80941Smrg}
87b8e80941Smrg
88b8e80941Smrgbool
89b8e80941Smrg_mesa_string_buffer_append_len(struct _mesa_string_buffer *str,
90b8e80941Smrg                               const char *c, uint32_t len)
91b8e80941Smrg{
92b8e80941Smrg   uint32_t needed_length = str->length + len + 1;
93b8e80941Smrg
94b8e80941Smrg   /* Check if we're overflowing uint32_t */
95b8e80941Smrg   if (needed_length < str->length)
96b8e80941Smrg      return false;
97b8e80941Smrg
98b8e80941Smrg   if (!ensure_capacity(str, needed_length))
99b8e80941Smrg      return false;
100b8e80941Smrg
101b8e80941Smrg   memcpy(str->buf + str->length, c, len);
102b8e80941Smrg   str->length += len;
103b8e80941Smrg   str->buf[str->length] = '\0';
104b8e80941Smrg   return true;
105b8e80941Smrg}
106b8e80941Smrg
107b8e80941Smrgbool
108b8e80941Smrg_mesa_string_buffer_vprintf(struct _mesa_string_buffer *str,
109b8e80941Smrg                            const char *format, va_list args)
110b8e80941Smrg{
111b8e80941Smrg   /* We're looping two times to avoid duplicating code */
112b8e80941Smrg   for (uint32_t i = 0; i < 2; i++) {
113b8e80941Smrg      va_list arg_copy;
114b8e80941Smrg      va_copy(arg_copy, args);
115b8e80941Smrg      uint32_t space_left = str->capacity - str->length;
116b8e80941Smrg
117b8e80941Smrg      int32_t len = util_vsnprintf(str->buf + str->length,
118b8e80941Smrg                                   space_left, format, arg_copy);
119b8e80941Smrg      va_end(arg_copy);
120b8e80941Smrg
121b8e80941Smrg      /* Error in vsnprintf() or measured len overflows size_t */
122b8e80941Smrg      if (unlikely(len < 0 || str->length + len + 1 < str->length))
123b8e80941Smrg         return false;
124b8e80941Smrg
125b8e80941Smrg      /* There was enough space for the string; we're done */
126b8e80941Smrg      if (len < space_left) {
127b8e80941Smrg         str->length += len;
128b8e80941Smrg         return true;
129b8e80941Smrg      }
130b8e80941Smrg
131b8e80941Smrg      /* Not enough space, resize and retry */
132b8e80941Smrg      ensure_capacity(str, str->length + len + 1);
133b8e80941Smrg   }
134b8e80941Smrg
135b8e80941Smrg   return false;
136b8e80941Smrg}
137b8e80941Smrg
138b8e80941Smrgbool
139b8e80941Smrg_mesa_string_buffer_printf(struct _mesa_string_buffer *str,
140b8e80941Smrg                            const char *format, ...)
141b8e80941Smrg{
142b8e80941Smrg   bool res;
143b8e80941Smrg   va_list args;
144b8e80941Smrg   va_start(args, format);
145b8e80941Smrg   res = _mesa_string_buffer_vprintf(str, format, args);
146b8e80941Smrg   va_end(args);
147b8e80941Smrg   return res;
148b8e80941Smrg}
149