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. */
23 #include "printf-parse.h"
25 /* Get size_t, NULL. */
29 #if HAVE_STDINT_H_WITH_UINTMAX
32 #if HAVE_INTTYPES_H_WITH_UINTMAX
33 # include <inttypes.h>
36 /* malloc(), realloc(), free(). */
43 printf_parse (const char *format
, char_directives
*d
, arguments
*a
)
45 const char *cp
= format
; /* pointer into format */
46 int arg_posn
= 0; /* number of regular arguments consumed */
47 unsigned int d_allocated
; /* allocated elements of d->dir */
48 unsigned int a_allocated
; /* allocated elements of a->arg */
49 unsigned int max_width_length
= 0;
50 unsigned int max_precision_length
= 0;
54 d
->dir
= malloc (d_allocated
* sizeof (char_directive
));
63 #define REGISTER_ARG(_index_,_type_) \
65 unsigned int n = (_index_); \
66 if (n >= a_allocated) \
69 a_allocated = 2 * a_allocated; \
70 if (a_allocated <= n) \
71 a_allocated = n + 1; \
73 ? realloc (a->arg, a_allocated * sizeof (argument)) \
74 : malloc (a_allocated * sizeof (argument))); \
76 /* Out of memory. */ \
80 while (a->count <= n) \
81 a->arg[a->count++].type = TYPE_NONE; \
82 if (a->arg[n].type == TYPE_NONE) \
83 a->arg[n].type = (_type_); \
84 else if (a->arg[n].type != (_type_)) \
85 /* Ambiguous type for positional argument. */ \
95 char_directive
*dp
= &d
->dir
[d
->count
];/* pointer to next directive */
97 /* Initialize the next directive. */
98 dp
->dir_start
= cp
- 1;
100 dp
->width_start
= NULL
;
101 dp
->width_end
= NULL
;
102 dp
->width_arg_index
= -1;
103 dp
->precision_start
= NULL
;
104 dp
->precision_end
= NULL
;
105 dp
->precision_arg_index
= -1;
108 /* Test for positional argument. */
109 if (*cp
>= '0' && *cp
<= '9')
113 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
119 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
120 n
= 10 * n
+ (*np
- '0');
122 /* Positional argument 0. */
129 /* Read the flags. */
134 dp
->flags
|= FLAG_GROUP
;
139 dp
->flags
|= FLAG_LEFT
;
144 dp
->flags
|= FLAG_SHOWSIGN
;
149 dp
->flags
|= FLAG_SPACE
;
154 dp
->flags
|= FLAG_ALT
;
159 dp
->flags
|= FLAG_ZERO
;
166 /* Parse the field width. */
169 dp
->width_start
= cp
;
172 if (max_width_length
< 1)
173 max_width_length
= 1;
175 /* Test for positional argument. */
176 if (*cp
>= '0' && *cp
<= '9')
180 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
186 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
187 n
= 10 * n
+ (*np
- '0');
189 /* Positional argument 0. */
191 dp
->width_arg_index
= n
- 1;
195 if (dp
->width_arg_index
< 0)
196 dp
->width_arg_index
= arg_posn
++;
197 REGISTER_ARG (dp
->width_arg_index
, TYPE_INT
);
199 else if (*cp
>= '0' && *cp
<= '9')
201 unsigned int width_length
;
203 dp
->width_start
= cp
;
204 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
207 width_length
= dp
->width_end
- dp
->width_start
;
208 if (max_width_length
< width_length
)
209 max_width_length
= width_length
;
212 /* Parse the precision. */
218 dp
->precision_start
= cp
- 1;
220 dp
->precision_end
= cp
;
221 if (max_precision_length
< 2)
222 max_precision_length
= 2;
224 /* Test for positional argument. */
225 if (*cp
>= '0' && *cp
<= '9')
229 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
235 for (np
= cp
; *np
>= '0' && *np
<= '9'; np
++)
236 n
= 10 * n
+ (*np
- '0');
238 /* Positional argument 0. */
240 dp
->precision_arg_index
= n
- 1;
244 if (dp
->precision_arg_index
< 0)
245 dp
->precision_arg_index
= arg_posn
++;
246 REGISTER_ARG (dp
->precision_arg_index
, TYPE_INT
);
250 unsigned int precision_length
;
252 dp
->precision_start
= cp
- 1;
253 for (; *cp
>= '0' && *cp
<= '9'; cp
++)
255 dp
->precision_end
= cp
;
256 precision_length
= dp
->precision_end
- dp
->precision_start
;
257 if (max_precision_length
< precision_length
)
258 max_precision_length
= precision_length
;
265 /* Parse argument type/size specifiers. */
273 flags
|= (1 << (flags
& 1));
289 if (sizeof (intmax_t) > sizeof (long))
291 /* intmax_t = long long */
294 else if (sizeof (intmax_t) > sizeof (int))
296 /* intmax_t = long */
302 else if (*cp
== 'z' || *cp
== 'Z')
304 /* 'z' is standardized in ISO C 99, but glibc uses 'Z'
305 because the warning facility in gcc-2.95.2 understands
306 only 'Z' (see gcc-2.95.2/gcc/c-common.c:1784). */
307 if (sizeof (size_t) > sizeof (long))
309 /* size_t = long long */
312 else if (sizeof (size_t) > sizeof (int))
321 if (sizeof (ptrdiff_t) > sizeof (long))
323 /* ptrdiff_t = long long */
326 else if (sizeof (ptrdiff_t) > sizeof (int))
328 /* ptrdiff_t = long */
337 /* Read the conversion character. */
342 #ifdef HAVE_LONG_LONG
343 if (flags
>= 16 || (flags
& 4))
344 type
= TYPE_LONGLONGINT
;
356 case 'o': case 'u': case 'x': case 'X':
357 #ifdef HAVE_LONG_LONG
358 if (flags
>= 16 || (flags
& 4))
359 type
= TYPE_ULONGLONGINT
;
363 type
= TYPE_ULONGINT
;
371 case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
373 #ifdef HAVE_LONG_DOUBLE
374 if (flags
>= 16 || (flags
& 4))
375 type
= TYPE_LONGDOUBLE
;
383 type
= TYPE_WIDE_CHAR
;
392 type
= TYPE_WIDE_CHAR
;
399 type
= TYPE_WIDE_STRING
;
408 type
= TYPE_WIDE_STRING
;
416 #ifdef HAVE_LONG_LONG
417 if (flags
>= 16 || (flags
& 4))
418 type
= TYPE_COUNT_LONGLONGINT_POINTER
;
422 type
= TYPE_COUNT_LONGINT_POINTER
;
424 type
= TYPE_COUNT_SCHAR_POINTER
;
426 type
= TYPE_COUNT_SHORT_POINTER
;
428 type
= TYPE_COUNT_INT_POINTER
;
434 /* Unknown conversion character. */
439 if (type
!= TYPE_NONE
)
441 dp
->arg_index
= arg_index
;
442 if (dp
->arg_index
< 0)
443 dp
->arg_index
= arg_posn
++;
444 REGISTER_ARG (dp
->arg_index
, type
);
451 if (d
->count
>= d_allocated
)
453 char_directive
*memory
;
455 d_allocated
= 2 * d_allocated
;
456 memory
= realloc (d
->dir
, d_allocated
* sizeof (char_directive
));
464 d
->dir
[d
->count
].dir_start
= cp
;
466 d
->max_width_length
= max_width_length
;
467 d
->max_precision_length
= max_precision_length
;