1/* A simple growing buffer for GDB.
2
3   Copyright (C) 2009-2023 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#ifndef COMMON_BUFFER_H
21#define COMMON_BUFFER_H
22
23struct buffer
24{
25  char *buffer;
26  size_t buffer_size; /* allocated size */
27  size_t used_size; /* actually used size */
28};
29
30/* Append DATA of size SIZE to the end of BUFFER.  Grows the buffer to
31   accommodate the new data.  */
32void buffer_grow (struct buffer *buffer, const char *data, size_t size);
33
34/* Append C to the end of BUFFER.  Grows the buffer to accommodate the
35   new data.  */
36
37static inline void
38buffer_grow_char (struct buffer *buffer, char c)
39{
40  buffer_grow (buffer, &c, 1);
41}
42
43/* Release any memory held by BUFFER.  */
44void buffer_free (struct buffer *buffer);
45
46/* Initialize BUFFER.  BUFFER holds no memory afterwards.  */
47void buffer_init (struct buffer *buffer);
48
49/* Return a pointer into BUFFER data, effectively transferring
50   ownership of the buffer memory to the caller.  Calling buffer_free
51   afterwards has no effect on the returned data.  */
52char* buffer_finish (struct buffer *buffer);
53
54/* Simple printf to buffer function.  Current implemented formatters:
55   %s - grow an xml escaped text in BUFFER.
56   %d - grow an signed integer in BUFFER.
57   %u - grow an unsigned integer in BUFFER.
58   %x - grow an unsigned integer formatted in hexadecimal in BUFFER.
59   %o - grow an unsigned integer formatted in octal in BUFFER.  */
60void buffer_xml_printf (struct buffer *buffer, const char *format, ...)
61  ATTRIBUTE_PRINTF (2, 3);
62
63#define buffer_grow_str(BUFFER,STRING)		\
64  buffer_grow (BUFFER, STRING, strlen (STRING))
65#define buffer_grow_str0(BUFFER,STRING)			\
66  buffer_grow (BUFFER, STRING, strlen (STRING) + 1)
67
68#endif /* COMMON_BUFFER_H */
69