1 /*************************************************************************
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. The n-char-sequence can be used
35 * to print the mantissa, e.g. NaN(0xc000000000000000)
36 * - Should we support the GNU %a alloc modifier? GNU has an ugly hack
37 * for %a, because C99 used %a for other purposes. If specified as
38 * %as or %a[ it is interpreted as the alloc modifier, otherwise as
39 * the C99 hex-float. This means that you cannot scan %as as a hex-float
40 * immediately followed by an 's'.
41 * - Scanning of collating symbols.
44 /*************************************************************************
51 #if !defined(TRIO_MINIMAL)
55 /**************************************************************************
59 *************************************************************************/
65 #if (defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) \
66 || defined(USE_MULTIBYTE) || TRIO_WIDECHAR) \
67 && !defined(_WIN32_WCE)
68 # define TRIO_COMPILER_SUPPORTS_MULTIBYTE
69 # if !defined(MB_LEN_MAX)
74 #if (defined(TRIO_COMPILER_MSVC) && (_MSC_VER >= 1100)) || defined(TRIO_COMPILER_BCB)
75 # define TRIO_COMPILER_SUPPORTS_MSVC_INT
78 #if defined(_WIN32_WCE)
79 #include <wincecompat.h>
82 /*************************************************************************
86 #if !(defined(DEBUG) || defined(NDEBUG))
92 #if !defined(TRIO_COMPILER_SUPPORTS_C99)
93 # define isblank(x) (((x)==32) || ((x)==9))
95 #if defined(TRIO_COMPILER_ANCIENT)
102 #if defined( HAVE_ERRNO_H ) || defined( __VMS )
109 #define NIL ((char)0)
111 # define FALSE (1 == 0)
112 # define TRUE (! FALSE)
114 #define BOOLEAN_T int
116 /* mincore() can be used for debugging purposes */
117 #define VALID(x) (NULL != (x))
121 * Encode the error code and the position. This is decoded
122 * with TRIO_ERROR_CODE and TRIO_ERROR_POSITION.
124 # define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
126 # define TRIO_ERROR_RETURN(x,y) (-1)
129 #ifndef VA_LIST_IS_ARRAY
130 #define TRIO_VA_LIST_PTR va_list *
131 #define TRIO_VA_LIST_ADDR(l) (&(l))
132 #define TRIO_VA_LIST_DEREF(l) (*(l))
134 #define TRIO_VA_LIST_PTR va_list
135 #define TRIO_VA_LIST_ADDR(l) (l)
136 #define TRIO_VA_LIST_DEREF(l) (l)
139 typedef unsigned long trio_flags_t
;
142 /*************************************************************************
143 * Platform specific definitions
145 #if defined(TRIO_PLATFORM_UNIX) || defined(TRIO_PLATFORM_OS400)
150 #endif /* TRIO_PLATFORM_UNIX */
151 #if defined(TRIO_PLATFORM_VMS)
154 #if defined(TRIO_PLATFORM_WIN32)
155 # if defined(_WIN32_WCE)
156 # include <wincecompat.h>
160 # define write _write
162 #endif /* TRIO_PLATFORM_WIN32 */
165 # if defined(TRIO_COMPILER_SUPPORTS_ISO94)
168 typedef wchar_t trio_wchar_t
;
169 typedef wint_t trio_wint_t
;
171 typedef char trio_wchar_t
;
172 typedef int trio_wint_t
;
173 # define WCONST(x) L ## x
175 # define iswalnum(x) isalnum(x)
176 # define iswalpha(x) isalpha(x)
177 # define iswblank(x) isblank(x)
178 # define iswcntrl(x) iscntrl(x)
179 # define iswdigit(x) isdigit(x)
180 # define iswgraph(x) isgraph(x)
181 # define iswlower(x) islower(x)
182 # define iswprint(x) isprint(x)
183 # define iswpunct(x) ispunct(x)
184 # define iswspace(x) isspace(x)
185 # define iswupper(x) isupper(x)
186 # define iswxdigit(x) isxdigit(x)
191 /*************************************************************************
192 * Compiler dependent definitions
195 /* Support for long long */
197 # if !defined(USE_LONGLONG)
198 # if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
199 # define USE_LONGLONG
200 # elif defined(TRIO_COMPILER_SUNPRO)
201 # define USE_LONGLONG
202 # elif defined(_LONG_LONG) || defined(_LONGLONG)
203 # define USE_LONGLONG
208 /* The extra long numbers */
209 #if defined(USE_LONGLONG)
210 typedef signed long long int trio_longlong_t
;
211 typedef unsigned long long int trio_ulonglong_t
;
212 #elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
213 typedef signed __int64 trio_longlong_t
;
214 typedef unsigned __int64 trio_ulonglong_t
;
216 typedef TRIO_SIGNED
long int trio_longlong_t
;
217 typedef unsigned long int trio_ulonglong_t
;
220 /* Maximal and fixed integer types */
221 #if defined(TRIO_COMPILER_SUPPORTS_C99) && !defined( __VMS )
223 typedef intmax_t trio_intmax_t
;
224 typedef uintmax_t trio_uintmax_t
;
225 typedef int8_t trio_int8_t
;
226 typedef int16_t trio_int16_t
;
227 typedef int32_t trio_int32_t
;
228 typedef int64_t trio_int64_t
;
229 #elif defined(TRIO_COMPILER_SUPPORTS_UNIX98) || defined( __VMS )
230 # include <inttypes.h>
232 typedef long long int intmax_t;
233 typedef unsigned long long int uintmax_t;
235 typedef intmax_t trio_intmax_t
;
236 typedef uintmax_t trio_uintmax_t
;
237 typedef int8_t trio_int8_t
;
238 typedef int16_t trio_int16_t
;
239 typedef int32_t trio_int32_t
;
240 typedef int64_t trio_int64_t
;
241 #elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
242 typedef trio_longlong_t trio_intmax_t
;
243 typedef trio_ulonglong_t trio_uintmax_t
;
244 typedef __int8 trio_int8_t
;
245 typedef __int16 trio_int16_t
;
246 typedef __int32 trio_int32_t
;
247 typedef __int64 trio_int64_t
;
249 typedef trio_longlong_t trio_intmax_t
;
250 typedef trio_ulonglong_t trio_uintmax_t
;
251 # if defined(TRIO_INT8_T)
252 typedef TRIO_INT8_T trio_int8_t
;
254 typedef TRIO_SIGNED
char trio_int8_t
;
256 # if defined(TRIO_INT16_T)
257 typedef TRIO_INT16_T trio_int16_t
;
259 typedef TRIO_SIGNED
short trio_int16_t
;
261 # if defined(TRIO_INT32_T)
262 typedef TRIO_INT32_T trio_int32_t
;
264 typedef TRIO_SIGNED
int trio_int32_t
;
266 # if defined(TRIO_INT64_T)
267 typedef TRIO_INT64_T trio_int64_t
;
269 typedef trio_longlong_t trio_int64_t
;
273 #if (!(defined(TRIO_COMPILER_SUPPORTS_C99) \
274 || defined(TRIO_COMPILER_SUPPORTS_UNIX01))) \
275 && !defined(_WIN32_WCE)
276 # define floorl(x) floor((double)(x))
277 # define fmodl(x,y) fmod((double)(x),(double)(y))
278 # define powl(x,y) pow((double)(x),(double)(y))
281 #define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
283 /*************************************************************************
284 * Internal Definitions
288 # define DECIMAL_DIG DBL_DIG
291 /* Long double sizes */
293 # define MAX_MANTISSA_DIGITS LDBL_DIG
294 # define MAX_EXPONENT_DIGITS 4
295 # define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
297 # define MAX_MANTISSA_DIGITS DECIMAL_DIG
298 # define MAX_EXPONENT_DIGITS 3
299 # define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
302 #if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
304 # undef LDBL_MANT_DIG
306 # define LDBL_DIG DBL_DIG
307 # define LDBL_MANT_DIG DBL_MANT_DIG
308 # define LDBL_EPSILON DBL_EPSILON
311 /* The maximal number of digits is for base 2 */
312 #define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
313 /* The width of a pointer. The number of bits in a hex digit is 4 */
314 #define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
316 /* Infinite and Not-A-Number for floating-point */
317 #define INFINITE_LOWER "inf"
318 #define INFINITE_UPPER "INF"
319 #define LONG_INFINITE_LOWER "infinite"
320 #define LONG_INFINITE_UPPER "INFINITE"
321 #define NAN_LOWER "nan"
322 #define NAN_UPPER "NAN"
324 #if !defined(HAVE_ISASCII) && !defined(isascii)
326 # define isascii(x) ((unsigned int)(x) < 128)
330 /* Various constants */
335 /* Flags. FLAGS_LAST must be less than ULONG_MAX */
338 FLAGS_SPACE
= 2 * FLAGS_STICKY
,
339 FLAGS_SHOWSIGN
= 2 * FLAGS_SPACE
,
340 FLAGS_LEFTADJUST
= 2 * FLAGS_SHOWSIGN
,
341 FLAGS_ALTERNATIVE
= 2 * FLAGS_LEFTADJUST
,
342 FLAGS_SHORT
= 2 * FLAGS_ALTERNATIVE
,
343 FLAGS_SHORTSHORT
= 2 * FLAGS_SHORT
,
344 FLAGS_LONG
= 2 * FLAGS_SHORTSHORT
,
345 FLAGS_QUAD
= 2 * FLAGS_LONG
,
346 FLAGS_LONGDOUBLE
= 2 * FLAGS_QUAD
,
347 FLAGS_SIZE_T
= 2 * FLAGS_LONGDOUBLE
,
348 FLAGS_PTRDIFF_T
= 2 * FLAGS_SIZE_T
,
349 FLAGS_INTMAX_T
= 2 * FLAGS_PTRDIFF_T
,
350 FLAGS_NILPADDING
= 2 * FLAGS_INTMAX_T
,
351 FLAGS_UNSIGNED
= 2 * FLAGS_NILPADDING
,
352 FLAGS_UPPER
= 2 * FLAGS_UNSIGNED
,
353 FLAGS_WIDTH
= 2 * FLAGS_UPPER
,
354 FLAGS_WIDTH_PARAMETER
= 2 * FLAGS_WIDTH
,
355 FLAGS_PRECISION
= 2 * FLAGS_WIDTH_PARAMETER
,
356 FLAGS_PRECISION_PARAMETER
= 2 * FLAGS_PRECISION
,
357 FLAGS_BASE
= 2 * FLAGS_PRECISION_PARAMETER
,
358 FLAGS_BASE_PARAMETER
= 2 * FLAGS_BASE
,
359 FLAGS_FLOAT_E
= 2 * FLAGS_BASE_PARAMETER
,
360 FLAGS_FLOAT_G
= 2 * FLAGS_FLOAT_E
,
361 FLAGS_QUOTE
= 2 * FLAGS_FLOAT_G
,
362 FLAGS_WIDECHAR
= 2 * FLAGS_QUOTE
,
363 FLAGS_ALLOC
= 2 * FLAGS_WIDECHAR
,
364 FLAGS_IGNORE
= 2 * FLAGS_ALLOC
,
365 FLAGS_IGNORE_PARAMETER
= 2 * FLAGS_IGNORE
,
366 FLAGS_VARSIZE_PARAMETER
= 2 * FLAGS_IGNORE_PARAMETER
,
367 FLAGS_FIXED_SIZE
= 2 * FLAGS_VARSIZE_PARAMETER
,
368 FLAGS_LAST
= FLAGS_FIXED_SIZE
,
370 FLAGS_EXCLUDE
= FLAGS_SHORT
,
371 FLAGS_USER_DEFINED
= FLAGS_IGNORE
,
372 FLAGS_ROUNDING
= FLAGS_INTMAX_T
,
373 /* Compounded flags */
374 FLAGS_ALL_VARSIZES
= FLAGS_LONG
| FLAGS_QUAD
| FLAGS_INTMAX_T
| FLAGS_PTRDIFF_T
| FLAGS_SIZE_T
,
375 FLAGS_ALL_SIZES
= FLAGS_ALL_VARSIZES
| FLAGS_SHORTSHORT
| FLAGS_SHORT
,
382 /* Do not change these */
391 /* Maximal number of allowed parameters */
393 /* Maximal number of characters in class */
394 MAX_CHARACTER_CLASS
= UCHAR_MAX
+ 1,
396 /* Maximal string lengths for user-defined specifiers */
400 /* Maximal length of locale separator strings */
401 MAX_LOCALE_SEPARATOR_LENGTH
= MB_LEN_MAX
,
402 /* Maximal number of integers in grouping */
403 MAX_LOCALE_GROUPS
= 64,
405 /* Initial size of asprintf buffer */
406 DYNAMIC_START_SIZE
= 32
409 #define NO_GROUPING ((int)CHAR_MAX)
411 /* Fundamental formatting parameter types */
412 #define FORMAT_UNKNOWN 0
414 #define FORMAT_DOUBLE 2
415 #define FORMAT_CHAR 3
416 #define FORMAT_STRING 4
417 #define FORMAT_POINTER 5
418 #define FORMAT_COUNT 6
419 #define FORMAT_PARAMETER 7
420 #define FORMAT_GROUP 8
422 # define FORMAT_ERRNO 9
425 # define FORMAT_USER_DEFINED 10
428 /* Character constants */
429 #define CHAR_IDENTIFIER '%'
430 #define CHAR_BACKSLASH '\\'
431 #define CHAR_QUOTE '\"'
432 #define CHAR_ADJUST ' '
434 /* Character class expressions */
435 #define CLASS_ALNUM "[:alnum:]"
436 #define CLASS_ALPHA "[:alpha:]"
437 #define CLASS_BLANK "[:blank:]"
438 #define CLASS_CNTRL "[:cntrl:]"
439 #define CLASS_DIGIT "[:digit:]"
440 #define CLASS_GRAPH "[:graph:]"
441 #define CLASS_LOWER "[:lower:]"
442 #define CLASS_PRINT "[:print:]"
443 #define CLASS_PUNCT "[:punct:]"
444 #define CLASS_SPACE "[:space:]"
445 #define CLASS_UPPER "[:upper:]"
446 #define CLASS_XDIGIT "[:xdigit:]"
455 * C Widechar character (wint_t)
469 * S Widechar string (wchar_t *)
478 * D Binary Coded Decimal %D(length,precision) (OS/390)
480 #define SPECIFIER_CHAR 'c'
481 #define SPECIFIER_STRING 's'
482 #define SPECIFIER_DECIMAL 'd'
483 #define SPECIFIER_INTEGER 'i'
484 #define SPECIFIER_UNSIGNED 'u'
485 #define SPECIFIER_OCTAL 'o'
486 #define SPECIFIER_HEX 'x'
487 #define SPECIFIER_HEX_UPPER 'X'
488 #define SPECIFIER_FLOAT_E 'e'
489 #define SPECIFIER_FLOAT_E_UPPER 'E'
490 #define SPECIFIER_FLOAT_F 'f'
491 #define SPECIFIER_FLOAT_F_UPPER 'F'
492 #define SPECIFIER_FLOAT_G 'g'
493 #define SPECIFIER_FLOAT_G_UPPER 'G'
494 #define SPECIFIER_POINTER 'p'
495 #define SPECIFIER_GROUP '['
496 #define SPECIFIER_UNGROUP ']'
497 #define SPECIFIER_COUNT 'n'
499 # define SPECIFIER_CHAR_UPPER 'C'
500 # define SPECIFIER_STRING_UPPER 'S'
503 # define SPECIFIER_HEXFLOAT 'a'
504 # define SPECIFIER_HEXFLOAT_UPPER 'A'
507 # define SPECIFIER_ERRNO 'm'
510 # define SPECIFIER_BINARY 'b'
511 # define SPECIFIER_BINARY_UPPER 'B'
512 # define SPECIFIER_USER_DEFINED_BEGIN '<'
513 # define SPECIFIER_USER_DEFINED_END '>'
514 # define SPECIFIER_USER_DEFINED_SEPARATOR ':'
521 * Numbers = d,i,o,u,x,X
522 * Float = a,A,e,E,f,F,g,G
528 * Use the 9th parameter. 9 can be any number between 1 and
529 * the maximal argument
532 * Set width to 9. 9 can be any number, but must not be postfixed
537 * (unsigned) short int
545 * (unsigned) long int
553 * (unsigned) long long int
561 * Decimal-point is always present
563 * non-printable characters are handled as \number
574 * print: use parameter
575 * scan: no parameter (ignore)
585 * Integer part grouped in thousands
587 * Number grouped in nibbles (4 bits)
596 * @ Parameter (for both print and scan)
600 * The following options exists
602 * I16 = 16-bit integer
603 * I32 = 32-bit integer
604 * I64 = 64-bit integer
606 #define QUALIFIER_POSITION '$'
607 #define QUALIFIER_SHORT 'h'
608 #define QUALIFIER_LONG 'l'
609 #define QUALIFIER_LONG_UPPER 'L'
610 #define QUALIFIER_ALTERNATIVE '#'
611 #define QUALIFIER_SPACE ' '
612 #define QUALIFIER_PLUS '+'
613 #define QUALIFIER_MINUS '-'
614 #define QUALIFIER_DOT '.'
615 #define QUALIFIER_STAR '*'
616 #define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
618 # define QUALIFIER_SIZE_T 'z'
619 # define QUALIFIER_PTRDIFF_T 't'
620 # define QUALIFIER_INTMAX_T 'j'
622 #if TRIO_BSD || TRIO_GNU
623 # define QUALIFIER_QUAD 'q'
626 # define QUALIFIER_SIZE_T_UPPER 'Z'
629 # define QUALIFIER_WIDECHAR 'w'
632 # define QUALIFIER_FIXED_SIZE 'I'
635 # define QUALIFIER_QUOTE '\''
636 # define QUALIFIER_STICKY '!'
637 # define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
638 # define QUALIFIER_PARAM '@' /* Experimental */
639 # define QUALIFIER_COLON ':' /* For scanlists */
640 # define QUALIFIER_EQUAL '=' /* For scanlists */
641 # define QUALIFIER_ROUNDING_UPPER 'R'
645 /*************************************************************************
647 * Internal Structures
649 *************************************************************************/
653 /* An indication of which entry in the data union is used */
657 /* The width qualifier */
659 /* The precision qualifier */
661 /* The base qualifier */
663 /* The size for the variable size qualifier */
665 /* The marker of the end of the specifier */
666 int indexAfterSpecifier
;
667 /* The data from the argument list */
671 trio_wchar_t
*wstring
;
673 trio_pointer_t pointer
;
675 trio_intmax_t as_signed
;
676 trio_uintmax_t as_unsigned
;
679 double *doublePointer
;
680 trio_long_double_t longdoubleNumber
;
681 trio_long_double_t
*longdoublePointer
;
684 /* For the user-defined specifier */
685 char user_name
[MAX_USER_NAME
];
686 char user_data
[MAX_USER_DATA
];
689 /* Container for customized functions */
692 trio_outstream_t out
;
695 trio_pointer_t closure
;
698 /* General trio "class" */
699 typedef struct _trio_class_t
{
701 * The function to write characters to a stream.
703 void (*OutStream
) TRIO_PROTO((struct _trio_class_t
*, int));
705 * The function to read characters from a stream.
707 void (*InStream
) TRIO_PROTO((struct _trio_class_t
*, int *));
709 * The current location in the stream.
711 trio_pointer_t location
;
713 * The character currently being processed.
717 * The number of characters that would have been written/read
718 * if there had been sufficient space.
722 * The number of characters that are actually written/read.
723 * Processed and committed will only differ for the *nprintf
724 * and *nscanf functions.
728 * The upper limit of characters that may be written/read.
732 * The last output error that was detected.
737 /* References (for user-defined callbacks) */
738 typedef struct _trio_reference_t
{
740 trio_parameter_t
*parameter
;
743 /* Registered entries (for user-defined callbacks) */
744 typedef struct _trio_userdef_t
{
745 struct _trio_userdef_t
*next
;
746 trio_callback_t callback
;
750 /*************************************************************************
754 *************************************************************************/
756 static TRIO_CONST
char rcsid
[] = "@(#)$Id$";
759 * Need this to workaround a parser bug in HP C/iX compiler that fails
760 * to resolves macro definitions that includes type 'long double',
761 * e.g: va_arg(arg_ptr, long double)
763 #if defined(TRIO_PLATFORM_MPEIX)
764 static TRIO_CONST trio_long_double_t ___dummy_long_double
= 0;
767 static TRIO_CONST
char internalNullString
[] = "(nil)";
769 #if defined(USE_LOCALE)
770 static struct lconv
*internalLocaleValues
= NULL
;
774 * UNIX98 says "in a locale where the radix character is not defined,
775 * the radix character defaults to a period (.)"
777 static int internalDecimalPointLength
= 1;
778 static int internalThousandSeparatorLength
= 1;
779 static char internalDecimalPoint
= '.';
780 static char internalDecimalPointString
[MAX_LOCALE_SEPARATOR_LENGTH
+ 1] = ".";
781 static char internalThousandSeparator
[MAX_LOCALE_SEPARATOR_LENGTH
+ 1] = ",";
782 static char internalGrouping
[MAX_LOCALE_GROUPS
] = { (char)NO_GROUPING
};
784 static TRIO_CONST
char internalDigitsLower
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
785 static TRIO_CONST
char internalDigitsUpper
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
786 static BOOLEAN_T internalDigitsUnconverted
= TRUE
;
787 static int internalDigitArray
[128];
789 static BOOLEAN_T internalCollationUnconverted
= TRUE
;
790 static char internalCollationArray
[MAX_CHARACTER_CLASS
][MAX_CHARACTER_CLASS
];
794 static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion
= NULL
;
795 static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion
= NULL
;
796 static trio_userdef_t
*internalUserDef
= NULL
;
800 /*************************************************************************
804 ************************************************************************/
806 #if defined(TRIO_MINIMAL)
807 # define TRIO_STRING_PUBLIC static
808 # include "triostr.c"
809 #endif /* defined(TRIO_MINIMAL) */
811 /*************************************************************************
815 * Remember to add all new qualifiers to this function.
816 * QUALIFIER_POSITION must not be added.
818 TRIO_PRIVATE BOOLEAN_T
820 TRIO_ARGS1((character
),
821 TRIO_CONST
char character
)
823 /* QUALIFIER_POSITION is not included */
826 case '0': case '1': case '2': case '3': case '4':
827 case '5': case '6': case '7': case '8': case '9':
829 case QUALIFIER_MINUS
:
830 case QUALIFIER_SPACE
:
833 case QUALIFIER_ALTERNATIVE
:
834 case QUALIFIER_SHORT
:
836 case QUALIFIER_LONG_UPPER
:
837 case QUALIFIER_CIRCUMFLEX
:
838 #if defined(QUALIFIER_SIZE_T)
839 case QUALIFIER_SIZE_T
:
841 #if defined(QUALIFIER_PTRDIFF_T)
842 case QUALIFIER_PTRDIFF_T
:
844 #if defined(QUALIFIER_INTMAX_T)
845 case QUALIFIER_INTMAX_T
:
847 #if defined(QUALIFIER_QUAD)
850 #if defined(QUALIFIER_SIZE_T_UPPER)
851 case QUALIFIER_SIZE_T_UPPER
:
853 #if defined(QUALIFIER_WIDECHAR)
854 case QUALIFIER_WIDECHAR
:
856 #if defined(QUALIFIER_QUOTE)
857 case QUALIFIER_QUOTE
:
859 #if defined(QUALIFIER_STICKY)
860 case QUALIFIER_STICKY
:
862 #if defined(QUALIFIER_VARSIZE)
863 case QUALIFIER_VARSIZE
:
865 #if defined(QUALIFIER_PARAM)
866 case QUALIFIER_PARAM
:
868 #if defined(QUALIFIER_FIXED_SIZE)
869 case QUALIFIER_FIXED_SIZE
:
871 #if defined(QUALIFIER_ROUNDING_UPPER)
872 case QUALIFIER_ROUNDING_UPPER
:
880 /*************************************************************************
883 #if defined(USE_LOCALE)
885 TrioSetLocale(TRIO_NOARGS
)
887 internalLocaleValues
= (struct lconv
*)localeconv();
888 if (internalLocaleValues
)
890 if ((internalLocaleValues
->decimal_point
) &&
891 (internalLocaleValues
->decimal_point
[0] != NIL
))
893 internalDecimalPointLength
= trio_length(internalLocaleValues
->decimal_point
);
894 if (internalDecimalPointLength
== 1)
896 internalDecimalPoint
= internalLocaleValues
->decimal_point
[0];
900 internalDecimalPoint
= NIL
;
901 trio_copy_max(internalDecimalPointString
,
902 sizeof(internalDecimalPointString
),
903 internalLocaleValues
->decimal_point
);
906 if ((internalLocaleValues
->thousands_sep
) &&
907 (internalLocaleValues
->thousands_sep
[0] != NIL
))
909 trio_copy_max(internalThousandSeparator
,
910 sizeof(internalThousandSeparator
),
911 internalLocaleValues
->thousands_sep
);
912 internalThousandSeparatorLength
= trio_length(internalThousandSeparator
);
914 if ((internalLocaleValues
->grouping
) &&
915 (internalLocaleValues
->grouping
[0] != NIL
))
917 trio_copy_max(internalGrouping
,
918 sizeof(internalGrouping
),
919 internalLocaleValues
->grouping
);
923 #endif /* defined(USE_LOCALE) */
926 TrioCalcThousandSeparatorLength
932 int step
= NO_GROUPING
;
933 char *groupingPointer
= internalGrouping
;
937 if (*groupingPointer
== CHAR_MAX
)
939 /* Disable grouping */
942 else if (*groupingPointer
== 0)
944 /* Repeat last group */
945 if (step
== NO_GROUPING
)
947 /* Error in locale */
953 step
= *groupingPointer
++;
956 count
+= internalThousandSeparatorLength
;
965 TRIO_PRIVATE BOOLEAN_T
966 TrioFollowedBySeparator
967 TRIO_ARGS1((position
),
972 char *groupingPointer
= internalGrouping
;
979 if (*groupingPointer
== CHAR_MAX
)
981 /* Disable grouping */
984 else if (*groupingPointer
!= 0)
986 step
= *groupingPointer
++;
992 return (position
== 0);
998 /*************************************************************************
1001 * Get the %n$ position.
1005 TRIO_ARGS2((format
, indexPointer
),
1006 TRIO_CONST
char *format
,
1012 int index
= *indexPointer
;
1014 number
= (int)trio_to_long(&format
[index
], &tmpformat
, BASE_DECIMAL
);
1015 index
= (int)(tmpformat
- format
);
1016 if ((number
!= 0) && (QUALIFIER_POSITION
== format
[index
++]))
1018 *indexPointer
= index
;
1020 * number is decreased by 1, because n$ starts from 1, whereas
1021 * the array it is indexing starts from 0.
1030 /*************************************************************************
1033 * Find registered user-defined specifier.
1034 * The prev argument is used for optimization only.
1036 TRIO_PRIVATE trio_userdef_t
*
1038 TRIO_ARGS2((name
, prev
),
1039 TRIO_CONST
char *name
,
1040 trio_userdef_t
**prev
)
1042 trio_userdef_t
*def
;
1044 if (internalEnterCriticalRegion
)
1045 (void)internalEnterCriticalRegion(NULL
);
1047 for (def
= internalUserDef
; def
; def
= def
->next
)
1049 /* Case-sensitive string comparison */
1050 if (trio_equal_case(def
->name
, name
))
1057 if (internalLeaveCriticalRegion
)
1058 (void)internalLeaveCriticalRegion(NULL
);
1064 /*************************************************************************
1068 * Calculate pow(base, exponent), where number and exponent are integers.
1070 TRIO_PRIVATE trio_long_double_t
1072 TRIO_ARGS2((number
, exponent
),
1076 trio_long_double_t result
;
1082 /* Speed up calculation of common cases */
1084 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E-1);
1087 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+0);
1090 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+1);
1093 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+2);
1096 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+3);
1099 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+4);
1102 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+5);
1105 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+6);
1108 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+7);
1111 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+8);
1114 result
= powl((trio_long_double_t
)number
,
1115 (trio_long_double_t
)exponent
);
1121 return powl((trio_long_double_t
)number
, (trio_long_double_t
)exponent
);
1126 /*************************************************************************
1131 TRIO_ARGS2((number
, base
),
1139 /* xlC crashes on log(0) */
1140 result
= (number
== 0.0) ? trio_ninf() : trio_nan();
1146 result
= log10(number
);
1150 result
= log10(number
) / log10((double)base
);
1156 /*************************************************************************
1166 case BASE_BINARY
: return 1.0;
1167 case BASE_OCTAL
: return 3.0;
1168 case BASE_DECIMAL
: return 3.321928094887362345;
1169 case BASE_HEX
: return 4.0;
1170 default : return TrioLogarithm((double)base
, 2);
1174 /*************************************************************************
1178 * Parse the format string
1182 TRIO_ARGS5((type
, format
, parameters
, arglist
, argarray
),
1184 TRIO_CONST
char *format
,
1185 trio_parameter_t
*parameters
,
1186 TRIO_VA_LIST_PTR arglist
,
1187 trio_pointer_t
*argarray
)
1189 /* Count the number of times a parameter is referenced */
1190 unsigned short usedEntries
[MAX_PARAMETERS
];
1191 /* Parameter counters */
1192 int parameterPosition
;
1195 /* Utility variables */
1201 int index
; /* Index into formatting string */
1202 int dots
; /* Count number of dots in modifier part */
1203 BOOLEAN_T positional
; /* Does the specifier have a positional? */
1204 BOOLEAN_T gotSticky
= FALSE
; /* Are there any sticky modifiers at all? */
1206 * indices specifies the order in which the parameters must be
1207 * read from the va_args (this is necessary to handle positionals)
1209 int indices
[MAX_PARAMETERS
];
1211 /* Various variables */
1213 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1221 /* One and only one of arglist and argarray must be used */
1222 assert((arglist
!= NULL
) ^ (argarray
!= NULL
));
1225 * The 'parameters' array is not initialized, but we need to
1226 * know which entries we have used.
1228 memset(usedEntries
, 0, sizeof(usedEntries
));
1232 parameterPosition
= 0;
1233 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1234 (void)mblen(NULL
, 0);
1237 while (format
[index
])
1239 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1240 if (! isascii(format
[index
]))
1243 * Multibyte characters cannot be legal specifiers or
1244 * modifiers, so we skip over them.
1246 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
1247 index
+= (charlen
> 0) ? charlen
: 1;
1248 continue; /* while */
1250 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
1251 if (CHAR_IDENTIFIER
== format
[index
++])
1253 if (CHAR_IDENTIFIER
== format
[index
])
1256 continue; /* while */
1261 currentParam
= TrioGetPosition(format
, &index
);
1262 positional
= (NO_POSITION
!= currentParam
);
1265 /* We have no positional, get the next counter */
1266 currentParam
= parameterPosition
;
1268 if(currentParam
>= MAX_PARAMETERS
)
1270 /* Bail out completely to make the error more obvious */
1271 return TRIO_ERROR_RETURN(TRIO_ETOOMANY
, index
);
1274 if (currentParam
> maxParam
)
1275 maxParam
= currentParam
;
1277 /* Default values */
1279 precision
= NO_PRECISION
;
1283 while (TrioIsQualifier(format
[index
]))
1285 ch
= format
[index
++];
1289 case QUALIFIER_SPACE
:
1290 flags
|= FLAGS_SPACE
;
1293 case QUALIFIER_PLUS
:
1294 flags
|= FLAGS_SHOWSIGN
;
1297 case QUALIFIER_MINUS
:
1298 flags
|= FLAGS_LEFTADJUST
;
1299 flags
&= ~FLAGS_NILPADDING
;
1302 case QUALIFIER_ALTERNATIVE
:
1303 flags
|= FLAGS_ALTERNATIVE
;
1307 if (dots
== 0) /* Precision */
1311 /* Skip if no precision */
1312 if (QUALIFIER_DOT
== format
[index
])
1315 /* After the first dot we have the precision */
1316 flags
|= FLAGS_PRECISION
;
1317 if ((QUALIFIER_STAR
== format
[index
])
1318 #if defined(QUALIFIER_PARAM)
1319 || (QUALIFIER_PARAM
== format
[index
])
1324 flags
|= FLAGS_PRECISION_PARAMETER
;
1326 precision
= TrioGetPosition(format
, &index
);
1327 if (precision
== NO_POSITION
)
1329 parameterPosition
++;
1331 precision
= parameterPosition
;
1334 precision
= currentParam
;
1335 currentParam
= precision
+ 1;
1341 currentParam
= precision
+ 1;
1342 if (width
> maxParam
)
1343 maxParam
= precision
;
1345 if (currentParam
> maxParam
)
1346 maxParam
= currentParam
;
1350 precision
= trio_to_long(&format
[index
],
1353 index
= (int)(tmpformat
- format
);
1356 else if (dots
== 1) /* Base */
1360 /* After the second dot we have the base */
1361 flags
|= FLAGS_BASE
;
1362 if ((QUALIFIER_STAR
== format
[index
])
1363 #if defined(QUALIFIER_PARAM)
1364 || (QUALIFIER_PARAM
== format
[index
])
1369 flags
|= FLAGS_BASE_PARAMETER
;
1370 base
= TrioGetPosition(format
, &index
);
1371 if (base
== NO_POSITION
)
1373 parameterPosition
++;
1375 base
= parameterPosition
;
1378 base
= currentParam
;
1379 currentParam
= base
+ 1;
1385 currentParam
= base
+ 1;
1386 if (base
> maxParam
)
1389 if (currentParam
> maxParam
)
1390 maxParam
= currentParam
;
1394 base
= trio_to_long(&format
[index
],
1397 if (base
> MAX_BASE
)
1398 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1399 index
= (int)(tmpformat
- format
);
1404 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1406 break; /* QUALIFIER_DOT */
1408 #if defined(QUALIFIER_PARAM)
1409 case QUALIFIER_PARAM
:
1413 case QUALIFIER_STAR
:
1414 /* This has different meanings for print and scan */
1415 if (TYPE_PRINT
== type
)
1417 /* Read with from parameter */
1418 flags
|= (FLAGS_WIDTH
| FLAGS_WIDTH_PARAMETER
);
1419 width
= TrioGetPosition(format
, &index
);
1420 if (width
== NO_POSITION
)
1422 parameterPosition
++;
1424 width
= parameterPosition
;
1427 width
= currentParam
;
1428 currentParam
= width
+ 1;
1434 currentParam
= width
+ 1;
1435 if (width
> maxParam
)
1438 if (currentParam
> maxParam
)
1439 maxParam
= currentParam
;
1443 /* Scan, but do not store result */
1444 flags
|= FLAGS_IGNORE
;
1447 break; /* QUALIFIER_STAR */
1450 if (! (flags
& FLAGS_LEFTADJUST
))
1451 flags
|= FLAGS_NILPADDING
;
1453 case '1': case '2': case '3': case '4':
1454 case '5': case '6': case '7': case '8': case '9':
1455 flags
|= FLAGS_WIDTH
;
1456 /* &format[index - 1] is used to "rewind" the read
1457 * character from format
1459 width
= trio_to_long(&format
[index
- 1],
1462 index
= (int)(tmpformat
- format
);
1465 case QUALIFIER_SHORT
:
1466 if (flags
& FLAGS_SHORTSHORT
)
1467 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1468 else if (flags
& FLAGS_SHORT
)
1469 flags
|= FLAGS_SHORTSHORT
;
1471 flags
|= FLAGS_SHORT
;
1474 case QUALIFIER_LONG
:
1475 if (flags
& FLAGS_QUAD
)
1476 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1477 else if (flags
& FLAGS_LONG
)
1478 flags
|= FLAGS_QUAD
;
1480 flags
|= FLAGS_LONG
;
1483 case QUALIFIER_LONG_UPPER
:
1484 flags
|= FLAGS_LONGDOUBLE
;
1487 #if defined(QUALIFIER_SIZE_T)
1488 case QUALIFIER_SIZE_T
:
1489 flags
|= FLAGS_SIZE_T
;
1490 /* Modify flags for later truncation of number */
1491 if (sizeof(size_t) == sizeof(trio_ulonglong_t
))
1492 flags
|= FLAGS_QUAD
;
1493 else if (sizeof(size_t) == sizeof(long))
1494 flags
|= FLAGS_LONG
;
1498 #if defined(QUALIFIER_PTRDIFF_T)
1499 case QUALIFIER_PTRDIFF_T
:
1500 flags
|= FLAGS_PTRDIFF_T
;
1501 if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t
))
1502 flags
|= FLAGS_QUAD
;
1503 else if (sizeof(ptrdiff_t) == sizeof(long))
1504 flags
|= FLAGS_LONG
;
1508 #if defined(QUALIFIER_INTMAX_T)
1509 case QUALIFIER_INTMAX_T
:
1510 flags
|= FLAGS_INTMAX_T
;
1511 if (sizeof(trio_intmax_t
) == sizeof(trio_ulonglong_t
))
1512 flags
|= FLAGS_QUAD
;
1513 else if (sizeof(trio_intmax_t
) == sizeof(long))
1514 flags
|= FLAGS_LONG
;
1518 #if defined(QUALIFIER_QUAD)
1519 case QUALIFIER_QUAD
:
1520 flags
|= FLAGS_QUAD
;
1524 #if defined(QUALIFIER_FIXED_SIZE)
1525 case QUALIFIER_FIXED_SIZE
:
1526 if (flags
& FLAGS_FIXED_SIZE
)
1527 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1529 if (flags
& (FLAGS_ALL_SIZES
| FLAGS_LONGDOUBLE
|
1530 FLAGS_WIDECHAR
| FLAGS_VARSIZE_PARAMETER
))
1531 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1533 if ((format
[index
] == '6') &&
1534 (format
[index
+ 1] == '4'))
1536 varsize
= sizeof(trio_int64_t
);
1539 else if ((format
[index
] == '3') &&
1540 (format
[index
+ 1] == '2'))
1542 varsize
= sizeof(trio_int32_t
);
1545 else if ((format
[index
] == '1') &&
1546 (format
[index
+ 1] == '6'))
1548 varsize
= sizeof(trio_int16_t
);
1551 else if (format
[index
] == '8')
1553 varsize
= sizeof(trio_int8_t
);
1557 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1559 flags
|= FLAGS_FIXED_SIZE
;
1563 #if defined(QUALIFIER_WIDECHAR)
1564 case QUALIFIER_WIDECHAR
:
1565 flags
|= FLAGS_WIDECHAR
;
1569 #if defined(QUALIFIER_SIZE_T_UPPER)
1570 case QUALIFIER_SIZE_T_UPPER
:
1574 #if defined(QUALIFIER_QUOTE)
1575 case QUALIFIER_QUOTE
:
1576 flags
|= FLAGS_QUOTE
;
1580 #if defined(QUALIFIER_STICKY)
1581 case QUALIFIER_STICKY
:
1582 flags
|= FLAGS_STICKY
;
1587 #if defined(QUALIFIER_VARSIZE)
1588 case QUALIFIER_VARSIZE
:
1589 flags
|= FLAGS_VARSIZE_PARAMETER
;
1590 parameterPosition
++;
1592 varsize
= parameterPosition
;
1595 varsize
= currentParam
;
1596 currentParam
= varsize
+ 1;
1598 if (currentParam
> maxParam
)
1599 maxParam
= currentParam
;
1603 #if defined(QUALIFIER_ROUNDING_UPPER)
1604 case QUALIFIER_ROUNDING_UPPER
:
1605 flags
|= FLAGS_ROUNDING
;
1610 /* Bail out completely to make the error more obvious */
1611 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1613 } /* while qualifier */
1616 * Parameters only need the type and value. The value is
1619 if (flags
& FLAGS_WIDTH_PARAMETER
)
1621 usedEntries
[width
] += 1;
1622 parameters
[pos
].type
= FORMAT_PARAMETER
;
1623 parameters
[pos
].flags
= 0;
1624 indices
[width
] = pos
;
1627 if (flags
& FLAGS_PRECISION_PARAMETER
)
1629 usedEntries
[precision
] += 1;
1630 parameters
[pos
].type
= FORMAT_PARAMETER
;
1631 parameters
[pos
].flags
= 0;
1632 indices
[precision
] = pos
;
1635 if (flags
& FLAGS_BASE_PARAMETER
)
1637 usedEntries
[base
] += 1;
1638 parameters
[pos
].type
= FORMAT_PARAMETER
;
1639 parameters
[pos
].flags
= 0;
1640 indices
[base
] = pos
;
1643 if (flags
& FLAGS_VARSIZE_PARAMETER
)
1645 usedEntries
[varsize
] += 1;
1646 parameters
[pos
].type
= FORMAT_PARAMETER
;
1647 parameters
[pos
].flags
= 0;
1648 indices
[varsize
] = pos
;
1652 indices
[currentParam
] = pos
;
1654 switch (format
[index
++])
1656 #if defined(SPECIFIER_CHAR_UPPER)
1657 case SPECIFIER_CHAR_UPPER
:
1658 flags
|= FLAGS_WIDECHAR
;
1661 case SPECIFIER_CHAR
:
1662 if (flags
& FLAGS_LONG
)
1663 flags
|= FLAGS_WIDECHAR
;
1664 else if (flags
& FLAGS_SHORT
)
1665 flags
&= ~FLAGS_WIDECHAR
;
1666 parameters
[pos
].type
= FORMAT_CHAR
;
1669 #if defined(SPECIFIER_STRING_UPPER)
1670 case SPECIFIER_STRING_UPPER
:
1671 flags
|= FLAGS_WIDECHAR
;
1674 case SPECIFIER_STRING
:
1675 if (flags
& FLAGS_LONG
)
1676 flags
|= FLAGS_WIDECHAR
;
1677 else if (flags
& FLAGS_SHORT
)
1678 flags
&= ~FLAGS_WIDECHAR
;
1679 parameters
[pos
].type
= FORMAT_STRING
;
1682 case SPECIFIER_GROUP
:
1683 if (TYPE_SCAN
== type
)
1686 parameters
[pos
].type
= FORMAT_GROUP
;
1687 if (format
[index
] == QUALIFIER_CIRCUMFLEX
)
1689 if (format
[index
] == SPECIFIER_UNGROUP
)
1691 if (format
[index
] == QUALIFIER_MINUS
)
1693 /* Skip nested brackets */
1694 while (format
[index
] != NIL
)
1696 if (format
[index
] == SPECIFIER_GROUP
)
1700 else if (format
[index
] == SPECIFIER_UNGROUP
)
1713 case SPECIFIER_INTEGER
:
1714 parameters
[pos
].type
= FORMAT_INT
;
1717 case SPECIFIER_UNSIGNED
:
1718 flags
|= FLAGS_UNSIGNED
;
1719 parameters
[pos
].type
= FORMAT_INT
;
1722 case SPECIFIER_DECIMAL
:
1723 /* Disable base modifier */
1724 flags
&= ~FLAGS_BASE_PARAMETER
;
1725 base
= BASE_DECIMAL
;
1726 parameters
[pos
].type
= FORMAT_INT
;
1729 case SPECIFIER_OCTAL
:
1730 flags
|= FLAGS_UNSIGNED
;
1731 flags
&= ~FLAGS_BASE_PARAMETER
;
1733 parameters
[pos
].type
= FORMAT_INT
;
1736 #if defined(SPECIFIER_BINARY)
1737 case SPECIFIER_BINARY_UPPER
:
1738 flags
|= FLAGS_UPPER
;
1740 case SPECIFIER_BINARY
:
1741 flags
|= FLAGS_NILPADDING
;
1742 flags
&= ~FLAGS_BASE_PARAMETER
;
1744 parameters
[pos
].type
= FORMAT_INT
;
1748 case SPECIFIER_HEX_UPPER
:
1749 flags
|= FLAGS_UPPER
;
1752 flags
|= FLAGS_UNSIGNED
;
1753 flags
&= ~FLAGS_BASE_PARAMETER
;
1755 parameters
[pos
].type
= FORMAT_INT
;
1758 case SPECIFIER_FLOAT_E_UPPER
:
1759 flags
|= FLAGS_UPPER
;
1761 case SPECIFIER_FLOAT_E
:
1762 flags
|= FLAGS_FLOAT_E
;
1763 parameters
[pos
].type
= FORMAT_DOUBLE
;
1766 case SPECIFIER_FLOAT_G_UPPER
:
1767 flags
|= FLAGS_UPPER
;
1769 case SPECIFIER_FLOAT_G
:
1770 flags
|= FLAGS_FLOAT_G
;
1771 parameters
[pos
].type
= FORMAT_DOUBLE
;
1774 case SPECIFIER_FLOAT_F_UPPER
:
1775 flags
|= FLAGS_UPPER
;
1777 case SPECIFIER_FLOAT_F
:
1778 parameters
[pos
].type
= FORMAT_DOUBLE
;
1781 case SPECIFIER_POINTER
:
1782 if (sizeof(trio_pointer_t
) == sizeof(trio_ulonglong_t
))
1783 flags
|= FLAGS_QUAD
;
1784 else if (sizeof(trio_pointer_t
) == sizeof(long))
1785 flags
|= FLAGS_LONG
;
1786 parameters
[pos
].type
= FORMAT_POINTER
;
1789 case SPECIFIER_COUNT
:
1790 parameters
[pos
].type
= FORMAT_COUNT
;
1793 #if defined(SPECIFIER_HEXFLOAT)
1794 # if defined(SPECIFIER_HEXFLOAT_UPPER)
1795 case SPECIFIER_HEXFLOAT_UPPER
:
1796 flags
|= FLAGS_UPPER
;
1799 case SPECIFIER_HEXFLOAT
:
1801 parameters
[pos
].type
= FORMAT_DOUBLE
;
1805 #if defined(FORMAT_ERRNO)
1806 case SPECIFIER_ERRNO
:
1807 parameters
[pos
].type
= FORMAT_ERRNO
;
1811 #if defined(SPECIFIER_USER_DEFINED_BEGIN)
1812 case SPECIFIER_USER_DEFINED_BEGIN
:
1815 int without_namespace
= TRUE
;
1817 parameters
[pos
].type
= FORMAT_USER_DEFINED
;
1818 parameters
[pos
].user_name
[0] = NIL
;
1819 tmpformat
= (char *)&format
[index
];
1821 while ((ch
= format
[index
]))
1824 if (ch
== SPECIFIER_USER_DEFINED_END
)
1826 if (without_namespace
)
1828 /* We must get the handle first */
1829 parameters
[pos
].type
= FORMAT_PARAMETER
;
1830 parameters
[pos
].indexAfterSpecifier
= index
;
1831 parameters
[pos
].flags
= FLAGS_USER_DEFINED
;
1832 /* Adjust parameters for insertion of new one */
1834 usedEntries
[currentParam
] += 1;
1835 parameters
[pos
].type
= FORMAT_USER_DEFINED
;
1837 indices
[currentParam
] = pos
;
1838 if (currentParam
> maxParam
)
1839 maxParam
= currentParam
;
1841 /* Copy the user data */
1842 max
= (unsigned int)(&format
[index
] - tmpformat
);
1843 if (max
> MAX_USER_DATA
)
1844 max
= MAX_USER_DATA
;
1845 trio_copy_max(parameters
[pos
].user_data
,
1850 if (ch
== SPECIFIER_USER_DEFINED_SEPARATOR
)
1852 without_namespace
= FALSE
;
1853 /* Copy the namespace for later looking-up */
1854 max
= (int)(&format
[index
] - tmpformat
);
1855 if (max
> MAX_USER_NAME
)
1856 max
= MAX_USER_NAME
;
1857 trio_copy_max(parameters
[pos
].user_name
,
1860 tmpformat
= (char *)&format
[index
];
1863 if (ch
!= SPECIFIER_USER_DEFINED_END
)
1864 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1867 #endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
1870 /* Bail out completely to make the error more obvious */
1871 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1874 /* Count the number of times this entry has been used */
1875 usedEntries
[currentParam
] += 1;
1877 /* Find last sticky parameters */
1878 if (gotSticky
&& !(flags
& FLAGS_STICKY
))
1880 for (i
= pos
- 1; i
>= 0; i
--)
1882 if (parameters
[i
].type
== FORMAT_PARAMETER
)
1884 if ((parameters
[i
].flags
& FLAGS_STICKY
) &&
1885 (parameters
[i
].type
== parameters
[pos
].type
))
1887 /* Do not overwrite current qualifiers */
1888 flags
|= (parameters
[i
].flags
& (unsigned long)~FLAGS_STICKY
);
1889 if (width
== NO_WIDTH
)
1890 width
= parameters
[i
].width
;
1891 if (precision
== NO_PRECISION
)
1892 precision
= parameters
[i
].precision
;
1893 if (base
== NO_BASE
)
1894 base
= parameters
[i
].base
;
1900 parameters
[pos
].indexAfterSpecifier
= index
;
1901 parameters
[pos
].flags
= flags
;
1902 parameters
[pos
].width
= width
;
1903 parameters
[pos
].precision
= precision
;
1904 parameters
[pos
].base
= (base
== NO_BASE
) ? BASE_DECIMAL
: base
;
1905 parameters
[pos
].varsize
= varsize
;
1909 parameterPosition
++;
1911 } /* if identifier */
1913 } /* while format characters left */
1915 for (num
= 0; num
<= maxParam
; num
++)
1917 if (usedEntries
[num
] != 1)
1919 if (usedEntries
[num
] == 0) /* gap detected */
1920 return TRIO_ERROR_RETURN(TRIO_EGAP
, num
);
1921 else /* double references detected */
1922 return TRIO_ERROR_RETURN(TRIO_EDBLREF
, num
);
1928 * FORMAT_PARAMETERS are only present if they must be read,
1929 * so it makes no sense to check the ignore flag (besides,
1930 * the flags variable is not set for that particular type)
1932 if ((parameters
[i
].type
!= FORMAT_PARAMETER
) &&
1933 (parameters
[i
].flags
& FLAGS_IGNORE
))
1934 continue; /* for all arguments */
1937 * The stack arguments are read according to ANSI C89
1938 * default argument promotions:
1942 * unsigned char = unsigned int
1943 * unsigned short = unsigned int
1946 * In addition to the ANSI C89 these types are read (the
1947 * default argument promotions of C99 has not been
1956 switch (parameters
[i
].type
)
1961 if (flags
& FLAGS_WIDECHAR
)
1963 parameters
[i
].data
.wstring
= (argarray
== NULL
)
1964 ? va_arg(TRIO_VA_LIST_DEREF(arglist
), trio_wchar_t
*)
1965 : (trio_wchar_t
*)(argarray
[num
]);
1970 parameters
[i
].data
.string
= (argarray
== NULL
)
1971 ? va_arg(TRIO_VA_LIST_DEREF(arglist
), char *)
1972 : (char *)(argarray
[num
]);
1976 #if defined(FORMAT_USER_DEFINED)
1977 case FORMAT_USER_DEFINED
:
1979 case FORMAT_POINTER
:
1981 case FORMAT_UNKNOWN
:
1982 parameters
[i
].data
.pointer
= (argarray
== NULL
)
1983 ? va_arg(TRIO_VA_LIST_DEREF(arglist
), trio_pointer_t
)
1989 if (TYPE_SCAN
== type
)
1991 if (argarray
== NULL
)
1992 parameters
[i
].data
.pointer
=
1993 (trio_pointer_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), trio_pointer_t
);
1996 if (parameters
[i
].type
== FORMAT_CHAR
)
1997 parameters
[i
].data
.pointer
=
1998 (trio_pointer_t
)((char *)argarray
[num
]);
1999 else if (parameters
[i
].flags
& FLAGS_SHORT
)
2000 parameters
[i
].data
.pointer
=
2001 (trio_pointer_t
)((short *)argarray
[num
]);
2003 parameters
[i
].data
.pointer
=
2004 (trio_pointer_t
)((int *)argarray
[num
]);
2009 #if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
2010 if (parameters
[i
].flags
2011 & (FLAGS_VARSIZE_PARAMETER
| FLAGS_FIXED_SIZE
))
2013 if (parameters
[i
].flags
& FLAGS_VARSIZE_PARAMETER
)
2016 * Variable sizes are mapped onto the fixed sizes, in
2017 * accordance with integer promotion.
2019 * Please note that this may not be portable, as we
2020 * only guess the size, not the layout of the numbers.
2021 * For example, if int is little-endian, and long is
2022 * big-endian, then this will fail.
2024 varsize
= (int)parameters
[parameters
[i
].varsize
].data
.number
.as_unsigned
;
2028 /* Used for the I<bits> modifiers */
2029 varsize
= parameters
[i
].varsize
;
2031 parameters
[i
].flags
&= ~FLAGS_ALL_VARSIZES
;
2033 if (varsize
<= (int)sizeof(int))
2035 else if (varsize
<= (int)sizeof(long))
2036 parameters
[i
].flags
|= FLAGS_LONG
;
2037 #if defined(QUALIFIER_INTMAX_T)
2038 else if (varsize
<= (int)sizeof(trio_longlong_t
))
2039 parameters
[i
].flags
|= FLAGS_QUAD
;
2041 parameters
[i
].flags
|= FLAGS_INTMAX_T
;
2044 parameters
[i
].flags
|= FLAGS_QUAD
;
2047 #endif /* defined(QUALIFIER_VARSIZE) */
2048 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
2049 if (parameters
[i
].flags
& FLAGS_SIZE_T
)
2050 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2051 ? (trio_uintmax_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), size_t)
2052 : (trio_uintmax_t
)(*((size_t *)argarray
[num
]));
2055 #if defined(QUALIFIER_PTRDIFF_T)
2056 if (parameters
[i
].flags
& FLAGS_PTRDIFF_T
)
2057 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2058 ? (trio_uintmax_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), ptrdiff_t)
2059 : (trio_uintmax_t
)(*((ptrdiff_t *)argarray
[num
]));
2062 #if defined(QUALIFIER_INTMAX_T)
2063 if (parameters
[i
].flags
& FLAGS_INTMAX_T
)
2064 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2065 ? (trio_uintmax_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), trio_intmax_t
)
2066 : (trio_uintmax_t
)(*((trio_intmax_t
*)argarray
[num
]));
2069 if (parameters
[i
].flags
& FLAGS_QUAD
)
2070 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2071 ? (trio_uintmax_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), trio_ulonglong_t
)
2072 : (trio_uintmax_t
)(*((trio_ulonglong_t
*)argarray
[num
]));
2073 else if (parameters
[i
].flags
& FLAGS_LONG
)
2074 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2075 ? (trio_uintmax_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), long)
2076 : (trio_uintmax_t
)(*((long *)argarray
[num
]));
2079 if (argarray
== NULL
)
2080 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), int);
2083 if (parameters
[i
].type
== FORMAT_CHAR
)
2084 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((char *)argarray
[num
]));
2085 else if (parameters
[i
].flags
& FLAGS_SHORT
)
2086 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((short *)argarray
[num
]));
2088 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((int *)argarray
[num
]));
2094 case FORMAT_PARAMETER
:
2096 * The parameter for the user-defined specifier is a pointer,
2097 * whereas the rest (width, precision, base) uses an integer.
2099 if (parameters
[i
].flags
& FLAGS_USER_DEFINED
)
2100 parameters
[i
].data
.pointer
= (argarray
== NULL
)
2101 ? va_arg(TRIO_VA_LIST_DEREF(arglist
), trio_pointer_t
)
2104 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2105 ? (trio_uintmax_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), int)
2106 : (trio_uintmax_t
)(*((int *)argarray
[num
]));
2110 if (TYPE_SCAN
== type
)
2112 if (parameters
[i
].flags
& FLAGS_LONGDOUBLE
)
2113 parameters
[i
].data
.longdoublePointer
= (argarray
== NULL
)
2114 ? va_arg(TRIO_VA_LIST_DEREF(arglist
), trio_long_double_t
*)
2115 : (trio_long_double_t
*)argarray
[num
];
2118 if (parameters
[i
].flags
& FLAGS_LONG
)
2119 parameters
[i
].data
.doublePointer
= (argarray
== NULL
)
2120 ? va_arg(TRIO_VA_LIST_DEREF(arglist
), double *)
2121 : (double *)argarray
[num
];
2123 parameters
[i
].data
.doublePointer
= (argarray
== NULL
)
2124 ? (double *)va_arg(TRIO_VA_LIST_DEREF(arglist
), float *)
2125 : (double *)((float *)argarray
[num
]);
2130 if (parameters
[i
].flags
& FLAGS_LONGDOUBLE
)
2131 parameters
[i
].data
.longdoubleNumber
= (argarray
== NULL
)
2132 ? va_arg(TRIO_VA_LIST_DEREF(arglist
), trio_long_double_t
)
2133 : (trio_long_double_t
)(*((trio_long_double_t
*)argarray
[num
]));
2136 if (argarray
== NULL
)
2137 parameters
[i
].data
.longdoubleNumber
=
2138 (trio_long_double_t
)va_arg(TRIO_VA_LIST_DEREF(arglist
), double);
2141 if (parameters
[i
].flags
& FLAGS_SHORT
)
2142 parameters
[i
].data
.longdoubleNumber
=
2143 (trio_long_double_t
)(*((float *)argarray
[num
]));
2145 parameters
[i
].data
.longdoubleNumber
=
2146 (trio_long_double_t
)(*((double *)argarray
[num
]));
2152 #if defined(FORMAT_ERRNO)
2154 parameters
[i
].data
.errorNumber
= save_errno
;
2161 } /* for all specifiers */
2166 /*************************************************************************
2170 ************************************************************************/
2173 /*************************************************************************
2178 * The complexity of this function is a result of the complexity
2179 * of the dependencies of the flags.
2183 TRIO_ARGS6((self
, number
, flags
, width
, precision
, base
),
2185 trio_uintmax_t number
,
2191 BOOLEAN_T isNegative
;
2192 BOOLEAN_T isNumberZero
;
2193 BOOLEAN_T isPrecisionZero
;
2194 BOOLEAN_T ignoreNumber
;
2195 char buffer
[MAX_CHARS_IN(trio_uintmax_t
) * (1 + MAX_LOCALE_SEPARATOR_LENGTH
) + 1];
2198 TRIO_CONST
char *digits
;
2204 assert(VALID(self
));
2205 assert(VALID(self
->OutStream
));
2206 assert(((base
>= MIN_BASE
) && (base
<= MAX_BASE
)) || (base
== NO_BASE
));
2208 digits
= (flags
& FLAGS_UPPER
) ? internalDigitsUpper
: internalDigitsLower
;
2209 if (base
== NO_BASE
)
2210 base
= BASE_DECIMAL
;
2212 isNumberZero
= (number
== 0);
2213 isPrecisionZero
= (precision
== 0);
2214 ignoreNumber
= (isNumberZero
2216 && !((flags
& FLAGS_ALTERNATIVE
) && (base
== BASE_OCTAL
)));
2218 if (flags
& FLAGS_UNSIGNED
)
2221 flags
&= ~FLAGS_SHOWSIGN
;
2225 isNegative
= ((trio_intmax_t
)number
< 0);
2227 number
= -((trio_intmax_t
)number
);
2230 if (flags
& FLAGS_QUAD
)
2231 number
&= (trio_ulonglong_t
)-1;
2232 else if (flags
& FLAGS_LONG
)
2233 number
&= (unsigned long)-1;
2235 number
&= (unsigned int)-1;
2238 pointer
= bufferend
= &buffer
[sizeof(buffer
) - 1];
2240 for (i
= 1; i
< (int)sizeof(buffer
); i
++)
2242 *pointer
-- = digits
[number
% base
];
2247 if ((flags
& FLAGS_QUOTE
) && TrioFollowedBySeparator(i
+ 1))
2250 * We are building the number from the least significant
2251 * to the most significant digit, so we have to copy the
2252 * thousand separator backwards
2254 length
= internalThousandSeparatorLength
;
2255 if (((int)(pointer
- buffer
) - length
) > 0)
2257 p
= &internalThousandSeparator
[length
- 1];
2258 while (length
-- > 0)
2267 width
-= (bufferend
- pointer
) - 1;
2270 /* Adjust precision */
2271 if (NO_PRECISION
!= precision
)
2273 precision
-= (bufferend
- pointer
) - 1;
2276 flags
|= FLAGS_NILPADDING
;
2279 /* Calculate padding */
2280 count
= (! ((flags
& FLAGS_LEFTADJUST
) || (precision
== NO_PRECISION
)))
2284 /* Adjust width further */
2285 if (isNegative
|| (flags
& FLAGS_SHOWSIGN
) || (flags
& FLAGS_SPACE
))
2287 if ((flags
& FLAGS_ALTERNATIVE
) && !isNumberZero
)
2296 if (!(flags
& FLAGS_NILPADDING
) || (count
== 0))
2304 /* Output prefixes spaces if needed */
2305 if (! ((flags
& FLAGS_LEFTADJUST
) ||
2306 ((flags
& FLAGS_NILPADDING
) && (precision
== NO_PRECISION
))))
2308 while (width
-- > count
)
2309 self
->OutStream(self
, CHAR_ADJUST
);
2312 /* width has been adjusted for signs and alternatives */
2314 self
->OutStream(self
, '-');
2315 else if (flags
& FLAGS_SHOWSIGN
)
2316 self
->OutStream(self
, '+');
2317 else if (flags
& FLAGS_SPACE
)
2318 self
->OutStream(self
, ' ');
2320 /* Prefix is not written when the value is zero */
2321 if ((flags
& FLAGS_ALTERNATIVE
) && !isNumberZero
)
2326 self
->OutStream(self
, '0');
2327 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'B' : 'b');
2331 if (!(flags
& FLAGS_NILPADDING
) || (count
== 0))
2332 self
->OutStream(self
, '0');
2336 self
->OutStream(self
, '0');
2337 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2345 /* Output prefixed zero padding if needed */
2346 if (flags
& FLAGS_NILPADDING
)
2348 if (precision
== NO_PRECISION
)
2350 while (precision
-- > 0)
2352 self
->OutStream(self
, '0');
2359 /* Output the number itself */
2360 while (*(++pointer
))
2362 self
->OutStream(self
, *pointer
);
2366 /* Output trailing spaces if needed */
2367 if (flags
& FLAGS_LEFTADJUST
)
2370 self
->OutStream(self
, CHAR_ADJUST
);
2374 /*************************************************************************
2375 * TrioWriteStringCharacter
2378 * Output a single character of a string
2381 TrioWriteStringCharacter
2382 TRIO_ARGS3((self
, ch
, flags
),
2387 if (flags
& FLAGS_ALTERNATIVE
)
2392 * Non-printable characters are converted to C escapes or
2393 * \number, if no C escape exists.
2395 self
->OutStream(self
, CHAR_BACKSLASH
);
2398 case '\007': self
->OutStream(self
, 'a'); break;
2399 case '\b': self
->OutStream(self
, 'b'); break;
2400 case '\f': self
->OutStream(self
, 'f'); break;
2401 case '\n': self
->OutStream(self
, 'n'); break;
2402 case '\r': self
->OutStream(self
, 'r'); break;
2403 case '\t': self
->OutStream(self
, 't'); break;
2404 case '\v': self
->OutStream(self
, 'v'); break;
2405 case '\\': self
->OutStream(self
, '\\'); break;
2407 self
->OutStream(self
, 'x');
2408 TrioWriteNumber(self
, (trio_uintmax_t
)ch
,
2409 FLAGS_UNSIGNED
| FLAGS_NILPADDING
,
2414 else if (ch
== CHAR_BACKSLASH
)
2416 self
->OutStream(self
, CHAR_BACKSLASH
);
2417 self
->OutStream(self
, CHAR_BACKSLASH
);
2421 self
->OutStream(self
, ch
);
2426 self
->OutStream(self
, ch
);
2430 /*************************************************************************
2438 TRIO_ARGS5((self
, string
, flags
, width
, precision
),
2440 TRIO_CONST
char *string
,
2448 assert(VALID(self
));
2449 assert(VALID(self
->OutStream
));
2453 string
= internalNullString
;
2454 length
= sizeof(internalNullString
) - 1;
2455 /* Disable quoting for the null pointer */
2456 flags
&= (~FLAGS_QUOTE
);
2461 length
= trio_length(string
);
2463 if ((NO_PRECISION
!= precision
) &&
2464 (precision
< length
))
2470 if (flags
& FLAGS_QUOTE
)
2471 self
->OutStream(self
, CHAR_QUOTE
);
2473 if (! (flags
& FLAGS_LEFTADJUST
))
2476 self
->OutStream(self
, CHAR_ADJUST
);
2479 while (length
-- > 0)
2481 /* The ctype parameters must be an unsigned char (or EOF) */
2482 ch
= (int)((unsigned char)(*string
++));
2483 TrioWriteStringCharacter(self
, ch
, flags
);
2486 if (flags
& FLAGS_LEFTADJUST
)
2489 self
->OutStream(self
, CHAR_ADJUST
);
2491 if (flags
& FLAGS_QUOTE
)
2492 self
->OutStream(self
, CHAR_QUOTE
);
2495 /*************************************************************************
2496 * TrioWriteWideStringCharacter
2499 * Output a wide string as a multi-byte sequence
2503 TrioWriteWideStringCharacter
2504 TRIO_ARGS4((self
, wch
, flags
, width
),
2514 char buffer
[MB_LEN_MAX
+ 1];
2516 if (width
== NO_WIDTH
)
2517 width
= sizeof(buffer
);
2519 size
= wctomb(buffer
, wch
);
2520 if ((size
<= 0) || (size
> width
) || (buffer
[0] == NIL
))
2525 while ((width
>= i
) && (width
-- > 0) && (i
-- > 0))
2527 /* The ctype parameters must be an unsigned char (or EOF) */
2528 ch
= (int)((unsigned char)(*string
++));
2529 TrioWriteStringCharacter(self
, ch
, flags
);
2533 #endif /* TRIO_WIDECHAR */
2535 /*************************************************************************
2536 * TrioWriteWideString
2539 * Output a wide character string as a multi-byte string
2544 TRIO_ARGS5((self
, wstring
, flags
, width
, precision
),
2546 TRIO_CONST trio_wchar_t
*wstring
,
2554 assert(VALID(self
));
2555 assert(VALID(self
->OutStream
));
2557 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
2558 (void)mblen(NULL
, 0);
2561 if (wstring
== NULL
)
2563 TrioWriteString(self
, NULL
, flags
, width
, precision
);
2567 if (NO_PRECISION
== precision
)
2577 if (flags
& FLAGS_QUOTE
)
2578 self
->OutStream(self
, CHAR_QUOTE
);
2580 if (! (flags
& FLAGS_LEFTADJUST
))
2583 self
->OutStream(self
, CHAR_ADJUST
);
2588 size
= TrioWriteWideStringCharacter(self
, *wstring
++, flags
, length
);
2594 if (flags
& FLAGS_LEFTADJUST
)
2597 self
->OutStream(self
, CHAR_ADJUST
);
2599 if (flags
& FLAGS_QUOTE
)
2600 self
->OutStream(self
, CHAR_QUOTE
);
2602 #endif /* TRIO_WIDECHAR */
2604 /*************************************************************************
2607 * http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_211.htm
2609 * "5.2.4.2.2 paragraph #4
2611 * The accuracy [...] is implementation defined, as is the accuracy
2612 * of the conversion between floating-point internal representations
2613 * and string representations performed by the libray routine in
2616 /* FIXME: handle all instances of constant long-double number (L)
2617 * and *l() math functions.
2621 TRIO_ARGS6((self
, number
, flags
, width
, precision
, base
),
2623 trio_long_double_t number
,
2629 trio_long_double_t integerNumber
;
2630 trio_long_double_t fractionNumber
;
2631 trio_long_double_t workNumber
;
2636 int integerThreshold
;
2637 int fractionThreshold
;
2640 unsigned int uExponent
= 0;
2642 trio_long_double_t dblBase
;
2643 trio_long_double_t dblIntegerBase
;
2644 trio_long_double_t dblFractionBase
;
2645 trio_long_double_t integerAdjust
;
2646 trio_long_double_t fractionAdjust
;
2647 BOOLEAN_T isNegative
;
2648 BOOLEAN_T isExponentNegative
= FALSE
;
2649 BOOLEAN_T requireTwoDigitExponent
;
2651 TRIO_CONST
char *digits
;
2652 char *groupingPointer
;
2655 BOOLEAN_T hasOnlyZeroes
;
2657 register int trailingZeroes
;
2658 BOOLEAN_T keepTrailingZeroes
;
2659 BOOLEAN_T keepDecimalPoint
;
2660 trio_long_double_t epsilon
;
2662 assert(VALID(self
));
2663 assert(VALID(self
->OutStream
));
2664 assert(((base
>= MIN_BASE
) && (base
<= MAX_BASE
)) || (base
== NO_BASE
));
2666 /* Determine sign and look for special quantities */
2667 switch (trio_fpclassify_and_signbit(number
, &isNegative
))
2670 TrioWriteString(self
,
2671 (flags
& FLAGS_UPPER
)
2674 flags
, width
, precision
);
2677 case TRIO_FP_INFINITE
:
2680 /* Negative infinity */
2681 TrioWriteString(self
,
2682 (flags
& FLAGS_UPPER
)
2683 ? "-" INFINITE_UPPER
2684 : "-" INFINITE_LOWER
,
2685 flags
, width
, precision
);
2690 /* Positive infinity */
2691 TrioWriteString(self
,
2692 (flags
& FLAGS_UPPER
)
2695 flags
, width
, precision
);
2704 /* Normal numbers */
2705 if (flags
& FLAGS_LONGDOUBLE
)
2707 baseDigits
= (base
== 10)
2709 : (int)floor(LDBL_MANT_DIG
/ TrioLogarithmBase(base
));
2710 epsilon
= LDBL_EPSILON
;
2712 else if (flags
& FLAGS_SHORT
)
2714 baseDigits
= (base
== BASE_DECIMAL
)
2716 : (int)floor(FLT_MANT_DIG
/ TrioLogarithmBase(base
));
2717 epsilon
= FLT_EPSILON
;
2721 baseDigits
= (base
== BASE_DECIMAL
)
2723 : (int)floor(DBL_MANT_DIG
/ TrioLogarithmBase(base
));
2724 epsilon
= DBL_EPSILON
;
2727 digits
= (flags
& FLAGS_UPPER
) ? internalDigitsUpper
: internalDigitsLower
;
2728 isHex
= (base
== BASE_HEX
);
2729 if (base
== NO_BASE
)
2730 base
= BASE_DECIMAL
;
2731 dblBase
= (trio_long_double_t
)base
;
2732 keepTrailingZeroes
= !( (flags
& FLAGS_ROUNDING
) ||
2733 ( (flags
& FLAGS_FLOAT_G
) &&
2734 !(flags
& FLAGS_ALTERNATIVE
) ) );
2736 if (flags
& FLAGS_ROUNDING
)
2737 precision
= baseDigits
;
2739 if (precision
== NO_PRECISION
)
2743 keepTrailingZeroes
= FALSE
;
2744 precision
= FLT_MANT_DIG
;
2748 precision
= FLT_DIG
;
2756 flags
|= FLAGS_FLOAT_E
;
2758 if (flags
& FLAGS_FLOAT_G
)
2763 if ((number
< 1.0E-4) || (number
> powl(base
,
2764 (trio_long_double_t
)precision
)))
2766 /* Use scientific notation */
2767 flags
|= FLAGS_FLOAT_E
;
2769 else if (number
< 1.0)
2772 * Use normal notation. If the integer part of the number is
2773 * zero, then adjust the precision to include leading fractional
2776 workNumber
= TrioLogarithm(number
, base
);
2777 workNumber
= TRIO_FABS(workNumber
);
2778 if (workNumber
- floorl(workNumber
) < 0.001)
2780 zeroes
= (int)floorl(workNumber
);
2784 if (flags
& FLAGS_FLOAT_E
)
2786 /* Scale the number */
2787 workNumber
= TrioLogarithm(number
, base
);
2788 if (trio_isinf(workNumber
) == -1)
2792 if (flags
& FLAGS_FLOAT_G
)
2793 flags
&= ~FLAGS_FLOAT_E
;
2797 exponent
= (int)floorl(workNumber
);
2798 number
/= powl(dblBase
, (trio_long_double_t
)exponent
);
2799 isExponentNegative
= (exponent
< 0);
2800 uExponent
= (isExponentNegative
) ? -exponent
: exponent
;
2802 uExponent
*= 4; /* log16(2) */
2803 /* No thousand separators */
2804 flags
&= ~FLAGS_QUOTE
;
2808 integerNumber
= floorl(number
);
2809 fractionNumber
= number
- integerNumber
;
2814 * Precision is number of significant digits for FLOAT_G
2815 * and number of fractional digits for others.
2817 integerDigits
= (integerNumber
> epsilon
)
2818 ? 1 + (int)TrioLogarithm(integerNumber
, base
)
2820 fractionDigits
= ((flags
& FLAGS_FLOAT_G
) && (zeroes
== 0))
2821 ? precision
- integerDigits
2822 : zeroes
+ precision
;
2824 dblFractionBase
= TrioPower(base
, fractionDigits
);
2826 workNumber
= number
+ 0.5 / dblFractionBase
;
2827 if (floorl(number
) != floorl(workNumber
))
2829 if (flags
& FLAGS_FLOAT_E
)
2831 /* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
2833 isExponentNegative
= (exponent
< 0);
2834 uExponent
= (isExponentNegative
) ? -exponent
: exponent
;
2836 uExponent
*= 4; /* log16(2) */
2837 workNumber
= (number
+ 0.5 / dblFractionBase
) / dblBase
;
2838 integerNumber
= floorl(workNumber
);
2839 fractionNumber
= workNumber
- integerNumber
;
2843 /* Adjust if number was rounded up one digit (ie. 99 to 100) */
2844 integerNumber
= floorl(number
+ 0.5);
2845 fractionNumber
= 0.0;
2846 integerDigits
= (integerNumber
> epsilon
)
2847 ? 1 + (int)TrioLogarithm(integerNumber
, base
)
2852 /* Estimate accuracy */
2853 integerAdjust
= fractionAdjust
= 0.5;
2854 if (flags
& FLAGS_ROUNDING
)
2856 if (integerDigits
> baseDigits
)
2858 integerThreshold
= baseDigits
;
2860 dblFractionBase
= 1.0;
2861 fractionThreshold
= 0;
2862 precision
= 0; /* Disable decimal-point */
2863 integerAdjust
= TrioPower(base
, integerDigits
- integerThreshold
- 1);
2864 fractionAdjust
= 0.0;
2868 integerThreshold
= integerDigits
;
2869 fractionThreshold
= fractionDigits
- integerThreshold
;
2870 fractionAdjust
= 1.0;
2875 integerThreshold
= INT_MAX
;
2876 fractionThreshold
= INT_MAX
;
2880 * Calculate expected width.
2881 * sign + integer part + thousands separators + decimal point
2882 * + fraction + exponent
2884 fractionAdjust
/= dblFractionBase
;
2885 hasOnlyZeroes
= (floorl((fractionNumber
+ fractionAdjust
) * dblFractionBase
) < epsilon
);
2886 keepDecimalPoint
= ( (flags
& FLAGS_ALTERNATIVE
) ||
2887 !((precision
== 0) ||
2888 (!keepTrailingZeroes
&& hasOnlyZeroes
)) );
2889 if (flags
& FLAGS_FLOAT_E
)
2891 exponentDigits
= (uExponent
== 0)
2893 : (int)ceil(TrioLogarithm((double)(uExponent
+ 1),
2894 (isHex
) ? 10.0 : base
));
2898 requireTwoDigitExponent
= ((base
== BASE_DECIMAL
) && (exponentDigits
== 1));
2900 expectedWidth
= integerDigits
+ fractionDigits
2902 ? internalDecimalPointLength
2904 + ((flags
& FLAGS_QUOTE
)
2905 ? TrioCalcThousandSeparatorLength(integerDigits
)
2907 if (isNegative
|| (flags
& FLAGS_SHOWSIGN
) || (flags
& FLAGS_SPACE
))
2908 expectedWidth
+= sizeof("-") - 1;
2909 if (exponentDigits
> 0)
2910 expectedWidth
+= exponentDigits
+
2911 ((requireTwoDigitExponent
? sizeof("E+0") : sizeof("E+")) - 1);
2913 expectedWidth
+= sizeof("0X") - 1;
2915 /* Output prefixing */
2916 if (flags
& FLAGS_NILPADDING
)
2918 /* Leading zeros must be after sign */
2920 self
->OutStream(self
, '-');
2921 else if (flags
& FLAGS_SHOWSIGN
)
2922 self
->OutStream(self
, '+');
2923 else if (flags
& FLAGS_SPACE
)
2924 self
->OutStream(self
, ' ');
2927 self
->OutStream(self
, '0');
2928 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2930 if (!(flags
& FLAGS_LEFTADJUST
))
2932 for (i
= expectedWidth
; i
< width
; i
++)
2934 self
->OutStream(self
, '0');
2940 /* Leading spaces must be before sign */
2941 if (!(flags
& FLAGS_LEFTADJUST
))
2943 for (i
= expectedWidth
; i
< width
; i
++)
2945 self
->OutStream(self
, CHAR_ADJUST
);
2949 self
->OutStream(self
, '-');
2950 else if (flags
& FLAGS_SHOWSIGN
)
2951 self
->OutStream(self
, '+');
2952 else if (flags
& FLAGS_SPACE
)
2953 self
->OutStream(self
, ' ');
2956 self
->OutStream(self
, '0');
2957 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2961 /* Output the integer part and thousand separators */
2962 dblIntegerBase
= 1.0 / TrioPower(base
, integerDigits
- 1);
2963 for (i
= 0; i
< integerDigits
; i
++)
2965 workNumber
= floorl(((integerNumber
+ integerAdjust
) * dblIntegerBase
));
2966 if (i
> integerThreshold
)
2968 /* Beyond accuracy */
2969 self
->OutStream(self
, digits
[0]);
2973 self
->OutStream(self
, digits
[(int)fmodl(workNumber
, dblBase
)]);
2975 dblIntegerBase
*= dblBase
;
2977 if (((flags
& (FLAGS_FLOAT_E
| FLAGS_QUOTE
)) == FLAGS_QUOTE
)
2978 && TrioFollowedBySeparator(integerDigits
- i
))
2980 for (groupingPointer
= internalThousandSeparator
;
2981 *groupingPointer
!= NIL
;
2984 self
->OutStream(self
, *groupingPointer
);
2989 /* Insert decimal point and build the fraction part */
2992 if (keepDecimalPoint
)
2994 if (internalDecimalPoint
)
2996 self
->OutStream(self
, internalDecimalPoint
);
3000 for (i
= 0; i
< internalDecimalPointLength
; i
++)
3002 self
->OutStream(self
, internalDecimalPointString
[i
]);
3007 for (i
= 0; i
< fractionDigits
; i
++)
3009 if ((integerDigits
> integerThreshold
) || (i
> fractionThreshold
))
3011 /* Beyond accuracy */
3016 fractionNumber
*= dblBase
;
3017 fractionAdjust
*= dblBase
;
3018 workNumber
= floorl(fractionNumber
+ fractionAdjust
);
3019 fractionNumber
-= workNumber
;
3020 index
= (int)fmodl(workNumber
, dblBase
);
3027 while (trailingZeroes
> 0)
3029 /* Not trailing zeroes after all */
3030 self
->OutStream(self
, digits
[0]);
3033 self
->OutStream(self
, digits
[index
]);
3038 if (keepTrailingZeroes
)
3040 while (trailingZeroes
> 0)
3042 self
->OutStream(self
, digits
[0]);
3047 /* Output exponent */
3048 if (exponentDigits
> 0)
3050 self
->OutStream(self
,
3052 ? ((flags
& FLAGS_UPPER
) ? 'P' : 'p')
3053 : ((flags
& FLAGS_UPPER
) ? 'E' : 'e'));
3054 self
->OutStream(self
, (isExponentNegative
) ? '-' : '+');
3056 /* The exponent must contain at least two digits */
3057 if (requireTwoDigitExponent
)
3058 self
->OutStream(self
, '0');
3062 exponentBase
= (int)TrioPower(base
, exponentDigits
- 1);
3063 for (i
= 0; i
< exponentDigits
; i
++)
3065 self
->OutStream(self
, digits
[(uExponent
/ exponentBase
) % base
]);
3066 exponentBase
/= base
;
3069 /* Output trailing spaces */
3070 if (flags
& FLAGS_LEFTADJUST
)
3072 for (i
= expectedWidth
; i
< width
; i
++)
3074 self
->OutStream(self
, CHAR_ADJUST
);
3079 /*************************************************************************
3083 * This is the main engine for formatting output
3087 TRIO_ARGS3((data
, format
, parameters
),
3089 TRIO_CONST
char *format
,
3090 trio_parameter_t
*parameters
)
3092 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3096 TRIO_CONST
char *string
;
3097 trio_pointer_t pointer
;
3106 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3107 (void)mblen(NULL
, 0);
3110 while (format
[index
])
3112 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3113 if (! isascii(format
[index
]))
3115 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
3117 * Only valid multibyte characters are handled here. Invalid
3118 * multibyte characters (charlen == -1) are handled as normal
3123 while (charlen
-- > 0)
3125 data
->OutStream(data
, format
[index
++]);
3127 continue; /* while characters left in formatting string */
3130 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
3131 if (CHAR_IDENTIFIER
== format
[index
])
3133 if (CHAR_IDENTIFIER
== format
[index
+ 1])
3135 data
->OutStream(data
, CHAR_IDENTIFIER
);
3140 /* Skip the parameter entries */
3141 while (parameters
[i
].type
== FORMAT_PARAMETER
)
3144 flags
= parameters
[i
].flags
;
3147 width
= parameters
[i
].width
;
3148 if (flags
& FLAGS_WIDTH_PARAMETER
)
3150 /* Get width from parameter list */
3151 width
= (int)parameters
[width
].data
.number
.as_signed
;
3155 * A negative width is the same as the - flag and
3158 flags
|= FLAGS_LEFTADJUST
;
3159 flags
&= ~FLAGS_NILPADDING
;
3164 /* Find precision */
3165 if (flags
& FLAGS_PRECISION
)
3167 precision
= parameters
[i
].precision
;
3168 if (flags
& FLAGS_PRECISION_PARAMETER
)
3170 /* Get precision from parameter list */
3171 precision
= (int)parameters
[precision
].data
.number
.as_signed
;
3175 * A negative precision is the same as no
3178 precision
= NO_PRECISION
;
3184 precision
= NO_PRECISION
;
3188 base
= parameters
[i
].base
;
3189 if (flags
& FLAGS_BASE_PARAMETER
)
3191 /* Get base from parameter list */
3192 base
= (int)parameters
[base
].data
.number
.as_signed
;
3195 switch (parameters
[i
].type
)
3198 if (flags
& FLAGS_QUOTE
)
3199 data
->OutStream(data
, CHAR_QUOTE
);
3200 if (! (flags
& FLAGS_LEFTADJUST
))
3203 data
->OutStream(data
, CHAR_ADJUST
);
3206 if (flags
& FLAGS_WIDECHAR
)
3208 TrioWriteWideStringCharacter(data
,
3209 (trio_wchar_t
)parameters
[i
].data
.number
.as_signed
,
3216 TrioWriteStringCharacter(data
,
3217 (int)parameters
[i
].data
.number
.as_signed
,
3221 if (flags
& FLAGS_LEFTADJUST
)
3224 data
->OutStream(data
, CHAR_ADJUST
);
3226 if (flags
& FLAGS_QUOTE
)
3227 data
->OutStream(data
, CHAR_QUOTE
);
3229 break; /* FORMAT_CHAR */
3232 TrioWriteNumber(data
,
3233 parameters
[i
].data
.number
.as_unsigned
,
3239 break; /* FORMAT_INT */
3242 TrioWriteDouble(data
,
3243 parameters
[i
].data
.longdoubleNumber
,
3248 break; /* FORMAT_DOUBLE */
3252 if (flags
& FLAGS_WIDECHAR
)
3254 TrioWriteWideString(data
,
3255 parameters
[i
].data
.wstring
,
3263 TrioWriteString(data
,
3264 parameters
[i
].data
.string
,
3269 break; /* FORMAT_STRING */
3271 case FORMAT_POINTER
:
3273 trio_reference_t reference
;
3275 reference
.data
= data
;
3276 reference
.parameter
= ¶meters
[i
];
3277 trio_print_pointer(&reference
, parameters
[i
].data
.pointer
);
3279 break; /* FORMAT_POINTER */
3282 pointer
= parameters
[i
].data
.pointer
;
3283 if (NULL
!= pointer
)
3286 * C99 paragraph 7.19.6.1.8 says "the number of
3287 * characters written to the output stream so far by
3288 * this call", which is data->committed
3290 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
3291 if (flags
& FLAGS_SIZE_T
)
3292 *(size_t *)pointer
= (size_t)data
->committed
;
3295 #if defined(QUALIFIER_PTRDIFF_T)
3296 if (flags
& FLAGS_PTRDIFF_T
)
3297 *(ptrdiff_t *)pointer
= (ptrdiff_t)data
->committed
;
3300 #if defined(QUALIFIER_INTMAX_T)
3301 if (flags
& FLAGS_INTMAX_T
)
3302 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)data
->committed
;
3305 if (flags
& FLAGS_QUAD
)
3307 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)data
->committed
;
3309 else if (flags
& FLAGS_LONG
)
3311 *(long int *)pointer
= (long int)data
->committed
;
3313 else if (flags
& FLAGS_SHORT
)
3315 *(short int *)pointer
= (short int)data
->committed
;
3319 *(int *)pointer
= (int)data
->committed
;
3322 break; /* FORMAT_COUNT */
3324 case FORMAT_PARAMETER
:
3325 break; /* FORMAT_PARAMETER */
3327 #if defined(FORMAT_ERRNO)
3329 string
= trio_error(parameters
[i
].data
.errorNumber
);
3332 TrioWriteString(data
,
3340 data
->OutStream(data
, '#');
3341 TrioWriteNumber(data
,
3342 (trio_uintmax_t
)parameters
[i
].data
.errorNumber
,
3348 break; /* FORMAT_ERRNO */
3349 #endif /* defined(FORMAT_ERRNO) */
3351 #if defined(FORMAT_USER_DEFINED)
3352 case FORMAT_USER_DEFINED
:
3354 trio_reference_t reference
;
3355 trio_userdef_t
*def
= NULL
;
3357 if (parameters
[i
].user_name
[0] == NIL
)
3361 (parameters
[i
- 1].type
== FORMAT_PARAMETER
))
3362 def
= (trio_userdef_t
*)parameters
[i
- 1].data
.pointer
;
3366 /* Look up namespace */
3367 def
= TrioFindNamespace(parameters
[i
].user_name
, NULL
);
3370 reference
.data
= data
;
3371 reference
.parameter
= ¶meters
[i
];
3372 def
->callback(&reference
);
3376 #endif /* defined(FORMAT_USER_DEFINED) */
3380 } /* switch parameter type */
3382 /* Prepare for next */
3383 index
= parameters
[i
].indexAfterSpecifier
;
3387 else /* not identifier */
3389 data
->OutStream(data
, format
[index
++]);
3392 return data
->processed
;
3395 /*************************************************************************
3400 TRIO_ARGS4((reference
, format
, arglist
, argarray
),
3401 trio_reference_t
*reference
,
3402 TRIO_CONST
char *format
,
3403 TRIO_VA_LIST_PTR arglist
,
3404 trio_pointer_t
*argarray
)
3407 trio_parameter_t parameters
[MAX_PARAMETERS
];
3409 status
= TrioParse(TYPE_PRINT
, format
, parameters
, arglist
, argarray
);
3413 status
= TrioFormatProcess(reference
->data
, format
, parameters
);
3414 if (reference
->data
->error
!= 0)
3416 status
= reference
->data
->error
;
3421 /*************************************************************************
3426 TRIO_ARGS6((destination
, destinationSize
, OutStream
, format
, arglist
, argarray
),
3427 trio_pointer_t destination
,
3428 size_t destinationSize
,
3429 void (*OutStream
) TRIO_PROTO((trio_class_t
*, int)),
3430 TRIO_CONST
char *format
,
3431 TRIO_VA_LIST_PTR arglist
,
3432 trio_pointer_t
*argarray
)
3436 trio_parameter_t parameters
[MAX_PARAMETERS
];
3438 assert(VALID(OutStream
));
3439 assert(VALID(format
));
3441 memset(&data
, 0, sizeof(data
));
3442 data
.OutStream
= OutStream
;
3443 data
.location
= destination
;
3444 data
.max
= destinationSize
;
3447 #if defined(USE_LOCALE)
3448 if (NULL
== internalLocaleValues
)
3454 status
= TrioParse(TYPE_PRINT
, format
, parameters
, arglist
, argarray
);
3458 status
= TrioFormatProcess(&data
, format
, parameters
);
3459 if (data
.error
!= 0)
3461 status
= data
.error
;
3466 /*************************************************************************
3471 TRIO_ARGS2((self
, output
),
3477 assert(VALID(self
));
3478 assert(VALID(self
->location
));
3480 file
= (FILE *)self
->location
;
3482 if (fputc(output
, file
) == EOF
)
3484 self
->error
= TRIO_ERROR_RETURN(TRIO_EOF
, 0);
3492 /*************************************************************************
3493 * TrioOutStreamFileDescriptor
3496 TrioOutStreamFileDescriptor
3497 TRIO_ARGS2((self
, output
),
3504 assert(VALID(self
));
3506 fd
= *((int *)self
->location
);
3509 if (write(fd
, &ch
, sizeof(char)) == -1)
3511 self
->error
= TRIO_ERROR_RETURN(TRIO_ERRNO
, 0);
3519 /*************************************************************************
3520 * TrioOutStreamCustom
3524 TRIO_ARGS2((self
, output
),
3529 trio_custom_t
*data
;
3531 assert(VALID(self
));
3532 assert(VALID(self
->location
));
3534 data
= (trio_custom_t
*)self
->location
;
3535 if (data
->stream
.out
)
3537 status
= (data
->stream
.out
)(data
->closure
, output
);
3544 if (self
->error
== 0)
3546 self
->error
= TRIO_ERROR_RETURN(TRIO_ECUSTOM
, -status
);
3553 /*************************************************************************
3554 * TrioOutStreamString
3558 TRIO_ARGS2((self
, output
),
3564 assert(VALID(self
));
3565 assert(VALID(self
->location
));
3567 buffer
= (char **)self
->location
;
3568 **buffer
= (char)output
;
3574 /*************************************************************************
3575 * TrioOutStreamStringMax
3578 TrioOutStreamStringMax
3579 TRIO_ARGS2((self
, output
),
3585 assert(VALID(self
));
3586 assert(VALID(self
->location
));
3588 buffer
= (char **)self
->location
;
3590 if (self
->processed
< self
->max
)
3592 **buffer
= (char)output
;
3599 /*************************************************************************
3600 * TrioOutStreamStringDynamic
3603 TrioOutStreamStringDynamic
3604 TRIO_ARGS2((self
, output
),
3608 assert(VALID(self
));
3609 assert(VALID(self
->location
));
3611 if (self
->error
== 0)
3613 trio_xstring_append_char((trio_string_t
*)self
->location
,
3617 /* The processed variable must always be increased */
3621 /*************************************************************************
3623 * Formatted printing functions
3625 ************************************************************************/
3627 #if defined(TRIO_DOCUMENTATION)
3628 # include "doc/doc_printf.h"
3630 /** @addtogroup Printf
3634 /*************************************************************************
3639 Print to standard output stream.
3641 @param format Formatting string.
3642 @param ... Arguments.
3643 @return Number of printed characters.
3647 TRIO_VARGS2((format
, va_alist
),
3648 TRIO_CONST
char *format
,
3654 assert(VALID(format
));
3656 TRIO_VA_START(args
, format
);
3657 status
= TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3663 Print to standard output stream.
3665 @param format Formatting string.
3666 @param args Arguments.
3667 @return Number of printed characters.
3671 TRIO_ARGS2((format
, args
),
3672 TRIO_CONST
char *format
,
3675 assert(VALID(format
));
3677 return TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3681 Print to standard output stream.
3683 @param format Formatting string.
3684 @param args Arguments.
3685 @return Number of printed characters.
3689 TRIO_ARGS2((format
, args
),
3690 TRIO_CONST
char *format
,
3691 trio_pointer_t
* args
)
3693 assert(VALID(format
));
3695 return TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, NULL
, args
);
3698 /*************************************************************************
3705 @param file File pointer.
3706 @param format Formatting string.
3707 @param ... Arguments.
3708 @return Number of printed characters.
3712 TRIO_VARGS3((file
, format
, va_alist
),
3714 TRIO_CONST
char *format
,
3720 assert(VALID(file
));
3721 assert(VALID(format
));
3723 TRIO_VA_START(args
, format
);
3724 status
= TrioFormat(file
, 0, TrioOutStreamFile
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3732 @param file File pointer.
3733 @param format Formatting string.
3734 @param args Arguments.
3735 @return Number of printed characters.
3739 TRIO_ARGS3((file
, format
, args
),
3741 TRIO_CONST
char *format
,
3744 assert(VALID(file
));
3745 assert(VALID(format
));
3747 return TrioFormat(file
, 0, TrioOutStreamFile
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3753 @param file File pointer.
3754 @param format Formatting string.
3755 @param args Arguments.
3756 @return Number of printed characters.
3760 TRIO_ARGS3((file
, format
, args
),
3762 TRIO_CONST
char *format
,
3763 trio_pointer_t
* args
)
3765 assert(VALID(file
));
3766 assert(VALID(format
));
3768 return TrioFormat(file
, 0, TrioOutStreamFile
, format
, NULL
, args
);
3771 /*************************************************************************
3776 Print to file descriptor.
3778 @param fd File descriptor.
3779 @param format Formatting string.
3780 @param ... Arguments.
3781 @return Number of printed characters.
3785 TRIO_VARGS3((fd
, format
, va_alist
),
3787 TRIO_CONST
char *format
,
3793 assert(VALID(format
));
3795 TRIO_VA_START(args
, format
);
3796 status
= TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3802 Print to file descriptor.
3804 @param fd File descriptor.
3805 @param format Formatting string.
3806 @param args Arguments.
3807 @return Number of printed characters.
3811 TRIO_ARGS3((fd
, format
, args
),
3813 TRIO_CONST
char *format
,
3816 assert(VALID(format
));
3818 return TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3822 Print to file descriptor.
3824 @param fd File descriptor.
3825 @param format Formatting string.
3826 @param args Arguments.
3827 @return Number of printed characters.
3831 TRIO_ARGS3((fd
, format
, args
),
3833 TRIO_CONST
char *format
,
3834 trio_pointer_t
*args
)
3836 assert(VALID(format
));
3838 return TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, NULL
, args
);
3841 /*************************************************************************
3846 TRIO_VARGS4((stream
, closure
, format
, va_alist
),
3847 trio_outstream_t stream
,
3848 trio_pointer_t closure
,
3849 TRIO_CONST
char *format
,
3856 assert(VALID(stream
));
3857 assert(VALID(format
));
3859 TRIO_VA_START(args
, format
);
3860 data
.stream
.out
= stream
;
3861 data
.closure
= closure
;
3862 status
= TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3869 TRIO_ARGS4((stream
, closure
, format
, args
),
3870 trio_outstream_t stream
,
3871 trio_pointer_t closure
,
3872 TRIO_CONST
char *format
,
3877 assert(VALID(stream
));
3878 assert(VALID(format
));
3880 data
.stream
.out
= stream
;
3881 data
.closure
= closure
;
3882 return TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3887 TRIO_ARGS4((stream
, closure
, format
, args
),
3888 trio_outstream_t stream
,
3889 trio_pointer_t closure
,
3890 TRIO_CONST
char *format
,
3895 assert(VALID(stream
));
3896 assert(VALID(format
));
3898 data
.stream
.out
= stream
;
3899 data
.closure
= closure
;
3900 return TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, NULL
, args
);
3903 /*************************************************************************
3910 @param buffer Output string.
3911 @param format Formatting string.
3912 @param ... Arguments.
3913 @return Number of printed characters.
3917 TRIO_VARGS3((buffer
, format
, va_alist
),
3919 TRIO_CONST
char *format
,
3925 assert(VALID(buffer
));
3926 assert(VALID(format
));
3928 TRIO_VA_START(args
, format
);
3929 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3930 *buffer
= NIL
; /* Terminate with NIL character */
3938 @param buffer Output string.
3939 @param format Formatting string.
3940 @param args Arguments.
3941 @return Number of printed characters.
3945 TRIO_ARGS3((buffer
, format
, args
),
3947 TRIO_CONST
char *format
,
3952 assert(VALID(buffer
));
3953 assert(VALID(format
));
3955 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
3963 @param buffer Output string.
3964 @param format Formatting string.
3965 @param args Arguments.
3966 @return Number of printed characters.
3970 TRIO_ARGS3((buffer
, format
, args
),
3972 TRIO_CONST
char *format
,
3973 trio_pointer_t
*args
)
3977 assert(VALID(buffer
));
3978 assert(VALID(format
));
3980 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, NULL
, args
);
3985 /*************************************************************************
3990 Print at most @p max characters to string.
3992 @param buffer Output string.
3993 @param max Maximum number of characters to print.
3994 @param format Formatting string.
3995 @param ... Arguments.
3996 @return Number of printed characters.
4000 TRIO_VARGS4((buffer
, max
, format
, va_alist
),
4003 TRIO_CONST
char *format
,
4009 assert(VALID(buffer
));
4010 assert(VALID(format
));
4012 TRIO_VA_START(args
, format
);
4013 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
4014 TrioOutStreamStringMax
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
4022 Print at most @p max characters to string.
4024 @param buffer Output string.
4025 @param max Maximum number of characters to print.
4026 @param format Formatting string.
4027 @param args Arguments.
4028 @return Number of printed characters.
4032 TRIO_ARGS4((buffer
, max
, format
, args
),
4035 TRIO_CONST
char *format
,
4040 assert(VALID(buffer
));
4041 assert(VALID(format
));
4043 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
4044 TrioOutStreamStringMax
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
4051 Print at most @p max characters to string.
4053 @param buffer Output string.
4054 @param max Maximum number of characters to print.
4055 @param format Formatting string.
4056 @param args Arguments.
4057 @return Number of printed characters.
4061 TRIO_ARGS4((buffer
, max
, format
, args
),
4064 TRIO_CONST
char *format
,
4065 trio_pointer_t
*args
)
4069 assert(VALID(buffer
));
4070 assert(VALID(format
));
4072 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
4073 TrioOutStreamStringMax
, format
, NULL
, args
);
4079 /*************************************************************************
4081 * Appends the new string to the buffer string overwriting the '\0'
4082 * character at the end of buffer.
4086 TRIO_VARGS4((buffer
, max
, format
, va_alist
),
4089 TRIO_CONST
char *format
,
4096 TRIO_VA_START(args
, format
);
4098 assert(VALID(buffer
));
4099 assert(VALID(format
));
4101 buf_len
= trio_length(buffer
);
4102 buffer
= &buffer
[buf_len
];
4104 status
= TrioFormat(&buffer
, max
- 1 - buf_len
,
4105 TrioOutStreamStringMax
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
4113 TRIO_ARGS4((buffer
, max
, format
, args
),
4116 TRIO_CONST
char *format
,
4122 assert(VALID(buffer
));
4123 assert(VALID(format
));
4125 buf_len
= trio_length(buffer
);
4126 buffer
= &buffer
[buf_len
];
4127 status
= TrioFormat(&buffer
, max
- 1 - buf_len
,
4128 TrioOutStreamStringMax
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
4133 /*************************************************************************
4140 TRIO_VARGS2((format
, va_alist
),
4141 TRIO_CONST
char *format
,
4145 trio_string_t
*info
;
4146 char *result
= NULL
;
4148 assert(VALID(format
));
4150 info
= trio_xstring_duplicate("");
4153 TRIO_VA_START(args
, format
);
4154 (void)TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4155 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
4158 trio_string_terminate(info
);
4159 result
= trio_string_extract(info
);
4160 trio_string_destroy(info
);
4168 TRIO_ARGS2((format
, args
),
4169 TRIO_CONST
char *format
,
4172 trio_string_t
*info
;
4173 char *result
= NULL
;
4175 assert(VALID(format
));
4177 info
= trio_xstring_duplicate("");
4180 (void)TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4181 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
4182 trio_string_terminate(info
);
4183 result
= trio_string_extract(info
);
4184 trio_string_destroy(info
);
4191 TRIO_VARGS3((result
, format
, va_alist
),
4193 TRIO_CONST
char *format
,
4198 trio_string_t
*info
;
4200 assert(VALID(format
));
4204 info
= trio_xstring_duplicate("");
4207 status
= TRIO_ERROR_RETURN(TRIO_ENOMEM
, 0);
4211 TRIO_VA_START(args
, format
);
4212 status
= TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4213 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
4217 trio_string_terminate(info
);
4218 *result
= trio_string_extract(info
);
4220 trio_string_destroy(info
);
4227 TRIO_ARGS3((result
, format
, args
),
4229 TRIO_CONST
char *format
,
4233 trio_string_t
*info
;
4235 assert(VALID(format
));
4239 info
= trio_xstring_duplicate("");
4242 status
= TRIO_ERROR_RETURN(TRIO_ENOMEM
, 0);
4246 status
= TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4247 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
4250 trio_string_terminate(info
);
4251 *result
= trio_string_extract(info
);
4253 trio_string_destroy(info
);
4258 /** @} End of Printf documentation module */
4260 /*************************************************************************
4264 ************************************************************************/
4266 #if defined(TRIO_DOCUMENTATION)
4267 # include "doc/doc_register.h"
4270 @addtogroup UserDefined
4276 /*************************************************************************
4281 Register new user-defined specifier.
4287 TRIO_PUBLIC trio_pointer_t
4289 TRIO_ARGS2((callback
, name
),
4290 trio_callback_t callback
,
4291 TRIO_CONST
char *name
)
4293 trio_userdef_t
*def
;
4294 trio_userdef_t
*prev
= NULL
;
4296 if (callback
== NULL
)
4301 /* Handle built-in namespaces */
4304 if (trio_equal(name
, ":enter"))
4306 internalEnterCriticalRegion
= callback
;
4308 else if (trio_equal(name
, ":leave"))
4310 internalLeaveCriticalRegion
= callback
;
4315 /* Bail out if namespace is too long */
4316 if (trio_length(name
) >= MAX_USER_NAME
)
4319 /* Bail out if namespace already is registered */
4320 def
= TrioFindNamespace(name
, &prev
);
4325 def
= (trio_userdef_t
*)TRIO_MALLOC(sizeof(trio_userdef_t
));
4328 if (internalEnterCriticalRegion
)
4329 (void)internalEnterCriticalRegion(NULL
);
4333 /* Link into internal list */
4335 internalUserDef
= def
;
4340 def
->callback
= callback
;
4341 def
->name
= (name
== NULL
)
4343 : trio_duplicate(name
);
4346 if (internalLeaveCriticalRegion
)
4347 (void)internalLeaveCriticalRegion(NULL
);
4349 return (trio_pointer_t
)def
;
4353 Unregister an existing user-defined specifier.
4359 TRIO_ARGS1((handle
),
4360 trio_pointer_t handle
)
4362 trio_userdef_t
*self
= (trio_userdef_t
*)handle
;
4363 trio_userdef_t
*def
;
4364 trio_userdef_t
*prev
= NULL
;
4366 assert(VALID(self
));
4370 def
= TrioFindNamespace(self
->name
, &prev
);
4373 if (internalEnterCriticalRegion
)
4374 (void)internalEnterCriticalRegion(NULL
);
4377 internalUserDef
= NULL
;
4379 prev
->next
= def
->next
;
4381 if (internalLeaveCriticalRegion
)
4382 (void)internalLeaveCriticalRegion(NULL
);
4384 trio_destroy(self
->name
);
4389 /*************************************************************************
4390 * trio_get_format [public]
4397 #if defined(FORMAT_USER_DEFINED)
4398 assert(((trio_reference_t
*)ref
)->parameter
->type
== FORMAT_USER_DEFINED
);
4401 return (((trio_reference_t
*)ref
)->parameter
->user_data
);
4404 /*************************************************************************
4405 * trio_get_argument [public]
4412 #if defined(FORMAT_USER_DEFINED)
4413 assert(((trio_reference_t
*)ref
)->parameter
->type
== FORMAT_USER_DEFINED
);
4416 return ((trio_reference_t
*)ref
)->parameter
->data
.pointer
;
4419 /*************************************************************************
4420 * trio_get_width / trio_set_width [public]
4427 return ((trio_reference_t
*)ref
)->parameter
->width
;
4432 TRIO_ARGS2((ref
, width
),
4436 ((trio_reference_t
*)ref
)->parameter
->width
= width
;
4439 /*************************************************************************
4440 * trio_get_precision / trio_set_precision [public]
4447 return (((trio_reference_t
*)ref
)->parameter
->precision
);
4452 TRIO_ARGS2((ref
, precision
),
4456 ((trio_reference_t
*)ref
)->parameter
->precision
= precision
;
4459 /*************************************************************************
4460 * trio_get_base / trio_set_base [public]
4467 return (((trio_reference_t
*)ref
)->parameter
->base
);
4472 TRIO_ARGS2((ref
, base
),
4476 ((trio_reference_t
*)ref
)->parameter
->base
= base
;
4479 /*************************************************************************
4480 * trio_get_long / trio_set_long [public]
4487 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LONG
)
4494 TRIO_ARGS2((ref
, is_long
),
4499 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LONG
;
4501 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LONG
;
4504 /*************************************************************************
4505 * trio_get_longlong / trio_set_longlong [public]
4512 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_QUAD
)
4519 TRIO_ARGS2((ref
, is_longlong
),
4524 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_QUAD
;
4526 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_QUAD
;
4529 /*************************************************************************
4530 * trio_get_longdouble / trio_set_longdouble [public]
4537 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LONGDOUBLE
)
4544 TRIO_ARGS2((ref
, is_longdouble
),
4549 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LONGDOUBLE
;
4551 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LONGDOUBLE
;
4554 /*************************************************************************
4555 * trio_get_short / trio_set_short [public]
4562 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHORT
)
4569 TRIO_ARGS2((ref
, is_short
),
4574 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHORT
;
4576 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHORT
;
4579 /*************************************************************************
4580 * trio_get_shortshort / trio_set_shortshort [public]
4587 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHORTSHORT
)
4594 TRIO_ARGS2((ref
, is_shortshort
),
4599 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHORTSHORT
;
4601 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHORTSHORT
;
4604 /*************************************************************************
4605 * trio_get_alternative / trio_set_alternative [public]
4608 trio_get_alternative
4612 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_ALTERNATIVE
)
4618 trio_set_alternative
4619 TRIO_ARGS2((ref
, is_alternative
),
4624 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_ALTERNATIVE
;
4626 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_ALTERNATIVE
;
4629 /*************************************************************************
4630 * trio_get_alignment / trio_set_alignment [public]
4637 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LEFTADJUST
)
4644 TRIO_ARGS2((ref
, is_leftaligned
),
4649 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LEFTADJUST
;
4651 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LEFTADJUST
;
4654 /*************************************************************************
4655 * trio_get_spacing /trio_set_spacing [public]
4662 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SPACE
)
4669 TRIO_ARGS2((ref
, is_space
),
4674 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SPACE
;
4676 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SPACE
;
4679 /*************************************************************************
4680 * trio_get_sign / trio_set_sign [public]
4687 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHOWSIGN
)
4694 TRIO_ARGS2((ref
, is_sign
),
4699 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHOWSIGN
;
4701 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHOWSIGN
;
4704 /*************************************************************************
4705 * trio_get_padding / trio_set_padding [public]
4712 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_NILPADDING
)
4719 TRIO_ARGS2((ref
, is_padding
),
4724 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_NILPADDING
;
4726 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_NILPADDING
;
4729 /*************************************************************************
4730 * trio_get_quote / trio_set_quote [public]
4737 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_QUOTE
)
4744 TRIO_ARGS2((ref
, is_quote
),
4749 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_QUOTE
;
4751 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_QUOTE
;
4754 /*************************************************************************
4755 * trio_get_upper / trio_set_upper [public]
4762 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_UPPER
)
4769 TRIO_ARGS2((ref
, is_upper
),
4774 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_UPPER
;
4776 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_UPPER
;
4779 /*************************************************************************
4780 * trio_get_largest / trio_set_largest [public]
4788 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_INTMAX_T
)
4795 TRIO_ARGS2((ref
, is_largest
),
4800 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_INTMAX_T
;
4802 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_INTMAX_T
;
4806 /*************************************************************************
4807 * trio_get_ptrdiff / trio_set_ptrdiff [public]
4814 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_PTRDIFF_T
)
4821 TRIO_ARGS2((ref
, is_ptrdiff
),
4826 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_PTRDIFF_T
;
4828 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_PTRDIFF_T
;
4831 /*************************************************************************
4832 * trio_get_size / trio_set_size [public]
4840 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SIZE_T
)
4847 TRIO_ARGS2((ref
, is_size
),
4852 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SIZE_T
;
4854 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SIZE_T
;
4858 /*************************************************************************
4859 * trio_print_int [public]
4863 TRIO_ARGS2((ref
, number
),
4867 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4869 TrioWriteNumber(self
->data
,
4870 (trio_uintmax_t
)number
,
4871 self
->parameter
->flags
,
4872 self
->parameter
->width
,
4873 self
->parameter
->precision
,
4874 self
->parameter
->base
);
4877 /*************************************************************************
4878 * trio_print_uint [public]
4882 TRIO_ARGS2((ref
, number
),
4884 unsigned int number
)
4886 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4888 TrioWriteNumber(self
->data
,
4889 (trio_uintmax_t
)number
,
4890 self
->parameter
->flags
| FLAGS_UNSIGNED
,
4891 self
->parameter
->width
,
4892 self
->parameter
->precision
,
4893 self
->parameter
->base
);
4896 /*************************************************************************
4897 * trio_print_double [public]
4901 TRIO_ARGS2((ref
, number
),
4905 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4907 TrioWriteDouble(self
->data
,
4909 self
->parameter
->flags
,
4910 self
->parameter
->width
,
4911 self
->parameter
->precision
,
4912 self
->parameter
->base
);
4915 /*************************************************************************
4916 * trio_print_string [public]
4920 TRIO_ARGS2((ref
, string
),
4924 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4926 TrioWriteString(self
->data
,
4928 self
->parameter
->flags
,
4929 self
->parameter
->width
,
4930 self
->parameter
->precision
);
4933 /*************************************************************************
4934 * trio_print_ref [public]
4938 TRIO_VARGS3((ref
, format
, va_alist
),
4940 TRIO_CONST
char *format
,
4946 assert(VALID(format
));
4948 TRIO_VA_START(arglist
, format
);
4949 status
= TrioFormatRef((trio_reference_t
*)ref
, format
, TRIO_VA_LIST_ADDR(arglist
), NULL
);
4950 TRIO_VA_END(arglist
);
4954 /*************************************************************************
4955 * trio_vprint_ref [public]
4959 TRIO_ARGS3((ref
, format
, arglist
),
4961 TRIO_CONST
char *format
,
4964 assert(VALID(format
));
4966 return TrioFormatRef((trio_reference_t
*)ref
, format
, TRIO_VA_LIST_ADDR(arglist
), NULL
);
4969 /*************************************************************************
4970 * trio_printv_ref [public]
4974 TRIO_ARGS3((ref
, format
, argarray
),
4976 TRIO_CONST
char *format
,
4977 trio_pointer_t
*argarray
)
4979 assert(VALID(format
));
4981 return TrioFormatRef((trio_reference_t
*)ref
, format
, NULL
, argarray
);
4984 #endif /* TRIO_EXTENSION */
4986 /*************************************************************************
4987 * trio_print_pointer [public]
4991 TRIO_ARGS2((ref
, pointer
),
4993 trio_pointer_t pointer
)
4995 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4997 trio_uintmax_t number
;
4999 if (NULL
== pointer
)
5001 TRIO_CONST
char *string
= internalNullString
;
5003 self
->data
->OutStream(self
->data
, *string
++);
5008 * The subtraction of the null pointer is a workaround
5009 * to avoid a compiler warning. The performance overhead
5010 * is negligible (and likely to be removed by an
5011 * optimizing compiler). The (char *) casting is done
5012 * to please ANSI C++.
5014 number
= (trio_uintmax_t
)((char *)pointer
- (char *)0);
5015 /* Shrink to size of pointer */
5016 number
&= (trio_uintmax_t
)-1;
5017 flags
= self
->parameter
->flags
;
5018 flags
|= (FLAGS_UNSIGNED
| FLAGS_ALTERNATIVE
|
5020 TrioWriteNumber(self
->data
,
5029 /** @} End of UserDefined documentation module */
5031 /*************************************************************************
5035 ************************************************************************/
5037 /*************************************************************************
5038 * trio_locale_set_decimal_point
5040 * Decimal point can only be one character. The input argument is a
5041 * string to enable multibyte characters. At most MB_LEN_MAX characters
5045 trio_locale_set_decimal_point
5046 TRIO_ARGS1((decimalPoint
),
5049 #if defined(USE_LOCALE)
5050 if (NULL
== internalLocaleValues
)
5055 internalDecimalPointLength
= trio_length(decimalPoint
);
5056 if (internalDecimalPointLength
== 1)
5058 internalDecimalPoint
= *decimalPoint
;
5062 internalDecimalPoint
= NIL
;
5063 trio_copy_max(internalDecimalPointString
,
5064 sizeof(internalDecimalPointString
),
5069 /*************************************************************************
5070 * trio_locale_set_thousand_separator
5072 * See trio_locale_set_decimal_point
5075 trio_locale_set_thousand_separator
5076 TRIO_ARGS1((thousandSeparator
),
5077 char *thousandSeparator
)
5079 #if defined(USE_LOCALE)
5080 if (NULL
== internalLocaleValues
)
5085 trio_copy_max(internalThousandSeparator
,
5086 sizeof(internalThousandSeparator
),
5088 internalThousandSeparatorLength
= trio_length(internalThousandSeparator
);
5091 /*************************************************************************
5092 * trio_locale_set_grouping
5094 * Array of bytes. Reversed order.
5096 * CHAR_MAX : No further grouping
5097 * 0 : Repeat last group for the remaining digits (not necessary
5098 * as C strings are zero-terminated)
5099 * n : Set current group to n
5101 * Same order as the grouping attribute in LC_NUMERIC.
5104 trio_locale_set_grouping
5105 TRIO_ARGS1((grouping
),
5108 #if defined(USE_LOCALE)
5109 if (NULL
== internalLocaleValues
)
5114 trio_copy_max(internalGrouping
,
5115 sizeof(internalGrouping
),
5120 /*************************************************************************
5124 ************************************************************************/
5126 /*************************************************************************
5127 * TrioSkipWhitespaces
5139 self
->InStream(self
, &ch
);
5144 /*************************************************************************
5149 TrioGetCollation(TRIO_NOARGS
)
5157 /* This is computationally expensive */
5160 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5164 for (j
= 0; j
< MAX_CHARACTER_CLASS
; j
++)
5166 second
[0] = (char)j
;
5167 if (trio_equal_locale(first
, second
))
5168 internalCollationArray
[i
][k
++] = (char)j
;
5170 internalCollationArray
[i
][k
] = NIL
;
5175 /*************************************************************************
5176 * TrioGetCharacterClass
5182 TrioGetCharacterClass
5183 TRIO_ARGS4((format
, indexPointer
, flagsPointer
, characterclass
),
5184 TRIO_CONST
char *format
,
5186 trio_flags_t
*flagsPointer
,
5187 int *characterclass
)
5189 int index
= *indexPointer
;
5195 *flagsPointer
&= ~FLAGS_EXCLUDE
;
5197 if (format
[index
] == QUALIFIER_CIRCUMFLEX
)
5199 *flagsPointer
|= FLAGS_EXCLUDE
;
5203 * If the ungroup character is at the beginning of the scanlist,
5204 * it will be part of the class, and a second ungroup character
5205 * must follow to end the group.
5207 if (format
[index
] == SPECIFIER_UNGROUP
)
5209 characterclass
[(int)SPECIFIER_UNGROUP
]++;
5213 * Minus is used to specify ranges. To include minus in the class,
5214 * it must be at the beginning of the list
5216 if (format
[index
] == QUALIFIER_MINUS
)
5218 characterclass
[(int)QUALIFIER_MINUS
]++;
5221 /* Collect characters */
5222 for (ch
= format
[index
];
5223 (ch
!= SPECIFIER_UNGROUP
) && (ch
!= NIL
);
5224 ch
= format
[++index
])
5228 case QUALIFIER_MINUS
: /* Scanlist ranges */
5231 * Both C99 and UNIX98 describes ranges as implementation-
5234 * We support the following behaviour (although this may
5235 * change as we become wiser)
5236 * - only increasing ranges, ie. [a-b] but not [b-a]
5237 * - transitive ranges, ie. [a-b-c] == [a-c]
5238 * - trailing minus, ie. [a-] is interpreted as an 'a'
5240 * - duplicates (although we can easily convert these
5243 range_begin
= format
[index
- 1];
5244 range_end
= format
[++index
];
5245 if (range_end
== SPECIFIER_UNGROUP
)
5247 /* Trailing minus is included */
5248 characterclass
[(int)ch
]++;
5252 if (range_end
== NIL
)
5253 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
5254 if (range_begin
> range_end
)
5255 return TRIO_ERROR_RETURN(TRIO_ERANGE
, index
);
5257 for (i
= (int)range_begin
; i
<= (int)range_end
; i
++)
5258 characterclass
[i
]++;
5265 case SPECIFIER_GROUP
:
5267 switch (format
[index
+ 1])
5269 case QUALIFIER_DOT
: /* Collating symbol */
5271 * FIXME: This will be easier to implement when multibyte
5272 * characters have been implemented. Until now, we ignore
5275 for (i
= index
+ 2; ; i
++)
5277 if (format
[i
] == NIL
)
5278 /* Error in syntax */
5280 else if (format
[i
] == QUALIFIER_DOT
)
5283 if (format
[++i
] != SPECIFIER_UNGROUP
)
5289 case QUALIFIER_EQUAL
: /* Equivalence class expressions */
5294 if (internalCollationUnconverted
)
5296 /* Lazy evaluation of collation array */
5298 internalCollationUnconverted
= FALSE
;
5300 for (i
= index
+ 2; ; i
++)
5302 if (format
[i
] == NIL
)
5303 /* Error in syntax */
5305 else if (format
[i
] == QUALIFIER_EQUAL
)
5309 /* Mark any equivalent character */
5310 k
= (unsigned int)format
[i
];
5311 for (j
= 0; internalCollationArray
[k
][j
] != NIL
; j
++)
5312 characterclass
[(int)internalCollationArray
[k
][j
]]++;
5315 if (format
[++i
] != SPECIFIER_UNGROUP
)
5322 case QUALIFIER_COLON
: /* Character class expressions */
5324 if (trio_equal_max(CLASS_ALNUM
, sizeof(CLASS_ALNUM
) - 1,
5327 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5329 characterclass
[i
]++;
5330 index
+= sizeof(CLASS_ALNUM
) - 1;
5332 else if (trio_equal_max(CLASS_ALPHA
, sizeof(CLASS_ALPHA
) - 1,
5335 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5337 characterclass
[i
]++;
5338 index
+= sizeof(CLASS_ALPHA
) - 1;
5340 else if (trio_equal_max(CLASS_CNTRL
, sizeof(CLASS_CNTRL
) - 1,
5343 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5345 characterclass
[i
]++;
5346 index
+= sizeof(CLASS_CNTRL
) - 1;
5348 else if (trio_equal_max(CLASS_DIGIT
, sizeof(CLASS_DIGIT
) - 1,
5351 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5353 characterclass
[i
]++;
5354 index
+= sizeof(CLASS_DIGIT
) - 1;
5356 else if (trio_equal_max(CLASS_GRAPH
, sizeof(CLASS_GRAPH
) - 1,
5359 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5361 characterclass
[i
]++;
5362 index
+= sizeof(CLASS_GRAPH
) - 1;
5364 else if (trio_equal_max(CLASS_LOWER
, sizeof(CLASS_LOWER
) - 1,
5367 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5369 characterclass
[i
]++;
5370 index
+= sizeof(CLASS_LOWER
) - 1;
5372 else if (trio_equal_max(CLASS_PRINT
, sizeof(CLASS_PRINT
) - 1,
5375 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5377 characterclass
[i
]++;
5378 index
+= sizeof(CLASS_PRINT
) - 1;
5380 else if (trio_equal_max(CLASS_PUNCT
, sizeof(CLASS_PUNCT
) - 1,
5383 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5385 characterclass
[i
]++;
5386 index
+= sizeof(CLASS_PUNCT
) - 1;
5388 else if (trio_equal_max(CLASS_SPACE
, sizeof(CLASS_SPACE
) - 1,
5391 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5393 characterclass
[i
]++;
5394 index
+= sizeof(CLASS_SPACE
) - 1;
5396 else if (trio_equal_max(CLASS_UPPER
, sizeof(CLASS_UPPER
) - 1,
5399 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5401 characterclass
[i
]++;
5402 index
+= sizeof(CLASS_UPPER
) - 1;
5404 else if (trio_equal_max(CLASS_XDIGIT
, sizeof(CLASS_XDIGIT
) - 1,
5407 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5409 characterclass
[i
]++;
5410 index
+= sizeof(CLASS_XDIGIT
) - 1;
5414 characterclass
[(int)ch
]++;
5419 characterclass
[(int)ch
]++;
5424 #endif /* TRIO_EXTENSION */
5427 characterclass
[(int)ch
]++;
5434 /*************************************************************************
5437 * We implement our own number conversion in preference of strtol and
5438 * strtoul, because we must handle 'long long' and thousand separators.
5440 TRIO_PRIVATE BOOLEAN_T
5442 TRIO_ARGS5((self
, target
, flags
, width
, base
),
5444 trio_uintmax_t
*target
,
5449 trio_uintmax_t number
= 0;
5452 BOOLEAN_T isNegative
= FALSE
;
5453 BOOLEAN_T gotNumber
= FALSE
;
5456 assert(VALID(self
));
5457 assert(VALID(self
->InStream
));
5458 assert((base
>= MIN_BASE
&& base
<= MAX_BASE
) || (base
== NO_BASE
));
5460 if (internalDigitsUnconverted
)
5462 /* Lazy evaluation of digits array */
5463 memset(internalDigitArray
, -1, sizeof(internalDigitArray
));
5464 for (j
= 0; j
< (int)sizeof(internalDigitsLower
) - 1; j
++)
5466 internalDigitArray
[(int)internalDigitsLower
[j
]] = j
;
5467 internalDigitArray
[(int)internalDigitsUpper
[j
]] = j
;
5469 internalDigitsUnconverted
= FALSE
;
5472 TrioSkipWhitespaces(self
);
5474 if (!(flags
& FLAGS_UNSIGNED
))
5477 if (self
->current
== '+')
5479 self
->InStream(self
, NULL
);
5481 else if (self
->current
== '-')
5483 self
->InStream(self
, NULL
);
5488 count
= self
->processed
;
5490 if (flags
& FLAGS_ALTERNATIVE
)
5498 if (self
->current
== '0')
5500 self
->InStream(self
, NULL
);
5503 if ((base
== BASE_HEX
) &&
5504 (trio_to_upper(self
->current
) == 'X'))
5506 self
->InStream(self
, NULL
);
5508 else if ((base
== BASE_BINARY
) &&
5509 (trio_to_upper(self
->current
) == 'B'))
5511 self
->InStream(self
, NULL
);
5523 while (((width
== NO_WIDTH
) || (self
->processed
- count
< width
)) &&
5524 (! ((self
->current
== EOF
) || isspace(self
->current
))))
5526 if (isascii(self
->current
))
5528 digit
= internalDigitArray
[self
->current
];
5529 /* Abort if digit is not allowed in the specified base */
5530 if ((digit
== -1) || (digit
>= base
))
5533 else if (flags
& FLAGS_QUOTE
)
5535 /* Compare with thousands separator */
5536 for (j
= 0; internalThousandSeparator
[j
] && self
->current
; j
++)
5538 if (internalThousandSeparator
[j
] != self
->current
)
5541 self
->InStream(self
, NULL
);
5543 if (internalThousandSeparator
[j
])
5544 break; /* Mismatch */
5546 continue; /* Match */
5553 gotNumber
= TRUE
; /* we need at least one digit */
5555 self
->InStream(self
, NULL
);
5558 /* Was anything read at all? */
5563 *target
= (isNegative
) ? -((trio_intmax_t
)number
) : number
;
5567 /*************************************************************************
5572 TRIO_ARGS4((self
, target
, flags
, width
),
5580 trio_uintmax_t number
;
5582 assert(VALID(self
));
5583 assert(VALID(self
->InStream
));
5586 (self
->current
!= EOF
) && (i
< width
);
5589 ch
= (char)self
->current
;
5590 self
->InStream(self
, NULL
);
5591 if ((flags
& FLAGS_ALTERNATIVE
) && (ch
== CHAR_BACKSLASH
))
5593 switch (self
->current
)
5595 case '\\': ch
= '\\'; break;
5596 case 'a': ch
= '\007'; break;
5597 case 'b': ch
= '\b'; break;
5598 case 'f': ch
= '\f'; break;
5599 case 'n': ch
= '\n'; break;
5600 case 'r': ch
= '\r'; break;
5601 case 't': ch
= '\t'; break;
5602 case 'v': ch
= '\v'; break;
5604 if (isdigit(self
->current
))
5606 /* Read octal number */
5607 if (!TrioReadNumber(self
, &number
, 0, 3, BASE_OCTAL
))
5611 else if (trio_to_upper(self
->current
) == 'X')
5613 /* Read hexadecimal number */
5614 self
->InStream(self
, NULL
);
5615 if (!TrioReadNumber(self
, &number
, 0, 2, BASE_HEX
))
5621 ch
= (char)self
->current
;
5633 /*************************************************************************
5636 TRIO_PRIVATE BOOLEAN_T
5638 TRIO_ARGS4((self
, target
, flags
, width
),
5646 assert(VALID(self
));
5647 assert(VALID(self
->InStream
));
5649 TrioSkipWhitespaces(self
);
5652 * Continue until end of string is reached, a whitespace is encountered,
5653 * or width is exceeded
5656 ((width
== NO_WIDTH
) || (i
< width
)) &&
5657 (! ((self
->current
== EOF
) || isspace(self
->current
)));
5660 if (TrioReadChar(self
, (target
? &target
[i
] : 0), flags
, 1) == 0)
5668 /*************************************************************************
5674 TRIO_ARGS4((self
, target
, flags
, width
),
5676 trio_wchar_t
*target
,
5685 char buffer
[MB_LEN_MAX
+ 1];
5687 assert(VALID(self
));
5688 assert(VALID(self
->InStream
));
5691 (self
->current
!= EOF
) && (i
< width
);
5694 if (isascii(self
->current
))
5696 if (TrioReadChar(self
, buffer
, flags
, 1) == 0)
5703 * Collect a multibyte character, by enlarging buffer until
5704 * it contains a fully legal multibyte character, or the
5710 buffer
[j
++] = (char)self
->current
;
5712 self
->InStream(self
, NULL
);
5714 while ((j
< (int)sizeof(buffer
)) && (mblen(buffer
, (size_t)j
) != j
));
5718 size
= mbtowc(&wch
, buffer
, sizeof(buffer
));
5723 self
->InStream(self
, NULL
);
5727 #endif /* TRIO_WIDECHAR */
5729 /*************************************************************************
5730 * TrioReadWideString
5733 TRIO_PRIVATE BOOLEAN_T
5735 TRIO_ARGS4((self
, target
, flags
, width
),
5737 trio_wchar_t
*target
,
5744 assert(VALID(self
));
5745 assert(VALID(self
->InStream
));
5747 TrioSkipWhitespaces(self
);
5749 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5750 (void)mblen(NULL
, 0);
5754 * Continue until end of string is reached, a whitespace is encountered,
5755 * or width is exceeded
5758 ((width
== NO_WIDTH
) || (i
< width
)) &&
5759 (! ((self
->current
== EOF
) || isspace(self
->current
)));
5762 size
= TrioReadWideChar(self
, &target
[i
], flags
, 1);
5769 target
[i
] = WCONST('\0');
5772 #endif /* TRIO_WIDECHAR */
5774 /*************************************************************************
5777 * FIXME: characterclass does not work with multibyte characters
5779 TRIO_PRIVATE BOOLEAN_T
5781 TRIO_ARGS5((self
, target
, characterclass
, flags
, width
),
5784 int *characterclass
,
5791 assert(VALID(self
));
5792 assert(VALID(self
->InStream
));
5796 ((width
== NO_WIDTH
) || (i
< width
)) &&
5798 (((flags
& FLAGS_EXCLUDE
) != 0) ^ (characterclass
[ch
] == 0))));
5802 target
[i
] = (char)ch
;
5803 self
->InStream(self
, &ch
);
5811 /*************************************************************************
5818 TRIO_PRIVATE BOOLEAN_T
5820 TRIO_ARGS4((self
, target
, flags
, width
),
5822 trio_pointer_t target
,
5827 char doubleString
[512];
5831 BOOLEAN_T isHex
= FALSE
;
5833 doubleString
[0] = 0;
5835 if ((width
== NO_WIDTH
) || (width
> (int)sizeof(doubleString
) - 1))
5836 width
= sizeof(doubleString
) - 1;
5838 TrioSkipWhitespaces(self
);
5841 * Read entire double number from stream. trio_to_double requires
5842 * a string as input, but InStream can be anything, so we have to
5843 * collect all characters.
5846 if ((ch
== '+') || (ch
== '-'))
5848 doubleString
[index
++] = (char)ch
;
5849 self
->InStream(self
, &ch
);
5865 while (isalpha(ch
) && (index
- start
< width
))
5867 doubleString
[index
++] = (char)ch
;
5868 self
->InStream(self
, &ch
);
5870 doubleString
[index
] = NIL
;
5872 /* Case insensitive string comparison */
5873 if (trio_equal(&doubleString
[start
], INFINITE_UPPER
) ||
5874 trio_equal(&doubleString
[start
], LONG_INFINITE_UPPER
))
5876 if (flags
& FLAGS_LONGDOUBLE
)
5878 if ((start
== 1) && (doubleString
[0] == '-'))
5880 *((trio_long_double_t
*)target
) = trio_ninf();
5884 *((trio_long_double_t
*)target
) = trio_pinf();
5889 if ((start
== 1) && (doubleString
[0] == '-'))
5891 *((double *)target
) = trio_ninf();
5895 *((double *)target
) = trio_pinf();
5900 if (trio_equal(doubleString
, NAN_UPPER
))
5902 /* NaN must not have a preceeding + nor - */
5903 if (flags
& FLAGS_LONGDOUBLE
)
5905 *((trio_long_double_t
*)target
) = trio_nan();
5909 *((double *)target
) = trio_nan();
5916 doubleString
[index
++] = (char)ch
;
5917 self
->InStream(self
, &ch
);
5918 if (trio_to_upper(ch
) == 'X')
5921 doubleString
[index
++] = (char)ch
;
5922 self
->InStream(self
, &ch
);
5930 while ((ch
!= EOF
) && (index
- start
< width
))
5933 if (isHex
? isxdigit(ch
) : isdigit(ch
))
5935 doubleString
[index
++] = (char)ch
;
5936 self
->InStream(self
, &ch
);
5938 else if (flags
& FLAGS_QUOTE
)
5940 /* Compare with thousands separator */
5941 for (j
= 0; internalThousandSeparator
[j
] && self
->current
; j
++)
5943 if (internalThousandSeparator
[j
] != self
->current
)
5946 self
->InStream(self
, &ch
);
5948 if (internalThousandSeparator
[j
])
5949 break; /* Mismatch */
5951 continue; /* Match */
5959 doubleString
[index
++] = (char)ch
;
5960 self
->InStream(self
, &ch
);
5961 while ((isHex
? isxdigit(ch
) : isdigit(ch
)) &&
5962 (index
- start
< width
))
5964 doubleString
[index
++] = (char)ch
;
5965 self
->InStream(self
, &ch
);
5967 if (isHex
? (trio_to_upper(ch
) == 'P') : (trio_to_upper(ch
) == 'E'))
5970 doubleString
[index
++] = (char)ch
;
5971 self
->InStream(self
, &ch
);
5972 if ((ch
== '+') || (ch
== '-'))
5974 doubleString
[index
++] = (char)ch
;
5975 self
->InStream(self
, &ch
);
5977 while (isdigit(ch
) && (index
- start
< width
))
5979 doubleString
[index
++] = (char)ch
;
5980 self
->InStream(self
, &ch
);
5985 if ((index
== start
) || (*doubleString
== NIL
))
5988 doubleString
[index
] = 0;
5990 if (flags
& FLAGS_LONGDOUBLE
)
5992 *((trio_long_double_t
*)target
) = trio_to_long_double(doubleString
, NULL
);
5996 *((double *)target
) = trio_to_double(doubleString
, NULL
);
6001 /*************************************************************************
6004 TRIO_PRIVATE BOOLEAN_T
6006 TRIO_ARGS3((self
, target
, flags
),
6008 trio_pointer_t
*target
,
6011 trio_uintmax_t number
;
6012 char buffer
[sizeof(internalNullString
)];
6014 flags
|= (FLAGS_UNSIGNED
| FLAGS_ALTERNATIVE
| FLAGS_NILPADDING
);
6016 if (TrioReadNumber(self
,
6023 * The strange assignment of number is a workaround for a compiler
6027 *target
= (char *)0 + number
;
6030 else if (TrioReadString(self
,
6031 (flags
& FLAGS_IGNORE
)
6035 sizeof(internalNullString
) - 1))
6037 if (trio_equal_case(buffer
, internalNullString
))
6047 /*************************************************************************
6052 TRIO_ARGS3((data
, format
, parameters
),
6054 TRIO_CONST
char *format
,
6055 trio_parameter_t
*parameters
)
6057 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6063 int index
; /* Index of format string */
6064 int i
; /* Index of current parameter */
6068 trio_pointer_t pointer
;
6073 data
->InStream(data
, &ch
);
6075 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6076 (void)mblen(NULL
, 0);
6079 while (format
[index
])
6081 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6082 if (! isascii(format
[index
]))
6084 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
6087 /* Compare multibyte characters in format string */
6088 for (cnt
= 0; cnt
< charlen
- 1; cnt
++)
6090 if (ch
!= format
[index
+ cnt
])
6092 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
6094 data
->InStream(data
, &ch
);
6096 continue; /* while characters left in formatting string */
6099 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
6101 if ((EOF
== ch
) && (parameters
[i
].type
!= FORMAT_COUNT
))
6103 return (assignment
> 0) ? assignment
: EOF
;
6106 if (CHAR_IDENTIFIER
== format
[index
])
6108 if (CHAR_IDENTIFIER
== format
[index
+ 1])
6110 /* Two % in format matches one % in input stream */
6111 if (CHAR_IDENTIFIER
== ch
)
6113 data
->InStream(data
, &ch
);
6115 continue; /* while format chars left */
6118 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
6121 /* Skip the parameter entries */
6122 while (parameters
[i
].type
== FORMAT_PARAMETER
)
6125 flags
= parameters
[i
].flags
;
6127 width
= parameters
[i
].width
;
6128 if (flags
& FLAGS_WIDTH_PARAMETER
)
6130 /* Get width from parameter list */
6131 width
= (int)parameters
[width
].data
.number
.as_signed
;
6134 base
= parameters
[i
].base
;
6135 if (flags
& FLAGS_BASE_PARAMETER
)
6137 /* Get base from parameter list */
6138 base
= (int)parameters
[base
].data
.number
.as_signed
;
6141 switch (parameters
[i
].type
)
6145 trio_uintmax_t number
;
6148 base
= BASE_DECIMAL
;
6150 if (!TrioReadNumber(data
,
6157 if (!(flags
& FLAGS_IGNORE
))
6161 pointer
= parameters
[i
].data
.pointer
;
6162 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6163 if (flags
& FLAGS_SIZE_T
)
6164 *(size_t *)pointer
= (size_t)number
;
6167 #if defined(QUALIFIER_PTRDIFF_T)
6168 if (flags
& FLAGS_PTRDIFF_T
)
6169 *(ptrdiff_t *)pointer
= (ptrdiff_t)number
;
6172 #if defined(QUALIFIER_INTMAX_T)
6173 if (flags
& FLAGS_INTMAX_T
)
6174 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)number
;
6177 if (flags
& FLAGS_QUAD
)
6178 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)number
;
6179 else if (flags
& FLAGS_LONG
)
6180 *(long int *)pointer
= (long int)number
;
6181 else if (flags
& FLAGS_SHORT
)
6182 *(short int *)pointer
= (short int)number
;
6184 *(int *)pointer
= (int)number
;
6187 break; /* FORMAT_INT */
6191 if (flags
& FLAGS_WIDECHAR
)
6193 if (!TrioReadWideString(data
,
6194 (flags
& FLAGS_IGNORE
)
6196 : parameters
[i
].data
.wstring
,
6204 if (!TrioReadString(data
,
6205 (flags
& FLAGS_IGNORE
)
6207 : parameters
[i
].data
.string
,
6212 if (!(flags
& FLAGS_IGNORE
))
6214 break; /* FORMAT_STRING */
6218 trio_pointer_t pointer
;
6220 if (flags
& FLAGS_IGNORE
)
6226 pointer
= (flags
& FLAGS_LONGDOUBLE
)
6227 ? (trio_pointer_t
)parameters
[i
].data
.longdoublePointer
6228 : (trio_pointer_t
)parameters
[i
].data
.doublePointer
;
6230 if (!TrioReadDouble(data
, pointer
, flags
, width
))
6234 if (!(flags
& FLAGS_IGNORE
))
6238 break; /* FORMAT_DOUBLE */
6242 int characterclass
[MAX_CHARACTER_CLASS
+ 1];
6245 /* Skip over modifiers */
6246 while (format
[index
] != SPECIFIER_GROUP
)
6250 /* Skip over group specifier */
6253 memset(characterclass
, 0, sizeof(characterclass
));
6254 rc
= TrioGetCharacterClass(format
,
6261 if (!TrioReadGroup(data
,
6262 (flags
& FLAGS_IGNORE
)
6264 : parameters
[i
].data
.string
,
6267 parameters
[i
].width
))
6269 if (!(flags
& FLAGS_IGNORE
))
6272 break; /* FORMAT_GROUP */
6275 pointer
= parameters
[i
].data
.pointer
;
6276 if (NULL
!= pointer
)
6278 int count
= data
->committed
;
6280 count
--; /* a character is read, but is not consumed yet */
6281 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6282 if (flags
& FLAGS_SIZE_T
)
6283 *(size_t *)pointer
= (size_t)count
;
6286 #if defined(QUALIFIER_PTRDIFF_T)
6287 if (flags
& FLAGS_PTRDIFF_T
)
6288 *(ptrdiff_t *)pointer
= (ptrdiff_t)count
;
6291 #if defined(QUALIFIER_INTMAX_T)
6292 if (flags
& FLAGS_INTMAX_T
)
6293 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)count
;
6296 if (flags
& FLAGS_QUAD
)
6298 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)count
;
6300 else if (flags
& FLAGS_LONG
)
6302 *(long int *)pointer
= (long int)count
;
6304 else if (flags
& FLAGS_SHORT
)
6306 *(short int *)pointer
= (short int)count
;
6310 *(int *)pointer
= (int)count
;
6313 break; /* FORMAT_COUNT */
6317 if (flags
& FLAGS_WIDECHAR
)
6319 if (TrioReadWideChar(data
,
6320 (flags
& FLAGS_IGNORE
)
6322 : parameters
[i
].data
.wstring
,
6324 (width
== NO_WIDTH
) ? 1 : width
) == 0)
6330 if (TrioReadChar(data
,
6331 (flags
& FLAGS_IGNORE
)
6333 : parameters
[i
].data
.string
,
6335 (width
== NO_WIDTH
) ? 1 : width
) == 0)
6338 if (!(flags
& FLAGS_IGNORE
))
6340 break; /* FORMAT_CHAR */
6342 case FORMAT_POINTER
:
6343 if (!TrioReadPointer(data
,
6344 (flags
& FLAGS_IGNORE
)
6346 : (trio_pointer_t
*)parameters
[i
].data
.pointer
,
6349 if (!(flags
& FLAGS_IGNORE
))
6351 break; /* FORMAT_POINTER */
6353 case FORMAT_PARAMETER
:
6354 break; /* FORMAT_PARAMETER */
6357 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
6360 index
= parameters
[i
].indexAfterSpecifier
;
6363 else /* Not an % identifier */
6365 if (isspace((int)format
[index
]))
6367 /* Whitespaces may match any amount of whitespaces */
6368 ch
= TrioSkipWhitespaces(data
);
6370 else if (ch
== format
[index
])
6372 data
->InStream(data
, &ch
);
6383 /*************************************************************************
6388 TRIO_ARGS6((source
, sourceSize
, InStream
, format
, arglist
, argarray
),
6389 trio_pointer_t source
,
6391 void (*InStream
) TRIO_PROTO((trio_class_t
*, int *)),
6392 TRIO_CONST
char *format
,
6393 TRIO_VA_LIST_PTR arglist
,
6394 trio_pointer_t
*argarray
)
6397 trio_parameter_t parameters
[MAX_PARAMETERS
];
6400 assert(VALID(InStream
));
6401 assert(VALID(format
));
6403 memset(&data
, 0, sizeof(data
));
6404 data
.InStream
= InStream
;
6405 data
.location
= (trio_pointer_t
)source
;
6406 data
.max
= sourceSize
;
6409 #if defined(USE_LOCALE)
6410 if (NULL
== internalLocaleValues
)
6416 status
= TrioParse(TYPE_SCAN
, format
, parameters
, arglist
, argarray
);
6420 status
= TrioScanProcess(&data
, format
, parameters
);
6421 if (data
.error
!= 0)
6423 status
= data
.error
;
6428 /*************************************************************************
6433 TRIO_ARGS2((self
, intPointer
),
6439 assert(VALID(self
));
6440 assert(VALID(self
->location
));
6441 assert(VALID(file
));
6443 file
= (FILE *)self
->location
;
6445 self
->current
= fgetc(file
);
6446 if (self
->current
== EOF
)
6448 self
->error
= (ferror(file
))
6449 ? TRIO_ERROR_RETURN(TRIO_ERRNO
, 0)
6450 : TRIO_ERROR_RETURN(TRIO_EOF
, 0);
6458 if (VALID(intPointer
))
6460 *intPointer
= self
->current
;
6464 /*************************************************************************
6465 * TrioInStreamFileDescriptor
6468 TrioInStreamFileDescriptor
6469 TRIO_ARGS2((self
, intPointer
),
6475 unsigned char input
;
6477 assert(VALID(self
));
6478 assert(VALID(self
->location
));
6480 fd
= *((int *)self
->location
);
6482 size
= read(fd
, &input
, sizeof(char));
6485 self
->error
= TRIO_ERROR_RETURN(TRIO_ERRNO
, 0);
6486 self
->current
= EOF
;
6490 self
->current
= (size
== 0) ? EOF
: input
;
6492 if (self
->current
!= EOF
)
6498 if (VALID(intPointer
))
6500 *intPointer
= self
->current
;
6504 /*************************************************************************
6505 * TrioInStreamCustom
6509 TRIO_ARGS2((self
, intPointer
),
6513 trio_custom_t
*data
;
6515 assert(VALID(self
));
6516 assert(VALID(self
->location
));
6518 data
= (trio_custom_t
*)self
->location
;
6520 self
->current
= (data
->stream
.in
== NULL
)
6522 : (data
->stream
.in
)(data
->closure
);
6524 if (self
->current
== NIL
)
6526 self
->current
= EOF
;
6534 if (VALID(intPointer
))
6536 *intPointer
= self
->current
;
6540 /*************************************************************************
6541 * TrioInStreamString
6545 TRIO_ARGS2((self
, intPointer
),
6549 unsigned char **buffer
;
6551 assert(VALID(self
));
6552 assert(VALID(self
->location
));
6554 buffer
= (unsigned char **)self
->location
;
6555 self
->current
= (*buffer
)[0];
6556 if (self
->current
== NIL
)
6558 self
->current
= EOF
;
6567 if (VALID(intPointer
))
6569 *intPointer
= self
->current
;
6573 /*************************************************************************
6575 * Formatted scanning functions
6577 ************************************************************************/
6579 #if defined(TRIO_DOCUMENTATION)
6580 # include "doc/doc_scanf.h"
6582 /** @addtogroup Scanf
6586 /*************************************************************************
6591 Scan characters from standard input stream.
6593 @param format Formatting string.
6594 @param ... Arguments.
6595 @return Number of scanned characters.
6599 TRIO_VARGS2((format
, va_alist
),
6600 TRIO_CONST
char *format
,
6606 assert(VALID(format
));
6608 TRIO_VA_START(args
, format
);
6609 status
= TrioScan((trio_pointer_t
)stdin
, 0,
6611 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6618 TRIO_ARGS2((format
, args
),
6619 TRIO_CONST
char *format
,
6622 assert(VALID(format
));
6624 return TrioScan((trio_pointer_t
)stdin
, 0,
6626 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6631 TRIO_ARGS2((format
, args
),
6632 TRIO_CONST
char *format
,
6633 trio_pointer_t
*args
)
6635 assert(VALID(format
));
6637 return TrioScan((trio_pointer_t
)stdin
, 0,
6639 format
, NULL
, args
);
6642 /*************************************************************************
6647 TRIO_VARGS3((file
, format
, va_alist
),
6649 TRIO_CONST
char *format
,
6655 assert(VALID(file
));
6656 assert(VALID(format
));
6658 TRIO_VA_START(args
, format
);
6659 status
= TrioScan((trio_pointer_t
)file
, 0,
6661 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6668 TRIO_ARGS3((file
, format
, args
),
6670 TRIO_CONST
char *format
,
6673 assert(VALID(file
));
6674 assert(VALID(format
));
6676 return TrioScan((trio_pointer_t
)file
, 0,
6678 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6683 TRIO_ARGS3((file
, format
, args
),
6685 TRIO_CONST
char *format
,
6686 trio_pointer_t
*args
)
6688 assert(VALID(file
));
6689 assert(VALID(format
));
6691 return TrioScan((trio_pointer_t
)file
, 0,
6693 format
, NULL
, args
);
6696 /*************************************************************************
6701 TRIO_VARGS3((fd
, format
, va_alist
),
6703 TRIO_CONST
char *format
,
6709 assert(VALID(format
));
6711 TRIO_VA_START(args
, format
);
6712 status
= TrioScan((trio_pointer_t
)&fd
, 0,
6713 TrioInStreamFileDescriptor
,
6714 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6721 TRIO_ARGS3((fd
, format
, args
),
6723 TRIO_CONST
char *format
,
6726 assert(VALID(format
));
6728 return TrioScan((trio_pointer_t
)&fd
, 0,
6729 TrioInStreamFileDescriptor
,
6730 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6735 TRIO_ARGS3((fd
, format
, args
),
6737 TRIO_CONST
char *format
,
6738 trio_pointer_t
*args
)
6740 assert(VALID(format
));
6742 return TrioScan((trio_pointer_t
)&fd
, 0,
6743 TrioInStreamFileDescriptor
,
6744 format
, NULL
, args
);
6747 /*************************************************************************
6752 TRIO_VARGS4((stream
, closure
, format
, va_alist
),
6753 trio_instream_t stream
,
6754 trio_pointer_t closure
,
6755 TRIO_CONST
char *format
,
6762 assert(VALID(stream
));
6763 assert(VALID(format
));
6765 TRIO_VA_START(args
, format
);
6766 data
.stream
.in
= stream
;
6767 data
.closure
= closure
;
6768 status
= TrioScan(&data
, 0, TrioInStreamCustom
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6775 TRIO_ARGS4((stream
, closure
, format
, args
),
6776 trio_instream_t stream
,
6777 trio_pointer_t closure
,
6778 TRIO_CONST
char *format
,
6783 assert(VALID(stream
));
6784 assert(VALID(format
));
6786 data
.stream
.in
= stream
;
6787 data
.closure
= closure
;
6788 return TrioScan(&data
, 0, TrioInStreamCustom
, format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6793 TRIO_ARGS4((stream
, closure
, format
, args
),
6794 trio_instream_t stream
,
6795 trio_pointer_t closure
,
6796 TRIO_CONST
char *format
,
6797 trio_pointer_t
*args
)
6801 assert(VALID(stream
));
6802 assert(VALID(format
));
6804 data
.stream
.in
= stream
;
6805 data
.closure
= closure
;
6806 return TrioScan(&data
, 0, TrioInStreamCustom
, format
, NULL
, args
);
6809 /*************************************************************************
6814 TRIO_VARGS3((buffer
, format
, va_alist
),
6815 TRIO_CONST
char *buffer
,
6816 TRIO_CONST
char *format
,
6822 assert(VALID(buffer
));
6823 assert(VALID(format
));
6825 TRIO_VA_START(args
, format
);
6826 status
= TrioScan((trio_pointer_t
)&buffer
, 0,
6828 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6835 TRIO_ARGS3((buffer
, format
, args
),
6836 TRIO_CONST
char *buffer
,
6837 TRIO_CONST
char *format
,
6840 assert(VALID(buffer
));
6841 assert(VALID(format
));
6843 return TrioScan((trio_pointer_t
)&buffer
, 0,
6845 format
, TRIO_VA_LIST_ADDR(args
), NULL
);
6850 TRIO_ARGS3((buffer
, format
, args
),
6851 TRIO_CONST
char *buffer
,
6852 TRIO_CONST
char *format
,
6853 trio_pointer_t
*args
)
6855 assert(VALID(buffer
));
6856 assert(VALID(format
));
6858 return TrioScan((trio_pointer_t
)&buffer
, 0,
6860 format
, NULL
, args
);
6863 /** @} End of Scanf documentation module */
6865 /*************************************************************************
6868 TRIO_PUBLIC TRIO_CONST
char *
6870 TRIO_ARGS1((errorcode
),
6873 /* Textual versions of the error codes */
6874 switch (TRIO_ERROR_CODE(errorcode
))
6877 return "End of file";
6879 return "Invalid argument";
6881 return "Too many arguments";
6883 return "Double reference";
6885 return "Reference gap";
6887 return "Out of memory";
6889 return "Invalid range";
6891 return "Custom error";