Upgrade GDB from 7.4.1 to 7.6.1 on the vendor branch
[dragonfly.git] / contrib / gdb-7 / gdb / common / buffer.c
CommitLineData
a45ae5f8
JM
1/* A simple growing buffer for GDB.
2
ef5ccd6c 3 Copyright (C) 2009-2013 Free Software Foundation, Inc.
a45ae5f8
JM
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#ifdef GDBSERVER
21#include "server.h"
22#else
23#include "defs.h"
24#endif
25
26#include "xml-utils.h"
27#include "buffer.h"
ef5ccd6c 28#include "inttypes.h"
a45ae5f8
JM
29
30#include <stdlib.h>
31#include <string.h>
32#include <stdio.h>
ef5ccd6c 33#include <stdint.h>
a45ae5f8
JM
34
35void
36buffer_grow (struct buffer *buffer, const char *data, size_t size)
37{
38 char *new_buffer;
39 size_t new_buffer_size;
40
41 if (size == 0)
42 return;
43
44 new_buffer_size = buffer->buffer_size;
45
46 if (new_buffer_size == 0)
47 new_buffer_size = 1;
48
49 while (buffer->used_size + size > new_buffer_size)
50 new_buffer_size *= 2;
51 new_buffer = xrealloc (buffer->buffer, new_buffer_size);
a45ae5f8
JM
52 memcpy (new_buffer + buffer->used_size, data, size);
53 buffer->buffer = new_buffer;
54 buffer->buffer_size = new_buffer_size;
55 buffer->used_size += size;
56}
57
58void
59buffer_free (struct buffer *buffer)
60{
61 if (!buffer)
62 return;
63
64 xfree (buffer->buffer);
65 buffer->buffer = NULL;
66 buffer->buffer_size = 0;
67 buffer->used_size = 0;
68}
69
70void
71buffer_init (struct buffer *buffer)
72{
73 memset (buffer, 0, sizeof (*buffer));
74}
75
76char*
77buffer_finish (struct buffer *buffer)
78{
79 char *ret = buffer->buffer;
80 buffer->buffer = NULL;
81 buffer->buffer_size = 0;
82 buffer->used_size = 0;
83 return ret;
84}
85
86void
87buffer_xml_printf (struct buffer *buffer, const char *format, ...)
88{
89 va_list ap;
90 const char *f;
91 const char *prev;
92 int percent = 0;
93
94 va_start (ap, format);
95
96 prev = format;
97 for (f = format; *f; f++)
98 {
99 if (percent)
100 {
101 char buf[32];
102 char *p;
103 char *str = buf;
ef5ccd6c 104 const char *f_old = f;
a45ae5f8
JM
105
106 switch (*f)
107 {
108 case 's':
109 str = va_arg (ap, char *);
110 break;
111 case 'd':
112 sprintf (str, "%d", va_arg (ap, int));
113 break;
114 case 'u':
115 sprintf (str, "%u", va_arg (ap, unsigned int));
116 break;
117 case 'x':
118 sprintf (str, "%x", va_arg (ap, unsigned int));
119 break;
120 case 'o':
121 sprintf (str, "%o", va_arg (ap, unsigned int));
122 break;
ef5ccd6c
JM
123 case 'l':
124 f++;
125 switch (*f)
126 {
127 case 'd':
128 sprintf (str, "%ld", va_arg (ap, long));
129 break;
130 case 'u':
131 sprintf (str, "%lu", va_arg (ap, unsigned long));
132 break;
133 case 'x':
134 sprintf (str, "%lx", va_arg (ap, unsigned long));
135 break;
136 case 'o':
137 sprintf (str, "%lo", va_arg (ap, unsigned long));
138 break;
139 case 'l':
140 f++;
141 switch (*f)
142 {
143 case 'd':
144 sprintf (str, "%" PRId64,
145 (int64_t) va_arg (ap, long long));
146 break;
147 case 'u':
148 sprintf (str, "%" PRIu64,
149 (uint64_t) va_arg (ap, unsigned long long));
150 break;
151 case 'x':
152 sprintf (str, "%" PRIx64,
153 (uint64_t) va_arg (ap, unsigned long long));
154 break;
155 case 'o':
156 sprintf (str, "%" PRIo64,
157 (uint64_t) va_arg (ap, unsigned long long));
158 break;
159 default:
160 str = 0;
161 break;
162 }
163 break;
164 default:
165 str = 0;
166 break;
167 }
168 break;
a45ae5f8
JM
169 default:
170 str = 0;
171 break;
172 }
ef5ccd6c 173
a45ae5f8
JM
174 if (str)
175 {
ef5ccd6c 176 buffer_grow (buffer, prev, f_old - prev - 1);
a45ae5f8
JM
177 p = xml_escape_text (str);
178 buffer_grow_str (buffer, p);
179 xfree (p);
180 prev = f + 1;
181 }
182 percent = 0;
183 }
184 else if (*f == '%')
185 percent = 1;
186 }
187
188 buffer_grow_str (buffer, prev);
189 va_end (ap);
190}
191