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, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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));
290 if (sizeof (intmax_t) > sizeof (long))
292 /* intmax_t = long long */
295 else if (sizeof (intmax_t) > sizeof (int))
297 /* intmax_t = long */
303 else if (*cp
== 'z' || *cp
== 'Z')
305 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
306 because the warning facility in gcc-2.95.2 understands
307 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
308 if (sizeof (size_t) > sizeof (long))
310 /* size_t = long long */
313 else if (sizeof (size_t) > sizeof (int))
322 if (sizeof (ptrdiff_t) > sizeof (long))
324 /* ptrdiff_t = long long */
327 else if (sizeof (ptrdiff_t) > sizeof (int))
329 /* ptrdiff_t = long */
338 /* Read the conversion character. */
343 #ifdef HAVE_LONG_LONG
344 if (flags
>= 16 || (flags
& 4))
345 type
= TYPE_LONGLONGINT
;
357 case 'o': case 'u': case 'x': case 'X':
358 #ifdef HAVE_LONG_LONG
359 if (flags
>= 16 || (flags
& 4))
360 type
= TYPE_ULONGLONGINT
;
364 type
= TYPE_ULONGINT
;
372 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
374 #ifdef HAVE_LONG_DOUBLE
375 if (flags
>= 16 || (flags
& 4))
376 type
= TYPE_LONGDOUBLE
;
384 type
= TYPE_WIDE_CHAR
;
393 type
= TYPE_WIDE_CHAR
;
400 type
= TYPE_WIDE_STRING
;
409 type
= TYPE_WIDE_STRING
;
417 #ifdef HAVE_LONG_LONG
418 if (flags
>= 16 || (flags
& 4))
419 type
= TYPE_COUNT_LONGLONGINT_POINTER
;
423 type
= TYPE_COUNT_LONGINT_POINTER
;
425 type
= TYPE_COUNT_SCHAR_POINTER
;
427 type
= TYPE_COUNT_SHORT_POINTER
;
429 type
= TYPE_COUNT_INT_POINTER
;
435 /* Unknown conversion character. */
440 if (type
!= TYPE_NONE
)
442 dp
->arg_index
= arg_index
;
443 if (dp
->arg_index
< 0)
444 dp
->arg_index
= arg_posn
++;
445 REGISTER_ARG (dp
->arg_index
, type
);
452 if (d
->count
>= d_allocated
)
454 char_directive
*memory
;
456 d_allocated
= 2 * d_allocated
;
457 memory
= realloc (d
->dir
, d_allocated
* sizeof (char_directive
));
465 d
->dir
[d
->count
].dir_start
= cp
;
467 d
->max_width_length
= max_width_length
;
468 d
->max_precision_length
= max_precision_length
;