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 it
5 under the terms of the GNU Library General Public License as published
6 by 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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, see <http://www.gnu.org/licenses/>. */
24 #include "printf-parse.h"
26 /* Get size_t, NULL. */
30 #if HAVE_STDINT_H_WITH_UINTMAX
33 #if HAVE_INTTYPES_H_WITH_UINTMAX
34 # include <inttypes.h>
37 /* malloc(), realloc(), free(). */
44 printf_parse (const char *format, char_directives *d, arguments *a)
46 const char *cp = format; /* pointer into format */
47 int arg_posn = 0; /* number of regular arguments consumed */
48 unsigned int d_allocated; /* allocated elements of d->dir */
49 unsigned int a_allocated; /* allocated elements of a->arg */
50 unsigned int max_width_length = 0;
51 unsigned int max_precision_length = 0;
55 d->dir = malloc (d_allocated * sizeof (char_directive));
64 #define REGISTER_ARG(_index_,_type_) \
66 unsigned int n = (_index_); \
67 if (n >= a_allocated) \
70 a_allocated = 2 * a_allocated; \
71 if (a_allocated <= n) \
72 a_allocated = n + 1; \
74 ? realloc (a->arg, a_allocated * sizeof (argument)) \
75 : malloc (a_allocated * sizeof (argument))); \
77 /* Out of memory. */ \
81 while (a->count <= n) \
82 a->arg[a->count++].type = TYPE_NONE; \
83 if (a->arg[n].type == TYPE_NONE) \
84 a->arg[n].type = (_type_); \
85 else if (a->arg[n].type != (_type_)) \
86 /* Ambiguous type for positional argument. */ \
96 char_directive *dp = &d->dir[d->count];/* pointer to next directive */
98 /* Initialize the next directive. */
99 dp->dir_start = cp - 1;
101 dp->width_start = NULL;
102 dp->width_end = NULL;
103 dp->width_arg_index = -1;
104 dp->precision_start = NULL;
105 dp->precision_end = NULL;
106 dp->precision_arg_index = -1;
109 /* Test for positional argument. */
110 if (*cp >= '0' && *cp <= '9')
114 for (np = cp; *np >= '0' && *np <= '9'; np++)
120 for (np = cp; *np >= '0' && *np <= '9'; np++)
121 n = 10 * n + (*np - '0');
123 /* Positional argument 0. */
130 /* Read the flags. */
135 dp->flags |= FLAG_GROUP;
140 dp->flags |= FLAG_LEFT;
145 dp->flags |= FLAG_SHOWSIGN;
150 dp->flags |= FLAG_SPACE;
155 dp->flags |= FLAG_ALT;
160 dp->flags |= FLAG_ZERO;
167 /* Parse the field width. */
170 dp->width_start = cp;
173 if (max_width_length < 1)
174 max_width_length = 1;
176 /* Test for positional argument. */
177 if (*cp >= '0' && *cp <= '9')
181 for (np = cp; *np >= '0' && *np <= '9'; np++)
187 for (np = cp; *np >= '0' && *np <= '9'; np++)
188 n = 10 * n + (*np - '0');
190 /* Positional argument 0. */
192 dp->width_arg_index = n - 1;
196 if (dp->width_arg_index < 0)
197 dp->width_arg_index = arg_posn++;
198 REGISTER_ARG (dp->width_arg_index, TYPE_INT);
200 else if (*cp >= '0' && *cp <= '9')
202 unsigned int width_length;
204 dp->width_start = cp;
205 for (; *cp >= '0' && *cp <= '9'; cp++)
208 width_length = dp->width_end - dp->width_start;
209 if (max_width_length < width_length)
210 max_width_length = width_length;
213 /* Parse the precision. */
219 dp->precision_start = cp - 1;
221 dp->precision_end = cp;
222 if (max_precision_length < 2)
223 max_precision_length = 2;
225 /* Test for positional argument. */
226 if (*cp >= '0' && *cp <= '9')
230 for (np = cp; *np >= '0' && *np <= '9'; np++)
236 for (np = cp; *np >= '0' && *np <= '9'; np++)
237 n = 10 * n + (*np - '0');
239 /* Positional argument 0. */
241 dp->precision_arg_index = n - 1;
245 if (dp->precision_arg_index < 0)
246 dp->precision_arg_index = arg_posn++;
247 REGISTER_ARG (dp->precision_arg_index, TYPE_INT);
251 unsigned int precision_length;
253 dp->precision_start = cp - 1;
254 for (; *cp >= '0' && *cp <= '9'; cp++)
256 dp->precision_end = cp;
257 precision_length = dp->precision_end - dp->precision_start;
258 if (max_precision_length < precision_length)
259 max_precision_length = precision_length;
266 /* Parse argument type/size specifiers. */
274 flags |= (1 << (flags & 1));
287 #ifdef HAVE_INT64_AND_I64
288 else if (cp[0] == 'I' &&
299 if (sizeof (intmax_t) > sizeof (long))
301 /* intmax_t = long long */
304 else if (sizeof (intmax_t) > sizeof (int))
306 /* intmax_t = long */
312 else if (*cp == 'z' || *cp == 'Z')
314 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
315 because the warning facility in gcc-2.95.2 understands
316 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
317 if (sizeof (size_t) > sizeof (long))
319 /* size_t = long long */
322 else if (sizeof (size_t) > sizeof (int))
331 if (sizeof (ptrdiff_t) > sizeof (long))
333 /* ptrdiff_t = long long */
336 else if (sizeof (ptrdiff_t) > sizeof (int))
338 /* ptrdiff_t = long */
347 /* Read the conversion character. */
352 #ifdef HAVE_INT64_AND_I64
357 #ifdef HAVE_LONG_LONG
358 if (flags >= 16 || (flags & 4))
359 type = TYPE_LONGLONGINT;
371 case 'o': case 'u': case 'x': case 'X':
372 #ifdef HAVE_INT64_AND_I64
377 #ifdef HAVE_LONG_LONG
378 if (flags >= 16 || (flags & 4))
379 type = TYPE_ULONGLONGINT;
383 type = TYPE_ULONGINT;
391 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
393 #ifdef HAVE_LONG_DOUBLE
394 if (flags >= 16 || (flags & 4))
395 type = TYPE_LONGDOUBLE;
403 type = TYPE_WIDE_CHAR;
412 type = TYPE_WIDE_CHAR;
419 type = TYPE_WIDE_STRING;
428 type = TYPE_WIDE_STRING;
436 #ifdef HAVE_LONG_LONG
437 if (flags >= 16 || (flags & 4))
438 type = TYPE_COUNT_LONGLONGINT_POINTER;
442 type = TYPE_COUNT_LONGINT_POINTER;
444 type = TYPE_COUNT_SCHAR_POINTER;
446 type = TYPE_COUNT_SHORT_POINTER;
448 type = TYPE_COUNT_INT_POINTER;
454 /* Unknown conversion character. */
459 if (type != TYPE_NONE)
461 dp->arg_index = arg_index;
462 if (dp->arg_index < 0)
463 dp->arg_index = arg_posn++;
464 REGISTER_ARG (dp->arg_index, type);
471 if (d->count >= d_allocated)
473 char_directive *memory;
475 d_allocated = 2 * d_allocated;
476 memory = realloc (d->dir, d_allocated * sizeof (char_directive));
484 d->dir[d->count].dir_start = cp;
486 d->max_width_length = max_width_length;
487 d->max_precision_length = max_precision_length;