GDB (API)
/home/stan/gdb/src/gdb/common/buffer.c
Go to the documentation of this file.
00001 /* A simple growing buffer for GDB.
00002   
00003    Copyright (C) 2009-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #ifdef GDBSERVER
00021 #include "server.h"
00022 #else
00023 #include "defs.h"
00024 #endif
00025 
00026 #include "xml-utils.h"
00027 #include "buffer.h"
00028 #include "inttypes.h"
00029 
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #include <stdio.h>
00033 #include <stdint.h>
00034 
00035 void
00036 buffer_grow (struct buffer *buffer, const char *data, size_t size)
00037 {
00038   char *new_buffer;
00039   size_t new_buffer_size;
00040 
00041   if (size == 0)
00042     return;
00043 
00044   new_buffer_size = buffer->buffer_size;
00045 
00046   if (new_buffer_size == 0)
00047     new_buffer_size = 1;
00048 
00049   while (buffer->used_size + size > new_buffer_size)
00050     new_buffer_size *= 2;
00051   new_buffer = xrealloc (buffer->buffer, new_buffer_size);
00052   memcpy (new_buffer + buffer->used_size, data, size);
00053   buffer->buffer = new_buffer;
00054   buffer->buffer_size = new_buffer_size;
00055   buffer->used_size += size;
00056 }
00057 
00058 void
00059 buffer_free (struct buffer *buffer)
00060 {
00061   if (!buffer)
00062     return;
00063 
00064   xfree (buffer->buffer);
00065   buffer->buffer = NULL;
00066   buffer->buffer_size = 0;
00067   buffer->used_size = 0;
00068 }
00069 
00070 void
00071 buffer_init (struct buffer *buffer)
00072 {
00073   memset (buffer, 0, sizeof (*buffer));
00074 }
00075 
00076 char*
00077 buffer_finish (struct buffer *buffer)
00078 {
00079   char *ret = buffer->buffer;
00080   buffer->buffer = NULL;
00081   buffer->buffer_size = 0;
00082   buffer->used_size = 0;
00083   return ret;
00084 }
00085 
00086 void
00087 buffer_xml_printf (struct buffer *buffer, const char *format, ...)
00088 {
00089   va_list ap;
00090   const char *f;
00091   const char *prev;
00092   int percent = 0;
00093 
00094   va_start (ap, format);
00095 
00096   prev = format;
00097   for (f = format; *f; f++)
00098     {
00099       if (percent)
00100         {
00101           char buf[32];
00102           char *p;
00103           char *str = buf;
00104           const char *f_old = f;
00105           
00106           switch (*f)
00107             {
00108             case 's':
00109               str = va_arg (ap, char *);
00110               break;
00111             case 'd':
00112               sprintf (str, "%d", va_arg (ap, int));
00113               break;
00114             case 'u':
00115               sprintf (str, "%u", va_arg (ap, unsigned int));
00116               break;
00117             case 'x':
00118               sprintf (str, "%x", va_arg (ap, unsigned int));
00119               break;
00120             case 'o':
00121               sprintf (str, "%o", va_arg (ap, unsigned int));
00122               break;
00123             case 'l':
00124               f++;
00125               switch (*f)
00126                 {
00127                 case 'd':
00128                   sprintf (str, "%ld", va_arg (ap, long));
00129                   break;
00130                 case 'u':
00131                   sprintf (str, "%lu", va_arg (ap, unsigned long));
00132                   break;
00133                 case 'x':
00134                   sprintf (str, "%lx", va_arg (ap, unsigned long));
00135                   break;
00136                 case 'o':
00137                   sprintf (str, "%lo", va_arg (ap, unsigned long));
00138                   break;
00139                 case 'l':
00140                   f++;
00141                   switch (*f)
00142                     {
00143                     case 'd':
00144                       sprintf (str, "%" PRId64,
00145                                (int64_t) va_arg (ap, long long));
00146                       break;
00147                     case 'u':
00148                       sprintf (str, "%" PRIu64,
00149                                (uint64_t) va_arg (ap, unsigned long long));
00150                       break;
00151                     case 'x':
00152                       sprintf (str, "%" PRIx64,
00153                                (uint64_t) va_arg (ap, unsigned long long));
00154                       break;
00155                     case 'o':
00156                       sprintf (str, "%" PRIo64,
00157                                (uint64_t) va_arg (ap, unsigned long long));
00158                       break;
00159                     default:
00160                       str = 0;
00161                       break;
00162                     }
00163                   break;
00164                 default:
00165                   str = 0;
00166                   break;
00167                 }
00168               break;
00169             default:
00170               str = 0;
00171               break;
00172             }
00173 
00174           if (str)
00175             {
00176               buffer_grow (buffer, prev, f_old - prev - 1);
00177               p = xml_escape_text (str);
00178               buffer_grow_str (buffer, p);
00179               xfree (p);
00180               prev = f + 1;
00181             }
00182           percent = 0;
00183         }
00184       else if (*f == '%')
00185         percent = 1;
00186     }
00187 
00188   buffer_grow_str (buffer, prev);
00189   va_end (ap);
00190 }
00191 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines