1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2004, 2006 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 # include "wprintf-parse.h"
24 # include "printf-parse.h"
27 /* Get size_t, NULL. */
31 #if HAVE_STDINT_H_WITH_UINTMAX
34 #if HAVE_INTTYPES_H_WITH_UINTMAX
35 # include <inttypes.h>
38 /* malloc(), realloc(), free(). */
42 # define SIZE_MAX ((size_t) -1)
46 # define PRINTF_PARSE wprintf_parse
47 # define CHAR_T wchar_t
48 # define DIRECTIVE wchar_t_directive
49 # define DIRECTIVES wchar_t_directives
51 # define PRINTF_PARSE printf_parse
53 # define DIRECTIVE char_directive
54 # define DIRECTIVES char_directives
61 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
63 const CHAR_T *cp = format; /* pointer into format */
64 size_t arg_posn = 0; /* number of regular arguments consumed */
65 size_t d_allocated; /* allocated elements of d->dir */
66 size_t a_allocated; /* allocated elements of a->arg */
67 size_t max_width_length = 0;
68 size_t max_precision_length = 0;
72 d->dir = malloc (d_allocated * sizeof (DIRECTIVE));
81 #define REGISTER_ARG(_index_,_type_) \
83 size_t n = (_index_); \
84 if (n >= a_allocated) \
90 if (a_allocated <= n) \
91 a_allocated = n + 1; \
92 if (SIZE_MAX / sizeof (argument) < a_allocated) \
93 /* Overflow, would lead to out of memory. */ \
95 memory_size = a_allocated * sizeof (argument); \
97 ? realloc (a->arg, memory_size) \
98 : malloc (memory_size)); \
100 /* Out of memory. */ \
104 while (a->count <= n) \
105 a->arg[a->count++].type = TYPE_NONE; \
106 if (a->arg[n].type == TYPE_NONE) \
107 a->arg[n].type = (_type_); \
108 else if (a->arg[n].type != (_type_)) \
109 /* Ambiguous type for positional argument. */ \
118 size_t arg_index = ARG_NONE;
119 DIRECTIVE *dp = &d->dir[d->count];/* pointer to next directive */
121 /* Initialize the next directive. */
122 dp->dir_start = cp - 1;
124 dp->width_start = NULL;
125 dp->width_end = NULL;
126 dp->width_arg_index = ARG_NONE;
127 dp->precision_start = NULL;
128 dp->precision_end = NULL;
129 dp->precision_arg_index = ARG_NONE;
130 dp->arg_index = ARG_NONE;
132 /* Test for positional argument. */
133 if (*cp >= '0' && *cp <= '9')
137 for (np = cp; *np >= '0' && *np <= '9'; np++)
143 for (np = cp; *np >= '0' && *np <= '9'; np++)
144 if (n < SIZE_MAX / 10)
145 n = 10 * n + (*np - '0');
147 /* n too large for memory. */
150 /* Positional argument 0. */
157 /* Read the flags. */
162 dp->flags |= FLAG_GROUP;
167 dp->flags |= FLAG_LEFT;
172 dp->flags |= FLAG_SHOWSIGN;
177 dp->flags |= FLAG_SPACE;
182 dp->flags |= FLAG_ALT;
187 dp->flags |= FLAG_ZERO;
194 /* Parse the field width. */
197 dp->width_start = cp;
200 if (max_width_length < 1)
201 max_width_length = 1;
203 /* Test for positional argument. */
204 if (*cp >= '0' && *cp <= '9')
208 for (np = cp; *np >= '0' && *np <= '9'; np++)
214 for (np = cp; *np >= '0' && *np <= '9'; np++)
215 if (n < SIZE_MAX / 10)
216 n = 10 * n + (*np - '0');
218 /* n too large for memory. */
221 /* Positional argument 0. */
223 dp->width_arg_index = n - 1;
227 if (dp->width_arg_index == ARG_NONE)
229 dp->width_arg_index = arg_posn++;
230 if (dp->width_arg_index == ARG_NONE)
231 /* arg_posn wrapped around. */
234 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
236 else if (*cp >= '0' && *cp <= '9')
240 dp->width_start = cp;
241 for (; *cp >= '0' && *cp <= '9'; cp++)
244 width_length = dp->width_end - dp->width_start;
245 if (max_width_length < width_length)
246 max_width_length = width_length;
249 /* Parse the precision. */
255 dp->precision_start = cp - 1;
257 dp->precision_end = cp;
258 if (max_precision_length < 2)
259 max_precision_length = 2;
261 /* Test for positional argument. */
262 if (*cp >= '0' && *cp <= '9')
266 for (np = cp; *np >= '0' && *np <= '9'; np++)
272 for (np = cp; *np >= '0' && *np <= '9'; np++)
273 if (n < SIZE_MAX / 10)
274 n = 10 * n + (*np - '0');
276 /* n too large for memory. */
279 /* Positional argument 0. */
281 dp->precision_arg_index = n - 1;
285 if (dp->precision_arg_index == ARG_NONE)
287 dp->precision_arg_index = arg_posn++;
288 if (dp->precision_arg_index == ARG_NONE)
289 /* arg_posn wrapped around. */
292 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
296 size_t precision_length;
298 dp->precision_start = cp - 1;
299 for (; *cp >= '0' && *cp <= '9'; cp++)
301 dp->precision_end = cp;
302 precision_length = dp->precision_end - dp->precision_start;
303 if (max_precision_length < precision_length)
304 max_precision_length = precision_length;
311 /* Parse argument type/size specifiers. */
319 flags |= (1 << (flags & 1));
335 if (sizeof (intmax_t) > sizeof (long))
337 /* intmax_t = long long */
340 else if (sizeof (intmax_t) > sizeof (int))
342 /* intmax_t = long */
348 else if (*cp == 'z' || *cp == 'Z')
350 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
351 because the warning facility in gcc-2.95.2 understands
352 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
353 if (sizeof (size_t) > sizeof (long))
355 /* size_t = long long */
358 else if (sizeof (size_t) > sizeof (int))
367 if (sizeof (ptrdiff_t) > sizeof (long))
369 /* ptrdiff_t = long long */
372 else if (sizeof (ptrdiff_t) > sizeof (int))
374 /* ptrdiff_t = long */
383 /* Read the conversion character. */
388 #ifdef HAVE_LONG_LONG
389 if (flags >= 16 || (flags & 4))
390 type = TYPE_LONGLONGINT;
402 case 'o': case 'u': case 'x': case 'X':
403 #ifdef HAVE_LONG_LONG
404 if (flags >= 16 || (flags & 4))
405 type = TYPE_ULONGLONGINT;
409 type = TYPE_ULONGINT;
417 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
419 #ifdef HAVE_LONG_DOUBLE
420 if (flags >= 16 || (flags & 4))
421 type = TYPE_LONGDOUBLE;
429 type = TYPE_WIDE_CHAR;
438 type = TYPE_WIDE_CHAR;
445 type = TYPE_WIDE_STRING;
454 type = TYPE_WIDE_STRING;
462 #ifdef HAVE_LONG_LONG
463 if (flags >= 16 || (flags & 4))
464 type = TYPE_COUNT_LONGLONGINT_POINTER;
468 type = TYPE_COUNT_LONGINT_POINTER;
470 type = TYPE_COUNT_SCHAR_POINTER;
472 type = TYPE_COUNT_SHORT_POINTER;
474 type = TYPE_COUNT_INT_POINTER;
480 /* Unknown conversion character. */
485 if (type != TYPE_NONE)
487 dp->arg_index = arg_index;
488 if (dp->arg_index == ARG_NONE)
490 dp->arg_index = arg_posn++;
491 if (dp->arg_index == ARG_NONE)
492 /* arg_posn wrapped around. */
495 REGISTER_ARG (dp->arg_index, type);
502 if (d->count >= d_allocated)
506 if (SIZE_MAX / (2 * sizeof (DIRECTIVE)) < d_allocated)
507 /* Overflow, would lead to out of memory. */
510 memory = realloc (d->dir, d_allocated * sizeof (DIRECTIVE));
518 d->dir[d->count].dir_start = cp;
520 d->max_width_length = max_width_length;
521 d->max_precision_length = max_precision_length;