1 /* A simple growing buffer for GDB.
3 Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "common-defs.h"
21 #include "xml-utils.h"
27 buffer_grow (struct buffer *buffer, const char *data, size_t size)
30 size_t new_buffer_size;
35 new_buffer_size = buffer->buffer_size;
37 if (new_buffer_size == 0)
40 while (buffer->used_size + size > new_buffer_size)
42 new_buffer = xrealloc (buffer->buffer, new_buffer_size);
43 memcpy (new_buffer + buffer->used_size, data, size);
44 buffer->buffer = new_buffer;
45 buffer->buffer_size = new_buffer_size;
46 buffer->used_size += size;
50 buffer_free (struct buffer *buffer)
55 xfree (buffer->buffer);
56 buffer->buffer = NULL;
57 buffer->buffer_size = 0;
58 buffer->used_size = 0;
62 buffer_init (struct buffer *buffer)
64 memset (buffer, 0, sizeof (*buffer));
68 buffer_finish (struct buffer *buffer)
70 char *ret = buffer->buffer;
71 buffer->buffer = NULL;
72 buffer->buffer_size = 0;
73 buffer->used_size = 0;
78 buffer_xml_printf (struct buffer *buffer, const char *format, ...)
85 va_start (ap, format);
88 for (f = format; *f; f++)
95 const char *f_old = f;
100 str = va_arg (ap, char *);
103 sprintf (str, "%d", va_arg (ap, int));
106 sprintf (str, "%u", va_arg (ap, unsigned int));
109 sprintf (str, "%x", va_arg (ap, unsigned int));
112 sprintf (str, "%o", va_arg (ap, unsigned int));
119 sprintf (str, "%ld", va_arg (ap, long));
122 sprintf (str, "%lu", va_arg (ap, unsigned long));
125 sprintf (str, "%lx", va_arg (ap, unsigned long));
128 sprintf (str, "%lo", va_arg (ap, unsigned long));
135 sprintf (str, "%" PRId64,
136 (int64_t) va_arg (ap, long long));
139 sprintf (str, "%" PRIu64,
140 (uint64_t) va_arg (ap, unsigned long long));
143 sprintf (str, "%" PRIx64,
144 (uint64_t) va_arg (ap, unsigned long long));
147 sprintf (str, "%" PRIo64,
148 (uint64_t) va_arg (ap, unsigned long long));
167 buffer_grow (buffer, prev, f_old - prev - 1);
168 p = xml_escape_text (str);
169 buffer_grow_str (buffer, p);
179 buffer_grow_str (buffer, prev);