51#if !defined(TRIO_MINIMAL)
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>
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)
108# define FALSE (1 == 0)
109# define TRUE (! FALSE)
114#define VALID(x) (NULL != (x))
121# define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
123# define TRIO_ERROR_RETURN(x,y) (-1)
126#ifndef VA_LIST_IS_ARRAY
127#define TRIO_VA_LIST_PTR va_list *
128#define TRIO_VA_LIST_ADDR(l) (&(l))
129#define TRIO_VA_LIST_DEREF(l) (*(l))
131#define TRIO_VA_LIST_PTR va_list
132#define TRIO_VA_LIST_ADDR(l) (l)
133#define TRIO_VA_LIST_DEREF(l) (l)
142#if defined(TRIO_PLATFORM_UNIX) || defined(TRIO_PLATFORM_OS400)
148#if defined(TRIO_PLATFORM_VMS)
151#if defined(TRIO_PLATFORM_WIN32)
152# if defined(_WIN32_WCE)
153# include <wincecompat.h>
162# if defined(TRIO_COMPILER_SUPPORTS_ISO94)
165typedef wchar_t trio_wchar_t;
166typedef wint_t trio_wint_t;
168typedef char trio_wchar_t;
169typedef int trio_wint_t;
170# define WCONST(x) L ## x
172# define iswalnum(x) isalnum(x)
173# define iswalpha(x) isalpha(x)
174# define iswblank(x) isblank(x)
175# define iswcntrl(x) iscntrl(x)
176# define iswdigit(x) isdigit(x)
177# define iswgraph(x) isgraph(x)
178# define iswlower(x) islower(x)
179# define iswprint(x) isprint(x)
180# define iswpunct(x) ispunct(x)
181# define iswspace(x) isspace(x)
182# define iswupper(x) isupper(x)
183# define iswxdigit(x) isxdigit(x)
194# if !defined(USE_LONGLONG)
195# if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
197# elif defined(TRIO_COMPILER_SUNPRO)
199# elif defined(_LONG_LONG) || defined(_LONGLONG)
206#if defined(USE_LONGLONG)
209#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
218#if defined(TRIO_COMPILER_SUPPORTS_C99) && !defined( __VMS )
226#elif defined(TRIO_COMPILER_SUPPORTS_UNIX98) || defined( __VMS )
227# include <inttypes.h>
238#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
248# if defined(TRIO_INT8_T)
253# if defined(TRIO_INT16_T)
258# if defined(TRIO_INT32_T)
263# if defined(TRIO_INT64_T)
270#if (!(defined(TRIO_COMPILER_SUPPORTS_C99) \
271 || defined(TRIO_COMPILER_SUPPORTS_UNIX01))) \
272 && !defined(_WIN32_WCE)
273# define floorl(x) floor((double)(x))
274# define fmodl(x,y) fmod((double)(x),(double)(y))
275# define powl(x,y) pow((double)(x),(double)(y))
278#define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
285# define DECIMAL_DIG DBL_DIG
290# define MAX_MANTISSA_DIGITS LDBL_DIG
291# define MAX_EXPONENT_DIGITS 4
292# define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
294# define MAX_MANTISSA_DIGITS DECIMAL_DIG
295# define MAX_EXPONENT_DIGITS 3
296# define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
299#if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
303# define LDBL_DIG DBL_DIG
304# define LDBL_MANT_DIG DBL_MANT_DIG
305# define LDBL_EPSILON DBL_EPSILON
309#define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
311#define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
314#define INFINITE_LOWER "inf"
315#define INFINITE_UPPER "INF"
316#define LONG_INFINITE_LOWER "infinite"
317#define LONG_INFINITE_UPPER "INFINITE"
318#define NAN_LOWER "nan"
319#define NAN_UPPER "NAN"
321#if !defined(HAVE_ISASCII) && !defined(isascii)
323# define isascii(x) ((unsigned int)(x) < 128)
406#define NO_GROUPING ((int)CHAR_MAX)
409#define FORMAT_UNKNOWN 0
411#define FORMAT_DOUBLE 2
413#define FORMAT_STRING 4
414#define FORMAT_POINTER 5
415#define FORMAT_COUNT 6
416#define FORMAT_PARAMETER 7
417#define FORMAT_GROUP 8
419# define FORMAT_ERRNO 9
422# define FORMAT_USER_DEFINED 10
426#define CHAR_IDENTIFIER '%'
427#define CHAR_BACKSLASH '\\'
428#define CHAR_QUOTE '\"'
429#define CHAR_ADJUST ' '
431/* Character class expressions */
432#define CLASS_ALNUM "[:alnum:]"
433#define CLASS_ALPHA "[:alpha:]"
434#define CLASS_BLANK "[:blank:]"
435#define CLASS_CNTRL "[:cntrl:]"
436#define CLASS_DIGIT "[:digit:]"
437#define CLASS_GRAPH "[:graph:]"
438#define CLASS_LOWER "[:lower:]"
439#define CLASS_PRINT "[:print:]"
440#define CLASS_PUNCT "[:punct:]"
441#define CLASS_SPACE "[:space:]"
442#define CLASS_UPPER "[:upper:]"
443#define CLASS_XDIGIT "[:xdigit:]"
452 * C Widechar character (wint_t)
466 * S Widechar string (wchar_t *)
475 * D Binary Coded Decimal %D(length,precision) (OS/390)
477#define SPECIFIER_CHAR 'c'
478#define SPECIFIER_STRING 's'
479#define SPECIFIER_DECIMAL 'd'
480#define SPECIFIER_INTEGER 'i'
481#define SPECIFIER_UNSIGNED 'u'
482#define SPECIFIER_OCTAL 'o'
483#define SPECIFIER_HEX 'x'
484#define SPECIFIER_HEX_UPPER 'X'
485#define SPECIFIER_FLOAT_E 'e'
486#define SPECIFIER_FLOAT_E_UPPER 'E'
487#define SPECIFIER_FLOAT_F 'f'
488#define SPECIFIER_FLOAT_F_UPPER 'F'
489#define SPECIFIER_FLOAT_G 'g'
490#define SPECIFIER_FLOAT_G_UPPER 'G'
491#define SPECIFIER_POINTER 'p'
492#define SPECIFIER_GROUP '['
493#define SPECIFIER_UNGROUP ']'
494#define SPECIFIER_COUNT 'n'
496# define SPECIFIER_CHAR_UPPER 'C'
497# define SPECIFIER_STRING_UPPER 'S'
500# define SPECIFIER_HEXFLOAT 'a'
501# define SPECIFIER_HEXFLOAT_UPPER 'A'
504# define SPECIFIER_ERRNO 'm'
507# define SPECIFIER_BINARY 'b'
508# define SPECIFIER_BINARY_UPPER 'B'
509# define SPECIFIER_USER_DEFINED_BEGIN '<'
510# define SPECIFIER_USER_DEFINED_END '>'
511# define SPECIFIER_USER_DEFINED_SEPARATOR ':'
518 * Numbers = d,i,o,u,x,X
519 * Float = a,A,e,E,f,F,g,G
525 * Use the 9th parameter. 9 can be any number between 1 and
526 * the maximal argument
529 * Set width to 9. 9 can be any number, but must not be postfixed
534 * (unsigned) short int
542 * (unsigned) long int
550 * (unsigned) long long int
558 * Decimal-point is always present
560 * non-printable characters are handled as \number
571 * print: use parameter
572 * scan: no parameter (ignore)
582 * Integer part grouped in thousands
584 * Number grouped in nibbles (4 bits)
593 * @ Parameter (for both print and scan)
597 * The following options exists
599 * I16 = 16-bit integer
600 * I32 = 32-bit integer
601 * I64 = 64-bit integer
603#define QUALIFIER_POSITION '$'
604#define QUALIFIER_SHORT 'h'
605#define QUALIFIER_LONG 'l'
606#define QUALIFIER_LONG_UPPER 'L'
607#define QUALIFIER_ALTERNATIVE '#'
608#define QUALIFIER_SPACE ' '
609#define QUALIFIER_PLUS '+'
610#define QUALIFIER_MINUS '-'
611#define QUALIFIER_DOT '.'
612#define QUALIFIER_STAR '*'
613#define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
615# define QUALIFIER_SIZE_T 'z'
616# define QUALIFIER_PTRDIFF_T 't'
617# define QUALIFIER_INTMAX_T 'j'
619#if TRIO_BSD || TRIO_GNU
620# define QUALIFIER_QUAD 'q'
623# define QUALIFIER_SIZE_T_UPPER 'Z'
626# define QUALIFIER_WIDECHAR 'w'
629# define QUALIFIER_FIXED_SIZE 'I'
632# define QUALIFIER_QUOTE '\''
633# define QUALIFIER_STICKY '!'
634# define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
635# define QUALIFIER_PARAM '@' /* Experimental */
636# define QUALIFIER_COLON ':' /* For scanlists */
637# define QUALIFIER_EQUAL '=' /* For scanlists */
638# define QUALIFIER_ROUNDING_UPPER 'R'
650 /* An indication of which entry in the data union is used */
654 /* The width qualifier */
656 /* The precision qualifier */
658 /* The base qualifier */
660 /* The size for the variable size qualifier */
662 /* The marker of the end of the specifier */
663 int indexAfterSpecifier;
664 /* The data from the argument list */
668 trio_wchar_t *wstring;
670 trio_pointer_t pointer;
672 trio_intmax_t as_signed;
673 trio_uintmax_t as_unsigned;
676 double *doublePointer;
677 trio_long_double_t longdoubleNumber;
678 trio_long_double_t *longdoublePointer;
681 /* For the user-defined specifier */
682 char user_name[MAX_USER_NAME];
683 char user_data[MAX_USER_DATA];
686/* Container for customized functions */
689 trio_outstream_t out;
692 trio_pointer_t closure;
695/* General trio "class" */
696typedef struct _trio_class_t {
698 * The function to write characters to a stream.
700 void (*OutStream) TRIO_PROTO((struct _trio_class_t *, int));
702 * The function to read characters from a stream.
704 void (*InStream) TRIO_PROTO((struct _trio_class_t *, int *));
706 * The current location in the stream.
708 trio_pointer_t location;
710 * The character currently being processed.
714 * The number of characters that would have been written/read
715 * if there had been sufficient space.
719 * The number of characters that are actually written/read.
720 * Processed and committed will only differ for the *nprintf
721 * and *nscanf functions.
725 * The upper limit of characters that may be written/read.
729 * The last output error that was detected.
734/* References (for user-defined callbacks) */
735typedef struct _trio_reference_t {
737 trio_parameter_t *parameter;
740/* Registered entries (for user-defined callbacks) */
741typedef struct _trio_userdef_t {
742 struct _trio_userdef_t *next;
743 trio_callback_t callback;
753static TRIO_CONST char rcsid[] = "@(#)$Id$";
756 * Need this to workaround a parser bug in HP C/iX compiler that fails
757 * to resolves macro definitions that includes type 'long double',
758 * e.g: va_arg(arg_ptr, long double)
760#if defined(TRIO_PLATFORM_MPEIX)
761static TRIO_CONST trio_long_double_t ___dummy_long_double = 0;
764static TRIO_CONST char internalNullString[] = "(nil)";
766#if defined(USE_LOCALE)
767static struct lconv *internalLocaleValues = NULL;
771 * UNIX98 says "in a locale where the radix character is not defined,
772 * the radix character
defaults to
a period (.)
"
774static int internalDecimalPointLength = 1;
775static int internalThousandSeparatorLength = 1;
776static char internalDecimalPoint = '.';
777static char internalDecimalPointString[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ".
";
778static char internalThousandSeparator[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ",
";
779static char internalGrouping[MAX_LOCALE_GROUPS] = { (char)NO_GROUPING };
781static TRIO_CONST char internalDigitsLower[] = "0123456789abcdefghijklmnopqrstuvwxyz
";
782static TRIO_CONST char internalDigitsUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
";
783static BOOLEAN_T internalDigitsUnconverted = TRUE;
784static int internalDigitArray[128];
786static BOOLEAN_T internalCollationUnconverted = TRUE;
787static char internalCollationArray[MAX_CHARACTER_CLASS][MAX_CHARACTER_CLASS];
791static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion = NULL;
792static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion = NULL;
793static trio_userdef_t *internalUserDef = NULL;
803#if defined(TRIO_MINIMAL)
804# define TRIO_STRING_PUBLIC static
806#endif /* defined(TRIO_MINIMAL) */
815TRIO_PRIVATE BOOLEAN_T
817TRIO_ARGS1((character),
818 TRIO_CONST char character)
820 /* QUALIFIER_POSITION is not included */
823 case '0': case '1': case '2': case '3': case '4':
824 case '5': case '6': case '7': case '8': case '9':
826 case QUALIFIER_MINUS:
827 case QUALIFIER_SPACE:
830 case QUALIFIER_ALTERNATIVE:
831 case QUALIFIER_SHORT:
833 case QUALIFIER_LONG_UPPER:
834 case QUALIFIER_CIRCUMFLEX:
835#if defined(QUALIFIER_SIZE_T)
836 case QUALIFIER_SIZE_T:
838#if defined(QUALIFIER_PTRDIFF_T)
839 case QUALIFIER_PTRDIFF_T:
841#if defined(QUALIFIER_INTMAX_T)
842 case QUALIFIER_INTMAX_T:
844#if defined(QUALIFIER_QUAD)
847#if defined(QUALIFIER_SIZE_T_UPPER)
848 case QUALIFIER_SIZE_T_UPPER:
850#if defined(QUALIFIER_WIDECHAR)
851 case QUALIFIER_WIDECHAR:
853#if defined(QUALIFIER_QUOTE)
854 case QUALIFIER_QUOTE:
856#if defined(QUALIFIER_STICKY)
857 case QUALIFIER_STICKY:
859#if defined(QUALIFIER_VARSIZE)
860 case QUALIFIER_VARSIZE:
862#if defined(QUALIFIER_PARAM)
863 case QUALIFIER_PARAM:
865#if defined(QUALIFIER_FIXED_SIZE)
866 case QUALIFIER_FIXED_SIZE:
868#if defined(QUALIFIER_ROUNDING_UPPER)
869 case QUALIFIER_ROUNDING_UPPER:
880#if defined(USE_LOCALE)
882TrioSetLocale(TRIO_NOARGS)
884 internalLocaleValues = (struct lconv *)localeconv();
885 if (internalLocaleValues)
887 if ((internalLocaleValues->decimal_point) &&
888 (internalLocaleValues->decimal_point[0] != NIL))
890 internalDecimalPointLength = trio_length(internalLocaleValues->decimal_point);
891 if (internalDecimalPointLength == 1)
893 internalDecimalPoint = internalLocaleValues->decimal_point[0];
897 internalDecimalPoint = NIL;
898 trio_copy_max(internalDecimalPointString,
899 sizeof(internalDecimalPointString),
900 internalLocaleValues->decimal_point);
903 if ((internalLocaleValues->thousands_sep) &&
904 (internalLocaleValues->thousands_sep[0] != NIL))
906 trio_copy_max(internalThousandSeparator,
907 sizeof(internalThousandSeparator),
908 internalLocaleValues->thousands_sep);
909 internalThousandSeparatorLength = trio_length(internalThousandSeparator);
911 if ((internalLocaleValues->grouping) &&
912 (internalLocaleValues->grouping[0] != NIL))
914 trio_copy_max(internalGrouping,
915 sizeof(internalGrouping),
916 internalLocaleValues->grouping);
920#endif /* defined(USE_LOCALE) */
923TrioCalcThousandSeparatorLength
929 int step = NO_GROUPING;
930 char *groupingPointer = internalGrouping;
934 if (*groupingPointer == CHAR_MAX)
936 /* Disable grouping */
939 else if (*groupingPointer == 0)
941 /* Repeat last group */
942 if (step == NO_GROUPING)
944 /* Error in locale */
950 step = *groupingPointer++;
953 count += internalThousandSeparatorLength;
962TRIO_PRIVATE BOOLEAN_T
963TrioFollowedBySeparator
964TRIO_ARGS1((position),
969 char *groupingPointer = internalGrouping;
976 if (*groupingPointer == CHAR_MAX)
978 /* Disable grouping */
981 else if (*groupingPointer != 0)
983 step = *groupingPointer++;
989 return (position == 0);
1002TRIO_ARGS2((format, indexPointer),
1003 TRIO_CONST char *format,
1009 int index = *indexPointer;
1011 number = (int)trio_to_long(&format[index], &tmpformat, BASE_DECIMAL);
1012 index = (int)(tmpformat - format);
1013 if ((number != 0) && (QUALIFIER_POSITION == format[index++]))
1015 *indexPointer = index;
1017 * number is decreased by 1, because n$ starts from 1, whereas
1018 * the array it is indexing starts from 0.
1033TRIO_PRIVATE trio_userdef_t *
1035TRIO_ARGS2((name, prev),
1036 TRIO_CONST char *name,
1037 trio_userdef_t **prev)
1039 trio_userdef_t *def;
1041 if (internalEnterCriticalRegion)
1042 (void)internalEnterCriticalRegion(NULL);
1044 for (def = internalUserDef; def; def = def->next)
1046 /* Case-sensitive string comparison */
1047 if (trio_equal_case(def->name, name))
1054 if (internalLeaveCriticalRegion)
1055 (void)internalLeaveCriticalRegion(NULL);
1067TRIO_PRIVATE trio_long_double_t
1069TRIO_ARGS2((number, exponent),
1073 trio_long_double_t result;
1079 /* Speed up calculation of common cases */
1081 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E-1);
1084 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+0);
1087 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+1);
1090 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+2);
1093 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+3);
1096 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+4);
1099 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+5);
1102 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+6);
1105 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+7);
1108 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+8);
1111 result = powl((trio_long_double_t)number,
1112 (trio_long_double_t)exponent);
1118 return powl((trio_long_double_t)number, (trio_long_double_t)exponent);
1128TRIO_ARGS2((number, base),
1136 /* xlC crashes on log(0) */
1137 result = (number == 0.0) ? trio_ninf() : trio_nan();
1143 result = log10(number);
1147 result = log10(number) / log10((double)base);
1163 case BASE_BINARY : return 1.0;
1164 case BASE_OCTAL : return 3.0;
1165 case BASE_DECIMAL: return 3.321928094887362345;
1166 case BASE_HEX : return 4.0;
1167 default : return TrioLogarithm((double)base, 2);
1179TRIO_ARGS5((type, format, parameters, arglist, argarray),
1181 TRIO_CONST char *format,
1182 trio_parameter_t *parameters,
1183 TRIO_VA_LIST_PTR arglist,
1184 trio_pointer_t *argarray)
1186 /* Count the number of times a parameter is referenced */
1187 unsigned short usedEntries[MAX_PARAMETERS];
1188 /* Parameter counters */
1189 int parameterPosition;
1192 /* Utility variables */
1198 int index; /* Index into formatting string */
1199 int dots; /* Count number of dots in modifier part */
1200 BOOLEAN_T positional; /* Does the specifier have a positional? */
1201 BOOLEAN_T gotSticky = FALSE; /* Are there any sticky modifiers at all? */
1203 * indices specifies the order in which the parameters must be
1204 * read from the va_args (this is necessary to handle positionals)
1206 int indices[MAX_PARAMETERS];
1208 /* Various variables */
1210#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1218 /* One and only one of arglist and argarray must be used */
1219 assert((arglist != NULL) ^ (argarray != NULL));
1222 * The 'parameters' array is not initialized, but we need to
1223 * know which entries we have used.
1225 memset(usedEntries, 0, sizeof(usedEntries));
1229 parameterPosition = 0;
1230#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1231 (void)mblen(NULL, 0);
1234 while (format[index])
1236#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1237 if (! isascii(format[index]))
1240 * Multibyte characters cannot be legal specifiers or
1241 * modifiers, so we skip over them.
1243 charlen = mblen(&format[index], MB_LEN_MAX);
1244 index += (charlen > 0) ? charlen : 1;
1245 continue; /* while */
1247#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
1248 if (CHAR_IDENTIFIER == format[index++])
1250 if (CHAR_IDENTIFIER == format[index])
1253 continue; /* while */
1258 currentParam = TrioGetPosition(format, &index);
1259 positional = (NO_POSITION != currentParam);
1262 /* We have no positional, get the next counter */
1263 currentParam = parameterPosition;
1265 if(currentParam >= MAX_PARAMETERS)
1267 /* Bail out completely to make the error more obvious */
1268 return TRIO_ERROR_RETURN(TRIO_ETOOMANY, index);
1271 if (currentParam > maxParam)
1272 maxParam = currentParam;
1274 /* Default values */
1276 precision = NO_PRECISION;
1280 while (TrioIsQualifier(format[index]))
1282 ch = format[index++];
1286 case QUALIFIER_SPACE:
1287 flags |= FLAGS_SPACE;
1290 case QUALIFIER_PLUS:
1291 flags |= FLAGS_SHOWSIGN;
1294 case QUALIFIER_MINUS:
1295 flags |= FLAGS_LEFTADJUST;
1296 flags &= ~FLAGS_NILPADDING;
1299 case QUALIFIER_ALTERNATIVE:
1300 flags |= FLAGS_ALTERNATIVE;
1304 if (dots == 0) /* Precision */
1308 /* Skip if no precision */
1309 if (QUALIFIER_DOT == format[index])
1312 /* After the first dot we have the precision */
1313 flags |= FLAGS_PRECISION;
1314 if ((QUALIFIER_STAR == format[index])
1315#if defined(QUALIFIER_PARAM)
1316 || (QUALIFIER_PARAM == format[index])
1321 flags |= FLAGS_PRECISION_PARAMETER;
1323 precision = TrioGetPosition(format, &index);
1324 if (precision == NO_POSITION)
1326 parameterPosition++;
1328 precision = parameterPosition;
1331 precision = currentParam;
1332 currentParam = precision + 1;
1338 currentParam = precision + 1;
1339 if (width > maxParam)
1340 maxParam = precision;
1342 if (currentParam > maxParam)
1343 maxParam = currentParam;
1347 precision = trio_to_long(&format[index],
1350 index = (int)(tmpformat - format);
1353 else if (dots == 1) /* Base */
1357 /* After the second dot we have the base */
1358 flags |= FLAGS_BASE;
1359 if ((QUALIFIER_STAR == format[index])
1360#if defined(QUALIFIER_PARAM)
1361 || (QUALIFIER_PARAM == format[index])
1366 flags |= FLAGS_BASE_PARAMETER;
1367 base = TrioGetPosition(format, &index);
1368 if (base == NO_POSITION)
1370 parameterPosition++;
1372 base = parameterPosition;
1375 base = currentParam;
1376 currentParam = base + 1;
1382 currentParam = base + 1;
1383 if (base > maxParam)
1386 if (currentParam > maxParam)
1387 maxParam = currentParam;
1391 base = trio_to_long(&format[index],
1394 if (base > MAX_BASE)
1395 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1396 index = (int)(tmpformat - format);
1401 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1403 break; /* QUALIFIER_DOT */
1405#if defined(QUALIFIER_PARAM)
1406 case QUALIFIER_PARAM:
1410 case QUALIFIER_STAR:
1411 /* This has different meanings for print and scan */
1412 if (TYPE_PRINT == type)
1414 /* Read with from parameter */
1415 flags |= (FLAGS_WIDTH | FLAGS_WIDTH_PARAMETER);
1416 width = TrioGetPosition(format, &index);
1417 if (width == NO_POSITION)
1419 parameterPosition++;
1421 width = parameterPosition;
1424 width = currentParam;
1425 currentParam = width + 1;
1431 currentParam = width + 1;
1432 if (width > maxParam)
1435 if (currentParam > maxParam)
1436 maxParam = currentParam;
1440 /* Scan, but do not store result */
1441 flags |= FLAGS_IGNORE;
1444 break; /* QUALIFIER_STAR */
1447 if (! (flags & FLAGS_LEFTADJUST))
1448 flags |= FLAGS_NILPADDING;
1450 case '1': case '2': case '3': case '4':
1451 case '5': case '6': case '7': case '8': case '9':
1452 flags |= FLAGS_WIDTH;
1453 /* &format[index - 1] is used to "
rewind" the read
1454 * character from format
1456 width = trio_to_long(&format[index - 1],
1459 index = (int)(tmpformat - format);
1462 case QUALIFIER_SHORT:
1463 if (flags & FLAGS_SHORTSHORT)
1464 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1465 else if (flags & FLAGS_SHORT)
1466 flags |= FLAGS_SHORTSHORT;
1468 flags |= FLAGS_SHORT;
1471 case QUALIFIER_LONG:
1472 if (flags & FLAGS_QUAD)
1473 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1474 else if (flags & FLAGS_LONG)
1475 flags |= FLAGS_QUAD;
1477 flags |= FLAGS_LONG;
1480 case QUALIFIER_LONG_UPPER:
1481 flags |= FLAGS_LONGDOUBLE;
1484#if defined(QUALIFIER_SIZE_T)
1485 case QUALIFIER_SIZE_T:
1486 flags |= FLAGS_SIZE_T;
1487 /* Modify flags for later truncation of number */
1488 if (sizeof(size_t) == sizeof(trio_ulonglong_t))
1489 flags |= FLAGS_QUAD;
1490 else if (sizeof(size_t) == sizeof(long))
1491 flags |= FLAGS_LONG;
1495#if defined(QUALIFIER_PTRDIFF_T)
1496 case QUALIFIER_PTRDIFF_T:
1497 flags |= FLAGS_PTRDIFF_T;
1498 if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t))
1499 flags |= FLAGS_QUAD;
1500 else if (sizeof(ptrdiff_t) == sizeof(long))
1501 flags |= FLAGS_LONG;
1505#if defined(QUALIFIER_INTMAX_T)
1506 case QUALIFIER_INTMAX_T:
1507 flags |= FLAGS_INTMAX_T;
1508 if (sizeof(trio_intmax_t) == sizeof(trio_ulonglong_t))
1509 flags |= FLAGS_QUAD;
1510 else if (sizeof(trio_intmax_t) == sizeof(long))
1511 flags |= FLAGS_LONG;
1515#if defined(QUALIFIER_QUAD)
1516 case QUALIFIER_QUAD:
1517 flags |= FLAGS_QUAD;
1521#if defined(QUALIFIER_FIXED_SIZE)
1522 case QUALIFIER_FIXED_SIZE:
1523 if (flags & FLAGS_FIXED_SIZE)
1524 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1526 if (flags & (FLAGS_ALL_SIZES | FLAGS_LONGDOUBLE |
1527 FLAGS_WIDECHAR | FLAGS_VARSIZE_PARAMETER))
1528 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1530 if ((format[index] == '6') &&
1531 (format[index + 1] == '4'))
1533 varsize = sizeof(trio_int64_t);
1536 else if ((format[index] == '3') &&
1537 (format[index + 1] == '2'))
1539 varsize = sizeof(trio_int32_t);
1542 else if ((format[index] == '1') &&
1543 (format[index + 1] == '6'))
1545 varsize = sizeof(trio_int16_t);
1548 else if (format[index] == '8')
1550 varsize = sizeof(trio_int8_t);
1554 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1556 flags |= FLAGS_FIXED_SIZE;
1560#if defined(QUALIFIER_WIDECHAR)
1561 case QUALIFIER_WIDECHAR:
1562 flags |= FLAGS_WIDECHAR;
1566#if defined(QUALIFIER_SIZE_T_UPPER)
1567 case QUALIFIER_SIZE_T_UPPER:
1571#if defined(QUALIFIER_QUOTE)
1572 case QUALIFIER_QUOTE:
1573 flags |= FLAGS_QUOTE;
1577#if defined(QUALIFIER_STICKY)
1578 case QUALIFIER_STICKY:
1579 flags |= FLAGS_STICKY;
1584#if defined(QUALIFIER_VARSIZE)
1585 case QUALIFIER_VARSIZE:
1586 flags |= FLAGS_VARSIZE_PARAMETER;
1587 parameterPosition++;
1589 varsize = parameterPosition;
1592 varsize = currentParam;
1593 currentParam = varsize + 1;
1595 if (currentParam > maxParam)
1596 maxParam = currentParam;
1600#if defined(QUALIFIER_ROUNDING_UPPER)
1601 case QUALIFIER_ROUNDING_UPPER:
1602 flags |= FLAGS_ROUNDING;
1607 /* Bail out completely to make the error more obvious */
1608 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1610 } /* while qualifier */
1613 * Parameters only need the type and value. The value is
1616 if (flags & FLAGS_WIDTH_PARAMETER)
1618 usedEntries[width] += 1;
1619 parameters[pos].type = FORMAT_PARAMETER;
1620 parameters[pos].flags = 0;
1621 indices[width] = pos;
1624 if (flags & FLAGS_PRECISION_PARAMETER)
1626 usedEntries[precision] += 1;
1627 parameters[pos].type = FORMAT_PARAMETER;
1628 parameters[pos].flags = 0;
1629 indices[precision] = pos;
1632 if (flags & FLAGS_BASE_PARAMETER)
1634 usedEntries[base] += 1;
1635 parameters[pos].type = FORMAT_PARAMETER;
1636 parameters[pos].flags = 0;
1637 indices[base] = pos;
1640 if (flags & FLAGS_VARSIZE_PARAMETER)
1642 usedEntries[varsize] += 1;
1643 parameters[pos].type = FORMAT_PARAMETER;
1644 parameters[pos].flags = 0;
1645 indices[varsize] = pos;
1649 indices[currentParam] = pos;
1651 switch (format[index++])
1653#if defined(SPECIFIER_CHAR_UPPER)
1654 case SPECIFIER_CHAR_UPPER:
1655 flags |= FLAGS_WIDECHAR;
1658 case SPECIFIER_CHAR:
1659 if (flags & FLAGS_LONG)
1660 flags |= FLAGS_WIDECHAR;
1661 else if (flags & FLAGS_SHORT)
1662 flags &= ~FLAGS_WIDECHAR;
1663 parameters[pos].type = FORMAT_CHAR;
1666#if defined(SPECIFIER_STRING_UPPER)
1667 case SPECIFIER_STRING_UPPER:
1668 flags |= FLAGS_WIDECHAR;
1671 case SPECIFIER_STRING:
1672 if (flags & FLAGS_LONG)
1673 flags |= FLAGS_WIDECHAR;
1674 else if (flags & FLAGS_SHORT)
1675 flags &= ~FLAGS_WIDECHAR;
1676 parameters[pos].type = FORMAT_STRING;
1679 case SPECIFIER_GROUP:
1680 if (TYPE_SCAN == type)
1683 parameters[pos].type = FORMAT_GROUP;
1684 if (format[index] == QUALIFIER_CIRCUMFLEX)
1686 if (format[index] == SPECIFIER_UNGROUP)
1688 if (format[index] == QUALIFIER_MINUS)
1690 /* Skip nested brackets */
1691 while (format[index] != NIL)
1693 if (format[index] == SPECIFIER_GROUP)
1697 else if (format[index] == SPECIFIER_UNGROUP)
1710 case SPECIFIER_INTEGER:
1711 parameters[pos].type = FORMAT_INT;
1714 case SPECIFIER_UNSIGNED:
1715 flags |= FLAGS_UNSIGNED;
1716 parameters[pos].type = FORMAT_INT;
1719 case SPECIFIER_DECIMAL:
1720 /* Disable base modifier */
1721 flags &= ~FLAGS_BASE_PARAMETER;
1722 base = BASE_DECIMAL;
1723 parameters[pos].type = FORMAT_INT;
1726 case SPECIFIER_OCTAL:
1727 flags |= FLAGS_UNSIGNED;
1728 flags &= ~FLAGS_BASE_PARAMETER;
1730 parameters[pos].type = FORMAT_INT;
1733#if defined(SPECIFIER_BINARY)
1734 case SPECIFIER_BINARY_UPPER:
1735 flags |= FLAGS_UPPER;
1737 case SPECIFIER_BINARY:
1738 flags |= FLAGS_NILPADDING;
1739 flags &= ~FLAGS_BASE_PARAMETER;
1741 parameters[pos].type = FORMAT_INT;
1745 case SPECIFIER_HEX_UPPER:
1746 flags |= FLAGS_UPPER;
1749 flags |= FLAGS_UNSIGNED;
1750 flags &= ~FLAGS_BASE_PARAMETER;
1752 parameters[pos].type = FORMAT_INT;
1755 case SPECIFIER_FLOAT_E_UPPER:
1756 flags |= FLAGS_UPPER;
1758 case SPECIFIER_FLOAT_E:
1759 flags |= FLAGS_FLOAT_E;
1760 parameters[pos].type = FORMAT_DOUBLE;
1763 case SPECIFIER_FLOAT_G_UPPER:
1764 flags |= FLAGS_UPPER;
1766 case SPECIFIER_FLOAT_G:
1767 flags |= FLAGS_FLOAT_G;
1768 parameters[pos].type = FORMAT_DOUBLE;
1771 case SPECIFIER_FLOAT_F_UPPER:
1772 flags |= FLAGS_UPPER;
1774 case SPECIFIER_FLOAT_F:
1775 parameters[pos].type = FORMAT_DOUBLE;
1778 case SPECIFIER_POINTER:
1779 if (sizeof(trio_pointer_t) == sizeof(trio_ulonglong_t))
1780 flags |= FLAGS_QUAD;
1781 else if (sizeof(trio_pointer_t) == sizeof(long))
1782 flags |= FLAGS_LONG;
1783 parameters[pos].type = FORMAT_POINTER;
1786 case SPECIFIER_COUNT:
1787 parameters[pos].type = FORMAT_COUNT;
1790#if defined(SPECIFIER_HEXFLOAT)
1791# if defined(SPECIFIER_HEXFLOAT_UPPER)
1792 case SPECIFIER_HEXFLOAT_UPPER:
1793 flags |= FLAGS_UPPER;
1796 case SPECIFIER_HEXFLOAT:
1798 parameters[pos].type = FORMAT_DOUBLE;
1802#if defined(FORMAT_ERRNO)
1803 case SPECIFIER_ERRNO:
1804 parameters[pos].type = FORMAT_ERRNO;
1808#if defined(SPECIFIER_USER_DEFINED_BEGIN)
1809 case SPECIFIER_USER_DEFINED_BEGIN:
1812 int without_namespace = TRUE;
1814 parameters[pos].type = FORMAT_USER_DEFINED;
1815 parameters[pos].user_name[0] = NIL;
1816 tmpformat = (char *)&format[index];
1818 while ((ch = format[index]))
1821 if (ch == SPECIFIER_USER_DEFINED_END)
1823 if (without_namespace)
1825 /* We must get the handle first */
1826 parameters[pos].type = FORMAT_PARAMETER;
1827 parameters[pos].indexAfterSpecifier = index;
1828 parameters[pos].flags = FLAGS_USER_DEFINED;
1829 /* Adjust parameters for insertion of new one */
1831 usedEntries[currentParam] += 1;
1832 parameters[pos].type = FORMAT_USER_DEFINED;
1834 indices[currentParam] = pos;
1835 if (currentParam > maxParam)
1836 maxParam = currentParam;
1838 /* Copy the user data */
1839 max = (unsigned int)(&format[index] - tmpformat);
1840 if (max > MAX_USER_DATA)
1841 max = MAX_USER_DATA;
1842 trio_copy_max(parameters[pos].user_data,
1847 if (ch == SPECIFIER_USER_DEFINED_SEPARATOR)
1849 without_namespace = FALSE;
1850 /* Copy the namespace for later looking-up */
1851 max = (int)(&format[index] - tmpformat);
1852 if (max > MAX_USER_NAME)
1853 max = MAX_USER_NAME;
1854 trio_copy_max(parameters[pos].user_name,
1857 tmpformat = (char *)&format[index];
1860 if (ch != SPECIFIER_USER_DEFINED_END)
1861 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1864#endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
1867 /* Bail out completely to make the error more obvious */
1868 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1871 /* Count the number of times this entry has been used */
1872 usedEntries[currentParam] += 1;
1874 /* Find last sticky parameters */
1875 if (gotSticky && !(flags & FLAGS_STICKY))
1877 for (i = pos - 1; i >= 0; i--)
1879 if (parameters[i].type == FORMAT_PARAMETER)
1881 if ((parameters[i].flags & FLAGS_STICKY) &&
1882 (parameters[i].type == parameters[pos].type))
1884 /* Do not overwrite current qualifiers */
1885 flags |= (parameters[i].flags & (unsigned long)~FLAGS_STICKY);
1886 if (width == NO_WIDTH)
1887 width = parameters[i].width;
1888 if (precision == NO_PRECISION)
1889 precision = parameters[i].precision;
1890 if (base == NO_BASE)
1891 base = parameters[i].base;
1897 parameters[pos].indexAfterSpecifier = index;
1898 parameters[pos].flags = flags;
1899 parameters[pos].width = width;
1900 parameters[pos].precision = precision;
1901 parameters[pos].base = (base == NO_BASE) ? BASE_DECIMAL : base;
1902 parameters[pos].varsize = varsize;
1906 parameterPosition++;
1908 } /* if identifier */
1910 } /* while format characters left */
1912 for (num = 0; num <= maxParam; num++)
1914 if (usedEntries[num] != 1)
1916 if (usedEntries[num] == 0) /* gap detected */
1917 return TRIO_ERROR_RETURN(TRIO_EGAP, num);
1918 else /* double references detected */
1919 return TRIO_ERROR_RETURN(TRIO_EDBLREF, num);
1925 * FORMAT_PARAMETERS are only present if they must be read,
1926 * so it makes no sense to check the ignore flag (besides,
1927 * the flags variable is not set for that particular type)
1929 if ((parameters[i].type != FORMAT_PARAMETER) &&
1930 (parameters[i].flags & FLAGS_IGNORE))
1931 continue; /* for all arguments */
1934 * The stack arguments are read according to ANSI C89
1935 * default argument promotions:
1939 * unsigned char = unsigned int
1940 * unsigned short = unsigned int
1943 * In addition to the ANSI C89 these types are read (the
1944 * default argument promotions of C99 has not been
1953 switch (parameters[i].type)
1958 if (flags & FLAGS_WIDECHAR)
1960 parameters[i].data.wstring = (argarray == NULL)
1961 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_wchar_t *)
1962 : (trio_wchar_t *)(argarray[num]);
1967 parameters[i].data.string = (argarray == NULL)
1968 ? va_arg(TRIO_VA_LIST_DEREF(arglist), char *)
1969 : (char *)(argarray[num]);
1973#if defined(FORMAT_USER_DEFINED)
1974 case FORMAT_USER_DEFINED:
1976 case FORMAT_POINTER:
1978 case FORMAT_UNKNOWN:
1979 parameters[i].data.pointer = (argarray == NULL)
1980 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t )
1986 if (TYPE_SCAN == type)
1988 if (argarray == NULL)
1989 parameters[i].data.pointer =
1990 (trio_pointer_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t);
1993 if (parameters[i].type == FORMAT_CHAR)
1994 parameters[i].data.pointer =
1995 (trio_pointer_t)((char *)argarray[num]);
1996 else if (parameters[i].flags & FLAGS_SHORT)
1997 parameters[i].data.pointer =
1998 (trio_pointer_t)((short *)argarray[num]);
2000 parameters[i].data.pointer =
2001 (trio_pointer_t)((int *)argarray[num]);
2006#if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
2007 if (parameters[i].flags
2008 & (FLAGS_VARSIZE_PARAMETER | FLAGS_FIXED_SIZE))
2010 if (parameters[i].flags & FLAGS_VARSIZE_PARAMETER)
2013 * Variable sizes are mapped onto the fixed sizes, in
2014 * accordance with integer promotion.
2016 * Please note that this may not be portable, as we
2017 * only guess the size, not the layout of the numbers.
2018 * For example, if int is little-endian, and long is
2019 * big-endian, then this will fail.
2021 varsize = (int)parameters[parameters[i].varsize].data.number.as_unsigned;
2025 /* Used for the I<bits> modifiers */
2026 varsize = parameters[i].varsize;
2028 parameters[i].flags &= ~FLAGS_ALL_VARSIZES;
2030 if (varsize <= (int)sizeof(int))
2032 else if (varsize <= (int)sizeof(long))
2033 parameters[i].flags |= FLAGS_LONG;
2034#if defined(QUALIFIER_INTMAX_T)
2035 else if (varsize <= (int)sizeof(trio_longlong_t))
2036 parameters[i].flags |= FLAGS_QUAD;
2038 parameters[i].flags |= FLAGS_INTMAX_T;
2041 parameters[i].flags |= FLAGS_QUAD;
2044#endif /* defined(QUALIFIER_VARSIZE) */
2045#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
2046 if (parameters[i].flags & FLAGS_SIZE_T)
2047 parameters[i].data.number.as_unsigned = (argarray == NULL)
2048 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), size_t)
2049 : (trio_uintmax_t)(*((size_t *)argarray[num]));
2052#if defined(QUALIFIER_PTRDIFF_T)
2053 if (parameters[i].flags & FLAGS_PTRDIFF_T)
2054 parameters[i].data.number.as_unsigned = (argarray == NULL)
2055 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), ptrdiff_t)
2056 : (trio_uintmax_t)(*((ptrdiff_t *)argarray[num]));
2059#if defined(QUALIFIER_INTMAX_T)
2060 if (parameters[i].flags & FLAGS_INTMAX_T)
2061 parameters[i].data.number.as_unsigned = (argarray == NULL)
2062 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_intmax_t)
2063 : (trio_uintmax_t)(*((trio_intmax_t *)argarray[num]));
2066 if (parameters[i].flags & FLAGS_QUAD)
2067 parameters[i].data.number.as_unsigned = (argarray == NULL)
2068 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_ulonglong_t)
2069 : (trio_uintmax_t)(*((trio_ulonglong_t *)argarray[num]));
2070 else if (parameters[i].flags & FLAGS_LONG)
2071 parameters[i].data.number.as_unsigned = (argarray == NULL)
2072 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), long)
2073 : (trio_uintmax_t)(*((long *)argarray[num]));
2076 if (argarray == NULL)
2077 parameters[i].data.number.as_unsigned = (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), int);
2080 if (parameters[i].type == FORMAT_CHAR)
2081 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((char *)argarray[num]));
2082 else if (parameters[i].flags & FLAGS_SHORT)
2083 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((short *)argarray[num]));
2085 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((int *)argarray[num]));
2091 case FORMAT_PARAMETER:
2093 * The parameter for the user-defined specifier is a pointer,
2094 * whereas the rest (width, precision, base) uses an integer.
2096 if (parameters[i].flags & FLAGS_USER_DEFINED)
2097 parameters[i].data.pointer = (argarray == NULL)
2098 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t )
2101 parameters[i].data.number.as_unsigned = (argarray == NULL)
2102 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), int)
2103 : (trio_uintmax_t)(*((int *)argarray[num]));
2107 if (TYPE_SCAN == type)
2109 if (parameters[i].flags & FLAGS_LONGDOUBLE)
2110 parameters[i].data.longdoublePointer = (argarray == NULL)
2111 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_long_double_t *)
2112 : (trio_long_double_t *)argarray[num];
2115 if (parameters[i].flags & FLAGS_LONG)
2116 parameters[i].data.doublePointer = (argarray == NULL)
2117 ? va_arg(TRIO_VA_LIST_DEREF(arglist), double *)
2118 : (double *)argarray[num];
2120 parameters[i].data.doublePointer = (argarray == NULL)
2121 ? (double *)va_arg(TRIO_VA_LIST_DEREF(arglist), float *)
2122 : (double *)((float *)argarray[num]);
2127 if (parameters[i].flags & FLAGS_LONGDOUBLE)
2128 parameters[i].data.longdoubleNumber = (argarray == NULL)
2129 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_long_double_t)
2130 : (trio_long_double_t)(*((trio_long_double_t *)argarray[num]));
2133 if (argarray == NULL)
2134 parameters[i].data.longdoubleNumber =
2135 (trio_long_double_t)va_arg(TRIO_VA_LIST_DEREF(arglist), double);
2138 if (parameters[i].flags & FLAGS_SHORT)
2139 parameters[i].data.longdoubleNumber =
2140 (trio_long_double_t)(*((float *)argarray[num]));
2142 parameters[i].data.longdoubleNumber =
2143 (trio_long_double_t)(*((double *)argarray[num]));
2149#if defined(FORMAT_ERRNO)
2151 parameters[i].data.errorNumber = save_errno;
2158 } /* for all specifiers */
2180TRIO_ARGS6((self, number, flags, width, precision, base),
2182 trio_uintmax_t number,
2188 BOOLEAN_T isNegative;
2189 BOOLEAN_T isNumberZero;
2190 BOOLEAN_T isPrecisionZero;
2191 BOOLEAN_T ignoreNumber;
2192 char buffer[MAX_CHARS_IN(trio_uintmax_t) * (1 + MAX_LOCALE_SEPARATOR_LENGTH) + 1];
2195 TRIO_CONST char *digits;
2201 assert(VALID(self));
2202 assert(VALID(self->OutStream));
2203 assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
2205 digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
2206 if (base == NO_BASE)
2207 base = BASE_DECIMAL;
2209 isNumberZero = (number == 0);
2210 isPrecisionZero = (precision == 0);
2211 ignoreNumber = (isNumberZero
2213 && !((flags & FLAGS_ALTERNATIVE) && (base == BASE_OCTAL)));
2215 if (flags & FLAGS_UNSIGNED)
2218 flags &= ~FLAGS_SHOWSIGN;
2222 isNegative = ((trio_intmax_t)number < 0);
2224 number = -((trio_intmax_t)number);
2227 if (flags & FLAGS_QUAD)
2228 number &= (trio_ulonglong_t)-1;
2229 else if (flags & FLAGS_LONG)
2230 number &= (unsigned long)-1;
2232 number &= (unsigned int)-1;
2235 pointer = bufferend = &buffer[sizeof(buffer) - 1];
2237 for (i = 1; i < (int)sizeof(buffer); i++)
2239 *pointer-- = digits[number % base];
2244 if ((flags & FLAGS_QUOTE) && TrioFollowedBySeparator(i + 1))
2247 * We are building the number from the least significant
2248 * to the most significant digit, so we have to copy the
2249 * thousand separator backwards
2251 length = internalThousandSeparatorLength;
2252 if (((int)(pointer - buffer) - length) > 0)
2254 p = &internalThousandSeparator[length - 1];
2255 while (length-- > 0)
2264 width -= (bufferend - pointer) - 1;
2267 /* Adjust precision */
2268 if (NO_PRECISION != precision)
2270 precision -= (bufferend - pointer) - 1;
2273 flags |= FLAGS_NILPADDING;
2276 /* Calculate padding */
2277 count = (! ((flags & FLAGS_LEFTADJUST) || (precision == NO_PRECISION)))
2281 /* Adjust width further */
2282 if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
2284 if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
2293 if (!(flags & FLAGS_NILPADDING) || (count == 0))
2301 /* Output prefixes spaces if needed */
2302 if (! ((flags & FLAGS_LEFTADJUST) ||
2303 ((flags & FLAGS_NILPADDING) && (precision == NO_PRECISION))))
2305 while (width-- > count)
2306 self->OutStream(self, CHAR_ADJUST);
2309 /* width has been adjusted for signs and alternatives */
2311 self->OutStream(self, '-');
2312 else if (flags & FLAGS_SHOWSIGN)
2313 self->OutStream(self, '+');
2314 else if (flags & FLAGS_SPACE)
2315 self->OutStream(self, ' ');
2317 /* Prefix is not written when the value is zero */
2318 if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
2323 self->OutStream(self, '0');
2324 self->OutStream(self, (flags & FLAGS_UPPER) ? 'B' : 'b');
2328 if (!(flags & FLAGS_NILPADDING) || (count == 0))
2329 self->OutStream(self, '0');
2333 self->OutStream(self, '0');
2334 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2342 /* Output prefixed zero padding if needed */
2343 if (flags & FLAGS_NILPADDING)
2345 if (precision == NO_PRECISION)
2347 while (precision-- > 0)
2349 self->OutStream(self, '0');
2356 /* Output the number itself */
2357 while (*(++pointer))
2359 self->OutStream(self, *pointer);
2363 /* Output trailing spaces if needed */
2364 if (flags & FLAGS_LEFTADJUST)
2367 self->OutStream(self, CHAR_ADJUST);
2378TrioWriteStringCharacter
2379TRIO_ARGS3((self, ch, flags),
2384 if (flags & FLAGS_ALTERNATIVE)
2389 * Non-printable characters are converted to C escapes or
2390 * \number, if no C escape exists.
2392 self->OutStream(self, CHAR_BACKSLASH);
2395 case '\007': self->OutStream(self, 'a'); break;
2396 case '\b': self->OutStream(self, 'b'); break;
2397 case '\f': self->OutStream(self, 'f'); break;
2398 case '\n': self->OutStream(self, 'n'); break;
2399 case '\r': self->OutStream(self, 'r'); break;
2400 case '\t': self->OutStream(self, 't'); break;
2401 case '\v': self->OutStream(self, 'v'); break;
2402 case '\\': self->OutStream(self, '\\'); break;
2404 self->OutStream(self, 'x');
2405 TrioWriteNumber(self, (trio_uintmax_t)ch,
2406 FLAGS_UNSIGNED | FLAGS_NILPADDING,
2411 else if (ch == CHAR_BACKSLASH)
2413 self->OutStream(self, CHAR_BACKSLASH);
2414 self->OutStream(self, CHAR_BACKSLASH);
2418 self->OutStream(self, ch);
2423 self->OutStream(self, ch);
2435TRIO_ARGS5((self, string, flags, width, precision),
2437 TRIO_CONST char *string,
2445 assert(VALID(self));
2446 assert(VALID(self->OutStream));
2450 string = internalNullString;
2451 length = sizeof(internalNullString) - 1;
2452 /* Disable quoting for the null pointer */
2453 flags &= (~FLAGS_QUOTE);
2458 length = trio_length(string);
2460 if ((NO_PRECISION != precision) &&
2461 (precision < length))
2467 if (flags & FLAGS_QUOTE)
2468 self->OutStream(self, CHAR_QUOTE);
2470 if (! (flags & FLAGS_LEFTADJUST))
2473 self->OutStream(self, CHAR_ADJUST);
2476 while (length-- > 0)
2478 /* The ctype parameters must be an unsigned char (or EOF) */
2479 ch = (int)((unsigned char)(*string++));
2480 TrioWriteStringCharacter(self, ch, flags);
2483 if (flags & FLAGS_LEFTADJUST)
2486 self->OutStream(self, CHAR_ADJUST);
2488 if (flags & FLAGS_QUOTE)
2489 self->OutStream(self, CHAR_QUOTE);
2500TrioWriteWideStringCharacter
2501TRIO_ARGS4((self, wch, flags, width),
2511 char buffer[MB_LEN_MAX + 1];
2513 if (width == NO_WIDTH)
2514 width = sizeof(buffer);
2516 size = wctomb(buffer, wch);
2517 if ((size <= 0) || (size > width) || (buffer[0] == NIL))
2522 while ((width >= i) && (width-- > 0) && (i-- > 0))
2524 /* The ctype parameters must be an unsigned char (or EOF) */
2525 ch = (int)((unsigned char)(*string++));
2526 TrioWriteStringCharacter(self, ch, flags);
2530#endif /* TRIO_WIDECHAR */
2541TRIO_ARGS5((self, wstring, flags, width, precision),
2543 TRIO_CONST trio_wchar_t *wstring,
2551 assert(VALID(self));
2552 assert(VALID(self->OutStream));
2554#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
2555 (void)mblen(NULL, 0);
2558 if (wstring == NULL)
2560 TrioWriteString(self, NULL, flags, width, precision);
2564 if (NO_PRECISION == precision)
2574 if (flags & FLAGS_QUOTE)
2575 self->OutStream(self, CHAR_QUOTE);
2577 if (! (flags & FLAGS_LEFTADJUST))
2580 self->OutStream(self, CHAR_ADJUST);
2585 size = TrioWriteWideStringCharacter(self, *wstring++, flags, length);
2591 if (flags & FLAGS_LEFTADJUST)
2594 self->OutStream(self, CHAR_ADJUST);
2596 if (flags & FLAGS_QUOTE)
2597 self->OutStream(self, CHAR_QUOTE);
2599#endif /* TRIO_WIDECHAR */
2613/* FIXME: handle all instances of constant long-double number (L)
2614 * and *l() math functions.
2618TRIO_ARGS6((self, number, flags, width, precision, base),
2620 trio_long_double_t number,
2626 trio_long_double_t integerNumber;
2627 trio_long_double_t fractionNumber;
2628 trio_long_double_t workNumber;
2633 int integerThreshold;
2634 int fractionThreshold;
2637 unsigned int uExponent = 0;
2639 trio_long_double_t dblBase;
2640 trio_long_double_t dblIntegerBase;
2641 trio_long_double_t dblFractionBase;
2642 trio_long_double_t integerAdjust;
2643 trio_long_double_t fractionAdjust;
2644 BOOLEAN_T isNegative;
2645 BOOLEAN_T isExponentNegative = FALSE;
2646 BOOLEAN_T requireTwoDigitExponent;
2648 TRIO_CONST char *digits;
2649 char *groupingPointer;
2652 BOOLEAN_T hasOnlyZeroes;
2654 register int trailingZeroes;
2655 BOOLEAN_T keepTrailingZeroes;
2656 BOOLEAN_T keepDecimalPoint;
2657 trio_long_double_t epsilon;
2659 assert(VALID(self));
2660 assert(VALID(self->OutStream));
2661 assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
2663 /* Determine sign and look for special quantities */
2664 switch (trio_fpclassify_and_signbit(number, &isNegative))
2667 TrioWriteString(self,
2668 (flags & FLAGS_UPPER)
2671 flags, width, precision);
2674 case TRIO_FP_INFINITE:
2677 /* Negative infinity */
2678 TrioWriteString(self,
2679 (flags & FLAGS_UPPER)
2680 ? "-
" INFINITE_UPPER
2681 : "-
" INFINITE_LOWER,
2682 flags, width, precision);
2687 /* Positive infinity */
2688 TrioWriteString(self,
2689 (flags & FLAGS_UPPER)
2692 flags, width, precision);
2701 /* Normal numbers */
2702 if (flags & FLAGS_LONGDOUBLE)
2704 baseDigits = (base == 10)
2706 : (int)floor(LDBL_MANT_DIG / TrioLogarithmBase(base));
2707 epsilon = LDBL_EPSILON;
2709 else if (flags & FLAGS_SHORT)
2711 baseDigits = (base == BASE_DECIMAL)
2713 : (int)floor(FLT_MANT_DIG / TrioLogarithmBase(base));
2714 epsilon = FLT_EPSILON;
2718 baseDigits = (base == BASE_DECIMAL)
2720 : (int)floor(DBL_MANT_DIG / TrioLogarithmBase(base));
2721 epsilon = DBL_EPSILON;
2724 digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
2725 isHex = (base == BASE_HEX);
2726 if (base == NO_BASE)
2727 base = BASE_DECIMAL;
2728 dblBase = (trio_long_double_t)base;
2729 keepTrailingZeroes = !( (flags & FLAGS_ROUNDING) ||
2730 ( (flags & FLAGS_FLOAT_G) &&
2731 !(flags & FLAGS_ALTERNATIVE) ) );
2733 if (flags & FLAGS_ROUNDING)
2734 precision = baseDigits;
2736 if (precision == NO_PRECISION)
2740 keepTrailingZeroes = FALSE;
2741 precision = FLT_MANT_DIG;
2745 precision = FLT_DIG;
2753 flags |= FLAGS_FLOAT_E;
2755 if (flags & FLAGS_FLOAT_G)
2760 if ((number < 1.0E-4) || (number > powl(base,
2761 (trio_long_double_t)precision)))
2763 /* Use scientific notation */
2764 flags |= FLAGS_FLOAT_E;
2766 else if (number < 1.0)
2769 * Use normal notation. If the integer part of the number is
2770 * zero, then adjust the precision to include leading fractional
2773 workNumber = TrioLogarithm(number, base);
2774 workNumber = TRIO_FABS(workNumber);
2775 if (workNumber - floorl(workNumber) < 0.001)
2777 zeroes = (int)floorl(workNumber);
2781 if (flags & FLAGS_FLOAT_E)
2783 /* Scale the number */
2784 workNumber = TrioLogarithm(number, base);
2785 if (trio_isinf(workNumber) == -1)
2789 if (flags & FLAGS_FLOAT_G)
2790 flags &= ~FLAGS_FLOAT_E;
2794 exponent = (int)floorl(workNumber);
2795 number /= powl(dblBase, (trio_long_double_t)exponent);
2796 isExponentNegative = (exponent < 0);
2797 uExponent = (isExponentNegative) ? -exponent : exponent;
2799 uExponent *= 4; /* log16(2) */
2800 /* No thousand separators */
2801 flags &= ~FLAGS_QUOTE;
2805 integerNumber = floorl(number);
2806 fractionNumber = number - integerNumber;
2811 * Precision is number of significant digits for FLOAT_G
2812 * and number of fractional digits for others.
2814 integerDigits = (integerNumber > epsilon)
2815 ? 1 + (int)TrioLogarithm(integerNumber, base)
2817 fractionDigits = ((flags & FLAGS_FLOAT_G) && (zeroes == 0))
2818 ? precision - integerDigits
2819 : zeroes + precision;
2821 dblFractionBase = TrioPower(base, fractionDigits);
2823 workNumber = number + 0.5 / dblFractionBase;
2824 if (floorl(number) != floorl(workNumber))
2826 if (flags & FLAGS_FLOAT_E)
2828 /* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
2830 isExponentNegative = (exponent < 0);
2831 uExponent = (isExponentNegative) ? -exponent : exponent;
2833 uExponent *= 4; /* log16(2) */
2834 workNumber = (number + 0.5 / dblFractionBase) / dblBase;
2835 integerNumber = floorl(workNumber);
2836 fractionNumber = workNumber - integerNumber;
2840 /* Adjust if number was rounded up one digit (ie. 99 to 100) */
2841 integerNumber = floorl(number + 0.5);
2842 fractionNumber = 0.0;
2843 integerDigits = (integerNumber > epsilon)
2844 ? 1 + (int)TrioLogarithm(integerNumber, base)
2849 /* Estimate accuracy */
2850 integerAdjust = fractionAdjust = 0.5;
2851 if (flags & FLAGS_ROUNDING)
2853 if (integerDigits > baseDigits)
2855 integerThreshold = baseDigits;
2857 dblFractionBase = 1.0;
2858 fractionThreshold = 0;
2859 precision = 0; /* Disable decimal-point */
2860 integerAdjust = TrioPower(base, integerDigits - integerThreshold - 1);
2861 fractionAdjust = 0.0;
2865 integerThreshold = integerDigits;
2866 fractionThreshold = fractionDigits - integerThreshold;
2867 fractionAdjust = 1.0;
2872 integerThreshold = INT_MAX;
2873 fractionThreshold = INT_MAX;
2877 * Calculate expected width.
2878 * sign + integer part + thousands separators + decimal point
2879 * + fraction + exponent
2881 fractionAdjust /= dblFractionBase;
2882 hasOnlyZeroes = (floorl((fractionNumber + fractionAdjust) * dblFractionBase) < epsilon);
2883 keepDecimalPoint = ( (flags & FLAGS_ALTERNATIVE) ||
2884 !((precision == 0) ||
2885 (!keepTrailingZeroes && hasOnlyZeroes)) );
2886 if (flags & FLAGS_FLOAT_E)
2888 exponentDigits = (uExponent == 0)
2890 : (int)ceil(TrioLogarithm((double)(uExponent + 1),
2891 (isHex) ? 10.0 : base));
2895 requireTwoDigitExponent = ((base == BASE_DECIMAL) && (exponentDigits == 1));
2897 expectedWidth = integerDigits + fractionDigits
2899 ? internalDecimalPointLength
2901 + ((flags & FLAGS_QUOTE)
2902 ? TrioCalcThousandSeparatorLength(integerDigits)
2904 if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
2905 expectedWidth += sizeof("-
") - 1;
2906 if (exponentDigits > 0)
2907 expectedWidth += exponentDigits +
2908 ((requireTwoDigitExponent ? sizeof("E+0
") : sizeof("E+
")) - 1);
2910 expectedWidth += sizeof("0
X") - 1;
2912 /* Output prefixing */
2913 if (flags & FLAGS_NILPADDING)
2915 /* Leading zeros must be after sign */
2917 self->OutStream(self, '-');
2918 else if (flags & FLAGS_SHOWSIGN)
2919 self->OutStream(self, '+');
2920 else if (flags & FLAGS_SPACE)
2921 self->OutStream(self, ' ');
2924 self->OutStream(self, '0');
2925 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2927 if (!(flags & FLAGS_LEFTADJUST))
2929 for (i = expectedWidth; i < width; i++)
2931 self->OutStream(self, '0');
2937 /* Leading spaces must be before sign */
2938 if (!(flags & FLAGS_LEFTADJUST))
2940 for (i = expectedWidth; i < width; i++)
2942 self->OutStream(self, CHAR_ADJUST);
2946 self->OutStream(self, '-');
2947 else if (flags & FLAGS_SHOWSIGN)
2948 self->OutStream(self, '+');
2949 else if (flags & FLAGS_SPACE)
2950 self->OutStream(self, ' ');
2953 self->OutStream(self, '0');
2954 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2958 /* Output the integer part and thousand separators */
2959 dblIntegerBase = 1.0 / TrioPower(base, integerDigits - 1);
2960 for (i = 0; i < integerDigits; i++)
2962 workNumber = floorl(((integerNumber + integerAdjust) * dblIntegerBase));
2963 if (i > integerThreshold)
2965 /* Beyond accuracy */
2966 self->OutStream(self, digits[0]);
2970 self->OutStream(self, digits[(int)fmodl(workNumber, dblBase)]);
2972 dblIntegerBase *= dblBase;
2974 if (((flags & (FLAGS_FLOAT_E | FLAGS_QUOTE)) == FLAGS_QUOTE)
2975 && TrioFollowedBySeparator(integerDigits - i))
2977 for (groupingPointer = internalThousandSeparator;
2978 *groupingPointer != NIL;
2981 self->OutStream(self, *groupingPointer);
2986 /* Insert decimal point and build the fraction part */
2989 if (keepDecimalPoint)
2991 if (internalDecimalPoint)
2993 self->OutStream(self, internalDecimalPoint);
2997 for (i = 0; i < internalDecimalPointLength; i++)
2999 self->OutStream(self, internalDecimalPointString[i]);
3004 for (i = 0; i < fractionDigits; i++)
3006 if ((integerDigits > integerThreshold) || (i > fractionThreshold))
3008 /* Beyond accuracy */
3013 fractionNumber *= dblBase;
3014 fractionAdjust *= dblBase;
3015 workNumber = floorl(fractionNumber + fractionAdjust);
3016 fractionNumber -= workNumber;
3017 index = (int)fmodl(workNumber, dblBase);
3024 while (trailingZeroes > 0)
3026 /* Not trailing zeroes after all */
3027 self->OutStream(self, digits[0]);
3030 self->OutStream(self, digits[index]);
3035 if (keepTrailingZeroes)
3037 while (trailingZeroes > 0)
3039 self->OutStream(self, digits[0]);
3044 /* Output exponent */
3045 if (exponentDigits > 0)
3047 self->OutStream(self,
3049 ? ((flags & FLAGS_UPPER) ? 'P' : 'p')
3050 : ((flags & FLAGS_UPPER) ? 'E' : 'e'));
3051 self->OutStream(self, (isExponentNegative) ? '-' : '+');
3053 /* The exponent must contain at least two digits */
3054 if (requireTwoDigitExponent)
3055 self->OutStream(self, '0');
3059 exponentBase = (int)TrioPower(base, exponentDigits - 1);
3060 for (i = 0; i < exponentDigits; i++)
3062 self->OutStream(self, digits[(uExponent / exponentBase) % base]);
3063 exponentBase /= base;
3066 /* Output trailing spaces */
3067 if (flags & FLAGS_LEFTADJUST)
3069 for (i = expectedWidth; i < width; i++)
3071 self->OutStream(self, CHAR_ADJUST);
3084TRIO_ARGS3((data, format, parameters),
3086 TRIO_CONST char *format,
3087 trio_parameter_t *parameters)
3089#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3093 TRIO_CONST char *string;
3094 trio_pointer_t pointer;
3103#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3104 (void)mblen(NULL, 0);
3107 while (format[index])
3109#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3110 if (! isascii(format[index]))
3112 charlen = mblen(&format[index], MB_LEN_MAX);
3114 * Only valid multibyte characters are handled here. Invalid
3115 * multibyte characters (charlen == -1) are handled as normal
3120 while (charlen-- > 0)
3122 data->OutStream(data, format[index++]);
3124 continue; /* while characters left in formatting string */
3127#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
3128 if (CHAR_IDENTIFIER == format[index])
3130 if (CHAR_IDENTIFIER == format[index + 1])
3132 data->OutStream(data, CHAR_IDENTIFIER);
3137 /* Skip the parameter entries */
3138 while (parameters[i].type == FORMAT_PARAMETER)
3141 flags = parameters[i].flags;
3144 width = parameters[i].width;
3145 if (flags & FLAGS_WIDTH_PARAMETER)
3147 /* Get width from parameter list */
3148 width = (int)parameters[width].data.number.as_signed;
3152 * A negative width is the same as the - flag and
3155 flags |= FLAGS_LEFTADJUST;
3156 flags &= ~FLAGS_NILPADDING;
3161 /* Find precision */
3162 if (flags & FLAGS_PRECISION)
3164 precision = parameters[i].precision;
3165 if (flags & FLAGS_PRECISION_PARAMETER)
3167 /* Get precision from parameter list */
3168 precision = (int)parameters[precision].data.number.as_signed;
3172 * A negative precision is the same as no
3175 precision = NO_PRECISION;
3181 precision = NO_PRECISION;
3185 base = parameters[i].base;
3186 if (flags & FLAGS_BASE_PARAMETER)
3188 /* Get base from parameter list */
3189 base = (int)parameters[base].data.number.as_signed;
3192 switch (parameters[i].type)
3195 if (flags & FLAGS_QUOTE)
3196 data->OutStream(data, CHAR_QUOTE);
3197 if (! (flags & FLAGS_LEFTADJUST))
3200 data->OutStream(data, CHAR_ADJUST);
3203 if (flags & FLAGS_WIDECHAR)
3205 TrioWriteWideStringCharacter(data,
3206 (trio_wchar_t)parameters[i].data.number.as_signed,
3213 TrioWriteStringCharacter(data,
3214 (int)parameters[i].data.number.as_signed,
3218 if (flags & FLAGS_LEFTADJUST)
3221 data->OutStream(data, CHAR_ADJUST);
3223 if (flags & FLAGS_QUOTE)
3224 data->OutStream(data, CHAR_QUOTE);
3226 break; /* FORMAT_CHAR */
3229 TrioWriteNumber(data,
3230 parameters[i].data.number.as_unsigned,
3236 break; /* FORMAT_INT */
3239 TrioWriteDouble(data,
3240 parameters[i].data.longdoubleNumber,
3245 break; /* FORMAT_DOUBLE */
3249 if (flags & FLAGS_WIDECHAR)
3251 TrioWriteWideString(data,
3252 parameters[i].data.wstring,
3260 TrioWriteString(data,
3261 parameters[i].data.string,
3266 break; /* FORMAT_STRING */
3268 case FORMAT_POINTER:
3270 trio_reference_t reference;
3272 reference.data = data;
3273 reference.parameter = ¶meters[i];
3274 trio_print_pointer(&reference, parameters[i].data.pointer);
3276 break; /* FORMAT_POINTER */
3279 pointer = parameters[i].data.pointer;
3280 if (NULL != pointer)
3283 * C99 paragraph 7.19.6.1.8 says "the
number of
3284 * characters written to the output
stream so
far by
3285 *
this call
", which is data->committed
3287#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
3288 if (flags & FLAGS_SIZE_T)
3289 *(size_t *)pointer = (size_t)data->committed;
3292#if defined(QUALIFIER_PTRDIFF_T)
3293 if (flags & FLAGS_PTRDIFF_T)
3294 *(ptrdiff_t *)pointer = (ptrdiff_t)data->committed;
3297#if defined(QUALIFIER_INTMAX_T)
3298 if (flags & FLAGS_INTMAX_T)
3299 *(trio_intmax_t *)pointer = (trio_intmax_t)data->committed;
3302 if (flags & FLAGS_QUAD)
3304 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)data->committed;
3306 else if (flags & FLAGS_LONG)
3308 *(long int *)pointer = (long int)data->committed;
3310 else if (flags & FLAGS_SHORT)
3312 *(short int *)pointer = (short int)data->committed;
3316 *(int *)pointer = (int)data->committed;
3319 break; /* FORMAT_COUNT */
3321 case FORMAT_PARAMETER:
3322 break; /* FORMAT_PARAMETER */
3324#if defined(FORMAT_ERRNO)
3326 string = trio_error(parameters[i].data.errorNumber);
3329 TrioWriteString(data,
3337 data->OutStream(data, '#');
3338 TrioWriteNumber(data,
3339 (trio_uintmax_t)parameters[i].data.errorNumber,
3345 break; /* FORMAT_ERRNO */
3346#endif /* defined(FORMAT_ERRNO) */
3348#if defined(FORMAT_USER_DEFINED)
3349 case FORMAT_USER_DEFINED:
3351 trio_reference_t reference;
3352 trio_userdef_t *def = NULL;
3354 if (parameters[i].user_name[0] == NIL)
3358 (parameters[i - 1].type == FORMAT_PARAMETER))
3359 def = (trio_userdef_t *)parameters[i - 1].data.pointer;
3363 /* Look up namespace */
3364 def = TrioFindNamespace(parameters[i].user_name, NULL);
3367 reference.data = data;
3368 reference.parameter = ¶meters[i];
3369 def->callback(&reference);
3373#endif /* defined(FORMAT_USER_DEFINED) */
3377 } /* switch parameter type */
3379 /* Prepare for next */
3380 index = parameters[i].indexAfterSpecifier;
3384 else /* not identifier */
3386 data->OutStream(data, format[index++]);
3389 return data->processed;
3397TRIO_ARGS4((reference, format, arglist, argarray),
3398 trio_reference_t *reference,
3399 TRIO_CONST char *format,
3400 TRIO_VA_LIST_PTR arglist,
3401 trio_pointer_t *argarray)
3404 trio_parameter_t parameters[MAX_PARAMETERS];
3406 status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
3410 status = TrioFormatProcess(reference->data, format, parameters);
3411 if (reference->data->error != 0)
3413 status = reference->data->error;
3423TRIO_ARGS6((destination, destinationSize, OutStream, format, arglist, argarray),
3424 trio_pointer_t destination,
3425 size_t destinationSize,
3426 void (*OutStream) TRIO_PROTO((trio_class_t *, int)),
3427 TRIO_CONST char *format,
3428 TRIO_VA_LIST_PTR arglist,
3429 trio_pointer_t *argarray)
3433 trio_parameter_t parameters[MAX_PARAMETERS];
3435 assert(VALID(OutStream));
3436 assert(VALID(format));
3438 memset(&data, 0, sizeof(data));
3439 data.OutStream = OutStream;
3440 data.location = destination;
3441 data.max = destinationSize;
3444#if defined(USE_LOCALE)
3445 if (NULL == internalLocaleValues)
3451 status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
3455 status = TrioFormatProcess(&data, format, parameters);
3456 if (data.error != 0)
3458 status = data.error;
3468TRIO_ARGS2((self, output),
3474 assert(VALID(self));
3475 assert(VALID(self->location));
3477 file = (FILE *)self->location;
3479 if (fputc(output, file) == EOF)
3481 self->error = TRIO_ERROR_RETURN(TRIO_EOF, 0);
3493TrioOutStreamFileDescriptor
3494TRIO_ARGS2((self, output),
3501 assert(VALID(self));
3503 fd = *((int *)self->location);
3506 if (write(fd, &ch, sizeof(char)) == -1)
3508 self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
3521TRIO_ARGS2((self, output),
3526 trio_custom_t *data;
3528 assert(VALID(self));
3529 assert(VALID(self->location));
3531 data = (trio_custom_t *)self->location;
3532 if (data->stream.out)
3534 status = (data->stream.out)(data->closure, output);
3541 if (self->error == 0)
3543 self->error = TRIO_ERROR_RETURN(TRIO_ECUSTOM, -status);
3555TRIO_ARGS2((self, output),
3561 assert(VALID(self));
3562 assert(VALID(self->location));
3564 buffer = (char **)self->location;
3565 **buffer = (char)output;
3575TrioOutStreamStringMax
3576TRIO_ARGS2((self, output),
3582 assert(VALID(self));
3583 assert(VALID(self->location));
3585 buffer = (char **)self->location;
3587 if (self->processed < self->max)
3589 **buffer = (char)output;
3600TrioOutStreamStringDynamic
3601TRIO_ARGS2((self, output),
3605 assert(VALID(self));
3606 assert(VALID(self->location));
3608 if (self->error == 0)
3610 trio_xstring_append_char((trio_string_t *)self->location,
3614 /* The processed variable must always be increased */
3624#if defined(TRIO_DOCUMENTATION)
3625# include "doc/doc_printf.h
"
3644TRIO_VARGS2((format, va_alist),
3645 TRIO_CONST char *format,
3651 assert(VALID(format));
3653 TRIO_VA_START(args, format);
3654 status = TrioFormat(stdout, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
3668TRIO_ARGS2((format, args),
3669 TRIO_CONST char *format,
3672 assert(VALID(format));
3674 return TrioFormat(stdout, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
3686TRIO_ARGS2((format, args),
3687 TRIO_CONST char *format,
3688 trio_pointer_t * args)
3690 assert(VALID(format));
3692 return TrioFormat(stdout, 0, TrioOutStreamFile, format, NULL, args);
3709TRIO_VARGS3((file, format, va_alist),
3711 TRIO_CONST char *format,
3717 assert(VALID(file));
3718 assert(VALID(format));
3720 TRIO_VA_START(args, format);
3721 status = TrioFormat(file, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
3736TRIO_ARGS3((file, format, args),
3738 TRIO_CONST char *format,
3741 assert(VALID(file));
3742 assert(VALID(format));
3744 return TrioFormat(file, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
3757TRIO_ARGS3((file, format, args),
3759 TRIO_CONST char *format,
3760 trio_pointer_t * args)
3762 assert(VALID(file));
3763 assert(VALID(format));
3765 return TrioFormat(file, 0, TrioOutStreamFile, format, NULL, args);
3782TRIO_VARGS3((fd, format, va_alist),
3784 TRIO_CONST char *format,
3790 assert(VALID(format));
3792 TRIO_VA_START(args, format);
3793 status = TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, TRIO_VA_LIST_ADDR(args), NULL);
3808TRIO_ARGS3((fd, format, args),
3810 TRIO_CONST char *format,
3813 assert(VALID(format));
3815 return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, TRIO_VA_LIST_ADDR(args), NULL);
3828TRIO_ARGS3((fd, format, args),
3830 TRIO_CONST char *format,
3831 trio_pointer_t *args)
3833 assert(VALID(format));
3835 return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, NULL, args);
3843TRIO_VARGS4((stream, closure, format, va_alist),
3844 trio_outstream_t stream,
3845 trio_pointer_t closure,
3846 TRIO_CONST char *format,
3853 assert(VALID(stream));
3854 assert(VALID(format));
3856 TRIO_VA_START(args, format);
3857 data.stream.out = stream;
3858 data.closure = closure;
3859 status = TrioFormat(&data, 0, TrioOutStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
3866TRIO_ARGS4((stream, closure, format, args),
3867 trio_outstream_t stream,
3868 trio_pointer_t closure,
3869 TRIO_CONST char *format,
3874 assert(VALID(stream));
3875 assert(VALID(format));
3877 data.stream.out = stream;
3878 data.closure = closure;
3879 return TrioFormat(&data, 0, TrioOutStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
3884TRIO_ARGS4((stream, closure, format, args),
3885 trio_outstream_t stream,
3886 trio_pointer_t closure,
3887 TRIO_CONST char *format,
3892 assert(VALID(stream));
3893 assert(VALID(format));
3895 data.stream.out = stream;
3896 data.closure = closure;
3897 return TrioFormat(&data, 0, TrioOutStreamCustom, format, NULL, args);
3914TRIO_VARGS3((buffer, format, va_alist),
3916 TRIO_CONST char *format,
3922 assert(VALID(buffer));
3923 assert(VALID(format));
3925 TRIO_VA_START(args, format);
3926 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, TRIO_VA_LIST_ADDR(args), NULL);
3927 *buffer = NIL; /* Terminate with NIL character */
3942TRIO_ARGS3((buffer, format, args),
3944 TRIO_CONST char *format,
3949 assert(VALID(buffer));
3950 assert(VALID(format));
3952 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, TRIO_VA_LIST_ADDR(args), NULL);
3967TRIO_ARGS3((buffer, format, args),
3969 TRIO_CONST char *format,
3970 trio_pointer_t *args)
3974 assert(VALID(buffer));
3975 assert(VALID(format));
3977 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, NULL, args);
3997TRIO_VARGS4((buffer, max, format, va_alist),
4000 TRIO_CONST char *format,
4006 assert(VALID(buffer));
4007 assert(VALID(format));
4009 TRIO_VA_START(args, format);
4010 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
4011 TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
4029TRIO_ARGS4((buffer, max, format, args),
4032 TRIO_CONST char *format,
4037 assert(VALID(buffer));
4038 assert(VALID(format));
4040 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
4041 TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
4058TRIO_ARGS4((buffer, max, format, args),
4061 TRIO_CONST char *format,
4062 trio_pointer_t *args)
4066 assert(VALID(buffer));
4067 assert(VALID(format));
4069 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
4070 TrioOutStreamStringMax, format, NULL, args);
4083TRIO_VARGS4((buffer, max, format, va_alist),
4086 TRIO_CONST char *format,
4093 TRIO_VA_START(args, format);
4095 assert(VALID(buffer));
4096 assert(VALID(format));
4098 buf_len = trio_length(buffer);
4099 buffer = &buffer[buf_len];
4101 status = TrioFormat(&buffer, max - 1 - buf_len,
4102 TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
4110TRIO_ARGS4((buffer, max, format, args),
4113 TRIO_CONST char *format,
4119 assert(VALID(buffer));
4120 assert(VALID(format));
4122 buf_len = trio_length(buffer);
4123 buffer = &buffer[buf_len];
4124 status = TrioFormat(&buffer, max - 1 - buf_len,
4125 TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
4137TRIO_VARGS2((format, va_alist),
4138 TRIO_CONST char *format,
4142 trio_string_t *info;
4143 char *result = NULL;
4145 assert(VALID(format));
4147 info = trio_xstring_duplicate("
");
4150 TRIO_VA_START(args, format);
4151 (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
4152 format, TRIO_VA_LIST_ADDR(args), NULL);
4155 trio_string_terminate(info);
4156 result = trio_string_extract(info);
4157 trio_string_destroy(info);
4165TRIO_ARGS2((format, args),
4166 TRIO_CONST char *format,
4169 trio_string_t *info;
4170 char *result = NULL;
4172 assert(VALID(format));
4174 info = trio_xstring_duplicate("");
4177 (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
4178 format, TRIO_VA_LIST_ADDR(args), NULL);
4179 trio_string_terminate(info);
4180 result = trio_string_extract(info);
4181 trio_string_destroy(info);
4188TRIO_VARGS3((result, format, va_alist),
4190 TRIO_CONST char *format,
4195 trio_string_t *info;
4197 assert(VALID(format));
4201 info = trio_xstring_duplicate("");
4204 status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
4208 TRIO_VA_START(args, format);
4209 status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
4210 format, TRIO_VA_LIST_ADDR(args), NULL);
4214 trio_string_terminate(info);
4215 *result = trio_string_extract(info);
4217 trio_string_destroy(info);
4224TRIO_ARGS3((result, format, args),
4226 TRIO_CONST char *format,
4230 trio_string_t *info;
4232 assert(VALID(format));
4236 info = trio_xstring_duplicate("");
4239 status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
4243 status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
4244 format, TRIO_VA_LIST_ADDR(args), NULL);
4247 trio_string_terminate(info);
4248 *result = trio_string_extract(info);
4250 trio_string_destroy(info);
4263#if defined(TRIO_DOCUMENTATION)
4264# include "doc/doc_register.h
"
4284TRIO_PUBLIC trio_pointer_t
4286TRIO_ARGS2((callback, name),
4287 trio_callback_t callback,
4288 TRIO_CONST char *name)
4290 trio_userdef_t *def;
4291 trio_userdef_t *prev = NULL;
4293 if (callback == NULL)
4298 /* Handle built-in namespaces */
4301 if (trio_equal(name, ":enter
"))
4303 internalEnterCriticalRegion = callback;
4305 else if (trio_equal(name, ":
leave"))
4307 internalLeaveCriticalRegion = callback;
4312 /* Bail out if namespace is too long */
4313 if (trio_length(name) >= MAX_USER_NAME)
4316 /* Bail out if namespace already is registered */
4317 def = TrioFindNamespace(name, &prev);
4322 def = (trio_userdef_t *)TRIO_MALLOC(sizeof(trio_userdef_t));
4325 if (internalEnterCriticalRegion)
4326 (void)internalEnterCriticalRegion(NULL);
4330 /* Link into internal list */
4332 internalUserDef = def;
4337 def->callback = callback;
4338 def->name = (name == NULL)
4340 : trio_duplicate(name);
4343 if (internalLeaveCriticalRegion)
4344 (void)internalLeaveCriticalRegion(NULL);
4346 return (trio_pointer_t)def;
4357 trio_pointer_t handle)
4359 trio_userdef_t *self = (trio_userdef_t *)handle;
4360 trio_userdef_t *def;
4361 trio_userdef_t *prev = NULL;
4363 assert(VALID(self));
4367 def = TrioFindNamespace(self->name, &prev);
4370 if (internalEnterCriticalRegion)
4371 (void)internalEnterCriticalRegion(NULL);
4374 internalUserDef = NULL;
4376 prev->next = def->next;
4378 if (internalLeaveCriticalRegion)
4379 (void)internalLeaveCriticalRegion(NULL);
4381 trio_destroy(self->name);
4394#if defined(FORMAT_USER_DEFINED)
4395 assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
4398 return (((trio_reference_t *)ref)->parameter->user_data);
4409#if defined(FORMAT_USER_DEFINED)
4410 assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
4413 return ((trio_reference_t *)ref)->parameter->data.pointer;
4424 return ((trio_reference_t *)ref)->parameter->width;
4429TRIO_ARGS2((ref, width),
4433 ((trio_reference_t *)ref)->parameter->width = width;
4444 return (((trio_reference_t *)ref)->parameter->precision);
4449TRIO_ARGS2((ref, precision),
4453 ((trio_reference_t *)ref)->parameter->precision = precision;
4464 return (((trio_reference_t *)ref)->parameter->base);
4469TRIO_ARGS2((ref, base),
4473 ((trio_reference_t *)ref)->parameter->base = base;
4484 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONG)
4491TRIO_ARGS2((ref, is_long),
4496 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONG;
4498 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONG;
4509 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUAD)
4516TRIO_ARGS2((ref, is_longlong),
4521 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUAD;
4523 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUAD;
4534 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONGDOUBLE)
4541TRIO_ARGS2((ref, is_longdouble),
4546 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONGDOUBLE;
4548 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONGDOUBLE;
4559 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORT)
4566TRIO_ARGS2((ref, is_short),
4571 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORT;
4573 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORT;
4584 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORTSHORT)
4591TRIO_ARGS2((ref, is_shortshort),
4596 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORTSHORT;
4598 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORTSHORT;
4609 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_ALTERNATIVE)
4616TRIO_ARGS2((ref, is_alternative),
4621 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_ALTERNATIVE;
4623 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_ALTERNATIVE;
4634 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LEFTADJUST)
4641TRIO_ARGS2((ref, is_leftaligned),
4646 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LEFTADJUST;
4648 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LEFTADJUST;
4659 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SPACE)
4666TRIO_ARGS2((ref, is_space),
4671 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SPACE;
4673 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SPACE;
4684 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHOWSIGN)
4691TRIO_ARGS2((ref, is_sign),
4696 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHOWSIGN;
4698 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHOWSIGN;
4709 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_NILPADDING)
4716TRIO_ARGS2((ref, is_padding),
4721 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_NILPADDING;
4723 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_NILPADDING;
4734 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUOTE)
4741TRIO_ARGS2((ref, is_quote),
4746 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUOTE;
4748 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUOTE;
4759 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_UPPER)
4766TRIO_ARGS2((ref, is_upper),
4771 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_UPPER;
4773 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_UPPER;
4785 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_INTMAX_T)
4792TRIO_ARGS2((ref, is_largest),
4797 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_INTMAX_T;
4799 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_INTMAX_T;
4811 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_PTRDIFF_T)
4818TRIO_ARGS2((ref, is_ptrdiff),
4823 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_PTRDIFF_T;
4825 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_PTRDIFF_T;
4837 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SIZE_T)
4844TRIO_ARGS2((ref, is_size),
4849 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SIZE_T;
4851 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SIZE_T;
4860TRIO_ARGS2((ref, number),
4864 trio_reference_t *self = (trio_reference_t *)ref;
4866 TrioWriteNumber(self->data,
4867 (trio_uintmax_t)number,
4868 self->parameter->flags,
4869 self->parameter->width,
4870 self->parameter->precision,
4871 self->parameter->base);
4879TRIO_ARGS2((ref, number),
4881 unsigned int number)
4883 trio_reference_t *self = (trio_reference_t *)ref;
4885 TrioWriteNumber(self->data,
4886 (trio_uintmax_t)number,
4887 self->parameter->flags | FLAGS_UNSIGNED,
4888 self->parameter->width,
4889 self->parameter->precision,
4890 self->parameter->base);
4898TRIO_ARGS2((ref, number),
4902 trio_reference_t *self = (trio_reference_t *)ref;
4904 TrioWriteDouble(self->data,
4906 self->parameter->flags,
4907 self->parameter->width,
4908 self->parameter->precision,
4909 self->parameter->base);
4917TRIO_ARGS2((ref, string),
4921 trio_reference_t *self = (trio_reference_t *)ref;
4923 TrioWriteString(self->data,
4925 self->parameter->flags,
4926 self->parameter->width,
4927 self->parameter->precision);
4935TRIO_VARGS3((ref, format, va_alist),
4937 TRIO_CONST char *format,
4943 assert(VALID(format));
4945 TRIO_VA_START(arglist, format);
4946 status = TrioFormatRef((trio_reference_t *)ref, format, TRIO_VA_LIST_ADDR(arglist), NULL);
4947 TRIO_VA_END(arglist);
4956TRIO_ARGS3((ref, format, arglist),
4958 TRIO_CONST char *format,
4961 assert(VALID(format));
4963 return TrioFormatRef((trio_reference_t *)ref, format, TRIO_VA_LIST_ADDR(arglist), NULL);
4971TRIO_ARGS3((ref, format, argarray),
4973 TRIO_CONST char *format,
4974 trio_pointer_t *argarray)
4976 assert(VALID(format));
4978 return TrioFormatRef((trio_reference_t *)ref, format, NULL, argarray);
4981#endif /* TRIO_EXTENSION */
4988TRIO_ARGS2((ref, pointer),
4990 trio_pointer_t pointer)
4992 trio_reference_t *self = (trio_reference_t *)ref;
4994 trio_uintmax_t number;
4996 if (NULL == pointer)
4998 TRIO_CONST char *string = internalNullString;
5000 self->data->OutStream(self->data, *string++);
5005 * The subtraction of the null pointer is a workaround
5006 * to avoid a compiler warning. The performance overhead
5007 * is negligible (and likely to be removed by an
5008 * optimizing compiler). The (char *) casting is done
5009 * to please ANSI C++.
5011 number = (trio_uintmax_t)((char *)pointer - (char *)0);
5012 /* Shrink to size of pointer */
5013 number &= (trio_uintmax_t)-1;
5014 flags = self->parameter->flags;
5015 flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE |
5017 TrioWriteNumber(self->data,
5042trio_locale_set_decimal_point
5043TRIO_ARGS1((decimalPoint),
5046#if defined(USE_LOCALE)
5047 if (NULL == internalLocaleValues)
5052 internalDecimalPointLength = trio_length(decimalPoint);
5053 if (internalDecimalPointLength == 1)
5055 internalDecimalPoint = *decimalPoint;
5059 internalDecimalPoint = NIL;
5060 trio_copy_max(internalDecimalPointString,
5061 sizeof(internalDecimalPointString),
5072trio_locale_set_thousand_separator
5073TRIO_ARGS1((thousandSeparator),
5074 char *thousandSeparator)
5076#if defined(USE_LOCALE)
5077 if (NULL == internalLocaleValues)
5082 trio_copy_max(internalThousandSeparator,
5083 sizeof(internalThousandSeparator),
5085 internalThousandSeparatorLength = trio_length(internalThousandSeparator);
5101trio_locale_set_grouping
5102TRIO_ARGS1((grouping),
5105#if defined(USE_LOCALE)
5106 if (NULL == internalLocaleValues)
5111 trio_copy_max(internalGrouping,
5112 sizeof(internalGrouping),
5136 self->InStream(self, &ch);
5146TrioGetCollation(TRIO_NOARGS)
5154 /* This is computationally expensive */
5157 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5161 for (j = 0; j < MAX_CHARACTER_CLASS; j++)
5163 second[0] = (char)j;
5164 if (trio_equal_locale(first, second))
5165 internalCollationArray[i][k++] = (char)j;
5167 internalCollationArray[i][k] = NIL;
5179TrioGetCharacterClass
5180TRIO_ARGS4((format, indexPointer, flagsPointer, characterclass),
5181 TRIO_CONST char *format,
5183 trio_flags_t *flagsPointer,
5184 int *characterclass)
5186 int index = *indexPointer;
5192 *flagsPointer &= ~FLAGS_EXCLUDE;
5194 if (format[index] == QUALIFIER_CIRCUMFLEX)
5196 *flagsPointer |= FLAGS_EXCLUDE;
5200 * If the ungroup character is at the beginning of the scanlist,
5201 * it will be part of the class, and a second ungroup character
5202 * must follow to end the group.
5204 if (format[index] == SPECIFIER_UNGROUP)
5206 characterclass[(int)SPECIFIER_UNGROUP]++;
5210 * Minus is used to specify ranges. To include minus in the class,
5211 * it must be at the beginning of the list
5213 if (format[index] == QUALIFIER_MINUS)
5215 characterclass[(int)QUALIFIER_MINUS]++;
5218 /* Collect characters */
5219 for (ch = format[index];
5220 (ch != SPECIFIER_UNGROUP) && (ch != NIL);
5221 ch = format[++index])
5225 case QUALIFIER_MINUS: /* Scanlist ranges */
5228 * Both C99 and UNIX98 describes ranges as implementation-
5231 * We support the following behaviour (although this may
5232 * change as we become wiser)
5233 * - only increasing ranges, ie. [a-b] but not [b-a]
5234 * - transitive ranges, ie. [a-b-c] == [a-c]
5235 * - trailing minus, ie. [a-] is interpreted as an 'a'
5237 * - duplicates (although we can easily convert these
5240 range_begin = format[index - 1];
5241 range_end = format[++index];
5242 if (range_end == SPECIFIER_UNGROUP)
5244 /* Trailing minus is included */
5245 characterclass[(int)ch]++;
5249 if (range_end == NIL)
5250 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
5251 if (range_begin > range_end)
5252 return TRIO_ERROR_RETURN(TRIO_ERANGE, index);
5254 for (i = (int)range_begin; i <= (int)range_end; i++)
5255 characterclass[i]++;
5262 case SPECIFIER_GROUP:
5264 switch (format[index + 1])
5266 case QUALIFIER_DOT: /* Collating symbol */
5268 * FIXME: This will be easier to implement when multibyte
5269 * characters have been implemented. Until now, we ignore
5272 for (i = index + 2; ; i++)
5274 if (format[i] == NIL)
5275 /* Error in syntax */
5277 else if (format[i] == QUALIFIER_DOT)
5280 if (format[++i] != SPECIFIER_UNGROUP)
5286 case QUALIFIER_EQUAL: /* Equivalence class expressions */
5291 if (internalCollationUnconverted)
5293 /* Lazy evaluation of collation array */
5295 internalCollationUnconverted = FALSE;
5297 for (i = index + 2; ; i++)
5299 if (format[i] == NIL)
5300 /* Error in syntax */
5302 else if (format[i] == QUALIFIER_EQUAL)
5306 /* Mark any equivalent character */
5307 k = (unsigned int)format[i];
5308 for (j = 0; internalCollationArray[k][j] != NIL; j++)
5309 characterclass[(int)internalCollationArray[k][j]]++;
5312 if (format[++i] != SPECIFIER_UNGROUP)
5319 case QUALIFIER_COLON: /* Character class expressions */
5321 if (trio_equal_max(CLASS_ALNUM, sizeof(CLASS_ALNUM) - 1,
5324 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5326 characterclass[i]++;
5327 index += sizeof(CLASS_ALNUM) - 1;
5329 else if (trio_equal_max(CLASS_ALPHA, sizeof(CLASS_ALPHA) - 1,
5332 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5334 characterclass[i]++;
5335 index += sizeof(CLASS_ALPHA) - 1;
5337 else if (trio_equal_max(CLASS_CNTRL, sizeof(CLASS_CNTRL) - 1,
5340 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5342 characterclass[i]++;
5343 index += sizeof(CLASS_CNTRL) - 1;
5345 else if (trio_equal_max(CLASS_DIGIT, sizeof(CLASS_DIGIT) - 1,
5348 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5350 characterclass[i]++;
5351 index += sizeof(CLASS_DIGIT) - 1;
5353 else if (trio_equal_max(CLASS_GRAPH, sizeof(CLASS_GRAPH) - 1,
5356 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5358 characterclass[i]++;
5359 index += sizeof(CLASS_GRAPH) - 1;
5361 else if (trio_equal_max(CLASS_LOWER, sizeof(CLASS_LOWER) - 1,
5364 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5366 characterclass[i]++;
5367 index += sizeof(CLASS_LOWER) - 1;
5369 else if (trio_equal_max(CLASS_PRINT, sizeof(CLASS_PRINT) - 1,
5372 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5374 characterclass[i]++;
5375 index += sizeof(CLASS_PRINT) - 1;
5377 else if (trio_equal_max(CLASS_PUNCT, sizeof(CLASS_PUNCT) - 1,
5380 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5382 characterclass[i]++;
5383 index += sizeof(CLASS_PUNCT) - 1;
5385 else if (trio_equal_max(CLASS_SPACE, sizeof(CLASS_SPACE) - 1,
5388 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5390 characterclass[i]++;
5391 index += sizeof(CLASS_SPACE) - 1;
5393 else if (trio_equal_max(CLASS_UPPER, sizeof(CLASS_UPPER) - 1,
5396 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5398 characterclass[i]++;
5399 index += sizeof(CLASS_UPPER) - 1;
5401 else if (trio_equal_max(CLASS_XDIGIT, sizeof(CLASS_XDIGIT) - 1,
5404 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5406 characterclass[i]++;
5407 index += sizeof(CLASS_XDIGIT) - 1;
5411 characterclass[(int)ch]++;
5416 characterclass[(int)ch]++;
5421#endif /* TRIO_EXTENSION */
5424 characterclass[(int)ch]++;
5437TRIO_PRIVATE BOOLEAN_T
5439TRIO_ARGS5((self, target, flags, width, base),
5441 trio_uintmax_t *target,
5446 trio_uintmax_t number = 0;
5449 BOOLEAN_T isNegative = FALSE;
5450 BOOLEAN_T gotNumber = FALSE;
5453 assert(VALID(self));
5454 assert(VALID(self->InStream));
5455 assert((base >= MIN_BASE && base <= MAX_BASE) || (base == NO_BASE));
5457 if (internalDigitsUnconverted)
5459 /* Lazy evaluation of digits array */
5460 memset(internalDigitArray, -1, sizeof(internalDigitArray));
5461 for (j = 0; j < (int)sizeof(internalDigitsLower) - 1; j++)
5463 internalDigitArray[(int)internalDigitsLower[j]] = j;
5464 internalDigitArray[(int)internalDigitsUpper[j]] = j;
5466 internalDigitsUnconverted = FALSE;
5469 TrioSkipWhitespaces(self);
5471 if (!(flags & FLAGS_UNSIGNED))
5474 if (self->current == '+')
5476 self->InStream(self, NULL);
5478 else if (self->current == '-')
5480 self->InStream(self, NULL);
5485 count = self->processed;
5487 if (flags & FLAGS_ALTERNATIVE)
5495 if (self->current == '0')
5497 self->InStream(self, NULL);
5500 if ((base == BASE_HEX) &&
5501 (trio_to_upper(self->current) == 'X'))
5503 self->InStream(self, NULL);
5505 else if ((base == BASE_BINARY) &&
5506 (trio_to_upper(self->current) == 'B'))
5508 self->InStream(self, NULL);
5520 while (((width == NO_WIDTH) || (self->processed - count < width)) &&
5521 (! ((self->current == EOF) || isspace(self->current))))
5523 if (isascii(self->current))
5525 digit = internalDigitArray[self->current];
5526 /* Abort if digit is not allowed in the specified base */
5527 if ((digit == -1) || (digit >= base))
5530 else if (flags & FLAGS_QUOTE)
5532 /* Compare with thousands separator */
5533 for (j = 0; internalThousandSeparator[j] && self->current; j++)
5535 if (internalThousandSeparator[j] != self->current)
5538 self->InStream(self, NULL);
5540 if (internalThousandSeparator[j])
5541 break; /* Mismatch */
5543 continue; /* Match */
5550 gotNumber = TRUE; /* we need at least one digit */
5552 self->InStream(self, NULL);
5555 /* Was anything read at all? */
5560 *target = (isNegative) ? -((trio_intmax_t)number) : number;
5569TRIO_ARGS4((self, target, flags, width),
5577 trio_uintmax_t number;
5579 assert(VALID(self));
5580 assert(VALID(self->InStream));
5583 (self->current != EOF) && (i < width);
5586 ch = (char)self->current;
5587 self->InStream(self, NULL);
5588 if ((flags & FLAGS_ALTERNATIVE) && (ch == CHAR_BACKSLASH))
5590 switch (self->current)
5592 case '\\': ch = '\\'; break;
5593 case 'a': ch = '\007'; break;
5594 case 'b': ch = '\b'; break;
5595 case 'f': ch = '\f'; break;
5596 case 'n': ch = '\n'; break;
5597 case 'r': ch = '\r'; break;
5598 case 't': ch = '\t'; break;
5599 case 'v': ch = '\v'; break;
5601 if (isdigit(self->current))
5603 /* Read octal number */
5604 if (!TrioReadNumber(self, &number, 0, 3, BASE_OCTAL))
5608 else if (trio_to_upper(self->current) == 'X')
5610 /* Read hexadecimal number */
5611 self->InStream(self, NULL);
5612 if (!TrioReadNumber(self, &number, 0, 2, BASE_HEX))
5618 ch = (char)self->current;
5633TRIO_PRIVATE BOOLEAN_T
5635TRIO_ARGS4((self, target, flags, width),
5643 assert(VALID(self));
5644 assert(VALID(self->InStream));
5646 TrioSkipWhitespaces(self);
5649 * Continue until end of string is reached, a whitespace is encountered,
5650 * or width is exceeded
5653 ((width == NO_WIDTH) || (i < width)) &&
5654 (! ((self->current == EOF) || isspace(self->current)));
5657 if (TrioReadChar(self, (target ? &target[i] : 0), flags, 1) == 0)
5671TRIO_ARGS4((self, target, flags, width),
5673 trio_wchar_t *target,
5682 char buffer[MB_LEN_MAX + 1];
5684 assert(VALID(self));
5685 assert(VALID(self->InStream));
5688 (self->current != EOF) && (i < width);
5691 if (isascii(self->current))
5693 if (TrioReadChar(self, buffer, flags, 1) == 0)
5700 * Collect a multibyte character, by enlarging buffer until
5701 * it contains a fully legal multibyte character, or the
5707 buffer[j++] = (char)self->current;
5709 self->InStream(self, NULL);
5711 while ((j < (int)sizeof(buffer)) && (mblen(buffer, (size_t)j) != j));
5715 size = mbtowc(&wch, buffer, sizeof(buffer));
5720 self->InStream(self, NULL);
5724#endif /* TRIO_WIDECHAR */
5730TRIO_PRIVATE BOOLEAN_T
5732TRIO_ARGS4((self, target, flags, width),
5734 trio_wchar_t *target,
5741 assert(VALID(self));
5742 assert(VALID(self->InStream));
5744 TrioSkipWhitespaces(self);
5746#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5747 (void)mblen(NULL, 0);
5751 * Continue until end of string is reached, a whitespace is encountered,
5752 * or width is exceeded
5755 ((width == NO_WIDTH) || (i < width)) &&
5756 (! ((self->current == EOF) || isspace(self->current)));
5759 size = TrioReadWideChar(self, &target[i], flags, 1);
5766 target[i] = WCONST('\0');
5769#endif /* TRIO_WIDECHAR */
5776TRIO_PRIVATE BOOLEAN_T
5778TRIO_ARGS5((self, target, characterclass, flags, width),
5781 int *characterclass,
5788 assert(VALID(self));
5789 assert(VALID(self->InStream));
5793 ((width == NO_WIDTH) || (i < width)) &&
5795 (((flags & FLAGS_EXCLUDE) != 0) ^ (characterclass[ch] == 0))));
5799 target[i] = (char)ch;
5800 self->InStream(self, &ch);
5815TRIO_PRIVATE BOOLEAN_T
5817TRIO_ARGS4((self, target, flags, width),
5819 trio_pointer_t target,
5824 char doubleString[512];
5828 BOOLEAN_T isHex = FALSE;
5830 doubleString[0] = 0;
5832 if ((width == NO_WIDTH) || (width > (int)sizeof(doubleString) - 1))
5833 width = sizeof(doubleString) - 1;
5835 TrioSkipWhitespaces(self);
5838 * Read entire double number from stream. trio_to_double requires
5839 * a string as input, but InStream can be anything, so we have to
5840 * collect all characters.
5843 if ((ch == '+') || (ch == '-'))
5845 doubleString[index++] = (char)ch;
5846 self->InStream(self, &ch);
5862 while (isalpha(ch) && (index - start < width))
5864 doubleString[index++] = (char)ch;
5865 self->InStream(self, &ch);
5867 doubleString[index] = NIL;
5869 /* Case insensitive string comparison */
5870 if (trio_equal(&doubleString[start], INFINITE_UPPER) ||
5871 trio_equal(&doubleString[start], LONG_INFINITE_UPPER))
5873 if (flags & FLAGS_LONGDOUBLE)
5875 if ((start == 1) && (doubleString[0] == '-'))
5877 *((trio_long_double_t *)target) = trio_ninf();
5881 *((trio_long_double_t *)target) = trio_pinf();
5886 if ((start == 1) && (doubleString[0] == '-'))
5888 *((double *)target) = trio_ninf();
5892 *((double *)target) = trio_pinf();
5897 if (trio_equal(doubleString, NAN_UPPER))
5899 /* NaN must not have a preceding + nor - */
5900 if (flags & FLAGS_LONGDOUBLE)
5902 *((trio_long_double_t *)target) = trio_nan();
5906 *((double *)target) = trio_nan();
5913 doubleString[index++] = (char)ch;
5914 self->InStream(self, &ch);
5915 if (trio_to_upper(ch) == 'X')
5918 doubleString[index++] = (char)ch;
5919 self->InStream(self, &ch);
5927 while ((ch != EOF) && (index - start < width))
5930 if (isHex ? isxdigit(ch) : isdigit(ch))
5932 doubleString[index++] = (char)ch;
5933 self->InStream(self, &ch);
5935 else if (flags & FLAGS_QUOTE)
5937 /* Compare with thousands separator */
5938 for (j = 0; internalThousandSeparator[j] && self->current; j++)
5940 if (internalThousandSeparator[j] != self->current)
5943 self->InStream(self, &ch);
5945 if (internalThousandSeparator[j])
5946 break; /* Mismatch */
5948 continue; /* Match */
5956 doubleString[index++] = (char)ch;
5957 self->InStream(self, &ch);
5958 while ((isHex ? isxdigit(ch) : isdigit(ch)) &&
5959 (index - start < width))
5961 doubleString[index++] = (char)ch;
5962 self->InStream(self, &ch);
5964 if (isHex ? (trio_to_upper(ch) == 'P') : (trio_to_upper(ch) == 'E'))
5967 doubleString[index++] = (char)ch;
5968 self->InStream(self, &ch);
5969 if ((ch == '+') || (ch == '-'))
5971 doubleString[index++] = (char)ch;
5972 self->InStream(self, &ch);
5974 while (isdigit(ch) && (index - start < width))
5976 doubleString[index++] = (char)ch;
5977 self->InStream(self, &ch);
5982 if ((index == start) || (*doubleString == NIL))
5985 doubleString[index] = 0;
5987 if (flags & FLAGS_LONGDOUBLE)
5989 *((trio_long_double_t *)target) = trio_to_long_double(doubleString, NULL);
5993 *((double *)target) = trio_to_double(doubleString, NULL);
6001TRIO_PRIVATE BOOLEAN_T
6003TRIO_ARGS3((self, target, flags),
6005 trio_pointer_t *target,
6008 trio_uintmax_t number;
6009 char buffer[sizeof(internalNullString)];
6011 flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE | FLAGS_NILPADDING);
6013 if (TrioReadNumber(self,
6020 * The strange assignment of number is a workaround for a compiler
6024 *target = (char *)0 + number;
6027 else if (TrioReadString(self,
6028 (flags & FLAGS_IGNORE)
6032 sizeof(internalNullString) - 1))
6034 if (trio_equal_case(buffer, internalNullString))
6049TRIO_ARGS3((data, format, parameters),
6051 TRIO_CONST char *format,
6052 trio_parameter_t *parameters)
6054#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6060 int index; /* Index of format string */
6061 int i; /* Index of current parameter */
6065 trio_pointer_t pointer;
6070 data->InStream(data, &ch);
6072#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6073 (void)mblen(NULL, 0);
6076 while (format[index])
6078#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6079 if (! isascii(format[index]))
6081 charlen = mblen(&format[index], MB_LEN_MAX);
6084 /* Compare multibyte characters in format string */
6085 for (cnt = 0; cnt < charlen - 1; cnt++)
6087 if (ch != format[index + cnt])
6089 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6091 data->InStream(data, &ch);
6093 continue; /* while characters left in formatting string */
6096#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
6098 if ((EOF == ch) && (parameters[i].type != FORMAT_COUNT))
6100 return (assignment > 0) ? assignment : EOF;
6103 if (CHAR_IDENTIFIER == format[index])
6105 if (CHAR_IDENTIFIER == format[index + 1])
6107 /* Two % in format matches one % in input stream */
6108 if (CHAR_IDENTIFIER == ch)
6110 data->InStream(data, &ch);
6112 continue; /* while format chars left */
6115 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6118 /* Skip the parameter entries */
6119 while (parameters[i].type == FORMAT_PARAMETER)
6122 flags = parameters[i].flags;
6124 width = parameters[i].width;
6125 if (flags & FLAGS_WIDTH_PARAMETER)
6127 /* Get width from parameter list */
6128 width = (int)parameters[width].data.number.as_signed;
6131 base = parameters[i].base;
6132 if (flags & FLAGS_BASE_PARAMETER)
6134 /* Get base from parameter list */
6135 base = (int)parameters[base].data.number.as_signed;
6138 switch (parameters[i].type)
6142 trio_uintmax_t number;
6145 base = BASE_DECIMAL;
6147 if (!TrioReadNumber(data,
6154 if (!(flags & FLAGS_IGNORE))
6158 pointer = parameters[i].data.pointer;
6159#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6160 if (flags & FLAGS_SIZE_T)
6161 *(size_t *)pointer = (size_t)number;
6164#if defined(QUALIFIER_PTRDIFF_T)
6165 if (flags & FLAGS_PTRDIFF_T)
6166 *(ptrdiff_t *)pointer = (ptrdiff_t)number;
6169#if defined(QUALIFIER_INTMAX_T)
6170 if (flags & FLAGS_INTMAX_T)
6171 *(trio_intmax_t *)pointer = (trio_intmax_t)number;
6174 if (flags & FLAGS_QUAD)
6175 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)number;
6176 else if (flags & FLAGS_LONG)
6177 *(long int *)pointer = (long int)number;
6178 else if (flags & FLAGS_SHORT)
6179 *(short int *)pointer = (short int)number;
6181 *(int *)pointer = (int)number;
6184 break; /* FORMAT_INT */
6188 if (flags & FLAGS_WIDECHAR)
6190 if (!TrioReadWideString(data,
6191 (flags & FLAGS_IGNORE)
6193 : parameters[i].data.wstring,
6201 if (!TrioReadString(data,
6202 (flags & FLAGS_IGNORE)
6204 : parameters[i].data.string,
6209 if (!(flags & FLAGS_IGNORE))
6211 break; /* FORMAT_STRING */
6215 trio_pointer_t pointer;
6217 if (flags & FLAGS_IGNORE)
6223 pointer = (flags & FLAGS_LONGDOUBLE)
6224 ? (trio_pointer_t)parameters[i].data.longdoublePointer
6225 : (trio_pointer_t)parameters[i].data.doublePointer;
6227 if (!TrioReadDouble(data, pointer, flags, width))
6231 if (!(flags & FLAGS_IGNORE))
6235 break; /* FORMAT_DOUBLE */
6239 int characterclass[MAX_CHARACTER_CLASS + 1];
6242 /* Skip over modifiers */
6243 while (format[index] != SPECIFIER_GROUP)
6247 /* Skip over group specifier */
6250 memset(characterclass, 0, sizeof(characterclass));
6251 rc = TrioGetCharacterClass(format,
6258 if (!TrioReadGroup(data,
6259 (flags & FLAGS_IGNORE)
6261 : parameters[i].data.string,
6264 parameters[i].width))
6266 if (!(flags & FLAGS_IGNORE))
6269 break; /* FORMAT_GROUP */
6272 pointer = parameters[i].data.pointer;
6273 if (NULL != pointer)
6275 int count = data->committed;
6277 count--; /* a character is read, but is not consumed yet */
6278#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6279 if (flags & FLAGS_SIZE_T)
6280 *(size_t *)pointer = (size_t)count;
6283#if defined(QUALIFIER_PTRDIFF_T)
6284 if (flags & FLAGS_PTRDIFF_T)
6285 *(ptrdiff_t *)pointer = (ptrdiff_t)count;
6288#if defined(QUALIFIER_INTMAX_T)
6289 if (flags & FLAGS_INTMAX_T)
6290 *(trio_intmax_t *)pointer = (trio_intmax_t)count;
6293 if (flags & FLAGS_QUAD)
6295 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)count;
6297 else if (flags & FLAGS_LONG)
6299 *(long int *)pointer = (long int)count;
6301 else if (flags & FLAGS_SHORT)
6303 *(short int *)pointer = (short int)count;
6307 *(int *)pointer = (int)count;
6310 break; /* FORMAT_COUNT */
6314 if (flags & FLAGS_WIDECHAR)
6316 if (TrioReadWideChar(data,
6317 (flags & FLAGS_IGNORE)
6319 : parameters[i].data.wstring,
6321 (width == NO_WIDTH) ? 1 : width) == 0)
6327 if (TrioReadChar(data,
6328 (flags & FLAGS_IGNORE)
6330 : parameters[i].data.string,
6332 (width == NO_WIDTH) ? 1 : width) == 0)
6335 if (!(flags & FLAGS_IGNORE))
6337 break; /* FORMAT_CHAR */
6339 case FORMAT_POINTER:
6340 if (!TrioReadPointer(data,
6341 (flags & FLAGS_IGNORE)
6343 : (trio_pointer_t *)parameters[i].data.pointer,
6346 if (!(flags & FLAGS_IGNORE))
6348 break; /* FORMAT_POINTER */
6350 case FORMAT_PARAMETER:
6351 break; /* FORMAT_PARAMETER */
6354 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6357 index = parameters[i].indexAfterSpecifier;
6360 else /* Not an % identifier */
6362 if (isspace((int)format[index]))
6364 /* Whitespaces may match any amount of whitespaces */
6365 ch = TrioSkipWhitespaces(data);
6367 else if (ch == format[index])
6369 data->InStream(data, &ch);
6385TRIO_ARGS6((source, sourceSize, InStream, format, arglist, argarray),
6386 trio_pointer_t source,
6388 void (*InStream) TRIO_PROTO((trio_class_t *, int *)),
6389 TRIO_CONST char *format,
6390 TRIO_VA_LIST_PTR arglist,
6391 trio_pointer_t *argarray)
6394 trio_parameter_t parameters[MAX_PARAMETERS];
6397 assert(VALID(InStream));
6398 assert(VALID(format));
6400 memset(&data, 0, sizeof(data));
6401 data.InStream = InStream;
6402 data.location = (trio_pointer_t)source;
6403 data.max = sourceSize;
6406#if defined(USE_LOCALE)
6407 if (NULL == internalLocaleValues)
6413 status = TrioParse(TYPE_SCAN, format, parameters, arglist, argarray);
6417 status = TrioScanProcess(&data, format, parameters);
6418 if (data.error != 0)
6420 status = data.error;
6430TRIO_ARGS2((self, intPointer),
6436 assert(VALID(self));
6437 assert(VALID(self->location));
6438 assert(VALID(file));
6440 file = (FILE *)self->location;
6442 self->current = fgetc(file);
6443 if (self->current == EOF)
6445 self->error = (ferror(file))
6446 ? TRIO_ERROR_RETURN(TRIO_ERRNO, 0)
6447 : TRIO_ERROR_RETURN(TRIO_EOF, 0);
6455 if (VALID(intPointer))
6457 *intPointer = self->current;
6465TrioInStreamFileDescriptor
6466TRIO_ARGS2((self, intPointer),
6472 unsigned char input;
6474 assert(VALID(self));
6475 assert(VALID(self->location));
6477 fd = *((int *)self->location);
6479 size = read(fd, &input, sizeof(char));
6482 self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
6483 self->current = EOF;
6487 self->current = (size == 0) ? EOF : input;
6489 if (self->current != EOF)
6495 if (VALID(intPointer))
6497 *intPointer = self->current;
6506TRIO_ARGS2((self, intPointer),
6510 trio_custom_t *data;
6512 assert(VALID(self));
6513 assert(VALID(self->location));
6515 data = (trio_custom_t *)self->location;
6517 self->current = (data->stream.in == NULL)
6519 : (data->stream.in)(data->closure);
6521 if (self->current == NIL)
6523 self->current = EOF;
6531 if (VALID(intPointer))
6533 *intPointer = self->current;
6542TRIO_ARGS2((self, intPointer),
6546 unsigned char **buffer;
6548 assert(VALID(self));
6549 assert(VALID(self->location));
6551 buffer = (unsigned char **)self->location;
6552 self->current = (*buffer)[0];
6553 if (self->current == NIL)
6555 self->current = EOF;
6564 if (VALID(intPointer))
6566 *intPointer = self->current;
6576#if defined(TRIO_DOCUMENTATION)
6577# include "doc/doc_scanf.h
"
6596TRIO_VARGS2((format, va_alist),
6597 TRIO_CONST char *format,
6603 assert(VALID(format));
6605 TRIO_VA_START(args, format);
6606 status = TrioScan((trio_pointer_t)stdin, 0,
6608 format, TRIO_VA_LIST_ADDR(args), NULL);
6615TRIO_ARGS2((format, args),
6616 TRIO_CONST char *format,
6619 assert(VALID(format));
6621 return TrioScan((trio_pointer_t)stdin, 0,
6623 format, TRIO_VA_LIST_ADDR(args), NULL);
6628TRIO_ARGS2((format, args),
6629 TRIO_CONST char *format,
6630 trio_pointer_t *args)
6632 assert(VALID(format));
6634 return TrioScan((trio_pointer_t)stdin, 0,
6636 format, NULL, args);
6644TRIO_VARGS3((file, format, va_alist),
6646 TRIO_CONST char *format,
6652 assert(VALID(file));
6653 assert(VALID(format));
6655 TRIO_VA_START(args, format);
6656 status = TrioScan((trio_pointer_t)file, 0,
6658 format, TRIO_VA_LIST_ADDR(args), NULL);
6665TRIO_ARGS3((file, format, args),
6667 TRIO_CONST char *format,
6670 assert(VALID(file));
6671 assert(VALID(format));
6673 return TrioScan((trio_pointer_t)file, 0,
6675 format, TRIO_VA_LIST_ADDR(args), NULL);
6680TRIO_ARGS3((file, format, args),
6682 TRIO_CONST char *format,
6683 trio_pointer_t *args)
6685 assert(VALID(file));
6686 assert(VALID(format));
6688 return TrioScan((trio_pointer_t)file, 0,
6690 format, NULL, args);
6698TRIO_VARGS3((fd, format, va_alist),
6700 TRIO_CONST char *format,
6706 assert(VALID(format));
6708 TRIO_VA_START(args, format);
6709 status = TrioScan((trio_pointer_t)&fd, 0,
6710 TrioInStreamFileDescriptor,
6711 format, TRIO_VA_LIST_ADDR(args), NULL);
6718TRIO_ARGS3((fd, format, args),
6720 TRIO_CONST char *format,
6723 assert(VALID(format));
6725 return TrioScan((trio_pointer_t)&fd, 0,
6726 TrioInStreamFileDescriptor,
6727 format, TRIO_VA_LIST_ADDR(args), NULL);
6732TRIO_ARGS3((fd, format, args),
6734 TRIO_CONST char *format,
6735 trio_pointer_t *args)
6737 assert(VALID(format));
6739 return TrioScan((trio_pointer_t)&fd, 0,
6740 TrioInStreamFileDescriptor,
6741 format, NULL, args);
6749TRIO_VARGS4((stream, closure, format, va_alist),
6750 trio_instream_t stream,
6751 trio_pointer_t closure,
6752 TRIO_CONST char *format,
6759 assert(VALID(stream));
6760 assert(VALID(format));
6762 TRIO_VA_START(args, format);
6763 data.stream.in = stream;
6764 data.closure = closure;
6765 status = TrioScan(&data, 0, TrioInStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
6772TRIO_ARGS4((stream, closure, format, args),
6773 trio_instream_t stream,
6774 trio_pointer_t closure,
6775 TRIO_CONST char *format,
6780 assert(VALID(stream));
6781 assert(VALID(format));
6783 data.stream.in = stream;
6784 data.closure = closure;
6785 return TrioScan(&data, 0, TrioInStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
6790TRIO_ARGS4((stream, closure, format, args),
6791 trio_instream_t stream,
6792 trio_pointer_t closure,
6793 TRIO_CONST char *format,
6794 trio_pointer_t *args)
6798 assert(VALID(stream));
6799 assert(VALID(format));
6801 data.stream.in = stream;
6802 data.closure = closure;
6803 return TrioScan(&data, 0, TrioInStreamCustom, format, NULL, args);
6811TRIO_VARGS3((buffer, format, va_alist),
6812 TRIO_CONST char *buffer,
6813 TRIO_CONST char *format,
6819 assert(VALID(buffer));
6820 assert(VALID(format));
6822 TRIO_VA_START(args, format);
6823 status = TrioScan((trio_pointer_t)&buffer, 0,
6825 format, TRIO_VA_LIST_ADDR(args), NULL);
6832TRIO_ARGS3((buffer, format, args),
6833 TRIO_CONST char *buffer,
6834 TRIO_CONST char *format,
6837 assert(VALID(buffer));
6838 assert(VALID(format));
6840 return TrioScan((trio_pointer_t)&buffer, 0,
6842 format, TRIO_VA_LIST_ADDR(args), NULL);
6847TRIO_ARGS3((buffer, format, args),
6848 TRIO_CONST char *buffer,
6849 TRIO_CONST char *format,
6850 trio_pointer_t *args)
6852 assert(VALID(buffer));
6853 assert(VALID(format));
6855 return TrioScan((trio_pointer_t)&buffer, 0,
6857 format, NULL, args);
6865TRIO_PUBLIC TRIO_CONST char *
6867TRIO_ARGS1((errorcode),
6870 /* Textual versions of the error codes */
6871 switch (TRIO_ERROR_CODE(errorcode))
6874 return "End of
file";
6878 return "Too many arguments
";
6882 return "Reference gap
";
6888 return "Custom
error";
GLboolean GLboolean GLboolean GLboolean a
_CRTIMP void __cdecl rewind(_Inout_ FILE *_File)
__MINGW_EXTENSION typedef unsigned long long uintmax_t
__MINGW_EXTENSION typedef long long intmax_t
static unsigned int number
static const ASMPROP_RES defaults[ASM_NAME_MAX_PARAMS]
static char memory[1024 *256]
TRIO_SIGNED long int trio_longlong_t
TRIO_SIGNED int trio_int32_t
trio_longlong_t trio_intmax_t
trio_ulonglong_t trio_uintmax_t
@ FLAGS_PRECISION_PARAMETER
@ MAX_LOCALE_SEPARATOR_LENGTH
@ FLAGS_VARSIZE_PARAMETER
unsigned long int trio_ulonglong_t
TRIO_SIGNED char trio_int8_t
TRIO_SIGNED short trio_int16_t
trio_longlong_t trio_int64_t
unsigned long trio_flags_t