1 /* A simple growing buffer for GDB.
3 Copyright (C) 2009-2023 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"
25 buffer_grow (struct buffer
*buffer
, const char *data
, size_t size
)
28 size_t new_buffer_size
;
33 new_buffer_size
= buffer
->buffer_size
;
35 if (new_buffer_size
== 0)
38 while (buffer
->used_size
+ size
> new_buffer_size
)
40 new_buffer
= (char *) xrealloc (buffer
->buffer
, new_buffer_size
);
41 memcpy (new_buffer
+ buffer
->used_size
, data
, size
);
42 buffer
->buffer
= new_buffer
;
43 buffer
->buffer_size
= new_buffer_size
;
44 buffer
->used_size
+= size
;
48 buffer_free (struct buffer
*buffer
)
53 xfree (buffer
->buffer
);
54 buffer
->buffer
= NULL
;
55 buffer
->buffer_size
= 0;
56 buffer
->used_size
= 0;
60 buffer_init (struct buffer
*buffer
)
62 memset (buffer
, 0, sizeof (*buffer
));
66 buffer_finish (struct buffer
*buffer
)
68 char *ret
= buffer
->buffer
;
69 buffer
->buffer
= NULL
;
70 buffer
->buffer_size
= 0;
71 buffer
->used_size
= 0;
76 buffer_xml_printf (struct buffer
*buffer
, const char *format
, ...)
83 va_start (ap
, format
);
86 for (f
= format
; *f
; f
++)
92 const char *f_old
= f
;
97 str
= va_arg (ap
, char *);
100 sprintf (str
, "%d", va_arg (ap
, int));
103 sprintf (str
, "%u", va_arg (ap
, unsigned int));
106 sprintf (str
, "%x", va_arg (ap
, unsigned int));
109 sprintf (str
, "%o", va_arg (ap
, unsigned int));
116 sprintf (str
, "%ld", va_arg (ap
, long));
119 sprintf (str
, "%lu", va_arg (ap
, unsigned long));
122 sprintf (str
, "%lx", va_arg (ap
, unsigned long));
125 sprintf (str
, "%lo", va_arg (ap
, unsigned long));
132 sprintf (str
, "%" PRId64
,
133 (int64_t) va_arg (ap
, long long));
136 sprintf (str
, "%" PRIu64
,
137 (uint64_t) va_arg (ap
, unsigned long long));
140 sprintf (str
, "%" PRIx64
,
141 (uint64_t) va_arg (ap
, unsigned long long));
144 sprintf (str
, "%" PRIo64
,
145 (uint64_t) va_arg (ap
, unsigned long long));
164 buffer_grow (buffer
, prev
, f_old
- prev
- 1);
165 std::string p
= xml_escape_text (str
);
166 buffer_grow_str (buffer
, p
.c_str ());
175 buffer_grow_str (buffer
, prev
);