annotate lib/vasnprintf.h @ 276:23034db7dd8a 2.5

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