buffer.h 2.3 KB

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