Blame gnulib-tests/vasnprintf.h

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