1 /*************************************************************************
3 * $Id: trio.c,v 1.10 2002/09/25 22:44:41 veillard Exp $
5 * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
13 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
14 * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
16 *************************************************************************
18 * A note to trio contributors:
20 * Avoid heap allocation at all costs to ensure that the trio functions
21 * are async-safe. The exceptions are the printf/fprintf functions, which
22 * uses fputc, and the asprintf functions and the <alloc> modifier, which
23 * by design are required to allocate form the heap.
25 ************************************************************************/
29 * - Scan is probably too permissive about its modifiers.
30 * - C escapes in %#[] ?
31 * - Multibyte characters (done for format parsing, except scan groups)
32 * - Complex numbers? (C99 _Complex)
33 * - Boolean values? (C99 _Bool)
34 * - C99 NaN(n-char-sequence) missing
35 * - Should we support the GNU %a alloc modifier? GNU has an ugly hack
36 * for %a, because C99 used %a for other purposes. If specified as
37 * %as or %a[ it is interpreted as the alloc modifier, otherwise as
38 * the C99 hex-float. This means that you cannot scan %as as a hex-float
39 * immediately followed by an 's'.
40 * - Scanning of collating symbols.
43 /*************************************************************************
50 #if !defined(TRIO_MINIMAL)
54 /**************************************************************************
58 *************************************************************************/
60 #if defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) || defined(USE_MULTIBYTE) || TRIO_WIDECHAR
61 # define TRIO_COMPILER_SUPPORTS_MULTIBYTE
62 # if !defined(MB_LEN_MAX)
67 /*************************************************************************
71 #if !(defined(DEBUG) || defined(NDEBUG))
76 #if !defined(TRIO_COMPILER_SUPPORTS_C99)
77 # define isblank(x) (((x)==32) || ((x)==9))
82 #if defined(TRIO_COMPILER_ANCIENT)
95 # define FALSE (1 == 0)
96 # define TRUE (! FALSE)
100 /* mincore() can be used for debugging purposes */
101 #define VALID(x) (NULL != (x))
105 * Encode the error code and the position. This is decoded
106 * with TRIO_ERROR_CODE and TRIO_ERROR_POSITION.
108 # define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
110 # define TRIO_ERROR_RETURN(x,y) (-1)
114 /*************************************************************************
115 * Platform specific definitions
117 #if defined(TRIO_PLATFORM_UNIX)
122 #endif /* TRIO_PLATFORM_UNIX */
123 #if defined(TRIO_PLATFORM_VMS)
126 #if defined(TRIO_PLATFORM_WIN32)
129 # define write _write
130 #endif /* TRIO_PLATFORM_WIN32 */
132 #define TRIO_MSVC_VERSION_5 1100
135 # if defined(TRIO_COMPILER_SUPPORTS_ISO94)
138 typedef wchar_t trio_wchar_t
;
139 typedef wint_t trio_wint_t
;
141 typedef char trio_wchar_t
;
142 typedef int trio_wint_t
;
143 # define WCONST(x) L ## x
145 # define iswalnum(x) isalnum(x)
146 # define iswalpha(x) isalpha(x)
147 # define iswblank(x) isblank(x)
148 # define iswcntrl(x) iscntrl(x)
149 # define iswdigit(x) isdigit(x)
150 # define iswgraph(x) isgraph(x)
151 # define iswlower(x) islower(x)
152 # define iswprint(x) isprint(x)
153 # define iswpunct(x) ispunct(x)
154 # define iswspace(x) isspace(x)
155 # define iswupper(x) isupper(x)
156 # define iswxdigit(x) isxdigit(x)
161 /*************************************************************************
162 * Compiler dependent definitions
165 /* Support for long long */
167 # if !defined(USE_LONGLONG)
168 # if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
169 # define USE_LONGLONG
170 # elif defined(TRIO_COMPILER_SUNPRO)
171 # define USE_LONGLONG
172 # elif defined(_LONG_LONG) || defined(_LONGLONG)
173 # define USE_LONGLONG
178 /* The extra long numbers */
179 #if defined(USE_LONGLONG)
180 typedef signed long long int trio_longlong_t
;
181 typedef unsigned long long int trio_ulonglong_t
;
182 #elif defined(TRIO_COMPILER_MSVC)
183 # if (_MSC_VER >= TRIO_MSVC_VERSION_5)
184 typedef signed __int64 trio_longlong_t
;
185 typedef unsigned __int64 trio_ulonglong_t
;
187 typedef signed long int trio_longlong_t
;
188 typedef unsigned long int trio_ulonglong_t
;
191 typedef TRIO_SIGNED
long int trio_longlong_t
;
192 typedef unsigned long int trio_ulonglong_t
;
195 /* Maximal and fixed integer types */
196 #if defined(TRIO_COMPILER_SUPPORTS_C99)
198 typedef intmax_t trio_intmax_t
;
199 typedef uintmax_t trio_uintmax_t
;
200 typedef int8_t trio_int8_t
;
201 typedef int16_t trio_int16_t
;
202 typedef int32_t trio_int32_t
;
203 typedef int64_t trio_int64_t
;
204 #elif defined(TRIO_COMPILER_SUPPORTS_UNIX98)
205 # include <inttypes.h>
206 typedef intmax_t trio_intmax_t
;
207 typedef uintmax_t trio_uintmax_t
;
208 typedef int8_t trio_int8_t
;
209 typedef int16_t trio_int16_t
;
210 typedef int32_t trio_int32_t
;
211 typedef int64_t trio_int64_t
;
212 #elif defined(TRIO_COMPILER_MSVC) && (_MSC_VER >= TRIO_MSVC_VERSION_5)
213 typedef trio_longlong_t trio_intmax_t
;
214 typedef trio_ulonglong_t trio_uintmax_t
;
215 typedef __int8 trio_int8_t
;
216 typedef __int16 trio_int16_t
;
217 typedef __int32 trio_int32_t
;
218 typedef __int64 trio_int64_t
;
220 typedef trio_longlong_t trio_intmax_t
;
221 typedef trio_ulonglong_t trio_uintmax_t
;
222 # if defined(TRIO_INT8_T)
223 typedef TRIO_INT8_T trio_int8_t
;
225 typedef TRIO_SIGNED
char trio_int8_t
;
227 # if defined(TRIO_INT16_T)
228 typedef TRIO_INT16_T trio_int16_t
;
230 typedef TRIO_SIGNED
short trio_int16_t
;
232 # if defined(TRIO_INT32_T)
233 typedef TRIO_INT32_T trio_int32_t
;
235 typedef TRIO_SIGNED
int trio_int32_t
;
237 # if defined(TRIO_INT64_T)
238 typedef TRIO_INT64_T trio_int64_t
;
240 typedef trio_longlong_t trio_int64_t
;
244 #if !(defined(TRIO_COMPILER_SUPPORTS_C99) \
245 || defined(TRIO_COMPILER_SUPPORTS_UNIX01))
246 # define floorl(x) floor((double)(x))
247 # define fmodl(x,y) fmod((double)(x),(double)(y))
248 # define powl(x,y) pow((double)(x),(double)(y))
251 #define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
253 /*************************************************************************
254 * Internal Definitions
258 # define DECIMAL_DIG DBL_DIG
261 /* Long double sizes */
263 # define MAX_MANTISSA_DIGITS LDBL_DIG
264 # define MAX_EXPONENT_DIGITS 4
265 # define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
267 # define MAX_MANTISSA_DIGITS DECIMAL_DIG
268 # define MAX_EXPONENT_DIGITS 3
269 # define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
272 #if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
274 # undef LDBL_MANT_DIG
276 # define LDBL_DIG DBL_DIG
277 # define LDBL_MANT_DIG DBL_MANT_DIG
278 # define LDBL_EPSILON DBL_EPSILON
281 /* The maximal number of digits is for base 2 */
282 #define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
283 /* The width of a pointer. The number of bits in a hex digit is 4 */
284 #define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
286 /* Infinite and Not-A-Number for floating-point */
287 #define INFINITE_LOWER "inf"
288 #define INFINITE_UPPER "INF"
289 #define LONG_INFINITE_LOWER "infinite"
290 #define LONG_INFINITE_UPPER "INFINITE"
291 #define NAN_LOWER "nan"
292 #define NAN_UPPER "NAN"
294 /* Various constants */
299 /* Flags. Use maximum 32 */
302 FLAGS_SPACE
= 2 * FLAGS_STICKY
,
303 FLAGS_SHOWSIGN
= 2 * FLAGS_SPACE
,
304 FLAGS_LEFTADJUST
= 2 * FLAGS_SHOWSIGN
,
305 FLAGS_ALTERNATIVE
= 2 * FLAGS_LEFTADJUST
,
306 FLAGS_SHORT
= 2 * FLAGS_ALTERNATIVE
,
307 FLAGS_SHORTSHORT
= 2 * FLAGS_SHORT
,
308 FLAGS_LONG
= 2 * FLAGS_SHORTSHORT
,
309 FLAGS_QUAD
= 2 * FLAGS_LONG
,
310 FLAGS_LONGDOUBLE
= 2 * FLAGS_QUAD
,
311 FLAGS_SIZE_T
= 2 * FLAGS_LONGDOUBLE
,
312 FLAGS_PTRDIFF_T
= 2 * FLAGS_SIZE_T
,
313 FLAGS_INTMAX_T
= 2 * FLAGS_PTRDIFF_T
,
314 FLAGS_NILPADDING
= 2 * FLAGS_INTMAX_T
,
315 FLAGS_UNSIGNED
= 2 * FLAGS_NILPADDING
,
316 FLAGS_UPPER
= 2 * FLAGS_UNSIGNED
,
317 FLAGS_WIDTH
= 2 * FLAGS_UPPER
,
318 FLAGS_WIDTH_PARAMETER
= 2 * FLAGS_WIDTH
,
319 FLAGS_PRECISION
= 2 * FLAGS_WIDTH_PARAMETER
,
320 FLAGS_PRECISION_PARAMETER
= 2 * FLAGS_PRECISION
,
321 FLAGS_BASE
= 2 * FLAGS_PRECISION_PARAMETER
,
322 FLAGS_BASE_PARAMETER
= 2 * FLAGS_BASE
,
323 FLAGS_FLOAT_E
= 2 * FLAGS_BASE_PARAMETER
,
324 FLAGS_FLOAT_G
= 2 * FLAGS_FLOAT_E
,
325 FLAGS_QUOTE
= 2 * FLAGS_FLOAT_G
,
326 FLAGS_WIDECHAR
= 2 * FLAGS_QUOTE
,
327 FLAGS_ALLOC
= 2 * FLAGS_WIDECHAR
,
328 FLAGS_IGNORE
= 2 * FLAGS_ALLOC
,
329 FLAGS_IGNORE_PARAMETER
= 2 * FLAGS_IGNORE
,
330 FLAGS_VARSIZE_PARAMETER
= 2 * FLAGS_IGNORE_PARAMETER
,
331 FLAGS_FIXED_SIZE
= 2 * FLAGS_VARSIZE_PARAMETER
,
333 FLAGS_EXCLUDE
= FLAGS_SHORT
,
334 FLAGS_USER_DEFINED
= FLAGS_IGNORE
,
335 FLAGS_ROUNDING
= FLAGS_INTMAX_T
,
336 /* Compounded flags */
337 FLAGS_ALL_VARSIZES
= FLAGS_LONG
| FLAGS_QUAD
| FLAGS_INTMAX_T
| FLAGS_PTRDIFF_T
| FLAGS_SIZE_T
,
338 FLAGS_ALL_SIZES
= FLAGS_ALL_VARSIZES
| FLAGS_SHORTSHORT
| FLAGS_SHORT
,
345 /* Do not change these */
354 /* Maximal number of allowed parameters */
356 /* Maximal number of characters in class */
357 MAX_CHARACTER_CLASS
= UCHAR_MAX
+ 1,
359 /* Maximal string lengths for user-defined specifiers */
363 /* Maximal length of locale separator strings */
364 MAX_LOCALE_SEPARATOR_LENGTH
= MB_LEN_MAX
,
365 /* Maximal number of integers in grouping */
366 MAX_LOCALE_GROUPS
= 64,
368 /* Initial size of asprintf buffer */
369 DYNAMIC_START_SIZE
= 32
372 #define NO_GROUPING ((int)CHAR_MAX)
374 /* Fundamental formatting parameter types */
375 #define FORMAT_UNKNOWN 0
377 #define FORMAT_DOUBLE 2
378 #define FORMAT_CHAR 3
379 #define FORMAT_STRING 4
380 #define FORMAT_POINTER 5
381 #define FORMAT_COUNT 6
382 #define FORMAT_PARAMETER 7
383 #define FORMAT_GROUP 8
385 # define FORMAT_ERRNO 9
388 # define FORMAT_USER_DEFINED 10
391 /* Character constants */
392 #define CHAR_IDENTIFIER '%'
393 #define CHAR_BACKSLASH '\\'
394 #define CHAR_QUOTE '\"'
395 #define CHAR_ADJUST ' '
397 /* Character class expressions */
398 #define CLASS_ALNUM "[:alnum:]"
399 #define CLASS_ALPHA "[:alpha:]"
400 #define CLASS_BLANK "[:blank:]"
401 #define CLASS_CNTRL "[:cntrl:]"
402 #define CLASS_DIGIT "[:digit:]"
403 #define CLASS_GRAPH "[:graph:]"
404 #define CLASS_LOWER "[:lower:]"
405 #define CLASS_PRINT "[:print:]"
406 #define CLASS_PUNCT "[:punct:]"
407 #define CLASS_SPACE "[:space:]"
408 #define CLASS_UPPER "[:upper:]"
409 #define CLASS_XDIGIT "[:xdigit:]"
418 * C Widechar character (wint_t)
432 * S Widechar string (wchar_t *)
441 * D Binary Coded Decimal %D(length,precision) (OS/390)
443 #define SPECIFIER_CHAR 'c'
444 #define SPECIFIER_STRING 's'
445 #define SPECIFIER_DECIMAL 'd'
446 #define SPECIFIER_INTEGER 'i'
447 #define SPECIFIER_UNSIGNED 'u'
448 #define SPECIFIER_OCTAL 'o'
449 #define SPECIFIER_HEX 'x'
450 #define SPECIFIER_HEX_UPPER 'X'
451 #define SPECIFIER_FLOAT_E 'e'
452 #define SPECIFIER_FLOAT_E_UPPER 'E'
453 #define SPECIFIER_FLOAT_F 'f'
454 #define SPECIFIER_FLOAT_F_UPPER 'F'
455 #define SPECIFIER_FLOAT_G 'g'
456 #define SPECIFIER_FLOAT_G_UPPER 'G'
457 #define SPECIFIER_POINTER 'p'
458 #define SPECIFIER_GROUP '['
459 #define SPECIFIER_UNGROUP ']'
460 #define SPECIFIER_COUNT 'n'
462 # define SPECIFIER_CHAR_UPPER 'C'
463 # define SPECIFIER_STRING_UPPER 'S'
466 # define SPECIFIER_HEXFLOAT 'a'
467 # define SPECIFIER_HEXFLOAT_UPPER 'A'
470 # define SPECIFIER_ERRNO 'm'
473 # define SPECIFIER_BINARY 'b'
474 # define SPECIFIER_BINARY_UPPER 'B'
475 # define SPECIFIER_USER_DEFINED_BEGIN '<'
476 # define SPECIFIER_USER_DEFINED_END '>'
477 # define SPECIFIER_USER_DEFINED_SEPARATOR ':'
484 * Numbers = d,i,o,u,x,X
485 * Float = a,A,e,E,f,F,g,G
491 * Use the 9th parameter. 9 can be any number between 1 and
492 * the maximal argument
495 * Set width to 9. 9 can be any number, but must not be postfixed
500 * (unsigned) short int
508 * (unsigned) long int
516 * (unsigned) long long int
524 * Decimal-point is always present
526 * non-printable characters are handled as \number
537 * print: use parameter
538 * scan: no parameter (ignore)
548 * Integer part grouped in thousands
550 * Number grouped in nibbles (4 bits)
559 * @ Parameter (for both print and scan)
563 * The following options exists
565 * I16 = 16-bit integer
566 * I32 = 32-bit integer
567 * I64 = 64-bit integer
569 #define QUALIFIER_POSITION '$'
570 #define QUALIFIER_SHORT 'h'
571 #define QUALIFIER_LONG 'l'
572 #define QUALIFIER_LONG_UPPER 'L'
573 #define QUALIFIER_ALTERNATIVE '#'
574 #define QUALIFIER_SPACE ' '
575 #define QUALIFIER_PLUS '+'
576 #define QUALIFIER_MINUS '-'
577 #define QUALIFIER_DOT '.'
578 #define QUALIFIER_STAR '*'
579 #define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
581 # define QUALIFIER_SIZE_T 'z'
582 # define QUALIFIER_PTRDIFF_T 't'
583 # define QUALIFIER_INTMAX_T 'j'
585 #if TRIO_BSD || TRIO_GNU
586 # define QUALIFIER_QUAD 'q'
589 # define QUALIFIER_SIZE_T_UPPER 'Z'
592 # define QUALIFIER_WIDECHAR 'w'
595 # define QUALIFIER_FIXED_SIZE 'I'
598 # define QUALIFIER_QUOTE '\''
599 # define QUALIFIER_STICKY '!'
600 # define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
601 # define QUALIFIER_PARAM '@' /* Experimental */
602 # define QUALIFIER_COLON ':' /* For scanlists */
603 # define QUALIFIER_EQUAL '=' /* For scanlists */
604 # define QUALIFIER_ROUNDING_UPPER 'R'
608 /*************************************************************************
610 * Internal Structures
612 *************************************************************************/
616 /* An indication of which entry in the data union is used */
620 /* The width qualifier */
622 /* The precision qualifier */
624 /* The base qualifier */
626 /* The size for the variable size qualifier */
628 /* The marker of the end of the specifier */
629 int indexAfterSpecifier
;
630 /* The data from the argument list */
634 trio_wchar_t
*wstring
;
636 trio_pointer_t pointer
;
638 trio_intmax_t as_signed
;
639 trio_uintmax_t as_unsigned
;
642 double *doublePointer
;
643 trio_long_double_t longdoubleNumber
;
644 trio_long_double_t
*longdoublePointer
;
647 /* For the user-defined specifier */
648 char user_name
[MAX_USER_NAME
];
649 char user_data
[MAX_USER_DATA
];
652 /* Container for customized functions */
655 trio_outstream_t out
;
658 trio_pointer_t closure
;
661 /* General trio "class" */
662 typedef struct _trio_class_t
{
664 * The function to write characters to a stream.
666 void (*OutStream
) TRIO_PROTO((struct _trio_class_t
*, int));
668 * The function to read characters from a stream.
670 void (*InStream
) TRIO_PROTO((struct _trio_class_t
*, int *));
672 * The current location in the stream.
674 trio_pointer_t location
;
676 * The character currently being processed.
680 * The number of characters that would have been written/read
681 * if there had been sufficient space.
685 * The number of characters that are actually written/read.
686 * Processed and committed will only differ for the *nprintf
687 * and *nscanf functions.
691 * The upper limit of characters that may be written/read.
695 * The last output error that was detected.
700 /* References (for user-defined callbacks) */
701 typedef struct _trio_reference_t
{
703 trio_parameter_t
*parameter
;
706 /* Registered entries (for user-defined callbacks) */
707 typedef struct _trio_userdef_t
{
708 struct _trio_userdef_t
*next
;
709 trio_callback_t callback
;
714 /*************************************************************************
718 *************************************************************************/
720 static TRIO_CONST
char rcsid
[] = "@(#)$Id: trio.c,v 1.10 2002/09/25 22:44:41 veillard Exp $";
723 * Need this to workaround a parser bug in HP C/iX compiler that fails
724 * to resolves macro definitions that includes type 'long double',
725 * e.g: va_arg(arg_ptr, long double)
727 #if defined(TRIO_PLATFORM_MPEIX)
728 static TRIO_CONST trio_long_double_t ___dummy_long_double
= 0;
731 static TRIO_CONST
char internalNullString
[] = "(nil)";
733 #if defined(USE_LOCALE)
734 static struct lconv
*internalLocaleValues
= NULL
;
738 * UNIX98 says "in a locale where the radix character is not defined,
739 * the radix character defaults to a period (.)"
741 static int internalDecimalPointLength
= 1;
742 static int internalThousandSeparatorLength
= 1;
743 static char internalDecimalPoint
= '.';
744 static char internalDecimalPointString
[MAX_LOCALE_SEPARATOR_LENGTH
+ 1] = ".";
745 static char internalThousandSeparator
[MAX_LOCALE_SEPARATOR_LENGTH
+ 1] = ",";
746 static char internalGrouping
[MAX_LOCALE_GROUPS
] = { (char)NO_GROUPING
};
748 static TRIO_CONST
char internalDigitsLower
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
749 static TRIO_CONST
char internalDigitsUpper
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
750 static BOOLEAN_T internalDigitsUnconverted
= TRUE
;
751 static int internalDigitArray
[128];
753 static BOOLEAN_T internalCollationUnconverted
= TRUE
;
754 static char internalCollationArray
[MAX_CHARACTER_CLASS
][MAX_CHARACTER_CLASS
];
758 static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion
= NULL
;
759 static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion
= NULL
;
760 static trio_userdef_t
*internalUserDef
= NULL
;
764 /*************************************************************************
768 ************************************************************************/
770 #if defined(TRIO_MINIMAL)
771 # define TRIO_STRING_PUBLIC static
772 # include "triostr.c"
773 #endif /* defined(TRIO_MINIMAL) */
775 /*************************************************************************
779 * Remember to add all new qualifiers to this function.
780 * QUALIFIER_POSITION must not be added.
782 TRIO_PRIVATE BOOLEAN_T
784 TRIO_ARGS1((character
),
785 TRIO_CONST
char character
)
787 /* QUALIFIER_POSITION is not included */
790 case '0': case '1': case '2': case '3': case '4':
791 case '5': case '6': case '7': case '8': case '9':
793 case QUALIFIER_MINUS
:
794 case QUALIFIER_SPACE
:
797 case QUALIFIER_ALTERNATIVE
:
798 case QUALIFIER_SHORT
:
800 case QUALIFIER_LONG_UPPER
:
801 case QUALIFIER_CIRCUMFLEX
:
802 #if defined(QUALIFIER_SIZE_T)
803 case QUALIFIER_SIZE_T
:
805 #if defined(QUALIFIER_PTRDIFF_T)
806 case QUALIFIER_PTRDIFF_T
:
808 #if defined(QUALIFIER_INTMAX_T)
809 case QUALIFIER_INTMAX_T
:
811 #if defined(QUALIFIER_QUAD)
814 #if defined(QUALIFIER_SIZE_T_UPPER)
815 case QUALIFIER_SIZE_T_UPPER
:
817 #if defined(QUALIFIER_WIDECHAR)
818 case QUALIFIER_WIDECHAR
:
820 #if defined(QUALIFIER_QUOTE)
821 case QUALIFIER_QUOTE
:
823 #if defined(QUALIFIER_STICKY)
824 case QUALIFIER_STICKY
:
826 #if defined(QUALIFIER_VARSIZE)
827 case QUALIFIER_VARSIZE
:
829 #if defined(QUALIFIER_PARAM)
830 case QUALIFIER_PARAM
:
832 #if defined(QUALIFIER_FIXED_SIZE)
833 case QUALIFIER_FIXED_SIZE
:
835 #if defined(QUALIFIER_ROUNDING_UPPER)
836 case QUALIFIER_ROUNDING_UPPER
:
844 /*************************************************************************
847 #if defined(USE_LOCALE)
849 TrioSetLocale(TRIO_NOARGS
)
851 internalLocaleValues
= (struct lconv
*)localeconv();
852 if (internalLocaleValues
)
854 if ((internalLocaleValues
->decimal_point
) &&
855 (internalLocaleValues
->decimal_point
[0] != NIL
))
857 internalDecimalPointLength
= trio_length(internalLocaleValues
->decimal_point
);
858 if (internalDecimalPointLength
== 1)
860 internalDecimalPoint
= internalLocaleValues
->decimal_point
[0];
864 internalDecimalPoint
= NIL
;
865 trio_copy_max(internalDecimalPointString
,
866 sizeof(internalDecimalPointString
),
867 internalLocaleValues
->decimal_point
);
870 if ((internalLocaleValues
->thousands_sep
) &&
871 (internalLocaleValues
->thousands_sep
[0] != NIL
))
873 trio_copy_max(internalThousandSeparator
,
874 sizeof(internalThousandSeparator
),
875 internalLocaleValues
->thousands_sep
);
876 internalThousandSeparatorLength
= trio_length(internalThousandSeparator
);
878 if ((internalLocaleValues
->grouping
) &&
879 (internalLocaleValues
->grouping
[0] != NIL
))
881 trio_copy_max(internalGrouping
,
882 sizeof(internalGrouping
),
883 internalLocaleValues
->grouping
);
887 #endif /* defined(USE_LOCALE) */
890 TrioCalcThousandSeparatorLength
896 int step
= NO_GROUPING
;
897 char *groupingPointer
= internalGrouping
;
901 if (*groupingPointer
== CHAR_MAX
)
903 /* Disable grouping */
906 else if (*groupingPointer
== 0)
908 /* Repeat last group */
909 if (step
== NO_GROUPING
)
911 /* Error in locale */
917 step
= *groupingPointer
++;
920 count
+= internalThousandSeparatorLength
;
929 TRIO_PRIVATE BOOLEAN_T
930 TrioFollowedBySeparator
931 TRIO_ARGS1((position
),
936 char *groupingPointer
= internalGrouping
;
943 if (*groupingPointer
== CHAR_MAX
)
945 /* Disable grouping */
948 else if (*groupingPointer
!= 0)
950 step
= *groupingPointer
++;
956 return (position
== 0);
962 /*************************************************************************
965 * Get the %n$ position.
969 TRIO_ARGS2((format
, indexPointer
),
970 TRIO_CONST
char *format
,
976 int index
= *indexPointer
;
978 number
= (int)trio_to_long(&format
[index
], &tmpformat
, BASE_DECIMAL
);
979 index
= (int)(tmpformat
- format
);
980 if ((number
!= 0) && (QUALIFIER_POSITION
== format
[index
++]))
982 *indexPointer
= index
;
984 * number is decreased by 1, because n$ starts from 1, whereas
985 * the array it is indexing starts from 0.
994 /*************************************************************************
997 * Find registered user-defined specifier.
998 * The prev argument is used for optimization only.
1000 TRIO_PRIVATE trio_userdef_t
*
1002 TRIO_ARGS2((name
, prev
),
1003 TRIO_CONST
char *name
,
1004 trio_userdef_t
**prev
)
1006 trio_userdef_t
*def
;
1008 if (internalEnterCriticalRegion
)
1009 (void)internalEnterCriticalRegion(NULL
);
1011 for (def
= internalUserDef
; def
; def
= def
->next
)
1013 /* Case-sensitive string comparison */
1014 if (trio_equal_case(def
->name
, name
))
1021 if (internalLeaveCriticalRegion
)
1022 (void)internalLeaveCriticalRegion(NULL
);
1028 /*************************************************************************
1032 * Calculate pow(base, exponent), where number and exponent are integers.
1034 TRIO_PRIVATE trio_long_double_t
1036 TRIO_ARGS2((number
, exponent
),
1040 trio_long_double_t result
;
1046 /* Speed up calculation of common cases */
1048 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E-1);
1051 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+0);
1054 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+1);
1057 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+2);
1060 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+3);
1063 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+4);
1066 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+5);
1069 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+6);
1072 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+7);
1075 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+8);
1078 result
= powl((trio_long_double_t
)number
,
1079 (trio_long_double_t
)exponent
);
1085 return powl((trio_long_double_t
)number
, (trio_long_double_t
)exponent
);
1090 /*************************************************************************
1095 TRIO_ARGS2((number
, base
),
1103 /* xlC crashes on log(0) */
1104 result
= (number
== 0.0) ? trio_ninf() : trio_nan();
1110 result
= log10(number
);
1114 result
= log10(number
) / log10((double)base
);
1120 /*************************************************************************
1130 case BASE_BINARY
: return 1.0;
1131 case BASE_OCTAL
: return 3.0;
1132 case BASE_DECIMAL
: return 3.321928094887362345;
1133 case BASE_HEX
: return 4.0;
1134 default : return TrioLogarithm((double)base
, 2);
1138 /*************************************************************************
1142 * Parse the format string
1146 TRIO_ARGS5((type
, format
, parameters
, arglist
, argarray
),
1148 TRIO_CONST
char *format
,
1149 trio_parameter_t
*parameters
,
1151 trio_pointer_t
*argarray
)
1153 /* Count the number of times a parameter is referenced */
1154 unsigned short usedEntries
[MAX_PARAMETERS
];
1155 /* Parameter counters */
1156 int parameterPosition
;
1159 /* Utility variables */
1160 unsigned long flags
;
1165 int index
; /* Index into formatting string */
1166 int dots
; /* Count number of dots in modifier part */
1167 BOOLEAN_T positional
; /* Does the specifier have a positional? */
1168 BOOLEAN_T gotSticky
= FALSE
; /* Are there any sticky modifiers at all? */
1170 * indices specifies the order in which the parameters must be
1171 * read from the va_args (this is necessary to handle positionals)
1173 int indices
[MAX_PARAMETERS
];
1175 /* Various variables */
1177 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1185 /* One and only one of arglist and argarray must be used */
1186 assert((arglist
!= NULL
) ^ (argarray
!= NULL
));
1189 * The 'parameters' array is not initialized, but we need to
1190 * know which entries we have used.
1192 memset(usedEntries
, 0, sizeof(usedEntries
));
1196 parameterPosition
= 0;
1197 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1198 (void)mblen(NULL
, 0);
1201 while (format
[index
])
1203 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1204 if (! isascii(format
[index
]))
1207 * Multibyte characters cannot be legal specifiers or
1208 * modifiers, so we skip over them.
1210 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
1211 index
+= (charlen
> 0) ? charlen
: 1;
1212 continue; /* while */
1214 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
1215 if (CHAR_IDENTIFIER
== format
[index
++])
1217 if (CHAR_IDENTIFIER
== format
[index
])
1220 continue; /* while */
1225 currentParam
= TrioGetPosition(format
, &index
);
1226 positional
= (NO_POSITION
!= currentParam
);
1229 /* We have no positional, get the next counter */
1230 currentParam
= parameterPosition
;
1232 if(currentParam
>= MAX_PARAMETERS
)
1234 /* Bail out completely to make the error more obvious */
1235 return TRIO_ERROR_RETURN(TRIO_ETOOMANY
, index
);
1238 if (currentParam
> maxParam
)
1239 maxParam
= currentParam
;
1241 /* Default values */
1243 precision
= NO_PRECISION
;
1247 while (TrioIsQualifier(format
[index
]))
1249 ch
= format
[index
++];
1253 case QUALIFIER_SPACE
:
1254 flags
|= FLAGS_SPACE
;
1257 case QUALIFIER_PLUS
:
1258 flags
|= FLAGS_SHOWSIGN
;
1261 case QUALIFIER_MINUS
:
1262 flags
|= FLAGS_LEFTADJUST
;
1263 flags
&= ~FLAGS_NILPADDING
;
1266 case QUALIFIER_ALTERNATIVE
:
1267 flags
|= FLAGS_ALTERNATIVE
;
1271 if (dots
== 0) /* Precision */
1275 /* Skip if no precision */
1276 if (QUALIFIER_DOT
== format
[index
])
1279 /* After the first dot we have the precision */
1280 flags
|= FLAGS_PRECISION
;
1281 if ((QUALIFIER_STAR
== format
[index
])
1282 #if defined(QUALIFIER_PARAM)
1283 || (QUALIFIER_PARAM
== format
[index
])
1288 flags
|= FLAGS_PRECISION_PARAMETER
;
1290 precision
= TrioGetPosition(format
, &index
);
1291 if (precision
== NO_POSITION
)
1293 parameterPosition
++;
1295 precision
= parameterPosition
;
1298 precision
= currentParam
;
1299 currentParam
= precision
+ 1;
1305 currentParam
= precision
+ 1;
1306 if (width
> maxParam
)
1307 maxParam
= precision
;
1309 if (currentParam
> maxParam
)
1310 maxParam
= currentParam
;
1314 precision
= trio_to_long(&format
[index
],
1317 index
= (int)(tmpformat
- format
);
1320 else if (dots
== 1) /* Base */
1324 /* After the second dot we have the base */
1325 flags
|= FLAGS_BASE
;
1326 if ((QUALIFIER_STAR
== format
[index
])
1327 #if defined(QUALIFIER_PARAM)
1328 || (QUALIFIER_PARAM
== format
[index
])
1333 flags
|= FLAGS_BASE_PARAMETER
;
1334 base
= TrioGetPosition(format
, &index
);
1335 if (base
== NO_POSITION
)
1337 parameterPosition
++;
1339 base
= parameterPosition
;
1342 base
= currentParam
;
1343 currentParam
= base
+ 1;
1349 currentParam
= base
+ 1;
1350 if (base
> maxParam
)
1353 if (currentParam
> maxParam
)
1354 maxParam
= currentParam
;
1358 base
= trio_to_long(&format
[index
],
1361 if (base
> MAX_BASE
)
1362 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1363 index
= (int)(tmpformat
- format
);
1368 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1370 break; /* QUALIFIER_DOT */
1372 #if defined(QUALIFIER_PARAM)
1373 case QUALIFIER_PARAM
:
1377 case QUALIFIER_STAR
:
1378 /* This has different meanings for print and scan */
1379 if (TYPE_PRINT
== type
)
1381 /* Read with from parameter */
1382 flags
|= (FLAGS_WIDTH
| FLAGS_WIDTH_PARAMETER
);
1383 width
= TrioGetPosition(format
, &index
);
1384 if (width
== NO_POSITION
)
1386 parameterPosition
++;
1388 width
= parameterPosition
;
1391 width
= currentParam
;
1392 currentParam
= width
+ 1;
1398 currentParam
= width
+ 1;
1399 if (width
> maxParam
)
1402 if (currentParam
> maxParam
)
1403 maxParam
= currentParam
;
1407 /* Scan, but do not store result */
1408 flags
|= FLAGS_IGNORE
;
1411 break; /* QUALIFIER_STAR */
1414 if (! (flags
& FLAGS_LEFTADJUST
))
1415 flags
|= FLAGS_NILPADDING
;
1417 case '1': case '2': case '3': case '4':
1418 case '5': case '6': case '7': case '8': case '9':
1419 flags
|= FLAGS_WIDTH
;
1420 /* &format[index - 1] is used to "rewind" the read
1421 * character from format
1423 width
= trio_to_long(&format
[index
- 1],
1426 index
= (int)(tmpformat
- format
);
1429 case QUALIFIER_SHORT
:
1430 if (flags
& FLAGS_SHORTSHORT
)
1431 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1432 else if (flags
& FLAGS_SHORT
)
1433 flags
|= FLAGS_SHORTSHORT
;
1435 flags
|= FLAGS_SHORT
;
1438 case QUALIFIER_LONG
:
1439 if (flags
& FLAGS_QUAD
)
1440 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1441 else if (flags
& FLAGS_LONG
)
1442 flags
|= FLAGS_QUAD
;
1444 flags
|= FLAGS_LONG
;
1447 case QUALIFIER_LONG_UPPER
:
1448 flags
|= FLAGS_LONGDOUBLE
;
1451 #if defined(QUALIFIER_SIZE_T)
1452 case QUALIFIER_SIZE_T
:
1453 flags
|= FLAGS_SIZE_T
;
1454 /* Modify flags for later truncation of number */
1455 if (sizeof(size_t) == sizeof(trio_ulonglong_t
))
1456 flags
|= FLAGS_QUAD
;
1457 else if (sizeof(size_t) == sizeof(long))
1458 flags
|= FLAGS_LONG
;
1462 #if defined(QUALIFIER_PTRDIFF_T)
1463 case QUALIFIER_PTRDIFF_T
:
1464 flags
|= FLAGS_PTRDIFF_T
;
1465 if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t
))
1466 flags
|= FLAGS_QUAD
;
1467 else if (sizeof(ptrdiff_t) == sizeof(long))
1468 flags
|= FLAGS_LONG
;
1472 #if defined(QUALIFIER_INTMAX_T)
1473 case QUALIFIER_INTMAX_T
:
1474 flags
|= FLAGS_INTMAX_T
;
1475 if (sizeof(trio_intmax_t
) == sizeof(trio_ulonglong_t
))
1476 flags
|= FLAGS_QUAD
;
1477 else if (sizeof(trio_intmax_t
) == sizeof(long))
1478 flags
|= FLAGS_LONG
;
1482 #if defined(QUALIFIER_QUAD)
1483 case QUALIFIER_QUAD
:
1484 flags
|= FLAGS_QUAD
;
1488 #if defined(QUALIFIER_FIXED_SIZE)
1489 case QUALIFIER_FIXED_SIZE
:
1490 if (flags
& FLAGS_FIXED_SIZE
)
1491 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1493 if (flags
& (FLAGS_ALL_SIZES
| FLAGS_LONGDOUBLE
|
1494 FLAGS_WIDECHAR
| FLAGS_VARSIZE_PARAMETER
))
1495 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1497 if ((format
[index
] == '6') &&
1498 (format
[index
+ 1] == '4'))
1500 varsize
= sizeof(trio_int64_t
);
1503 else if ((format
[index
] == '3') &&
1504 (format
[index
+ 1] == '2'))
1506 varsize
= sizeof(trio_int32_t
);
1509 else if ((format
[index
] == '1') &&
1510 (format
[index
+ 1] == '6'))
1512 varsize
= sizeof(trio_int16_t
);
1515 else if (format
[index
] == '8')
1517 varsize
= sizeof(trio_int8_t
);
1521 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1523 flags
|= FLAGS_FIXED_SIZE
;
1527 #if defined(QUALIFIER_WIDECHAR)
1528 case QUALIFIER_WIDECHAR
:
1529 flags
|= FLAGS_WIDECHAR
;
1533 #if defined(QUALIFIER_SIZE_T_UPPER)
1534 case QUALIFIER_SIZE_T_UPPER
:
1538 #if defined(QUALIFIER_QUOTE)
1539 case QUALIFIER_QUOTE
:
1540 flags
|= FLAGS_QUOTE
;
1544 #if defined(QUALIFIER_STICKY)
1545 case QUALIFIER_STICKY
:
1546 flags
|= FLAGS_STICKY
;
1551 #if defined(QUALIFIER_VARSIZE)
1552 case QUALIFIER_VARSIZE
:
1553 flags
|= FLAGS_VARSIZE_PARAMETER
;
1554 parameterPosition
++;
1556 varsize
= parameterPosition
;
1559 varsize
= currentParam
;
1560 currentParam
= varsize
+ 1;
1562 if (currentParam
> maxParam
)
1563 maxParam
= currentParam
;
1567 #if defined(QUALIFIER_ROUNDING_UPPER)
1568 case QUALIFIER_ROUNDING_UPPER
:
1569 flags
|= FLAGS_ROUNDING
;
1574 /* Bail out completely to make the error more obvious */
1575 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1577 } /* while qualifier */
1580 * Parameters only need the type and value. The value is
1583 if (flags
& FLAGS_WIDTH_PARAMETER
)
1585 usedEntries
[width
] += 1;
1586 parameters
[pos
].type
= FORMAT_PARAMETER
;
1587 parameters
[pos
].flags
= 0;
1588 indices
[width
] = pos
;
1591 if (flags
& FLAGS_PRECISION_PARAMETER
)
1593 usedEntries
[precision
] += 1;
1594 parameters
[pos
].type
= FORMAT_PARAMETER
;
1595 parameters
[pos
].flags
= 0;
1596 indices
[precision
] = pos
;
1599 if (flags
& FLAGS_BASE_PARAMETER
)
1601 usedEntries
[base
] += 1;
1602 parameters
[pos
].type
= FORMAT_PARAMETER
;
1603 parameters
[pos
].flags
= 0;
1604 indices
[base
] = pos
;
1607 if (flags
& FLAGS_VARSIZE_PARAMETER
)
1609 usedEntries
[varsize
] += 1;
1610 parameters
[pos
].type
= FORMAT_PARAMETER
;
1611 parameters
[pos
].flags
= 0;
1612 indices
[varsize
] = pos
;
1616 indices
[currentParam
] = pos
;
1618 switch (format
[index
++])
1620 #if defined(SPECIFIER_CHAR_UPPER)
1621 case SPECIFIER_CHAR_UPPER
:
1622 flags
|= FLAGS_WIDECHAR
;
1625 case SPECIFIER_CHAR
:
1626 if (flags
& FLAGS_LONG
)
1627 flags
|= FLAGS_WIDECHAR
;
1628 else if (flags
& FLAGS_SHORT
)
1629 flags
&= ~FLAGS_WIDECHAR
;
1630 parameters
[pos
].type
= FORMAT_CHAR
;
1633 #if defined(SPECIFIER_STRING_UPPER)
1634 case SPECIFIER_STRING_UPPER
:
1635 flags
|= FLAGS_WIDECHAR
;
1638 case SPECIFIER_STRING
:
1639 if (flags
& FLAGS_LONG
)
1640 flags
|= FLAGS_WIDECHAR
;
1641 else if (flags
& FLAGS_SHORT
)
1642 flags
&= ~FLAGS_WIDECHAR
;
1643 parameters
[pos
].type
= FORMAT_STRING
;
1646 case SPECIFIER_GROUP
:
1647 if (TYPE_SCAN
== type
)
1650 parameters
[pos
].type
= FORMAT_GROUP
;
1651 if (format
[index
] == QUALIFIER_CIRCUMFLEX
)
1653 if (format
[index
] == SPECIFIER_UNGROUP
)
1655 if (format
[index
] == QUALIFIER_MINUS
)
1657 /* Skip nested brackets */
1658 while (format
[index
] != NIL
)
1660 if (format
[index
] == SPECIFIER_GROUP
)
1664 else if (format
[index
] == SPECIFIER_UNGROUP
)
1677 case SPECIFIER_INTEGER
:
1678 parameters
[pos
].type
= FORMAT_INT
;
1681 case SPECIFIER_UNSIGNED
:
1682 flags
|= FLAGS_UNSIGNED
;
1683 parameters
[pos
].type
= FORMAT_INT
;
1686 case SPECIFIER_DECIMAL
:
1687 /* Disable base modifier */
1688 flags
&= ~FLAGS_BASE_PARAMETER
;
1689 base
= BASE_DECIMAL
;
1690 parameters
[pos
].type
= FORMAT_INT
;
1693 case SPECIFIER_OCTAL
:
1694 flags
&= ~FLAGS_BASE_PARAMETER
;
1696 parameters
[pos
].type
= FORMAT_INT
;
1699 #if defined(SPECIFIER_BINARY)
1700 case SPECIFIER_BINARY_UPPER
:
1701 flags
|= FLAGS_UPPER
;
1703 case SPECIFIER_BINARY
:
1704 flags
|= FLAGS_NILPADDING
;
1705 flags
&= ~FLAGS_BASE_PARAMETER
;
1707 parameters
[pos
].type
= FORMAT_INT
;
1711 case SPECIFIER_HEX_UPPER
:
1712 flags
|= FLAGS_UPPER
;
1715 flags
|= FLAGS_UNSIGNED
;
1716 flags
&= ~FLAGS_BASE_PARAMETER
;
1718 parameters
[pos
].type
= FORMAT_INT
;
1721 case SPECIFIER_FLOAT_E_UPPER
:
1722 flags
|= FLAGS_UPPER
;
1724 case SPECIFIER_FLOAT_E
:
1725 flags
|= FLAGS_FLOAT_E
;
1726 parameters
[pos
].type
= FORMAT_DOUBLE
;
1729 case SPECIFIER_FLOAT_G_UPPER
:
1730 flags
|= FLAGS_UPPER
;
1732 case SPECIFIER_FLOAT_G
:
1733 flags
|= FLAGS_FLOAT_G
;
1734 parameters
[pos
].type
= FORMAT_DOUBLE
;
1737 case SPECIFIER_FLOAT_F_UPPER
:
1738 flags
|= FLAGS_UPPER
;
1740 case SPECIFIER_FLOAT_F
:
1741 parameters
[pos
].type
= FORMAT_DOUBLE
;
1744 case SPECIFIER_POINTER
:
1745 if (sizeof(trio_pointer_t
) == sizeof(trio_ulonglong_t
))
1746 flags
|= FLAGS_QUAD
;
1747 else if (sizeof(trio_pointer_t
) == sizeof(long))
1748 flags
|= FLAGS_LONG
;
1749 parameters
[pos
].type
= FORMAT_POINTER
;
1752 case SPECIFIER_COUNT
:
1753 parameters
[pos
].type
= FORMAT_COUNT
;
1756 #if defined(SPECIFIER_HEXFLOAT)
1757 # if defined(SPECIFIER_HEXFLOAT_UPPER)
1758 case SPECIFIER_HEXFLOAT_UPPER
:
1759 flags
|= FLAGS_UPPER
;
1762 case SPECIFIER_HEXFLOAT
:
1764 parameters
[pos
].type
= FORMAT_DOUBLE
;
1768 #if defined(FORMAT_ERRNO)
1769 case SPECIFIER_ERRNO
:
1770 parameters
[pos
].type
= FORMAT_ERRNO
;
1774 #if defined(SPECIFIER_USER_DEFINED_BEGIN)
1775 case SPECIFIER_USER_DEFINED_BEGIN
:
1778 int without_namespace
= TRUE
;
1780 parameters
[pos
].type
= FORMAT_USER_DEFINED
;
1781 parameters
[pos
].user_name
[0] = NIL
;
1782 tmpformat
= (char *)&format
[index
];
1784 while ((ch
= format
[index
]))
1787 if (ch
== SPECIFIER_USER_DEFINED_END
)
1789 if (without_namespace
)
1791 /* We must get the handle first */
1792 parameters
[pos
].type
= FORMAT_PARAMETER
;
1793 parameters
[pos
].indexAfterSpecifier
= index
;
1794 parameters
[pos
].flags
= FLAGS_USER_DEFINED
;
1795 /* Adjust parameters for insertion of new one */
1797 usedEntries
[currentParam
] += 1;
1798 parameters
[pos
].type
= FORMAT_USER_DEFINED
;
1800 indices
[currentParam
] = pos
;
1801 if (currentParam
> maxParam
)
1802 maxParam
= currentParam
;
1804 /* Copy the user data */
1805 max
= (unsigned int)(&format
[index
] - tmpformat
);
1806 if (max
> MAX_USER_DATA
)
1807 max
= MAX_USER_DATA
;
1808 trio_copy_max(parameters
[pos
].user_data
,
1813 if (ch
== SPECIFIER_USER_DEFINED_SEPARATOR
)
1815 without_namespace
= FALSE
;
1816 /* Copy the namespace for later looking-up */
1817 max
= (int)(&format
[index
] - tmpformat
);
1818 if (max
> MAX_USER_NAME
)
1819 max
= MAX_USER_NAME
;
1820 trio_copy_max(parameters
[pos
].user_name
,
1823 tmpformat
= (char *)&format
[index
];
1826 if (ch
!= SPECIFIER_USER_DEFINED_END
)
1827 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1830 #endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
1833 /* Bail out completely to make the error more obvious */
1834 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1837 /* Count the number of times this entry has been used */
1838 usedEntries
[currentParam
] += 1;
1840 /* Find last sticky parameters */
1841 if (gotSticky
&& !(flags
& FLAGS_STICKY
))
1843 for (i
= pos
- 1; i
>= 0; i
--)
1845 if (parameters
[i
].type
== FORMAT_PARAMETER
)
1847 if ((parameters
[i
].flags
& FLAGS_STICKY
) &&
1848 (parameters
[i
].type
== parameters
[pos
].type
))
1850 /* Do not overwrite current qualifiers */
1851 flags
|= (parameters
[i
].flags
& (unsigned long)~FLAGS_STICKY
);
1852 if (width
== NO_WIDTH
)
1853 width
= parameters
[i
].width
;
1854 if (precision
== NO_PRECISION
)
1855 precision
= parameters
[i
].precision
;
1856 if (base
== NO_BASE
)
1857 base
= parameters
[i
].base
;
1863 parameters
[pos
].indexAfterSpecifier
= index
;
1864 parameters
[pos
].flags
= flags
;
1865 parameters
[pos
].width
= width
;
1866 parameters
[pos
].precision
= precision
;
1867 parameters
[pos
].base
= (base
== NO_BASE
) ? BASE_DECIMAL
: base
;
1868 parameters
[pos
].varsize
= varsize
;
1872 parameterPosition
++;
1874 } /* if identifier */
1876 } /* while format characters left */
1878 for (num
= 0; num
<= maxParam
; num
++)
1880 if (usedEntries
[num
] != 1)
1882 if (usedEntries
[num
] == 0) /* gap detected */
1883 return TRIO_ERROR_RETURN(TRIO_EGAP
, num
);
1884 else /* double references detected */
1885 return TRIO_ERROR_RETURN(TRIO_EDBLREF
, num
);
1891 * FORMAT_PARAMETERS are only present if they must be read,
1892 * so it makes no sense to check the ignore flag (besides,
1893 * the flags variable is not set for that particular type)
1895 if ((parameters
[i
].type
!= FORMAT_PARAMETER
) &&
1896 (parameters
[i
].flags
& FLAGS_IGNORE
))
1897 continue; /* for all arguments */
1900 * The stack arguments are read according to ANSI C89
1901 * default argument promotions:
1905 * unsigned char = unsigned int
1906 * unsigned short = unsigned int
1909 * In addition to the ANSI C89 these types are read (the
1910 * default argument promotions of C99 has not been
1919 switch (parameters
[i
].type
)
1924 if (flags
& FLAGS_WIDECHAR
)
1926 parameters
[i
].data
.wstring
= (argarray
== NULL
)
1927 ? va_arg(*arglist
, trio_wchar_t
*)
1928 : (trio_wchar_t
*)(argarray
[num
]);
1933 parameters
[i
].data
.string
= (argarray
== NULL
)
1934 ? va_arg(*arglist
, char *)
1935 : (char *)(argarray
[num
]);
1939 #if defined(FORMAT_USER_DEFINED)
1940 case FORMAT_USER_DEFINED
:
1942 case FORMAT_POINTER
:
1944 case FORMAT_UNKNOWN
:
1945 parameters
[i
].data
.pointer
= (argarray
== NULL
)
1946 ? va_arg(*arglist
, trio_pointer_t
)
1952 if (TYPE_SCAN
== type
)
1954 if (argarray
== NULL
)
1955 parameters
[i
].data
.pointer
=
1956 (trio_pointer_t
)va_arg(*arglist
, trio_pointer_t
);
1959 if (parameters
[i
].type
== FORMAT_CHAR
)
1960 parameters
[i
].data
.pointer
=
1961 (trio_pointer_t
)((char *)argarray
[num
]);
1962 else if (parameters
[i
].flags
& FLAGS_SHORT
)
1963 parameters
[i
].data
.pointer
=
1964 (trio_pointer_t
)((short *)argarray
[num
]);
1966 parameters
[i
].data
.pointer
=
1967 (trio_pointer_t
)((int *)argarray
[num
]);
1972 #if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
1973 if (parameters
[i
].flags
1974 & (FLAGS_VARSIZE_PARAMETER
| FLAGS_FIXED_SIZE
))
1976 if (parameters
[i
].flags
& FLAGS_VARSIZE_PARAMETER
)
1979 * Variable sizes are mapped onto the fixed sizes, in
1980 * accordance with integer promotion.
1982 * Please note that this may not be portable, as we
1983 * only guess the size, not the layout of the numbers.
1984 * For example, if int is little-endian, and long is
1985 * big-endian, then this will fail.
1987 varsize
= (int)parameters
[parameters
[i
].varsize
].data
.number
.as_unsigned
;
1991 /* Used for the I<bits> modifiers */
1992 varsize
= parameters
[i
].varsize
;
1994 parameters
[i
].flags
&= ~FLAGS_ALL_VARSIZES
;
1996 if (varsize
<= (int)sizeof(int))
1998 else if (varsize
<= (int)sizeof(long))
1999 parameters
[i
].flags
|= FLAGS_LONG
;
2000 #if defined(QUALIFIER_INTMAX_T)
2001 else if (varsize
<= (int)sizeof(trio_longlong_t
))
2002 parameters
[i
].flags
|= FLAGS_QUAD
;
2004 parameters
[i
].flags
|= FLAGS_INTMAX_T
;
2007 parameters
[i
].flags
|= FLAGS_QUAD
;
2010 #endif /* defined(QUALIFIER_VARSIZE) */
2011 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
2012 if (parameters
[i
].flags
& FLAGS_SIZE_T
)
2013 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2014 ? (trio_uintmax_t
)va_arg(*arglist
, size_t)
2015 : (trio_uintmax_t
)(*((size_t *)argarray
[num
]));
2018 #if defined(QUALIFIER_PTRDIFF_T)
2019 if (parameters
[i
].flags
& FLAGS_PTRDIFF_T
)
2020 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2021 ? (trio_uintmax_t
)va_arg(*arglist
, ptrdiff_t)
2022 : (trio_uintmax_t
)(*((ptrdiff_t *)argarray
[num
]));
2025 #if defined(QUALIFIER_INTMAX_T)
2026 if (parameters
[i
].flags
& FLAGS_INTMAX_T
)
2027 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2028 ? (trio_uintmax_t
)va_arg(*arglist
, trio_intmax_t
)
2029 : (trio_uintmax_t
)(*((trio_intmax_t
*)argarray
[num
]));
2032 if (parameters
[i
].flags
& FLAGS_QUAD
)
2033 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2034 ? (trio_uintmax_t
)va_arg(*arglist
, trio_ulonglong_t
)
2035 : (trio_uintmax_t
)(*((trio_ulonglong_t
*)argarray
[num
]));
2036 else if (parameters
[i
].flags
& FLAGS_LONG
)
2037 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2038 ? (trio_uintmax_t
)va_arg(*arglist
, long)
2039 : (trio_uintmax_t
)(*((long *)argarray
[num
]));
2042 if (argarray
== NULL
)
2043 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)va_arg(*arglist
, int);
2046 if (parameters
[i
].type
== FORMAT_CHAR
)
2047 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((char *)argarray
[num
]));
2048 else if (parameters
[i
].flags
& FLAGS_SHORT
)
2049 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((short *)argarray
[num
]));
2051 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((int *)argarray
[num
]));
2057 case FORMAT_PARAMETER
:
2059 * The parameter for the user-defined specifier is a pointer,
2060 * whereas the rest (width, precision, base) uses an integer.
2062 if (parameters
[i
].flags
& FLAGS_USER_DEFINED
)
2063 parameters
[i
].data
.pointer
= (argarray
== NULL
)
2064 ? va_arg(*arglist
, trio_pointer_t
)
2067 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2068 ? (trio_uintmax_t
)va_arg(*arglist
, int)
2069 : (trio_uintmax_t
)(*((int *)argarray
[num
]));
2073 if (TYPE_SCAN
== type
)
2075 if (parameters
[i
].flags
& FLAGS_LONGDOUBLE
)
2076 parameters
[i
].data
.longdoublePointer
= (argarray
== NULL
)
2077 ? va_arg(*arglist
, trio_long_double_t
*)
2078 : (trio_long_double_t
*)argarray
[num
];
2081 if (parameters
[i
].flags
& FLAGS_LONG
)
2082 parameters
[i
].data
.doublePointer
= (argarray
== NULL
)
2083 ? va_arg(*arglist
, double *)
2084 : (double *)argarray
[num
];
2086 parameters
[i
].data
.doublePointer
= (argarray
== NULL
)
2087 ? (double *)va_arg(*arglist
, float *)
2088 : (double *)((float *)argarray
[num
]);
2093 if (parameters
[i
].flags
& FLAGS_LONGDOUBLE
)
2094 parameters
[i
].data
.longdoubleNumber
= (argarray
== NULL
)
2095 ? va_arg(*arglist
, trio_long_double_t
)
2096 : (trio_long_double_t
)(*((trio_long_double_t
*)argarray
[num
]));
2099 if (argarray
== NULL
)
2100 parameters
[i
].data
.longdoubleNumber
=
2101 (trio_long_double_t
)va_arg(*arglist
, double);
2104 if (parameters
[i
].flags
& FLAGS_SHORT
)
2105 parameters
[i
].data
.longdoubleNumber
=
2106 (trio_long_double_t
)(*((float *)argarray
[num
]));
2108 parameters
[i
].data
.longdoubleNumber
=
2109 (trio_long_double_t
)(*((double *)argarray
[num
]));
2115 #if defined(FORMAT_ERRNO)
2117 parameters
[i
].data
.errorNumber
= save_errno
;
2124 } /* for all specifiers */
2129 /*************************************************************************
2133 ************************************************************************/
2136 /*************************************************************************
2141 * The complexity of this function is a result of the complexity
2142 * of the dependencies of the flags.
2146 TRIO_ARGS6((self
, number
, flags
, width
, precision
, base
),
2148 trio_uintmax_t number
,
2149 unsigned long flags
,
2154 BOOLEAN_T isNegative
;
2155 char buffer
[MAX_CHARS_IN(trio_uintmax_t
) * (1 + MAX_LOCALE_SEPARATOR_LENGTH
) + 1];
2158 TRIO_CONST
char *digits
;
2164 assert(VALID(self
));
2165 assert(VALID(self
->OutStream
));
2166 assert(((base
>= MIN_BASE
) && (base
<= MAX_BASE
)) || (base
== NO_BASE
));
2168 digits
= (flags
& FLAGS_UPPER
) ? internalDigitsUpper
: internalDigitsLower
;
2169 if (base
== NO_BASE
)
2170 base
= BASE_DECIMAL
;
2172 isNegative
= (flags
& FLAGS_UNSIGNED
)
2174 : ((trio_intmax_t
)number
< 0);
2176 number
= -((trio_intmax_t
)number
);
2178 if (flags
& FLAGS_QUAD
)
2179 number
&= (trio_ulonglong_t
)-1;
2180 else if (flags
& FLAGS_LONG
)
2181 number
&= (unsigned long)-1;
2183 number
&= (unsigned int)-1;
2186 pointer
= bufferend
= &buffer
[sizeof(buffer
) - 1];
2188 for (i
= 1; i
< (int)sizeof(buffer
); i
++)
2190 *pointer
-- = digits
[number
% base
];
2195 if ((flags
& FLAGS_QUOTE
) && TrioFollowedBySeparator(i
+ 1))
2198 * We are building the number from the least significant
2199 * to the most significant digit, so we have to copy the
2200 * thousand separator backwards
2202 length
= internalThousandSeparatorLength
;
2203 if (((int)(pointer
- buffer
) - length
) > 0)
2205 p
= &internalThousandSeparator
[length
- 1];
2206 while (length
-- > 0)
2213 width
-= (bufferend
- pointer
) - 1;
2215 /* Adjust precision */
2216 if (NO_PRECISION
!= precision
)
2218 precision
-= (bufferend
- pointer
) - 1;
2221 flags
|= FLAGS_NILPADDING
;
2224 /* Adjust width further */
2225 if (isNegative
|| (flags
& FLAGS_SHOWSIGN
) || (flags
& FLAGS_SPACE
))
2227 if (flags
& FLAGS_ALTERNATIVE
)
2243 /* Output prefixes spaces if needed */
2244 if (! ((flags
& FLAGS_LEFTADJUST
) ||
2245 ((flags
& FLAGS_NILPADDING
) && (precision
== NO_PRECISION
))))
2247 count
= (precision
== NO_PRECISION
) ? 0 : precision
;
2248 while (width
-- > count
)
2249 self
->OutStream(self
, CHAR_ADJUST
);
2252 /* width has been adjusted for signs and alternatives */
2254 self
->OutStream(self
, '-');
2255 else if (flags
& FLAGS_SHOWSIGN
)
2256 self
->OutStream(self
, '+');
2257 else if (flags
& FLAGS_SPACE
)
2258 self
->OutStream(self
, ' ');
2260 if (flags
& FLAGS_ALTERNATIVE
)
2265 self
->OutStream(self
, '0');
2266 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'B' : 'b');
2270 self
->OutStream(self
, '0');
2274 self
->OutStream(self
, '0');
2275 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2283 /* Output prefixed zero padding if needed */
2284 if (flags
& FLAGS_NILPADDING
)
2286 if (precision
== NO_PRECISION
)
2288 while (precision
-- > 0)
2290 self
->OutStream(self
, '0');
2295 /* Output the number itself */
2296 while (*(++pointer
))
2298 self
->OutStream(self
, *pointer
);
2301 /* Output trailing spaces if needed */
2302 if (flags
& FLAGS_LEFTADJUST
)
2305 self
->OutStream(self
, CHAR_ADJUST
);
2309 /*************************************************************************
2310 * TrioWriteStringCharacter
2313 * Output a single character of a string
2316 TrioWriteStringCharacter
2317 TRIO_ARGS3((self
, ch
, flags
),
2320 unsigned long flags
)
2322 if (flags
& FLAGS_ALTERNATIVE
)
2327 * Non-printable characters are converted to C escapes or
2328 * \number, if no C escape exists.
2330 self
->OutStream(self
, CHAR_BACKSLASH
);
2333 case '\007': self
->OutStream(self
, 'a'); break;
2334 case '\b': self
->OutStream(self
, 'b'); break;
2335 case '\f': self
->OutStream(self
, 'f'); break;
2336 case '\n': self
->OutStream(self
, 'n'); break;
2337 case '\r': self
->OutStream(self
, 'r'); break;
2338 case '\t': self
->OutStream(self
, 't'); break;
2339 case '\v': self
->OutStream(self
, 'v'); break;
2340 case '\\': self
->OutStream(self
, '\\'); break;
2342 self
->OutStream(self
, 'x');
2343 TrioWriteNumber(self
, (trio_uintmax_t
)ch
,
2344 FLAGS_UNSIGNED
| FLAGS_NILPADDING
,
2349 else if (ch
== CHAR_BACKSLASH
)
2351 self
->OutStream(self
, CHAR_BACKSLASH
);
2352 self
->OutStream(self
, CHAR_BACKSLASH
);
2356 self
->OutStream(self
, ch
);
2361 self
->OutStream(self
, ch
);
2365 /*************************************************************************
2373 TRIO_ARGS5((self
, string
, flags
, width
, precision
),
2375 TRIO_CONST
char *string
,
2376 unsigned long flags
,
2383 assert(VALID(self
));
2384 assert(VALID(self
->OutStream
));
2388 string
= internalNullString
;
2389 length
= sizeof(internalNullString
) - 1;
2390 /* Disable quoting for the null pointer */
2391 flags
&= (~FLAGS_QUOTE
);
2396 length
= trio_length(string
);
2398 if ((NO_PRECISION
!= precision
) &&
2399 (precision
< length
))
2405 if (flags
& FLAGS_QUOTE
)
2406 self
->OutStream(self
, CHAR_QUOTE
);
2408 if (! (flags
& FLAGS_LEFTADJUST
))
2411 self
->OutStream(self
, CHAR_ADJUST
);
2414 while (length
-- > 0)
2416 /* The ctype parameters must be an unsigned char (or EOF) */
2417 ch
= (int)((unsigned char)(*string
++));
2418 TrioWriteStringCharacter(self
, ch
, flags
);
2421 if (flags
& FLAGS_LEFTADJUST
)
2424 self
->OutStream(self
, CHAR_ADJUST
);
2426 if (flags
& FLAGS_QUOTE
)
2427 self
->OutStream(self
, CHAR_QUOTE
);
2430 /*************************************************************************
2431 * TrioWriteWideStringCharacter
2434 * Output a wide string as a multi-byte sequence
2438 TrioWriteWideStringCharacter
2439 TRIO_ARGS4((self
, wch
, flags
, width
),
2442 unsigned long flags
,
2449 char buffer
[MB_LEN_MAX
+ 1];
2451 if (width
== NO_WIDTH
)
2452 width
= sizeof(buffer
);
2454 size
= wctomb(buffer
, wch
);
2455 if ((size
<= 0) || (size
> width
) || (buffer
[0] == NIL
))
2460 while ((width
>= i
) && (width
-- > 0) && (i
-- > 0))
2462 /* The ctype parameters must be an unsigned char (or EOF) */
2463 ch
= (int)((unsigned char)(*string
++));
2464 TrioWriteStringCharacter(self
, ch
, flags
);
2468 #endif /* TRIO_WIDECHAR */
2470 /*************************************************************************
2471 * TrioWriteWideString
2474 * Output a wide character string as a multi-byte string
2479 TRIO_ARGS5((self
, wstring
, flags
, width
, precision
),
2481 TRIO_CONST trio_wchar_t
*wstring
,
2482 unsigned long flags
,
2489 assert(VALID(self
));
2490 assert(VALID(self
->OutStream
));
2492 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
2493 (void)mblen(NULL
, 0);
2496 if (wstring
== NULL
)
2498 TrioWriteString(self
, NULL
, flags
, width
, precision
);
2502 if (NO_PRECISION
== precision
)
2512 if (flags
& FLAGS_QUOTE
)
2513 self
->OutStream(self
, CHAR_QUOTE
);
2515 if (! (flags
& FLAGS_LEFTADJUST
))
2518 self
->OutStream(self
, CHAR_ADJUST
);
2523 size
= TrioWriteWideStringCharacter(self
, *wstring
++, flags
, length
);
2529 if (flags
& FLAGS_LEFTADJUST
)
2532 self
->OutStream(self
, CHAR_ADJUST
);
2534 if (flags
& FLAGS_QUOTE
)
2535 self
->OutStream(self
, CHAR_QUOTE
);
2537 #endif /* TRIO_WIDECHAR */
2539 /*************************************************************************
2542 * http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_211.htm
2544 * "5.2.4.2.2 paragraph #4
2546 * The accuracy [...] is implementation defined, as is the accuracy
2547 * of the conversion between floating-point internal representations
2548 * and string representations performed by the libray routine in
2551 /* FIXME: handle all instances of constant long-double number (L)
2552 * and *l() math functions.
2556 TRIO_ARGS6((self
, number
, flags
, width
, precision
, base
),
2558 trio_long_double_t number
,
2559 unsigned long flags
,
2564 trio_long_double_t integerNumber
;
2565 trio_long_double_t fractionNumber
;
2566 trio_long_double_t workNumber
;
2571 int integerThreshold
;
2572 int fractionThreshold
;
2575 unsigned int uExponent
= 0;
2577 trio_long_double_t dblBase
;
2578 trio_long_double_t dblIntegerBase
;
2579 trio_long_double_t dblFractionBase
;
2580 trio_long_double_t integerAdjust
;
2581 trio_long_double_t fractionAdjust
;
2582 BOOLEAN_T isNegative
;
2583 BOOLEAN_T isExponentNegative
= FALSE
;
2584 BOOLEAN_T requireTwoDigitExponent
;
2586 TRIO_CONST
char *digits
;
2587 char *groupingPointer
;
2590 BOOLEAN_T hasOnlyZeroes
;
2592 register int trailingZeroes
;
2593 BOOLEAN_T keepTrailingZeroes
;
2594 BOOLEAN_T keepDecimalPoint
;
2595 trio_long_double_t epsilon
;
2597 assert(VALID(self
));
2598 assert(VALID(self
->OutStream
));
2599 assert(((base
>= MIN_BASE
) && (base
<= MAX_BASE
)) || (base
== NO_BASE
));
2601 /* Determine sign and look for special quantities */
2602 switch (trio_fpclassify_and_signbit(number
, &isNegative
))
2605 TrioWriteString(self
,
2606 (flags
& FLAGS_UPPER
)
2609 flags
, width
, precision
);
2612 case TRIO_FP_INFINITE
:
2615 /* Negative infinity */
2616 TrioWriteString(self
,
2617 (flags
& FLAGS_UPPER
)
2618 ? "-" INFINITE_UPPER
2619 : "-" INFINITE_LOWER
,
2620 flags
, width
, precision
);
2625 /* Positive infinity */
2626 TrioWriteString(self
,
2627 (flags
& FLAGS_UPPER
)
2630 flags
, width
, precision
);
2639 /* Normal numbers */
2640 if (flags
& FLAGS_LONGDOUBLE
)
2642 baseDigits
= (base
== 10)
2644 : (int)floor(LDBL_MANT_DIG
/ TrioLogarithmBase(base
));
2645 epsilon
= LDBL_EPSILON
;
2647 else if (flags
& FLAGS_SHORT
)
2649 baseDigits
= (base
== BASE_DECIMAL
)
2651 : (int)floor(FLT_MANT_DIG
/ TrioLogarithmBase(base
));
2652 epsilon
= FLT_EPSILON
;
2656 baseDigits
= (base
== BASE_DECIMAL
)
2658 : (int)floor(DBL_MANT_DIG
/ TrioLogarithmBase(base
));
2659 epsilon
= DBL_EPSILON
;
2662 digits
= (flags
& FLAGS_UPPER
) ? internalDigitsUpper
: internalDigitsLower
;
2663 isHex
= (base
== BASE_HEX
);
2664 if (base
== NO_BASE
)
2665 base
= BASE_DECIMAL
;
2666 dblBase
= (trio_long_double_t
)base
;
2667 keepTrailingZeroes
= !( (flags
& FLAGS_ROUNDING
) ||
2668 ( (flags
& FLAGS_FLOAT_G
) &&
2669 !(flags
& FLAGS_ALTERNATIVE
) ) );
2671 if (flags
& FLAGS_ROUNDING
)
2672 precision
= baseDigits
;
2674 if (precision
== NO_PRECISION
)
2675 precision
= FLT_DIG
;
2681 flags
|= FLAGS_FLOAT_E
;
2683 if (flags
& FLAGS_FLOAT_G
)
2688 if ((number
< 1.0E-4) || (number
> powl(base
,
2689 (trio_long_double_t
)precision
)))
2691 /* Use scientific notation */
2692 flags
|= FLAGS_FLOAT_E
;
2694 else if (number
< 1.0)
2697 * Use normal notation. If the integer part of the number is
2698 * zero, then adjust the precision to include leading fractional
2701 workNumber
= TrioLogarithm(number
, base
);
2702 workNumber
= TRIO_FABS(workNumber
);
2703 if (workNumber
- floorl(workNumber
) < 0.001)
2705 zeroes
= (int)floorl(workNumber
);
2709 if (flags
& FLAGS_FLOAT_E
)
2711 /* Scale the number */
2712 workNumber
= TrioLogarithm(number
, base
);
2713 if (trio_isinf(workNumber
) == -1)
2717 if (flags
& FLAGS_FLOAT_G
)
2718 flags
&= ~FLAGS_FLOAT_E
;
2722 exponent
= (int)floorl(workNumber
);
2723 number
/= powl(dblBase
, (trio_long_double_t
)exponent
);
2724 isExponentNegative
= (exponent
< 0);
2725 uExponent
= (isExponentNegative
) ? -exponent
: exponent
;
2726 /* No thousand separators */
2727 flags
&= ~FLAGS_QUOTE
;
2731 integerNumber
= floorl(number
);
2732 fractionNumber
= number
- integerNumber
;
2737 * Precision is number of significant digits for FLOAT_G
2738 * and number of fractional digits for others.
2740 integerDigits
= (integerNumber
> epsilon
)
2741 ? 1 + (int)TrioLogarithm(integerNumber
, base
)
2743 fractionDigits
= ((flags
& FLAGS_FLOAT_G
) && (zeroes
== 0))
2744 ? precision
- integerDigits
2745 : zeroes
+ precision
;
2747 dblFractionBase
= TrioPower(base
, fractionDigits
);
2749 workNumber
= number
+ 0.5 / dblFractionBase
;
2750 if (floorl(number
) != floorl(workNumber
))
2752 if (flags
& FLAGS_FLOAT_E
)
2754 /* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
2756 isExponentNegative
= (exponent
< 0);
2757 uExponent
= (isExponentNegative
) ? -exponent
: exponent
;
2758 workNumber
= (number
+ 0.5 / dblFractionBase
) / dblBase
;
2759 integerNumber
= floorl(workNumber
);
2760 fractionNumber
= workNumber
- integerNumber
;
2764 /* Adjust if number was rounded up one digit (ie. 99 to 100) */
2765 integerNumber
= floorl(number
+ 0.5);
2766 fractionNumber
= 0.0;
2767 integerDigits
= (integerNumber
> epsilon
)
2768 ? 1 + (int)TrioLogarithm(integerNumber
, base
)
2773 /* Estimate accuracy */
2774 integerAdjust
= fractionAdjust
= 0.5;
2775 if (flags
& FLAGS_ROUNDING
)
2777 if (integerDigits
> baseDigits
)
2779 integerThreshold
= baseDigits
;
2781 dblFractionBase
= 1.0;
2782 fractionThreshold
= 0;
2783 precision
= 0; /* Disable decimal-point */
2784 integerAdjust
= TrioPower(base
, integerDigits
- integerThreshold
- 1);
2785 fractionAdjust
= 0.0;
2789 integerThreshold
= integerDigits
;
2790 fractionThreshold
= fractionDigits
- integerThreshold
;
2791 fractionAdjust
= 1.0;
2796 integerThreshold
= INT_MAX
;
2797 fractionThreshold
= INT_MAX
;
2801 * Calculate expected width.
2802 * sign + integer part + thousands separators + decimal point
2803 * + fraction + exponent
2805 fractionAdjust
/= dblFractionBase
;
2806 hasOnlyZeroes
= (floorl((fractionNumber
+ fractionAdjust
) * dblFractionBase
) < epsilon
);
2807 keepDecimalPoint
= ( (flags
& FLAGS_ALTERNATIVE
) ||
2808 !((precision
== 0) ||
2809 (!keepTrailingZeroes
&& hasOnlyZeroes
)) );
2810 if (flags
& FLAGS_FLOAT_E
)
2812 exponentDigits
= (uExponent
== 0)
2814 : (int)ceil(TrioLogarithm((double)(uExponent
+ 1), base
));
2818 requireTwoDigitExponent
= ((base
== BASE_DECIMAL
) && (exponentDigits
== 1));
2820 expectedWidth
= integerDigits
+ fractionDigits
2822 ? internalDecimalPointLength
2824 + ((flags
& FLAGS_QUOTE
)
2825 ? TrioCalcThousandSeparatorLength(integerDigits
)
2827 if (isNegative
|| (flags
& FLAGS_SHOWSIGN
) || (flags
& FLAGS_SPACE
))
2828 expectedWidth
+= sizeof("-") - 1;
2829 if (exponentDigits
> 0)
2830 expectedWidth
+= exponentDigits
+
2831 ((requireTwoDigitExponent
? sizeof("E+0") : sizeof("E+")) - 1);
2833 expectedWidth
+= sizeof("0X") - 1;
2835 /* Output prefixing */
2836 if (flags
& FLAGS_NILPADDING
)
2838 /* Leading zeros must be after sign */
2840 self
->OutStream(self
, '-');
2841 else if (flags
& FLAGS_SHOWSIGN
)
2842 self
->OutStream(self
, '+');
2843 else if (flags
& FLAGS_SPACE
)
2844 self
->OutStream(self
, ' ');
2847 self
->OutStream(self
, '0');
2848 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2850 if (!(flags
& FLAGS_LEFTADJUST
))
2852 for (i
= expectedWidth
; i
< width
; i
++)
2854 self
->OutStream(self
, '0');
2860 /* Leading spaces must be before sign */
2861 if (!(flags
& FLAGS_LEFTADJUST
))
2863 for (i
= expectedWidth
; i
< width
; i
++)
2865 self
->OutStream(self
, CHAR_ADJUST
);
2869 self
->OutStream(self
, '-');
2870 else if (flags
& FLAGS_SHOWSIGN
)
2871 self
->OutStream(self
, '+');
2872 else if (flags
& FLAGS_SPACE
)
2873 self
->OutStream(self
, ' ');
2876 self
->OutStream(self
, '0');
2877 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2881 /* Output the integer part and thousand separators */
2882 dblIntegerBase
= 1.0 / TrioPower(base
, integerDigits
- 1);
2883 for (i
= 0; i
< integerDigits
; i
++)
2885 workNumber
= floorl(((integerNumber
+ integerAdjust
) * dblIntegerBase
));
2886 if (i
> integerThreshold
)
2888 /* Beyond accuracy */
2889 self
->OutStream(self
, digits
[0]);
2893 self
->OutStream(self
, digits
[(int)fmodl(workNumber
, dblBase
)]);
2895 dblIntegerBase
*= dblBase
;
2897 if (((flags
& (FLAGS_FLOAT_E
| FLAGS_QUOTE
)) == FLAGS_QUOTE
)
2898 && TrioFollowedBySeparator(integerDigits
- i
))
2900 for (groupingPointer
= internalThousandSeparator
;
2901 *groupingPointer
!= NIL
;
2904 self
->OutStream(self
, *groupingPointer
);
2909 /* Insert decimal point and build the fraction part */
2912 if (keepDecimalPoint
)
2914 if (internalDecimalPoint
)
2916 self
->OutStream(self
, internalDecimalPoint
);
2920 for (i
= 0; i
< internalDecimalPointLength
; i
++)
2922 self
->OutStream(self
, internalDecimalPointString
[i
]);
2927 for (i
= 0; i
< fractionDigits
; i
++)
2929 if ((integerDigits
> integerThreshold
) || (i
> fractionThreshold
))
2931 /* Beyond accuracy */
2936 fractionNumber
*= dblBase
;
2937 fractionAdjust
*= dblBase
;
2938 workNumber
= floorl(fractionNumber
+ fractionAdjust
);
2939 fractionNumber
-= workNumber
;
2940 index
= (int)fmodl(workNumber
, dblBase
);
2947 while (trailingZeroes
> 0)
2949 /* Not trailing zeroes after all */
2950 self
->OutStream(self
, digits
[0]);
2953 self
->OutStream(self
, digits
[index
]);
2958 if (keepTrailingZeroes
)
2960 while (trailingZeroes
> 0)
2962 self
->OutStream(self
, digits
[0]);
2967 /* Output exponent */
2968 if (exponentDigits
> 0)
2970 self
->OutStream(self
,
2972 ? ((flags
& FLAGS_UPPER
) ? 'P' : 'p')
2973 : ((flags
& FLAGS_UPPER
) ? 'E' : 'e'));
2974 self
->OutStream(self
, (isExponentNegative
) ? '-' : '+');
2976 /* The exponent must contain at least two digits */
2977 if (requireTwoDigitExponent
)
2978 self
->OutStream(self
, '0');
2980 exponentBase
= (int)TrioPower(base
, exponentDigits
- 1);
2981 for (i
= 0; i
< exponentDigits
; i
++)
2983 self
->OutStream(self
, digits
[(uExponent
/ exponentBase
) % base
]);
2984 exponentBase
/= base
;
2987 /* Output trailing spaces */
2988 if (flags
& FLAGS_LEFTADJUST
)
2990 for (i
= expectedWidth
; i
< width
; i
++)
2992 self
->OutStream(self
, CHAR_ADJUST
);
2997 /*************************************************************************
3001 * This is the main engine for formatting output
3005 TRIO_ARGS3((data
, format
, parameters
),
3007 TRIO_CONST
char *format
,
3008 trio_parameter_t
*parameters
)
3010 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3014 TRIO_CONST
char *string
;
3015 trio_pointer_t pointer
;
3016 unsigned long flags
;
3024 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3025 (void)mblen(NULL
, 0);
3028 while (format
[index
])
3030 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3031 if (! isascii(format
[index
]))
3033 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
3035 * Only valid multibyte characters are handled here. Invalid
3036 * multibyte characters (charlen == -1) are handled as normal
3041 while (charlen
-- > 0)
3043 data
->OutStream(data
, format
[index
++]);
3045 continue; /* while characters left in formatting string */
3048 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
3049 if (CHAR_IDENTIFIER
== format
[index
])
3051 if (CHAR_IDENTIFIER
== format
[index
+ 1])
3053 data
->OutStream(data
, CHAR_IDENTIFIER
);
3058 /* Skip the parameter entries */
3059 while (parameters
[i
].type
== FORMAT_PARAMETER
)
3062 flags
= parameters
[i
].flags
;
3065 width
= parameters
[i
].width
;
3066 if (flags
& FLAGS_WIDTH_PARAMETER
)
3068 /* Get width from parameter list */
3069 width
= (int)parameters
[width
].data
.number
.as_signed
;
3072 /* Find precision */
3073 if (flags
& FLAGS_PRECISION
)
3075 precision
= parameters
[i
].precision
;
3076 if (flags
& FLAGS_PRECISION_PARAMETER
)
3078 /* Get precision from parameter list */
3079 precision
= (int)parameters
[precision
].data
.number
.as_signed
;
3084 precision
= NO_PRECISION
;
3088 base
= parameters
[i
].base
;
3089 if (flags
& FLAGS_BASE_PARAMETER
)
3091 /* Get base from parameter list */
3092 base
= (int)parameters
[base
].data
.number
.as_signed
;
3095 switch (parameters
[i
].type
)
3098 if (flags
& FLAGS_QUOTE
)
3099 data
->OutStream(data
, CHAR_QUOTE
);
3100 if (! (flags
& FLAGS_LEFTADJUST
))
3103 data
->OutStream(data
, CHAR_ADJUST
);
3106 if (flags
& FLAGS_WIDECHAR
)
3108 TrioWriteWideStringCharacter(data
,
3109 (trio_wchar_t
)parameters
[i
].data
.number
.as_signed
,
3116 TrioWriteStringCharacter(data
,
3117 (int)parameters
[i
].data
.number
.as_signed
,
3121 if (flags
& FLAGS_LEFTADJUST
)
3124 data
->OutStream(data
, CHAR_ADJUST
);
3126 if (flags
& FLAGS_QUOTE
)
3127 data
->OutStream(data
, CHAR_QUOTE
);
3129 break; /* FORMAT_CHAR */
3132 TrioWriteNumber(data
,
3133 parameters
[i
].data
.number
.as_unsigned
,
3139 break; /* FORMAT_INT */
3142 TrioWriteDouble(data
,
3143 parameters
[i
].data
.longdoubleNumber
,
3148 break; /* FORMAT_DOUBLE */
3152 if (flags
& FLAGS_WIDECHAR
)
3154 TrioWriteWideString(data
,
3155 parameters
[i
].data
.wstring
,
3163 TrioWriteString(data
,
3164 parameters
[i
].data
.string
,
3169 break; /* FORMAT_STRING */
3171 case FORMAT_POINTER
:
3173 trio_reference_t reference
;
3175 reference
.data
= data
;
3176 reference
.parameter
= ¶meters
[i
];
3177 trio_print_pointer(&reference
, parameters
[i
].data
.pointer
);
3179 break; /* FORMAT_POINTER */
3182 pointer
= parameters
[i
].data
.pointer
;
3183 if (NULL
!= pointer
)
3186 * C99 paragraph 7.19.6.1.8 says "the number of
3187 * characters written to the output stream so far by
3188 * this call", which is data->committed
3190 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
3191 if (flags
& FLAGS_SIZE_T
)
3192 *(size_t *)pointer
= (size_t)data
->committed
;
3195 #if defined(QUALIFIER_PTRDIFF_T)
3196 if (flags
& FLAGS_PTRDIFF_T
)
3197 *(ptrdiff_t *)pointer
= (ptrdiff_t)data
->committed
;
3200 #if defined(QUALIFIER_INTMAX_T)
3201 if (flags
& FLAGS_INTMAX_T
)
3202 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)data
->committed
;
3205 if (flags
& FLAGS_QUAD
)
3207 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)data
->committed
;
3209 else if (flags
& FLAGS_LONG
)
3211 *(long int *)pointer
= (long int)data
->committed
;
3213 else if (flags
& FLAGS_SHORT
)
3215 *(short int *)pointer
= (short int)data
->committed
;
3219 *(int *)pointer
= (int)data
->committed
;
3222 break; /* FORMAT_COUNT */
3224 case FORMAT_PARAMETER
:
3225 break; /* FORMAT_PARAMETER */
3227 #if defined(FORMAT_ERRNO)
3229 string
= trio_error(parameters
[i
].data
.errorNumber
);
3232 TrioWriteString(data
,
3240 data
->OutStream(data
, '#');
3241 TrioWriteNumber(data
,
3242 (trio_uintmax_t
)parameters
[i
].data
.errorNumber
,
3248 break; /* FORMAT_ERRNO */
3249 #endif /* defined(FORMAT_ERRNO) */
3251 #if defined(FORMAT_USER_DEFINED)
3252 case FORMAT_USER_DEFINED
:
3254 trio_reference_t reference
;
3255 trio_userdef_t
*def
= NULL
;
3257 if (parameters
[i
].user_name
[0] == NIL
)
3261 (parameters
[i
- 1].type
== FORMAT_PARAMETER
))
3262 def
= (trio_userdef_t
*)parameters
[i
- 1].data
.pointer
;
3266 /* Look up namespace */
3267 def
= TrioFindNamespace(parameters
[i
].user_name
, NULL
);
3270 reference
.data
= data
;
3271 reference
.parameter
= ¶meters
[i
];
3272 def
->callback(&reference
);
3276 #endif /* defined(FORMAT_USER_DEFINED) */
3280 } /* switch parameter type */
3282 /* Prepare for next */
3283 index
= parameters
[i
].indexAfterSpecifier
;
3287 else /* not identifier */
3289 data
->OutStream(data
, format
[index
++]);
3292 return data
->processed
;
3295 /*************************************************************************
3300 TRIO_ARGS4((reference
, format
, arglist
, argarray
),
3301 trio_reference_t
*reference
,
3302 TRIO_CONST
char *format
,
3304 trio_pointer_t
*argarray
)
3307 trio_parameter_t parameters
[MAX_PARAMETERS
];
3309 status
= TrioParse(TYPE_PRINT
, format
, parameters
, arglist
, argarray
);
3313 status
= TrioFormatProcess(reference
->data
, format
, parameters
);
3314 if (reference
->data
->error
!= 0)
3316 status
= reference
->data
->error
;
3321 /*************************************************************************
3326 TRIO_ARGS6((destination
, destinationSize
, OutStream
, format
, arglist
, argarray
),
3327 trio_pointer_t destination
,
3328 size_t destinationSize
,
3329 void (*OutStream
) TRIO_PROTO((trio_class_t
*, int)),
3330 TRIO_CONST
char *format
,
3332 trio_pointer_t
*argarray
)
3336 trio_parameter_t parameters
[MAX_PARAMETERS
];
3338 assert(VALID(OutStream
));
3339 assert(VALID(format
));
3341 memset(&data
, 0, sizeof(data
));
3342 data
.OutStream
= OutStream
;
3343 data
.location
= destination
;
3344 data
.max
= destinationSize
;
3347 #if defined(USE_LOCALE)
3348 if (NULL
== internalLocaleValues
)
3354 status
= TrioParse(TYPE_PRINT
, format
, parameters
, arglist
, argarray
);
3358 status
= TrioFormatProcess(&data
, format
, parameters
);
3359 if (data
.error
!= 0)
3361 status
= data
.error
;
3366 /*************************************************************************
3371 TRIO_ARGS2((self
, output
),
3377 assert(VALID(self
));
3378 assert(VALID(self
->location
));
3380 file
= (FILE *)self
->location
;
3382 if (fputc(output
, file
) == EOF
)
3384 self
->error
= TRIO_ERROR_RETURN(TRIO_EOF
, 0);
3392 /*************************************************************************
3393 * TrioOutStreamFileDescriptor
3396 TrioOutStreamFileDescriptor
3397 TRIO_ARGS2((self
, output
),
3404 assert(VALID(self
));
3406 fd
= *((int *)self
->location
);
3409 if (write(fd
, &ch
, sizeof(char)) == -1)
3411 self
->error
= TRIO_ERROR_RETURN(TRIO_ERRNO
, 0);
3419 /*************************************************************************
3420 * TrioOutStreamCustom
3424 TRIO_ARGS2((self
, output
),
3429 trio_custom_t
*data
;
3431 assert(VALID(self
));
3432 assert(VALID(self
->location
));
3434 data
= (trio_custom_t
*)self
->location
;
3435 if (data
->stream
.out
)
3437 status
= (data
->stream
.out
)(data
->closure
, output
);
3444 if (self
->error
== 0)
3446 self
->error
= TRIO_ERROR_RETURN(TRIO_ECUSTOM
, -status
);
3453 /*************************************************************************
3454 * TrioOutStreamString
3458 TRIO_ARGS2((self
, output
),
3464 assert(VALID(self
));
3465 assert(VALID(self
->location
));
3467 buffer
= (char **)self
->location
;
3468 **buffer
= (char)output
;
3474 /*************************************************************************
3475 * TrioOutStreamStringMax
3478 TrioOutStreamStringMax
3479 TRIO_ARGS2((self
, output
),
3485 assert(VALID(self
));
3486 assert(VALID(self
->location
));
3488 buffer
= (char **)self
->location
;
3490 if (self
->processed
< self
->max
)
3492 **buffer
= (char)output
;
3499 /*************************************************************************
3500 * TrioOutStreamStringDynamic
3503 TrioOutStreamStringDynamic
3504 TRIO_ARGS2((self
, output
),
3508 assert(VALID(self
));
3509 assert(VALID(self
->location
));
3511 if (self
->error
== 0)
3513 trio_xstring_append_char((trio_string_t
*)self
->location
,
3517 /* The processed variable must always be increased */
3521 /*************************************************************************
3523 * Formatted printing functions
3525 ************************************************************************/
3527 #if defined(TRIO_DOCUMENTATION)
3528 # include "doc/doc_printf.h"
3530 /** @addtogroup Printf
3534 /*************************************************************************
3539 Print to standard output stream.
3541 @param format Formatting string.
3542 @param ... Arguments.
3543 @return Number of printed characters.
3547 TRIO_VARGS2((format
, va_alist
),
3548 TRIO_CONST
char *format
,
3554 assert(VALID(format
));
3556 TRIO_VA_START(args
, format
);
3557 status
= TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, &args
, NULL
);
3563 Print to standard output stream.
3565 @param format Formatting string.
3566 @param args Arguments.
3567 @return Number of printed characters.
3571 TRIO_ARGS2((format
, args
),
3572 TRIO_CONST
char *format
,
3575 assert(VALID(format
));
3577 return TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, &args
, NULL
);
3581 Print to standard output stream.
3583 @param format Formatting string.
3584 @param args Arguments.
3585 @return Number of printed characters.
3589 TRIO_ARGS2((format
, args
),
3590 TRIO_CONST
char *format
,
3591 trio_pointer_t
* args
)
3593 assert(VALID(format
));
3595 return TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, NULL
, args
);
3598 /*************************************************************************
3605 @param file File pointer.
3606 @param format Formatting string.
3607 @param ... Arguments.
3608 @return Number of printed characters.
3612 TRIO_VARGS3((file
, format
, va_alist
),
3614 TRIO_CONST
char *format
,
3620 assert(VALID(file
));
3621 assert(VALID(format
));
3623 TRIO_VA_START(args
, format
);
3624 status
= TrioFormat(file
, 0, TrioOutStreamFile
, format
, &args
, NULL
);
3632 @param file File pointer.
3633 @param format Formatting string.
3634 @param args Arguments.
3635 @return Number of printed characters.
3639 TRIO_ARGS3((file
, format
, args
),
3641 TRIO_CONST
char *format
,
3644 assert(VALID(file
));
3645 assert(VALID(format
));
3647 return TrioFormat(file
, 0, TrioOutStreamFile
, format
, &args
, NULL
);
3653 @param file File pointer.
3654 @param format Formatting string.
3655 @param args Arguments.
3656 @return Number of printed characters.
3660 TRIO_ARGS3((file
, format
, args
),
3662 TRIO_CONST
char *format
,
3663 trio_pointer_t
* args
)
3665 assert(VALID(file
));
3666 assert(VALID(format
));
3668 return TrioFormat(file
, 0, TrioOutStreamFile
, format
, NULL
, args
);
3671 /*************************************************************************
3676 Print to file descriptor.
3678 @param fd File descriptor.
3679 @param format Formatting string.
3680 @param ... Arguments.
3681 @return Number of printed characters.
3685 TRIO_VARGS3((fd
, format
, va_alist
),
3687 TRIO_CONST
char *format
,
3693 assert(VALID(format
));
3695 TRIO_VA_START(args
, format
);
3696 status
= TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, &args
, NULL
);
3702 Print to file descriptor.
3704 @param fd File descriptor.
3705 @param format Formatting string.
3706 @param args Arguments.
3707 @return Number of printed characters.
3711 TRIO_ARGS3((fd
, format
, args
),
3713 TRIO_CONST
char *format
,
3716 assert(VALID(format
));
3718 return TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, &args
, NULL
);
3722 Print to file descriptor.
3724 @param fd File descriptor.
3725 @param format Formatting string.
3726 @param args Arguments.
3727 @return Number of printed characters.
3731 TRIO_ARGS3((fd
, format
, args
),
3733 TRIO_CONST
char *format
,
3734 trio_pointer_t
*args
)
3736 assert(VALID(format
));
3738 return TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, NULL
, args
);
3741 /*************************************************************************
3746 TRIO_VARGS4((stream
, closure
, format
, va_alist
),
3747 trio_outstream_t stream
,
3748 trio_pointer_t closure
,
3749 TRIO_CONST
char *format
,
3756 assert(VALID(stream
));
3757 assert(VALID(format
));
3759 TRIO_VA_START(args
, format
);
3760 data
.stream
.out
= stream
;
3761 data
.closure
= closure
;
3762 status
= TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, &args
, NULL
);
3769 TRIO_ARGS4((stream
, closure
, format
, args
),
3770 trio_outstream_t stream
,
3771 trio_pointer_t closure
,
3772 TRIO_CONST
char *format
,
3777 assert(VALID(stream
));
3778 assert(VALID(format
));
3780 data
.stream
.out
= stream
;
3781 data
.closure
= closure
;
3782 return TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, &args
, NULL
);
3787 TRIO_ARGS4((stream
, closure
, format
, args
),
3788 trio_outstream_t stream
,
3789 trio_pointer_t closure
,
3790 TRIO_CONST
char *format
,
3795 assert(VALID(stream
));
3796 assert(VALID(format
));
3798 data
.stream
.out
= stream
;
3799 data
.closure
= closure
;
3800 return TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, NULL
, args
);
3803 /*************************************************************************
3810 @param buffer Output string.
3811 @param format Formatting string.
3812 @param ... Arguments.
3813 @return Number of printed characters.
3817 TRIO_VARGS3((buffer
, format
, va_alist
),
3819 TRIO_CONST
char *format
,
3825 assert(VALID(buffer
));
3826 assert(VALID(format
));
3828 TRIO_VA_START(args
, format
);
3829 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, &args
, NULL
);
3830 *buffer
= NIL
; /* Terminate with NIL character */
3838 @param buffer Output string.
3839 @param format Formatting string.
3840 @param args Arguments.
3841 @return Number of printed characters.
3845 TRIO_ARGS3((buffer
, format
, args
),
3847 TRIO_CONST
char *format
,
3852 assert(VALID(buffer
));
3853 assert(VALID(format
));
3855 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, &args
, NULL
);
3863 @param buffer Output string.
3864 @param format Formatting string.
3865 @param args Arguments.
3866 @return Number of printed characters.
3870 TRIO_ARGS3((buffer
, format
, args
),
3872 TRIO_CONST
char *format
,
3873 trio_pointer_t
*args
)
3877 assert(VALID(buffer
));
3878 assert(VALID(format
));
3880 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, NULL
, args
);
3885 /*************************************************************************
3890 Print at most @p max characters to string.
3892 @param buffer Output string.
3893 @param max Maximum number of characters to print.
3894 @param format Formatting string.
3895 @param ... Arguments.
3896 @return Number of printed characters.
3900 TRIO_VARGS4((buffer
, max
, format
, va_alist
),
3903 TRIO_CONST
char *format
,
3909 assert(VALID(buffer
));
3910 assert(VALID(format
));
3912 TRIO_VA_START(args
, format
);
3913 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
3914 TrioOutStreamStringMax
, format
, &args
, NULL
);
3922 Print at most @p max characters to string.
3924 @param buffer Output string.
3925 @param max Maximum number of characters to print.
3926 @param format Formatting string.
3927 @param args Arguments.
3928 @return Number of printed characters.
3932 TRIO_ARGS4((buffer
, max
, format
, args
),
3935 TRIO_CONST
char *format
,
3940 assert(VALID(buffer
));
3941 assert(VALID(format
));
3943 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
3944 TrioOutStreamStringMax
, format
, &args
, NULL
);
3951 Print at most @p max characters to string.
3953 @param buffer Output string.
3954 @param max Maximum number of characters to print.
3955 @param format Formatting string.
3956 @param args Arguments.
3957 @return Number of printed characters.
3961 TRIO_ARGS4((buffer
, max
, format
, args
),
3964 TRIO_CONST
char *format
,
3965 trio_pointer_t
*args
)
3969 assert(VALID(buffer
));
3970 assert(VALID(format
));
3972 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
3973 TrioOutStreamStringMax
, format
, NULL
, args
);
3979 /*************************************************************************
3981 * Appends the new string to the buffer string overwriting the '\0'
3982 * character at the end of buffer.
3986 TRIO_VARGS4((buffer
, max
, format
, va_alist
),
3989 TRIO_CONST
char *format
,
3996 TRIO_VA_START(args
, format
);
3998 assert(VALID(buffer
));
3999 assert(VALID(format
));
4001 buf_len
= trio_length(buffer
);
4002 buffer
= &buffer
[buf_len
];
4004 status
= TrioFormat(&buffer
, max
- 1 - buf_len
,
4005 TrioOutStreamStringMax
, format
, &args
, NULL
);
4013 TRIO_ARGS4((buffer
, max
, format
, args
),
4016 TRIO_CONST
char *format
,
4022 assert(VALID(buffer
));
4023 assert(VALID(format
));
4025 buf_len
= trio_length(buffer
);
4026 buffer
= &buffer
[buf_len
];
4027 status
= TrioFormat(&buffer
, max
- 1 - buf_len
,
4028 TrioOutStreamStringMax
, format
, &args
, NULL
);
4033 /*************************************************************************
4040 TRIO_VARGS2((format
, va_alist
),
4041 TRIO_CONST
char *format
,
4045 trio_string_t
*info
;
4046 char *result
= NULL
;
4048 assert(VALID(format
));
4050 info
= trio_xstring_duplicate("");
4053 TRIO_VA_START(args
, format
);
4054 (void)TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4055 format
, &args
, NULL
);
4058 trio_string_terminate(info
);
4059 result
= trio_string_extract(info
);
4060 trio_string_destroy(info
);
4068 TRIO_ARGS2((format
, args
),
4069 TRIO_CONST
char *format
,
4072 trio_string_t
*info
;
4073 char *result
= NULL
;
4075 assert(VALID(format
));
4077 info
= trio_xstring_duplicate("");
4080 (void)TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4081 format
, &args
, NULL
);
4082 trio_string_terminate(info
);
4083 result
= trio_string_extract(info
);
4084 trio_string_destroy(info
);
4091 TRIO_VARGS3((result
, format
, va_alist
),
4093 TRIO_CONST
char *format
,
4098 trio_string_t
*info
;
4100 assert(VALID(format
));
4104 info
= trio_xstring_duplicate("");
4107 status
= TRIO_ERROR_RETURN(TRIO_ENOMEM
, 0);
4111 TRIO_VA_START(args
, format
);
4112 status
= TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4113 format
, &args
, NULL
);
4117 trio_string_terminate(info
);
4118 *result
= trio_string_extract(info
);
4120 trio_string_destroy(info
);
4127 TRIO_ARGS3((result
, format
, args
),
4129 TRIO_CONST
char *format
,
4133 trio_string_t
*info
;
4135 assert(VALID(format
));
4139 info
= trio_xstring_duplicate("");
4142 status
= TRIO_ERROR_RETURN(TRIO_ENOMEM
, 0);
4146 status
= TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4147 format
, &args
, NULL
);
4150 trio_string_terminate(info
);
4151 *result
= trio_string_extract(info
);
4153 trio_string_destroy(info
);
4158 /** @} End of Printf documentation module */
4160 /*************************************************************************
4164 ************************************************************************/
4166 #if defined(TRIO_DOCUMENTATION)
4167 # include "doc/doc_register.h"
4170 @addtogroup UserDefined
4176 /*************************************************************************
4181 Register new user-defined specifier.
4187 TRIO_PUBLIC trio_pointer_t
4189 TRIO_ARGS2((callback
, name
),
4190 trio_callback_t callback
,
4191 TRIO_CONST
char *name
)
4193 trio_userdef_t
*def
;
4194 trio_userdef_t
*prev
= NULL
;
4196 if (callback
== NULL
)
4201 /* Handle built-in namespaces */
4204 if (trio_equal(name
, ":enter"))
4206 internalEnterCriticalRegion
= callback
;
4208 else if (trio_equal(name
, ":leave"))
4210 internalLeaveCriticalRegion
= callback
;
4215 /* Bail out if namespace is too long */
4216 if (trio_length(name
) >= MAX_USER_NAME
)
4219 /* Bail out if namespace already is registered */
4220 def
= TrioFindNamespace(name
, &prev
);
4225 def
= (trio_userdef_t
*)TRIO_MALLOC(sizeof(trio_userdef_t
));
4228 if (internalEnterCriticalRegion
)
4229 (void)internalEnterCriticalRegion(NULL
);
4233 /* Link into internal list */
4235 internalUserDef
= def
;
4240 def
->callback
= callback
;
4241 def
->name
= (name
== NULL
)
4243 : trio_duplicate(name
);
4246 if (internalLeaveCriticalRegion
)
4247 (void)internalLeaveCriticalRegion(NULL
);
4249 return (trio_pointer_t
)def
;
4253 Unregister an existing user-defined specifier.
4259 TRIO_ARGS1((handle
),
4260 trio_pointer_t handle
)
4262 trio_userdef_t
*self
= (trio_userdef_t
*)handle
;
4263 trio_userdef_t
*def
;
4264 trio_userdef_t
*prev
= NULL
;
4266 assert(VALID(self
));
4270 def
= TrioFindNamespace(self
->name
, &prev
);
4273 if (internalEnterCriticalRegion
)
4274 (void)internalEnterCriticalRegion(NULL
);
4277 internalUserDef
= NULL
;
4279 prev
->next
= def
->next
;
4281 if (internalLeaveCriticalRegion
)
4282 (void)internalLeaveCriticalRegion(NULL
);
4284 trio_destroy(self
->name
);
4289 /*************************************************************************
4290 * trio_get_format [public]
4297 #if defined(FORMAT_USER_DEFINED)
4298 assert(((trio_reference_t
*)ref
)->parameter
->type
== FORMAT_USER_DEFINED
);
4301 return (((trio_reference_t
*)ref
)->parameter
->user_data
);
4304 /*************************************************************************
4305 * trio_get_argument [public]
4312 #if defined(FORMAT_USER_DEFINED)
4313 assert(((trio_reference_t
*)ref
)->parameter
->type
== FORMAT_USER_DEFINED
);
4316 return ((trio_reference_t
*)ref
)->parameter
->data
.pointer
;
4319 /*************************************************************************
4320 * trio_get_width / trio_set_width [public]
4327 return ((trio_reference_t
*)ref
)->parameter
->width
;
4332 TRIO_ARGS2((ref
, width
),
4336 ((trio_reference_t
*)ref
)->parameter
->width
= width
;
4339 /*************************************************************************
4340 * trio_get_precision / trio_set_precision [public]
4347 return (((trio_reference_t
*)ref
)->parameter
->precision
);
4352 TRIO_ARGS2((ref
, precision
),
4356 ((trio_reference_t
*)ref
)->parameter
->precision
= precision
;
4359 /*************************************************************************
4360 * trio_get_base / trio_set_base [public]
4367 return (((trio_reference_t
*)ref
)->parameter
->base
);
4372 TRIO_ARGS2((ref
, base
),
4376 ((trio_reference_t
*)ref
)->parameter
->base
= base
;
4379 /*************************************************************************
4380 * trio_get_long / trio_set_long [public]
4387 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LONG
);
4392 TRIO_ARGS2((ref
, is_long
),
4397 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LONG
;
4399 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LONG
;
4402 /*************************************************************************
4403 * trio_get_longlong / trio_set_longlong [public]
4410 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_QUAD
);
4415 TRIO_ARGS2((ref
, is_longlong
),
4420 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_QUAD
;
4422 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_QUAD
;
4425 /*************************************************************************
4426 * trio_get_longdouble / trio_set_longdouble [public]
4433 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LONGDOUBLE
);
4438 TRIO_ARGS2((ref
, is_longdouble
),
4443 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LONGDOUBLE
;
4445 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LONGDOUBLE
;
4448 /*************************************************************************
4449 * trio_get_short / trio_set_short [public]
4456 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHORT
);
4461 TRIO_ARGS2((ref
, is_short
),
4466 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHORT
;
4468 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHORT
;
4471 /*************************************************************************
4472 * trio_get_shortshort / trio_set_shortshort [public]
4479 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHORTSHORT
);
4484 TRIO_ARGS2((ref
, is_shortshort
),
4489 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHORTSHORT
;
4491 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHORTSHORT
;
4494 /*************************************************************************
4495 * trio_get_alternative / trio_set_alternative [public]
4498 trio_get_alternative
4502 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_ALTERNATIVE
);
4506 trio_set_alternative
4507 TRIO_ARGS2((ref
, is_alternative
),
4512 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_ALTERNATIVE
;
4514 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_ALTERNATIVE
;
4517 /*************************************************************************
4518 * trio_get_alignment / trio_set_alignment [public]
4525 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LEFTADJUST
);
4530 TRIO_ARGS2((ref
, is_leftaligned
),
4535 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LEFTADJUST
;
4537 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LEFTADJUST
;
4540 /*************************************************************************
4541 * trio_get_spacing /trio_set_spacing [public]
4548 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SPACE
);
4553 TRIO_ARGS2((ref
, is_space
),
4558 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SPACE
;
4560 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SPACE
;
4563 /*************************************************************************
4564 * trio_get_sign / trio_set_sign [public]
4571 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHOWSIGN
);
4576 TRIO_ARGS2((ref
, is_sign
),
4581 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHOWSIGN
;
4583 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHOWSIGN
;
4586 /*************************************************************************
4587 * trio_get_padding / trio_set_padding [public]
4594 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_NILPADDING
);
4599 TRIO_ARGS2((ref
, is_padding
),
4604 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_NILPADDING
;
4606 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_NILPADDING
;
4609 /*************************************************************************
4610 * trio_get_quote / trio_set_quote [public]
4617 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_QUOTE
);
4622 TRIO_ARGS2((ref
, is_quote
),
4627 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_QUOTE
;
4629 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_QUOTE
;
4632 /*************************************************************************
4633 * trio_get_upper / trio_set_upper [public]
4640 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_UPPER
);
4645 TRIO_ARGS2((ref
, is_upper
),
4650 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_UPPER
;
4652 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_UPPER
;
4655 /*************************************************************************
4656 * trio_get_largest / trio_set_largest [public]
4664 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_INTMAX_T
);
4669 TRIO_ARGS2((ref
, is_largest
),
4674 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_INTMAX_T
;
4676 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_INTMAX_T
;
4680 /*************************************************************************
4681 * trio_get_ptrdiff / trio_set_ptrdiff [public]
4688 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_PTRDIFF_T
);
4693 TRIO_ARGS2((ref
, is_ptrdiff
),
4698 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_PTRDIFF_T
;
4700 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_PTRDIFF_T
;
4703 /*************************************************************************
4704 * trio_get_size / trio_set_size [public]
4712 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SIZE_T
);
4717 TRIO_ARGS2((ref
, is_size
),
4722 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SIZE_T
;
4724 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SIZE_T
;
4728 /*************************************************************************
4729 * trio_print_int [public]
4733 TRIO_ARGS2((ref
, number
),
4737 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4739 TrioWriteNumber(self
->data
,
4740 (trio_uintmax_t
)number
,
4741 self
->parameter
->flags
,
4742 self
->parameter
->width
,
4743 self
->parameter
->precision
,
4744 self
->parameter
->base
);
4747 /*************************************************************************
4748 * trio_print_uint [public]
4752 TRIO_ARGS2((ref
, number
),
4754 unsigned int number
)
4756 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4758 TrioWriteNumber(self
->data
,
4759 (trio_uintmax_t
)number
,
4760 self
->parameter
->flags
| FLAGS_UNSIGNED
,
4761 self
->parameter
->width
,
4762 self
->parameter
->precision
,
4763 self
->parameter
->base
);
4766 /*************************************************************************
4767 * trio_print_double [public]
4771 TRIO_ARGS2((ref
, number
),
4775 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4777 TrioWriteDouble(self
->data
,
4779 self
->parameter
->flags
,
4780 self
->parameter
->width
,
4781 self
->parameter
->precision
,
4782 self
->parameter
->base
);
4785 /*************************************************************************
4786 * trio_print_string [public]
4790 TRIO_ARGS2((ref
, string
),
4794 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4796 TrioWriteString(self
->data
,
4798 self
->parameter
->flags
,
4799 self
->parameter
->width
,
4800 self
->parameter
->precision
);
4803 /*************************************************************************
4804 * trio_print_ref [public]
4808 TRIO_VARGS3((ref
, format
, va_alist
),
4810 TRIO_CONST
char *format
,
4816 assert(VALID(format
));
4818 TRIO_VA_START(arglist
, format
);
4819 status
= TrioFormatRef((trio_reference_t
*)ref
, format
, &arglist
, NULL
);
4820 TRIO_VA_END(arglist
);
4824 /*************************************************************************
4825 * trio_vprint_ref [public]
4829 TRIO_ARGS3((ref
, format
, arglist
),
4831 TRIO_CONST
char *format
,
4834 assert(VALID(format
));
4836 return TrioFormatRef((trio_reference_t
*)ref
, format
, &arglist
, NULL
);
4839 /*************************************************************************
4840 * trio_printv_ref [public]
4844 TRIO_ARGS3((ref
, format
, argarray
),
4846 TRIO_CONST
char *format
,
4847 trio_pointer_t
*argarray
)
4849 assert(VALID(format
));
4851 return TrioFormatRef((trio_reference_t
*)ref
, format
, NULL
, argarray
);
4854 #endif /* TRIO_EXTENSION */
4856 /*************************************************************************
4857 * trio_print_pointer [public]
4861 TRIO_ARGS2((ref
, pointer
),
4863 trio_pointer_t pointer
)
4865 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4866 unsigned long flags
;
4867 trio_uintmax_t number
;
4869 if (NULL
== pointer
)
4871 TRIO_CONST
char *string
= internalNullString
;
4873 self
->data
->OutStream(self
->data
, *string
++);
4878 * The subtraction of the null pointer is a workaround
4879 * to avoid a compiler warning. The performance overhead
4880 * is negligible (and likely to be removed by an
4881 * optimizing compiler). The (char *) casting is done
4882 * to please ANSI C++.
4884 number
= (trio_uintmax_t
)((char *)pointer
- (char *)0);
4885 /* Shrink to size of pointer */
4886 number
&= (trio_uintmax_t
)-1;
4887 flags
= self
->parameter
->flags
;
4888 flags
|= (FLAGS_UNSIGNED
| FLAGS_ALTERNATIVE
|
4890 TrioWriteNumber(self
->data
,
4899 /** @} End of UserDefined documentation module */
4901 /*************************************************************************
4905 ************************************************************************/
4907 /*************************************************************************
4908 * trio_locale_set_decimal_point
4910 * Decimal point can only be one character. The input argument is a
4911 * string to enable multibyte characters. At most MB_LEN_MAX characters
4915 trio_locale_set_decimal_point
4916 TRIO_ARGS1((decimalPoint
),
4919 #if defined(USE_LOCALE)
4920 if (NULL
== internalLocaleValues
)
4925 internalDecimalPointLength
= trio_length(decimalPoint
);
4926 if (internalDecimalPointLength
== 1)
4928 internalDecimalPoint
= *decimalPoint
;
4932 internalDecimalPoint
= NIL
;
4933 trio_copy_max(internalDecimalPointString
,
4934 sizeof(internalDecimalPointString
),
4939 /*************************************************************************
4940 * trio_locale_set_thousand_separator
4942 * See trio_locale_set_decimal_point
4945 trio_locale_set_thousand_separator
4946 TRIO_ARGS1((thousandSeparator
),
4947 char *thousandSeparator
)
4949 #if defined(USE_LOCALE)
4950 if (NULL
== internalLocaleValues
)
4955 trio_copy_max(internalThousandSeparator
,
4956 sizeof(internalThousandSeparator
),
4958 internalThousandSeparatorLength
= trio_length(internalThousandSeparator
);
4961 /*************************************************************************
4962 * trio_locale_set_grouping
4964 * Array of bytes. Reversed order.
4966 * CHAR_MAX : No further grouping
4967 * 0 : Repeat last group for the remaining digits (not necessary
4968 * as C strings are zero-terminated)
4969 * n : Set current group to n
4971 * Same order as the grouping attribute in LC_NUMERIC.
4974 trio_locale_set_grouping
4975 TRIO_ARGS1((grouping
),
4978 #if defined(USE_LOCALE)
4979 if (NULL
== internalLocaleValues
)
4984 trio_copy_max(internalGrouping
,
4985 sizeof(internalGrouping
),
4990 /*************************************************************************
4994 ************************************************************************/
4996 /*************************************************************************
4997 * TrioSkipWhitespaces
5009 self
->InStream(self
, &ch
);
5014 /*************************************************************************
5019 TrioGetCollation(TRIO_NOARGS
)
5027 /* This is computational expensive */
5030 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5034 for (j
= 0; j
< MAX_CHARACTER_CLASS
; j
++)
5036 second
[0] = (char)j
;
5037 if (trio_equal_locale(first
, second
))
5038 internalCollationArray
[i
][k
++] = (char)j
;
5040 internalCollationArray
[i
][k
] = NIL
;
5045 /*************************************************************************
5046 * TrioGetCharacterClass
5052 TrioGetCharacterClass
5053 TRIO_ARGS4((format
, indexPointer
, flagsPointer
, characterclass
),
5054 TRIO_CONST
char *format
,
5056 unsigned long *flagsPointer
,
5057 int *characterclass
)
5059 int index
= *indexPointer
;
5065 *flagsPointer
&= ~FLAGS_EXCLUDE
;
5067 if (format
[index
] == QUALIFIER_CIRCUMFLEX
)
5069 *flagsPointer
|= FLAGS_EXCLUDE
;
5073 * If the ungroup character is at the beginning of the scanlist,
5074 * it will be part of the class, and a second ungroup character
5075 * must follow to end the group.
5077 if (format
[index
] == SPECIFIER_UNGROUP
)
5079 characterclass
[(int)SPECIFIER_UNGROUP
]++;
5083 * Minus is used to specify ranges. To include minus in the class,
5084 * it must be at the beginning of the list
5086 if (format
[index
] == QUALIFIER_MINUS
)
5088 characterclass
[(int)QUALIFIER_MINUS
]++;
5091 /* Collect characters */
5092 for (ch
= format
[index
];
5093 (ch
!= SPECIFIER_UNGROUP
) && (ch
!= NIL
);
5094 ch
= format
[++index
])
5098 case QUALIFIER_MINUS
: /* Scanlist ranges */
5101 * Both C99 and UNIX98 describes ranges as implementation-
5104 * We support the following behaviour (although this may
5105 * change as we become wiser)
5106 * - only increasing ranges, ie. [a-b] but not [b-a]
5107 * - transitive ranges, ie. [a-b-c] == [a-c]
5108 * - trailing minus, ie. [a-] is interpreted as an 'a'
5110 * - duplicates (although we can easily convert these
5113 range_begin
= format
[index
- 1];
5114 range_end
= format
[++index
];
5115 if (range_end
== SPECIFIER_UNGROUP
)
5117 /* Trailing minus is included */
5118 characterclass
[(int)ch
]++;
5122 if (range_end
== NIL
)
5123 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
5124 if (range_begin
> range_end
)
5125 return TRIO_ERROR_RETURN(TRIO_ERANGE
, index
);
5127 for (i
= (int)range_begin
; i
<= (int)range_end
; i
++)
5128 characterclass
[i
]++;
5135 case SPECIFIER_GROUP
:
5137 switch (format
[index
+ 1])
5139 case QUALIFIER_DOT
: /* Collating symbol */
5141 * FIXME: This will be easier to implement when multibyte
5142 * characters have been implemented. Until now, we ignore
5145 for (i
= index
+ 2; ; i
++)
5147 if (format
[i
] == NIL
)
5148 /* Error in syntax */
5150 else if (format
[i
] == QUALIFIER_DOT
)
5153 if (format
[++i
] != SPECIFIER_UNGROUP
)
5159 case QUALIFIER_EQUAL
: /* Equivalence class expressions */
5164 if (internalCollationUnconverted
)
5166 /* Lazy evaluation of collation array */
5168 internalCollationUnconverted
= FALSE
;
5170 for (i
= index
+ 2; ; i
++)
5172 if (format
[i
] == NIL
)
5173 /* Error in syntax */
5175 else if (format
[i
] == QUALIFIER_EQUAL
)
5179 /* Mark any equivalent character */
5180 k
= (unsigned int)format
[i
];
5181 for (j
= 0; internalCollationArray
[k
][j
] != NIL
; j
++)
5182 characterclass
[(int)internalCollationArray
[k
][j
]]++;
5185 if (format
[++i
] != SPECIFIER_UNGROUP
)
5192 case QUALIFIER_COLON
: /* Character class expressions */
5194 if (trio_equal_max(CLASS_ALNUM
, sizeof(CLASS_ALNUM
) - 1,
5197 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5199 characterclass
[i
]++;
5200 index
+= sizeof(CLASS_ALNUM
) - 1;
5202 else if (trio_equal_max(CLASS_ALPHA
, sizeof(CLASS_ALPHA
) - 1,
5205 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5207 characterclass
[i
]++;
5208 index
+= sizeof(CLASS_ALPHA
) - 1;
5210 else if (trio_equal_max(CLASS_CNTRL
, sizeof(CLASS_CNTRL
) - 1,
5213 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5215 characterclass
[i
]++;
5216 index
+= sizeof(CLASS_CNTRL
) - 1;
5218 else if (trio_equal_max(CLASS_DIGIT
, sizeof(CLASS_DIGIT
) - 1,
5221 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5223 characterclass
[i
]++;
5224 index
+= sizeof(CLASS_DIGIT
) - 1;
5226 else if (trio_equal_max(CLASS_GRAPH
, sizeof(CLASS_GRAPH
) - 1,
5229 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5231 characterclass
[i
]++;
5232 index
+= sizeof(CLASS_GRAPH
) - 1;
5234 else if (trio_equal_max(CLASS_LOWER
, sizeof(CLASS_LOWER
) - 1,
5237 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5239 characterclass
[i
]++;
5240 index
+= sizeof(CLASS_LOWER
) - 1;
5242 else if (trio_equal_max(CLASS_PRINT
, sizeof(CLASS_PRINT
) - 1,
5245 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5247 characterclass
[i
]++;
5248 index
+= sizeof(CLASS_PRINT
) - 1;
5250 else if (trio_equal_max(CLASS_PUNCT
, sizeof(CLASS_PUNCT
) - 1,
5253 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5255 characterclass
[i
]++;
5256 index
+= sizeof(CLASS_PUNCT
) - 1;
5258 else if (trio_equal_max(CLASS_SPACE
, sizeof(CLASS_SPACE
) - 1,
5261 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5263 characterclass
[i
]++;
5264 index
+= sizeof(CLASS_SPACE
) - 1;
5266 else if (trio_equal_max(CLASS_UPPER
, sizeof(CLASS_UPPER
) - 1,
5269 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5271 characterclass
[i
]++;
5272 index
+= sizeof(CLASS_UPPER
) - 1;
5274 else if (trio_equal_max(CLASS_XDIGIT
, sizeof(CLASS_XDIGIT
) - 1,
5277 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5279 characterclass
[i
]++;
5280 index
+= sizeof(CLASS_XDIGIT
) - 1;
5284 characterclass
[(int)ch
]++;
5289 characterclass
[(int)ch
]++;
5294 #endif /* TRIO_EXTENSION */
5297 characterclass
[(int)ch
]++;
5304 /*************************************************************************
5307 * We implement our own number conversion in preference of strtol and
5308 * strtoul, because we must handle 'long long' and thousand separators.
5310 TRIO_PRIVATE BOOLEAN_T
5312 TRIO_ARGS5((self
, target
, flags
, width
, base
),
5314 trio_uintmax_t
*target
,
5315 unsigned long flags
,
5319 trio_uintmax_t number
= 0;
5322 BOOLEAN_T isNegative
= FALSE
;
5323 BOOLEAN_T gotNumber
= FALSE
;
5326 assert(VALID(self
));
5327 assert(VALID(self
->InStream
));
5328 assert((base
>= MIN_BASE
&& base
<= MAX_BASE
) || (base
== NO_BASE
));
5330 if (internalDigitsUnconverted
)
5332 /* Lazy evaluation of digits array */
5333 memset(internalDigitArray
, -1, sizeof(internalDigitArray
));
5334 for (j
= 0; j
< (int)sizeof(internalDigitsLower
) - 1; j
++)
5336 internalDigitArray
[(int)internalDigitsLower
[j
]] = j
;
5337 internalDigitArray
[(int)internalDigitsUpper
[j
]] = j
;
5339 internalDigitsUnconverted
= FALSE
;
5342 TrioSkipWhitespaces(self
);
5344 if (!(flags
& FLAGS_UNSIGNED
))
5347 if (self
->current
== '+')
5349 self
->InStream(self
, NULL
);
5351 else if (self
->current
== '-')
5353 self
->InStream(self
, NULL
);
5358 count
= self
->processed
;
5360 if (flags
& FLAGS_ALTERNATIVE
)
5368 if (self
->current
== '0')
5370 self
->InStream(self
, NULL
);
5373 if ((base
== BASE_HEX
) &&
5374 (toupper(self
->current
) == 'X'))
5376 self
->InStream(self
, NULL
);
5378 else if ((base
== BASE_BINARY
) &&
5379 (toupper(self
->current
) == 'B'))
5381 self
->InStream(self
, NULL
);
5393 while (((width
== NO_WIDTH
) || (self
->processed
- count
< width
)) &&
5394 (! ((self
->current
== EOF
) || isspace(self
->current
))))
5396 if (isascii(self
->current
))
5398 digit
= internalDigitArray
[self
->current
];
5399 /* Abort if digit is not allowed in the specified base */
5400 if ((digit
== -1) || (digit
>= base
))
5403 else if (flags
& FLAGS_QUOTE
)
5405 /* Compare with thousands separator */
5406 for (j
= 0; internalThousandSeparator
[j
] && self
->current
; j
++)
5408 if (internalThousandSeparator
[j
] != self
->current
)
5411 self
->InStream(self
, NULL
);
5413 if (internalThousandSeparator
[j
])
5414 break; /* Mismatch */
5416 continue; /* Match */
5423 gotNumber
= TRUE
; /* we need at least one digit */
5425 self
->InStream(self
, NULL
);
5428 /* Was anything read at all? */
5433 *target
= (isNegative
) ? -((trio_intmax_t
)number
) : number
;
5437 /*************************************************************************
5442 TRIO_ARGS4((self
, target
, flags
, width
),
5445 unsigned long flags
,
5450 trio_uintmax_t number
;
5452 assert(VALID(self
));
5453 assert(VALID(self
->InStream
));
5456 (self
->current
!= EOF
) && (i
< width
);
5459 ch
= (char)self
->current
;
5460 self
->InStream(self
, NULL
);
5461 if ((flags
& FLAGS_ALTERNATIVE
) && (ch
== CHAR_BACKSLASH
))
5463 switch (self
->current
)
5465 case '\\': ch
= '\\'; break;
5466 case 'a': ch
= '\007'; break;
5467 case 'b': ch
= '\b'; break;
5468 case 'f': ch
= '\f'; break;
5469 case 'n': ch
= '\n'; break;
5470 case 'r': ch
= '\r'; break;
5471 case 't': ch
= '\t'; break;
5472 case 'v': ch
= '\v'; break;
5474 if (isdigit(self
->current
))
5476 /* Read octal number */
5477 if (!TrioReadNumber(self
, &number
, 0, 3, BASE_OCTAL
))
5481 else if (toupper(self
->current
) == 'X')
5483 /* Read hexadecimal number */
5484 self
->InStream(self
, NULL
);
5485 if (!TrioReadNumber(self
, &number
, 0, 2, BASE_HEX
))
5491 ch
= (char)self
->current
;
5503 /*************************************************************************
5506 TRIO_PRIVATE BOOLEAN_T
5508 TRIO_ARGS4((self
, target
, flags
, width
),
5511 unsigned long flags
,
5516 assert(VALID(self
));
5517 assert(VALID(self
->InStream
));
5519 TrioSkipWhitespaces(self
);
5522 * Continue until end of string is reached, a whitespace is encountered,
5523 * or width is exceeded
5526 ((width
== NO_WIDTH
) || (i
< width
)) &&
5527 (! ((self
->current
== EOF
) || isspace(self
->current
)));
5530 if (TrioReadChar(self
, (target
? &target
[i
] : 0), flags
, 1) == 0)
5538 /*************************************************************************
5544 TRIO_ARGS4((self
, target
, flags
, width
),
5546 trio_wchar_t
*target
,
5547 unsigned long flags
,
5555 char buffer
[MB_LEN_MAX
+ 1];
5557 assert(VALID(self
));
5558 assert(VALID(self
->InStream
));
5561 (self
->current
!= EOF
) && (i
< width
);
5564 if (isascii(self
->current
))
5566 if (TrioReadChar(self
, buffer
, flags
, 1) == 0)
5573 * Collect a multibyte character, by enlarging buffer until
5574 * it contains a fully legal multibyte character, or the
5580 buffer
[j
++] = (char)self
->current
;
5582 self
->InStream(self
, NULL
);
5584 while ((j
< (int)sizeof(buffer
)) && (mblen(buffer
, (size_t)j
) != j
));
5588 size
= mbtowc(&wch
, buffer
, sizeof(buffer
));
5593 self
->InStream(self
, NULL
);
5597 #endif /* TRIO_WIDECHAR */
5599 /*************************************************************************
5600 * TrioReadWideString
5603 TRIO_PRIVATE BOOLEAN_T
5605 TRIO_ARGS4((self
, target
, flags
, width
),
5607 trio_wchar_t
*target
,
5608 unsigned long flags
,
5614 assert(VALID(self
));
5615 assert(VALID(self
->InStream
));
5617 TrioSkipWhitespaces(self
);
5619 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5620 (void)mblen(NULL
, 0);
5624 * Continue until end of string is reached, a whitespace is encountered,
5625 * or width is exceeded
5628 ((width
== NO_WIDTH
) || (i
< width
)) &&
5629 (! ((self
->current
== EOF
) || isspace(self
->current
)));
5632 size
= TrioReadWideChar(self
, &target
[i
], flags
, 1);
5639 target
[i
] = WCONST('\0');
5642 #endif /* TRIO_WIDECHAR */
5644 /*************************************************************************
5647 * FIXME: characterclass does not work with multibyte characters
5649 TRIO_PRIVATE BOOLEAN_T
5651 TRIO_ARGS5((self
, target
, characterclass
, flags
, width
),
5654 int *characterclass
,
5655 unsigned long flags
,
5661 assert(VALID(self
));
5662 assert(VALID(self
->InStream
));
5666 ((width
== NO_WIDTH
) || (i
< width
)) &&
5668 (((flags
& FLAGS_EXCLUDE
) != 0) ^ (characterclass
[ch
] == 0))));
5672 target
[i
] = (char)ch
;
5673 self
->InStream(self
, &ch
);
5681 /*************************************************************************
5688 TRIO_PRIVATE BOOLEAN_T
5690 TRIO_ARGS4((self
, target
, flags
, width
),
5692 trio_pointer_t target
,
5693 unsigned long flags
,
5697 char doubleString
[512];
5701 BOOLEAN_T isHex
= FALSE
;
5703 doubleString
[0] = 0;
5705 if ((width
== NO_WIDTH
) || (width
> (int)sizeof(doubleString
) - 1))
5706 width
= sizeof(doubleString
) - 1;
5708 TrioSkipWhitespaces(self
);
5711 * Read entire double number from stream. trio_to_double requires
5712 * a string as input, but InStream can be anything, so we have to
5713 * collect all characters.
5716 if ((ch
== '+') || (ch
== '-'))
5718 doubleString
[index
++] = (char)ch
;
5719 self
->InStream(self
, &ch
);
5735 while (isalpha(ch
) && (index
- start
< width
))
5737 doubleString
[index
++] = (char)ch
;
5738 self
->InStream(self
, &ch
);
5740 doubleString
[index
] = NIL
;
5742 /* Case insensitive string comparison */
5743 if (trio_equal(&doubleString
[start
], INFINITE_UPPER
) ||
5744 trio_equal(&doubleString
[start
], LONG_INFINITE_UPPER
))
5746 if (flags
& FLAGS_LONGDOUBLE
)
5748 if ((start
== 1) && (doubleString
[0] == '-'))
5750 *((trio_long_double_t
*)target
) = trio_ninf();
5754 *((trio_long_double_t
*)target
) = trio_pinf();
5759 if ((start
== 1) && (doubleString
[0] == '-'))
5761 *((double *)target
) = trio_ninf();
5765 *((double *)target
) = trio_pinf();
5770 if (trio_equal(doubleString
, NAN_UPPER
))
5772 /* NaN must not have a preceeding + nor - */
5773 if (flags
& FLAGS_LONGDOUBLE
)
5775 *((trio_long_double_t
*)target
) = trio_nan();
5779 *((double *)target
) = trio_nan();
5786 doubleString
[index
++] = (char)ch
;
5787 self
->InStream(self
, &ch
);
5788 if (toupper(ch
) == 'X')
5791 doubleString
[index
++] = (char)ch
;
5792 self
->InStream(self
, &ch
);
5800 while ((ch
!= EOF
) && (index
- start
< width
))
5803 if (isHex
? isxdigit(ch
) : isdigit(ch
))
5805 doubleString
[index
++] = (char)ch
;
5806 self
->InStream(self
, &ch
);
5808 else if (flags
& FLAGS_QUOTE
)
5810 /* Compare with thousands separator */
5811 for (j
= 0; internalThousandSeparator
[j
] && self
->current
; j
++)
5813 if (internalThousandSeparator
[j
] != self
->current
)
5816 self
->InStream(self
, &ch
);
5818 if (internalThousandSeparator
[j
])
5819 break; /* Mismatch */
5821 continue; /* Match */
5829 doubleString
[index
++] = (char)ch
;
5830 self
->InStream(self
, &ch
);
5831 while ((isHex
? isxdigit(ch
) : isdigit(ch
)) &&
5832 (index
- start
< width
))
5834 doubleString
[index
++] = (char)ch
;
5835 self
->InStream(self
, &ch
);
5837 if (isHex
? (toupper(ch
) == 'P') : (toupper(ch
) == 'E'))
5840 doubleString
[index
++] = (char)ch
;
5841 self
->InStream(self
, &ch
);
5842 if ((ch
== '+') || (ch
== '-'))
5844 doubleString
[index
++] = (char)ch
;
5845 self
->InStream(self
, &ch
);
5847 while ((isHex
? isxdigit(ch
) : isdigit(ch
)) &&
5848 (index
- start
< width
))
5850 doubleString
[index
++] = (char)ch
;
5851 self
->InStream(self
, &ch
);
5856 if ((index
== start
) || (*doubleString
== NIL
))
5859 doubleString
[index
] = 0;
5861 if (flags
& FLAGS_LONGDOUBLE
)
5863 *((trio_long_double_t
*)target
) = trio_to_long_double(doubleString
, NULL
);
5867 *((double *)target
) = trio_to_double(doubleString
, NULL
);
5872 /*************************************************************************
5875 TRIO_PRIVATE BOOLEAN_T
5877 TRIO_ARGS3((self
, target
, flags
),
5879 trio_pointer_t
*target
,
5880 unsigned long flags
)
5882 trio_uintmax_t number
;
5883 char buffer
[sizeof(internalNullString
)];
5885 flags
|= (FLAGS_UNSIGNED
| FLAGS_ALTERNATIVE
| FLAGS_NILPADDING
);
5887 if (TrioReadNumber(self
,
5894 * The strange assignment of number is a workaround for a compiler
5898 *target
= (char *)0 + number
;
5901 else if (TrioReadString(self
,
5902 (flags
& FLAGS_IGNORE
)
5906 sizeof(internalNullString
) - 1))
5908 if (trio_equal_case(buffer
, internalNullString
))
5918 /*************************************************************************
5923 TRIO_ARGS3((data
, format
, parameters
),
5925 TRIO_CONST
char *format
,
5926 trio_parameter_t
*parameters
)
5928 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5934 int index
; /* Index of format string */
5935 int i
; /* Index of current parameter */
5936 unsigned long flags
;
5939 trio_pointer_t pointer
;
5944 data
->InStream(data
, &ch
);
5946 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5947 (void)mblen(NULL
, 0);
5950 while (format
[index
])
5952 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5953 if (! isascii(format
[index
]))
5955 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
5958 /* Compare multibyte characters in format string */
5959 for (cnt
= 0; cnt
< charlen
- 1; cnt
++)
5961 if (ch
!= format
[index
+ cnt
])
5963 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
5965 data
->InStream(data
, &ch
);
5967 continue; /* while characters left in formatting string */
5970 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
5972 if ((EOF
== ch
) && (parameters
[i
].type
!= FORMAT_COUNT
))
5974 return (assignment
> 0) ? assignment
: EOF
;
5977 if (CHAR_IDENTIFIER
== format
[index
])
5979 if (CHAR_IDENTIFIER
== format
[index
+ 1])
5981 /* Two % in format matches one % in input stream */
5982 if (CHAR_IDENTIFIER
== ch
)
5984 data
->InStream(data
, &ch
);
5986 continue; /* while format chars left */
5989 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
5992 /* Skip the parameter entries */
5993 while (parameters
[i
].type
== FORMAT_PARAMETER
)
5996 flags
= parameters
[i
].flags
;
5998 width
= parameters
[i
].width
;
5999 if (flags
& FLAGS_WIDTH_PARAMETER
)
6001 /* Get width from parameter list */
6002 width
= (int)parameters
[width
].data
.number
.as_signed
;
6005 base
= parameters
[i
].base
;
6006 if (flags
& FLAGS_BASE_PARAMETER
)
6008 /* Get base from parameter list */
6009 base
= (int)parameters
[base
].data
.number
.as_signed
;
6012 switch (parameters
[i
].type
)
6016 trio_uintmax_t number
;
6019 base
= BASE_DECIMAL
;
6021 if (!TrioReadNumber(data
,
6028 if (!(flags
& FLAGS_IGNORE
))
6032 pointer
= parameters
[i
].data
.pointer
;
6033 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6034 if (flags
& FLAGS_SIZE_T
)
6035 *(size_t *)pointer
= (size_t)number
;
6038 #if defined(QUALIFIER_PTRDIFF_T)
6039 if (flags
& FLAGS_PTRDIFF_T
)
6040 *(ptrdiff_t *)pointer
= (ptrdiff_t)number
;
6043 #if defined(QUALIFIER_INTMAX_T)
6044 if (flags
& FLAGS_INTMAX_T
)
6045 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)number
;
6048 if (flags
& FLAGS_QUAD
)
6049 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)number
;
6050 else if (flags
& FLAGS_LONG
)
6051 *(long int *)pointer
= (long int)number
;
6052 else if (flags
& FLAGS_SHORT
)
6053 *(short int *)pointer
= (short int)number
;
6055 *(int *)pointer
= (int)number
;
6058 break; /* FORMAT_INT */
6062 if (flags
& FLAGS_WIDECHAR
)
6064 if (!TrioReadWideString(data
,
6065 (flags
& FLAGS_IGNORE
)
6067 : parameters
[i
].data
.wstring
,
6075 if (!TrioReadString(data
,
6076 (flags
& FLAGS_IGNORE
)
6078 : parameters
[i
].data
.string
,
6083 if (!(flags
& FLAGS_IGNORE
))
6085 break; /* FORMAT_STRING */
6089 trio_pointer_t pointer
;
6091 if (flags
& FLAGS_IGNORE
)
6097 pointer
= (flags
& FLAGS_LONGDOUBLE
)
6098 ? (trio_pointer_t
)parameters
[i
].data
.longdoublePointer
6099 : (trio_pointer_t
)parameters
[i
].data
.doublePointer
;
6101 if (!TrioReadDouble(data
, pointer
, flags
, width
))
6105 if (!(flags
& FLAGS_IGNORE
))
6109 break; /* FORMAT_DOUBLE */
6113 int characterclass
[MAX_CHARACTER_CLASS
+ 1];
6116 /* Skip over modifiers */
6117 while (format
[index
] != SPECIFIER_GROUP
)
6121 /* Skip over group specifier */
6124 memset(characterclass
, 0, sizeof(characterclass
));
6125 rc
= TrioGetCharacterClass(format
,
6132 if (!TrioReadGroup(data
,
6133 (flags
& FLAGS_IGNORE
)
6135 : parameters
[i
].data
.string
,
6138 parameters
[i
].width
))
6140 if (!(flags
& FLAGS_IGNORE
))
6143 break; /* FORMAT_GROUP */
6146 pointer
= parameters
[i
].data
.pointer
;
6147 if (NULL
!= pointer
)
6149 int count
= data
->committed
;
6151 count
--; /* a character is read, but is not consumed yet */
6152 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6153 if (flags
& FLAGS_SIZE_T
)
6154 *(size_t *)pointer
= (size_t)count
;
6157 #if defined(QUALIFIER_PTRDIFF_T)
6158 if (flags
& FLAGS_PTRDIFF_T
)
6159 *(ptrdiff_t *)pointer
= (ptrdiff_t)count
;
6162 #if defined(QUALIFIER_INTMAX_T)
6163 if (flags
& FLAGS_INTMAX_T
)
6164 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)count
;
6167 if (flags
& FLAGS_QUAD
)
6169 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)count
;
6171 else if (flags
& FLAGS_LONG
)
6173 *(long int *)pointer
= (long int)count
;
6175 else if (flags
& FLAGS_SHORT
)
6177 *(short int *)pointer
= (short int)count
;
6181 *(int *)pointer
= (int)count
;
6184 break; /* FORMAT_COUNT */
6188 if (flags
& FLAGS_WIDECHAR
)
6190 if (TrioReadWideChar(data
,
6191 (flags
& FLAGS_IGNORE
)
6193 : parameters
[i
].data
.wstring
,
6195 (width
== NO_WIDTH
) ? 1 : width
) == 0)
6201 if (TrioReadChar(data
,
6202 (flags
& FLAGS_IGNORE
)
6204 : parameters
[i
].data
.string
,
6206 (width
== NO_WIDTH
) ? 1 : width
) == 0)
6209 if (!(flags
& FLAGS_IGNORE
))
6211 break; /* FORMAT_CHAR */
6213 case FORMAT_POINTER
:
6214 if (!TrioReadPointer(data
,
6215 (flags
& FLAGS_IGNORE
)
6217 : (trio_pointer_t
*)parameters
[i
].data
.pointer
,
6220 if (!(flags
& FLAGS_IGNORE
))
6222 break; /* FORMAT_POINTER */
6224 case FORMAT_PARAMETER
:
6225 break; /* FORMAT_PARAMETER */
6228 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
6231 index
= parameters
[i
].indexAfterSpecifier
;
6234 else /* Not an % identifier */
6236 if (isspace((int)format
[index
]))
6238 /* Whitespaces may match any amount of whitespaces */
6239 ch
= TrioSkipWhitespaces(data
);
6241 else if (ch
== format
[index
])
6243 data
->InStream(data
, &ch
);
6254 /*************************************************************************
6259 TRIO_ARGS6((source
, sourceSize
, InStream
, format
, arglist
, argarray
),
6260 trio_pointer_t source
,
6262 void (*InStream
) TRIO_PROTO((trio_class_t
*, int *)),
6263 TRIO_CONST
char *format
,
6265 trio_pointer_t
*argarray
)
6268 trio_parameter_t parameters
[MAX_PARAMETERS
];
6271 assert(VALID(InStream
));
6272 assert(VALID(format
));
6274 memset(&data
, 0, sizeof(data
));
6275 data
.InStream
= InStream
;
6276 data
.location
= (trio_pointer_t
)source
;
6277 data
.max
= sourceSize
;
6280 #if defined(USE_LOCALE)
6281 if (NULL
== internalLocaleValues
)
6287 status
= TrioParse(TYPE_SCAN
, format
, parameters
, arglist
, argarray
);
6291 status
= TrioScanProcess(&data
, format
, parameters
);
6292 if (data
.error
!= 0)
6294 status
= data
.error
;
6299 /*************************************************************************
6304 TRIO_ARGS2((self
, intPointer
),
6308 FILE *file
= (FILE *)self
->location
;
6310 assert(VALID(self
));
6311 assert(VALID(file
));
6313 self
->current
= fgetc(file
);
6314 if (self
->current
== EOF
)
6316 self
->error
= (ferror(file
))
6317 ? TRIO_ERROR_RETURN(TRIO_ERRNO
, 0)
6318 : TRIO_ERROR_RETURN(TRIO_EOF
, 0);
6326 if (VALID(intPointer
))
6328 *intPointer
= self
->current
;
6332 /*************************************************************************
6333 * TrioInStreamFileDescriptor
6336 TrioInStreamFileDescriptor
6337 TRIO_ARGS2((self
, intPointer
),
6341 int fd
= *((int *)self
->location
);
6343 unsigned char input
;
6345 assert(VALID(self
));
6347 size
= read(fd
, &input
, sizeof(char));
6350 self
->error
= TRIO_ERROR_RETURN(TRIO_ERRNO
, 0);
6351 self
->current
= EOF
;
6355 self
->current
= (size
== 0) ? EOF
: input
;
6357 if (self
->current
!= EOF
)
6363 if (VALID(intPointer
))
6365 *intPointer
= self
->current
;
6369 /*************************************************************************
6370 * TrioInStreamCustom
6374 TRIO_ARGS2((self
, intPointer
),
6378 trio_custom_t
*data
;
6380 assert(VALID(self
));
6381 assert(VALID(self
->location
));
6383 data
= (trio_custom_t
*)self
->location
;
6385 self
->current
= (data
->stream
.in
== NULL
)
6387 : (data
->stream
.in
)(data
->closure
);
6389 if (self
->current
== NIL
)
6391 self
->current
= EOF
;
6399 if (VALID(intPointer
))
6401 *intPointer
= self
->current
;
6405 /*************************************************************************
6406 * TrioInStreamString
6410 TRIO_ARGS2((self
, intPointer
),
6414 unsigned char **buffer
;
6416 assert(VALID(self
));
6417 assert(VALID(self
->location
));
6419 buffer
= (unsigned char **)self
->location
;
6420 self
->current
= (*buffer
)[0];
6421 if (self
->current
== NIL
)
6423 self
->current
= EOF
;
6432 if (VALID(intPointer
))
6434 *intPointer
= self
->current
;
6438 /*************************************************************************
6440 * Formatted scanning functions
6442 ************************************************************************/
6444 #if defined(TRIO_DOCUMENTATION)
6445 # include "doc/doc_scanf.h"
6447 /** @addtogroup Scanf
6451 /*************************************************************************
6456 Scan characters from standard input stream.
6458 @param format Formatting string.
6459 @param ... Arguments.
6460 @return Number of scanned characters.
6464 TRIO_VARGS2((format
, va_alist
),
6465 TRIO_CONST
char *format
,
6471 assert(VALID(format
));
6473 TRIO_VA_START(args
, format
);
6474 status
= TrioScan((trio_pointer_t
)stdin
, 0,
6476 format
, &args
, NULL
);
6483 TRIO_ARGS2((format
, args
),
6484 TRIO_CONST
char *format
,
6487 assert(VALID(format
));
6489 return TrioScan((trio_pointer_t
)stdin
, 0,
6491 format
, &args
, NULL
);
6496 TRIO_ARGS2((format
, args
),
6497 TRIO_CONST
char *format
,
6498 trio_pointer_t
*args
)
6500 assert(VALID(format
));
6502 return TrioScan((trio_pointer_t
)stdin
, 0,
6504 format
, NULL
, args
);
6507 /*************************************************************************
6512 TRIO_VARGS3((file
, format
, va_alist
),
6514 TRIO_CONST
char *format
,
6520 assert(VALID(file
));
6521 assert(VALID(format
));
6523 TRIO_VA_START(args
, format
);
6524 status
= TrioScan((trio_pointer_t
)file
, 0,
6526 format
, &args
, NULL
);
6533 TRIO_ARGS3((file
, format
, args
),
6535 TRIO_CONST
char *format
,
6538 assert(VALID(file
));
6539 assert(VALID(format
));
6541 return TrioScan((trio_pointer_t
)file
, 0,
6543 format
, &args
, NULL
);
6548 TRIO_ARGS3((file
, format
, args
),
6550 TRIO_CONST
char *format
,
6551 trio_pointer_t
*args
)
6553 assert(VALID(file
));
6554 assert(VALID(format
));
6556 return TrioScan((trio_pointer_t
)file
, 0,
6558 format
, NULL
, args
);
6561 /*************************************************************************
6566 TRIO_VARGS3((fd
, format
, va_alist
),
6568 TRIO_CONST
char *format
,
6574 assert(VALID(format
));
6576 TRIO_VA_START(args
, format
);
6577 status
= TrioScan((trio_pointer_t
)&fd
, 0,
6578 TrioInStreamFileDescriptor
,
6579 format
, &args
, NULL
);
6586 TRIO_ARGS3((fd
, format
, args
),
6588 TRIO_CONST
char *format
,
6591 assert(VALID(format
));
6593 return TrioScan((trio_pointer_t
)&fd
, 0,
6594 TrioInStreamFileDescriptor
,
6595 format
, &args
, NULL
);
6600 TRIO_ARGS3((fd
, format
, args
),
6602 TRIO_CONST
char *format
,
6603 trio_pointer_t
*args
)
6605 assert(VALID(format
));
6607 return TrioScan((trio_pointer_t
)&fd
, 0,
6608 TrioInStreamFileDescriptor
,
6609 format
, NULL
, args
);
6612 /*************************************************************************
6617 TRIO_VARGS4((stream
, closure
, format
, va_alist
),
6618 trio_instream_t stream
,
6619 trio_pointer_t closure
,
6620 TRIO_CONST
char *format
,
6627 assert(VALID(stream
));
6628 assert(VALID(format
));
6630 TRIO_VA_START(args
, format
);
6631 data
.stream
.in
= stream
;
6632 data
.closure
= closure
;
6633 status
= TrioScan(&data
, 0, TrioInStreamCustom
, format
, &args
, NULL
);
6640 TRIO_ARGS4((stream
, closure
, format
, args
),
6641 trio_instream_t stream
,
6642 trio_pointer_t closure
,
6643 TRIO_CONST
char *format
,
6648 assert(VALID(stream
));
6649 assert(VALID(format
));
6651 data
.stream
.in
= stream
;
6652 data
.closure
= closure
;
6653 return TrioScan(&data
, 0, TrioInStreamCustom
, format
, &args
, NULL
);
6658 TRIO_ARGS4((stream
, closure
, format
, args
),
6659 trio_instream_t stream
,
6660 trio_pointer_t closure
,
6661 TRIO_CONST
char *format
,
6662 trio_pointer_t
*args
)
6666 assert(VALID(stream
));
6667 assert(VALID(format
));
6669 data
.stream
.in
= stream
;
6670 data
.closure
= closure
;
6671 return TrioScan(&data
, 0, TrioInStreamCustom
, format
, NULL
, args
);
6674 /*************************************************************************
6679 TRIO_VARGS3((buffer
, format
, va_alist
),
6680 TRIO_CONST
char *buffer
,
6681 TRIO_CONST
char *format
,
6687 assert(VALID(buffer
));
6688 assert(VALID(format
));
6690 TRIO_VA_START(args
, format
);
6691 status
= TrioScan((trio_pointer_t
)&buffer
, 0,
6693 format
, &args
, NULL
);
6700 TRIO_ARGS3((buffer
, format
, args
),
6701 TRIO_CONST
char *buffer
,
6702 TRIO_CONST
char *format
,
6705 assert(VALID(buffer
));
6706 assert(VALID(format
));
6708 return TrioScan((trio_pointer_t
)&buffer
, 0,
6710 format
, &args
, NULL
);
6715 TRIO_ARGS3((buffer
, format
, args
),
6716 TRIO_CONST
char *buffer
,
6717 TRIO_CONST
char *format
,
6718 trio_pointer_t
*args
)
6720 assert(VALID(buffer
));
6721 assert(VALID(format
));
6723 return TrioScan((trio_pointer_t
)&buffer
, 0,
6725 format
, NULL
, args
);
6728 /** @} End of Scanf documentation module */
6730 /*************************************************************************
6733 TRIO_PUBLIC TRIO_CONST
char *
6735 TRIO_ARGS1((errorcode
),
6738 /* Textual versions of the error codes */
6739 switch (TRIO_ERROR_CODE(errorcode
))
6742 return "End of file";
6744 return "Invalid argument";
6746 return "Too many arguments";
6748 return "Double reference";
6750 return "Reference gap";
6752 return "Out of memory";
6754 return "Invalid range";
6756 return "Custom error";