Blame gnulib/lib/vasnprintf.h

Packit eba2e2
/* vsprintf with automatic memory allocation.
Packit eba2e2
   Copyright (C) 2002-2004, 2007-2014 Free Software Foundation, Inc.
Packit eba2e2
Packit eba2e2
   This program is free software; you can redistribute it and/or modify
Packit eba2e2
   it under the terms of the GNU General Public License as published by
Packit eba2e2
   the Free Software Foundation; either version 3, or (at your option)
Packit eba2e2
   any later version.
Packit eba2e2
Packit eba2e2
   This program is distributed in the hope that it will be useful,
Packit eba2e2
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit eba2e2
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit eba2e2
   GNU General Public License for more details.
Packit eba2e2
Packit eba2e2
   You should have received a copy of the GNU General Public License along
Packit eba2e2
   with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit eba2e2
Packit eba2e2
#ifndef _VASNPRINTF_H
Packit eba2e2
#define _VASNPRINTF_H
Packit eba2e2
Packit eba2e2
/* Get va_list.  */
Packit eba2e2
#include <stdarg.h>
Packit eba2e2
Packit eba2e2
/* Get size_t.  */
Packit eba2e2
#include <stddef.h>
Packit eba2e2
Packit eba2e2
/* The __attribute__ feature is available in gcc versions 2.5 and later.
Packit eba2e2
   The __-protected variants of the attributes 'format' and 'printf' are
Packit eba2e2
   accepted by gcc versions 2.6.4 (effectively 2.7) and later.
Packit eba2e2
   We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
Packit eba2e2
   gnulib and libintl do '#define printf __printf__' when they override
Packit eba2e2
   the 'printf' function.  */
Packit eba2e2
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
Packit eba2e2
# define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
Packit eba2e2
#else
Packit eba2e2
# define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
Packit eba2e2
#endif
Packit eba2e2
Packit eba2e2
#ifdef __cplusplus
Packit eba2e2
extern "C" {
Packit eba2e2
#endif
Packit eba2e2
Packit eba2e2
/* Write formatted output to a string dynamically allocated with malloc().
Packit eba2e2
   You can pass a preallocated buffer for the result in RESULTBUF and its
Packit eba2e2
   size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
Packit eba2e2
   If successful, return the address of the string (this may be = RESULTBUF
Packit eba2e2
   if no dynamic memory allocation was necessary) and set *LENGTHP to the
Packit eba2e2
   number of resulting bytes, excluding the trailing NUL.  Upon error, set
Packit eba2e2
   errno and return NULL.
Packit eba2e2
Packit eba2e2
   When dynamic memory allocation occurs, the preallocated buffer is left
Packit eba2e2
   alone (with possibly modified contents).  This makes it possible to use
Packit eba2e2
   a statically allocated or stack-allocated buffer, like this:
Packit eba2e2
Packit eba2e2
          char buf[100];
Packit eba2e2
          size_t len = sizeof (buf);
Packit eba2e2
          char *output = vasnprintf (buf, &len, format, args);
Packit eba2e2
          if (output == NULL)
Packit eba2e2
            ... error handling ...;
Packit eba2e2
          else
Packit eba2e2
            {
Packit eba2e2
              ... use the output string ...;
Packit eba2e2
              if (output != buf)
Packit eba2e2
                free (output);
Packit eba2e2
            }
Packit eba2e2
  */
Packit eba2e2
#if REPLACE_VASNPRINTF
Packit eba2e2
# define asnprintf rpl_asnprintf
Packit eba2e2
# define vasnprintf rpl_vasnprintf
Packit eba2e2
#endif
Packit eba2e2
extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
Packit eba2e2
       _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 4));
Packit eba2e2
extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
Packit eba2e2
       _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 0));
Packit eba2e2
Packit eba2e2
#ifdef __cplusplus
Packit eba2e2
}
Packit eba2e2
#endif
Packit eba2e2
Packit eba2e2
#endif /* _VASNPRINTF_H */