Back to index

courier  0.68.2
vasnprintf.h
Go to the documentation of this file.
00001 /* vsprintf with automatic memory allocation.
00002    Copyright (C) 2002-2004 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify it
00005    under the terms of the GNU Library General Public License as published
00006    by the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public
00015    License along with this program; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00017    USA.  */
00018 
00019 #ifndef _VASNPRINTF_H
00020 #define _VASNPRINTF_H
00021 
00022 /* Get va_list.  */
00023 #include <stdarg.h>
00024 
00025 /* Get size_t.  */
00026 #include <stddef.h>
00027 
00028 #ifndef __attribute__
00029 /* This feature is available in gcc versions 2.5 and later.  */
00030 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
00031 #  define __attribute__(Spec) /* empty */
00032 # endif
00033 /* The __-protected variants of `format' and `printf' attributes
00034    are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
00035 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
00036 #  define __format__ format
00037 #  define __printf__ printf
00038 # endif
00039 #endif
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 /* Write formatted output to a string dynamically allocated with malloc().
00046    You can pass a preallocated buffer for the result in RESULTBUF and its
00047    size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
00048    If successful, return the address of the string (this may be = RESULTBUF
00049    if no dynamic memory allocation was necessary) and set *LENGTHP to the
00050    number of resulting bytes, excluding the trailing NUL.  Upon error, set
00051    errno and return NULL.
00052 
00053    When dynamic memory allocation occurs, the preallocated buffer is left
00054    alone (with possibly modified contents).  This makes it possible to use
00055    a statically allocated or stack-allocated buffer, like this:
00056 
00057           char buf[100];
00058           size_t len = sizeof (buf);
00059           char *output = vasnprintf (buf, &len, format, args);
00060           if (output == NULL)
00061             ... error handling ...;
00062           else
00063             {
00064               ... use the output string ...;
00065               if (output != buf)
00066                 free (output);
00067             }
00068   */
00069 extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
00070        __attribute__ ((__format__ (__printf__, 3, 4)));
00071 extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
00072        __attribute__ ((__format__ (__printf__, 3, 0)));
00073 
00074 #ifdef __cplusplus
00075 }
00076 #endif
00077 
00078 #endif /* _VASNPRINTF_H */