gdb/common/buffer.c - gdb

Functions defined

Source code

  1. /* A simple growing buffer for GDB.

  2.    Copyright (C) 2009-2015 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. #include "common-defs.h"
  15. #include "xml-utils.h"
  16. #include "buffer.h"
  17. #include "inttypes.h"
  18. #include <stdint.h>

  19. void
  20. buffer_grow (struct buffer *buffer, const char *data, size_t size)
  21. {
  22.   char *new_buffer;
  23.   size_t new_buffer_size;

  24.   if (size == 0)
  25.     return;

  26.   new_buffer_size = buffer->buffer_size;

  27.   if (new_buffer_size == 0)
  28.     new_buffer_size = 1;

  29.   while (buffer->used_size + size > new_buffer_size)
  30.     new_buffer_size *= 2;
  31.   new_buffer = xrealloc (buffer->buffer, new_buffer_size);
  32.   memcpy (new_buffer + buffer->used_size, data, size);
  33.   buffer->buffer = new_buffer;
  34.   buffer->buffer_size = new_buffer_size;
  35.   buffer->used_size += size;
  36. }

  37. void
  38. buffer_free (struct buffer *buffer)
  39. {
  40.   if (!buffer)
  41.     return;

  42.   xfree (buffer->buffer);
  43.   buffer->buffer = NULL;
  44.   buffer->buffer_size = 0;
  45.   buffer->used_size = 0;
  46. }

  47. void
  48. buffer_init (struct buffer *buffer)
  49. {
  50.   memset (buffer, 0, sizeof (*buffer));
  51. }

  52. char*
  53. buffer_finish (struct buffer *buffer)
  54. {
  55.   char *ret = buffer->buffer;
  56.   buffer->buffer = NULL;
  57.   buffer->buffer_size = 0;
  58.   buffer->used_size = 0;
  59.   return ret;
  60. }

  61. void
  62. buffer_xml_printf (struct buffer *buffer, const char *format, ...)
  63. {
  64.   va_list ap;
  65.   const char *f;
  66.   const char *prev;
  67.   int percent = 0;

  68.   va_start (ap, format);

  69.   prev = format;
  70.   for (f = format; *f; f++)
  71.     {
  72.       if (percent)
  73.         {
  74.           char buf[32];
  75.           char *p;
  76.           char *str = buf;
  77.           const char *f_old = f;

  78.           switch (*f)
  79.             {
  80.             case 's':
  81.               str = va_arg (ap, char *);
  82.               break;
  83.             case 'd':
  84.               sprintf (str, "%d", va_arg (ap, int));
  85.               break;
  86.             case 'u':
  87.               sprintf (str, "%u", va_arg (ap, unsigned int));
  88.               break;
  89.             case 'x':
  90.               sprintf (str, "%x", va_arg (ap, unsigned int));
  91.               break;
  92.             case 'o':
  93.               sprintf (str, "%o", va_arg (ap, unsigned int));
  94.               break;
  95.             case 'l':
  96.               f++;
  97.               switch (*f)
  98.                 {
  99.                 case 'd':
  100.                   sprintf (str, "%ld", va_arg (ap, long));
  101.                   break;
  102.                 case 'u':
  103.                   sprintf (str, "%lu", va_arg (ap, unsigned long));
  104.                   break;
  105.                 case 'x':
  106.                   sprintf (str, "%lx", va_arg (ap, unsigned long));
  107.                   break;
  108.                 case 'o':
  109.                   sprintf (str, "%lo", va_arg (ap, unsigned long));
  110.                   break;
  111.                 case 'l':
  112.                   f++;
  113.                   switch (*f)
  114.                     {
  115.                     case 'd':
  116.                       sprintf (str, "%" PRId64,
  117.                                (int64_t) va_arg (ap, long long));
  118.                       break;
  119.                     case 'u':
  120.                       sprintf (str, "%" PRIu64,
  121.                                (uint64_t) va_arg (ap, unsigned long long));
  122.                       break;
  123.                     case 'x':
  124.                       sprintf (str, "%" PRIx64,
  125.                                (uint64_t) va_arg (ap, unsigned long long));
  126.                       break;
  127.                     case 'o':
  128.                       sprintf (str, "%" PRIo64,
  129.                                (uint64_t) va_arg (ap, unsigned long long));
  130.                       break;
  131.                     default:
  132.                       str = 0;
  133.                       break;
  134.                     }
  135.                   break;
  136.                 default:
  137.                   str = 0;
  138.                   break;
  139.                 }
  140.               break;
  141.             default:
  142.               str = 0;
  143.               break;
  144.             }

  145.           if (str)
  146.             {
  147.               buffer_grow (buffer, prev, f_old - prev - 1);
  148.               p = xml_escape_text (str);
  149.               buffer_grow_str (buffer, p);
  150.               xfree (p);
  151.               prev = f + 1;
  152.             }
  153.           percent = 0;
  154.         }
  155.       else if (*f == '%')
  156.         percent = 1;
  157.     }

  158.   buffer_grow_str (buffer, prev);
  159.   va_end (ap);
  160. }