1 /* Formatted output to strings.
2 Copyright (C) 1999-2000, 2002-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
24 # include "wprintf-parse.h"
26 # include "printf-parse.h"
29 /* Get size_t, NULL. */
33 #if HAVE_STDINT_H_WITH_UINTMAX
36 #if HAVE_INTTYPES_H_WITH_UINTMAX
37 # include <inttypes.h>
40 /* malloc(), realloc(), free(). */
44 # define PRINTF_PARSE wprintf_parse
45 # define CHAR_T wchar_t
46 # define DIRECTIVE wchar_t_directive
47 # define DIRECTIVES wchar_t_directives
49 # define PRINTF_PARSE printf_parse
51 # define DIRECTIVE char_directive
52 # define DIRECTIVES char_directives
59 PRINTF_PARSE (const CHAR_T *format, DIRECTIVES *d, arguments *a)
61 const CHAR_T *cp = format; /* pointer into format */
62 int arg_posn = 0; /* number of regular arguments consumed */
63 unsigned int d_allocated; /* allocated elements of d->dir */
64 unsigned int a_allocated; /* allocated elements of a->arg */
65 unsigned int max_width_length = 0;
66 unsigned int max_precision_length = 0;
70 d->dir = malloc (d_allocated * sizeof (DIRECTIVE));
79 #define REGISTER_ARG(_index_,_type_) \
81 unsigned int n = (_index_); \
82 if (n >= a_allocated) \
85 a_allocated = 2 * a_allocated; \
86 if (a_allocated <= n) \
87 a_allocated = n + 1; \
89 ? realloc (a->arg, a_allocated * sizeof (argument)) \
90 : malloc (a_allocated * sizeof (argument))); \
92 /* Out of memory. */ \
96 while (a->count <= n) \
97 a->arg[a->count++].type = TYPE_NONE; \
98 if (a->arg[n].type == TYPE_NONE) \
99 a->arg[n].type = (_type_); \
100 else if (a->arg[n].type != (_type_)) \
101 /* Ambiguous type for positional argument. */ \
111 DIRECTIVE *dp = &d->dir[d->count];/* pointer to next directive */
113 /* Initialize the next directive. */
114 dp->dir_start = cp - 1;
116 dp->width_start = NULL;
117 dp->width_end = NULL;
118 dp->width_arg_index = -1;
119 dp->precision_start = NULL;
120 dp->precision_end = NULL;
121 dp->precision_arg_index = -1;
124 /* Test for positional argument. */
125 if (*cp >= '0' && *cp <= '9')
129 for (np = cp; *np >= '0' && *np <= '9'; np++)
135 for (np = cp; *np >= '0' && *np <= '9'; np++)
136 n = 10 * n + (*np - '0');
138 /* Positional argument 0. */
145 /* Read the flags. */
150 dp->flags |= FLAG_GROUP;
155 dp->flags |= FLAG_LEFT;
160 dp->flags |= FLAG_SHOWSIGN;
165 dp->flags |= FLAG_SPACE;
170 dp->flags |= FLAG_ALT;
175 dp->flags |= FLAG_ZERO;
182 /* Parse the field width. */
185 dp->width_start = cp;
188 if (max_width_length < 1)
189 max_width_length = 1;
191 /* Test for positional argument. */
192 if (*cp >= '0' && *cp <= '9')
196 for (np = cp; *np >= '0' && *np <= '9'; np++)
202 for (np = cp; *np >= '0' && *np <= '9'; np++)
203 n = 10 * n + (*np - '0');
205 /* Positional argument 0. */
207 dp->width_arg_index = n - 1;
211 if (dp->width_arg_index < 0)
212 dp->width_arg_index = arg_posn++;
213 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
215 else if (*cp >= '0' && *cp <= '9')
217 unsigned int width_length;
219 dp->width_start = cp;
220 for (; *cp >= '0' && *cp <= '9'; cp++)
223 width_length = dp->width_end - dp->width_start;
224 if (max_width_length < width_length)
225 max_width_length = width_length;
228 /* Parse the precision. */
234 dp->precision_start = cp - 1;
236 dp->precision_end = cp;
237 if (max_precision_length < 2)
238 max_precision_length = 2;
240 /* Test for positional argument. */
241 if (*cp >= '0' && *cp <= '9')
245 for (np = cp; *np >= '0' && *np <= '9'; np++)
251 for (np = cp; *np >= '0' && *np <= '9'; np++)
252 n = 10 * n + (*np - '0');
254 /* Positional argument 0. */
256 dp->precision_arg_index = n - 1;
260 if (dp->precision_arg_index < 0)
261 dp->precision_arg_index = arg_posn++;
262 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
266 unsigned int precision_length;
268 dp->precision_start = cp - 1;
269 for (; *cp >= '0' && *cp <= '9'; cp++)
271 dp->precision_end = cp;
272 precision_length = dp->precision_end - dp->precision_start;
273 if (max_precision_length < precision_length)
274 max_precision_length = precision_length;
281 /* Parse argument type/size specifiers. */
289 flags |= (1 << (flags & 1));
305 if (sizeof (intmax_t) > sizeof (long))
307 /* intmax_t = long long */
310 else if (sizeof (intmax_t) > sizeof (int))
312 /* intmax_t = long */
318 else if (*cp == 'z' || *cp == 'Z')
320 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
321 because the warning facility in gcc-2.95.2 understands
322 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
323 if (sizeof (size_t) > sizeof (long))
325 /* size_t = long long */
328 else if (sizeof (size_t) > sizeof (int))
337 if (sizeof (ptrdiff_t) > sizeof (long))
339 /* ptrdiff_t = long long */
342 else if (sizeof (ptrdiff_t) > sizeof (int))
344 /* ptrdiff_t = long */
353 /* Read the conversion character. */
358 #ifdef HAVE_LONG_LONG
359 if (flags >= 16 || (flags & 4))
360 type = TYPE_LONGLONGINT;
372 case 'o': case 'u': case 'x': case 'X':
373 #ifdef HAVE_LONG_LONG
374 if (flags >= 16 || (flags & 4))
375 type = TYPE_ULONGLONGINT;
379 type = TYPE_ULONGINT;
387 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
389 #ifdef HAVE_LONG_DOUBLE
390 if (flags >= 16 || (flags & 4))
391 type = TYPE_LONGDOUBLE;
399 type = TYPE_WIDE_CHAR;
408 type = TYPE_WIDE_CHAR;
415 type = TYPE_WIDE_STRING;
424 type = TYPE_WIDE_STRING;
432 #ifdef HAVE_LONG_LONG
433 if (flags >= 16 || (flags & 4))
434 type = TYPE_COUNT_LONGLONGINT_POINTER;
438 type = TYPE_COUNT_LONGINT_POINTER;
440 type = TYPE_COUNT_SCHAR_POINTER;
442 type = TYPE_COUNT_SHORT_POINTER;
444 type = TYPE_COUNT_INT_POINTER;
450 /* Unknown conversion character. */
455 if (type != TYPE_NONE)
457 dp->arg_index = arg_index;
458 if (dp->arg_index < 0)
459 dp->arg_index = arg_posn++;
460 REGISTER_ARG (dp->arg_index, type);
467 if (d->count >= d_allocated)
471 d_allocated = 2 * d_allocated;
472 memory = realloc (d->dir, d_allocated * sizeof (DIRECTIVE));
480 d->dir[d->count].dir_start = cp;
482 d->max_width_length = max_width_length;
483 d->max_precision_length = max_precision_length;