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 /* Various constants */
315 /* Flags. FLAGS_LAST must be less than ULONG_MAX */
318 FLAGS_SPACE
= 2 * FLAGS_STICKY
,
319 FLAGS_SHOWSIGN
= 2 * FLAGS_SPACE
,
320 FLAGS_LEFTADJUST
= 2 * FLAGS_SHOWSIGN
,
321 FLAGS_ALTERNATIVE
= 2 * FLAGS_LEFTADJUST
,
322 FLAGS_SHORT
= 2 * FLAGS_ALTERNATIVE
,
323 FLAGS_SHORTSHORT
= 2 * FLAGS_SHORT
,
324 FLAGS_LONG
= 2 * FLAGS_SHORTSHORT
,
325 FLAGS_QUAD
= 2 * FLAGS_LONG
,
326 FLAGS_LONGDOUBLE
= 2 * FLAGS_QUAD
,
327 FLAGS_SIZE_T
= 2 * FLAGS_LONGDOUBLE
,
328 FLAGS_PTRDIFF_T
= 2 * FLAGS_SIZE_T
,
329 FLAGS_INTMAX_T
= 2 * FLAGS_PTRDIFF_T
,
330 FLAGS_NILPADDING
= 2 * FLAGS_INTMAX_T
,
331 FLAGS_UNSIGNED
= 2 * FLAGS_NILPADDING
,
332 FLAGS_UPPER
= 2 * FLAGS_UNSIGNED
,
333 FLAGS_WIDTH
= 2 * FLAGS_UPPER
,
334 FLAGS_WIDTH_PARAMETER
= 2 * FLAGS_WIDTH
,
335 FLAGS_PRECISION
= 2 * FLAGS_WIDTH_PARAMETER
,
336 FLAGS_PRECISION_PARAMETER
= 2 * FLAGS_PRECISION
,
337 FLAGS_BASE
= 2 * FLAGS_PRECISION_PARAMETER
,
338 FLAGS_BASE_PARAMETER
= 2 * FLAGS_BASE
,
339 FLAGS_FLOAT_E
= 2 * FLAGS_BASE_PARAMETER
,
340 FLAGS_FLOAT_G
= 2 * FLAGS_FLOAT_E
,
341 FLAGS_QUOTE
= 2 * FLAGS_FLOAT_G
,
342 FLAGS_WIDECHAR
= 2 * FLAGS_QUOTE
,
343 FLAGS_ALLOC
= 2 * FLAGS_WIDECHAR
,
344 FLAGS_IGNORE
= 2 * FLAGS_ALLOC
,
345 FLAGS_IGNORE_PARAMETER
= 2 * FLAGS_IGNORE
,
346 FLAGS_VARSIZE_PARAMETER
= 2 * FLAGS_IGNORE_PARAMETER
,
347 FLAGS_FIXED_SIZE
= 2 * FLAGS_VARSIZE_PARAMETER
,
348 FLAGS_LAST
= FLAGS_FIXED_SIZE
,
350 FLAGS_EXCLUDE
= FLAGS_SHORT
,
351 FLAGS_USER_DEFINED
= FLAGS_IGNORE
,
352 FLAGS_ROUNDING
= FLAGS_INTMAX_T
,
353 /* Compounded flags */
354 FLAGS_ALL_VARSIZES
= FLAGS_LONG
| FLAGS_QUAD
| FLAGS_INTMAX_T
| FLAGS_PTRDIFF_T
| FLAGS_SIZE_T
,
355 FLAGS_ALL_SIZES
= FLAGS_ALL_VARSIZES
| FLAGS_SHORTSHORT
| FLAGS_SHORT
,
362 /* Do not change these */
371 /* Maximal number of allowed parameters */
373 /* Maximal number of characters in class */
374 MAX_CHARACTER_CLASS
= UCHAR_MAX
+ 1,
376 /* Maximal string lengths for user-defined specifiers */
380 /* Maximal length of locale separator strings */
381 MAX_LOCALE_SEPARATOR_LENGTH
= MB_LEN_MAX
,
382 /* Maximal number of integers in grouping */
383 MAX_LOCALE_GROUPS
= 64,
385 /* Initial size of asprintf buffer */
386 DYNAMIC_START_SIZE
= 32
389 #define NO_GROUPING ((int)CHAR_MAX)
391 /* Fundamental formatting parameter types */
392 #define FORMAT_UNKNOWN 0
394 #define FORMAT_DOUBLE 2
395 #define FORMAT_CHAR 3
396 #define FORMAT_STRING 4
397 #define FORMAT_POINTER 5
398 #define FORMAT_COUNT 6
399 #define FORMAT_PARAMETER 7
400 #define FORMAT_GROUP 8
402 # define FORMAT_ERRNO 9
405 # define FORMAT_USER_DEFINED 10
408 /* Character constants */
409 #define CHAR_IDENTIFIER '%'
410 #define CHAR_BACKSLASH '\\'
411 #define CHAR_QUOTE '\"'
412 #define CHAR_ADJUST ' '
414 /* Character class expressions */
415 #define CLASS_ALNUM "[:alnum:]"
416 #define CLASS_ALPHA "[:alpha:]"
417 #define CLASS_BLANK "[:blank:]"
418 #define CLASS_CNTRL "[:cntrl:]"
419 #define CLASS_DIGIT "[:digit:]"
420 #define CLASS_GRAPH "[:graph:]"
421 #define CLASS_LOWER "[:lower:]"
422 #define CLASS_PRINT "[:print:]"
423 #define CLASS_PUNCT "[:punct:]"
424 #define CLASS_SPACE "[:space:]"
425 #define CLASS_UPPER "[:upper:]"
426 #define CLASS_XDIGIT "[:xdigit:]"
435 * C Widechar character (wint_t)
449 * S Widechar string (wchar_t *)
458 * D Binary Coded Decimal %D(length,precision) (OS/390)
460 #define SPECIFIER_CHAR 'c'
461 #define SPECIFIER_STRING 's'
462 #define SPECIFIER_DECIMAL 'd'
463 #define SPECIFIER_INTEGER 'i'
464 #define SPECIFIER_UNSIGNED 'u'
465 #define SPECIFIER_OCTAL 'o'
466 #define SPECIFIER_HEX 'x'
467 #define SPECIFIER_HEX_UPPER 'X'
468 #define SPECIFIER_FLOAT_E 'e'
469 #define SPECIFIER_FLOAT_E_UPPER 'E'
470 #define SPECIFIER_FLOAT_F 'f'
471 #define SPECIFIER_FLOAT_F_UPPER 'F'
472 #define SPECIFIER_FLOAT_G 'g'
473 #define SPECIFIER_FLOAT_G_UPPER 'G'
474 #define SPECIFIER_POINTER 'p'
475 #define SPECIFIER_GROUP '['
476 #define SPECIFIER_UNGROUP ']'
477 #define SPECIFIER_COUNT 'n'
479 # define SPECIFIER_CHAR_UPPER 'C'
480 # define SPECIFIER_STRING_UPPER 'S'
483 # define SPECIFIER_HEXFLOAT 'a'
484 # define SPECIFIER_HEXFLOAT_UPPER 'A'
487 # define SPECIFIER_ERRNO 'm'
490 # define SPECIFIER_BINARY 'b'
491 # define SPECIFIER_BINARY_UPPER 'B'
492 # define SPECIFIER_USER_DEFINED_BEGIN '<'
493 # define SPECIFIER_USER_DEFINED_END '>'
494 # define SPECIFIER_USER_DEFINED_SEPARATOR ':'
501 * Numbers = d,i,o,u,x,X
502 * Float = a,A,e,E,f,F,g,G
508 * Use the 9th parameter. 9 can be any number between 1 and
509 * the maximal argument
512 * Set width to 9. 9 can be any number, but must not be postfixed
517 * (unsigned) short int
525 * (unsigned) long int
533 * (unsigned) long long int
541 * Decimal-point is always present
543 * non-printable characters are handled as \number
554 * print: use parameter
555 * scan: no parameter (ignore)
565 * Integer part grouped in thousands
567 * Number grouped in nibbles (4 bits)
576 * @ Parameter (for both print and scan)
580 * The following options exists
582 * I16 = 16-bit integer
583 * I32 = 32-bit integer
584 * I64 = 64-bit integer
586 #define QUALIFIER_POSITION '$'
587 #define QUALIFIER_SHORT 'h'
588 #define QUALIFIER_LONG 'l'
589 #define QUALIFIER_LONG_UPPER 'L'
590 #define QUALIFIER_ALTERNATIVE '#'
591 #define QUALIFIER_SPACE ' '
592 #define QUALIFIER_PLUS '+'
593 #define QUALIFIER_MINUS '-'
594 #define QUALIFIER_DOT '.'
595 #define QUALIFIER_STAR '*'
596 #define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
598 # define QUALIFIER_SIZE_T 'z'
599 # define QUALIFIER_PTRDIFF_T 't'
600 # define QUALIFIER_INTMAX_T 'j'
602 #if TRIO_BSD || TRIO_GNU
603 # define QUALIFIER_QUAD 'q'
606 # define QUALIFIER_SIZE_T_UPPER 'Z'
609 # define QUALIFIER_WIDECHAR 'w'
612 # define QUALIFIER_FIXED_SIZE 'I'
615 # define QUALIFIER_QUOTE '\''
616 # define QUALIFIER_STICKY '!'
617 # define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
618 # define QUALIFIER_PARAM '@' /* Experimental */
619 # define QUALIFIER_COLON ':' /* For scanlists */
620 # define QUALIFIER_EQUAL '=' /* For scanlists */
621 # define QUALIFIER_ROUNDING_UPPER 'R'
625 /*************************************************************************
627 * Internal Structures
629 *************************************************************************/
633 /* An indication of which entry in the data union is used */
637 /* The width qualifier */
639 /* The precision qualifier */
641 /* The base qualifier */
643 /* The size for the variable size qualifier */
645 /* The marker of the end of the specifier */
646 int indexAfterSpecifier
;
647 /* The data from the argument list */
651 trio_wchar_t
*wstring
;
653 trio_pointer_t pointer
;
655 trio_intmax_t as_signed
;
656 trio_uintmax_t as_unsigned
;
659 double *doublePointer
;
660 trio_long_double_t longdoubleNumber
;
661 trio_long_double_t
*longdoublePointer
;
664 /* For the user-defined specifier */
665 char user_name
[MAX_USER_NAME
];
666 char user_data
[MAX_USER_DATA
];
669 /* Container for customized functions */
672 trio_outstream_t out
;
675 trio_pointer_t closure
;
678 /* General trio "class" */
679 typedef struct _trio_class_t
{
681 * The function to write characters to a stream.
683 void (*OutStream
) TRIO_PROTO((struct _trio_class_t
*, int));
685 * The function to read characters from a stream.
687 void (*InStream
) TRIO_PROTO((struct _trio_class_t
*, int *));
689 * The current location in the stream.
691 trio_pointer_t location
;
693 * The character currently being processed.
697 * The number of characters that would have been written/read
698 * if there had been sufficient space.
702 * The number of characters that are actually written/read.
703 * Processed and committed will only differ for the *nprintf
704 * and *nscanf functions.
708 * The upper limit of characters that may be written/read.
712 * The last output error that was detected.
717 /* References (for user-defined callbacks) */
718 typedef struct _trio_reference_t
{
720 trio_parameter_t
*parameter
;
723 /* Registered entries (for user-defined callbacks) */
724 typedef struct _trio_userdef_t
{
725 struct _trio_userdef_t
*next
;
726 trio_callback_t callback
;
730 /*************************************************************************
734 *************************************************************************/
736 static TRIO_CONST
char rcsid
[] = "@(#)$Id$";
739 * Need this to workaround a parser bug in HP C/iX compiler that fails
740 * to resolves macro definitions that includes type 'long double',
741 * e.g: va_arg(arg_ptr, long double)
743 #if defined(TRIO_PLATFORM_MPEIX)
744 static TRIO_CONST trio_long_double_t ___dummy_long_double
= 0;
747 static TRIO_CONST
char internalNullString
[] = "(nil)";
749 #if defined(USE_LOCALE)
750 static struct lconv
*internalLocaleValues
= NULL
;
754 * UNIX98 says "in a locale where the radix character is not defined,
755 * the radix character defaults to a period (.)"
757 static int internalDecimalPointLength
= 1;
758 static int internalThousandSeparatorLength
= 1;
759 static char internalDecimalPoint
= '.';
760 static char internalDecimalPointString
[MAX_LOCALE_SEPARATOR_LENGTH
+ 1] = ".";
761 static char internalThousandSeparator
[MAX_LOCALE_SEPARATOR_LENGTH
+ 1] = ",";
762 static char internalGrouping
[MAX_LOCALE_GROUPS
] = { (char)NO_GROUPING
};
764 static TRIO_CONST
char internalDigitsLower
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
765 static TRIO_CONST
char internalDigitsUpper
[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
766 static BOOLEAN_T internalDigitsUnconverted
= TRUE
;
767 static int internalDigitArray
[128];
769 static BOOLEAN_T internalCollationUnconverted
= TRUE
;
770 static char internalCollationArray
[MAX_CHARACTER_CLASS
][MAX_CHARACTER_CLASS
];
774 static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion
= NULL
;
775 static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion
= NULL
;
776 static trio_userdef_t
*internalUserDef
= NULL
;
780 /*************************************************************************
784 ************************************************************************/
786 #if defined(TRIO_MINIMAL)
787 # define TRIO_STRING_PUBLIC static
788 # include "triostr.c"
789 #endif /* defined(TRIO_MINIMAL) */
791 /*************************************************************************
795 * Remember to add all new qualifiers to this function.
796 * QUALIFIER_POSITION must not be added.
798 TRIO_PRIVATE BOOLEAN_T
800 TRIO_ARGS1((character
),
801 TRIO_CONST
char character
)
803 /* QUALIFIER_POSITION is not included */
806 case '0': case '1': case '2': case '3': case '4':
807 case '5': case '6': case '7': case '8': case '9':
809 case QUALIFIER_MINUS
:
810 case QUALIFIER_SPACE
:
813 case QUALIFIER_ALTERNATIVE
:
814 case QUALIFIER_SHORT
:
816 case QUALIFIER_LONG_UPPER
:
817 case QUALIFIER_CIRCUMFLEX
:
818 #if defined(QUALIFIER_SIZE_T)
819 case QUALIFIER_SIZE_T
:
821 #if defined(QUALIFIER_PTRDIFF_T)
822 case QUALIFIER_PTRDIFF_T
:
824 #if defined(QUALIFIER_INTMAX_T)
825 case QUALIFIER_INTMAX_T
:
827 #if defined(QUALIFIER_QUAD)
830 #if defined(QUALIFIER_SIZE_T_UPPER)
831 case QUALIFIER_SIZE_T_UPPER
:
833 #if defined(QUALIFIER_WIDECHAR)
834 case QUALIFIER_WIDECHAR
:
836 #if defined(QUALIFIER_QUOTE)
837 case QUALIFIER_QUOTE
:
839 #if defined(QUALIFIER_STICKY)
840 case QUALIFIER_STICKY
:
842 #if defined(QUALIFIER_VARSIZE)
843 case QUALIFIER_VARSIZE
:
845 #if defined(QUALIFIER_PARAM)
846 case QUALIFIER_PARAM
:
848 #if defined(QUALIFIER_FIXED_SIZE)
849 case QUALIFIER_FIXED_SIZE
:
851 #if defined(QUALIFIER_ROUNDING_UPPER)
852 case QUALIFIER_ROUNDING_UPPER
:
860 /*************************************************************************
863 #if defined(USE_LOCALE)
865 TrioSetLocale(TRIO_NOARGS
)
867 internalLocaleValues
= (struct lconv
*)localeconv();
868 if (internalLocaleValues
)
870 if ((internalLocaleValues
->decimal_point
) &&
871 (internalLocaleValues
->decimal_point
[0] != NIL
))
873 internalDecimalPointLength
= trio_length(internalLocaleValues
->decimal_point
);
874 if (internalDecimalPointLength
== 1)
876 internalDecimalPoint
= internalLocaleValues
->decimal_point
[0];
880 internalDecimalPoint
= NIL
;
881 trio_copy_max(internalDecimalPointString
,
882 sizeof(internalDecimalPointString
),
883 internalLocaleValues
->decimal_point
);
886 if ((internalLocaleValues
->thousands_sep
) &&
887 (internalLocaleValues
->thousands_sep
[0] != NIL
))
889 trio_copy_max(internalThousandSeparator
,
890 sizeof(internalThousandSeparator
),
891 internalLocaleValues
->thousands_sep
);
892 internalThousandSeparatorLength
= trio_length(internalThousandSeparator
);
894 if ((internalLocaleValues
->grouping
) &&
895 (internalLocaleValues
->grouping
[0] != NIL
))
897 trio_copy_max(internalGrouping
,
898 sizeof(internalGrouping
),
899 internalLocaleValues
->grouping
);
903 #endif /* defined(USE_LOCALE) */
906 TrioCalcThousandSeparatorLength
912 int step
= NO_GROUPING
;
913 char *groupingPointer
= internalGrouping
;
917 if (*groupingPointer
== CHAR_MAX
)
919 /* Disable grouping */
922 else if (*groupingPointer
== 0)
924 /* Repeat last group */
925 if (step
== NO_GROUPING
)
927 /* Error in locale */
933 step
= *groupingPointer
++;
936 count
+= internalThousandSeparatorLength
;
945 TRIO_PRIVATE BOOLEAN_T
946 TrioFollowedBySeparator
947 TRIO_ARGS1((position
),
952 char *groupingPointer
= internalGrouping
;
959 if (*groupingPointer
== CHAR_MAX
)
961 /* Disable grouping */
964 else if (*groupingPointer
!= 0)
966 step
= *groupingPointer
++;
972 return (position
== 0);
978 /*************************************************************************
981 * Get the %n$ position.
985 TRIO_ARGS2((format
, indexPointer
),
986 TRIO_CONST
char *format
,
992 int index
= *indexPointer
;
994 number
= (int)trio_to_long(&format
[index
], &tmpformat
, BASE_DECIMAL
);
995 index
= (int)(tmpformat
- format
);
996 if ((number
!= 0) && (QUALIFIER_POSITION
== format
[index
++]))
998 *indexPointer
= index
;
1000 * number is decreased by 1, because n$ starts from 1, whereas
1001 * the array it is indexing starts from 0.
1010 /*************************************************************************
1013 * Find registered user-defined specifier.
1014 * The prev argument is used for optimization only.
1016 TRIO_PRIVATE trio_userdef_t
*
1018 TRIO_ARGS2((name
, prev
),
1019 TRIO_CONST
char *name
,
1020 trio_userdef_t
**prev
)
1022 trio_userdef_t
*def
;
1024 if (internalEnterCriticalRegion
)
1025 (void)internalEnterCriticalRegion(NULL
);
1027 for (def
= internalUserDef
; def
; def
= def
->next
)
1029 /* Case-sensitive string comparison */
1030 if (trio_equal_case(def
->name
, name
))
1037 if (internalLeaveCriticalRegion
)
1038 (void)internalLeaveCriticalRegion(NULL
);
1044 /*************************************************************************
1048 * Calculate pow(base, exponent), where number and exponent are integers.
1050 TRIO_PRIVATE trio_long_double_t
1052 TRIO_ARGS2((number
, exponent
),
1056 trio_long_double_t result
;
1062 /* Speed up calculation of common cases */
1064 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E-1);
1067 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+0);
1070 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+1);
1073 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+2);
1076 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+3);
1079 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+4);
1082 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+5);
1085 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+6);
1088 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+7);
1091 result
= (trio_long_double_t
)number
* TRIO_SUFFIX_LONG(1E+8);
1094 result
= powl((trio_long_double_t
)number
,
1095 (trio_long_double_t
)exponent
);
1101 return powl((trio_long_double_t
)number
, (trio_long_double_t
)exponent
);
1106 /*************************************************************************
1111 TRIO_ARGS2((number
, base
),
1119 /* xlC crashes on log(0) */
1120 result
= (number
== 0.0) ? trio_ninf() : trio_nan();
1126 result
= log10(number
);
1130 result
= log10(number
) / log10((double)base
);
1136 /*************************************************************************
1146 case BASE_BINARY
: return 1.0;
1147 case BASE_OCTAL
: return 3.0;
1148 case BASE_DECIMAL
: return 3.321928094887362345;
1149 case BASE_HEX
: return 4.0;
1150 default : return TrioLogarithm((double)base
, 2);
1154 /*************************************************************************
1158 * Parse the format string
1162 TRIO_ARGS5((type
, format
, parameters
, arglist
, argarray
),
1164 TRIO_CONST
char *format
,
1165 trio_parameter_t
*parameters
,
1167 trio_pointer_t
*argarray
)
1169 /* Count the number of times a parameter is referenced */
1170 unsigned short usedEntries
[MAX_PARAMETERS
];
1171 /* Parameter counters */
1172 int parameterPosition
;
1175 /* Utility variables */
1181 int index
; /* Index into formatting string */
1182 int dots
; /* Count number of dots in modifier part */
1183 BOOLEAN_T positional
; /* Does the specifier have a positional? */
1184 BOOLEAN_T gotSticky
= FALSE
; /* Are there any sticky modifiers at all? */
1186 * indices specifies the order in which the parameters must be
1187 * read from the va_args (this is necessary to handle positionals)
1189 int indices
[MAX_PARAMETERS
];
1191 /* Various variables */
1193 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1201 /* One and only one of arglist and argarray must be used */
1202 assert((arglist
!= NULL
) ^ (argarray
!= NULL
));
1205 * The 'parameters' array is not initialized, but we need to
1206 * know which entries we have used.
1208 memset(usedEntries
, 0, sizeof(usedEntries
));
1212 parameterPosition
= 0;
1213 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1214 (void)mblen(NULL
, 0);
1217 while (format
[index
])
1219 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1220 if (! isascii(format
[index
]))
1223 * Multibyte characters cannot be legal specifiers or
1224 * modifiers, so we skip over them.
1226 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
1227 index
+= (charlen
> 0) ? charlen
: 1;
1228 continue; /* while */
1230 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
1231 if (CHAR_IDENTIFIER
== format
[index
++])
1233 if (CHAR_IDENTIFIER
== format
[index
])
1236 continue; /* while */
1241 currentParam
= TrioGetPosition(format
, &index
);
1242 positional
= (NO_POSITION
!= currentParam
);
1245 /* We have no positional, get the next counter */
1246 currentParam
= parameterPosition
;
1248 if(currentParam
>= MAX_PARAMETERS
)
1250 /* Bail out completely to make the error more obvious */
1251 return TRIO_ERROR_RETURN(TRIO_ETOOMANY
, index
);
1254 if (currentParam
> maxParam
)
1255 maxParam
= currentParam
;
1257 /* Default values */
1259 precision
= NO_PRECISION
;
1263 while (TrioIsQualifier(format
[index
]))
1265 ch
= format
[index
++];
1269 case QUALIFIER_SPACE
:
1270 flags
|= FLAGS_SPACE
;
1273 case QUALIFIER_PLUS
:
1274 flags
|= FLAGS_SHOWSIGN
;
1277 case QUALIFIER_MINUS
:
1278 flags
|= FLAGS_LEFTADJUST
;
1279 flags
&= ~FLAGS_NILPADDING
;
1282 case QUALIFIER_ALTERNATIVE
:
1283 flags
|= FLAGS_ALTERNATIVE
;
1287 if (dots
== 0) /* Precision */
1291 /* Skip if no precision */
1292 if (QUALIFIER_DOT
== format
[index
])
1295 /* After the first dot we have the precision */
1296 flags
|= FLAGS_PRECISION
;
1297 if ((QUALIFIER_STAR
== format
[index
])
1298 #if defined(QUALIFIER_PARAM)
1299 || (QUALIFIER_PARAM
== format
[index
])
1304 flags
|= FLAGS_PRECISION_PARAMETER
;
1306 precision
= TrioGetPosition(format
, &index
);
1307 if (precision
== NO_POSITION
)
1309 parameterPosition
++;
1311 precision
= parameterPosition
;
1314 precision
= currentParam
;
1315 currentParam
= precision
+ 1;
1321 currentParam
= precision
+ 1;
1322 if (width
> maxParam
)
1323 maxParam
= precision
;
1325 if (currentParam
> maxParam
)
1326 maxParam
= currentParam
;
1330 precision
= trio_to_long(&format
[index
],
1333 index
= (int)(tmpformat
- format
);
1336 else if (dots
== 1) /* Base */
1340 /* After the second dot we have the base */
1341 flags
|= FLAGS_BASE
;
1342 if ((QUALIFIER_STAR
== format
[index
])
1343 #if defined(QUALIFIER_PARAM)
1344 || (QUALIFIER_PARAM
== format
[index
])
1349 flags
|= FLAGS_BASE_PARAMETER
;
1350 base
= TrioGetPosition(format
, &index
);
1351 if (base
== NO_POSITION
)
1353 parameterPosition
++;
1355 base
= parameterPosition
;
1358 base
= currentParam
;
1359 currentParam
= base
+ 1;
1365 currentParam
= base
+ 1;
1366 if (base
> maxParam
)
1369 if (currentParam
> maxParam
)
1370 maxParam
= currentParam
;
1374 base
= trio_to_long(&format
[index
],
1377 if (base
> MAX_BASE
)
1378 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1379 index
= (int)(tmpformat
- format
);
1384 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1386 break; /* QUALIFIER_DOT */
1388 #if defined(QUALIFIER_PARAM)
1389 case QUALIFIER_PARAM
:
1393 case QUALIFIER_STAR
:
1394 /* This has different meanings for print and scan */
1395 if (TYPE_PRINT
== type
)
1397 /* Read with from parameter */
1398 flags
|= (FLAGS_WIDTH
| FLAGS_WIDTH_PARAMETER
);
1399 width
= TrioGetPosition(format
, &index
);
1400 if (width
== NO_POSITION
)
1402 parameterPosition
++;
1404 width
= parameterPosition
;
1407 width
= currentParam
;
1408 currentParam
= width
+ 1;
1414 currentParam
= width
+ 1;
1415 if (width
> maxParam
)
1418 if (currentParam
> maxParam
)
1419 maxParam
= currentParam
;
1423 /* Scan, but do not store result */
1424 flags
|= FLAGS_IGNORE
;
1427 break; /* QUALIFIER_STAR */
1430 if (! (flags
& FLAGS_LEFTADJUST
))
1431 flags
|= FLAGS_NILPADDING
;
1433 case '1': case '2': case '3': case '4':
1434 case '5': case '6': case '7': case '8': case '9':
1435 flags
|= FLAGS_WIDTH
;
1436 /* &format[index - 1] is used to "rewind" the read
1437 * character from format
1439 width
= trio_to_long(&format
[index
- 1],
1442 index
= (int)(tmpformat
- format
);
1445 case QUALIFIER_SHORT
:
1446 if (flags
& FLAGS_SHORTSHORT
)
1447 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1448 else if (flags
& FLAGS_SHORT
)
1449 flags
|= FLAGS_SHORTSHORT
;
1451 flags
|= FLAGS_SHORT
;
1454 case QUALIFIER_LONG
:
1455 if (flags
& FLAGS_QUAD
)
1456 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1457 else if (flags
& FLAGS_LONG
)
1458 flags
|= FLAGS_QUAD
;
1460 flags
|= FLAGS_LONG
;
1463 case QUALIFIER_LONG_UPPER
:
1464 flags
|= FLAGS_LONGDOUBLE
;
1467 #if defined(QUALIFIER_SIZE_T)
1468 case QUALIFIER_SIZE_T
:
1469 flags
|= FLAGS_SIZE_T
;
1470 /* Modify flags for later truncation of number */
1471 if (sizeof(size_t) == sizeof(trio_ulonglong_t
))
1472 flags
|= FLAGS_QUAD
;
1473 else if (sizeof(size_t) == sizeof(long))
1474 flags
|= FLAGS_LONG
;
1478 #if defined(QUALIFIER_PTRDIFF_T)
1479 case QUALIFIER_PTRDIFF_T
:
1480 flags
|= FLAGS_PTRDIFF_T
;
1481 if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t
))
1482 flags
|= FLAGS_QUAD
;
1483 else if (sizeof(ptrdiff_t) == sizeof(long))
1484 flags
|= FLAGS_LONG
;
1488 #if defined(QUALIFIER_INTMAX_T)
1489 case QUALIFIER_INTMAX_T
:
1490 flags
|= FLAGS_INTMAX_T
;
1491 if (sizeof(trio_intmax_t
) == sizeof(trio_ulonglong_t
))
1492 flags
|= FLAGS_QUAD
;
1493 else if (sizeof(trio_intmax_t
) == sizeof(long))
1494 flags
|= FLAGS_LONG
;
1498 #if defined(QUALIFIER_QUAD)
1499 case QUALIFIER_QUAD
:
1500 flags
|= FLAGS_QUAD
;
1504 #if defined(QUALIFIER_FIXED_SIZE)
1505 case QUALIFIER_FIXED_SIZE
:
1506 if (flags
& FLAGS_FIXED_SIZE
)
1507 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1509 if (flags
& (FLAGS_ALL_SIZES
| FLAGS_LONGDOUBLE
|
1510 FLAGS_WIDECHAR
| FLAGS_VARSIZE_PARAMETER
))
1511 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1513 if ((format
[index
] == '6') &&
1514 (format
[index
+ 1] == '4'))
1516 varsize
= sizeof(trio_int64_t
);
1519 else if ((format
[index
] == '3') &&
1520 (format
[index
+ 1] == '2'))
1522 varsize
= sizeof(trio_int32_t
);
1525 else if ((format
[index
] == '1') &&
1526 (format
[index
+ 1] == '6'))
1528 varsize
= sizeof(trio_int16_t
);
1531 else if (format
[index
] == '8')
1533 varsize
= sizeof(trio_int8_t
);
1537 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1539 flags
|= FLAGS_FIXED_SIZE
;
1543 #if defined(QUALIFIER_WIDECHAR)
1544 case QUALIFIER_WIDECHAR
:
1545 flags
|= FLAGS_WIDECHAR
;
1549 #if defined(QUALIFIER_SIZE_T_UPPER)
1550 case QUALIFIER_SIZE_T_UPPER
:
1554 #if defined(QUALIFIER_QUOTE)
1555 case QUALIFIER_QUOTE
:
1556 flags
|= FLAGS_QUOTE
;
1560 #if defined(QUALIFIER_STICKY)
1561 case QUALIFIER_STICKY
:
1562 flags
|= FLAGS_STICKY
;
1567 #if defined(QUALIFIER_VARSIZE)
1568 case QUALIFIER_VARSIZE
:
1569 flags
|= FLAGS_VARSIZE_PARAMETER
;
1570 parameterPosition
++;
1572 varsize
= parameterPosition
;
1575 varsize
= currentParam
;
1576 currentParam
= varsize
+ 1;
1578 if (currentParam
> maxParam
)
1579 maxParam
= currentParam
;
1583 #if defined(QUALIFIER_ROUNDING_UPPER)
1584 case QUALIFIER_ROUNDING_UPPER
:
1585 flags
|= FLAGS_ROUNDING
;
1590 /* Bail out completely to make the error more obvious */
1591 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1593 } /* while qualifier */
1596 * Parameters only need the type and value. The value is
1599 if (flags
& FLAGS_WIDTH_PARAMETER
)
1601 usedEntries
[width
] += 1;
1602 parameters
[pos
].type
= FORMAT_PARAMETER
;
1603 parameters
[pos
].flags
= 0;
1604 indices
[width
] = pos
;
1607 if (flags
& FLAGS_PRECISION_PARAMETER
)
1609 usedEntries
[precision
] += 1;
1610 parameters
[pos
].type
= FORMAT_PARAMETER
;
1611 parameters
[pos
].flags
= 0;
1612 indices
[precision
] = pos
;
1615 if (flags
& FLAGS_BASE_PARAMETER
)
1617 usedEntries
[base
] += 1;
1618 parameters
[pos
].type
= FORMAT_PARAMETER
;
1619 parameters
[pos
].flags
= 0;
1620 indices
[base
] = pos
;
1623 if (flags
& FLAGS_VARSIZE_PARAMETER
)
1625 usedEntries
[varsize
] += 1;
1626 parameters
[pos
].type
= FORMAT_PARAMETER
;
1627 parameters
[pos
].flags
= 0;
1628 indices
[varsize
] = pos
;
1632 indices
[currentParam
] = pos
;
1634 switch (format
[index
++])
1636 #if defined(SPECIFIER_CHAR_UPPER)
1637 case SPECIFIER_CHAR_UPPER
:
1638 flags
|= FLAGS_WIDECHAR
;
1641 case SPECIFIER_CHAR
:
1642 if (flags
& FLAGS_LONG
)
1643 flags
|= FLAGS_WIDECHAR
;
1644 else if (flags
& FLAGS_SHORT
)
1645 flags
&= ~FLAGS_WIDECHAR
;
1646 parameters
[pos
].type
= FORMAT_CHAR
;
1649 #if defined(SPECIFIER_STRING_UPPER)
1650 case SPECIFIER_STRING_UPPER
:
1651 flags
|= FLAGS_WIDECHAR
;
1654 case SPECIFIER_STRING
:
1655 if (flags
& FLAGS_LONG
)
1656 flags
|= FLAGS_WIDECHAR
;
1657 else if (flags
& FLAGS_SHORT
)
1658 flags
&= ~FLAGS_WIDECHAR
;
1659 parameters
[pos
].type
= FORMAT_STRING
;
1662 case SPECIFIER_GROUP
:
1663 if (TYPE_SCAN
== type
)
1666 parameters
[pos
].type
= FORMAT_GROUP
;
1667 if (format
[index
] == QUALIFIER_CIRCUMFLEX
)
1669 if (format
[index
] == SPECIFIER_UNGROUP
)
1671 if (format
[index
] == QUALIFIER_MINUS
)
1673 /* Skip nested brackets */
1674 while (format
[index
] != NIL
)
1676 if (format
[index
] == SPECIFIER_GROUP
)
1680 else if (format
[index
] == SPECIFIER_UNGROUP
)
1693 case SPECIFIER_INTEGER
:
1694 parameters
[pos
].type
= FORMAT_INT
;
1697 case SPECIFIER_UNSIGNED
:
1698 flags
|= FLAGS_UNSIGNED
;
1699 parameters
[pos
].type
= FORMAT_INT
;
1702 case SPECIFIER_DECIMAL
:
1703 /* Disable base modifier */
1704 flags
&= ~FLAGS_BASE_PARAMETER
;
1705 base
= BASE_DECIMAL
;
1706 parameters
[pos
].type
= FORMAT_INT
;
1709 case SPECIFIER_OCTAL
:
1710 flags
|= FLAGS_UNSIGNED
;
1711 flags
&= ~FLAGS_BASE_PARAMETER
;
1713 parameters
[pos
].type
= FORMAT_INT
;
1716 #if defined(SPECIFIER_BINARY)
1717 case SPECIFIER_BINARY_UPPER
:
1718 flags
|= FLAGS_UPPER
;
1720 case SPECIFIER_BINARY
:
1721 flags
|= FLAGS_NILPADDING
;
1722 flags
&= ~FLAGS_BASE_PARAMETER
;
1724 parameters
[pos
].type
= FORMAT_INT
;
1728 case SPECIFIER_HEX_UPPER
:
1729 flags
|= FLAGS_UPPER
;
1732 flags
|= FLAGS_UNSIGNED
;
1733 flags
&= ~FLAGS_BASE_PARAMETER
;
1735 parameters
[pos
].type
= FORMAT_INT
;
1738 case SPECIFIER_FLOAT_E_UPPER
:
1739 flags
|= FLAGS_UPPER
;
1741 case SPECIFIER_FLOAT_E
:
1742 flags
|= FLAGS_FLOAT_E
;
1743 parameters
[pos
].type
= FORMAT_DOUBLE
;
1746 case SPECIFIER_FLOAT_G_UPPER
:
1747 flags
|= FLAGS_UPPER
;
1749 case SPECIFIER_FLOAT_G
:
1750 flags
|= FLAGS_FLOAT_G
;
1751 parameters
[pos
].type
= FORMAT_DOUBLE
;
1754 case SPECIFIER_FLOAT_F_UPPER
:
1755 flags
|= FLAGS_UPPER
;
1757 case SPECIFIER_FLOAT_F
:
1758 parameters
[pos
].type
= FORMAT_DOUBLE
;
1761 case SPECIFIER_POINTER
:
1762 if (sizeof(trio_pointer_t
) == sizeof(trio_ulonglong_t
))
1763 flags
|= FLAGS_QUAD
;
1764 else if (sizeof(trio_pointer_t
) == sizeof(long))
1765 flags
|= FLAGS_LONG
;
1766 parameters
[pos
].type
= FORMAT_POINTER
;
1769 case SPECIFIER_COUNT
:
1770 parameters
[pos
].type
= FORMAT_COUNT
;
1773 #if defined(SPECIFIER_HEXFLOAT)
1774 # if defined(SPECIFIER_HEXFLOAT_UPPER)
1775 case SPECIFIER_HEXFLOAT_UPPER
:
1776 flags
|= FLAGS_UPPER
;
1779 case SPECIFIER_HEXFLOAT
:
1781 parameters
[pos
].type
= FORMAT_DOUBLE
;
1785 #if defined(FORMAT_ERRNO)
1786 case SPECIFIER_ERRNO
:
1787 parameters
[pos
].type
= FORMAT_ERRNO
;
1791 #if defined(SPECIFIER_USER_DEFINED_BEGIN)
1792 case SPECIFIER_USER_DEFINED_BEGIN
:
1795 int without_namespace
= TRUE
;
1797 parameters
[pos
].type
= FORMAT_USER_DEFINED
;
1798 parameters
[pos
].user_name
[0] = NIL
;
1799 tmpformat
= (char *)&format
[index
];
1801 while ((ch
= format
[index
]))
1804 if (ch
== SPECIFIER_USER_DEFINED_END
)
1806 if (without_namespace
)
1808 /* We must get the handle first */
1809 parameters
[pos
].type
= FORMAT_PARAMETER
;
1810 parameters
[pos
].indexAfterSpecifier
= index
;
1811 parameters
[pos
].flags
= FLAGS_USER_DEFINED
;
1812 /* Adjust parameters for insertion of new one */
1814 usedEntries
[currentParam
] += 1;
1815 parameters
[pos
].type
= FORMAT_USER_DEFINED
;
1817 indices
[currentParam
] = pos
;
1818 if (currentParam
> maxParam
)
1819 maxParam
= currentParam
;
1821 /* Copy the user data */
1822 max
= (unsigned int)(&format
[index
] - tmpformat
);
1823 if (max
> MAX_USER_DATA
)
1824 max
= MAX_USER_DATA
;
1825 trio_copy_max(parameters
[pos
].user_data
,
1830 if (ch
== SPECIFIER_USER_DEFINED_SEPARATOR
)
1832 without_namespace
= FALSE
;
1833 /* Copy the namespace for later looking-up */
1834 max
= (int)(&format
[index
] - tmpformat
);
1835 if (max
> MAX_USER_NAME
)
1836 max
= MAX_USER_NAME
;
1837 trio_copy_max(parameters
[pos
].user_name
,
1840 tmpformat
= (char *)&format
[index
];
1843 if (ch
!= SPECIFIER_USER_DEFINED_END
)
1844 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1847 #endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
1850 /* Bail out completely to make the error more obvious */
1851 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
1854 /* Count the number of times this entry has been used */
1855 usedEntries
[currentParam
] += 1;
1857 /* Find last sticky parameters */
1858 if (gotSticky
&& !(flags
& FLAGS_STICKY
))
1860 for (i
= pos
- 1; i
>= 0; i
--)
1862 if (parameters
[i
].type
== FORMAT_PARAMETER
)
1864 if ((parameters
[i
].flags
& FLAGS_STICKY
) &&
1865 (parameters
[i
].type
== parameters
[pos
].type
))
1867 /* Do not overwrite current qualifiers */
1868 flags
|= (parameters
[i
].flags
& (unsigned long)~FLAGS_STICKY
);
1869 if (width
== NO_WIDTH
)
1870 width
= parameters
[i
].width
;
1871 if (precision
== NO_PRECISION
)
1872 precision
= parameters
[i
].precision
;
1873 if (base
== NO_BASE
)
1874 base
= parameters
[i
].base
;
1880 parameters
[pos
].indexAfterSpecifier
= index
;
1881 parameters
[pos
].flags
= flags
;
1882 parameters
[pos
].width
= width
;
1883 parameters
[pos
].precision
= precision
;
1884 parameters
[pos
].base
= (base
== NO_BASE
) ? BASE_DECIMAL
: base
;
1885 parameters
[pos
].varsize
= varsize
;
1889 parameterPosition
++;
1891 } /* if identifier */
1893 } /* while format characters left */
1895 for (num
= 0; num
<= maxParam
; num
++)
1897 if (usedEntries
[num
] != 1)
1899 if (usedEntries
[num
] == 0) /* gap detected */
1900 return TRIO_ERROR_RETURN(TRIO_EGAP
, num
);
1901 else /* double references detected */
1902 return TRIO_ERROR_RETURN(TRIO_EDBLREF
, num
);
1908 * FORMAT_PARAMETERS are only present if they must be read,
1909 * so it makes no sense to check the ignore flag (besides,
1910 * the flags variable is not set for that particular type)
1912 if ((parameters
[i
].type
!= FORMAT_PARAMETER
) &&
1913 (parameters
[i
].flags
& FLAGS_IGNORE
))
1914 continue; /* for all arguments */
1917 * The stack arguments are read according to ANSI C89
1918 * default argument promotions:
1922 * unsigned char = unsigned int
1923 * unsigned short = unsigned int
1926 * In addition to the ANSI C89 these types are read (the
1927 * default argument promotions of C99 has not been
1936 switch (parameters
[i
].type
)
1941 if (flags
& FLAGS_WIDECHAR
)
1943 parameters
[i
].data
.wstring
= (argarray
== NULL
)
1944 ? va_arg(*arglist
, trio_wchar_t
*)
1945 : (trio_wchar_t
*)(argarray
[num
]);
1950 parameters
[i
].data
.string
= (argarray
== NULL
)
1951 ? va_arg(*arglist
, char *)
1952 : (char *)(argarray
[num
]);
1956 #if defined(FORMAT_USER_DEFINED)
1957 case FORMAT_USER_DEFINED
:
1959 case FORMAT_POINTER
:
1961 case FORMAT_UNKNOWN
:
1962 parameters
[i
].data
.pointer
= (argarray
== NULL
)
1963 ? va_arg(*arglist
, trio_pointer_t
)
1969 if (TYPE_SCAN
== type
)
1971 if (argarray
== NULL
)
1972 parameters
[i
].data
.pointer
=
1973 (trio_pointer_t
)va_arg(*arglist
, trio_pointer_t
);
1976 if (parameters
[i
].type
== FORMAT_CHAR
)
1977 parameters
[i
].data
.pointer
=
1978 (trio_pointer_t
)((char *)argarray
[num
]);
1979 else if (parameters
[i
].flags
& FLAGS_SHORT
)
1980 parameters
[i
].data
.pointer
=
1981 (trio_pointer_t
)((short *)argarray
[num
]);
1983 parameters
[i
].data
.pointer
=
1984 (trio_pointer_t
)((int *)argarray
[num
]);
1989 #if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
1990 if (parameters
[i
].flags
1991 & (FLAGS_VARSIZE_PARAMETER
| FLAGS_FIXED_SIZE
))
1993 if (parameters
[i
].flags
& FLAGS_VARSIZE_PARAMETER
)
1996 * Variable sizes are mapped onto the fixed sizes, in
1997 * accordance with integer promotion.
1999 * Please note that this may not be portable, as we
2000 * only guess the size, not the layout of the numbers.
2001 * For example, if int is little-endian, and long is
2002 * big-endian, then this will fail.
2004 varsize
= (int)parameters
[parameters
[i
].varsize
].data
.number
.as_unsigned
;
2008 /* Used for the I<bits> modifiers */
2009 varsize
= parameters
[i
].varsize
;
2011 parameters
[i
].flags
&= ~FLAGS_ALL_VARSIZES
;
2013 if (varsize
<= (int)sizeof(int))
2015 else if (varsize
<= (int)sizeof(long))
2016 parameters
[i
].flags
|= FLAGS_LONG
;
2017 #if defined(QUALIFIER_INTMAX_T)
2018 else if (varsize
<= (int)sizeof(trio_longlong_t
))
2019 parameters
[i
].flags
|= FLAGS_QUAD
;
2021 parameters
[i
].flags
|= FLAGS_INTMAX_T
;
2024 parameters
[i
].flags
|= FLAGS_QUAD
;
2027 #endif /* defined(QUALIFIER_VARSIZE) */
2028 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
2029 if (parameters
[i
].flags
& FLAGS_SIZE_T
)
2030 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2031 ? (trio_uintmax_t
)va_arg(*arglist
, size_t)
2032 : (trio_uintmax_t
)(*((size_t *)argarray
[num
]));
2035 #if defined(QUALIFIER_PTRDIFF_T)
2036 if (parameters
[i
].flags
& FLAGS_PTRDIFF_T
)
2037 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2038 ? (trio_uintmax_t
)va_arg(*arglist
, ptrdiff_t)
2039 : (trio_uintmax_t
)(*((ptrdiff_t *)argarray
[num
]));
2042 #if defined(QUALIFIER_INTMAX_T)
2043 if (parameters
[i
].flags
& FLAGS_INTMAX_T
)
2044 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2045 ? (trio_uintmax_t
)va_arg(*arglist
, trio_intmax_t
)
2046 : (trio_uintmax_t
)(*((trio_intmax_t
*)argarray
[num
]));
2049 if (parameters
[i
].flags
& FLAGS_QUAD
)
2050 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2051 ? (trio_uintmax_t
)va_arg(*arglist
, trio_ulonglong_t
)
2052 : (trio_uintmax_t
)(*((trio_ulonglong_t
*)argarray
[num
]));
2053 else if (parameters
[i
].flags
& FLAGS_LONG
)
2054 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2055 ? (trio_uintmax_t
)va_arg(*arglist
, long)
2056 : (trio_uintmax_t
)(*((long *)argarray
[num
]));
2059 if (argarray
== NULL
)
2060 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)va_arg(*arglist
, int);
2063 if (parameters
[i
].type
== FORMAT_CHAR
)
2064 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((char *)argarray
[num
]));
2065 else if (parameters
[i
].flags
& FLAGS_SHORT
)
2066 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((short *)argarray
[num
]));
2068 parameters
[i
].data
.number
.as_unsigned
= (trio_uintmax_t
)(*((int *)argarray
[num
]));
2074 case FORMAT_PARAMETER
:
2076 * The parameter for the user-defined specifier is a pointer,
2077 * whereas the rest (width, precision, base) uses an integer.
2079 if (parameters
[i
].flags
& FLAGS_USER_DEFINED
)
2080 parameters
[i
].data
.pointer
= (argarray
== NULL
)
2081 ? va_arg(*arglist
, trio_pointer_t
)
2084 parameters
[i
].data
.number
.as_unsigned
= (argarray
== NULL
)
2085 ? (trio_uintmax_t
)va_arg(*arglist
, int)
2086 : (trio_uintmax_t
)(*((int *)argarray
[num
]));
2090 if (TYPE_SCAN
== type
)
2092 if (parameters
[i
].flags
& FLAGS_LONGDOUBLE
)
2093 parameters
[i
].data
.longdoublePointer
= (argarray
== NULL
)
2094 ? va_arg(*arglist
, trio_long_double_t
*)
2095 : (trio_long_double_t
*)argarray
[num
];
2098 if (parameters
[i
].flags
& FLAGS_LONG
)
2099 parameters
[i
].data
.doublePointer
= (argarray
== NULL
)
2100 ? va_arg(*arglist
, double *)
2101 : (double *)argarray
[num
];
2103 parameters
[i
].data
.doublePointer
= (argarray
== NULL
)
2104 ? (double *)va_arg(*arglist
, float *)
2105 : (double *)((float *)argarray
[num
]);
2110 if (parameters
[i
].flags
& FLAGS_LONGDOUBLE
)
2111 parameters
[i
].data
.longdoubleNumber
= (argarray
== NULL
)
2112 ? va_arg(*arglist
, trio_long_double_t
)
2113 : (trio_long_double_t
)(*((trio_long_double_t
*)argarray
[num
]));
2116 if (argarray
== NULL
)
2117 parameters
[i
].data
.longdoubleNumber
=
2118 (trio_long_double_t
)va_arg(*arglist
, double);
2121 if (parameters
[i
].flags
& FLAGS_SHORT
)
2122 parameters
[i
].data
.longdoubleNumber
=
2123 (trio_long_double_t
)(*((float *)argarray
[num
]));
2125 parameters
[i
].data
.longdoubleNumber
=
2126 (trio_long_double_t
)(*((double *)argarray
[num
]));
2132 #if defined(FORMAT_ERRNO)
2134 parameters
[i
].data
.errorNumber
= save_errno
;
2141 } /* for all specifiers */
2146 /*************************************************************************
2150 ************************************************************************/
2153 /*************************************************************************
2158 * The complexity of this function is a result of the complexity
2159 * of the dependencies of the flags.
2163 TRIO_ARGS6((self
, number
, flags
, width
, precision
, base
),
2165 trio_uintmax_t number
,
2171 BOOLEAN_T isNegative
;
2172 BOOLEAN_T isNumberZero
;
2173 BOOLEAN_T isPrecisionZero
;
2174 BOOLEAN_T ignoreNumber
;
2175 char buffer
[MAX_CHARS_IN(trio_uintmax_t
) * (1 + MAX_LOCALE_SEPARATOR_LENGTH
) + 1];
2178 TRIO_CONST
char *digits
;
2184 assert(VALID(self
));
2185 assert(VALID(self
->OutStream
));
2186 assert(((base
>= MIN_BASE
) && (base
<= MAX_BASE
)) || (base
== NO_BASE
));
2188 digits
= (flags
& FLAGS_UPPER
) ? internalDigitsUpper
: internalDigitsLower
;
2189 if (base
== NO_BASE
)
2190 base
= BASE_DECIMAL
;
2192 isNumberZero
= (number
== 0);
2193 isPrecisionZero
= (precision
== 0);
2194 ignoreNumber
= (isNumberZero
2196 && !((flags
& FLAGS_ALTERNATIVE
) && (base
== BASE_OCTAL
)));
2198 if (flags
& FLAGS_UNSIGNED
)
2201 flags
&= ~FLAGS_SHOWSIGN
;
2205 isNegative
= ((trio_intmax_t
)number
< 0);
2207 number
= -((trio_intmax_t
)number
);
2210 if (flags
& FLAGS_QUAD
)
2211 number
&= (trio_ulonglong_t
)-1;
2212 else if (flags
& FLAGS_LONG
)
2213 number
&= (unsigned long)-1;
2215 number
&= (unsigned int)-1;
2218 pointer
= bufferend
= &buffer
[sizeof(buffer
) - 1];
2220 for (i
= 1; i
< (int)sizeof(buffer
); i
++)
2222 *pointer
-- = digits
[number
% base
];
2227 if ((flags
& FLAGS_QUOTE
) && TrioFollowedBySeparator(i
+ 1))
2230 * We are building the number from the least significant
2231 * to the most significant digit, so we have to copy the
2232 * thousand separator backwards
2234 length
= internalThousandSeparatorLength
;
2235 if (((int)(pointer
- buffer
) - length
) > 0)
2237 p
= &internalThousandSeparator
[length
- 1];
2238 while (length
-- > 0)
2247 width
-= (bufferend
- pointer
) - 1;
2250 /* Adjust precision */
2251 if (NO_PRECISION
!= precision
)
2253 precision
-= (bufferend
- pointer
) - 1;
2256 flags
|= FLAGS_NILPADDING
;
2259 /* Calculate padding */
2260 count
= (! ((flags
& FLAGS_LEFTADJUST
) || (precision
== NO_PRECISION
)))
2264 /* Adjust width further */
2265 if (isNegative
|| (flags
& FLAGS_SHOWSIGN
) || (flags
& FLAGS_SPACE
))
2267 if ((flags
& FLAGS_ALTERNATIVE
) && !isNumberZero
)
2276 if (!(flags
& FLAGS_NILPADDING
) || (count
== 0))
2284 /* Output prefixes spaces if needed */
2285 if (! ((flags
& FLAGS_LEFTADJUST
) ||
2286 ((flags
& FLAGS_NILPADDING
) && (precision
== NO_PRECISION
))))
2288 while (width
-- > count
)
2289 self
->OutStream(self
, CHAR_ADJUST
);
2292 /* width has been adjusted for signs and alternatives */
2294 self
->OutStream(self
, '-');
2295 else if (flags
& FLAGS_SHOWSIGN
)
2296 self
->OutStream(self
, '+');
2297 else if (flags
& FLAGS_SPACE
)
2298 self
->OutStream(self
, ' ');
2300 /* Prefix is not written when the value is zero */
2301 if ((flags
& FLAGS_ALTERNATIVE
) && !isNumberZero
)
2306 self
->OutStream(self
, '0');
2307 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'B' : 'b');
2311 if (!(flags
& FLAGS_NILPADDING
) || (count
== 0))
2312 self
->OutStream(self
, '0');
2316 self
->OutStream(self
, '0');
2317 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2325 /* Output prefixed zero padding if needed */
2326 if (flags
& FLAGS_NILPADDING
)
2328 if (precision
== NO_PRECISION
)
2330 while (precision
-- > 0)
2332 self
->OutStream(self
, '0');
2339 /* Output the number itself */
2340 while (*(++pointer
))
2342 self
->OutStream(self
, *pointer
);
2346 /* Output trailing spaces if needed */
2347 if (flags
& FLAGS_LEFTADJUST
)
2350 self
->OutStream(self
, CHAR_ADJUST
);
2354 /*************************************************************************
2355 * TrioWriteStringCharacter
2358 * Output a single character of a string
2361 TrioWriteStringCharacter
2362 TRIO_ARGS3((self
, ch
, flags
),
2367 if (flags
& FLAGS_ALTERNATIVE
)
2372 * Non-printable characters are converted to C escapes or
2373 * \number, if no C escape exists.
2375 self
->OutStream(self
, CHAR_BACKSLASH
);
2378 case '\007': self
->OutStream(self
, 'a'); break;
2379 case '\b': self
->OutStream(self
, 'b'); break;
2380 case '\f': self
->OutStream(self
, 'f'); break;
2381 case '\n': self
->OutStream(self
, 'n'); break;
2382 case '\r': self
->OutStream(self
, 'r'); break;
2383 case '\t': self
->OutStream(self
, 't'); break;
2384 case '\v': self
->OutStream(self
, 'v'); break;
2385 case '\\': self
->OutStream(self
, '\\'); break;
2387 self
->OutStream(self
, 'x');
2388 TrioWriteNumber(self
, (trio_uintmax_t
)ch
,
2389 FLAGS_UNSIGNED
| FLAGS_NILPADDING
,
2394 else if (ch
== CHAR_BACKSLASH
)
2396 self
->OutStream(self
, CHAR_BACKSLASH
);
2397 self
->OutStream(self
, CHAR_BACKSLASH
);
2401 self
->OutStream(self
, ch
);
2406 self
->OutStream(self
, ch
);
2410 /*************************************************************************
2418 TRIO_ARGS5((self
, string
, flags
, width
, precision
),
2420 TRIO_CONST
char *string
,
2428 assert(VALID(self
));
2429 assert(VALID(self
->OutStream
));
2433 string
= internalNullString
;
2434 length
= sizeof(internalNullString
) - 1;
2435 /* Disable quoting for the null pointer */
2436 flags
&= (~FLAGS_QUOTE
);
2441 length
= trio_length(string
);
2443 if ((NO_PRECISION
!= precision
) &&
2444 (precision
< length
))
2450 if (flags
& FLAGS_QUOTE
)
2451 self
->OutStream(self
, CHAR_QUOTE
);
2453 if (! (flags
& FLAGS_LEFTADJUST
))
2456 self
->OutStream(self
, CHAR_ADJUST
);
2459 while (length
-- > 0)
2461 /* The ctype parameters must be an unsigned char (or EOF) */
2462 ch
= (int)((unsigned char)(*string
++));
2463 TrioWriteStringCharacter(self
, ch
, flags
);
2466 if (flags
& FLAGS_LEFTADJUST
)
2469 self
->OutStream(self
, CHAR_ADJUST
);
2471 if (flags
& FLAGS_QUOTE
)
2472 self
->OutStream(self
, CHAR_QUOTE
);
2475 /*************************************************************************
2476 * TrioWriteWideStringCharacter
2479 * Output a wide string as a multi-byte sequence
2483 TrioWriteWideStringCharacter
2484 TRIO_ARGS4((self
, wch
, flags
, width
),
2494 char buffer
[MB_LEN_MAX
+ 1];
2496 if (width
== NO_WIDTH
)
2497 width
= sizeof(buffer
);
2499 size
= wctomb(buffer
, wch
);
2500 if ((size
<= 0) || (size
> width
) || (buffer
[0] == NIL
))
2505 while ((width
>= i
) && (width
-- > 0) && (i
-- > 0))
2507 /* The ctype parameters must be an unsigned char (or EOF) */
2508 ch
= (int)((unsigned char)(*string
++));
2509 TrioWriteStringCharacter(self
, ch
, flags
);
2513 #endif /* TRIO_WIDECHAR */
2515 /*************************************************************************
2516 * TrioWriteWideString
2519 * Output a wide character string as a multi-byte string
2524 TRIO_ARGS5((self
, wstring
, flags
, width
, precision
),
2526 TRIO_CONST trio_wchar_t
*wstring
,
2534 assert(VALID(self
));
2535 assert(VALID(self
->OutStream
));
2537 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
2538 (void)mblen(NULL
, 0);
2541 if (wstring
== NULL
)
2543 TrioWriteString(self
, NULL
, flags
, width
, precision
);
2547 if (NO_PRECISION
== precision
)
2557 if (flags
& FLAGS_QUOTE
)
2558 self
->OutStream(self
, CHAR_QUOTE
);
2560 if (! (flags
& FLAGS_LEFTADJUST
))
2563 self
->OutStream(self
, CHAR_ADJUST
);
2568 size
= TrioWriteWideStringCharacter(self
, *wstring
++, flags
, length
);
2574 if (flags
& FLAGS_LEFTADJUST
)
2577 self
->OutStream(self
, CHAR_ADJUST
);
2579 if (flags
& FLAGS_QUOTE
)
2580 self
->OutStream(self
, CHAR_QUOTE
);
2582 #endif /* TRIO_WIDECHAR */
2584 /*************************************************************************
2587 * http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_211.htm
2589 * "5.2.4.2.2 paragraph #4
2591 * The accuracy [...] is implementation defined, as is the accuracy
2592 * of the conversion between floating-point internal representations
2593 * and string representations performed by the libray routine in
2596 /* FIXME: handle all instances of constant long-double number (L)
2597 * and *l() math functions.
2601 TRIO_ARGS6((self
, number
, flags
, width
, precision
, base
),
2603 trio_long_double_t number
,
2609 trio_long_double_t integerNumber
;
2610 trio_long_double_t fractionNumber
;
2611 trio_long_double_t workNumber
;
2616 int integerThreshold
;
2617 int fractionThreshold
;
2620 unsigned int uExponent
= 0;
2622 trio_long_double_t dblBase
;
2623 trio_long_double_t dblIntegerBase
;
2624 trio_long_double_t dblFractionBase
;
2625 trio_long_double_t integerAdjust
;
2626 trio_long_double_t fractionAdjust
;
2627 BOOLEAN_T isNegative
;
2628 BOOLEAN_T isExponentNegative
= FALSE
;
2629 BOOLEAN_T requireTwoDigitExponent
;
2631 TRIO_CONST
char *digits
;
2632 char *groupingPointer
;
2635 BOOLEAN_T hasOnlyZeroes
;
2637 register int trailingZeroes
;
2638 BOOLEAN_T keepTrailingZeroes
;
2639 BOOLEAN_T keepDecimalPoint
;
2640 trio_long_double_t epsilon
;
2642 assert(VALID(self
));
2643 assert(VALID(self
->OutStream
));
2644 assert(((base
>= MIN_BASE
) && (base
<= MAX_BASE
)) || (base
== NO_BASE
));
2646 /* Determine sign and look for special quantities */
2647 switch (trio_fpclassify_and_signbit(number
, &isNegative
))
2650 TrioWriteString(self
,
2651 (flags
& FLAGS_UPPER
)
2654 flags
, width
, precision
);
2657 case TRIO_FP_INFINITE
:
2660 /* Negative infinity */
2661 TrioWriteString(self
,
2662 (flags
& FLAGS_UPPER
)
2663 ? "-" INFINITE_UPPER
2664 : "-" INFINITE_LOWER
,
2665 flags
, width
, precision
);
2670 /* Positive infinity */
2671 TrioWriteString(self
,
2672 (flags
& FLAGS_UPPER
)
2675 flags
, width
, precision
);
2684 /* Normal numbers */
2685 if (flags
& FLAGS_LONGDOUBLE
)
2687 baseDigits
= (base
== 10)
2689 : (int)floor(LDBL_MANT_DIG
/ TrioLogarithmBase(base
));
2690 epsilon
= LDBL_EPSILON
;
2692 else if (flags
& FLAGS_SHORT
)
2694 baseDigits
= (base
== BASE_DECIMAL
)
2696 : (int)floor(FLT_MANT_DIG
/ TrioLogarithmBase(base
));
2697 epsilon
= FLT_EPSILON
;
2701 baseDigits
= (base
== BASE_DECIMAL
)
2703 : (int)floor(DBL_MANT_DIG
/ TrioLogarithmBase(base
));
2704 epsilon
= DBL_EPSILON
;
2707 digits
= (flags
& FLAGS_UPPER
) ? internalDigitsUpper
: internalDigitsLower
;
2708 isHex
= (base
== BASE_HEX
);
2709 if (base
== NO_BASE
)
2710 base
= BASE_DECIMAL
;
2711 dblBase
= (trio_long_double_t
)base
;
2712 keepTrailingZeroes
= !( (flags
& FLAGS_ROUNDING
) ||
2713 ( (flags
& FLAGS_FLOAT_G
) &&
2714 !(flags
& FLAGS_ALTERNATIVE
) ) );
2716 if (flags
& FLAGS_ROUNDING
)
2717 precision
= baseDigits
;
2719 if (precision
== NO_PRECISION
)
2723 keepTrailingZeroes
= FALSE
;
2724 precision
= FLT_MANT_DIG
;
2728 precision
= FLT_DIG
;
2736 flags
|= FLAGS_FLOAT_E
;
2738 if (flags
& FLAGS_FLOAT_G
)
2743 if ((number
< 1.0E-4) || (number
> powl(base
,
2744 (trio_long_double_t
)precision
)))
2746 /* Use scientific notation */
2747 flags
|= FLAGS_FLOAT_E
;
2749 else if (number
< 1.0)
2752 * Use normal notation. If the integer part of the number is
2753 * zero, then adjust the precision to include leading fractional
2756 workNumber
= TrioLogarithm(number
, base
);
2757 workNumber
= TRIO_FABS(workNumber
);
2758 if (workNumber
- floorl(workNumber
) < 0.001)
2760 zeroes
= (int)floorl(workNumber
);
2764 if (flags
& FLAGS_FLOAT_E
)
2766 /* Scale the number */
2767 workNumber
= TrioLogarithm(number
, base
);
2768 if (trio_isinf(workNumber
) == -1)
2772 if (flags
& FLAGS_FLOAT_G
)
2773 flags
&= ~FLAGS_FLOAT_E
;
2777 exponent
= (int)floorl(workNumber
);
2778 number
/= powl(dblBase
, (trio_long_double_t
)exponent
);
2779 isExponentNegative
= (exponent
< 0);
2780 uExponent
= (isExponentNegative
) ? -exponent
: exponent
;
2782 uExponent
*= 4; /* log16(2) */
2783 /* No thousand separators */
2784 flags
&= ~FLAGS_QUOTE
;
2788 integerNumber
= floorl(number
);
2789 fractionNumber
= number
- integerNumber
;
2794 * Precision is number of significant digits for FLOAT_G
2795 * and number of fractional digits for others.
2797 integerDigits
= (integerNumber
> epsilon
)
2798 ? 1 + (int)TrioLogarithm(integerNumber
, base
)
2800 fractionDigits
= ((flags
& FLAGS_FLOAT_G
) && (zeroes
== 0))
2801 ? precision
- integerDigits
2802 : zeroes
+ precision
;
2804 dblFractionBase
= TrioPower(base
, fractionDigits
);
2806 workNumber
= number
+ 0.5 / dblFractionBase
;
2807 if (floorl(number
) != floorl(workNumber
))
2809 if (flags
& FLAGS_FLOAT_E
)
2811 /* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
2813 isExponentNegative
= (exponent
< 0);
2814 uExponent
= (isExponentNegative
) ? -exponent
: exponent
;
2816 uExponent
*= 4; /* log16(2) */
2817 workNumber
= (number
+ 0.5 / dblFractionBase
) / dblBase
;
2818 integerNumber
= floorl(workNumber
);
2819 fractionNumber
= workNumber
- integerNumber
;
2823 /* Adjust if number was rounded up one digit (ie. 99 to 100) */
2824 integerNumber
= floorl(number
+ 0.5);
2825 fractionNumber
= 0.0;
2826 integerDigits
= (integerNumber
> epsilon
)
2827 ? 1 + (int)TrioLogarithm(integerNumber
, base
)
2832 /* Estimate accuracy */
2833 integerAdjust
= fractionAdjust
= 0.5;
2834 if (flags
& FLAGS_ROUNDING
)
2836 if (integerDigits
> baseDigits
)
2838 integerThreshold
= baseDigits
;
2840 dblFractionBase
= 1.0;
2841 fractionThreshold
= 0;
2842 precision
= 0; /* Disable decimal-point */
2843 integerAdjust
= TrioPower(base
, integerDigits
- integerThreshold
- 1);
2844 fractionAdjust
= 0.0;
2848 integerThreshold
= integerDigits
;
2849 fractionThreshold
= fractionDigits
- integerThreshold
;
2850 fractionAdjust
= 1.0;
2855 integerThreshold
= INT_MAX
;
2856 fractionThreshold
= INT_MAX
;
2860 * Calculate expected width.
2861 * sign + integer part + thousands separators + decimal point
2862 * + fraction + exponent
2864 fractionAdjust
/= dblFractionBase
;
2865 hasOnlyZeroes
= (floorl((fractionNumber
+ fractionAdjust
) * dblFractionBase
) < epsilon
);
2866 keepDecimalPoint
= ( (flags
& FLAGS_ALTERNATIVE
) ||
2867 !((precision
== 0) ||
2868 (!keepTrailingZeroes
&& hasOnlyZeroes
)) );
2869 if (flags
& FLAGS_FLOAT_E
)
2871 exponentDigits
= (uExponent
== 0)
2873 : (int)ceil(TrioLogarithm((double)(uExponent
+ 1),
2874 (isHex
) ? 10.0 : base
));
2878 requireTwoDigitExponent
= ((base
== BASE_DECIMAL
) && (exponentDigits
== 1));
2880 expectedWidth
= integerDigits
+ fractionDigits
2882 ? internalDecimalPointLength
2884 + ((flags
& FLAGS_QUOTE
)
2885 ? TrioCalcThousandSeparatorLength(integerDigits
)
2887 if (isNegative
|| (flags
& FLAGS_SHOWSIGN
) || (flags
& FLAGS_SPACE
))
2888 expectedWidth
+= sizeof("-") - 1;
2889 if (exponentDigits
> 0)
2890 expectedWidth
+= exponentDigits
+
2891 ((requireTwoDigitExponent
? sizeof("E+0") : sizeof("E+")) - 1);
2893 expectedWidth
+= sizeof("0X") - 1;
2895 /* Output prefixing */
2896 if (flags
& FLAGS_NILPADDING
)
2898 /* Leading zeros must be after sign */
2900 self
->OutStream(self
, '-');
2901 else if (flags
& FLAGS_SHOWSIGN
)
2902 self
->OutStream(self
, '+');
2903 else if (flags
& FLAGS_SPACE
)
2904 self
->OutStream(self
, ' ');
2907 self
->OutStream(self
, '0');
2908 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2910 if (!(flags
& FLAGS_LEFTADJUST
))
2912 for (i
= expectedWidth
; i
< width
; i
++)
2914 self
->OutStream(self
, '0');
2920 /* Leading spaces must be before sign */
2921 if (!(flags
& FLAGS_LEFTADJUST
))
2923 for (i
= expectedWidth
; i
< width
; i
++)
2925 self
->OutStream(self
, CHAR_ADJUST
);
2929 self
->OutStream(self
, '-');
2930 else if (flags
& FLAGS_SHOWSIGN
)
2931 self
->OutStream(self
, '+');
2932 else if (flags
& FLAGS_SPACE
)
2933 self
->OutStream(self
, ' ');
2936 self
->OutStream(self
, '0');
2937 self
->OutStream(self
, (flags
& FLAGS_UPPER
) ? 'X' : 'x');
2941 /* Output the integer part and thousand separators */
2942 dblIntegerBase
= 1.0 / TrioPower(base
, integerDigits
- 1);
2943 for (i
= 0; i
< integerDigits
; i
++)
2945 workNumber
= floorl(((integerNumber
+ integerAdjust
) * dblIntegerBase
));
2946 if (i
> integerThreshold
)
2948 /* Beyond accuracy */
2949 self
->OutStream(self
, digits
[0]);
2953 self
->OutStream(self
, digits
[(int)fmodl(workNumber
, dblBase
)]);
2955 dblIntegerBase
*= dblBase
;
2957 if (((flags
& (FLAGS_FLOAT_E
| FLAGS_QUOTE
)) == FLAGS_QUOTE
)
2958 && TrioFollowedBySeparator(integerDigits
- i
))
2960 for (groupingPointer
= internalThousandSeparator
;
2961 *groupingPointer
!= NIL
;
2964 self
->OutStream(self
, *groupingPointer
);
2969 /* Insert decimal point and build the fraction part */
2972 if (keepDecimalPoint
)
2974 if (internalDecimalPoint
)
2976 self
->OutStream(self
, internalDecimalPoint
);
2980 for (i
= 0; i
< internalDecimalPointLength
; i
++)
2982 self
->OutStream(self
, internalDecimalPointString
[i
]);
2987 for (i
= 0; i
< fractionDigits
; i
++)
2989 if ((integerDigits
> integerThreshold
) || (i
> fractionThreshold
))
2991 /* Beyond accuracy */
2996 fractionNumber
*= dblBase
;
2997 fractionAdjust
*= dblBase
;
2998 workNumber
= floorl(fractionNumber
+ fractionAdjust
);
2999 fractionNumber
-= workNumber
;
3000 index
= (int)fmodl(workNumber
, dblBase
);
3007 while (trailingZeroes
> 0)
3009 /* Not trailing zeroes after all */
3010 self
->OutStream(self
, digits
[0]);
3013 self
->OutStream(self
, digits
[index
]);
3018 if (keepTrailingZeroes
)
3020 while (trailingZeroes
> 0)
3022 self
->OutStream(self
, digits
[0]);
3027 /* Output exponent */
3028 if (exponentDigits
> 0)
3030 self
->OutStream(self
,
3032 ? ((flags
& FLAGS_UPPER
) ? 'P' : 'p')
3033 : ((flags
& FLAGS_UPPER
) ? 'E' : 'e'));
3034 self
->OutStream(self
, (isExponentNegative
) ? '-' : '+');
3036 /* The exponent must contain at least two digits */
3037 if (requireTwoDigitExponent
)
3038 self
->OutStream(self
, '0');
3042 exponentBase
= (int)TrioPower(base
, exponentDigits
- 1);
3043 for (i
= 0; i
< exponentDigits
; i
++)
3045 self
->OutStream(self
, digits
[(uExponent
/ exponentBase
) % base
]);
3046 exponentBase
/= base
;
3049 /* Output trailing spaces */
3050 if (flags
& FLAGS_LEFTADJUST
)
3052 for (i
= expectedWidth
; i
< width
; i
++)
3054 self
->OutStream(self
, CHAR_ADJUST
);
3059 /*************************************************************************
3063 * This is the main engine for formatting output
3067 TRIO_ARGS3((data
, format
, parameters
),
3069 TRIO_CONST
char *format
,
3070 trio_parameter_t
*parameters
)
3072 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3076 TRIO_CONST
char *string
;
3077 trio_pointer_t pointer
;
3086 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3087 (void)mblen(NULL
, 0);
3090 while (format
[index
])
3092 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3093 if (! isascii(format
[index
]))
3095 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
3097 * Only valid multibyte characters are handled here. Invalid
3098 * multibyte characters (charlen == -1) are handled as normal
3103 while (charlen
-- > 0)
3105 data
->OutStream(data
, format
[index
++]);
3107 continue; /* while characters left in formatting string */
3110 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
3111 if (CHAR_IDENTIFIER
== format
[index
])
3113 if (CHAR_IDENTIFIER
== format
[index
+ 1])
3115 data
->OutStream(data
, CHAR_IDENTIFIER
);
3120 /* Skip the parameter entries */
3121 while (parameters
[i
].type
== FORMAT_PARAMETER
)
3124 flags
= parameters
[i
].flags
;
3127 width
= parameters
[i
].width
;
3128 if (flags
& FLAGS_WIDTH_PARAMETER
)
3130 /* Get width from parameter list */
3131 width
= (int)parameters
[width
].data
.number
.as_signed
;
3135 * A negative width is the same as the - flag and
3138 flags
|= FLAGS_LEFTADJUST
;
3139 flags
&= ~FLAGS_NILPADDING
;
3144 /* Find precision */
3145 if (flags
& FLAGS_PRECISION
)
3147 precision
= parameters
[i
].precision
;
3148 if (flags
& FLAGS_PRECISION_PARAMETER
)
3150 /* Get precision from parameter list */
3151 precision
= (int)parameters
[precision
].data
.number
.as_signed
;
3155 * A negative precision is the same as no
3158 precision
= NO_PRECISION
;
3164 precision
= NO_PRECISION
;
3168 base
= parameters
[i
].base
;
3169 if (flags
& FLAGS_BASE_PARAMETER
)
3171 /* Get base from parameter list */
3172 base
= (int)parameters
[base
].data
.number
.as_signed
;
3175 switch (parameters
[i
].type
)
3178 if (flags
& FLAGS_QUOTE
)
3179 data
->OutStream(data
, CHAR_QUOTE
);
3180 if (! (flags
& FLAGS_LEFTADJUST
))
3183 data
->OutStream(data
, CHAR_ADJUST
);
3186 if (flags
& FLAGS_WIDECHAR
)
3188 TrioWriteWideStringCharacter(data
,
3189 (trio_wchar_t
)parameters
[i
].data
.number
.as_signed
,
3196 TrioWriteStringCharacter(data
,
3197 (int)parameters
[i
].data
.number
.as_signed
,
3201 if (flags
& FLAGS_LEFTADJUST
)
3204 data
->OutStream(data
, CHAR_ADJUST
);
3206 if (flags
& FLAGS_QUOTE
)
3207 data
->OutStream(data
, CHAR_QUOTE
);
3209 break; /* FORMAT_CHAR */
3212 TrioWriteNumber(data
,
3213 parameters
[i
].data
.number
.as_unsigned
,
3219 break; /* FORMAT_INT */
3222 TrioWriteDouble(data
,
3223 parameters
[i
].data
.longdoubleNumber
,
3228 break; /* FORMAT_DOUBLE */
3232 if (flags
& FLAGS_WIDECHAR
)
3234 TrioWriteWideString(data
,
3235 parameters
[i
].data
.wstring
,
3243 TrioWriteString(data
,
3244 parameters
[i
].data
.string
,
3249 break; /* FORMAT_STRING */
3251 case FORMAT_POINTER
:
3253 trio_reference_t reference
;
3255 reference
.data
= data
;
3256 reference
.parameter
= ¶meters
[i
];
3257 trio_print_pointer(&reference
, parameters
[i
].data
.pointer
);
3259 break; /* FORMAT_POINTER */
3262 pointer
= parameters
[i
].data
.pointer
;
3263 if (NULL
!= pointer
)
3266 * C99 paragraph 7.19.6.1.8 says "the number of
3267 * characters written to the output stream so far by
3268 * this call", which is data->committed
3270 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
3271 if (flags
& FLAGS_SIZE_T
)
3272 *(size_t *)pointer
= (size_t)data
->committed
;
3275 #if defined(QUALIFIER_PTRDIFF_T)
3276 if (flags
& FLAGS_PTRDIFF_T
)
3277 *(ptrdiff_t *)pointer
= (ptrdiff_t)data
->committed
;
3280 #if defined(QUALIFIER_INTMAX_T)
3281 if (flags
& FLAGS_INTMAX_T
)
3282 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)data
->committed
;
3285 if (flags
& FLAGS_QUAD
)
3287 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)data
->committed
;
3289 else if (flags
& FLAGS_LONG
)
3291 *(long int *)pointer
= (long int)data
->committed
;
3293 else if (flags
& FLAGS_SHORT
)
3295 *(short int *)pointer
= (short int)data
->committed
;
3299 *(int *)pointer
= (int)data
->committed
;
3302 break; /* FORMAT_COUNT */
3304 case FORMAT_PARAMETER
:
3305 break; /* FORMAT_PARAMETER */
3307 #if defined(FORMAT_ERRNO)
3309 string
= trio_error(parameters
[i
].data
.errorNumber
);
3312 TrioWriteString(data
,
3320 data
->OutStream(data
, '#');
3321 TrioWriteNumber(data
,
3322 (trio_uintmax_t
)parameters
[i
].data
.errorNumber
,
3328 break; /* FORMAT_ERRNO */
3329 #endif /* defined(FORMAT_ERRNO) */
3331 #if defined(FORMAT_USER_DEFINED)
3332 case FORMAT_USER_DEFINED
:
3334 trio_reference_t reference
;
3335 trio_userdef_t
*def
= NULL
;
3337 if (parameters
[i
].user_name
[0] == NIL
)
3341 (parameters
[i
- 1].type
== FORMAT_PARAMETER
))
3342 def
= (trio_userdef_t
*)parameters
[i
- 1].data
.pointer
;
3346 /* Look up namespace */
3347 def
= TrioFindNamespace(parameters
[i
].user_name
, NULL
);
3350 reference
.data
= data
;
3351 reference
.parameter
= ¶meters
[i
];
3352 def
->callback(&reference
);
3356 #endif /* defined(FORMAT_USER_DEFINED) */
3360 } /* switch parameter type */
3362 /* Prepare for next */
3363 index
= parameters
[i
].indexAfterSpecifier
;
3367 else /* not identifier */
3369 data
->OutStream(data
, format
[index
++]);
3372 return data
->processed
;
3375 /*************************************************************************
3380 TRIO_ARGS4((reference
, format
, arglist
, argarray
),
3381 trio_reference_t
*reference
,
3382 TRIO_CONST
char *format
,
3384 trio_pointer_t
*argarray
)
3387 trio_parameter_t parameters
[MAX_PARAMETERS
];
3389 status
= TrioParse(TYPE_PRINT
, format
, parameters
, arglist
, argarray
);
3393 status
= TrioFormatProcess(reference
->data
, format
, parameters
);
3394 if (reference
->data
->error
!= 0)
3396 status
= reference
->data
->error
;
3401 /*************************************************************************
3406 TRIO_ARGS6((destination
, destinationSize
, OutStream
, format
, arglist
, argarray
),
3407 trio_pointer_t destination
,
3408 size_t destinationSize
,
3409 void (*OutStream
) TRIO_PROTO((trio_class_t
*, int)),
3410 TRIO_CONST
char *format
,
3412 trio_pointer_t
*argarray
)
3416 trio_parameter_t parameters
[MAX_PARAMETERS
];
3418 assert(VALID(OutStream
));
3419 assert(VALID(format
));
3421 memset(&data
, 0, sizeof(data
));
3422 data
.OutStream
= OutStream
;
3423 data
.location
= destination
;
3424 data
.max
= destinationSize
;
3427 #if defined(USE_LOCALE)
3428 if (NULL
== internalLocaleValues
)
3434 status
= TrioParse(TYPE_PRINT
, format
, parameters
, arglist
, argarray
);
3438 status
= TrioFormatProcess(&data
, format
, parameters
);
3439 if (data
.error
!= 0)
3441 status
= data
.error
;
3446 /*************************************************************************
3451 TRIO_ARGS2((self
, output
),
3457 assert(VALID(self
));
3458 assert(VALID(self
->location
));
3460 file
= (FILE *)self
->location
;
3462 if (fputc(output
, file
) == EOF
)
3464 self
->error
= TRIO_ERROR_RETURN(TRIO_EOF
, 0);
3472 /*************************************************************************
3473 * TrioOutStreamFileDescriptor
3476 TrioOutStreamFileDescriptor
3477 TRIO_ARGS2((self
, output
),
3484 assert(VALID(self
));
3486 fd
= *((int *)self
->location
);
3489 if (write(fd
, &ch
, sizeof(char)) == -1)
3491 self
->error
= TRIO_ERROR_RETURN(TRIO_ERRNO
, 0);
3499 /*************************************************************************
3500 * TrioOutStreamCustom
3504 TRIO_ARGS2((self
, output
),
3509 trio_custom_t
*data
;
3511 assert(VALID(self
));
3512 assert(VALID(self
->location
));
3514 data
= (trio_custom_t
*)self
->location
;
3515 if (data
->stream
.out
)
3517 status
= (data
->stream
.out
)(data
->closure
, output
);
3524 if (self
->error
== 0)
3526 self
->error
= TRIO_ERROR_RETURN(TRIO_ECUSTOM
, -status
);
3533 /*************************************************************************
3534 * TrioOutStreamString
3538 TRIO_ARGS2((self
, output
),
3544 assert(VALID(self
));
3545 assert(VALID(self
->location
));
3547 buffer
= (char **)self
->location
;
3548 **buffer
= (char)output
;
3554 /*************************************************************************
3555 * TrioOutStreamStringMax
3558 TrioOutStreamStringMax
3559 TRIO_ARGS2((self
, output
),
3565 assert(VALID(self
));
3566 assert(VALID(self
->location
));
3568 buffer
= (char **)self
->location
;
3570 if (self
->processed
< self
->max
)
3572 **buffer
= (char)output
;
3579 /*************************************************************************
3580 * TrioOutStreamStringDynamic
3583 TrioOutStreamStringDynamic
3584 TRIO_ARGS2((self
, output
),
3588 assert(VALID(self
));
3589 assert(VALID(self
->location
));
3591 if (self
->error
== 0)
3593 trio_xstring_append_char((trio_string_t
*)self
->location
,
3597 /* The processed variable must always be increased */
3601 /*************************************************************************
3603 * Formatted printing functions
3605 ************************************************************************/
3607 #if defined(TRIO_DOCUMENTATION)
3608 # include "doc/doc_printf.h"
3610 /** @addtogroup Printf
3614 /*************************************************************************
3619 Print to standard output stream.
3621 @param format Formatting string.
3622 @param ... Arguments.
3623 @return Number of printed characters.
3627 TRIO_VARGS2((format
, va_alist
),
3628 TRIO_CONST
char *format
,
3634 assert(VALID(format
));
3636 TRIO_VA_START(args
, format
);
3637 status
= TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, &args
, NULL
);
3643 Print to standard output stream.
3645 @param format Formatting string.
3646 @param args Arguments.
3647 @return Number of printed characters.
3651 TRIO_ARGS2((format
, args
),
3652 TRIO_CONST
char *format
,
3655 assert(VALID(format
));
3657 return TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, &args
, NULL
);
3661 Print to standard output stream.
3663 @param format Formatting string.
3664 @param args Arguments.
3665 @return Number of printed characters.
3669 TRIO_ARGS2((format
, args
),
3670 TRIO_CONST
char *format
,
3671 trio_pointer_t
* args
)
3673 assert(VALID(format
));
3675 return TrioFormat(stdout
, 0, TrioOutStreamFile
, format
, NULL
, args
);
3678 /*************************************************************************
3685 @param file File pointer.
3686 @param format Formatting string.
3687 @param ... Arguments.
3688 @return Number of printed characters.
3692 TRIO_VARGS3((file
, format
, va_alist
),
3694 TRIO_CONST
char *format
,
3700 assert(VALID(file
));
3701 assert(VALID(format
));
3703 TRIO_VA_START(args
, format
);
3704 status
= TrioFormat(file
, 0, TrioOutStreamFile
, format
, &args
, NULL
);
3712 @param file File pointer.
3713 @param format Formatting string.
3714 @param args Arguments.
3715 @return Number of printed characters.
3719 TRIO_ARGS3((file
, format
, args
),
3721 TRIO_CONST
char *format
,
3724 assert(VALID(file
));
3725 assert(VALID(format
));
3727 return TrioFormat(file
, 0, TrioOutStreamFile
, format
, &args
, NULL
);
3733 @param file File pointer.
3734 @param format Formatting string.
3735 @param args Arguments.
3736 @return Number of printed characters.
3740 TRIO_ARGS3((file
, format
, args
),
3742 TRIO_CONST
char *format
,
3743 trio_pointer_t
* args
)
3745 assert(VALID(file
));
3746 assert(VALID(format
));
3748 return TrioFormat(file
, 0, TrioOutStreamFile
, format
, NULL
, args
);
3751 /*************************************************************************
3756 Print to file descriptor.
3758 @param fd File descriptor.
3759 @param format Formatting string.
3760 @param ... Arguments.
3761 @return Number of printed characters.
3765 TRIO_VARGS3((fd
, format
, va_alist
),
3767 TRIO_CONST
char *format
,
3773 assert(VALID(format
));
3775 TRIO_VA_START(args
, format
);
3776 status
= TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, &args
, NULL
);
3782 Print to file descriptor.
3784 @param fd File descriptor.
3785 @param format Formatting string.
3786 @param args Arguments.
3787 @return Number of printed characters.
3791 TRIO_ARGS3((fd
, format
, args
),
3793 TRIO_CONST
char *format
,
3796 assert(VALID(format
));
3798 return TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, &args
, NULL
);
3802 Print to file descriptor.
3804 @param fd File descriptor.
3805 @param format Formatting string.
3806 @param args Arguments.
3807 @return Number of printed characters.
3811 TRIO_ARGS3((fd
, format
, args
),
3813 TRIO_CONST
char *format
,
3814 trio_pointer_t
*args
)
3816 assert(VALID(format
));
3818 return TrioFormat(&fd
, 0, TrioOutStreamFileDescriptor
, format
, NULL
, args
);
3821 /*************************************************************************
3826 TRIO_VARGS4((stream
, closure
, format
, va_alist
),
3827 trio_outstream_t stream
,
3828 trio_pointer_t closure
,
3829 TRIO_CONST
char *format
,
3836 assert(VALID(stream
));
3837 assert(VALID(format
));
3839 TRIO_VA_START(args
, format
);
3840 data
.stream
.out
= stream
;
3841 data
.closure
= closure
;
3842 status
= TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, &args
, NULL
);
3849 TRIO_ARGS4((stream
, closure
, format
, args
),
3850 trio_outstream_t stream
,
3851 trio_pointer_t closure
,
3852 TRIO_CONST
char *format
,
3857 assert(VALID(stream
));
3858 assert(VALID(format
));
3860 data
.stream
.out
= stream
;
3861 data
.closure
= closure
;
3862 return TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, &args
, NULL
);
3867 TRIO_ARGS4((stream
, closure
, format
, args
),
3868 trio_outstream_t stream
,
3869 trio_pointer_t closure
,
3870 TRIO_CONST
char *format
,
3875 assert(VALID(stream
));
3876 assert(VALID(format
));
3878 data
.stream
.out
= stream
;
3879 data
.closure
= closure
;
3880 return TrioFormat(&data
, 0, TrioOutStreamCustom
, format
, NULL
, args
);
3883 /*************************************************************************
3890 @param buffer Output string.
3891 @param format Formatting string.
3892 @param ... Arguments.
3893 @return Number of printed characters.
3897 TRIO_VARGS3((buffer
, format
, va_alist
),
3899 TRIO_CONST
char *format
,
3905 assert(VALID(buffer
));
3906 assert(VALID(format
));
3908 TRIO_VA_START(args
, format
);
3909 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, &args
, NULL
);
3910 *buffer
= NIL
; /* Terminate with NIL character */
3918 @param buffer Output string.
3919 @param format Formatting string.
3920 @param args Arguments.
3921 @return Number of printed characters.
3925 TRIO_ARGS3((buffer
, format
, args
),
3927 TRIO_CONST
char *format
,
3932 assert(VALID(buffer
));
3933 assert(VALID(format
));
3935 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, &args
, NULL
);
3943 @param buffer Output string.
3944 @param format Formatting string.
3945 @param args Arguments.
3946 @return Number of printed characters.
3950 TRIO_ARGS3((buffer
, format
, args
),
3952 TRIO_CONST
char *format
,
3953 trio_pointer_t
*args
)
3957 assert(VALID(buffer
));
3958 assert(VALID(format
));
3960 status
= TrioFormat(&buffer
, 0, TrioOutStreamString
, format
, NULL
, args
);
3965 /*************************************************************************
3970 Print at most @p max characters to string.
3972 @param buffer Output string.
3973 @param max Maximum number of characters to print.
3974 @param format Formatting string.
3975 @param ... Arguments.
3976 @return Number of printed characters.
3980 TRIO_VARGS4((buffer
, max
, format
, va_alist
),
3983 TRIO_CONST
char *format
,
3989 assert(VALID(buffer
));
3990 assert(VALID(format
));
3992 TRIO_VA_START(args
, format
);
3993 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
3994 TrioOutStreamStringMax
, format
, &args
, NULL
);
4002 Print at most @p max characters to string.
4004 @param buffer Output string.
4005 @param max Maximum number of characters to print.
4006 @param format Formatting string.
4007 @param args Arguments.
4008 @return Number of printed characters.
4012 TRIO_ARGS4((buffer
, max
, format
, args
),
4015 TRIO_CONST
char *format
,
4020 assert(VALID(buffer
));
4021 assert(VALID(format
));
4023 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
4024 TrioOutStreamStringMax
, format
, &args
, NULL
);
4031 Print at most @p max characters to string.
4033 @param buffer Output string.
4034 @param max Maximum number of characters to print.
4035 @param format Formatting string.
4036 @param args Arguments.
4037 @return Number of printed characters.
4041 TRIO_ARGS4((buffer
, max
, format
, args
),
4044 TRIO_CONST
char *format
,
4045 trio_pointer_t
*args
)
4049 assert(VALID(buffer
));
4050 assert(VALID(format
));
4052 status
= TrioFormat(&buffer
, max
> 0 ? max
- 1 : 0,
4053 TrioOutStreamStringMax
, format
, NULL
, args
);
4059 /*************************************************************************
4061 * Appends the new string to the buffer string overwriting the '\0'
4062 * character at the end of buffer.
4066 TRIO_VARGS4((buffer
, max
, format
, va_alist
),
4069 TRIO_CONST
char *format
,
4076 TRIO_VA_START(args
, format
);
4078 assert(VALID(buffer
));
4079 assert(VALID(format
));
4081 buf_len
= trio_length(buffer
);
4082 buffer
= &buffer
[buf_len
];
4084 status
= TrioFormat(&buffer
, max
- 1 - buf_len
,
4085 TrioOutStreamStringMax
, format
, &args
, NULL
);
4093 TRIO_ARGS4((buffer
, max
, format
, args
),
4096 TRIO_CONST
char *format
,
4102 assert(VALID(buffer
));
4103 assert(VALID(format
));
4105 buf_len
= trio_length(buffer
);
4106 buffer
= &buffer
[buf_len
];
4107 status
= TrioFormat(&buffer
, max
- 1 - buf_len
,
4108 TrioOutStreamStringMax
, format
, &args
, NULL
);
4113 /*************************************************************************
4120 TRIO_VARGS2((format
, va_alist
),
4121 TRIO_CONST
char *format
,
4125 trio_string_t
*info
;
4126 char *result
= NULL
;
4128 assert(VALID(format
));
4130 info
= trio_xstring_duplicate("");
4133 TRIO_VA_START(args
, format
);
4134 (void)TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4135 format
, &args
, NULL
);
4138 trio_string_terminate(info
);
4139 result
= trio_string_extract(info
);
4140 trio_string_destroy(info
);
4148 TRIO_ARGS2((format
, args
),
4149 TRIO_CONST
char *format
,
4152 trio_string_t
*info
;
4153 char *result
= NULL
;
4155 assert(VALID(format
));
4157 info
= trio_xstring_duplicate("");
4160 (void)TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4161 format
, &args
, NULL
);
4162 trio_string_terminate(info
);
4163 result
= trio_string_extract(info
);
4164 trio_string_destroy(info
);
4171 TRIO_VARGS3((result
, format
, va_alist
),
4173 TRIO_CONST
char *format
,
4178 trio_string_t
*info
;
4180 assert(VALID(format
));
4184 info
= trio_xstring_duplicate("");
4187 status
= TRIO_ERROR_RETURN(TRIO_ENOMEM
, 0);
4191 TRIO_VA_START(args
, format
);
4192 status
= TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4193 format
, &args
, NULL
);
4197 trio_string_terminate(info
);
4198 *result
= trio_string_extract(info
);
4200 trio_string_destroy(info
);
4207 TRIO_ARGS3((result
, format
, args
),
4209 TRIO_CONST
char *format
,
4213 trio_string_t
*info
;
4215 assert(VALID(format
));
4219 info
= trio_xstring_duplicate("");
4222 status
= TRIO_ERROR_RETURN(TRIO_ENOMEM
, 0);
4226 status
= TrioFormat(info
, 0, TrioOutStreamStringDynamic
,
4227 format
, &args
, NULL
);
4230 trio_string_terminate(info
);
4231 *result
= trio_string_extract(info
);
4233 trio_string_destroy(info
);
4238 /** @} End of Printf documentation module */
4240 /*************************************************************************
4244 ************************************************************************/
4246 #if defined(TRIO_DOCUMENTATION)
4247 # include "doc/doc_register.h"
4250 @addtogroup UserDefined
4256 /*************************************************************************
4261 Register new user-defined specifier.
4267 TRIO_PUBLIC trio_pointer_t
4269 TRIO_ARGS2((callback
, name
),
4270 trio_callback_t callback
,
4271 TRIO_CONST
char *name
)
4273 trio_userdef_t
*def
;
4274 trio_userdef_t
*prev
= NULL
;
4276 if (callback
== NULL
)
4281 /* Handle built-in namespaces */
4284 if (trio_equal(name
, ":enter"))
4286 internalEnterCriticalRegion
= callback
;
4288 else if (trio_equal(name
, ":leave"))
4290 internalLeaveCriticalRegion
= callback
;
4295 /* Bail out if namespace is too long */
4296 if (trio_length(name
) >= MAX_USER_NAME
)
4299 /* Bail out if namespace already is registered */
4300 def
= TrioFindNamespace(name
, &prev
);
4305 def
= (trio_userdef_t
*)TRIO_MALLOC(sizeof(trio_userdef_t
));
4308 if (internalEnterCriticalRegion
)
4309 (void)internalEnterCriticalRegion(NULL
);
4313 /* Link into internal list */
4315 internalUserDef
= def
;
4320 def
->callback
= callback
;
4321 def
->name
= (name
== NULL
)
4323 : trio_duplicate(name
);
4326 if (internalLeaveCriticalRegion
)
4327 (void)internalLeaveCriticalRegion(NULL
);
4329 return (trio_pointer_t
)def
;
4333 Unregister an existing user-defined specifier.
4339 TRIO_ARGS1((handle
),
4340 trio_pointer_t handle
)
4342 trio_userdef_t
*self
= (trio_userdef_t
*)handle
;
4343 trio_userdef_t
*def
;
4344 trio_userdef_t
*prev
= NULL
;
4346 assert(VALID(self
));
4350 def
= TrioFindNamespace(self
->name
, &prev
);
4353 if (internalEnterCriticalRegion
)
4354 (void)internalEnterCriticalRegion(NULL
);
4357 internalUserDef
= NULL
;
4359 prev
->next
= def
->next
;
4361 if (internalLeaveCriticalRegion
)
4362 (void)internalLeaveCriticalRegion(NULL
);
4364 trio_destroy(self
->name
);
4369 /*************************************************************************
4370 * trio_get_format [public]
4377 #if defined(FORMAT_USER_DEFINED)
4378 assert(((trio_reference_t
*)ref
)->parameter
->type
== FORMAT_USER_DEFINED
);
4381 return (((trio_reference_t
*)ref
)->parameter
->user_data
);
4384 /*************************************************************************
4385 * trio_get_argument [public]
4392 #if defined(FORMAT_USER_DEFINED)
4393 assert(((trio_reference_t
*)ref
)->parameter
->type
== FORMAT_USER_DEFINED
);
4396 return ((trio_reference_t
*)ref
)->parameter
->data
.pointer
;
4399 /*************************************************************************
4400 * trio_get_width / trio_set_width [public]
4407 return ((trio_reference_t
*)ref
)->parameter
->width
;
4412 TRIO_ARGS2((ref
, width
),
4416 ((trio_reference_t
*)ref
)->parameter
->width
= width
;
4419 /*************************************************************************
4420 * trio_get_precision / trio_set_precision [public]
4427 return (((trio_reference_t
*)ref
)->parameter
->precision
);
4432 TRIO_ARGS2((ref
, precision
),
4436 ((trio_reference_t
*)ref
)->parameter
->precision
= precision
;
4439 /*************************************************************************
4440 * trio_get_base / trio_set_base [public]
4447 return (((trio_reference_t
*)ref
)->parameter
->base
);
4452 TRIO_ARGS2((ref
, base
),
4456 ((trio_reference_t
*)ref
)->parameter
->base
= base
;
4459 /*************************************************************************
4460 * trio_get_long / trio_set_long [public]
4467 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LONG
)
4474 TRIO_ARGS2((ref
, is_long
),
4479 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LONG
;
4481 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LONG
;
4484 /*************************************************************************
4485 * trio_get_longlong / trio_set_longlong [public]
4492 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_QUAD
)
4499 TRIO_ARGS2((ref
, is_longlong
),
4504 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_QUAD
;
4506 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_QUAD
;
4509 /*************************************************************************
4510 * trio_get_longdouble / trio_set_longdouble [public]
4517 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LONGDOUBLE
)
4524 TRIO_ARGS2((ref
, is_longdouble
),
4529 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LONGDOUBLE
;
4531 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LONGDOUBLE
;
4534 /*************************************************************************
4535 * trio_get_short / trio_set_short [public]
4542 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHORT
)
4549 TRIO_ARGS2((ref
, is_short
),
4554 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHORT
;
4556 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHORT
;
4559 /*************************************************************************
4560 * trio_get_shortshort / trio_set_shortshort [public]
4567 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHORTSHORT
)
4574 TRIO_ARGS2((ref
, is_shortshort
),
4579 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHORTSHORT
;
4581 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHORTSHORT
;
4584 /*************************************************************************
4585 * trio_get_alternative / trio_set_alternative [public]
4588 trio_get_alternative
4592 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_ALTERNATIVE
)
4598 trio_set_alternative
4599 TRIO_ARGS2((ref
, is_alternative
),
4604 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_ALTERNATIVE
;
4606 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_ALTERNATIVE
;
4609 /*************************************************************************
4610 * trio_get_alignment / trio_set_alignment [public]
4617 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_LEFTADJUST
)
4624 TRIO_ARGS2((ref
, is_leftaligned
),
4629 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_LEFTADJUST
;
4631 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_LEFTADJUST
;
4634 /*************************************************************************
4635 * trio_get_spacing /trio_set_spacing [public]
4642 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SPACE
)
4649 TRIO_ARGS2((ref
, is_space
),
4654 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SPACE
;
4656 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SPACE
;
4659 /*************************************************************************
4660 * trio_get_sign / trio_set_sign [public]
4667 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SHOWSIGN
)
4674 TRIO_ARGS2((ref
, is_sign
),
4679 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SHOWSIGN
;
4681 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SHOWSIGN
;
4684 /*************************************************************************
4685 * trio_get_padding / trio_set_padding [public]
4692 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_NILPADDING
)
4699 TRIO_ARGS2((ref
, is_padding
),
4704 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_NILPADDING
;
4706 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_NILPADDING
;
4709 /*************************************************************************
4710 * trio_get_quote / trio_set_quote [public]
4717 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_QUOTE
)
4724 TRIO_ARGS2((ref
, is_quote
),
4729 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_QUOTE
;
4731 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_QUOTE
;
4734 /*************************************************************************
4735 * trio_get_upper / trio_set_upper [public]
4742 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_UPPER
)
4749 TRIO_ARGS2((ref
, is_upper
),
4754 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_UPPER
;
4756 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_UPPER
;
4759 /*************************************************************************
4760 * trio_get_largest / trio_set_largest [public]
4768 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_INTMAX_T
)
4775 TRIO_ARGS2((ref
, is_largest
),
4780 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_INTMAX_T
;
4782 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_INTMAX_T
;
4786 /*************************************************************************
4787 * trio_get_ptrdiff / trio_set_ptrdiff [public]
4794 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_PTRDIFF_T
)
4801 TRIO_ARGS2((ref
, is_ptrdiff
),
4806 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_PTRDIFF_T
;
4808 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_PTRDIFF_T
;
4811 /*************************************************************************
4812 * trio_get_size / trio_set_size [public]
4820 return (((trio_reference_t
*)ref
)->parameter
->flags
& FLAGS_SIZE_T
)
4827 TRIO_ARGS2((ref
, is_size
),
4832 ((trio_reference_t
*)ref
)->parameter
->flags
|= FLAGS_SIZE_T
;
4834 ((trio_reference_t
*)ref
)->parameter
->flags
&= ~FLAGS_SIZE_T
;
4838 /*************************************************************************
4839 * trio_print_int [public]
4843 TRIO_ARGS2((ref
, number
),
4847 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4849 TrioWriteNumber(self
->data
,
4850 (trio_uintmax_t
)number
,
4851 self
->parameter
->flags
,
4852 self
->parameter
->width
,
4853 self
->parameter
->precision
,
4854 self
->parameter
->base
);
4857 /*************************************************************************
4858 * trio_print_uint [public]
4862 TRIO_ARGS2((ref
, number
),
4864 unsigned int number
)
4866 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4868 TrioWriteNumber(self
->data
,
4869 (trio_uintmax_t
)number
,
4870 self
->parameter
->flags
| FLAGS_UNSIGNED
,
4871 self
->parameter
->width
,
4872 self
->parameter
->precision
,
4873 self
->parameter
->base
);
4876 /*************************************************************************
4877 * trio_print_double [public]
4881 TRIO_ARGS2((ref
, number
),
4885 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4887 TrioWriteDouble(self
->data
,
4889 self
->parameter
->flags
,
4890 self
->parameter
->width
,
4891 self
->parameter
->precision
,
4892 self
->parameter
->base
);
4895 /*************************************************************************
4896 * trio_print_string [public]
4900 TRIO_ARGS2((ref
, string
),
4904 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4906 TrioWriteString(self
->data
,
4908 self
->parameter
->flags
,
4909 self
->parameter
->width
,
4910 self
->parameter
->precision
);
4913 /*************************************************************************
4914 * trio_print_ref [public]
4918 TRIO_VARGS3((ref
, format
, va_alist
),
4920 TRIO_CONST
char *format
,
4926 assert(VALID(format
));
4928 TRIO_VA_START(arglist
, format
);
4929 status
= TrioFormatRef((trio_reference_t
*)ref
, format
, &arglist
, NULL
);
4930 TRIO_VA_END(arglist
);
4934 /*************************************************************************
4935 * trio_vprint_ref [public]
4939 TRIO_ARGS3((ref
, format
, arglist
),
4941 TRIO_CONST
char *format
,
4944 assert(VALID(format
));
4946 return TrioFormatRef((trio_reference_t
*)ref
, format
, &arglist
, NULL
);
4949 /*************************************************************************
4950 * trio_printv_ref [public]
4954 TRIO_ARGS3((ref
, format
, argarray
),
4956 TRIO_CONST
char *format
,
4957 trio_pointer_t
*argarray
)
4959 assert(VALID(format
));
4961 return TrioFormatRef((trio_reference_t
*)ref
, format
, NULL
, argarray
);
4964 #endif /* TRIO_EXTENSION */
4966 /*************************************************************************
4967 * trio_print_pointer [public]
4971 TRIO_ARGS2((ref
, pointer
),
4973 trio_pointer_t pointer
)
4975 trio_reference_t
*self
= (trio_reference_t
*)ref
;
4977 trio_uintmax_t number
;
4979 if (NULL
== pointer
)
4981 TRIO_CONST
char *string
= internalNullString
;
4983 self
->data
->OutStream(self
->data
, *string
++);
4988 * The subtraction of the null pointer is a workaround
4989 * to avoid a compiler warning. The performance overhead
4990 * is negligible (and likely to be removed by an
4991 * optimizing compiler). The (char *) casting is done
4992 * to please ANSI C++.
4994 number
= (trio_uintmax_t
)((char *)pointer
- (char *)0);
4995 /* Shrink to size of pointer */
4996 number
&= (trio_uintmax_t
)-1;
4997 flags
= self
->parameter
->flags
;
4998 flags
|= (FLAGS_UNSIGNED
| FLAGS_ALTERNATIVE
|
5000 TrioWriteNumber(self
->data
,
5009 /** @} End of UserDefined documentation module */
5011 /*************************************************************************
5015 ************************************************************************/
5017 /*************************************************************************
5018 * trio_locale_set_decimal_point
5020 * Decimal point can only be one character. The input argument is a
5021 * string to enable multibyte characters. At most MB_LEN_MAX characters
5025 trio_locale_set_decimal_point
5026 TRIO_ARGS1((decimalPoint
),
5029 #if defined(USE_LOCALE)
5030 if (NULL
== internalLocaleValues
)
5035 internalDecimalPointLength
= trio_length(decimalPoint
);
5036 if (internalDecimalPointLength
== 1)
5038 internalDecimalPoint
= *decimalPoint
;
5042 internalDecimalPoint
= NIL
;
5043 trio_copy_max(internalDecimalPointString
,
5044 sizeof(internalDecimalPointString
),
5049 /*************************************************************************
5050 * trio_locale_set_thousand_separator
5052 * See trio_locale_set_decimal_point
5055 trio_locale_set_thousand_separator
5056 TRIO_ARGS1((thousandSeparator
),
5057 char *thousandSeparator
)
5059 #if defined(USE_LOCALE)
5060 if (NULL
== internalLocaleValues
)
5065 trio_copy_max(internalThousandSeparator
,
5066 sizeof(internalThousandSeparator
),
5068 internalThousandSeparatorLength
= trio_length(internalThousandSeparator
);
5071 /*************************************************************************
5072 * trio_locale_set_grouping
5074 * Array of bytes. Reversed order.
5076 * CHAR_MAX : No further grouping
5077 * 0 : Repeat last group for the remaining digits (not necessary
5078 * as C strings are zero-terminated)
5079 * n : Set current group to n
5081 * Same order as the grouping attribute in LC_NUMERIC.
5084 trio_locale_set_grouping
5085 TRIO_ARGS1((grouping
),
5088 #if defined(USE_LOCALE)
5089 if (NULL
== internalLocaleValues
)
5094 trio_copy_max(internalGrouping
,
5095 sizeof(internalGrouping
),
5100 /*************************************************************************
5104 ************************************************************************/
5106 /*************************************************************************
5107 * TrioSkipWhitespaces
5119 self
->InStream(self
, &ch
);
5124 /*************************************************************************
5129 TrioGetCollation(TRIO_NOARGS
)
5137 /* This is computationally expensive */
5140 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5144 for (j
= 0; j
< MAX_CHARACTER_CLASS
; j
++)
5146 second
[0] = (char)j
;
5147 if (trio_equal_locale(first
, second
))
5148 internalCollationArray
[i
][k
++] = (char)j
;
5150 internalCollationArray
[i
][k
] = NIL
;
5155 /*************************************************************************
5156 * TrioGetCharacterClass
5162 TrioGetCharacterClass
5163 TRIO_ARGS4((format
, indexPointer
, flagsPointer
, characterclass
),
5164 TRIO_CONST
char *format
,
5166 trio_flags_t
*flagsPointer
,
5167 int *characterclass
)
5169 int index
= *indexPointer
;
5175 *flagsPointer
&= ~FLAGS_EXCLUDE
;
5177 if (format
[index
] == QUALIFIER_CIRCUMFLEX
)
5179 *flagsPointer
|= FLAGS_EXCLUDE
;
5183 * If the ungroup character is at the beginning of the scanlist,
5184 * it will be part of the class, and a second ungroup character
5185 * must follow to end the group.
5187 if (format
[index
] == SPECIFIER_UNGROUP
)
5189 characterclass
[(int)SPECIFIER_UNGROUP
]++;
5193 * Minus is used to specify ranges. To include minus in the class,
5194 * it must be at the beginning of the list
5196 if (format
[index
] == QUALIFIER_MINUS
)
5198 characterclass
[(int)QUALIFIER_MINUS
]++;
5201 /* Collect characters */
5202 for (ch
= format
[index
];
5203 (ch
!= SPECIFIER_UNGROUP
) && (ch
!= NIL
);
5204 ch
= format
[++index
])
5208 case QUALIFIER_MINUS
: /* Scanlist ranges */
5211 * Both C99 and UNIX98 describes ranges as implementation-
5214 * We support the following behaviour (although this may
5215 * change as we become wiser)
5216 * - only increasing ranges, ie. [a-b] but not [b-a]
5217 * - transitive ranges, ie. [a-b-c] == [a-c]
5218 * - trailing minus, ie. [a-] is interpreted as an 'a'
5220 * - duplicates (although we can easily convert these
5223 range_begin
= format
[index
- 1];
5224 range_end
= format
[++index
];
5225 if (range_end
== SPECIFIER_UNGROUP
)
5227 /* Trailing minus is included */
5228 characterclass
[(int)ch
]++;
5232 if (range_end
== NIL
)
5233 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
5234 if (range_begin
> range_end
)
5235 return TRIO_ERROR_RETURN(TRIO_ERANGE
, index
);
5237 for (i
= (int)range_begin
; i
<= (int)range_end
; i
++)
5238 characterclass
[i
]++;
5245 case SPECIFIER_GROUP
:
5247 switch (format
[index
+ 1])
5249 case QUALIFIER_DOT
: /* Collating symbol */
5251 * FIXME: This will be easier to implement when multibyte
5252 * characters have been implemented. Until now, we ignore
5255 for (i
= index
+ 2; ; i
++)
5257 if (format
[i
] == NIL
)
5258 /* Error in syntax */
5260 else if (format
[i
] == QUALIFIER_DOT
)
5263 if (format
[++i
] != SPECIFIER_UNGROUP
)
5269 case QUALIFIER_EQUAL
: /* Equivalence class expressions */
5274 if (internalCollationUnconverted
)
5276 /* Lazy evaluation of collation array */
5278 internalCollationUnconverted
= FALSE
;
5280 for (i
= index
+ 2; ; i
++)
5282 if (format
[i
] == NIL
)
5283 /* Error in syntax */
5285 else if (format
[i
] == QUALIFIER_EQUAL
)
5289 /* Mark any equivalent character */
5290 k
= (unsigned int)format
[i
];
5291 for (j
= 0; internalCollationArray
[k
][j
] != NIL
; j
++)
5292 characterclass
[(int)internalCollationArray
[k
][j
]]++;
5295 if (format
[++i
] != SPECIFIER_UNGROUP
)
5302 case QUALIFIER_COLON
: /* Character class expressions */
5304 if (trio_equal_max(CLASS_ALNUM
, sizeof(CLASS_ALNUM
) - 1,
5307 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5309 characterclass
[i
]++;
5310 index
+= sizeof(CLASS_ALNUM
) - 1;
5312 else if (trio_equal_max(CLASS_ALPHA
, sizeof(CLASS_ALPHA
) - 1,
5315 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5317 characterclass
[i
]++;
5318 index
+= sizeof(CLASS_ALPHA
) - 1;
5320 else if (trio_equal_max(CLASS_CNTRL
, sizeof(CLASS_CNTRL
) - 1,
5323 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5325 characterclass
[i
]++;
5326 index
+= sizeof(CLASS_CNTRL
) - 1;
5328 else if (trio_equal_max(CLASS_DIGIT
, sizeof(CLASS_DIGIT
) - 1,
5331 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5333 characterclass
[i
]++;
5334 index
+= sizeof(CLASS_DIGIT
) - 1;
5336 else if (trio_equal_max(CLASS_GRAPH
, sizeof(CLASS_GRAPH
) - 1,
5339 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5341 characterclass
[i
]++;
5342 index
+= sizeof(CLASS_GRAPH
) - 1;
5344 else if (trio_equal_max(CLASS_LOWER
, sizeof(CLASS_LOWER
) - 1,
5347 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5349 characterclass
[i
]++;
5350 index
+= sizeof(CLASS_LOWER
) - 1;
5352 else if (trio_equal_max(CLASS_PRINT
, sizeof(CLASS_PRINT
) - 1,
5355 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5357 characterclass
[i
]++;
5358 index
+= sizeof(CLASS_PRINT
) - 1;
5360 else if (trio_equal_max(CLASS_PUNCT
, sizeof(CLASS_PUNCT
) - 1,
5363 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5365 characterclass
[i
]++;
5366 index
+= sizeof(CLASS_PUNCT
) - 1;
5368 else if (trio_equal_max(CLASS_SPACE
, sizeof(CLASS_SPACE
) - 1,
5371 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5373 characterclass
[i
]++;
5374 index
+= sizeof(CLASS_SPACE
) - 1;
5376 else if (trio_equal_max(CLASS_UPPER
, sizeof(CLASS_UPPER
) - 1,
5379 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5381 characterclass
[i
]++;
5382 index
+= sizeof(CLASS_UPPER
) - 1;
5384 else if (trio_equal_max(CLASS_XDIGIT
, sizeof(CLASS_XDIGIT
) - 1,
5387 for (i
= 0; i
< MAX_CHARACTER_CLASS
; i
++)
5389 characterclass
[i
]++;
5390 index
+= sizeof(CLASS_XDIGIT
) - 1;
5394 characterclass
[(int)ch
]++;
5399 characterclass
[(int)ch
]++;
5404 #endif /* TRIO_EXTENSION */
5407 characterclass
[(int)ch
]++;
5414 /*************************************************************************
5417 * We implement our own number conversion in preference of strtol and
5418 * strtoul, because we must handle 'long long' and thousand separators.
5420 TRIO_PRIVATE BOOLEAN_T
5422 TRIO_ARGS5((self
, target
, flags
, width
, base
),
5424 trio_uintmax_t
*target
,
5429 trio_uintmax_t number
= 0;
5432 BOOLEAN_T isNegative
= FALSE
;
5433 BOOLEAN_T gotNumber
= FALSE
;
5436 assert(VALID(self
));
5437 assert(VALID(self
->InStream
));
5438 assert((base
>= MIN_BASE
&& base
<= MAX_BASE
) || (base
== NO_BASE
));
5440 if (internalDigitsUnconverted
)
5442 /* Lazy evaluation of digits array */
5443 memset(internalDigitArray
, -1, sizeof(internalDigitArray
));
5444 for (j
= 0; j
< (int)sizeof(internalDigitsLower
) - 1; j
++)
5446 internalDigitArray
[(int)internalDigitsLower
[j
]] = j
;
5447 internalDigitArray
[(int)internalDigitsUpper
[j
]] = j
;
5449 internalDigitsUnconverted
= FALSE
;
5452 TrioSkipWhitespaces(self
);
5454 if (!(flags
& FLAGS_UNSIGNED
))
5457 if (self
->current
== '+')
5459 self
->InStream(self
, NULL
);
5461 else if (self
->current
== '-')
5463 self
->InStream(self
, NULL
);
5468 count
= self
->processed
;
5470 if (flags
& FLAGS_ALTERNATIVE
)
5478 if (self
->current
== '0')
5480 self
->InStream(self
, NULL
);
5483 if ((base
== BASE_HEX
) &&
5484 (trio_to_upper(self
->current
) == 'X'))
5486 self
->InStream(self
, NULL
);
5488 else if ((base
== BASE_BINARY
) &&
5489 (trio_to_upper(self
->current
) == 'B'))
5491 self
->InStream(self
, NULL
);
5503 while (((width
== NO_WIDTH
) || (self
->processed
- count
< width
)) &&
5504 (! ((self
->current
== EOF
) || isspace(self
->current
))))
5506 if (isascii(self
->current
))
5508 digit
= internalDigitArray
[self
->current
];
5509 /* Abort if digit is not allowed in the specified base */
5510 if ((digit
== -1) || (digit
>= base
))
5513 else if (flags
& FLAGS_QUOTE
)
5515 /* Compare with thousands separator */
5516 for (j
= 0; internalThousandSeparator
[j
] && self
->current
; j
++)
5518 if (internalThousandSeparator
[j
] != self
->current
)
5521 self
->InStream(self
, NULL
);
5523 if (internalThousandSeparator
[j
])
5524 break; /* Mismatch */
5526 continue; /* Match */
5533 gotNumber
= TRUE
; /* we need at least one digit */
5535 self
->InStream(self
, NULL
);
5538 /* Was anything read at all? */
5543 *target
= (isNegative
) ? -((trio_intmax_t
)number
) : number
;
5547 /*************************************************************************
5552 TRIO_ARGS4((self
, target
, flags
, width
),
5560 trio_uintmax_t number
;
5562 assert(VALID(self
));
5563 assert(VALID(self
->InStream
));
5566 (self
->current
!= EOF
) && (i
< width
);
5569 ch
= (char)self
->current
;
5570 self
->InStream(self
, NULL
);
5571 if ((flags
& FLAGS_ALTERNATIVE
) && (ch
== CHAR_BACKSLASH
))
5573 switch (self
->current
)
5575 case '\\': ch
= '\\'; break;
5576 case 'a': ch
= '\007'; break;
5577 case 'b': ch
= '\b'; break;
5578 case 'f': ch
= '\f'; break;
5579 case 'n': ch
= '\n'; break;
5580 case 'r': ch
= '\r'; break;
5581 case 't': ch
= '\t'; break;
5582 case 'v': ch
= '\v'; break;
5584 if (isdigit(self
->current
))
5586 /* Read octal number */
5587 if (!TrioReadNumber(self
, &number
, 0, 3, BASE_OCTAL
))
5591 else if (trio_to_upper(self
->current
) == 'X')
5593 /* Read hexadecimal number */
5594 self
->InStream(self
, NULL
);
5595 if (!TrioReadNumber(self
, &number
, 0, 2, BASE_HEX
))
5601 ch
= (char)self
->current
;
5613 /*************************************************************************
5616 TRIO_PRIVATE BOOLEAN_T
5618 TRIO_ARGS4((self
, target
, flags
, width
),
5626 assert(VALID(self
));
5627 assert(VALID(self
->InStream
));
5629 TrioSkipWhitespaces(self
);
5632 * Continue until end of string is reached, a whitespace is encountered,
5633 * or width is exceeded
5636 ((width
== NO_WIDTH
) || (i
< width
)) &&
5637 (! ((self
->current
== EOF
) || isspace(self
->current
)));
5640 if (TrioReadChar(self
, (target
? &target
[i
] : 0), flags
, 1) == 0)
5648 /*************************************************************************
5654 TRIO_ARGS4((self
, target
, flags
, width
),
5656 trio_wchar_t
*target
,
5665 char buffer
[MB_LEN_MAX
+ 1];
5667 assert(VALID(self
));
5668 assert(VALID(self
->InStream
));
5671 (self
->current
!= EOF
) && (i
< width
);
5674 if (isascii(self
->current
))
5676 if (TrioReadChar(self
, buffer
, flags
, 1) == 0)
5683 * Collect a multibyte character, by enlarging buffer until
5684 * it contains a fully legal multibyte character, or the
5690 buffer
[j
++] = (char)self
->current
;
5692 self
->InStream(self
, NULL
);
5694 while ((j
< (int)sizeof(buffer
)) && (mblen(buffer
, (size_t)j
) != j
));
5698 size
= mbtowc(&wch
, buffer
, sizeof(buffer
));
5703 self
->InStream(self
, NULL
);
5707 #endif /* TRIO_WIDECHAR */
5709 /*************************************************************************
5710 * TrioReadWideString
5713 TRIO_PRIVATE BOOLEAN_T
5715 TRIO_ARGS4((self
, target
, flags
, width
),
5717 trio_wchar_t
*target
,
5724 assert(VALID(self
));
5725 assert(VALID(self
->InStream
));
5727 TrioSkipWhitespaces(self
);
5729 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5730 (void)mblen(NULL
, 0);
5734 * Continue until end of string is reached, a whitespace is encountered,
5735 * or width is exceeded
5738 ((width
== NO_WIDTH
) || (i
< width
)) &&
5739 (! ((self
->current
== EOF
) || isspace(self
->current
)));
5742 size
= TrioReadWideChar(self
, &target
[i
], flags
, 1);
5749 target
[i
] = WCONST('\0');
5752 #endif /* TRIO_WIDECHAR */
5754 /*************************************************************************
5757 * FIXME: characterclass does not work with multibyte characters
5759 TRIO_PRIVATE BOOLEAN_T
5761 TRIO_ARGS5((self
, target
, characterclass
, flags
, width
),
5764 int *characterclass
,
5771 assert(VALID(self
));
5772 assert(VALID(self
->InStream
));
5776 ((width
== NO_WIDTH
) || (i
< width
)) &&
5778 (((flags
& FLAGS_EXCLUDE
) != 0) ^ (characterclass
[ch
] == 0))));
5782 target
[i
] = (char)ch
;
5783 self
->InStream(self
, &ch
);
5791 /*************************************************************************
5798 TRIO_PRIVATE BOOLEAN_T
5800 TRIO_ARGS4((self
, target
, flags
, width
),
5802 trio_pointer_t target
,
5807 char doubleString
[512];
5811 BOOLEAN_T isHex
= FALSE
;
5813 doubleString
[0] = 0;
5815 if ((width
== NO_WIDTH
) || (width
> (int)sizeof(doubleString
) - 1))
5816 width
= sizeof(doubleString
) - 1;
5818 TrioSkipWhitespaces(self
);
5821 * Read entire double number from stream. trio_to_double requires
5822 * a string as input, but InStream can be anything, so we have to
5823 * collect all characters.
5826 if ((ch
== '+') || (ch
== '-'))
5828 doubleString
[index
++] = (char)ch
;
5829 self
->InStream(self
, &ch
);
5845 while (isalpha(ch
) && (index
- start
< width
))
5847 doubleString
[index
++] = (char)ch
;
5848 self
->InStream(self
, &ch
);
5850 doubleString
[index
] = NIL
;
5852 /* Case insensitive string comparison */
5853 if (trio_equal(&doubleString
[start
], INFINITE_UPPER
) ||
5854 trio_equal(&doubleString
[start
], LONG_INFINITE_UPPER
))
5856 if (flags
& FLAGS_LONGDOUBLE
)
5858 if ((start
== 1) && (doubleString
[0] == '-'))
5860 *((trio_long_double_t
*)target
) = trio_ninf();
5864 *((trio_long_double_t
*)target
) = trio_pinf();
5869 if ((start
== 1) && (doubleString
[0] == '-'))
5871 *((double *)target
) = trio_ninf();
5875 *((double *)target
) = trio_pinf();
5880 if (trio_equal(doubleString
, NAN_UPPER
))
5882 /* NaN must not have a preceeding + nor - */
5883 if (flags
& FLAGS_LONGDOUBLE
)
5885 *((trio_long_double_t
*)target
) = trio_nan();
5889 *((double *)target
) = trio_nan();
5896 doubleString
[index
++] = (char)ch
;
5897 self
->InStream(self
, &ch
);
5898 if (trio_to_upper(ch
) == 'X')
5901 doubleString
[index
++] = (char)ch
;
5902 self
->InStream(self
, &ch
);
5910 while ((ch
!= EOF
) && (index
- start
< width
))
5913 if (isHex
? isxdigit(ch
) : isdigit(ch
))
5915 doubleString
[index
++] = (char)ch
;
5916 self
->InStream(self
, &ch
);
5918 else if (flags
& FLAGS_QUOTE
)
5920 /* Compare with thousands separator */
5921 for (j
= 0; internalThousandSeparator
[j
] && self
->current
; j
++)
5923 if (internalThousandSeparator
[j
] != self
->current
)
5926 self
->InStream(self
, &ch
);
5928 if (internalThousandSeparator
[j
])
5929 break; /* Mismatch */
5931 continue; /* Match */
5939 doubleString
[index
++] = (char)ch
;
5940 self
->InStream(self
, &ch
);
5941 while ((isHex
? isxdigit(ch
) : isdigit(ch
)) &&
5942 (index
- start
< width
))
5944 doubleString
[index
++] = (char)ch
;
5945 self
->InStream(self
, &ch
);
5947 if (isHex
? (trio_to_upper(ch
) == 'P') : (trio_to_upper(ch
) == 'E'))
5950 doubleString
[index
++] = (char)ch
;
5951 self
->InStream(self
, &ch
);
5952 if ((ch
== '+') || (ch
== '-'))
5954 doubleString
[index
++] = (char)ch
;
5955 self
->InStream(self
, &ch
);
5957 while (isdigit(ch
) && (index
- start
< width
))
5959 doubleString
[index
++] = (char)ch
;
5960 self
->InStream(self
, &ch
);
5965 if ((index
== start
) || (*doubleString
== NIL
))
5968 doubleString
[index
] = 0;
5970 if (flags
& FLAGS_LONGDOUBLE
)
5972 *((trio_long_double_t
*)target
) = trio_to_long_double(doubleString
, NULL
);
5976 *((double *)target
) = trio_to_double(doubleString
, NULL
);
5981 /*************************************************************************
5984 TRIO_PRIVATE BOOLEAN_T
5986 TRIO_ARGS3((self
, target
, flags
),
5988 trio_pointer_t
*target
,
5991 trio_uintmax_t number
;
5992 char buffer
[sizeof(internalNullString
)];
5994 flags
|= (FLAGS_UNSIGNED
| FLAGS_ALTERNATIVE
| FLAGS_NILPADDING
);
5996 if (TrioReadNumber(self
,
6003 * The strange assignment of number is a workaround for a compiler
6007 *target
= (char *)0 + number
;
6010 else if (TrioReadString(self
,
6011 (flags
& FLAGS_IGNORE
)
6015 sizeof(internalNullString
) - 1))
6017 if (trio_equal_case(buffer
, internalNullString
))
6027 /*************************************************************************
6032 TRIO_ARGS3((data
, format
, parameters
),
6034 TRIO_CONST
char *format
,
6035 trio_parameter_t
*parameters
)
6037 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6043 int index
; /* Index of format string */
6044 int i
; /* Index of current parameter */
6048 trio_pointer_t pointer
;
6053 data
->InStream(data
, &ch
);
6055 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6056 (void)mblen(NULL
, 0);
6059 while (format
[index
])
6061 #if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6062 if (! isascii(format
[index
]))
6064 charlen
= mblen(&format
[index
], MB_LEN_MAX
);
6067 /* Compare multibyte characters in format string */
6068 for (cnt
= 0; cnt
< charlen
- 1; cnt
++)
6070 if (ch
!= format
[index
+ cnt
])
6072 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
6074 data
->InStream(data
, &ch
);
6076 continue; /* while characters left in formatting string */
6079 #endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
6081 if ((EOF
== ch
) && (parameters
[i
].type
!= FORMAT_COUNT
))
6083 return (assignment
> 0) ? assignment
: EOF
;
6086 if (CHAR_IDENTIFIER
== format
[index
])
6088 if (CHAR_IDENTIFIER
== format
[index
+ 1])
6090 /* Two % in format matches one % in input stream */
6091 if (CHAR_IDENTIFIER
== ch
)
6093 data
->InStream(data
, &ch
);
6095 continue; /* while format chars left */
6098 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
6101 /* Skip the parameter entries */
6102 while (parameters
[i
].type
== FORMAT_PARAMETER
)
6105 flags
= parameters
[i
].flags
;
6107 width
= parameters
[i
].width
;
6108 if (flags
& FLAGS_WIDTH_PARAMETER
)
6110 /* Get width from parameter list */
6111 width
= (int)parameters
[width
].data
.number
.as_signed
;
6114 base
= parameters
[i
].base
;
6115 if (flags
& FLAGS_BASE_PARAMETER
)
6117 /* Get base from parameter list */
6118 base
= (int)parameters
[base
].data
.number
.as_signed
;
6121 switch (parameters
[i
].type
)
6125 trio_uintmax_t number
;
6128 base
= BASE_DECIMAL
;
6130 if (!TrioReadNumber(data
,
6137 if (!(flags
& FLAGS_IGNORE
))
6141 pointer
= parameters
[i
].data
.pointer
;
6142 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6143 if (flags
& FLAGS_SIZE_T
)
6144 *(size_t *)pointer
= (size_t)number
;
6147 #if defined(QUALIFIER_PTRDIFF_T)
6148 if (flags
& FLAGS_PTRDIFF_T
)
6149 *(ptrdiff_t *)pointer
= (ptrdiff_t)number
;
6152 #if defined(QUALIFIER_INTMAX_T)
6153 if (flags
& FLAGS_INTMAX_T
)
6154 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)number
;
6157 if (flags
& FLAGS_QUAD
)
6158 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)number
;
6159 else if (flags
& FLAGS_LONG
)
6160 *(long int *)pointer
= (long int)number
;
6161 else if (flags
& FLAGS_SHORT
)
6162 *(short int *)pointer
= (short int)number
;
6164 *(int *)pointer
= (int)number
;
6167 break; /* FORMAT_INT */
6171 if (flags
& FLAGS_WIDECHAR
)
6173 if (!TrioReadWideString(data
,
6174 (flags
& FLAGS_IGNORE
)
6176 : parameters
[i
].data
.wstring
,
6184 if (!TrioReadString(data
,
6185 (flags
& FLAGS_IGNORE
)
6187 : parameters
[i
].data
.string
,
6192 if (!(flags
& FLAGS_IGNORE
))
6194 break; /* FORMAT_STRING */
6198 trio_pointer_t pointer
;
6200 if (flags
& FLAGS_IGNORE
)
6206 pointer
= (flags
& FLAGS_LONGDOUBLE
)
6207 ? (trio_pointer_t
)parameters
[i
].data
.longdoublePointer
6208 : (trio_pointer_t
)parameters
[i
].data
.doublePointer
;
6210 if (!TrioReadDouble(data
, pointer
, flags
, width
))
6214 if (!(flags
& FLAGS_IGNORE
))
6218 break; /* FORMAT_DOUBLE */
6222 int characterclass
[MAX_CHARACTER_CLASS
+ 1];
6225 /* Skip over modifiers */
6226 while (format
[index
] != SPECIFIER_GROUP
)
6230 /* Skip over group specifier */
6233 memset(characterclass
, 0, sizeof(characterclass
));
6234 rc
= TrioGetCharacterClass(format
,
6241 if (!TrioReadGroup(data
,
6242 (flags
& FLAGS_IGNORE
)
6244 : parameters
[i
].data
.string
,
6247 parameters
[i
].width
))
6249 if (!(flags
& FLAGS_IGNORE
))
6252 break; /* FORMAT_GROUP */
6255 pointer
= parameters
[i
].data
.pointer
;
6256 if (NULL
!= pointer
)
6258 int count
= data
->committed
;
6260 count
--; /* a character is read, but is not consumed yet */
6261 #if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6262 if (flags
& FLAGS_SIZE_T
)
6263 *(size_t *)pointer
= (size_t)count
;
6266 #if defined(QUALIFIER_PTRDIFF_T)
6267 if (flags
& FLAGS_PTRDIFF_T
)
6268 *(ptrdiff_t *)pointer
= (ptrdiff_t)count
;
6271 #if defined(QUALIFIER_INTMAX_T)
6272 if (flags
& FLAGS_INTMAX_T
)
6273 *(trio_intmax_t
*)pointer
= (trio_intmax_t
)count
;
6276 if (flags
& FLAGS_QUAD
)
6278 *(trio_ulonglong_t
*)pointer
= (trio_ulonglong_t
)count
;
6280 else if (flags
& FLAGS_LONG
)
6282 *(long int *)pointer
= (long int)count
;
6284 else if (flags
& FLAGS_SHORT
)
6286 *(short int *)pointer
= (short int)count
;
6290 *(int *)pointer
= (int)count
;
6293 break; /* FORMAT_COUNT */
6297 if (flags
& FLAGS_WIDECHAR
)
6299 if (TrioReadWideChar(data
,
6300 (flags
& FLAGS_IGNORE
)
6302 : parameters
[i
].data
.wstring
,
6304 (width
== NO_WIDTH
) ? 1 : width
) == 0)
6310 if (TrioReadChar(data
,
6311 (flags
& FLAGS_IGNORE
)
6313 : parameters
[i
].data
.string
,
6315 (width
== NO_WIDTH
) ? 1 : width
) == 0)
6318 if (!(flags
& FLAGS_IGNORE
))
6320 break; /* FORMAT_CHAR */
6322 case FORMAT_POINTER
:
6323 if (!TrioReadPointer(data
,
6324 (flags
& FLAGS_IGNORE
)
6326 : (trio_pointer_t
*)parameters
[i
].data
.pointer
,
6329 if (!(flags
& FLAGS_IGNORE
))
6331 break; /* FORMAT_POINTER */
6333 case FORMAT_PARAMETER
:
6334 break; /* FORMAT_PARAMETER */
6337 return TRIO_ERROR_RETURN(TRIO_EINVAL
, index
);
6340 index
= parameters
[i
].indexAfterSpecifier
;
6343 else /* Not an % identifier */
6345 if (isspace((int)format
[index
]))
6347 /* Whitespaces may match any amount of whitespaces */
6348 ch
= TrioSkipWhitespaces(data
);
6350 else if (ch
== format
[index
])
6352 data
->InStream(data
, &ch
);
6363 /*************************************************************************
6368 TRIO_ARGS6((source
, sourceSize
, InStream
, format
, arglist
, argarray
),
6369 trio_pointer_t source
,
6371 void (*InStream
) TRIO_PROTO((trio_class_t
*, int *)),
6372 TRIO_CONST
char *format
,
6374 trio_pointer_t
*argarray
)
6377 trio_parameter_t parameters
[MAX_PARAMETERS
];
6380 assert(VALID(InStream
));
6381 assert(VALID(format
));
6383 memset(&data
, 0, sizeof(data
));
6384 data
.InStream
= InStream
;
6385 data
.location
= (trio_pointer_t
)source
;
6386 data
.max
= sourceSize
;
6389 #if defined(USE_LOCALE)
6390 if (NULL
== internalLocaleValues
)
6396 status
= TrioParse(TYPE_SCAN
, format
, parameters
, arglist
, argarray
);
6400 status
= TrioScanProcess(&data
, format
, parameters
);
6401 if (data
.error
!= 0)
6403 status
= data
.error
;
6408 /*************************************************************************
6413 TRIO_ARGS2((self
, intPointer
),
6417 FILE *file
= (FILE *)self
->location
;
6419 assert(VALID(self
));
6420 assert(VALID(file
));
6422 self
->current
= fgetc(file
);
6423 if (self
->current
== EOF
)
6425 self
->error
= (ferror(file
))
6426 ? TRIO_ERROR_RETURN(TRIO_ERRNO
, 0)
6427 : TRIO_ERROR_RETURN(TRIO_EOF
, 0);
6435 if (VALID(intPointer
))
6437 *intPointer
= self
->current
;
6441 /*************************************************************************
6442 * TrioInStreamFileDescriptor
6445 TrioInStreamFileDescriptor
6446 TRIO_ARGS2((self
, intPointer
),
6450 int fd
= *((int *)self
->location
);
6452 unsigned char input
;
6454 assert(VALID(self
));
6456 size
= read(fd
, &input
, sizeof(char));
6459 self
->error
= TRIO_ERROR_RETURN(TRIO_ERRNO
, 0);
6460 self
->current
= EOF
;
6464 self
->current
= (size
== 0) ? EOF
: input
;
6466 if (self
->current
!= EOF
)
6472 if (VALID(intPointer
))
6474 *intPointer
= self
->current
;
6478 /*************************************************************************
6479 * TrioInStreamCustom
6483 TRIO_ARGS2((self
, intPointer
),
6487 trio_custom_t
*data
;
6489 assert(VALID(self
));
6490 assert(VALID(self
->location
));
6492 data
= (trio_custom_t
*)self
->location
;
6494 self
->current
= (data
->stream
.in
== NULL
)
6496 : (data
->stream
.in
)(data
->closure
);
6498 if (self
->current
== NIL
)
6500 self
->current
= EOF
;
6508 if (VALID(intPointer
))
6510 *intPointer
= self
->current
;
6514 /*************************************************************************
6515 * TrioInStreamString
6519 TRIO_ARGS2((self
, intPointer
),
6523 unsigned char **buffer
;
6525 assert(VALID(self
));
6526 assert(VALID(self
->location
));
6528 buffer
= (unsigned char **)self
->location
;
6529 self
->current
= (*buffer
)[0];
6530 if (self
->current
== NIL
)
6532 self
->current
= EOF
;
6541 if (VALID(intPointer
))
6543 *intPointer
= self
->current
;
6547 /*************************************************************************
6549 * Formatted scanning functions
6551 ************************************************************************/
6553 #if defined(TRIO_DOCUMENTATION)
6554 # include "doc/doc_scanf.h"
6556 /** @addtogroup Scanf
6560 /*************************************************************************
6565 Scan characters from standard input stream.
6567 @param format Formatting string.
6568 @param ... Arguments.
6569 @return Number of scanned characters.
6573 TRIO_VARGS2((format
, va_alist
),
6574 TRIO_CONST
char *format
,
6580 assert(VALID(format
));
6582 TRIO_VA_START(args
, format
);
6583 status
= TrioScan((trio_pointer_t
)stdin
, 0,
6585 format
, &args
, NULL
);
6592 TRIO_ARGS2((format
, args
),
6593 TRIO_CONST
char *format
,
6596 assert(VALID(format
));
6598 return TrioScan((trio_pointer_t
)stdin
, 0,
6600 format
, &args
, NULL
);
6605 TRIO_ARGS2((format
, args
),
6606 TRIO_CONST
char *format
,
6607 trio_pointer_t
*args
)
6609 assert(VALID(format
));
6611 return TrioScan((trio_pointer_t
)stdin
, 0,
6613 format
, NULL
, args
);
6616 /*************************************************************************
6621 TRIO_VARGS3((file
, format
, va_alist
),
6623 TRIO_CONST
char *format
,
6629 assert(VALID(file
));
6630 assert(VALID(format
));
6632 TRIO_VA_START(args
, format
);
6633 status
= TrioScan((trio_pointer_t
)file
, 0,
6635 format
, &args
, NULL
);
6642 TRIO_ARGS3((file
, format
, args
),
6644 TRIO_CONST
char *format
,
6647 assert(VALID(file
));
6648 assert(VALID(format
));
6650 return TrioScan((trio_pointer_t
)file
, 0,
6652 format
, &args
, NULL
);
6657 TRIO_ARGS3((file
, format
, args
),
6659 TRIO_CONST
char *format
,
6660 trio_pointer_t
*args
)
6662 assert(VALID(file
));
6663 assert(VALID(format
));
6665 return TrioScan((trio_pointer_t
)file
, 0,
6667 format
, NULL
, args
);
6670 /*************************************************************************
6675 TRIO_VARGS3((fd
, format
, va_alist
),
6677 TRIO_CONST
char *format
,
6683 assert(VALID(format
));
6685 TRIO_VA_START(args
, format
);
6686 status
= TrioScan((trio_pointer_t
)&fd
, 0,
6687 TrioInStreamFileDescriptor
,
6688 format
, &args
, NULL
);
6695 TRIO_ARGS3((fd
, format
, args
),
6697 TRIO_CONST
char *format
,
6700 assert(VALID(format
));
6702 return TrioScan((trio_pointer_t
)&fd
, 0,
6703 TrioInStreamFileDescriptor
,
6704 format
, &args
, NULL
);
6709 TRIO_ARGS3((fd
, format
, args
),
6711 TRIO_CONST
char *format
,
6712 trio_pointer_t
*args
)
6714 assert(VALID(format
));
6716 return TrioScan((trio_pointer_t
)&fd
, 0,
6717 TrioInStreamFileDescriptor
,
6718 format
, NULL
, args
);
6721 /*************************************************************************
6726 TRIO_VARGS4((stream
, closure
, format
, va_alist
),
6727 trio_instream_t stream
,
6728 trio_pointer_t closure
,
6729 TRIO_CONST
char *format
,
6736 assert(VALID(stream
));
6737 assert(VALID(format
));
6739 TRIO_VA_START(args
, format
);
6740 data
.stream
.in
= stream
;
6741 data
.closure
= closure
;
6742 status
= TrioScan(&data
, 0, TrioInStreamCustom
, format
, &args
, NULL
);
6749 TRIO_ARGS4((stream
, closure
, format
, args
),
6750 trio_instream_t stream
,
6751 trio_pointer_t closure
,
6752 TRIO_CONST
char *format
,
6757 assert(VALID(stream
));
6758 assert(VALID(format
));
6760 data
.stream
.in
= stream
;
6761 data
.closure
= closure
;
6762 return TrioScan(&data
, 0, TrioInStreamCustom
, format
, &args
, NULL
);
6767 TRIO_ARGS4((stream
, closure
, format
, args
),
6768 trio_instream_t stream
,
6769 trio_pointer_t closure
,
6770 TRIO_CONST
char *format
,
6771 trio_pointer_t
*args
)
6775 assert(VALID(stream
));
6776 assert(VALID(format
));
6778 data
.stream
.in
= stream
;
6779 data
.closure
= closure
;
6780 return TrioScan(&data
, 0, TrioInStreamCustom
, format
, NULL
, args
);
6783 /*************************************************************************
6788 TRIO_VARGS3((buffer
, format
, va_alist
),
6789 TRIO_CONST
char *buffer
,
6790 TRIO_CONST
char *format
,
6796 assert(VALID(buffer
));
6797 assert(VALID(format
));
6799 TRIO_VA_START(args
, format
);
6800 status
= TrioScan((trio_pointer_t
)&buffer
, 0,
6802 format
, &args
, NULL
);
6809 TRIO_ARGS3((buffer
, format
, args
),
6810 TRIO_CONST
char *buffer
,
6811 TRIO_CONST
char *format
,
6814 assert(VALID(buffer
));
6815 assert(VALID(format
));
6817 return TrioScan((trio_pointer_t
)&buffer
, 0,
6819 format
, &args
, NULL
);
6824 TRIO_ARGS3((buffer
, format
, args
),
6825 TRIO_CONST
char *buffer
,
6826 TRIO_CONST
char *format
,
6827 trio_pointer_t
*args
)
6829 assert(VALID(buffer
));
6830 assert(VALID(format
));
6832 return TrioScan((trio_pointer_t
)&buffer
, 0,
6834 format
, NULL
, args
);
6837 /** @} End of Scanf documentation module */
6839 /*************************************************************************
6842 TRIO_PUBLIC TRIO_CONST
char *
6844 TRIO_ARGS1((errorcode
),
6847 /* Textual versions of the error codes */
6848 switch (TRIO_ERROR_CODE(errorcode
))
6851 return "End of file";
6853 return "Invalid argument";
6855 return "Too many arguments";
6857 return "Double reference";
6859 return "Reference gap";
6861 return "Out of memory";
6863 return "Invalid range";
6865 return "Custom error";