gdb:
[deliverable/binutils-gdb.git] / gdb / common / buffer.c
1 /* A simple growing buffer for GDB.
2
3 Copyright (C) 2009-2012 Free Software Foundation, Inc.
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"
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32
33 void
34 buffer_grow (struct buffer *buffer, const char *data, size_t size)
35 {
36 char *new_buffer;
37 size_t new_buffer_size;
38
39 if (size == 0)
40 return;
41
42 new_buffer_size = buffer->buffer_size;
43
44 if (new_buffer_size == 0)
45 new_buffer_size = 1;
46
47 while (buffer->used_size + size > new_buffer_size)
48 new_buffer_size *= 2;
49 new_buffer = xrealloc (buffer->buffer, new_buffer_size);
50 if (!new_buffer)
51 abort ();
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
58 void
59 buffer_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
70 void
71 buffer_init (struct buffer *buffer)
72 {
73 memset (buffer, 0, sizeof (*buffer));
74 }
75
76 char*
77 buffer_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
86 void
87 buffer_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;
104
105 switch (*f)
106 {
107 case 's':
108 str = va_arg (ap, char *);
109 break;
110 case 'd':
111 sprintf (str, "%d", va_arg (ap, int));
112 break;
113 case 'u':
114 sprintf (str, "%u", va_arg (ap, unsigned int));
115 break;
116 case 'x':
117 sprintf (str, "%x", va_arg (ap, unsigned int));
118 break;
119 case 'o':
120 sprintf (str, "%o", va_arg (ap, unsigned int));
121 break;
122 default:
123 str = 0;
124 break;
125 }
126
127 if (str)
128 {
129 buffer_grow (buffer, prev, f - prev - 1);
130 p = xml_escape_text (str);
131 buffer_grow_str (buffer, p);
132 xfree (p);
133 prev = f + 1;
134 }
135 percent = 0;
136 }
137 else if (*f == '%')
138 percent = 1;
139 }
140
141 buffer_grow_str (buffer, prev);
142 va_end (ap);
143 }
144
This page took 0.054706 seconds and 4 git commands to generate.