xref: /haiku/src/system/libroot/posix/glibc/stdio-common/printf.h (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /* Copyright (C) 1991-1993,1995-1999,2000,2001 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18 
19 #ifndef	_PRINTF_H
20 
21 #define	_PRINTF_H	1
22 #include <features.h>
23 
24 __BEGIN_DECLS
25 
26 #define	__need_FILE
27 #include <stdio.h>
28 #define	__need_size_t
29 #define __need_wchar_t
30 #include <stddef.h>
31 
32 
33 struct printf_info
34 {
35   int prec;			/* Precision.  */
36   int width;			/* Width.  */
37   wchar_t spec;			/* Format letter.  */
38   unsigned int is_long_double:1;/* L flag.  */
39   unsigned int is_short:1;	/* h flag.  */
40   unsigned int is_long:1;	/* l flag.  */
41   unsigned int alt:1;		/* # flag.  */
42   unsigned int space:1;		/* Space flag.  */
43   unsigned int left:1;		/* - flag.  */
44   unsigned int showsign:1;	/* + flag.  */
45   unsigned int group:1;		/* ' flag.  */
46   unsigned int extra:1;		/* For special use.  */
47   unsigned int is_char:1;	/* hh flag.  */
48   unsigned int wide:1;		/* Nonzero for wide character streams.  */
49   unsigned int i18n:1;		/* I flag.  */
50   wchar_t pad;			/* Padding character.  */
51 };
52 
53 
54 /* Type of a printf specifier-handler function.
55    STREAM is the FILE on which to write output.
56    INFO gives information about the format specification.
57    ARGS is a vector of pointers to the argument data;
58    the number of pointers will be the number returned
59    by the associated arginfo function for the same INFO.
60 
61    The function should return the number of characters written,
62    or -1 for errors.  */
63 
64 typedef int printf_function (FILE *__stream,
65 			     __const struct printf_info *__info,
66 			     __const void *__const *__args);
67 
68 /* Type of a printf specifier-arginfo function.
69    INFO gives information about the format specification.
70    N, ARGTYPES, and return value are as for parse_printf_format.  */
71 
72 typedef int printf_arginfo_function (__const struct printf_info *__info,
73 				     size_t __n, int *__argtypes);
74 
75 
76 /* Register FUNC to be called to format SPEC specifiers; ARGINFO must be
77    specified to determine how many arguments a SPEC conversion requires and
78    what their types are.  */
79 
80 extern int register_printf_function (int __spec, printf_function __func,
81 				     printf_arginfo_function __arginfo);
82 
83 
84 /* Parse FMT, and fill in N elements of ARGTYPES with the
85    types needed for the conversions FMT specifies.  Returns
86    the number of arguments required by FMT.
87 
88    The ARGINFO function registered with a user-defined format is passed a
89    `struct printf_info' describing the format spec being parsed.  A width
90    or precision of INT_MIN means a `*' was used to indicate that the
91    width/precision will come from an arg.  The function should fill in the
92    array it is passed with the types of the arguments it wants, and return
93    the number of arguments it wants.  */
94 
95 extern size_t parse_printf_format (__const char *__restrict __fmt, size_t __n,
96 				   int *__restrict __argtypes) __THROW;
97 
98 
99 /* Codes returned by `parse_printf_format' for basic types.
100 
101    These values cover all the standard format specifications.
102    Users can add new values after PA_LAST for their own types.  */
103 
104 enum
105 {				/* C type: */
106   PA_INT,			/* int */
107   PA_CHAR,			/* int, cast to char */
108   PA_WCHAR,			/* wide char */
109   PA_STRING,			/* const char *, a '\0'-terminated string */
110   PA_WSTRING,			/* const wchar_t *, wide character string */
111   PA_POINTER,			/* void * */
112   PA_FLOAT,			/* float */
113   PA_DOUBLE,			/* double */
114   PA_LAST
115 };
116 
117 /* Flag bits that can be set in a type returned by `parse_printf_format'.  */
118 #define	PA_FLAG_MASK		0xff00
119 #define	PA_FLAG_LONG_LONG	(1 << 8)
120 #define	PA_FLAG_LONG_DOUBLE	PA_FLAG_LONG_LONG
121 #define	PA_FLAG_LONG		(1 << 9)
122 #define	PA_FLAG_SHORT		(1 << 10)
123 #define	PA_FLAG_PTR		(1 << 11)
124 
125 
126 
127 /* Function which can be registered as `printf'-handlers.  */
128 
129 /* Print floating point value using using abbreviations for the orders
130    of magnitude used for numbers ('k' for kilo, 'm' for mega etc).  If
131    the format specifier is a uppercase character powers of 1000 are
132    used.  Otherwise powers of 1024.  */
133 extern int printf_size (FILE *__restrict __fp,
134 			__const struct printf_info *__info,
135 			__const void *__const *__restrict __args) __THROW;
136 
137 /* This is the appropriate argument information function for `printf_size'.  */
138 extern int printf_size_info (__const struct printf_info *__restrict
139 			     __info, size_t __n, int *__restrict __argtypes)
140      __THROW;
141 
142 
143 __END_DECLS
144 
145 #endif /* printf.h  */
146