3 /* Formatted output to strings.
4 Copyright (C) 1999-2000, 2002-2003 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Library General Public License as published
8 by the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 # include "wprintf-parse.h"
29 # include "printf-parse.h"
32 /* Get size_t, NULL. */
36 #if HAVE_STDINT_H_WITH_UINTMAX
39 #if HAVE_INTTYPES_H_WITH_UINTMAX
40 # include <inttypes.h>
43 /* malloc(), realloc(), free(). */
46 /* Checked size_t computations. */
50 # define PRINTF_PARSE wprintf_parse
51 # define CHAR_T wchar_t
52 # define DIRECTIVE wchar_t_directive
53 # define DIRECTIVES wchar_t_directives
55 # define PRINTF_PARSE printf_parse
57 # define DIRECTIVE char_directive
58 # define DIRECTIVES char_directives
65 PRINTF_PARSE (const CHAR_T
*format
, DIRECTIVES
*d
, arguments
*a
)
67 const CHAR_T
*cp
= format
; /* pointer into format */
68 size_t arg_posn
= 0; /* number of regular arguments consumed */
69 size_t d_allocated
; /* allocated elements of d->dir */
70 size_t a_allocated
; /* allocated elements of a->arg */
71 size_t max_width_length
= 0;
72 size_t max_precision_length
= 0;
76 d
->dir
= malloc (d_allocated
* sizeof (DIRECTIVE
));
85 #define REGISTER_ARG(_index_,_type_) \
87 size_t n = (_index_); \
88 if (n >= a_allocated) \
93 a_allocated = xtimes (a_allocated, 2); \
94 if (a_allocated <= n) \
95 a_allocated = xsum (n, 1); \
96 memory_size = xtimes (a_allocated, sizeof (argument)); \
97 if (size_overflow_p (memory_size)) \
98 /* Overflow, would lead to out of memory. */ \
101 ? realloc (a->arg, memory_size) \
102 : malloc (memory_size)); \
103 if (memory == NULL) \
104 /* Out of memory. */ \
108 while (a->count <= n) \
109 a->arg[a->count++].type = TYPE_NONE; \
110 if (a->arg[n].type == TYPE_NONE) \
111 a->arg[n].type = (_type_); \
112 else if (a->arg[n].type != (_type_)) \
113 /* Ambiguous type for positional argument. */ \
122 size_t arg_index
= ARG_NONE
;
123 DIRECTIVE
*dp
= &d
->dir
[d
->count
];/* pointer to next directive */
125 /* Initialize the next directive. */
126 dp
->dir_start
= cp
- 1;
128 dp
->width_start
= NULL
;
129 dp
->width_end
= NULL
;
130 dp
->width_arg_index
= ARG_NONE
;
131 dp
->precision_start
= NULL
;
132 dp
->precision_end
= NULL
;
133 dp
->precision_arg_index
= ARG_NONE
;
134 dp
->arg_index
= ARG_NONE
;
136 /* Test for positional argument. */
137 if (*cp
>= '0' && *cp
<= '9')
141 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
147 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
148 n
= xsum (xtimes (n
, 10), *np
- '0');
150 /* Positional argument 0. */
152 if (size_overflow_p (n
))
153 /* n too large, would lead to out of memory later. */
160 /* Read the flags. */
165 dp
->flags
|= FLAG_GROUP
;
170 dp
->flags
|= FLAG_LEFT
;
175 dp
->flags
|= FLAG_SHOWSIGN
;
180 dp
->flags
|= FLAG_SPACE
;
185 dp
->flags
|= FLAG_ALT
;
190 dp
->flags
|= FLAG_ZERO
;
197 /* Parse the field width. */
200 dp
->width_start
= cp
;
203 if (max_width_length
< 1)
204 max_width_length
= 1;
206 /* Test for positional argument. */
207 if (*cp
>= '0' && *cp
<= '9')
211 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
217 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
218 n
= xsum (xtimes (n
, 10), *np
- '0');
220 /* Positional argument 0. */
222 if (size_overflow_p (n
))
223 /* n too large, would lead to out of memory later. */
225 dp
->width_arg_index
= n
- 1;
229 if (dp
->width_arg_index
== ARG_NONE
)
231 dp
->width_arg_index
= arg_posn
++;
232 if (dp
->width_arg_index
== ARG_NONE
)
233 /* arg_posn wrapped around. */
236 REGISTER_ARG (dp
->width_arg_index
, TYPE_INT
);
238 else if (*cp
>= '0' && *cp
<= '9')
242 dp
->width_start
= cp
;
243 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
246 width_length
= dp
->width_end
- dp
->width_start
;
247 if (max_width_length
< width_length
)
248 max_width_length
= width_length
;
251 /* Parse the precision. */
257 dp
->precision_start
= cp
- 1;
259 dp
->precision_end
= cp
;
260 if (max_precision_length
< 2)
261 max_precision_length
= 2;
263 /* Test for positional argument. */
264 if (*cp
>= '0' && *cp
<= '9')
268 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
274 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
275 n
= xsum (xtimes (n
, 10), *np
- '0');
277 /* Positional argument 0. */
279 if (size_overflow_p (n
))
280 /* n too large, would lead to out of memory
283 dp
->precision_arg_index
= n
- 1;
287 if (dp
->precision_arg_index
== ARG_NONE
)
289 dp
->precision_arg_index
= arg_posn
++;
290 if (dp
->precision_arg_index
== ARG_NONE
)
291 /* arg_posn wrapped around. */
294 REGISTER_ARG (dp
->precision_arg_index
, TYPE_INT
);
298 size_t precision_length
;
300 dp
->precision_start
= cp
- 1;
301 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
303 dp
->precision_end
= cp
;
304 precision_length
= dp
->precision_end
- dp
->precision_start
;
305 if (max_precision_length
< precision_length
)
306 max_precision_length
= precision_length
;
313 /* Parse argument type/size specifiers. */
321 flags
|= (1 << (flags
& 1));
337 if (sizeof (intmax_t) > sizeof (long))
339 /* intmax_t = long long */
342 else if (sizeof (intmax_t) > sizeof (int))
344 /* intmax_t = long */
350 else if (*cp
== 'z' || *cp
== 'Z')
352 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
353 because the warning facility in gcc-2.95.2 understands
354 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
355 if (sizeof (size_t) > sizeof (long))
357 /* size_t = long long */
360 else if (sizeof (size_t) > sizeof (int))
369 if (sizeof (ptrdiff_t) > sizeof (long))
371 /* ptrdiff_t = long long */
374 else if (sizeof (ptrdiff_t) > sizeof (int))
376 /* ptrdiff_t = long */
385 /* Read the conversion character. */
390 #ifdef HAVE_LONG_LONG
391 if (flags
>= 16 || (flags
& 4))
392 type
= TYPE_LONGLONGINT
;
404 case 'o': case 'u': case 'x': case 'X':
405 #ifdef HAVE_LONG_LONG
406 if (flags
>= 16 || (flags
& 4))
407 type
= TYPE_ULONGLONGINT
;
411 type
= TYPE_ULONGINT
;
419 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
421 #ifdef HAVE_LONG_DOUBLE
422 if (flags
>= 16 || (flags
& 4))
423 type
= TYPE_LONGDOUBLE
;
431 type
= TYPE_WIDE_CHAR
;
440 type
= TYPE_WIDE_CHAR
;
447 type
= TYPE_WIDE_STRING
;
456 type
= TYPE_WIDE_STRING
;
464 #ifdef HAVE_LONG_LONG
465 if (flags
>= 16 || (flags
& 4))
466 type
= TYPE_COUNT_LONGLONGINT_POINTER
;
470 type
= TYPE_COUNT_LONGINT_POINTER
;
472 type
= TYPE_COUNT_SCHAR_POINTER
;
474 type
= TYPE_COUNT_SHORT_POINTER
;
476 type
= TYPE_COUNT_INT_POINTER
;
482 /* Unknown conversion character. */
487 if (type
!= TYPE_NONE
)
489 dp
->arg_index
= arg_index
;
490 if (dp
->arg_index
== ARG_NONE
)
492 dp
->arg_index
= arg_posn
++;
493 if (dp
->arg_index
== ARG_NONE
)
494 /* arg_posn wrapped around. */
497 REGISTER_ARG (dp
->arg_index
, type
);
504 if (d
->count
>= d_allocated
)
509 d_allocated
= xtimes (d_allocated
, 2);
510 memory_size
= xtimes (d_allocated
, sizeof (DIRECTIVE
));
511 if (size_overflow_p (memory_size
))
512 /* Overflow, would lead to out of memory. */
514 memory
= realloc (d
->dir
, memory_size
);
522 d
->dir
[d
->count
].dir_start
= cp
;
524 d
->max_width_length
= max_width_length
;
525 d
->max_precision_length
= max_precision_length
;