Mercurial > hg > octave-shane > gnulib-hg
annotate lib/vasnprintf.h @ 7419:0c560af115cb
.
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Sat, 07 Oct 2006 07:42:41 +0000 |
parents | a48fb0e98c8c |
children | 557787fcc6cd |
rev | line source |
---|---|
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* vsprintf with automatic memory allocation. |
5297
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
2 Copyright (C) 2002-2004 Free Software Foundation, Inc. |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
4 This program is free software; you can redistribute it and/or modify |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
6 the Free Software Foundation; either version 2, or (at your option) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 any later version. |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
12 GNU General Public License for more details. |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
14 You should have received a copy of the GNU General Public License along |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4224
diff
changeset
|
15 with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5297
diff
changeset
|
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 #ifndef _VASNPRINTF_H |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 #define _VASNPRINTF_H |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 /* Get va_list. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 #include <stdarg.h> |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 /* Get size_t. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 #include <stddef.h> |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 #ifndef __attribute__ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 /* This feature is available in gcc versions 2.5 and later. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 # define __attribute__(Spec) /* empty */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
31 # endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
32 /* The __-protected variants of `format' and `printf' attributes |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
33 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 # define __format__ format |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
36 # define __printf__ printf |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
37 # endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
38 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 #ifdef __cplusplus |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 extern "C" { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 /* Write formatted output to a string dynamically allocated with malloc(). |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 You can pass a preallocated buffer for the result in RESULTBUF and its |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
46 size in *LENGTHP; otherwise you pass RESULTBUF = NULL. |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
47 If successful, return the address of the string (this may be = RESULTBUF |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 if no dynamic memory allocation was necessary) and set *LENGTHP to the |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 number of resulting bytes, excluding the trailing NUL. Upon error, set |
5297
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
50 errno and return NULL. |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
51 |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
52 When dynamic memory allocation occurs, the preallocated buffer is left |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
53 alone (with possibly modified contents). This makes it possible to use |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
54 a statically allocated or stack-allocated buffer, like this: |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
55 |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
56 char buf[100]; |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
57 size_t len = sizeof (buf); |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
58 char *output = vasnprintf (buf, &len, format, args); |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
59 if (output == NULL) |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
60 ... error handling ...; |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
61 else |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
62 { |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
63 ... use the output string ...; |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
64 if (output != buf) |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
65 free (output); |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
66 } |
c0fb011695d9
Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents:
4440
diff
changeset
|
67 */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
69 __attribute__ ((__format__ (__printf__, 3, 4))); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 __attribute__ ((__format__ (__printf__, 3, 0))); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
72 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
73 #ifdef __cplusplus |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
74 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
76 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
77 #endif /* _VASNPRINTF_H */ |