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)
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 typedef unsigned long trio_flags_t;
132 /*************************************************************************
133 * Platform specific definitions
135 #if defined(TRIO_PLATFORM_UNIX)
140 #endif /* TRIO_PLATFORM_UNIX */
141 #if defined(TRIO_PLATFORM_VMS)
144 #if defined(TRIO_PLATFORM_WIN32)
145 # if defined(_WIN32_WCE)
146 # include <wincecompat.h>
150 # define write _write
152 #endif /* TRIO_PLATFORM_WIN32 */
155 # if defined(TRIO_COMPILER_SUPPORTS_ISO94)
158 typedef wchar_t trio_wchar_t;
159 typedef wint_t trio_wint_t;
161 typedef char trio_wchar_t;
162 typedef int trio_wint_t;
163 # define WCONST(x) L ## x
165 # define iswalnum(x) isalnum(x)
166 # define iswalpha(x) isalpha(x)
167 # define iswblank(x) isblank(x)
168 # define iswcntrl(x) iscntrl(x)
169 # define iswdigit(x) isdigit(x)
170 # define iswgraph(x) isgraph(x)
171 # define iswlower(x) islower(x)
172 # define iswprint(x) isprint(x)
173 # define iswpunct(x) ispunct(x)
174 # define iswspace(x) isspace(x)
175 # define iswupper(x) isupper(x)
176 # define iswxdigit(x) isxdigit(x)
181 /*************************************************************************
182 * Compiler dependent definitions
185 /* Support for long long */
187 # if !defined(USE_LONGLONG)
188 # if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
189 # define USE_LONGLONG
190 # elif defined(TRIO_COMPILER_SUNPRO)
191 # define USE_LONGLONG
192 # elif defined(_LONG_LONG) || defined(_LONGLONG)
193 # define USE_LONGLONG
198 /* The extra long numbers */
199 #if defined(USE_LONGLONG)
200 typedef signed long long int trio_longlong_t;
201 typedef unsigned long long int trio_ulonglong_t;
202 #elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
203 typedef signed __int64 trio_longlong_t;
204 typedef unsigned __int64 trio_ulonglong_t;
206 typedef TRIO_SIGNED long int trio_longlong_t;
207 typedef unsigned long int trio_ulonglong_t;
210 /* Maximal and fixed integer types */
211 #if defined(TRIO_COMPILER_SUPPORTS_C99)
213 typedef intmax_t trio_intmax_t;
214 typedef uintmax_t trio_uintmax_t;
215 typedef int8_t trio_int8_t;
216 typedef int16_t trio_int16_t;
217 typedef int32_t trio_int32_t;
218 typedef int64_t trio_int64_t;
219 #elif defined(TRIO_COMPILER_SUPPORTS_UNIX98)
220 # include <inttypes.h>
221 typedef intmax_t trio_intmax_t;
222 typedef uintmax_t trio_uintmax_t;
223 typedef int8_t trio_int8_t;
224 typedef int16_t trio_int16_t;
225 typedef int32_t trio_int32_t;
226 typedef int64_t trio_int64_t;
227 #elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
228 typedef trio_longlong_t trio_intmax_t;
229 typedef trio_ulonglong_t trio_uintmax_t;
230 typedef __int8 trio_int8_t;
231 typedef __int16 trio_int16_t;
232 typedef __int32 trio_int32_t;
233 typedef __int64 trio_int64_t;
235 typedef trio_longlong_t trio_intmax_t;
236 typedef trio_ulonglong_t trio_uintmax_t;
237 # if defined(TRIO_INT8_T)
238 typedef TRIO_INT8_T trio_int8_t;
240 typedef TRIO_SIGNED char trio_int8_t;
242 # if defined(TRIO_INT16_T)
243 typedef TRIO_INT16_T trio_int16_t;
245 typedef TRIO_SIGNED short trio_int16_t;
247 # if defined(TRIO_INT32_T)
248 typedef TRIO_INT32_T trio_int32_t;
250 typedef TRIO_SIGNED int trio_int32_t;
252 # if defined(TRIO_INT64_T)
253 typedef TRIO_INT64_T trio_int64_t;
255 typedef trio_longlong_t trio_int64_t;
259 #if (!(defined(TRIO_COMPILER_SUPPORTS_C99) \
260 || defined(TRIO_COMPILER_SUPPORTS_UNIX01))) \
261 && !defined(_WIN32_WCE)
262 # define floorl(x) floor((double)(x))
263 # define fmodl(x,y) fmod((double)(x),(double)(y))
264 # define powl(x,y) pow((double)(x),(double)(y))
267 #define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
269 /*************************************************************************
270 * Internal Definitions
274 # define DECIMAL_DIG DBL_DIG
277 /* Long double sizes */
279 # define MAX_MANTISSA_DIGITS LDBL_DIG
280 # define MAX_EXPONENT_DIGITS 4
281 # define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
283 # define MAX_MANTISSA_DIGITS DECIMAL_DIG
284 # define MAX_EXPONENT_DIGITS 3
285 # define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
288 #if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
290 # undef LDBL_MANT_DIG
292 # define LDBL_DIG DBL_DIG
293 # define LDBL_MANT_DIG DBL_MANT_DIG
294 # define LDBL_EPSILON DBL_EPSILON
297 /* The maximal number of digits is for base 2 */
298 #define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
299 /* The width of a pointer. The number of bits in a hex digit is 4 */
300 #define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
302 /* Infinite and Not-A-Number for floating-point */
303 #define INFINITE_LOWER "inf"
304 #define INFINITE_UPPER "INF"
305 #define LONG_INFINITE_LOWER "infinite"
306 #define LONG_INFINITE_UPPER "INFINITE"
307 #define NAN_LOWER "nan"
308 #define NAN_UPPER "NAN"
310 #if !defined(HAVE_ISASCII) && !defined(isascii)
311 # define isascii(x) ((unsigned int)(x) < 128)
314 /* Various constants */
319 /* Flags. FLAGS_LAST must be less than ULONG_MAX */
322 FLAGS_SPACE = 2 * FLAGS_STICKY,
323 FLAGS_SHOWSIGN = 2 * FLAGS_SPACE,
324 FLAGS_LEFTADJUST = 2 * FLAGS_SHOWSIGN,
325 FLAGS_ALTERNATIVE = 2 * FLAGS_LEFTADJUST,
326 FLAGS_SHORT = 2 * FLAGS_ALTERNATIVE,
327 FLAGS_SHORTSHORT = 2 * FLAGS_SHORT,
328 FLAGS_LONG = 2 * FLAGS_SHORTSHORT,
329 FLAGS_QUAD = 2 * FLAGS_LONG,
330 FLAGS_LONGDOUBLE = 2 * FLAGS_QUAD,
331 FLAGS_SIZE_T = 2 * FLAGS_LONGDOUBLE,
332 FLAGS_PTRDIFF_T = 2 * FLAGS_SIZE_T,
333 FLAGS_INTMAX_T = 2 * FLAGS_PTRDIFF_T,
334 FLAGS_NILPADDING = 2 * FLAGS_INTMAX_T,
335 FLAGS_UNSIGNED = 2 * FLAGS_NILPADDING,
336 FLAGS_UPPER = 2 * FLAGS_UNSIGNED,
337 FLAGS_WIDTH = 2 * FLAGS_UPPER,
338 FLAGS_WIDTH_PARAMETER = 2 * FLAGS_WIDTH,
339 FLAGS_PRECISION = 2 * FLAGS_WIDTH_PARAMETER,
340 FLAGS_PRECISION_PARAMETER = 2 * FLAGS_PRECISION,
341 FLAGS_BASE = 2 * FLAGS_PRECISION_PARAMETER,
342 FLAGS_BASE_PARAMETER = 2 * FLAGS_BASE,
343 FLAGS_FLOAT_E = 2 * FLAGS_BASE_PARAMETER,
344 FLAGS_FLOAT_G = 2 * FLAGS_FLOAT_E,
345 FLAGS_QUOTE = 2 * FLAGS_FLOAT_G,
346 FLAGS_WIDECHAR = 2 * FLAGS_QUOTE,
347 FLAGS_ALLOC = 2 * FLAGS_WIDECHAR,
348 FLAGS_IGNORE = 2 * FLAGS_ALLOC,
349 FLAGS_IGNORE_PARAMETER = 2 * FLAGS_IGNORE,
350 FLAGS_VARSIZE_PARAMETER = 2 * FLAGS_IGNORE_PARAMETER,
351 FLAGS_FIXED_SIZE = 2 * FLAGS_VARSIZE_PARAMETER,
352 FLAGS_LAST = FLAGS_FIXED_SIZE,
354 FLAGS_EXCLUDE = FLAGS_SHORT,
355 FLAGS_USER_DEFINED = FLAGS_IGNORE,
356 FLAGS_ROUNDING = FLAGS_INTMAX_T,
357 /* Compounded flags */
358 FLAGS_ALL_VARSIZES = FLAGS_LONG | FLAGS_QUAD | FLAGS_INTMAX_T | FLAGS_PTRDIFF_T | FLAGS_SIZE_T,
359 FLAGS_ALL_SIZES = FLAGS_ALL_VARSIZES | FLAGS_SHORTSHORT | FLAGS_SHORT,
366 /* Do not change these */
375 /* Maximal number of allowed parameters */
377 /* Maximal number of characters in class */
378 MAX_CHARACTER_CLASS = UCHAR_MAX + 1,
380 /* Maximal string lengths for user-defined specifiers */
384 /* Maximal length of locale separator strings */
385 MAX_LOCALE_SEPARATOR_LENGTH = MB_LEN_MAX,
386 /* Maximal number of integers in grouping */
387 MAX_LOCALE_GROUPS = 64,
389 /* Initial size of asprintf buffer */
390 DYNAMIC_START_SIZE = 32
393 #define NO_GROUPING ((int)CHAR_MAX)
395 /* Fundamental formatting parameter types */
396 #define FORMAT_UNKNOWN 0
398 #define FORMAT_DOUBLE 2
399 #define FORMAT_CHAR 3
400 #define FORMAT_STRING 4
401 #define FORMAT_POINTER 5
402 #define FORMAT_COUNT 6
403 #define FORMAT_PARAMETER 7
404 #define FORMAT_GROUP 8
406 # define FORMAT_ERRNO 9
409 # define FORMAT_USER_DEFINED 10
412 /* Character constants */
413 #define CHAR_IDENTIFIER '%'
414 #define CHAR_BACKSLASH '\\'
415 #define CHAR_QUOTE '\"'
416 #define CHAR_ADJUST ' '
418 /* Character class expressions */
419 #define CLASS_ALNUM "[:alnum:]"
420 #define CLASS_ALPHA "[:alpha:]"
421 #define CLASS_BLANK "[:blank:]"
422 #define CLASS_CNTRL "[:cntrl:]"
423 #define CLASS_DIGIT "[:digit:]"
424 #define CLASS_GRAPH "[:graph:]"
425 #define CLASS_LOWER "[:lower:]"
426 #define CLASS_PRINT "[:print:]"
427 #define CLASS_PUNCT "[:punct:]"
428 #define CLASS_SPACE "[:space:]"
429 #define CLASS_UPPER "[:upper:]"
430 #define CLASS_XDIGIT "[:xdigit:]"
439 * C Widechar character (wint_t)
453 * S Widechar string (wchar_t *)
462 * D Binary Coded Decimal %D(length,precision) (OS/390)
464 #define SPECIFIER_CHAR 'c'
465 #define SPECIFIER_STRING 's'
466 #define SPECIFIER_DECIMAL 'd'
467 #define SPECIFIER_INTEGER 'i'
468 #define SPECIFIER_UNSIGNED 'u'
469 #define SPECIFIER_OCTAL 'o'
470 #define SPECIFIER_HEX 'x'
471 #define SPECIFIER_HEX_UPPER 'X'
472 #define SPECIFIER_FLOAT_E 'e'
473 #define SPECIFIER_FLOAT_E_UPPER 'E'
474 #define SPECIFIER_FLOAT_F 'f'
475 #define SPECIFIER_FLOAT_F_UPPER 'F'
476 #define SPECIFIER_FLOAT_G 'g'
477 #define SPECIFIER_FLOAT_G_UPPER 'G'
478 #define SPECIFIER_POINTER 'p'
479 #define SPECIFIER_GROUP '['
480 #define SPECIFIER_UNGROUP ']'
481 #define SPECIFIER_COUNT 'n'
483 # define SPECIFIER_CHAR_UPPER 'C'
484 # define SPECIFIER_STRING_UPPER 'S'
487 # define SPECIFIER_HEXFLOAT 'a'
488 # define SPECIFIER_HEXFLOAT_UPPER 'A'
491 # define SPECIFIER_ERRNO 'm'
494 # define SPECIFIER_BINARY 'b'
495 # define SPECIFIER_BINARY_UPPER 'B'
496 # define SPECIFIER_USER_DEFINED_BEGIN '<'
497 # define SPECIFIER_USER_DEFINED_END '>'
498 # define SPECIFIER_USER_DEFINED_SEPARATOR ':'
505 * Numbers = d,i,o,u,x,X
506 * Float = a,A,e,E,f,F,g,G
512 * Use the 9th parameter. 9 can be any number between 1 and
513 * the maximal argument
516 * Set width to 9. 9 can be any number, but must not be postfixed
521 * (unsigned) short int
529 * (unsigned) long int
537 * (unsigned) long long int
545 * Decimal-point is always present
547 * non-printable characters are handled as \number
558 * print: use parameter
559 * scan: no parameter (ignore)
569 * Integer part grouped in thousands
571 * Number grouped in nibbles (4 bits)
580 * @ Parameter (for both print and scan)
584 * The following options exists
586 * I16 = 16-bit integer
587 * I32 = 32-bit integer
588 * I64 = 64-bit integer
590 #define QUALIFIER_POSITION '$'
591 #define QUALIFIER_SHORT 'h'
592 #define QUALIFIER_LONG 'l'
593 #define QUALIFIER_LONG_UPPER 'L'
594 #define QUALIFIER_ALTERNATIVE '#'
595 #define QUALIFIER_SPACE ' '
596 #define QUALIFIER_PLUS '+'
597 #define QUALIFIER_MINUS '-'
598 #define QUALIFIER_DOT '.'
599 #define QUALIFIER_STAR '*'
600 #define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
602 # define QUALIFIER_SIZE_T 'z'
603 # define QUALIFIER_PTRDIFF_T 't'
604 # define QUALIFIER_INTMAX_T 'j'
606 #if TRIO_BSD || TRIO_GNU
607 # define QUALIFIER_QUAD 'q'
610 # define QUALIFIER_SIZE_T_UPPER 'Z'
613 # define QUALIFIER_WIDECHAR 'w'
616 # define QUALIFIER_FIXED_SIZE 'I'
619 # define QUALIFIER_QUOTE '\''
620 # define QUALIFIER_STICKY '!'
621 # define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
622 # define QUALIFIER_PARAM '@' /* Experimental */
623 # define QUALIFIER_COLON ':' /* For scanlists */
624 # define QUALIFIER_EQUAL '=' /* For scanlists */
625 # define QUALIFIER_ROUNDING_UPPER 'R'
629 /*************************************************************************
631 * Internal Structures
633 *************************************************************************/
637 /* An indication of which entry in the data union is used */
641 /* The width qualifier */
643 /* The precision qualifier */
645 /* The base qualifier */
647 /* The size for the variable size qualifier */
649 /* The marker of the end of the specifier */
650 int indexAfterSpecifier;
651 /* The data from the argument list */
655 trio_wchar_t *wstring;
657 trio_pointer_t pointer;
659 trio_intmax_t as_signed;
660 trio_uintmax_t as_unsigned;
663 double *doublePointer;
664 trio_long_double_t longdoubleNumber;
665 trio_long_double_t *longdoublePointer;
668 /* For the user-defined specifier */
669 char user_name[MAX_USER_NAME];
670 char user_data[MAX_USER_DATA];
673 /* Container for customized functions */
676 trio_outstream_t out;
679 trio_pointer_t closure;
682 /* General trio "class" */
683 typedef struct _trio_class_t {
685 * The function to write characters to a stream.
687 void (*OutStream) TRIO_PROTO((struct _trio_class_t *, int));
689 * The function to read characters from a stream.
691 void (*InStream) TRIO_PROTO((struct _trio_class_t *, int *));
693 * The current location in the stream.
695 trio_pointer_t location;
697 * The character currently being processed.
701 * The number of characters that would have been written/read
702 * if there had been sufficient space.
706 * The number of characters that are actually written/read.
707 * Processed and committed will only differ for the *nprintf
708 * and *nscanf functions.
712 * The upper limit of characters that may be written/read.
716 * The last output error that was detected.
721 /* References (for user-defined callbacks) */
722 typedef struct _trio_reference_t {
724 trio_parameter_t *parameter;
727 /* Registered entries (for user-defined callbacks) */
728 typedef struct _trio_userdef_t {
729 struct _trio_userdef_t *next;
730 trio_callback_t callback;
734 /*************************************************************************
738 *************************************************************************/
740 static TRIO_CONST char rcsid[] = "@(#)$Id$";
743 * Need this to workaround a parser bug in HP C/iX compiler that fails
744 * to resolves macro definitions that includes type 'long double',
745 * e.g: va_arg(arg_ptr, long double)
747 #if defined(TRIO_PLATFORM_MPEIX)
748 static TRIO_CONST trio_long_double_t ___dummy_long_double = 0;
751 static TRIO_CONST char internalNullString[] = "(nil)";
753 #if defined(USE_LOCALE)
754 static struct lconv *internalLocaleValues = NULL;
758 * UNIX98 says "in a locale where the radix character is not defined,
759 * the radix character defaults to a period (.)"
761 static int internalDecimalPointLength = 1;
762 static int internalThousandSeparatorLength = 1;
763 static char internalDecimalPoint = '.';
764 static char internalDecimalPointString[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ".";
765 static char internalThousandSeparator[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ",";
766 static char internalGrouping[MAX_LOCALE_GROUPS] = { (char)NO_GROUPING };
768 static TRIO_CONST char internalDigitsLower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
769 static TRIO_CONST char internalDigitsUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
770 static BOOLEAN_T internalDigitsUnconverted = TRUE;
771 static int internalDigitArray[128];
773 static BOOLEAN_T internalCollationUnconverted = TRUE;
774 static char internalCollationArray[MAX_CHARACTER_CLASS][MAX_CHARACTER_CLASS];
778 static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion = NULL;
779 static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion = NULL;
780 static trio_userdef_t *internalUserDef = NULL;
784 /*************************************************************************
788 ************************************************************************/
790 #if defined(TRIO_MINIMAL)
791 # define TRIO_STRING_PUBLIC static
792 # include "triostr.c"
793 #endif /* defined(TRIO_MINIMAL) */
795 /*************************************************************************
799 * Remember to add all new qualifiers to this function.
800 * QUALIFIER_POSITION must not be added.
802 TRIO_PRIVATE BOOLEAN_T
804 TRIO_ARGS1((character),
805 TRIO_CONST char character)
807 /* QUALIFIER_POSITION is not included */
810 case '0': case '1': case '2': case '3': case '4':
811 case '5': case '6': case '7': case '8': case '9':
813 case QUALIFIER_MINUS:
814 case QUALIFIER_SPACE:
817 case QUALIFIER_ALTERNATIVE:
818 case QUALIFIER_SHORT:
820 case QUALIFIER_LONG_UPPER:
821 case QUALIFIER_CIRCUMFLEX:
822 #if defined(QUALIFIER_SIZE_T)
823 case QUALIFIER_SIZE_T:
825 #if defined(QUALIFIER_PTRDIFF_T)
826 case QUALIFIER_PTRDIFF_T:
828 #if defined(QUALIFIER_INTMAX_T)
829 case QUALIFIER_INTMAX_T:
831 #if defined(QUALIFIER_QUAD)
834 #if defined(QUALIFIER_SIZE_T_UPPER)
835 case QUALIFIER_SIZE_T_UPPER:
837 #if defined(QUALIFIER_WIDECHAR)
838 case QUALIFIER_WIDECHAR:
840 #if defined(QUALIFIER_QUOTE)
841 case QUALIFIER_QUOTE:
843 #if defined(QUALIFIER_STICKY)
844 case QUALIFIER_STICKY:
846 #if defined(QUALIFIER_VARSIZE)
847 case QUALIFIER_VARSIZE:
849 #if defined(QUALIFIER_PARAM)
850 case QUALIFIER_PARAM:
852 #if defined(QUALIFIER_FIXED_SIZE)
853 case QUALIFIER_FIXED_SIZE:
855 #if defined(QUALIFIER_ROUNDING_UPPER)
856 case QUALIFIER_ROUNDING_UPPER:
864 /*************************************************************************
867 #if defined(USE_LOCALE)
869 TrioSetLocale(TRIO_NOARGS)
871 internalLocaleValues = (struct lconv *)localeconv();
872 if (internalLocaleValues)
874 if ((internalLocaleValues->decimal_point) &&
875 (internalLocaleValues->decimal_point[0] != NIL))
877 internalDecimalPointLength = trio_length(internalLocaleValues->decimal_point);
878 if (internalDecimalPointLength == 1)
880 internalDecimalPoint = internalLocaleValues->decimal_point[0];
884 internalDecimalPoint = NIL;
885 trio_copy_max(internalDecimalPointString,
886 sizeof(internalDecimalPointString),
887 internalLocaleValues->decimal_point);
890 if ((internalLocaleValues->thousands_sep) &&
891 (internalLocaleValues->thousands_sep[0] != NIL))
893 trio_copy_max(internalThousandSeparator,
894 sizeof(internalThousandSeparator),
895 internalLocaleValues->thousands_sep);
896 internalThousandSeparatorLength = trio_length(internalThousandSeparator);
898 if ((internalLocaleValues->grouping) &&
899 (internalLocaleValues->grouping[0] != NIL))
901 trio_copy_max(internalGrouping,
902 sizeof(internalGrouping),
903 internalLocaleValues->grouping);
907 #endif /* defined(USE_LOCALE) */
910 TrioCalcThousandSeparatorLength
916 int step = NO_GROUPING;
917 char *groupingPointer = internalGrouping;
921 if (*groupingPointer == CHAR_MAX)
923 /* Disable grouping */
926 else if (*groupingPointer == 0)
928 /* Repeat last group */
929 if (step == NO_GROUPING)
931 /* Error in locale */
937 step = *groupingPointer++;
940 count += internalThousandSeparatorLength;
949 TRIO_PRIVATE BOOLEAN_T
950 TrioFollowedBySeparator
951 TRIO_ARGS1((position),
956 char *groupingPointer = internalGrouping;
963 if (*groupingPointer == CHAR_MAX)
965 /* Disable grouping */
968 else if (*groupingPointer != 0)
970 step = *groupingPointer++;
976 return (position == 0);
982 /*************************************************************************
985 * Get the %n$ position.
989 TRIO_ARGS2((format, indexPointer),
990 TRIO_CONST char *format,
996 int index = *indexPointer;
998 number = (int)trio_to_long(&format[index], &tmpformat, BASE_DECIMAL);
999 index = (int)(tmpformat - format);
1000 if ((number != 0) && (QUALIFIER_POSITION == format[index++]))
1002 *indexPointer = index;
1004 * number is decreased by 1, because n$ starts from 1, whereas
1005 * the array it is indexing starts from 0.
1014 /*************************************************************************
1017 * Find registered user-defined specifier.
1018 * The prev argument is used for optimization only.
1020 TRIO_PRIVATE trio_userdef_t *
1022 TRIO_ARGS2((name, prev),
1023 TRIO_CONST char *name,
1024 trio_userdef_t **prev)
1026 trio_userdef_t *def;
1028 if (internalEnterCriticalRegion)
1029 (void)internalEnterCriticalRegion(NULL);
1031 for (def = internalUserDef; def; def = def->next)
1033 /* Case-sensitive string comparison */
1034 if (trio_equal_case(def->name, name))
1041 if (internalLeaveCriticalRegion)
1042 (void)internalLeaveCriticalRegion(NULL);
1048 /*************************************************************************
1052 * Calculate pow(base, exponent), where number and exponent are integers.
1054 TRIO_PRIVATE trio_long_double_t
1056 TRIO_ARGS2((number, exponent),
1060 trio_long_double_t result;
1066 /* Speed up calculation of common cases */
1068 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E-1);
1071 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+0);
1074 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+1);
1077 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+2);
1080 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+3);
1083 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+4);
1086 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+5);
1089 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+6);
1092 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+7);
1095 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+8);
1098 result = powl((trio_long_double_t)number,
1099 (trio_long_double_t)exponent);
1105 return powl((trio_long_double_t)number, (trio_long_double_t)exponent);
1110 /*************************************************************************
1115 TRIO_ARGS2((number, base),
1123 /* xlC crashes on log(0) */
1124 result = (number == 0.0) ? trio_ninf() : trio_nan();
1130 result = log10(number);
1134 result = log10(number) / log10((double)base);
1140 /*************************************************************************
1150 case BASE_BINARY : return 1.0;
1151 case BASE_OCTAL : return 3.0;
1152 case BASE_DECIMAL: return 3.321928094887362345;
1153 case BASE_HEX : return 4.0;
1154 default : return TrioLogarithm((double)base, 2);
1158 /*************************************************************************
1162 * Parse the format string
1166 TRIO_ARGS5((type, format, parameters, arglist, argarray),
1168 TRIO_CONST char *format,
1169 trio_parameter_t *parameters,
1171 trio_pointer_t *argarray)
1173 /* Count the number of times a parameter is referenced */
1174 unsigned short usedEntries[MAX_PARAMETERS];
1175 /* Parameter counters */
1176 int parameterPosition;
1179 /* Utility variables */
1185 int index; /* Index into formatting string */
1186 int dots; /* Count number of dots in modifier part */
1187 BOOLEAN_T positional; /* Does the specifier have a positional? */
1188 BOOLEAN_T gotSticky = FALSE; /* Are there any sticky modifiers at all? */
1190 * indices specifies the order in which the parameters must be
1191 * read from the va_args (this is necessary to handle positionals)
1193 int indices[MAX_PARAMETERS];
1195 /* Various variables */
1197 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1205 /* One and only one of arglist and argarray must be used */
1206 assert((arglist != NULL) ^ (argarray != NULL));
1209 * The 'parameters' array is not initialized, but we need to
1210 * know which entries we have used.
1212 memset(usedEntries, 0, sizeof(usedEntries));
1216 parameterPosition = 0;
1217 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1218 (void)mblen(NULL, 0);
1221 while (format[index])
1223 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1224 if (! isascii(format[index]))
1227 * Multibyte characters cannot be legal specifiers or
1228 * modifiers, so we skip over them.
1230 charlen = mblen(&format[index], MB_LEN_MAX);
1231 index += (charlen > 0) ? charlen : 1;
1232 continue; /* while */
1234 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
1235 if (CHAR_IDENTIFIER == format[index++])
1237 if (CHAR_IDENTIFIER == format[index])
1240 continue; /* while */
1245 currentParam = TrioGetPosition(format, &index);
1246 positional = (NO_POSITION != currentParam);
1249 /* We have no positional, get the next counter */
1250 currentParam = parameterPosition;
1252 if(currentParam >= MAX_PARAMETERS)
1254 /* Bail out completely to make the error more obvious */
1255 return TRIO_ERROR_RETURN(TRIO_ETOOMANY, index);
1258 if (currentParam > maxParam)
1259 maxParam = currentParam;
1261 /* Default values */
1263 precision = NO_PRECISION;
1267 while (TrioIsQualifier(format[index]))
1269 ch = format[index++];
1273 case QUALIFIER_SPACE:
1274 flags |= FLAGS_SPACE;
1277 case QUALIFIER_PLUS:
1278 flags |= FLAGS_SHOWSIGN;
1281 case QUALIFIER_MINUS:
1282 flags |= FLAGS_LEFTADJUST;
1283 flags &= ~FLAGS_NILPADDING;
1286 case QUALIFIER_ALTERNATIVE:
1287 flags |= FLAGS_ALTERNATIVE;
1291 if (dots == 0) /* Precision */
1295 /* Skip if no precision */
1296 if (QUALIFIER_DOT == format[index])
1299 /* After the first dot we have the precision */
1300 flags |= FLAGS_PRECISION;
1301 if ((QUALIFIER_STAR == format[index])
1302 #if defined(QUALIFIER_PARAM)
1303 || (QUALIFIER_PARAM == format[index])
1308 flags |= FLAGS_PRECISION_PARAMETER;
1310 precision = TrioGetPosition(format, &index);
1311 if (precision == NO_POSITION)
1313 parameterPosition++;
1315 precision = parameterPosition;
1318 precision = currentParam;
1319 currentParam = precision + 1;
1325 currentParam = precision + 1;
1326 if (width > maxParam)
1327 maxParam = precision;
1329 if (currentParam > maxParam)
1330 maxParam = currentParam;
1334 precision = trio_to_long(&format[index],
1337 index = (int)(tmpformat - format);
1340 else if (dots == 1) /* Base */
1344 /* After the second dot we have the base */
1345 flags |= FLAGS_BASE;
1346 if ((QUALIFIER_STAR == format[index])
1347 #if defined(QUALIFIER_PARAM)
1348 || (QUALIFIER_PARAM == format[index])
1353 flags |= FLAGS_BASE_PARAMETER;
1354 base = TrioGetPosition(format, &index);
1355 if (base == NO_POSITION)
1357 parameterPosition++;
1359 base = parameterPosition;
1362 base = currentParam;
1363 currentParam = base + 1;
1369 currentParam = base + 1;
1370 if (base > maxParam)
1373 if (currentParam > maxParam)
1374 maxParam = currentParam;
1378 base = trio_to_long(&format[index],
1381 if (base > MAX_BASE)
1382 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1383 index = (int)(tmpformat - format);
1388 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1390 break; /* QUALIFIER_DOT */
1392 #if defined(QUALIFIER_PARAM)
1393 case QUALIFIER_PARAM:
1397 case QUALIFIER_STAR:
1398 /* This has different meanings for print and scan */
1399 if (TYPE_PRINT == type)
1401 /* Read with from parameter */
1402 flags |= (FLAGS_WIDTH | FLAGS_WIDTH_PARAMETER);
1403 width = TrioGetPosition(format, &index);
1404 if (width == NO_POSITION)
1406 parameterPosition++;
1408 width = parameterPosition;
1411 width = currentParam;
1412 currentParam = width + 1;
1418 currentParam = width + 1;
1419 if (width > maxParam)
1422 if (currentParam > maxParam)
1423 maxParam = currentParam;
1427 /* Scan, but do not store result */
1428 flags |= FLAGS_IGNORE;
1431 break; /* QUALIFIER_STAR */
1434 if (! (flags & FLAGS_LEFTADJUST))
1435 flags |= FLAGS_NILPADDING;
1437 case '1': case '2': case '3': case '4':
1438 case '5': case '6': case '7': case '8': case '9':
1439 flags |= FLAGS_WIDTH;
1440 /* &format[index - 1] is used to "rewind" the read
1441 * character from format
1443 width = trio_to_long(&format[index - 1],
1446 index = (int)(tmpformat - format);
1449 case QUALIFIER_SHORT:
1450 if (flags & FLAGS_SHORTSHORT)
1451 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1452 else if (flags & FLAGS_SHORT)
1453 flags |= FLAGS_SHORTSHORT;
1455 flags |= FLAGS_SHORT;
1458 case QUALIFIER_LONG:
1459 if (flags & FLAGS_QUAD)
1460 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1461 else if (flags & FLAGS_LONG)
1462 flags |= FLAGS_QUAD;
1464 flags |= FLAGS_LONG;
1467 case QUALIFIER_LONG_UPPER:
1468 flags |= FLAGS_LONGDOUBLE;
1471 #if defined(QUALIFIER_SIZE_T)
1472 case QUALIFIER_SIZE_T:
1473 flags |= FLAGS_SIZE_T;
1474 /* Modify flags for later truncation of number */
1475 if (sizeof(size_t) == sizeof(trio_ulonglong_t))
1476 flags |= FLAGS_QUAD;
1477 else if (sizeof(size_t) == sizeof(long))
1478 flags |= FLAGS_LONG;
1482 #if defined(QUALIFIER_PTRDIFF_T)
1483 case QUALIFIER_PTRDIFF_T:
1484 flags |= FLAGS_PTRDIFF_T;
1485 if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t))
1486 flags |= FLAGS_QUAD;
1487 else if (sizeof(ptrdiff_t) == sizeof(long))
1488 flags |= FLAGS_LONG;
1492 #if defined(QUALIFIER_INTMAX_T)
1493 case QUALIFIER_INTMAX_T:
1494 flags |= FLAGS_INTMAX_T;
1495 if (sizeof(trio_intmax_t) == sizeof(trio_ulonglong_t))
1496 flags |= FLAGS_QUAD;
1497 else if (sizeof(trio_intmax_t) == sizeof(long))
1498 flags |= FLAGS_LONG;
1502 #if defined(QUALIFIER_QUAD)
1503 case QUALIFIER_QUAD:
1504 flags |= FLAGS_QUAD;
1508 #if defined(QUALIFIER_FIXED_SIZE)
1509 case QUALIFIER_FIXED_SIZE:
1510 if (flags & FLAGS_FIXED_SIZE)
1511 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1513 if (flags & (FLAGS_ALL_SIZES | FLAGS_LONGDOUBLE |
1514 FLAGS_WIDECHAR | FLAGS_VARSIZE_PARAMETER))
1515 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1517 if ((format[index] == '6') &&
1518 (format[index + 1] == '4'))
1520 varsize = sizeof(trio_int64_t);
1523 else if ((format[index] == '3') &&
1524 (format[index + 1] == '2'))
1526 varsize = sizeof(trio_int32_t);
1529 else if ((format[index] == '1') &&
1530 (format[index + 1] == '6'))
1532 varsize = sizeof(trio_int16_t);
1535 else if (format[index] == '8')
1537 varsize = sizeof(trio_int8_t);
1541 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1543 flags |= FLAGS_FIXED_SIZE;
1547 #if defined(QUALIFIER_WIDECHAR)
1548 case QUALIFIER_WIDECHAR:
1549 flags |= FLAGS_WIDECHAR;
1553 #if defined(QUALIFIER_SIZE_T_UPPER)
1554 case QUALIFIER_SIZE_T_UPPER:
1558 #if defined(QUALIFIER_QUOTE)
1559 case QUALIFIER_QUOTE:
1560 flags |= FLAGS_QUOTE;
1564 #if defined(QUALIFIER_STICKY)
1565 case QUALIFIER_STICKY:
1566 flags |= FLAGS_STICKY;
1571 #if defined(QUALIFIER_VARSIZE)
1572 case QUALIFIER_VARSIZE:
1573 flags |= FLAGS_VARSIZE_PARAMETER;
1574 parameterPosition++;
1576 varsize = parameterPosition;
1579 varsize = currentParam;
1580 currentParam = varsize + 1;
1582 if (currentParam > maxParam)
1583 maxParam = currentParam;
1587 #if defined(QUALIFIER_ROUNDING_UPPER)
1588 case QUALIFIER_ROUNDING_UPPER:
1589 flags |= FLAGS_ROUNDING;
1594 /* Bail out completely to make the error more obvious */
1595 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1597 } /* while qualifier */
1600 * Parameters only need the type and value. The value is
1603 if (flags & FLAGS_WIDTH_PARAMETER)
1605 usedEntries[width] += 1;
1606 parameters[pos].type = FORMAT_PARAMETER;
1607 parameters[pos].flags = 0;
1608 indices[width] = pos;
1611 if (flags & FLAGS_PRECISION_PARAMETER)
1613 usedEntries[precision] += 1;
1614 parameters[pos].type = FORMAT_PARAMETER;
1615 parameters[pos].flags = 0;
1616 indices[precision] = pos;
1619 if (flags & FLAGS_BASE_PARAMETER)
1621 usedEntries[base] += 1;
1622 parameters[pos].type = FORMAT_PARAMETER;
1623 parameters[pos].flags = 0;
1624 indices[base] = pos;
1627 if (flags & FLAGS_VARSIZE_PARAMETER)
1629 usedEntries[varsize] += 1;
1630 parameters[pos].type = FORMAT_PARAMETER;
1631 parameters[pos].flags = 0;
1632 indices[varsize] = pos;
1636 indices[currentParam] = pos;
1638 switch (format[index++])
1640 #if defined(SPECIFIER_CHAR_UPPER)
1641 case SPECIFIER_CHAR_UPPER:
1642 flags |= FLAGS_WIDECHAR;
1645 case SPECIFIER_CHAR:
1646 if (flags & FLAGS_LONG)
1647 flags |= FLAGS_WIDECHAR;
1648 else if (flags & FLAGS_SHORT)
1649 flags &= ~FLAGS_WIDECHAR;
1650 parameters[pos].type = FORMAT_CHAR;
1653 #if defined(SPECIFIER_STRING_UPPER)
1654 case SPECIFIER_STRING_UPPER:
1655 flags |= FLAGS_WIDECHAR;
1658 case SPECIFIER_STRING:
1659 if (flags & FLAGS_LONG)
1660 flags |= FLAGS_WIDECHAR;
1661 else if (flags & FLAGS_SHORT)
1662 flags &= ~FLAGS_WIDECHAR;
1663 parameters[pos].type = FORMAT_STRING;
1666 case SPECIFIER_GROUP:
1667 if (TYPE_SCAN == type)
1670 parameters[pos].type = FORMAT_GROUP;
1671 if (format[index] == QUALIFIER_CIRCUMFLEX)
1673 if (format[index] == SPECIFIER_UNGROUP)
1675 if (format[index] == QUALIFIER_MINUS)
1677 /* Skip nested brackets */
1678 while (format[index] != NIL)
1680 if (format[index] == SPECIFIER_GROUP)
1684 else if (format[index] == SPECIFIER_UNGROUP)
1697 case SPECIFIER_INTEGER:
1698 parameters[pos].type = FORMAT_INT;
1701 case SPECIFIER_UNSIGNED:
1702 flags |= FLAGS_UNSIGNED;
1703 parameters[pos].type = FORMAT_INT;
1706 case SPECIFIER_DECIMAL:
1707 /* Disable base modifier */
1708 flags &= ~FLAGS_BASE_PARAMETER;
1709 base = BASE_DECIMAL;
1710 parameters[pos].type = FORMAT_INT;
1713 case SPECIFIER_OCTAL:
1714 flags |= FLAGS_UNSIGNED;
1715 flags &= ~FLAGS_BASE_PARAMETER;
1717 parameters[pos].type = FORMAT_INT;
1720 #if defined(SPECIFIER_BINARY)
1721 case SPECIFIER_BINARY_UPPER:
1722 flags |= FLAGS_UPPER;
1724 case SPECIFIER_BINARY:
1725 flags |= FLAGS_NILPADDING;
1726 flags &= ~FLAGS_BASE_PARAMETER;
1728 parameters[pos].type = FORMAT_INT;
1732 case SPECIFIER_HEX_UPPER:
1733 flags |= FLAGS_UPPER;
1736 flags |= FLAGS_UNSIGNED;
1737 flags &= ~FLAGS_BASE_PARAMETER;
1739 parameters[pos].type = FORMAT_INT;
1742 case SPECIFIER_FLOAT_E_UPPER:
1743 flags |= FLAGS_UPPER;
1745 case SPECIFIER_FLOAT_E:
1746 flags |= FLAGS_FLOAT_E;
1747 parameters[pos].type = FORMAT_DOUBLE;
1750 case SPECIFIER_FLOAT_G_UPPER:
1751 flags |= FLAGS_UPPER;
1753 case SPECIFIER_FLOAT_G:
1754 flags |= FLAGS_FLOAT_G;
1755 parameters[pos].type = FORMAT_DOUBLE;
1758 case SPECIFIER_FLOAT_F_UPPER:
1759 flags |= FLAGS_UPPER;
1761 case SPECIFIER_FLOAT_F:
1762 parameters[pos].type = FORMAT_DOUBLE;
1765 case SPECIFIER_POINTER:
1766 if (sizeof(trio_pointer_t) == sizeof(trio_ulonglong_t))
1767 flags |= FLAGS_QUAD;
1768 else if (sizeof(trio_pointer_t) == sizeof(long))
1769 flags |= FLAGS_LONG;
1770 parameters[pos].type = FORMAT_POINTER;
1773 case SPECIFIER_COUNT:
1774 parameters[pos].type = FORMAT_COUNT;
1777 #if defined(SPECIFIER_HEXFLOAT)
1778 # if defined(SPECIFIER_HEXFLOAT_UPPER)
1779 case SPECIFIER_HEXFLOAT_UPPER:
1780 flags |= FLAGS_UPPER;
1783 case SPECIFIER_HEXFLOAT:
1785 parameters[pos].type = FORMAT_DOUBLE;
1789 #if defined(FORMAT_ERRNO)
1790 case SPECIFIER_ERRNO:
1791 parameters[pos].type = FORMAT_ERRNO;
1795 #if defined(SPECIFIER_USER_DEFINED_BEGIN)
1796 case SPECIFIER_USER_DEFINED_BEGIN:
1799 int without_namespace = TRUE;
1801 parameters[pos].type = FORMAT_USER_DEFINED;
1802 parameters[pos].user_name[0] = NIL;
1803 tmpformat = (char *)&format[index];
1805 while ((ch = format[index]))
1808 if (ch == SPECIFIER_USER_DEFINED_END)
1810 if (without_namespace)
1812 /* We must get the handle first */
1813 parameters[pos].type = FORMAT_PARAMETER;
1814 parameters[pos].indexAfterSpecifier = index;
1815 parameters[pos].flags = FLAGS_USER_DEFINED;
1816 /* Adjust parameters for insertion of new one */
1818 usedEntries[currentParam] += 1;
1819 parameters[pos].type = FORMAT_USER_DEFINED;
1821 indices[currentParam] = pos;
1822 if (currentParam > maxParam)
1823 maxParam = currentParam;
1825 /* Copy the user data */
1826 max = (unsigned int)(&format[index] - tmpformat);
1827 if (max > MAX_USER_DATA)
1828 max = MAX_USER_DATA;
1829 trio_copy_max(parameters[pos].user_data,
1834 if (ch == SPECIFIER_USER_DEFINED_SEPARATOR)
1836 without_namespace = FALSE;
1837 /* Copy the namespace for later looking-up */
1838 max = (int)(&format[index] - tmpformat);
1839 if (max > MAX_USER_NAME)
1840 max = MAX_USER_NAME;
1841 trio_copy_max(parameters[pos].user_name,
1844 tmpformat = (char *)&format[index];
1847 if (ch != SPECIFIER_USER_DEFINED_END)
1848 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1851 #endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
1854 /* Bail out completely to make the error more obvious */
1855 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1858 /* Count the number of times this entry has been used */
1859 usedEntries[currentParam] += 1;
1861 /* Find last sticky parameters */
1862 if (gotSticky && !(flags & FLAGS_STICKY))
1864 for (i = pos - 1; i >= 0; i--)
1866 if (parameters[i].type == FORMAT_PARAMETER)
1868 if ((parameters[i].flags & FLAGS_STICKY) &&
1869 (parameters[i].type == parameters[pos].type))
1871 /* Do not overwrite current qualifiers */
1872 flags |= (parameters[i].flags & (unsigned long)~FLAGS_STICKY);
1873 if (width == NO_WIDTH)
1874 width = parameters[i].width;
1875 if (precision == NO_PRECISION)
1876 precision = parameters[i].precision;
1877 if (base == NO_BASE)
1878 base = parameters[i].base;
1884 parameters[pos].indexAfterSpecifier = index;
1885 parameters[pos].flags = flags;
1886 parameters[pos].width = width;
1887 parameters[pos].precision = precision;
1888 parameters[pos].base = (base == NO_BASE) ? BASE_DECIMAL : base;
1889 parameters[pos].varsize = varsize;
1893 parameterPosition++;
1895 } /* if identifier */
1897 } /* while format characters left */
1899 for (num = 0; num <= maxParam; num++)
1901 if (usedEntries[num] != 1)
1903 if (usedEntries[num] == 0) /* gap detected */
1904 return TRIO_ERROR_RETURN(TRIO_EGAP, num);
1905 else /* double references detected */
1906 return TRIO_ERROR_RETURN(TRIO_EDBLREF, num);
1912 * FORMAT_PARAMETERS are only present if they must be read,
1913 * so it makes no sense to check the ignore flag (besides,
1914 * the flags variable is not set for that particular type)
1916 if ((parameters[i].type != FORMAT_PARAMETER) &&
1917 (parameters[i].flags & FLAGS_IGNORE))
1918 continue; /* for all arguments */
1921 * The stack arguments are read according to ANSI C89
1922 * default argument promotions:
1926 * unsigned char = unsigned int
1927 * unsigned short = unsigned int
1930 * In addition to the ANSI C89 these types are read (the
1931 * default argument promotions of C99 has not been
1940 switch (parameters[i].type)
1945 if (flags & FLAGS_WIDECHAR)
1947 parameters[i].data.wstring = (argarray == NULL)
1948 ? va_arg(*arglist, trio_wchar_t *)
1949 : (trio_wchar_t *)(argarray[num]);
1954 parameters[i].data.string = (argarray == NULL)
1955 ? va_arg(*arglist, char *)
1956 : (char *)(argarray[num]);
1960 #if defined(FORMAT_USER_DEFINED)
1961 case FORMAT_USER_DEFINED:
1963 case FORMAT_POINTER:
1965 case FORMAT_UNKNOWN:
1966 parameters[i].data.pointer = (argarray == NULL)
1967 ? va_arg(*arglist, trio_pointer_t )
1973 if (TYPE_SCAN == type)
1975 if (argarray == NULL)
1976 parameters[i].data.pointer =
1977 (trio_pointer_t)va_arg(*arglist, trio_pointer_t);
1980 if (parameters[i].type == FORMAT_CHAR)
1981 parameters[i].data.pointer =
1982 (trio_pointer_t)((char *)argarray[num]);
1983 else if (parameters[i].flags & FLAGS_SHORT)
1984 parameters[i].data.pointer =
1985 (trio_pointer_t)((short *)argarray[num]);
1987 parameters[i].data.pointer =
1988 (trio_pointer_t)((int *)argarray[num]);
1993 #if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
1994 if (parameters[i].flags
1995 & (FLAGS_VARSIZE_PARAMETER | FLAGS_FIXED_SIZE))
1997 if (parameters[i].flags & FLAGS_VARSIZE_PARAMETER)
2000 * Variable sizes are mapped onto the fixed sizes, in
2001 * accordance with integer promotion.
2003 * Please note that this may not be portable, as we
2004 * only guess the size, not the layout of the numbers.
2005 * For example, if int is little-endian, and long is
2006 * big-endian, then this will fail.
2008 varsize = (int)parameters[parameters[i].varsize].data.number.as_unsigned;
2012 /* Used for the I<bits> modifiers */
2013 varsize = parameters[i].varsize;
2015 parameters[i].flags &= ~FLAGS_ALL_VARSIZES;
2017 if (varsize <= (int)sizeof(int))
2019 else if (varsize <= (int)sizeof(long))
2020 parameters[i].flags |= FLAGS_LONG;
2021 #if defined(QUALIFIER_INTMAX_T)
2022 else if (varsize <= (int)sizeof(trio_longlong_t))
2023 parameters[i].flags |= FLAGS_QUAD;
2025 parameters[i].flags |= FLAGS_INTMAX_T;
2028 parameters[i].flags |= FLAGS_QUAD;
2031 #endif /* defined(QUALIFIER_VARSIZE) */
2032 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
2033 if (parameters[i].flags & FLAGS_SIZE_T)
2034 parameters[i].data.number.as_unsigned = (argarray == NULL)
2035 ? (trio_uintmax_t)va_arg(*arglist, size_t)
2036 : (trio_uintmax_t)(*((size_t *)argarray[num]));
2039 #if defined(QUALIFIER_PTRDIFF_T)
2040 if (parameters[i].flags & FLAGS_PTRDIFF_T)
2041 parameters[i].data.number.as_unsigned = (argarray == NULL)
2042 ? (trio_uintmax_t)va_arg(*arglist, ptrdiff_t)
2043 : (trio_uintmax_t)(*((ptrdiff_t *)argarray[num]));
2046 #if defined(QUALIFIER_INTMAX_T)
2047 if (parameters[i].flags & FLAGS_INTMAX_T)
2048 parameters[i].data.number.as_unsigned = (argarray == NULL)
2049 ? (trio_uintmax_t)va_arg(*arglist, trio_intmax_t)
2050 : (trio_uintmax_t)(*((trio_intmax_t *)argarray[num]));
2053 if (parameters[i].flags & FLAGS_QUAD)
2054 parameters[i].data.number.as_unsigned = (argarray == NULL)
2055 ? (trio_uintmax_t)va_arg(*arglist, trio_ulonglong_t)
2056 : (trio_uintmax_t)(*((trio_ulonglong_t *)argarray[num]));
2057 else if (parameters[i].flags & FLAGS_LONG)
2058 parameters[i].data.number.as_unsigned = (argarray == NULL)
2059 ? (trio_uintmax_t)va_arg(*arglist, long)
2060 : (trio_uintmax_t)(*((long *)argarray[num]));
2063 if (argarray == NULL)
2064 parameters[i].data.number.as_unsigned = (trio_uintmax_t)va_arg(*arglist, int);
2067 if (parameters[i].type == FORMAT_CHAR)
2068 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((char *)argarray[num]));
2069 else if (parameters[i].flags & FLAGS_SHORT)
2070 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((short *)argarray[num]));
2072 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((int *)argarray[num]));
2078 case FORMAT_PARAMETER:
2080 * The parameter for the user-defined specifier is a pointer,
2081 * whereas the rest (width, precision, base) uses an integer.
2083 if (parameters[i].flags & FLAGS_USER_DEFINED)
2084 parameters[i].data.pointer = (argarray == NULL)
2085 ? va_arg(*arglist, trio_pointer_t )
2088 parameters[i].data.number.as_unsigned = (argarray == NULL)
2089 ? (trio_uintmax_t)va_arg(*arglist, int)
2090 : (trio_uintmax_t)(*((int *)argarray[num]));
2094 if (TYPE_SCAN == type)
2096 if (parameters[i].flags & FLAGS_LONGDOUBLE)
2097 parameters[i].data.longdoublePointer = (argarray == NULL)
2098 ? va_arg(*arglist, trio_long_double_t *)
2099 : (trio_long_double_t *)argarray[num];
2102 if (parameters[i].flags & FLAGS_LONG)
2103 parameters[i].data.doublePointer = (argarray == NULL)
2104 ? va_arg(*arglist, double *)
2105 : (double *)argarray[num];
2107 parameters[i].data.doublePointer = (argarray == NULL)
2108 ? (double *)va_arg(*arglist, float *)
2109 : (double *)((float *)argarray[num]);
2114 if (parameters[i].flags & FLAGS_LONGDOUBLE)
2115 parameters[i].data.longdoubleNumber = (argarray == NULL)
2116 ? va_arg(*arglist, trio_long_double_t)
2117 : (trio_long_double_t)(*((trio_long_double_t *)argarray[num]));
2120 if (argarray == NULL)
2121 parameters[i].data.longdoubleNumber =
2122 (trio_long_double_t)va_arg(*arglist, double);
2125 if (parameters[i].flags & FLAGS_SHORT)
2126 parameters[i].data.longdoubleNumber =
2127 (trio_long_double_t)(*((float *)argarray[num]));
2129 parameters[i].data.longdoubleNumber =
2130 (trio_long_double_t)(*((double *)argarray[num]));
2136 #if defined(FORMAT_ERRNO)
2138 parameters[i].data.errorNumber = save_errno;
2145 } /* for all specifiers */
2150 /*************************************************************************
2154 ************************************************************************/
2157 /*************************************************************************
2162 * The complexity of this function is a result of the complexity
2163 * of the dependencies of the flags.
2167 TRIO_ARGS6((self, number, flags, width, precision, base),
2169 trio_uintmax_t number,
2175 BOOLEAN_T isNegative;
2176 BOOLEAN_T isNumberZero;
2177 BOOLEAN_T isPrecisionZero;
2178 BOOLEAN_T ignoreNumber;
2179 char buffer[MAX_CHARS_IN(trio_uintmax_t) * (1 + MAX_LOCALE_SEPARATOR_LENGTH) + 1];
2182 TRIO_CONST char *digits;
2188 assert(VALID(self));
2189 assert(VALID(self->OutStream));
2190 assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
2192 digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
2193 if (base == NO_BASE)
2194 base = BASE_DECIMAL;
2196 isNumberZero = (number == 0);
2197 isPrecisionZero = (precision == 0);
2198 ignoreNumber = (isNumberZero
2200 && !((flags & FLAGS_ALTERNATIVE) && (base == BASE_OCTAL)));
2202 if (flags & FLAGS_UNSIGNED)
2205 flags &= ~FLAGS_SHOWSIGN;
2209 isNegative = ((trio_intmax_t)number < 0);
2211 number = -((trio_intmax_t)number);
2214 if (flags & FLAGS_QUAD)
2215 number &= (trio_ulonglong_t)-1;
2216 else if (flags & FLAGS_LONG)
2217 number &= (unsigned long)-1;
2219 number &= (unsigned int)-1;
2222 pointer = bufferend = &buffer[sizeof(buffer) - 1];
2224 for (i = 1; i < (int)sizeof(buffer); i++)
2226 *pointer-- = digits[number % base];
2231 if ((flags & FLAGS_QUOTE) && TrioFollowedBySeparator(i + 1))
2234 * We are building the number from the least significant
2235 * to the most significant digit, so we have to copy the
2236 * thousand separator backwards
2238 length = internalThousandSeparatorLength;
2239 if (((int)(pointer - buffer) - length) > 0)
2241 p = &internalThousandSeparator[length - 1];
2242 while (length-- > 0)
2251 width -= (bufferend - pointer) - 1;
2254 /* Adjust precision */
2255 if (NO_PRECISION != precision)
2257 precision -= (bufferend - pointer) - 1;
2260 flags |= FLAGS_NILPADDING;
2263 /* Calculate padding */
2264 count = (! ((flags & FLAGS_LEFTADJUST) || (precision == NO_PRECISION)))
2268 /* Adjust width further */
2269 if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
2271 if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
2280 if (!(flags & FLAGS_NILPADDING) || (count == 0))
2288 /* Output prefixes spaces if needed */
2289 if (! ((flags & FLAGS_LEFTADJUST) ||
2290 ((flags & FLAGS_NILPADDING) && (precision == NO_PRECISION))))
2292 while (width-- > count)
2293 self->OutStream(self, CHAR_ADJUST);
2296 /* width has been adjusted for signs and alternatives */
2298 self->OutStream(self, '-');
2299 else if (flags & FLAGS_SHOWSIGN)
2300 self->OutStream(self, '+');
2301 else if (flags & FLAGS_SPACE)
2302 self->OutStream(self, ' ');
2304 /* Prefix is not written when the value is zero */
2305 if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
2310 self->OutStream(self, '0');
2311 self->OutStream(self, (flags & FLAGS_UPPER) ? 'B' : 'b');
2315 if (!(flags & FLAGS_NILPADDING) || (count == 0))
2316 self->OutStream(self, '0');
2320 self->OutStream(self, '0');
2321 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2329 /* Output prefixed zero padding if needed */
2330 if (flags & FLAGS_NILPADDING)
2332 if (precision == NO_PRECISION)
2334 while (precision-- > 0)
2336 self->OutStream(self, '0');
2343 /* Output the number itself */
2344 while (*(++pointer))
2346 self->OutStream(self, *pointer);
2350 /* Output trailing spaces if needed */
2351 if (flags & FLAGS_LEFTADJUST)
2354 self->OutStream(self, CHAR_ADJUST);
2358 /*************************************************************************
2359 * TrioWriteStringCharacter
2362 * Output a single character of a string
2365 TrioWriteStringCharacter
2366 TRIO_ARGS3((self, ch, flags),
2371 if (flags & FLAGS_ALTERNATIVE)
2376 * Non-printable characters are converted to C escapes or
2377 * \number, if no C escape exists.
2379 self->OutStream(self, CHAR_BACKSLASH);
2382 case '\007': self->OutStream(self, 'a'); break;
2383 case '\b': self->OutStream(self, 'b'); break;
2384 case '\f': self->OutStream(self, 'f'); break;
2385 case '\n': self->OutStream(self, 'n'); break;
2386 case '\r': self->OutStream(self, 'r'); break;
2387 case '\t': self->OutStream(self, 't'); break;
2388 case '\v': self->OutStream(self, 'v'); break;
2389 case '\\': self->OutStream(self, '\\'); break;
2391 self->OutStream(self, 'x');
2392 TrioWriteNumber(self, (trio_uintmax_t)ch,
2393 FLAGS_UNSIGNED | FLAGS_NILPADDING,
2398 else if (ch == CHAR_BACKSLASH)
2400 self->OutStream(self, CHAR_BACKSLASH);
2401 self->OutStream(self, CHAR_BACKSLASH);
2405 self->OutStream(self, ch);
2410 self->OutStream(self, ch);
2414 /*************************************************************************
2422 TRIO_ARGS5((self, string, flags, width, precision),
2424 TRIO_CONST char *string,
2432 assert(VALID(self));
2433 assert(VALID(self->OutStream));
2437 string = internalNullString;
2438 length = sizeof(internalNullString) - 1;
2439 /* Disable quoting for the null pointer */
2440 flags &= (~FLAGS_QUOTE);
2445 length = trio_length(string);
2447 if ((NO_PRECISION != precision) &&
2448 (precision < length))
2454 if (flags & FLAGS_QUOTE)
2455 self->OutStream(self, CHAR_QUOTE);
2457 if (! (flags & FLAGS_LEFTADJUST))
2460 self->OutStream(self, CHAR_ADJUST);
2463 while (length-- > 0)
2465 /* The ctype parameters must be an unsigned char (or EOF) */
2466 ch = (int)((unsigned char)(*string++));
2467 TrioWriteStringCharacter(self, ch, flags);
2470 if (flags & FLAGS_LEFTADJUST)
2473 self->OutStream(self, CHAR_ADJUST);
2475 if (flags & FLAGS_QUOTE)
2476 self->OutStream(self, CHAR_QUOTE);
2479 /*************************************************************************
2480 * TrioWriteWideStringCharacter
2483 * Output a wide string as a multi-byte sequence
2487 TrioWriteWideStringCharacter
2488 TRIO_ARGS4((self, wch, flags, width),
2498 char buffer[MB_LEN_MAX + 1];
2500 if (width == NO_WIDTH)
2501 width = sizeof(buffer);
2503 size = wctomb(buffer, wch);
2504 if ((size <= 0) || (size > width) || (buffer[0] == NIL))
2509 while ((width >= i) && (width-- > 0) && (i-- > 0))
2511 /* The ctype parameters must be an unsigned char (or EOF) */
2512 ch = (int)((unsigned char)(*string++));
2513 TrioWriteStringCharacter(self, ch, flags);
2517 #endif /* TRIO_WIDECHAR */
2519 /*************************************************************************
2520 * TrioWriteWideString
2523 * Output a wide character string as a multi-byte string
2528 TRIO_ARGS5((self, wstring, flags, width, precision),
2530 TRIO_CONST trio_wchar_t *wstring,
2538 assert(VALID(self));
2539 assert(VALID(self->OutStream));
2541 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
2542 (void)mblen(NULL, 0);
2545 if (wstring == NULL)
2547 TrioWriteString(self, NULL, flags, width, precision);
2551 if (NO_PRECISION == precision)
2561 if (flags & FLAGS_QUOTE)
2562 self->OutStream(self, CHAR_QUOTE);
2564 if (! (flags & FLAGS_LEFTADJUST))
2567 self->OutStream(self, CHAR_ADJUST);
2572 size = TrioWriteWideStringCharacter(self, *wstring++, flags, length);
2578 if (flags & FLAGS_LEFTADJUST)
2581 self->OutStream(self, CHAR_ADJUST);
2583 if (flags & FLAGS_QUOTE)
2584 self->OutStream(self, CHAR_QUOTE);
2586 #endif /* TRIO_WIDECHAR */
2588 /*************************************************************************
2591 * http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_211.htm
2593 * "5.2.4.2.2 paragraph #4
2595 * The accuracy [...] is implementation defined, as is the accuracy
2596 * of the conversion between floating-point internal representations
2597 * and string representations performed by the libray routine in
2600 /* FIXME: handle all instances of constant long-double number (L)
2601 * and *l() math functions.
2605 TRIO_ARGS6((self, number, flags, width, precision, base),
2607 trio_long_double_t number,
2613 trio_long_double_t integerNumber;
2614 trio_long_double_t fractionNumber;
2615 trio_long_double_t workNumber;
2620 int integerThreshold;
2621 int fractionThreshold;
2624 unsigned int uExponent = 0;
2626 trio_long_double_t dblBase;
2627 trio_long_double_t dblIntegerBase;
2628 trio_long_double_t dblFractionBase;
2629 trio_long_double_t integerAdjust;
2630 trio_long_double_t fractionAdjust;
2631 BOOLEAN_T isNegative;
2632 BOOLEAN_T isExponentNegative = FALSE;
2633 BOOLEAN_T requireTwoDigitExponent;
2635 TRIO_CONST char *digits;
2636 char *groupingPointer;
2639 BOOLEAN_T hasOnlyZeroes;
2641 register int trailingZeroes;
2642 BOOLEAN_T keepTrailingZeroes;
2643 BOOLEAN_T keepDecimalPoint;
2644 trio_long_double_t epsilon;
2646 assert(VALID(self));
2647 assert(VALID(self->OutStream));
2648 assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
2650 /* Determine sign and look for special quantities */
2651 switch (trio_fpclassify_and_signbit(number, &isNegative))
2654 TrioWriteString(self,
2655 (flags & FLAGS_UPPER)
2658 flags, width, precision);
2661 case TRIO_FP_INFINITE:
2664 /* Negative infinity */
2665 TrioWriteString(self,
2666 (flags & FLAGS_UPPER)
2667 ? "-" INFINITE_UPPER
2668 : "-" INFINITE_LOWER,
2669 flags, width, precision);
2674 /* Positive infinity */
2675 TrioWriteString(self,
2676 (flags & FLAGS_UPPER)
2679 flags, width, precision);
2688 /* Normal numbers */
2689 if (flags & FLAGS_LONGDOUBLE)
2691 baseDigits = (base == 10)
2693 : (int)floor(LDBL_MANT_DIG / TrioLogarithmBase(base));
2694 epsilon = LDBL_EPSILON;
2696 else if (flags & FLAGS_SHORT)
2698 baseDigits = (base == BASE_DECIMAL)
2700 : (int)floor(FLT_MANT_DIG / TrioLogarithmBase(base));
2701 epsilon = FLT_EPSILON;
2705 baseDigits = (base == BASE_DECIMAL)
2707 : (int)floor(DBL_MANT_DIG / TrioLogarithmBase(base));
2708 epsilon = DBL_EPSILON;
2711 digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
2712 isHex = (base == BASE_HEX);
2713 if (base == NO_BASE)
2714 base = BASE_DECIMAL;
2715 dblBase = (trio_long_double_t)base;
2716 keepTrailingZeroes = !( (flags & FLAGS_ROUNDING) ||
2717 ( (flags & FLAGS_FLOAT_G) &&
2718 !(flags & FLAGS_ALTERNATIVE) ) );
2720 if (flags & FLAGS_ROUNDING)
2721 precision = baseDigits;
2723 if (precision == NO_PRECISION)
2727 keepTrailingZeroes = FALSE;
2728 precision = FLT_MANT_DIG;
2732 precision = FLT_DIG;
2740 flags |= FLAGS_FLOAT_E;
2742 if (flags & FLAGS_FLOAT_G)
2747 if ((number < 1.0E-4) || (number > powl(base,
2748 (trio_long_double_t)precision)))
2750 /* Use scientific notation */
2751 flags |= FLAGS_FLOAT_E;
2753 else if (number < 1.0)
2756 * Use normal notation. If the integer part of the number is
2757 * zero, then adjust the precision to include leading fractional
2760 workNumber = TrioLogarithm(number, base);
2761 workNumber = TRIO_FABS(workNumber);
2762 if (workNumber - floorl(workNumber) < 0.001)
2764 zeroes = (int)floorl(workNumber);
2768 if (flags & FLAGS_FLOAT_E)
2770 /* Scale the number */
2771 workNumber = TrioLogarithm(number, base);
2772 if (trio_isinf(workNumber) == -1)
2776 if (flags & FLAGS_FLOAT_G)
2777 flags &= ~FLAGS_FLOAT_E;
2781 exponent = (int)floorl(workNumber);
2782 number /= powl(dblBase, (trio_long_double_t)exponent);
2783 isExponentNegative = (exponent < 0);
2784 uExponent = (isExponentNegative) ? -exponent : exponent;
2786 uExponent *= 4; /* log16(2) */
2787 /* No thousand separators */
2788 flags &= ~FLAGS_QUOTE;
2792 integerNumber = floorl(number);
2793 fractionNumber = number - integerNumber;
2798 * Precision is number of significant digits for FLOAT_G
2799 * and number of fractional digits for others.
2801 integerDigits = (integerNumber > epsilon)
2802 ? 1 + (int)TrioLogarithm(integerNumber, base)
2804 fractionDigits = ((flags & FLAGS_FLOAT_G) && (zeroes == 0))
2805 ? precision - integerDigits
2806 : zeroes + precision;
2808 dblFractionBase = TrioPower(base, fractionDigits);
2810 workNumber = number + 0.5 / dblFractionBase;
2811 if (floorl(number) != floorl(workNumber))
2813 if (flags & FLAGS_FLOAT_E)
2815 /* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
2817 isExponentNegative = (exponent < 0);
2818 uExponent = (isExponentNegative) ? -exponent : exponent;
2820 uExponent *= 4; /* log16(2) */
2821 workNumber = (number + 0.5 / dblFractionBase) / dblBase;
2822 integerNumber = floorl(workNumber);
2823 fractionNumber = workNumber - integerNumber;
2827 /* Adjust if number was rounded up one digit (ie. 99 to 100) */
2828 integerNumber = floorl(number + 0.5);
2829 fractionNumber = 0.0;
2830 integerDigits = (integerNumber > epsilon)
2831 ? 1 + (int)TrioLogarithm(integerNumber, base)
2836 /* Estimate accuracy */
2837 integerAdjust = fractionAdjust = 0.5;
2838 if (flags & FLAGS_ROUNDING)
2840 if (integerDigits > baseDigits)
2842 integerThreshold = baseDigits;
2844 dblFractionBase = 1.0;
2845 fractionThreshold = 0;
2846 precision = 0; /* Disable decimal-point */
2847 integerAdjust = TrioPower(base, integerDigits - integerThreshold - 1);
2848 fractionAdjust = 0.0;
2852 integerThreshold = integerDigits;
2853 fractionThreshold = fractionDigits - integerThreshold;
2854 fractionAdjust = 1.0;
2859 integerThreshold = INT_MAX;
2860 fractionThreshold = INT_MAX;
2864 * Calculate expected width.
2865 * sign + integer part + thousands separators + decimal point
2866 * + fraction + exponent
2868 fractionAdjust /= dblFractionBase;
2869 hasOnlyZeroes = (floorl((fractionNumber + fractionAdjust) * dblFractionBase) < epsilon);
2870 keepDecimalPoint = ( (flags & FLAGS_ALTERNATIVE) ||
2871 !((precision == 0) ||
2872 (!keepTrailingZeroes && hasOnlyZeroes)) );
2873 if (flags & FLAGS_FLOAT_E)
2875 exponentDigits = (uExponent == 0)
2877 : (int)ceil(TrioLogarithm((double)(uExponent + 1),
2878 (isHex) ? 10.0 : base));
2882 requireTwoDigitExponent = ((base == BASE_DECIMAL) && (exponentDigits == 1));
2884 expectedWidth = integerDigits + fractionDigits
2886 ? internalDecimalPointLength
2888 + ((flags & FLAGS_QUOTE)
2889 ? TrioCalcThousandSeparatorLength(integerDigits)
2891 if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
2892 expectedWidth += sizeof("-") - 1;
2893 if (exponentDigits > 0)
2894 expectedWidth += exponentDigits +
2895 ((requireTwoDigitExponent ? sizeof("E+0") : sizeof("E+")) - 1);
2897 expectedWidth += sizeof("0X") - 1;
2899 /* Output prefixing */
2900 if (flags & FLAGS_NILPADDING)
2902 /* Leading zeros must be after sign */
2904 self->OutStream(self, '-');
2905 else if (flags & FLAGS_SHOWSIGN)
2906 self->OutStream(self, '+');
2907 else if (flags & FLAGS_SPACE)
2908 self->OutStream(self, ' ');
2911 self->OutStream(self, '0');
2912 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2914 if (!(flags & FLAGS_LEFTADJUST))
2916 for (i = expectedWidth; i < width; i++)
2918 self->OutStream(self, '0');
2924 /* Leading spaces must be before sign */
2925 if (!(flags & FLAGS_LEFTADJUST))
2927 for (i = expectedWidth; i < width; i++)
2929 self->OutStream(self, CHAR_ADJUST);
2933 self->OutStream(self, '-');
2934 else if (flags & FLAGS_SHOWSIGN)
2935 self->OutStream(self, '+');
2936 else if (flags & FLAGS_SPACE)
2937 self->OutStream(self, ' ');
2940 self->OutStream(self, '0');
2941 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2945 /* Output the integer part and thousand separators */
2946 dblIntegerBase = 1.0 / TrioPower(base, integerDigits - 1);
2947 for (i = 0; i < integerDigits; i++)
2949 workNumber = floorl(((integerNumber + integerAdjust) * dblIntegerBase));
2950 if (i > integerThreshold)
2952 /* Beyond accuracy */
2953 self->OutStream(self, digits[0]);
2957 self->OutStream(self, digits[(int)fmodl(workNumber, dblBase)]);
2959 dblIntegerBase *= dblBase;
2961 if (((flags & (FLAGS_FLOAT_E | FLAGS_QUOTE)) == FLAGS_QUOTE)
2962 && TrioFollowedBySeparator(integerDigits - i))
2964 for (groupingPointer = internalThousandSeparator;
2965 *groupingPointer != NIL;
2968 self->OutStream(self, *groupingPointer);
2973 /* Insert decimal point and build the fraction part */
2976 if (keepDecimalPoint)
2978 if (internalDecimalPoint)
2980 self->OutStream(self, internalDecimalPoint);
2984 for (i = 0; i < internalDecimalPointLength; i++)
2986 self->OutStream(self, internalDecimalPointString[i]);
2991 for (i = 0; i < fractionDigits; i++)
2993 if ((integerDigits > integerThreshold) || (i > fractionThreshold))
2995 /* Beyond accuracy */
3000 fractionNumber *= dblBase;
3001 fractionAdjust *= dblBase;
3002 workNumber = floorl(fractionNumber + fractionAdjust);
3003 fractionNumber -= workNumber;
3004 index = (int)fmodl(workNumber, dblBase);
3011 while (trailingZeroes > 0)
3013 /* Not trailing zeroes after all */
3014 self->OutStream(self, digits[0]);
3017 self->OutStream(self, digits[index]);
3022 if (keepTrailingZeroes)
3024 while (trailingZeroes > 0)
3026 self->OutStream(self, digits[0]);
3031 /* Output exponent */
3032 if (exponentDigits > 0)
3034 self->OutStream(self,
3036 ? ((flags & FLAGS_UPPER) ? 'P' : 'p')
3037 : ((flags & FLAGS_UPPER) ? 'E' : 'e'));
3038 self->OutStream(self, (isExponentNegative) ? '-' : '+');
3040 /* The exponent must contain at least two digits */
3041 if (requireTwoDigitExponent)
3042 self->OutStream(self, '0');
3046 exponentBase = (int)TrioPower(base, exponentDigits - 1);
3047 for (i = 0; i < exponentDigits; i++)
3049 self->OutStream(self, digits[(uExponent / exponentBase) % base]);
3050 exponentBase /= base;
3053 /* Output trailing spaces */
3054 if (flags & FLAGS_LEFTADJUST)
3056 for (i = expectedWidth; i < width; i++)
3058 self->OutStream(self, CHAR_ADJUST);
3063 /*************************************************************************
3067 * This is the main engine for formatting output
3071 TRIO_ARGS3((data, format, parameters),
3073 TRIO_CONST char *format,
3074 trio_parameter_t *parameters)
3076 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3080 TRIO_CONST char *string;
3081 trio_pointer_t pointer;
3090 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3091 (void)mblen(NULL, 0);
3094 while (format[index])
3096 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3097 if (! isascii(format[index]))
3099 charlen = mblen(&format[index], MB_LEN_MAX);
3101 * Only valid multibyte characters are handled here. Invalid
3102 * multibyte characters (charlen == -1) are handled as normal
3107 while (charlen-- > 0)
3109 data->OutStream(data, format[index++]);
3111 continue; /* while characters left in formatting string */
3114 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
3115 if (CHAR_IDENTIFIER == format[index])
3117 if (CHAR_IDENTIFIER == format[index + 1])
3119 data->OutStream(data, CHAR_IDENTIFIER);
3124 /* Skip the parameter entries */
3125 while (parameters[i].type == FORMAT_PARAMETER)
3128 flags = parameters[i].flags;
3131 width = parameters[i].width;
3132 if (flags & FLAGS_WIDTH_PARAMETER)
3134 /* Get width from parameter list */
3135 width = (int)parameters[width].data.number.as_signed;
3139 * A negative width is the same as the - flag and
3142 flags |= FLAGS_LEFTADJUST;
3143 flags &= ~FLAGS_NILPADDING;
3148 /* Find precision */
3149 if (flags & FLAGS_PRECISION)
3151 precision = parameters[i].precision;
3152 if (flags & FLAGS_PRECISION_PARAMETER)
3154 /* Get precision from parameter list */
3155 precision = (int)parameters[precision].data.number.as_signed;
3159 * A negative precision is the same as no
3162 precision = NO_PRECISION;
3168 precision = NO_PRECISION;
3172 base = parameters[i].base;
3173 if (flags & FLAGS_BASE_PARAMETER)
3175 /* Get base from parameter list */
3176 base = (int)parameters[base].data.number.as_signed;
3179 switch (parameters[i].type)
3182 if (flags & FLAGS_QUOTE)
3183 data->OutStream(data, CHAR_QUOTE);
3184 if (! (flags & FLAGS_LEFTADJUST))
3187 data->OutStream(data, CHAR_ADJUST);
3190 if (flags & FLAGS_WIDECHAR)
3192 TrioWriteWideStringCharacter(data,
3193 (trio_wchar_t)parameters[i].data.number.as_signed,
3200 TrioWriteStringCharacter(data,
3201 (int)parameters[i].data.number.as_signed,
3205 if (flags & FLAGS_LEFTADJUST)
3208 data->OutStream(data, CHAR_ADJUST);
3210 if (flags & FLAGS_QUOTE)
3211 data->OutStream(data, CHAR_QUOTE);
3213 break; /* FORMAT_CHAR */
3216 TrioWriteNumber(data,
3217 parameters[i].data.number.as_unsigned,
3223 break; /* FORMAT_INT */
3226 TrioWriteDouble(data,
3227 parameters[i].data.longdoubleNumber,
3232 break; /* FORMAT_DOUBLE */
3236 if (flags & FLAGS_WIDECHAR)
3238 TrioWriteWideString(data,
3239 parameters[i].data.wstring,
3247 TrioWriteString(data,
3248 parameters[i].data.string,
3253 break; /* FORMAT_STRING */
3255 case FORMAT_POINTER:
3257 trio_reference_t reference;
3259 reference.data = data;
3260 reference.parameter = ¶meters[i];
3261 trio_print_pointer(&reference, parameters[i].data.pointer);
3263 break; /* FORMAT_POINTER */
3266 pointer = parameters[i].data.pointer;
3267 if (NULL != pointer)
3270 * C99 paragraph 7.19.6.1.8 says "the number of
3271 * characters written to the output stream so far by
3272 * this call", which is data->committed
3274 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
3275 if (flags & FLAGS_SIZE_T)
3276 *(size_t *)pointer = (size_t)data->committed;
3279 #if defined(QUALIFIER_PTRDIFF_T)
3280 if (flags & FLAGS_PTRDIFF_T)
3281 *(ptrdiff_t *)pointer = (ptrdiff_t)data->committed;
3284 #if defined(QUALIFIER_INTMAX_T)
3285 if (flags & FLAGS_INTMAX_T)
3286 *(trio_intmax_t *)pointer = (trio_intmax_t)data->committed;
3289 if (flags & FLAGS_QUAD)
3291 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)data->committed;
3293 else if (flags & FLAGS_LONG)
3295 *(long int *)pointer = (long int)data->committed;
3297 else if (flags & FLAGS_SHORT)
3299 *(short int *)pointer = (short int)data->committed;
3303 *(int *)pointer = (int)data->committed;
3306 break; /* FORMAT_COUNT */
3308 case FORMAT_PARAMETER:
3309 break; /* FORMAT_PARAMETER */
3311 #if defined(FORMAT_ERRNO)
3313 string = trio_error(parameters[i].data.errorNumber);
3316 TrioWriteString(data,
3324 data->OutStream(data, '#');
3325 TrioWriteNumber(data,
3326 (trio_uintmax_t)parameters[i].data.errorNumber,
3332 break; /* FORMAT_ERRNO */
3333 #endif /* defined(FORMAT_ERRNO) */
3335 #if defined(FORMAT_USER_DEFINED)
3336 case FORMAT_USER_DEFINED:
3338 trio_reference_t reference;
3339 trio_userdef_t *def = NULL;
3341 if (parameters[i].user_name[0] == NIL)
3345 (parameters[i - 1].type == FORMAT_PARAMETER))
3346 def = (trio_userdef_t *)parameters[i - 1].data.pointer;
3350 /* Look up namespace */
3351 def = TrioFindNamespace(parameters[i].user_name, NULL);
3354 reference.data = data;
3355 reference.parameter = ¶meters[i];
3356 def->callback(&reference);
3360 #endif /* defined(FORMAT_USER_DEFINED) */
3364 } /* switch parameter type */
3366 /* Prepare for next */
3367 index = parameters[i].indexAfterSpecifier;
3371 else /* not identifier */
3373 data->OutStream(data, format[index++]);
3376 return data->processed;
3379 /*************************************************************************
3384 TRIO_ARGS4((reference, format, arglist, argarray),
3385 trio_reference_t *reference,
3386 TRIO_CONST char *format,
3388 trio_pointer_t *argarray)
3391 trio_parameter_t parameters[MAX_PARAMETERS];
3393 status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
3397 status = TrioFormatProcess(reference->data, format, parameters);
3398 if (reference->data->error != 0)
3400 status = reference->data->error;
3405 /*************************************************************************
3410 TRIO_ARGS6((destination, destinationSize, OutStream, format, arglist, argarray),
3411 trio_pointer_t destination,
3412 size_t destinationSize,
3413 void (*OutStream) TRIO_PROTO((trio_class_t *, int)),
3414 TRIO_CONST char *format,
3416 trio_pointer_t *argarray)
3420 trio_parameter_t parameters[MAX_PARAMETERS];
3422 assert(VALID(OutStream));
3423 assert(VALID(format));
3425 memset(&data, 0, sizeof(data));
3426 data.OutStream = OutStream;
3427 data.location = destination;
3428 data.max = destinationSize;
3431 #if defined(USE_LOCALE)
3432 if (NULL == internalLocaleValues)
3438 status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
3442 status = TrioFormatProcess(&data, format, parameters);
3443 if (data.error != 0)
3445 status = data.error;
3450 /*************************************************************************
3455 TRIO_ARGS2((self, output),
3461 assert(VALID(self));
3462 assert(VALID(self->location));
3464 file = (FILE *)self->location;
3466 if (fputc(output, file) == EOF)
3468 self->error = TRIO_ERROR_RETURN(TRIO_EOF, 0);
3476 /*************************************************************************
3477 * TrioOutStreamFileDescriptor
3480 TrioOutStreamFileDescriptor
3481 TRIO_ARGS2((self, output),
3488 assert(VALID(self));
3490 fd = *((int *)self->location);
3493 if (write(fd, &ch, sizeof(char)) == -1)
3495 self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
3503 /*************************************************************************
3504 * TrioOutStreamCustom
3508 TRIO_ARGS2((self, output),
3513 trio_custom_t *data;
3515 assert(VALID(self));
3516 assert(VALID(self->location));
3518 data = (trio_custom_t *)self->location;
3519 if (data->stream.out)
3521 status = (data->stream.out)(data->closure, output);
3528 if (self->error == 0)
3530 self->error = TRIO_ERROR_RETURN(TRIO_ECUSTOM, -status);
3537 /*************************************************************************
3538 * TrioOutStreamString
3542 TRIO_ARGS2((self, output),
3548 assert(VALID(self));
3549 assert(VALID(self->location));
3551 buffer = (char **)self->location;
3552 **buffer = (char)output;
3558 /*************************************************************************
3559 * TrioOutStreamStringMax
3562 TrioOutStreamStringMax
3563 TRIO_ARGS2((self, output),
3569 assert(VALID(self));
3570 assert(VALID(self->location));
3572 buffer = (char **)self->location;
3574 if (self->processed < self->max)
3576 **buffer = (char)output;
3583 /*************************************************************************
3584 * TrioOutStreamStringDynamic
3587 TrioOutStreamStringDynamic
3588 TRIO_ARGS2((self, output),
3592 assert(VALID(self));
3593 assert(VALID(self->location));
3595 if (self->error == 0)
3597 trio_xstring_append_char((trio_string_t *)self->location,
3601 /* The processed variable must always be increased */
3605 /*************************************************************************
3607 * Formatted printing functions
3609 ************************************************************************/
3611 #if defined(TRIO_DOCUMENTATION)
3612 # include "doc/doc_printf.h"
3614 /** @addtogroup Printf
3618 /*************************************************************************
3623 Print to standard output stream.
3625 @param format Formatting string.
3626 @param ... Arguments.
3627 @return Number of printed characters.
3631 TRIO_VARGS2((format, va_alist),
3632 TRIO_CONST char *format,
3638 assert(VALID(format));
3640 TRIO_VA_START(args, format);
3641 status = TrioFormat(stdout, 0, TrioOutStreamFile, format, &args, NULL);
3647 Print to standard output stream.
3649 @param format Formatting string.
3650 @param args Arguments.
3651 @return Number of printed characters.
3655 TRIO_ARGS2((format, args),
3656 TRIO_CONST char *format,
3659 assert(VALID(format));
3661 return TrioFormat(stdout, 0, TrioOutStreamFile, format, &args, NULL);
3665 Print to standard output stream.
3667 @param format Formatting string.
3668 @param args Arguments.
3669 @return Number of printed characters.
3673 TRIO_ARGS2((format, args),
3674 TRIO_CONST char *format,
3675 trio_pointer_t * args)
3677 assert(VALID(format));
3679 return TrioFormat(stdout, 0, TrioOutStreamFile, format, NULL, args);
3682 /*************************************************************************
3689 @param file File pointer.
3690 @param format Formatting string.
3691 @param ... Arguments.
3692 @return Number of printed characters.
3696 TRIO_VARGS3((file, format, va_alist),
3698 TRIO_CONST char *format,
3704 assert(VALID(file));
3705 assert(VALID(format));
3707 TRIO_VA_START(args, format);
3708 status = TrioFormat(file, 0, TrioOutStreamFile, format, &args, NULL);
3716 @param file File pointer.
3717 @param format Formatting string.
3718 @param args Arguments.
3719 @return Number of printed characters.
3723 TRIO_ARGS3((file, format, args),
3725 TRIO_CONST char *format,
3728 assert(VALID(file));
3729 assert(VALID(format));
3731 return TrioFormat(file, 0, TrioOutStreamFile, format, &args, NULL);
3737 @param file File pointer.
3738 @param format Formatting string.
3739 @param args Arguments.
3740 @return Number of printed characters.
3744 TRIO_ARGS3((file, format, args),
3746 TRIO_CONST char *format,
3747 trio_pointer_t * args)
3749 assert(VALID(file));
3750 assert(VALID(format));
3752 return TrioFormat(file, 0, TrioOutStreamFile, format, NULL, args);
3755 /*************************************************************************
3760 Print to file descriptor.
3762 @param fd File descriptor.
3763 @param format Formatting string.
3764 @param ... Arguments.
3765 @return Number of printed characters.
3769 TRIO_VARGS3((fd, format, va_alist),
3771 TRIO_CONST char *format,
3777 assert(VALID(format));
3779 TRIO_VA_START(args, format);
3780 status = TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, &args, NULL);
3786 Print to file descriptor.
3788 @param fd File descriptor.
3789 @param format Formatting string.
3790 @param args Arguments.
3791 @return Number of printed characters.
3795 TRIO_ARGS3((fd, format, args),
3797 TRIO_CONST char *format,
3800 assert(VALID(format));
3802 return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, &args, NULL);
3806 Print to file descriptor.
3808 @param fd File descriptor.
3809 @param format Formatting string.
3810 @param args Arguments.
3811 @return Number of printed characters.
3815 TRIO_ARGS3((fd, format, args),
3817 TRIO_CONST char *format,
3818 trio_pointer_t *args)
3820 assert(VALID(format));
3822 return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, NULL, args);
3825 /*************************************************************************
3830 TRIO_VARGS4((stream, closure, format, va_alist),
3831 trio_outstream_t stream,
3832 trio_pointer_t closure,
3833 TRIO_CONST char *format,
3840 assert(VALID(stream));
3841 assert(VALID(format));
3843 TRIO_VA_START(args, format);
3844 data.stream.out = stream;
3845 data.closure = closure;
3846 status = TrioFormat(&data, 0, TrioOutStreamCustom, format, &args, NULL);
3853 TRIO_ARGS4((stream, closure, format, args),
3854 trio_outstream_t stream,
3855 trio_pointer_t closure,
3856 TRIO_CONST char *format,
3861 assert(VALID(stream));
3862 assert(VALID(format));
3864 data.stream.out = stream;
3865 data.closure = closure;
3866 return TrioFormat(&data, 0, TrioOutStreamCustom, format, &args, NULL);
3871 TRIO_ARGS4((stream, closure, format, args),
3872 trio_outstream_t stream,
3873 trio_pointer_t closure,
3874 TRIO_CONST char *format,
3879 assert(VALID(stream));
3880 assert(VALID(format));
3882 data.stream.out = stream;
3883 data.closure = closure;
3884 return TrioFormat(&data, 0, TrioOutStreamCustom, format, NULL, args);
3887 /*************************************************************************
3894 @param buffer Output string.
3895 @param format Formatting string.
3896 @param ... Arguments.
3897 @return Number of printed characters.
3901 TRIO_VARGS3((buffer, format, va_alist),
3903 TRIO_CONST char *format,
3909 assert(VALID(buffer));
3910 assert(VALID(format));
3912 TRIO_VA_START(args, format);
3913 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, &args, NULL);
3914 *buffer = NIL; /* Terminate with NIL character */
3922 @param buffer Output string.
3923 @param format Formatting string.
3924 @param args Arguments.
3925 @return Number of printed characters.
3929 TRIO_ARGS3((buffer, format, args),
3931 TRIO_CONST char *format,
3936 assert(VALID(buffer));
3937 assert(VALID(format));
3939 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, &args, NULL);
3947 @param buffer Output string.
3948 @param format Formatting string.
3949 @param args Arguments.
3950 @return Number of printed characters.
3954 TRIO_ARGS3((buffer, format, args),
3956 TRIO_CONST char *format,
3957 trio_pointer_t *args)
3961 assert(VALID(buffer));
3962 assert(VALID(format));
3964 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, NULL, args);
3969 /*************************************************************************
3974 Print at most @p max characters to string.
3976 @param buffer Output string.
3977 @param max Maximum number of characters to print.
3978 @param format Formatting string.
3979 @param ... Arguments.
3980 @return Number of printed characters.
3984 TRIO_VARGS4((buffer, max, format, va_alist),
3987 TRIO_CONST char *format,
3993 assert(VALID(buffer));
3994 assert(VALID(format));
3996 TRIO_VA_START(args, format);
3997 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
3998 TrioOutStreamStringMax, format, &args, NULL);
4006 Print at most @p max characters to string.
4008 @param buffer Output string.
4009 @param max Maximum number of characters to print.
4010 @param format Formatting string.
4011 @param args Arguments.
4012 @return Number of printed characters.
4016 TRIO_ARGS4((buffer, max, format, args),
4019 TRIO_CONST char *format,
4024 assert(VALID(buffer));
4025 assert(VALID(format));
4027 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
4028 TrioOutStreamStringMax, format, &args, NULL);
4035 Print at most @p max characters to string.
4037 @param buffer Output string.
4038 @param max Maximum number of characters to print.
4039 @param format Formatting string.
4040 @param args Arguments.
4041 @return Number of printed characters.
4045 TRIO_ARGS4((buffer, max, format, args),
4048 TRIO_CONST char *format,
4049 trio_pointer_t *args)
4053 assert(VALID(buffer));
4054 assert(VALID(format));
4056 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
4057 TrioOutStreamStringMax, format, NULL, args);
4063 /*************************************************************************
4065 * Appends the new string to the buffer string overwriting the '\0'
4066 * character at the end of buffer.
4070 TRIO_VARGS4((buffer, max, format, va_alist),
4073 TRIO_CONST char *format,
4080 TRIO_VA_START(args, format);
4082 assert(VALID(buffer));
4083 assert(VALID(format));
4085 buf_len = trio_length(buffer);
4086 buffer = &buffer[buf_len];
4088 status = TrioFormat(&buffer, max - 1 - buf_len,
4089 TrioOutStreamStringMax, format, &args, NULL);
4097 TRIO_ARGS4((buffer, max, format, args),
4100 TRIO_CONST char *format,
4106 assert(VALID(buffer));
4107 assert(VALID(format));
4109 buf_len = trio_length(buffer);
4110 buffer = &buffer[buf_len];
4111 status = TrioFormat(&buffer, max - 1 - buf_len,
4112 TrioOutStreamStringMax, format, &args, NULL);
4117 /*************************************************************************
4124 TRIO_VARGS2((format, va_alist),
4125 TRIO_CONST char *format,
4129 trio_string_t *info;
4130 char *result = NULL;
4132 assert(VALID(format));
4134 info = trio_xstring_duplicate("");
4137 TRIO_VA_START(args, format);
4138 (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
4139 format, &args, NULL);
4142 trio_string_terminate(info);
4143 result = trio_string_extract(info);
4144 trio_string_destroy(info);
4152 TRIO_ARGS2((format, args),
4153 TRIO_CONST char *format,
4156 trio_string_t *info;
4157 char *result = NULL;
4159 assert(VALID(format));
4161 info = trio_xstring_duplicate("");
4164 (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
4165 format, &args, NULL);
4166 trio_string_terminate(info);
4167 result = trio_string_extract(info);
4168 trio_string_destroy(info);
4175 TRIO_VARGS3((result, format, va_alist),
4177 TRIO_CONST char *format,
4182 trio_string_t *info;
4184 assert(VALID(format));
4188 info = trio_xstring_duplicate("");
4191 status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
4195 TRIO_VA_START(args, format);
4196 status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
4197 format, &args, NULL);
4201 trio_string_terminate(info);
4202 *result = trio_string_extract(info);
4204 trio_string_destroy(info);
4211 TRIO_ARGS3((result, format, args),
4213 TRIO_CONST char *format,
4217 trio_string_t *info;
4219 assert(VALID(format));
4223 info = trio_xstring_duplicate("");
4226 status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
4230 status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
4231 format, &args, NULL);
4234 trio_string_terminate(info);
4235 *result = trio_string_extract(info);
4237 trio_string_destroy(info);
4242 /** @} End of Printf documentation module */
4244 /*************************************************************************
4248 ************************************************************************/
4250 #if defined(TRIO_DOCUMENTATION)
4251 # include "doc/doc_register.h"
4254 @addtogroup UserDefined
4260 /*************************************************************************
4265 Register new user-defined specifier.
4271 TRIO_PUBLIC trio_pointer_t
4273 TRIO_ARGS2((callback, name),
4274 trio_callback_t callback,
4275 TRIO_CONST char *name)
4277 trio_userdef_t *def;
4278 trio_userdef_t *prev = NULL;
4280 if (callback == NULL)
4285 /* Handle built-in namespaces */
4288 if (trio_equal(name, ":enter"))
4290 internalEnterCriticalRegion = callback;
4292 else if (trio_equal(name, ":leave"))
4294 internalLeaveCriticalRegion = callback;
4299 /* Bail out if namespace is too long */
4300 if (trio_length(name) >= MAX_USER_NAME)
4303 /* Bail out if namespace already is registered */
4304 def = TrioFindNamespace(name, &prev);
4309 def = (trio_userdef_t *)TRIO_MALLOC(sizeof(trio_userdef_t));
4312 if (internalEnterCriticalRegion)
4313 (void)internalEnterCriticalRegion(NULL);
4317 /* Link into internal list */
4319 internalUserDef = def;
4324 def->callback = callback;
4325 def->name = (name == NULL)
4327 : trio_duplicate(name);
4330 if (internalLeaveCriticalRegion)
4331 (void)internalLeaveCriticalRegion(NULL);
4333 return (trio_pointer_t)def;
4337 Unregister an existing user-defined specifier.
4343 TRIO_ARGS1((handle),
4344 trio_pointer_t handle)
4346 trio_userdef_t *self = (trio_userdef_t *)handle;
4347 trio_userdef_t *def;
4348 trio_userdef_t *prev = NULL;
4350 assert(VALID(self));
4354 def = TrioFindNamespace(self->name, &prev);
4357 if (internalEnterCriticalRegion)
4358 (void)internalEnterCriticalRegion(NULL);
4361 internalUserDef = NULL;
4363 prev->next = def->next;
4365 if (internalLeaveCriticalRegion)
4366 (void)internalLeaveCriticalRegion(NULL);
4368 trio_destroy(self->name);
4373 /*************************************************************************
4374 * trio_get_format [public]
4381 #if defined(FORMAT_USER_DEFINED)
4382 assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
4385 return (((trio_reference_t *)ref)->parameter->user_data);
4388 /*************************************************************************
4389 * trio_get_argument [public]
4396 #if defined(FORMAT_USER_DEFINED)
4397 assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
4400 return ((trio_reference_t *)ref)->parameter->data.pointer;
4403 /*************************************************************************
4404 * trio_get_width / trio_set_width [public]
4411 return ((trio_reference_t *)ref)->parameter->width;
4416 TRIO_ARGS2((ref, width),
4420 ((trio_reference_t *)ref)->parameter->width = width;
4423 /*************************************************************************
4424 * trio_get_precision / trio_set_precision [public]
4431 return (((trio_reference_t *)ref)->parameter->precision);
4436 TRIO_ARGS2((ref, precision),
4440 ((trio_reference_t *)ref)->parameter->precision = precision;
4443 /*************************************************************************
4444 * trio_get_base / trio_set_base [public]
4451 return (((trio_reference_t *)ref)->parameter->base);
4456 TRIO_ARGS2((ref, base),
4460 ((trio_reference_t *)ref)->parameter->base = base;
4463 /*************************************************************************
4464 * trio_get_long / trio_set_long [public]
4471 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONG)
4478 TRIO_ARGS2((ref, is_long),
4483 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONG;
4485 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONG;
4488 /*************************************************************************
4489 * trio_get_longlong / trio_set_longlong [public]
4496 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUAD)
4503 TRIO_ARGS2((ref, is_longlong),
4508 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUAD;
4510 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUAD;
4513 /*************************************************************************
4514 * trio_get_longdouble / trio_set_longdouble [public]
4521 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONGDOUBLE)
4528 TRIO_ARGS2((ref, is_longdouble),
4533 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONGDOUBLE;
4535 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONGDOUBLE;
4538 /*************************************************************************
4539 * trio_get_short / trio_set_short [public]
4546 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORT)
4553 TRIO_ARGS2((ref, is_short),
4558 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORT;
4560 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORT;
4563 /*************************************************************************
4564 * trio_get_shortshort / trio_set_shortshort [public]
4571 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORTSHORT)
4578 TRIO_ARGS2((ref, is_shortshort),
4583 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORTSHORT;
4585 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORTSHORT;
4588 /*************************************************************************
4589 * trio_get_alternative / trio_set_alternative [public]
4592 trio_get_alternative
4596 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_ALTERNATIVE)
4602 trio_set_alternative
4603 TRIO_ARGS2((ref, is_alternative),
4608 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_ALTERNATIVE;
4610 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_ALTERNATIVE;
4613 /*************************************************************************
4614 * trio_get_alignment / trio_set_alignment [public]
4621 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LEFTADJUST)
4628 TRIO_ARGS2((ref, is_leftaligned),
4633 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LEFTADJUST;
4635 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LEFTADJUST;
4638 /*************************************************************************
4639 * trio_get_spacing /trio_set_spacing [public]
4646 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SPACE)
4653 TRIO_ARGS2((ref, is_space),
4658 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SPACE;
4660 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SPACE;
4663 /*************************************************************************
4664 * trio_get_sign / trio_set_sign [public]
4671 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHOWSIGN)
4678 TRIO_ARGS2((ref, is_sign),
4683 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHOWSIGN;
4685 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHOWSIGN;
4688 /*************************************************************************
4689 * trio_get_padding / trio_set_padding [public]
4696 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_NILPADDING)
4703 TRIO_ARGS2((ref, is_padding),
4708 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_NILPADDING;
4710 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_NILPADDING;
4713 /*************************************************************************
4714 * trio_get_quote / trio_set_quote [public]
4721 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUOTE)
4728 TRIO_ARGS2((ref, is_quote),
4733 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUOTE;
4735 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUOTE;
4738 /*************************************************************************
4739 * trio_get_upper / trio_set_upper [public]
4746 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_UPPER)
4753 TRIO_ARGS2((ref, is_upper),
4758 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_UPPER;
4760 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_UPPER;
4763 /*************************************************************************
4764 * trio_get_largest / trio_set_largest [public]
4772 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_INTMAX_T)
4779 TRIO_ARGS2((ref, is_largest),
4784 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_INTMAX_T;
4786 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_INTMAX_T;
4790 /*************************************************************************
4791 * trio_get_ptrdiff / trio_set_ptrdiff [public]
4798 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_PTRDIFF_T)
4805 TRIO_ARGS2((ref, is_ptrdiff),
4810 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_PTRDIFF_T;
4812 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_PTRDIFF_T;
4815 /*************************************************************************
4816 * trio_get_size / trio_set_size [public]
4824 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SIZE_T)
4831 TRIO_ARGS2((ref, is_size),
4836 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SIZE_T;
4838 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SIZE_T;
4842 /*************************************************************************
4843 * trio_print_int [public]
4847 TRIO_ARGS2((ref, number),
4851 trio_reference_t *self = (trio_reference_t *)ref;
4853 TrioWriteNumber(self->data,
4854 (trio_uintmax_t)number,
4855 self->parameter->flags,
4856 self->parameter->width,
4857 self->parameter->precision,
4858 self->parameter->base);
4861 /*************************************************************************
4862 * trio_print_uint [public]
4866 TRIO_ARGS2((ref, number),
4868 unsigned int number)
4870 trio_reference_t *self = (trio_reference_t *)ref;
4872 TrioWriteNumber(self->data,
4873 (trio_uintmax_t)number,
4874 self->parameter->flags | FLAGS_UNSIGNED,
4875 self->parameter->width,
4876 self->parameter->precision,
4877 self->parameter->base);
4880 /*************************************************************************
4881 * trio_print_double [public]
4885 TRIO_ARGS2((ref, number),
4889 trio_reference_t *self = (trio_reference_t *)ref;
4891 TrioWriteDouble(self->data,
4893 self->parameter->flags,
4894 self->parameter->width,
4895 self->parameter->precision,
4896 self->parameter->base);
4899 /*************************************************************************
4900 * trio_print_string [public]
4904 TRIO_ARGS2((ref, string),
4908 trio_reference_t *self = (trio_reference_t *)ref;
4910 TrioWriteString(self->data,
4912 self->parameter->flags,
4913 self->parameter->width,
4914 self->parameter->precision);
4917 /*************************************************************************
4918 * trio_print_ref [public]
4922 TRIO_VARGS3((ref, format, va_alist),
4924 TRIO_CONST char *format,
4930 assert(VALID(format));
4932 TRIO_VA_START(arglist, format);
4933 status = TrioFormatRef((trio_reference_t *)ref, format, &arglist, NULL);
4934 TRIO_VA_END(arglist);
4938 /*************************************************************************
4939 * trio_vprint_ref [public]
4943 TRIO_ARGS3((ref, format, arglist),
4945 TRIO_CONST char *format,
4948 assert(VALID(format));
4950 return TrioFormatRef((trio_reference_t *)ref, format, &arglist, NULL);
4953 /*************************************************************************
4954 * trio_printv_ref [public]
4958 TRIO_ARGS3((ref, format, argarray),
4960 TRIO_CONST char *format,
4961 trio_pointer_t *argarray)
4963 assert(VALID(format));
4965 return TrioFormatRef((trio_reference_t *)ref, format, NULL, argarray);
4968 #endif /* TRIO_EXTENSION */
4970 /*************************************************************************
4971 * trio_print_pointer [public]
4975 TRIO_ARGS2((ref, pointer),
4977 trio_pointer_t pointer)
4979 trio_reference_t *self = (trio_reference_t *)ref;
4981 trio_uintmax_t number;
4983 if (NULL == pointer)
4985 TRIO_CONST char *string = internalNullString;
4987 self->data->OutStream(self->data, *string++);
4992 * The subtraction of the null pointer is a workaround
4993 * to avoid a compiler warning. The performance overhead
4994 * is negligible (and likely to be removed by an
4995 * optimizing compiler). The (char *) casting is done
4996 * to please ANSI C++.
4998 number = (trio_uintmax_t)((char *)pointer - (char *)0);
4999 /* Shrink to size of pointer */
5000 number &= (trio_uintmax_t)-1;
5001 flags = self->parameter->flags;
5002 flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE |
5004 TrioWriteNumber(self->data,
5013 /** @} End of UserDefined documentation module */
5015 /*************************************************************************
5019 ************************************************************************/
5021 /*************************************************************************
5022 * trio_locale_set_decimal_point
5024 * Decimal point can only be one character. The input argument is a
5025 * string to enable multibyte characters. At most MB_LEN_MAX characters
5029 trio_locale_set_decimal_point
5030 TRIO_ARGS1((decimalPoint),
5033 #if defined(USE_LOCALE)
5034 if (NULL == internalLocaleValues)
5039 internalDecimalPointLength = trio_length(decimalPoint);
5040 if (internalDecimalPointLength == 1)
5042 internalDecimalPoint = *decimalPoint;
5046 internalDecimalPoint = NIL;
5047 trio_copy_max(internalDecimalPointString,
5048 sizeof(internalDecimalPointString),
5053 /*************************************************************************
5054 * trio_locale_set_thousand_separator
5056 * See trio_locale_set_decimal_point
5059 trio_locale_set_thousand_separator
5060 TRIO_ARGS1((thousandSeparator),
5061 char *thousandSeparator)
5063 #if defined(USE_LOCALE)
5064 if (NULL == internalLocaleValues)
5069 trio_copy_max(internalThousandSeparator,
5070 sizeof(internalThousandSeparator),
5072 internalThousandSeparatorLength = trio_length(internalThousandSeparator);
5075 /*************************************************************************
5076 * trio_locale_set_grouping
5078 * Array of bytes. Reversed order.
5080 * CHAR_MAX : No further grouping
5081 * 0 : Repeat last group for the remaining digits (not necessary
5082 * as C strings are zero-terminated)
5083 * n : Set current group to n
5085 * Same order as the grouping attribute in LC_NUMERIC.
5088 trio_locale_set_grouping
5089 TRIO_ARGS1((grouping),
5092 #if defined(USE_LOCALE)
5093 if (NULL == internalLocaleValues)
5098 trio_copy_max(internalGrouping,
5099 sizeof(internalGrouping),
5104 /*************************************************************************
5108 ************************************************************************/
5110 /*************************************************************************
5111 * TrioSkipWhitespaces
5123 self->InStream(self, &ch);
5128 /*************************************************************************
5133 TrioGetCollation(TRIO_NOARGS)
5141 /* This is computationally expensive */
5144 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5148 for (j = 0; j < MAX_CHARACTER_CLASS; j++)
5150 second[0] = (char)j;
5151 if (trio_equal_locale(first, second))
5152 internalCollationArray[i][k++] = (char)j;
5154 internalCollationArray[i][k] = NIL;
5159 /*************************************************************************
5160 * TrioGetCharacterClass
5166 TrioGetCharacterClass
5167 TRIO_ARGS4((format, indexPointer, flagsPointer, characterclass),
5168 TRIO_CONST char *format,
5170 trio_flags_t *flagsPointer,
5171 int *characterclass)
5173 int index = *indexPointer;
5179 *flagsPointer &= ~FLAGS_EXCLUDE;
5181 if (format[index] == QUALIFIER_CIRCUMFLEX)
5183 *flagsPointer |= FLAGS_EXCLUDE;
5187 * If the ungroup character is at the beginning of the scanlist,
5188 * it will be part of the class, and a second ungroup character
5189 * must follow to end the group.
5191 if (format[index] == SPECIFIER_UNGROUP)
5193 characterclass[(int)SPECIFIER_UNGROUP]++;
5197 * Minus is used to specify ranges. To include minus in the class,
5198 * it must be at the beginning of the list
5200 if (format[index] == QUALIFIER_MINUS)
5202 characterclass[(int)QUALIFIER_MINUS]++;
5205 /* Collect characters */
5206 for (ch = format[index];
5207 (ch != SPECIFIER_UNGROUP) && (ch != NIL);
5208 ch = format[++index])
5212 case QUALIFIER_MINUS: /* Scanlist ranges */
5215 * Both C99 and UNIX98 describes ranges as implementation-
5218 * We support the following behaviour (although this may
5219 * change as we become wiser)
5220 * - only increasing ranges, ie. [a-b] but not [b-a]
5221 * - transitive ranges, ie. [a-b-c] == [a-c]
5222 * - trailing minus, ie. [a-] is interpreted as an 'a'
5224 * - duplicates (although we can easily convert these
5227 range_begin = format[index - 1];
5228 range_end = format[++index];
5229 if (range_end == SPECIFIER_UNGROUP)
5231 /* Trailing minus is included */
5232 characterclass[(int)ch]++;
5236 if (range_end == NIL)
5237 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
5238 if (range_begin > range_end)
5239 return TRIO_ERROR_RETURN(TRIO_ERANGE, index);
5241 for (i = (int)range_begin; i <= (int)range_end; i++)
5242 characterclass[i]++;
5249 case SPECIFIER_GROUP:
5251 switch (format[index + 1])
5253 case QUALIFIER_DOT: /* Collating symbol */
5255 * FIXME: This will be easier to implement when multibyte
5256 * characters have been implemented. Until now, we ignore
5259 for (i = index + 2; ; i++)
5261 if (format[i] == NIL)
5262 /* Error in syntax */
5264 else if (format[i] == QUALIFIER_DOT)
5267 if (format[++i] != SPECIFIER_UNGROUP)
5273 case QUALIFIER_EQUAL: /* Equivalence class expressions */
5278 if (internalCollationUnconverted)
5280 /* Lazy evaluation of collation array */
5282 internalCollationUnconverted = FALSE;
5284 for (i = index + 2; ; i++)
5286 if (format[i] == NIL)
5287 /* Error in syntax */
5289 else if (format[i] == QUALIFIER_EQUAL)
5293 /* Mark any equivalent character */
5294 k = (unsigned int)format[i];
5295 for (j = 0; internalCollationArray[k][j] != NIL; j++)
5296 characterclass[(int)internalCollationArray[k][j]]++;
5299 if (format[++i] != SPECIFIER_UNGROUP)
5306 case QUALIFIER_COLON: /* Character class expressions */
5308 if (trio_equal_max(CLASS_ALNUM, sizeof(CLASS_ALNUM) - 1,
5311 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5313 characterclass[i]++;
5314 index += sizeof(CLASS_ALNUM) - 1;
5316 else if (trio_equal_max(CLASS_ALPHA, sizeof(CLASS_ALPHA) - 1,
5319 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5321 characterclass[i]++;
5322 index += sizeof(CLASS_ALPHA) - 1;
5324 else if (trio_equal_max(CLASS_CNTRL, sizeof(CLASS_CNTRL) - 1,
5327 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5329 characterclass[i]++;
5330 index += sizeof(CLASS_CNTRL) - 1;
5332 else if (trio_equal_max(CLASS_DIGIT, sizeof(CLASS_DIGIT) - 1,
5335 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5337 characterclass[i]++;
5338 index += sizeof(CLASS_DIGIT) - 1;
5340 else if (trio_equal_max(CLASS_GRAPH, sizeof(CLASS_GRAPH) - 1,
5343 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5345 characterclass[i]++;
5346 index += sizeof(CLASS_GRAPH) - 1;
5348 else if (trio_equal_max(CLASS_LOWER, sizeof(CLASS_LOWER) - 1,
5351 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5353 characterclass[i]++;
5354 index += sizeof(CLASS_LOWER) - 1;
5356 else if (trio_equal_max(CLASS_PRINT, sizeof(CLASS_PRINT) - 1,
5359 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5361 characterclass[i]++;
5362 index += sizeof(CLASS_PRINT) - 1;
5364 else if (trio_equal_max(CLASS_PUNCT, sizeof(CLASS_PUNCT) - 1,
5367 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5369 characterclass[i]++;
5370 index += sizeof(CLASS_PUNCT) - 1;
5372 else if (trio_equal_max(CLASS_SPACE, sizeof(CLASS_SPACE) - 1,
5375 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5377 characterclass[i]++;
5378 index += sizeof(CLASS_SPACE) - 1;
5380 else if (trio_equal_max(CLASS_UPPER, sizeof(CLASS_UPPER) - 1,
5383 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5385 characterclass[i]++;
5386 index += sizeof(CLASS_UPPER) - 1;
5388 else if (trio_equal_max(CLASS_XDIGIT, sizeof(CLASS_XDIGIT) - 1,
5391 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5393 characterclass[i]++;
5394 index += sizeof(CLASS_XDIGIT) - 1;
5398 characterclass[(int)ch]++;
5403 characterclass[(int)ch]++;
5408 #endif /* TRIO_EXTENSION */
5411 characterclass[(int)ch]++;
5418 /*************************************************************************
5421 * We implement our own number conversion in preference of strtol and
5422 * strtoul, because we must handle 'long long' and thousand separators.
5424 TRIO_PRIVATE BOOLEAN_T
5426 TRIO_ARGS5((self, target, flags, width, base),
5428 trio_uintmax_t *target,
5433 trio_uintmax_t number = 0;
5436 BOOLEAN_T isNegative = FALSE;
5437 BOOLEAN_T gotNumber = FALSE;
5440 assert(VALID(self));
5441 assert(VALID(self->InStream));
5442 assert((base >= MIN_BASE && base <= MAX_BASE) || (base == NO_BASE));
5444 if (internalDigitsUnconverted)
5446 /* Lazy evaluation of digits array */
5447 memset(internalDigitArray, -1, sizeof(internalDigitArray));
5448 for (j = 0; j < (int)sizeof(internalDigitsLower) - 1; j++)
5450 internalDigitArray[(int)internalDigitsLower[j]] = j;
5451 internalDigitArray[(int)internalDigitsUpper[j]] = j;
5453 internalDigitsUnconverted = FALSE;
5456 TrioSkipWhitespaces(self);
5458 if (!(flags & FLAGS_UNSIGNED))
5461 if (self->current == '+')
5463 self->InStream(self, NULL);
5465 else if (self->current == '-')
5467 self->InStream(self, NULL);
5472 count = self->processed;
5474 if (flags & FLAGS_ALTERNATIVE)
5482 if (self->current == '0')
5484 self->InStream(self, NULL);
5487 if ((base == BASE_HEX) &&
5488 (trio_to_upper(self->current) == 'X'))
5490 self->InStream(self, NULL);
5492 else if ((base == BASE_BINARY) &&
5493 (trio_to_upper(self->current) == 'B'))
5495 self->InStream(self, NULL);
5507 while (((width == NO_WIDTH) || (self->processed - count < width)) &&
5508 (! ((self->current == EOF) || isspace(self->current))))
5510 if (isascii(self->current))
5512 digit = internalDigitArray[self->current];
5513 /* Abort if digit is not allowed in the specified base */
5514 if ((digit == -1) || (digit >= base))
5517 else if (flags & FLAGS_QUOTE)
5519 /* Compare with thousands separator */
5520 for (j = 0; internalThousandSeparator[j] && self->current; j++)
5522 if (internalThousandSeparator[j] != self->current)
5525 self->InStream(self, NULL);
5527 if (internalThousandSeparator[j])
5528 break; /* Mismatch */
5530 continue; /* Match */
5537 gotNumber = TRUE; /* we need at least one digit */
5539 self->InStream(self, NULL);
5542 /* Was anything read at all? */
5547 *target = (isNegative) ? -((trio_intmax_t)number) : number;
5551 /*************************************************************************
5556 TRIO_ARGS4((self, target, flags, width),
5564 trio_uintmax_t number;
5566 assert(VALID(self));
5567 assert(VALID(self->InStream));
5570 (self->current != EOF) && (i < width);
5573 ch = (char)self->current;
5574 self->InStream(self, NULL);
5575 if ((flags & FLAGS_ALTERNATIVE) && (ch == CHAR_BACKSLASH))
5577 switch (self->current)
5579 case '\\': ch = '\\'; break;
5580 case 'a': ch = '\007'; break;
5581 case 'b': ch = '\b'; break;
5582 case 'f': ch = '\f'; break;
5583 case 'n': ch = '\n'; break;
5584 case 'r': ch = '\r'; break;
5585 case 't': ch = '\t'; break;
5586 case 'v': ch = '\v'; break;
5588 if (isdigit(self->current))
5590 /* Read octal number */
5591 if (!TrioReadNumber(self, &number, 0, 3, BASE_OCTAL))
5595 else if (trio_to_upper(self->current) == 'X')
5597 /* Read hexadecimal number */
5598 self->InStream(self, NULL);
5599 if (!TrioReadNumber(self, &number, 0, 2, BASE_HEX))
5605 ch = (char)self->current;
5617 /*************************************************************************
5620 TRIO_PRIVATE BOOLEAN_T
5622 TRIO_ARGS4((self, target, flags, width),
5630 assert(VALID(self));
5631 assert(VALID(self->InStream));
5633 TrioSkipWhitespaces(self);
5636 * Continue until end of string is reached, a whitespace is encountered,
5637 * or width is exceeded
5640 ((width == NO_WIDTH) || (i < width)) &&
5641 (! ((self->current == EOF) || isspace(self->current)));
5644 if (TrioReadChar(self, (target ? &target[i] : 0), flags, 1) == 0)
5652 /*************************************************************************
5658 TRIO_ARGS4((self, target, flags, width),
5660 trio_wchar_t *target,
5669 char buffer[MB_LEN_MAX + 1];
5671 assert(VALID(self));
5672 assert(VALID(self->InStream));
5675 (self->current != EOF) && (i < width);
5678 if (isascii(self->current))
5680 if (TrioReadChar(self, buffer, flags, 1) == 0)
5687 * Collect a multibyte character, by enlarging buffer until
5688 * it contains a fully legal multibyte character, or the
5694 buffer[j++] = (char)self->current;
5696 self->InStream(self, NULL);
5698 while ((j < (int)sizeof(buffer)) && (mblen(buffer, (size_t)j) != j));
5702 size = mbtowc(&wch, buffer, sizeof(buffer));
5707 self->InStream(self, NULL);
5711 #endif /* TRIO_WIDECHAR */
5713 /*************************************************************************
5714 * TrioReadWideString
5717 TRIO_PRIVATE BOOLEAN_T
5719 TRIO_ARGS4((self, target, flags, width),
5721 trio_wchar_t *target,
5728 assert(VALID(self));
5729 assert(VALID(self->InStream));
5731 TrioSkipWhitespaces(self);
5733 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5734 (void)mblen(NULL, 0);
5738 * Continue until end of string is reached, a whitespace is encountered,
5739 * or width is exceeded
5742 ((width == NO_WIDTH) || (i < width)) &&
5743 (! ((self->current == EOF) || isspace(self->current)));
5746 size = TrioReadWideChar(self, &target[i], flags, 1);
5753 target[i] = WCONST('\0');
5756 #endif /* TRIO_WIDECHAR */
5758 /*************************************************************************
5761 * FIXME: characterclass does not work with multibyte characters
5763 TRIO_PRIVATE BOOLEAN_T
5765 TRIO_ARGS5((self, target, characterclass, flags, width),
5768 int *characterclass,
5775 assert(VALID(self));
5776 assert(VALID(self->InStream));
5780 ((width == NO_WIDTH) || (i < width)) &&
5782 (((flags & FLAGS_EXCLUDE) != 0) ^ (characterclass[ch] == 0))));
5786 target[i] = (char)ch;
5787 self->InStream(self, &ch);
5795 /*************************************************************************
5802 TRIO_PRIVATE BOOLEAN_T
5804 TRIO_ARGS4((self, target, flags, width),
5806 trio_pointer_t target,
5811 char doubleString[512];
5815 BOOLEAN_T isHex = FALSE;
5817 doubleString[0] = 0;
5819 if ((width == NO_WIDTH) || (width > (int)sizeof(doubleString) - 1))
5820 width = sizeof(doubleString) - 1;
5822 TrioSkipWhitespaces(self);
5825 * Read entire double number from stream. trio_to_double requires
5826 * a string as input, but InStream can be anything, so we have to
5827 * collect all characters.
5830 if ((ch == '+') || (ch == '-'))
5832 doubleString[index++] = (char)ch;
5833 self->InStream(self, &ch);
5849 while (isalpha(ch) && (index - start < width))
5851 doubleString[index++] = (char)ch;
5852 self->InStream(self, &ch);
5854 doubleString[index] = NIL;
5856 /* Case insensitive string comparison */
5857 if (trio_equal(&doubleString[start], INFINITE_UPPER) ||
5858 trio_equal(&doubleString[start], LONG_INFINITE_UPPER))
5860 if (flags & FLAGS_LONGDOUBLE)
5862 if ((start == 1) && (doubleString[0] == '-'))
5864 *((trio_long_double_t *)target) = trio_ninf();
5868 *((trio_long_double_t *)target) = trio_pinf();
5873 if ((start == 1) && (doubleString[0] == '-'))
5875 *((double *)target) = trio_ninf();
5879 *((double *)target) = trio_pinf();
5884 if (trio_equal(doubleString, NAN_UPPER))
5886 /* NaN must not have a preceeding + nor - */
5887 if (flags & FLAGS_LONGDOUBLE)
5889 *((trio_long_double_t *)target) = trio_nan();
5893 *((double *)target) = trio_nan();
5900 doubleString[index++] = (char)ch;
5901 self->InStream(self, &ch);
5902 if (trio_to_upper(ch) == 'X')
5905 doubleString[index++] = (char)ch;
5906 self->InStream(self, &ch);
5914 while ((ch != EOF) && (index - start < width))
5917 if (isHex ? isxdigit(ch) : isdigit(ch))
5919 doubleString[index++] = (char)ch;
5920 self->InStream(self, &ch);
5922 else if (flags & FLAGS_QUOTE)
5924 /* Compare with thousands separator */
5925 for (j = 0; internalThousandSeparator[j] && self->current; j++)
5927 if (internalThousandSeparator[j] != self->current)
5930 self->InStream(self, &ch);
5932 if (internalThousandSeparator[j])
5933 break; /* Mismatch */
5935 continue; /* Match */
5943 doubleString[index++] = (char)ch;
5944 self->InStream(self, &ch);
5945 while ((isHex ? isxdigit(ch) : isdigit(ch)) &&
5946 (index - start < width))
5948 doubleString[index++] = (char)ch;
5949 self->InStream(self, &ch);
5951 if (isHex ? (trio_to_upper(ch) == 'P') : (trio_to_upper(ch) == 'E'))
5954 doubleString[index++] = (char)ch;
5955 self->InStream(self, &ch);
5956 if ((ch == '+') || (ch == '-'))
5958 doubleString[index++] = (char)ch;
5959 self->InStream(self, &ch);
5961 while (isdigit(ch) && (index - start < width))
5963 doubleString[index++] = (char)ch;
5964 self->InStream(self, &ch);
5969 if ((index == start) || (*doubleString == NIL))
5972 doubleString[index] = 0;
5974 if (flags & FLAGS_LONGDOUBLE)
5976 *((trio_long_double_t *)target) = trio_to_long_double(doubleString, NULL);
5980 *((double *)target) = trio_to_double(doubleString, NULL);
5985 /*************************************************************************
5988 TRIO_PRIVATE BOOLEAN_T
5990 TRIO_ARGS3((self, target, flags),
5992 trio_pointer_t *target,
5995 trio_uintmax_t number;
5996 char buffer[sizeof(internalNullString)];
5998 flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE | FLAGS_NILPADDING);
6000 if (TrioReadNumber(self,
6007 * The strange assignment of number is a workaround for a compiler
6011 *target = (char *)0 + number;
6014 else if (TrioReadString(self,
6015 (flags & FLAGS_IGNORE)
6019 sizeof(internalNullString) - 1))
6021 if (trio_equal_case(buffer, internalNullString))
6031 /*************************************************************************
6036 TRIO_ARGS3((data, format, parameters),
6038 TRIO_CONST char *format,
6039 trio_parameter_t *parameters)
6041 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6047 int index; /* Index of format string */
6048 int i; /* Index of current parameter */
6052 trio_pointer_t pointer;
6057 data->InStream(data, &ch);
6059 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6060 (void)mblen(NULL, 0);
6063 while (format[index])
6065 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6066 if (! isascii(format[index]))
6068 charlen = mblen(&format[index], MB_LEN_MAX);
6071 /* Compare multibyte characters in format string */
6072 for (cnt = 0; cnt < charlen - 1; cnt++)
6074 if (ch != format[index + cnt])
6076 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6078 data->InStream(data, &ch);
6080 continue; /* while characters left in formatting string */
6083 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
6085 if ((EOF == ch) && (parameters[i].type != FORMAT_COUNT))
6087 return (assignment > 0) ? assignment : EOF;
6090 if (CHAR_IDENTIFIER == format[index])
6092 if (CHAR_IDENTIFIER == format[index + 1])
6094 /* Two % in format matches one % in input stream */
6095 if (CHAR_IDENTIFIER == ch)
6097 data->InStream(data, &ch);
6099 continue; /* while format chars left */
6102 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6105 /* Skip the parameter entries */
6106 while (parameters[i].type == FORMAT_PARAMETER)
6109 flags = parameters[i].flags;
6111 width = parameters[i].width;
6112 if (flags & FLAGS_WIDTH_PARAMETER)
6114 /* Get width from parameter list */
6115 width = (int)parameters[width].data.number.as_signed;
6118 base = parameters[i].base;
6119 if (flags & FLAGS_BASE_PARAMETER)
6121 /* Get base from parameter list */
6122 base = (int)parameters[base].data.number.as_signed;
6125 switch (parameters[i].type)
6129 trio_uintmax_t number;
6132 base = BASE_DECIMAL;
6134 if (!TrioReadNumber(data,
6141 if (!(flags & FLAGS_IGNORE))
6145 pointer = parameters[i].data.pointer;
6146 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6147 if (flags & FLAGS_SIZE_T)
6148 *(size_t *)pointer = (size_t)number;
6151 #if defined(QUALIFIER_PTRDIFF_T)
6152 if (flags & FLAGS_PTRDIFF_T)
6153 *(ptrdiff_t *)pointer = (ptrdiff_t)number;
6156 #if defined(QUALIFIER_INTMAX_T)
6157 if (flags & FLAGS_INTMAX_T)
6158 *(trio_intmax_t *)pointer = (trio_intmax_t)number;
6161 if (flags & FLAGS_QUAD)
6162 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)number;
6163 else if (flags & FLAGS_LONG)
6164 *(long int *)pointer = (long int)number;
6165 else if (flags & FLAGS_SHORT)
6166 *(short int *)pointer = (short int)number;
6168 *(int *)pointer = (int)number;
6171 break; /* FORMAT_INT */
6175 if (flags & FLAGS_WIDECHAR)
6177 if (!TrioReadWideString(data,
6178 (flags & FLAGS_IGNORE)
6180 : parameters[i].data.wstring,
6188 if (!TrioReadString(data,
6189 (flags & FLAGS_IGNORE)
6191 : parameters[i].data.string,
6196 if (!(flags & FLAGS_IGNORE))
6198 break; /* FORMAT_STRING */
6202 trio_pointer_t pointer;
6204 if (flags & FLAGS_IGNORE)
6210 pointer = (flags & FLAGS_LONGDOUBLE)
6211 ? (trio_pointer_t)parameters[i].data.longdoublePointer
6212 : (trio_pointer_t)parameters[i].data.doublePointer;
6214 if (!TrioReadDouble(data, pointer, flags, width))
6218 if (!(flags & FLAGS_IGNORE))
6222 break; /* FORMAT_DOUBLE */
6226 int characterclass[MAX_CHARACTER_CLASS + 1];
6229 /* Skip over modifiers */
6230 while (format[index] != SPECIFIER_GROUP)
6234 /* Skip over group specifier */
6237 memset(characterclass, 0, sizeof(characterclass));
6238 rc = TrioGetCharacterClass(format,
6245 if (!TrioReadGroup(data,
6246 (flags & FLAGS_IGNORE)
6248 : parameters[i].data.string,
6251 parameters[i].width))
6253 if (!(flags & FLAGS_IGNORE))
6256 break; /* FORMAT_GROUP */
6259 pointer = parameters[i].data.pointer;
6260 if (NULL != pointer)
6262 int count = data->committed;
6264 count--; /* a character is read, but is not consumed yet */
6265 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6266 if (flags & FLAGS_SIZE_T)
6267 *(size_t *)pointer = (size_t)count;
6270 #if defined(QUALIFIER_PTRDIFF_T)
6271 if (flags & FLAGS_PTRDIFF_T)
6272 *(ptrdiff_t *)pointer = (ptrdiff_t)count;
6275 #if defined(QUALIFIER_INTMAX_T)
6276 if (flags & FLAGS_INTMAX_T)
6277 *(trio_intmax_t *)pointer = (trio_intmax_t)count;
6280 if (flags & FLAGS_QUAD)
6282 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)count;
6284 else if (flags & FLAGS_LONG)
6286 *(long int *)pointer = (long int)count;
6288 else if (flags & FLAGS_SHORT)
6290 *(short int *)pointer = (short int)count;
6294 *(int *)pointer = (int)count;
6297 break; /* FORMAT_COUNT */
6301 if (flags & FLAGS_WIDECHAR)
6303 if (TrioReadWideChar(data,
6304 (flags & FLAGS_IGNORE)
6306 : parameters[i].data.wstring,
6308 (width == NO_WIDTH) ? 1 : width) == 0)
6314 if (TrioReadChar(data,
6315 (flags & FLAGS_IGNORE)
6317 : parameters[i].data.string,
6319 (width == NO_WIDTH) ? 1 : width) == 0)
6322 if (!(flags & FLAGS_IGNORE))
6324 break; /* FORMAT_CHAR */
6326 case FORMAT_POINTER:
6327 if (!TrioReadPointer(data,
6328 (flags & FLAGS_IGNORE)
6330 : (trio_pointer_t *)parameters[i].data.pointer,
6333 if (!(flags & FLAGS_IGNORE))
6335 break; /* FORMAT_POINTER */
6337 case FORMAT_PARAMETER:
6338 break; /* FORMAT_PARAMETER */
6341 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6344 index = parameters[i].indexAfterSpecifier;
6347 else /* Not an % identifier */
6349 if (isspace((int)format[index]))
6351 /* Whitespaces may match any amount of whitespaces */
6352 ch = TrioSkipWhitespaces(data);
6354 else if (ch == format[index])
6356 data->InStream(data, &ch);
6367 /*************************************************************************
6372 TRIO_ARGS6((source, sourceSize, InStream, format, arglist, argarray),
6373 trio_pointer_t source,
6375 void (*InStream) TRIO_PROTO((trio_class_t *, int *)),
6376 TRIO_CONST char *format,
6378 trio_pointer_t *argarray)
6381 trio_parameter_t parameters[MAX_PARAMETERS];
6384 assert(VALID(InStream));
6385 assert(VALID(format));
6387 memset(&data, 0, sizeof(data));
6388 data.InStream = InStream;
6389 data.location = (trio_pointer_t)source;
6390 data.max = sourceSize;
6393 #if defined(USE_LOCALE)
6394 if (NULL == internalLocaleValues)
6400 status = TrioParse(TYPE_SCAN, format, parameters, arglist, argarray);
6404 status = TrioScanProcess(&data, format, parameters);
6405 if (data.error != 0)
6407 status = data.error;
6412 /*************************************************************************
6417 TRIO_ARGS2((self, intPointer),
6421 FILE *file = (FILE *)self->location;
6423 assert(VALID(self));
6424 assert(VALID(file));
6426 self->current = fgetc(file);
6427 if (self->current == EOF)
6429 self->error = (ferror(file))
6430 ? TRIO_ERROR_RETURN(TRIO_ERRNO, 0)
6431 : TRIO_ERROR_RETURN(TRIO_EOF, 0);
6439 if (VALID(intPointer))
6441 *intPointer = self->current;
6445 /*************************************************************************
6446 * TrioInStreamFileDescriptor
6449 TrioInStreamFileDescriptor
6450 TRIO_ARGS2((self, intPointer),
6454 int fd = *((int *)self->location);
6456 unsigned char input;
6458 assert(VALID(self));
6460 size = read(fd, &input, sizeof(char));
6463 self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
6464 self->current = EOF;
6468 self->current = (size == 0) ? EOF : input;
6470 if (self->current != EOF)
6476 if (VALID(intPointer))
6478 *intPointer = self->current;
6482 /*************************************************************************
6483 * TrioInStreamCustom
6487 TRIO_ARGS2((self, intPointer),
6491 trio_custom_t *data;
6493 assert(VALID(self));
6494 assert(VALID(self->location));
6496 data = (trio_custom_t *)self->location;
6498 self->current = (data->stream.in == NULL)
6500 : (data->stream.in)(data->closure);
6502 if (self->current == NIL)
6504 self->current = EOF;
6512 if (VALID(intPointer))
6514 *intPointer = self->current;
6518 /*************************************************************************
6519 * TrioInStreamString
6523 TRIO_ARGS2((self, intPointer),
6527 unsigned char **buffer;
6529 assert(VALID(self));
6530 assert(VALID(self->location));
6532 buffer = (unsigned char **)self->location;
6533 self->current = (*buffer)[0];
6534 if (self->current == NIL)
6536 self->current = EOF;
6545 if (VALID(intPointer))
6547 *intPointer = self->current;
6551 /*************************************************************************
6553 * Formatted scanning functions
6555 ************************************************************************/
6557 #if defined(TRIO_DOCUMENTATION)
6558 # include "doc/doc_scanf.h"
6560 /** @addtogroup Scanf
6564 /*************************************************************************
6569 Scan characters from standard input stream.
6571 @param format Formatting string.
6572 @param ... Arguments.
6573 @return Number of scanned characters.
6577 TRIO_VARGS2((format, va_alist),
6578 TRIO_CONST char *format,
6584 assert(VALID(format));
6586 TRIO_VA_START(args, format);
6587 status = TrioScan((trio_pointer_t)stdin, 0,
6589 format, &args, NULL);
6596 TRIO_ARGS2((format, args),
6597 TRIO_CONST char *format,
6600 assert(VALID(format));
6602 return TrioScan((trio_pointer_t)stdin, 0,
6604 format, &args, NULL);
6609 TRIO_ARGS2((format, args),
6610 TRIO_CONST char *format,
6611 trio_pointer_t *args)
6613 assert(VALID(format));
6615 return TrioScan((trio_pointer_t)stdin, 0,
6617 format, NULL, args);
6620 /*************************************************************************
6625 TRIO_VARGS3((file, format, va_alist),
6627 TRIO_CONST char *format,
6633 assert(VALID(file));
6634 assert(VALID(format));
6636 TRIO_VA_START(args, format);
6637 status = TrioScan((trio_pointer_t)file, 0,
6639 format, &args, NULL);
6646 TRIO_ARGS3((file, format, args),
6648 TRIO_CONST char *format,
6651 assert(VALID(file));
6652 assert(VALID(format));
6654 return TrioScan((trio_pointer_t)file, 0,
6656 format, &args, NULL);
6661 TRIO_ARGS3((file, format, args),
6663 TRIO_CONST char *format,
6664 trio_pointer_t *args)
6666 assert(VALID(file));
6667 assert(VALID(format));
6669 return TrioScan((trio_pointer_t)file, 0,
6671 format, NULL, args);
6674 /*************************************************************************
6679 TRIO_VARGS3((fd, format, va_alist),
6681 TRIO_CONST char *format,
6687 assert(VALID(format));
6689 TRIO_VA_START(args, format);
6690 status = TrioScan((trio_pointer_t)&fd, 0,
6691 TrioInStreamFileDescriptor,
6692 format, &args, NULL);
6699 TRIO_ARGS3((fd, format, args),
6701 TRIO_CONST char *format,
6704 assert(VALID(format));
6706 return TrioScan((trio_pointer_t)&fd, 0,
6707 TrioInStreamFileDescriptor,
6708 format, &args, NULL);
6713 TRIO_ARGS3((fd, format, args),
6715 TRIO_CONST char *format,
6716 trio_pointer_t *args)
6718 assert(VALID(format));
6720 return TrioScan((trio_pointer_t)&fd, 0,
6721 TrioInStreamFileDescriptor,
6722 format, NULL, args);
6725 /*************************************************************************
6730 TRIO_VARGS4((stream, closure, format, va_alist),
6731 trio_instream_t stream,
6732 trio_pointer_t closure,
6733 TRIO_CONST char *format,
6740 assert(VALID(stream));
6741 assert(VALID(format));
6743 TRIO_VA_START(args, format);
6744 data.stream.in = stream;
6745 data.closure = closure;
6746 status = TrioScan(&data, 0, TrioInStreamCustom, format, &args, NULL);
6753 TRIO_ARGS4((stream, closure, format, args),
6754 trio_instream_t stream,
6755 trio_pointer_t closure,
6756 TRIO_CONST char *format,
6761 assert(VALID(stream));
6762 assert(VALID(format));
6764 data.stream.in = stream;
6765 data.closure = closure;
6766 return TrioScan(&data, 0, TrioInStreamCustom, format, &args, NULL);
6771 TRIO_ARGS4((stream, closure, format, args),
6772 trio_instream_t stream,
6773 trio_pointer_t closure,
6774 TRIO_CONST char *format,
6775 trio_pointer_t *args)
6779 assert(VALID(stream));
6780 assert(VALID(format));
6782 data.stream.in = stream;
6783 data.closure = closure;
6784 return TrioScan(&data, 0, TrioInStreamCustom, format, NULL, args);
6787 /*************************************************************************
6792 TRIO_VARGS3((buffer, format, va_alist),
6793 TRIO_CONST char *buffer,
6794 TRIO_CONST char *format,
6800 assert(VALID(buffer));
6801 assert(VALID(format));
6803 TRIO_VA_START(args, format);
6804 status = TrioScan((trio_pointer_t)&buffer, 0,
6806 format, &args, NULL);
6813 TRIO_ARGS3((buffer, format, args),
6814 TRIO_CONST char *buffer,
6815 TRIO_CONST char *format,
6818 assert(VALID(buffer));
6819 assert(VALID(format));
6821 return TrioScan((trio_pointer_t)&buffer, 0,
6823 format, &args, NULL);
6828 TRIO_ARGS3((buffer, format, args),
6829 TRIO_CONST char *buffer,
6830 TRIO_CONST char *format,
6831 trio_pointer_t *args)
6833 assert(VALID(buffer));
6834 assert(VALID(format));
6836 return TrioScan((trio_pointer_t)&buffer, 0,
6838 format, NULL, args);
6841 /** @} End of Scanf documentation module */
6843 /*************************************************************************
6846 TRIO_PUBLIC TRIO_CONST char *
6848 TRIO_ARGS1((errorcode),
6851 /* Textual versions of the error codes */
6852 switch (TRIO_ERROR_CODE(errorcode))
6855 return "End of file";
6857 return "Invalid argument";
6859 return "Too many arguments";
6861 return "Double reference";
6863 return "Reference gap";
6865 return "Out of memory";
6867 return "Invalid range";
6869 return "Custom error";