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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [libiberty/] [vasprintf.c] - Diff between revs 154 and 816

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

Rev 154 Rev 816
/* 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, 2003 Free Software Foundation, Inc.
   Copyright (C) 1994, 2003 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., 51 Franklin Street - Fifth Floor,
not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA.  */
Boston, MA 02110-1301, USA.  */
 
 
#ifdef HAVE_CONFIG_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#include "config.h"
#endif
#endif
#include <ansidecl.h>
#include <ansidecl.h>
#include <stdarg.h>
#include <stdarg.h>
#if !defined (va_copy) && defined (__va_copy)
#if !defined (va_copy) && defined (__va_copy)
# define va_copy(d,s)  __va_copy((d),(s))
# define va_copy(d,s)  __va_copy((d),(s))
#endif
#endif
#include <stdio.h>
#include <stdio.h>
#ifdef HAVE_STRING_H
#ifdef HAVE_STRING_H
#include <string.h>
#include <string.h>
#endif
#endif
#ifdef HAVE_STDLIB_H
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#include <stdlib.h>
#else
#else
extern unsigned long strtoul ();
extern unsigned long strtoul ();
extern PTR malloc ();
extern PTR malloc ();
#endif
#endif
#include "libiberty.h"
#include "libiberty.h"
 
 
#ifdef TEST
#ifdef TEST
int global_total_width;
int global_total_width;
#endif
#endif
 
 
/*
/*
 
 
@deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args})
@deftypefn Extension int vasprintf (char **@var{resptr}, const char *@var{format}, va_list @var{args})
 
 
Like @code{vsprintf}, but instead of passing a pointer to a buffer,
Like @code{vsprintf}, but instead of passing a pointer to a buffer,
you pass a pointer to a pointer.  This function will compute the size
you pass a pointer to a pointer.  This function will compute the size
of the buffer needed, allocate memory with @code{malloc}, and store a
of the buffer needed, allocate memory with @code{malloc}, and store a
pointer to the allocated memory in @code{*@var{resptr}}.  The value
pointer to the allocated memory in @code{*@var{resptr}}.  The value
returned is the same as @code{vsprintf} would return.  If memory could
returned is the same as @code{vsprintf} would return.  If memory could
not be allocated, minus one is returned and @code{NULL} is stored in
not be allocated, minus one is returned and @code{NULL} is stored in
@code{*@var{resptr}}.
@code{*@var{resptr}}.
 
 
@end deftypefn
@end deftypefn
 
 
*/
*/
 
 
static int int_vasprintf (char **, const char *, va_list);
static int int_vasprintf (char **, const char *, va_list);
 
 
static int
static int
int_vasprintf (char **result, const char *format, va_list args)
int_vasprintf (char **result, const char *format, 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;
 
 
#ifdef va_copy
#ifdef va_copy
  va_copy (ap, args);
  va_copy (ap, args);
#else
#else
  memcpy ((PTR) &ap, (PTR) &args, sizeof (va_list));
  memcpy ((PTR) &ap, (PTR) &args, sizeof (va_list));
#endif
#endif
 
 
  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, (char **) &p, 10);
            total_width += strtoul (p, (char **) &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, (char **) &p, 10);
              total_width += strtoul (p, (char **) &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 va_copy
#ifdef va_copy
  va_end (ap);
  va_end (ap);
#endif
#endif
#ifdef TEST
#ifdef TEST
  global_total_width = total_width;
  global_total_width = total_width;
#endif
#endif
  *result = (char *) malloc (total_width);
  *result = (char *) malloc (total_width);
  if (*result != NULL)
  if (*result != NULL)
    return vsprintf (*result, format, args);
    return vsprintf (*result, format, args);
  else
  else
    return -1;
    return -1;
}
}
 
 
int
int
vasprintf (char **result, const char *format,
vasprintf (char **result, 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
static void ATTRIBUTE_PRINTF_1
static void ATTRIBUTE_PRINTF_1
checkit (const char *format, ...)
checkit (const char *format, ...)
{
{
  char *result;
  char *result;
  VA_OPEN (args, format);
  VA_OPEN (args, format);
  VA_FIXEDARG (args, const char *, format);
  VA_FIXEDARG (args, const char *, format);
  vasprintf (&result, format, args);
  vasprintf (&result, format, args);
  VA_CLOSE (args);
  VA_CLOSE (args);
 
 
  if (strlen (result) < (size_t) global_total_width)
  if (strlen (result) < (size_t) 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);
 
 
  free (result);
  free (result);
}
}
 
 
extern int main (void);
extern int main (void);
 
 
int
int
main (void)
main (void)
{
{
  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");
 
 
  return 0;
  return 0;
}
}
#endif /* TEST */
#endif /* TEST */
 
 

powered by: WebSVN 2.1.0

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