xref: /dflybsd-src/contrib/gdb-7/gdb/common/buffer.h (revision de8e141f24382815c10a4012d209bbbf7abf1112)
1a45ae5f8SJohn Marino /* A simple growing buffer for GDB.
2a45ae5f8SJohn Marino 
3*ef5ccd6cSJohn Marino    Copyright (C) 2009-2013 Free Software Foundation, Inc.
4a45ae5f8SJohn Marino 
5a45ae5f8SJohn Marino    This file is part of GDB.
6a45ae5f8SJohn Marino 
7a45ae5f8SJohn Marino    This program is free software; you can redistribute it and/or modify
8a45ae5f8SJohn Marino    it under the terms of the GNU General Public License as published by
9a45ae5f8SJohn Marino    the Free Software Foundation; either version 3 of the License, or
10a45ae5f8SJohn Marino    (at your option) any later version.
11a45ae5f8SJohn Marino 
12a45ae5f8SJohn Marino    This program is distributed in the hope that it will be useful,
13a45ae5f8SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
14a45ae5f8SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15a45ae5f8SJohn Marino    GNU General Public License for more details.
16a45ae5f8SJohn Marino 
17a45ae5f8SJohn Marino    You should have received a copy of the GNU General Public License
18a45ae5f8SJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19a45ae5f8SJohn Marino 
20a45ae5f8SJohn Marino #ifndef BUFFER_H
21a45ae5f8SJohn Marino #define BUFFER_H
22a45ae5f8SJohn Marino 
23a45ae5f8SJohn Marino #include <stddef.h>
24a45ae5f8SJohn Marino #include <string.h>
25a45ae5f8SJohn Marino #include "ansidecl.h"
26a45ae5f8SJohn Marino 
27a45ae5f8SJohn Marino struct buffer
28a45ae5f8SJohn Marino {
29a45ae5f8SJohn Marino   char *buffer;
30a45ae5f8SJohn Marino   size_t buffer_size; /* allocated size */
31a45ae5f8SJohn Marino   size_t used_size; /* actually used size */
32a45ae5f8SJohn Marino };
33a45ae5f8SJohn Marino 
34a45ae5f8SJohn Marino /* Append DATA of size SIZE to the end of BUFFER.  Grows the buffer to
35a45ae5f8SJohn Marino    accommodate the new data.  */
36a45ae5f8SJohn Marino void buffer_grow (struct buffer *buffer, const char *data, size_t size);
37a45ae5f8SJohn Marino 
38a45ae5f8SJohn Marino /* Release any memory held by BUFFER.  */
39a45ae5f8SJohn Marino void buffer_free (struct buffer *buffer);
40a45ae5f8SJohn Marino 
41a45ae5f8SJohn Marino /* Initialize BUFFER.  BUFFER holds no memory afterwards.  */
42a45ae5f8SJohn Marino void buffer_init (struct buffer *buffer);
43a45ae5f8SJohn Marino 
44a45ae5f8SJohn Marino /* Return a pointer into BUFFER data, effectivelly transfering
45a45ae5f8SJohn Marino    ownership of the buffer memory to the caller.  Calling buffer_free
46a45ae5f8SJohn Marino    afterwards has no effect on the returned data.  */
47a45ae5f8SJohn Marino char* buffer_finish (struct buffer *buffer);
48a45ae5f8SJohn Marino 
49a45ae5f8SJohn Marino /* Simple printf to buffer function.  Current implemented formatters:
50a45ae5f8SJohn Marino    %s - grow an xml escaped text in BUFFER.
51a45ae5f8SJohn Marino    %d - grow an signed integer in BUFFER.
52a45ae5f8SJohn Marino    %u - grow an unsigned integer in BUFFER.
53a45ae5f8SJohn Marino    %x - grow an unsigned integer formatted in hexadecimal in BUFFER.
54a45ae5f8SJohn Marino    %o - grow an unsigned integer formatted in octal in BUFFER.  */
55a45ae5f8SJohn Marino void buffer_xml_printf (struct buffer *buffer, const char *format, ...)
56a45ae5f8SJohn Marino   ATTRIBUTE_PRINTF (2, 3);
57a45ae5f8SJohn Marino 
58a45ae5f8SJohn Marino #define buffer_grow_str(BUFFER,STRING)		\
59a45ae5f8SJohn Marino   buffer_grow (BUFFER, STRING, strlen (STRING))
60a45ae5f8SJohn Marino #define buffer_grow_str0(BUFFER,STRING)			\
61a45ae5f8SJohn Marino   buffer_grow (BUFFER, STRING, strlen (STRING) + 1)
62a45ae5f8SJohn Marino 
63a45ae5f8SJohn Marino #endif
64