OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [libiberty/] [vasprintf.c] - Diff between revs 107 and 1765

Only display areas with differences | Details | Blame | View Log

Rev 107 Rev 1765
/* Like vsprintf but provides a pointer to malloc'd storage, which must
/* Like vsprintf but provides a pointer to malloc'd storage, which must
   be freed by the caller.
   be freed by the caller.
   Copyright (C) 1994 Free Software Foundation, Inc.
   Copyright (C) 1994 Free Software Foundation, Inc.
 
 
This file is part of the libiberty library.
This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
Libiberty is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
version 2 of the License, or (at your option) any later version.
 
 
Libiberty is distributed in the hope that it will be useful,
Libiberty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.
Library General Public License for more details.
 
 
You should have received a copy of the GNU Library General Public
You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB.  If
License along with libiberty; see the file COPYING.LIB.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Boston, MA 02111-1307, USA.  */
 
 
#ifdef __STDC__
#ifdef __STDC__
#include <stdarg.h>
#include <stdarg.h>
#else
#else
#include <varargs.h>
#include <varargs.h>
#endif
#endif
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
#include <ansidecl.h>
#include <ansidecl.h>
 
 
#ifdef TEST
#ifdef TEST
int global_total_width;
int global_total_width;
#endif
#endif
 
 
unsigned long strtoul ();
unsigned long strtoul ();
char *malloc ();
char *malloc ();
 
 
static int
static int
int_vasprintf (result, format, args)
int_vasprintf (result, format, args)
     char **result;
     char **result;
     const char *format;
     const char *format;
     va_list *args;
     va_list *args;
{
{
  const char *p = format;
  const char *p = format;
  /* Add one to make sure that it is never zero, which might cause malloc
  /* Add one to make sure that it is never zero, which might cause malloc
     to return NULL.  */
     to return NULL.  */
  int total_width = strlen (format) + 1;
  int total_width = strlen (format) + 1;
  va_list ap;
  va_list ap;
 
 
  memcpy ((PTR) &ap, (PTR) args, sizeof (va_list));
  memcpy ((PTR) &ap, (PTR) args, sizeof (va_list));
 
 
  while (*p != '\0')
  while (*p != '\0')
    {
    {
      if (*p++ == '%')
      if (*p++ == '%')
        {
        {
          while (strchr ("-+ #0", *p))
          while (strchr ("-+ #0", *p))
            ++p;
            ++p;
          if (*p == '*')
          if (*p == '*')
            {
            {
              ++p;
              ++p;
              total_width += abs (va_arg (ap, int));
              total_width += abs (va_arg (ap, int));
            }
            }
          else
          else
            total_width += strtoul (p, &p, 10);
            total_width += strtoul (p, &p, 10);
          if (*p == '.')
          if (*p == '.')
            {
            {
              ++p;
              ++p;
              if (*p == '*')
              if (*p == '*')
                {
                {
                  ++p;
                  ++p;
                  total_width += abs (va_arg (ap, int));
                  total_width += abs (va_arg (ap, int));
                }
                }
              else
              else
              total_width += strtoul (p, &p, 10);
              total_width += strtoul (p, &p, 10);
            }
            }
          while (strchr ("hlL", *p))
          while (strchr ("hlL", *p))
            ++p;
            ++p;
          /* Should be big enough for any format specifier except %s and floats.  */
          /* Should be big enough for any format specifier except %s and floats.  */
          total_width += 30;
          total_width += 30;
          switch (*p)
          switch (*p)
            {
            {
            case 'd':
            case 'd':
            case 'i':
            case 'i':
            case 'o':
            case 'o':
            case 'u':
            case 'u':
            case 'x':
            case 'x':
            case 'X':
            case 'X':
            case 'c':
            case 'c':
              (void) va_arg (ap, int);
              (void) va_arg (ap, int);
              break;
              break;
            case 'f':
            case 'f':
            case 'e':
            case 'e':
            case 'E':
            case 'E':
            case 'g':
            case 'g':
            case 'G':
            case 'G':
              (void) va_arg (ap, double);
              (void) va_arg (ap, double);
              /* Since an ieee double can have an exponent of 307, we'll
              /* Since an ieee double can have an exponent of 307, we'll
                 make the buffer wide enough to cover the gross case. */
                 make the buffer wide enough to cover the gross case. */
              total_width += 307;
              total_width += 307;
              break;
              break;
            case 's':
            case 's':
              total_width += strlen (va_arg (ap, char *));
              total_width += strlen (va_arg (ap, char *));
              break;
              break;
            case 'p':
            case 'p':
            case 'n':
            case 'n':
              (void) va_arg (ap, char *);
              (void) va_arg (ap, char *);
              break;
              break;
            }
            }
          p++;
          p++;
        }
        }
    }
    }
#ifdef TEST
#ifdef TEST
  global_total_width = total_width;
  global_total_width = total_width;
#endif
#endif
  *result = malloc (total_width);
  *result = malloc (total_width);
  if (*result != NULL)
  if (*result != NULL)
    return vsprintf (*result, format, *args);
    return vsprintf (*result, format, *args);
  else
  else
    return 0;
    return 0;
}
}
 
 
int
int
vasprintf (result, format, args)
vasprintf (result, format, args)
     char **result;
     char **result;
     const char *format;
     const char *format;
#if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
#if defined (_BSD_VA_LIST_) && defined (__FreeBSD__)
     _BSD_VA_LIST_ args;
     _BSD_VA_LIST_ args;
#else
#else
     va_list args;
     va_list args;
#endif
#endif
{
{
  return int_vasprintf (result, format, &args);
  return int_vasprintf (result, format, &args);
}
}
 
 
#ifdef TEST
#ifdef TEST
void
void
checkit
checkit
#ifdef __STDC__
#ifdef __STDC__
     (const char* format, ...)
     (const char* format, ...)
#else
#else
     (va_alist)
     (va_alist)
     va_dcl
     va_dcl
#endif
#endif
{
{
  va_list args;
  va_list args;
  char *result;
  char *result;
 
 
#ifdef __STDC__
#ifdef __STDC__
  va_start (args, format);
  va_start (args, format);
#else
#else
  char *format;
  char *format;
  va_start (args);
  va_start (args);
  format = va_arg (args, char *);
  format = va_arg (args, char *);
#endif
#endif
  vasprintf (&result, format, args);
  vasprintf (&result, format, args);
  if (strlen (result) < global_total_width)
  if (strlen (result) < global_total_width)
    printf ("PASS: ");
    printf ("PASS: ");
  else
  else
    printf ("FAIL: ");
    printf ("FAIL: ");
  printf ("%d %s\n", global_total_width, result);
  printf ("%d %s\n", global_total_width, result);
}
}
 
 
int
int
main ()
main ()
{
{
  checkit ("%d", 0x12345678);
  checkit ("%d", 0x12345678);
  checkit ("%200d", 5);
  checkit ("%200d", 5);
  checkit ("%.300d", 6);
  checkit ("%.300d", 6);
  checkit ("%100.150d", 7);
  checkit ("%100.150d", 7);
  checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
  checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\
777777777777777777333333333333366666666666622222222222777777777777733333");
777777777777777777333333333333366666666666622222222222777777777777733333");
  checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
  checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx");
}
}
#endif /* TEST */
#endif /* TEST */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.