annotate lib/vasnprintf.h @ 265:68fbca173508 2.6

Added generated files for release
author lost
date Tue, 22 Dec 2009 05:31:23 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
265
68fbca173508 Added generated files for release
lost
parents:
diff changeset
1 /* vsprintf with automatic memory allocation.
68fbca173508 Added generated files for release
lost
parents:
diff changeset
2 Copyright (C) 2002-2004, 2007-2008 Free Software Foundation, Inc.
68fbca173508 Added generated files for release
lost
parents:
diff changeset
3
68fbca173508 Added generated files for release
lost
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
68fbca173508 Added generated files for release
lost
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
68fbca173508 Added generated files for release
lost
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
7 any later version.
68fbca173508 Added generated files for release
lost
parents:
diff changeset
8
68fbca173508 Added generated files for release
lost
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
68fbca173508 Added generated files for release
lost
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
68fbca173508 Added generated files for release
lost
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68fbca173508 Added generated files for release
lost
parents:
diff changeset
12 GNU General Public License for more details.
68fbca173508 Added generated files for release
lost
parents:
diff changeset
13
68fbca173508 Added generated files for release
lost
parents:
diff changeset
14 You should have received a copy of the GNU General Public License along
68fbca173508 Added generated files for release
lost
parents:
diff changeset
15 with this program; if not, write to the Free Software Foundation,
68fbca173508 Added generated files for release
lost
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
17
68fbca173508 Added generated files for release
lost
parents:
diff changeset
18 #ifndef _VASNPRINTF_H
68fbca173508 Added generated files for release
lost
parents:
diff changeset
19 #define _VASNPRINTF_H
68fbca173508 Added generated files for release
lost
parents:
diff changeset
20
68fbca173508 Added generated files for release
lost
parents:
diff changeset
21 /* Get va_list. */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
22 #include <stdarg.h>
68fbca173508 Added generated files for release
lost
parents:
diff changeset
23
68fbca173508 Added generated files for release
lost
parents:
diff changeset
24 /* Get size_t. */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
25 #include <stddef.h>
68fbca173508 Added generated files for release
lost
parents:
diff changeset
26
68fbca173508 Added generated files for release
lost
parents:
diff changeset
27 #ifndef __attribute__
68fbca173508 Added generated files for release
lost
parents:
diff changeset
28 /* This feature is available in gcc versions 2.5 and later. */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
29 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
30 # define __attribute__(Spec) /* empty */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
31 # endif
68fbca173508 Added generated files for release
lost
parents:
diff changeset
32 /* The __-protected variants of `format' and `printf' attributes
68fbca173508 Added generated files for release
lost
parents:
diff changeset
33 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
34 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
35 # define __format__ format
68fbca173508 Added generated files for release
lost
parents:
diff changeset
36 # define __printf__ printf
68fbca173508 Added generated files for release
lost
parents:
diff changeset
37 # endif
68fbca173508 Added generated files for release
lost
parents:
diff changeset
38 #endif
68fbca173508 Added generated files for release
lost
parents:
diff changeset
39
68fbca173508 Added generated files for release
lost
parents:
diff changeset
40 #ifdef __cplusplus
68fbca173508 Added generated files for release
lost
parents:
diff changeset
41 extern "C" {
68fbca173508 Added generated files for release
lost
parents:
diff changeset
42 #endif
68fbca173508 Added generated files for release
lost
parents:
diff changeset
43
68fbca173508 Added generated files for release
lost
parents:
diff changeset
44 /* Write formatted output to a string dynamically allocated with malloc().
68fbca173508 Added generated files for release
lost
parents:
diff changeset
45 You can pass a preallocated buffer for the result in RESULTBUF and its
68fbca173508 Added generated files for release
lost
parents:
diff changeset
46 size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
68fbca173508 Added generated files for release
lost
parents:
diff changeset
47 If successful, return the address of the string (this may be = RESULTBUF
68fbca173508 Added generated files for release
lost
parents:
diff changeset
48 if no dynamic memory allocation was necessary) and set *LENGTHP to the
68fbca173508 Added generated files for release
lost
parents:
diff changeset
49 number of resulting bytes, excluding the trailing NUL. Upon error, set
68fbca173508 Added generated files for release
lost
parents:
diff changeset
50 errno and return NULL.
68fbca173508 Added generated files for release
lost
parents:
diff changeset
51
68fbca173508 Added generated files for release
lost
parents:
diff changeset
52 When dynamic memory allocation occurs, the preallocated buffer is left
68fbca173508 Added generated files for release
lost
parents:
diff changeset
53 alone (with possibly modified contents). This makes it possible to use
68fbca173508 Added generated files for release
lost
parents:
diff changeset
54 a statically allocated or stack-allocated buffer, like this:
68fbca173508 Added generated files for release
lost
parents:
diff changeset
55
68fbca173508 Added generated files for release
lost
parents:
diff changeset
56 char buf[100];
68fbca173508 Added generated files for release
lost
parents:
diff changeset
57 size_t len = sizeof (buf);
68fbca173508 Added generated files for release
lost
parents:
diff changeset
58 char *output = vasnprintf (buf, &len, format, args);
68fbca173508 Added generated files for release
lost
parents:
diff changeset
59 if (output == NULL)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
60 ... error handling ...;
68fbca173508 Added generated files for release
lost
parents:
diff changeset
61 else
68fbca173508 Added generated files for release
lost
parents:
diff changeset
62 {
68fbca173508 Added generated files for release
lost
parents:
diff changeset
63 ... use the output string ...;
68fbca173508 Added generated files for release
lost
parents:
diff changeset
64 if (output != buf)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
65 free (output);
68fbca173508 Added generated files for release
lost
parents:
diff changeset
66 }
68fbca173508 Added generated files for release
lost
parents:
diff changeset
67 */
68fbca173508 Added generated files for release
lost
parents:
diff changeset
68 #if REPLACE_VASNPRINTF
68fbca173508 Added generated files for release
lost
parents:
diff changeset
69 # define asnprintf rpl_asnprintf
68fbca173508 Added generated files for release
lost
parents:
diff changeset
70 # define vasnprintf rpl_vasnprintf
68fbca173508 Added generated files for release
lost
parents:
diff changeset
71 #endif
68fbca173508 Added generated files for release
lost
parents:
diff changeset
72 extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
73 __attribute__ ((__format__ (__printf__, 3, 4)));
68fbca173508 Added generated files for release
lost
parents:
diff changeset
74 extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
68fbca173508 Added generated files for release
lost
parents:
diff changeset
75 __attribute__ ((__format__ (__printf__, 3, 0)));
68fbca173508 Added generated files for release
lost
parents:
diff changeset
76
68fbca173508 Added generated files for release
lost
parents:
diff changeset
77 #ifdef __cplusplus
68fbca173508 Added generated files for release
lost
parents:
diff changeset
78 }
68fbca173508 Added generated files for release
lost
parents:
diff changeset
79 #endif
68fbca173508 Added generated files for release
lost
parents:
diff changeset
80
68fbca173508 Added generated files for release
lost
parents:
diff changeset
81 #endif /* _VASNPRINTF_H */