1
/*************************************************************************
3
* $Id: trio.c,v 1.2 2003/10/29 14:31:52 m-a Exp $
5
* Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
7
* Permission to use, copy, modify, and distribute this software for any
8
* purpose with or without fee is hereby granted, provided that the above
9
* copyright notice and this permission notice appear in all copies.
11
* THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
12
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
13
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
14
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
16
*************************************************************************
18
* A note to trio contributors:
20
* Avoid heap allocation at all costs to ensure that the trio functions
21
* are async-safe. The exceptions are the printf/fprintf functions, which
22
* uses fputc, and the asprintf functions and the <alloc> modifier, which
23
* by design are required to allocate form the heap.
25
************************************************************************/
29
* - Scan is probably too permissive about its modifiers.
30
* - C escapes in %#[] ?
31
* - Multibyte characters (done for format parsing, except scan groups)
32
* - Complex numbers? (C99 _Complex)
33
* - Boolean values? (C99 _Bool)
34
* - C99 NaN(n-char-sequence) missing. The n-char-sequence can be used
35
* to print the mantissa, e.g. NaN(0xc000000000000000)
36
* - Should we support the GNU %a alloc modifier? GNU has an ugly hack
37
* for %a, because C99 used %a for other purposes. If specified as
38
* %as or %a[ it is interpreted as the alloc modifier, otherwise as
39
* the C99 hex-float. This means that you cannot scan %as as a hex-float
40
* immediately followed by an 's'.
41
* - Scanning of collating symbols.
44
/*************************************************************************
51
#if !defined(TRIO_MINIMAL)
55
/**************************************************************************
59
*************************************************************************/
65
#if defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) || defined(USE_MULTIBYTE) || TRIO_WIDECHAR
66
# define TRIO_COMPILER_SUPPORTS_MULTIBYTE
67
# if !defined(MB_LEN_MAX)
72
#if (defined(TRIO_COMPILER_MSVC) && (_MSC_VER >= 1100)) || defined(TRIO_COMPILER_BCB)
73
# define TRIO_COMPILER_SUPPORTS_MSVC_INT
76
/*************************************************************************
80
#if !(defined(DEBUG) || defined(NDEBUG))
86
#if !defined(TRIO_COMPILER_SUPPORTS_C99)
87
# define isblank(x) (((x)==32) || ((x)==9))
89
#if defined(TRIO_COMPILER_ANCIENT)
100
#define NIL ((char)0)
102
# define FALSE (1 == 0)
103
# define TRUE (! FALSE)
105
#define BOOLEAN_T int
107
/* mincore() can be used for debugging purposes */
108
#define VALID(x) (NULL != (x))
112
* Encode the error code and the position. This is decoded
113
* with TRIO_ERROR_CODE and TRIO_ERROR_POSITION.
115
# define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
117
# define TRIO_ERROR_RETURN(x,y) (-1)
120
typedef unsigned long trio_flags_t;
123
/*************************************************************************
124
* Platform specific definitions
126
#if defined(TRIO_PLATFORM_UNIX)
131
#endif /* TRIO_PLATFORM_UNIX */
132
#if defined(TRIO_PLATFORM_VMS)
135
#if defined(TRIO_PLATFORM_WIN32)
138
# define write _write
139
#endif /* TRIO_PLATFORM_WIN32 */
142
# if defined(TRIO_COMPILER_SUPPORTS_ISO94)
145
typedef wchar_t trio_wchar_t;
146
typedef wint_t trio_wint_t;
148
typedef char trio_wchar_t;
149
typedef int trio_wint_t;
150
# define WCONST(x) L ## x
152
# define iswalnum(x) isalnum(x)
153
# define iswalpha(x) isalpha(x)
154
# define iswblank(x) isblank(x)
155
# define iswcntrl(x) iscntrl(x)
156
# define iswdigit(x) isdigit(x)
157
# define iswgraph(x) isgraph(x)
158
# define iswlower(x) islower(x)
159
# define iswprint(x) isprint(x)
160
# define iswpunct(x) ispunct(x)
161
# define iswspace(x) isspace(x)
162
# define iswupper(x) isupper(x)
163
# define iswxdigit(x) isxdigit(x)
168
/*************************************************************************
169
* Compiler dependent definitions
172
/* Support for long long */
174
# if !defined(USE_LONGLONG)
175
# if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
176
# define USE_LONGLONG
177
# elif defined(TRIO_COMPILER_SUNPRO)
178
# define USE_LONGLONG
179
# elif defined(_LONG_LONG) || defined(_LONGLONG)
180
# define USE_LONGLONG
185
/* The extra long numbers */
186
#if defined(USE_LONGLONG)
187
typedef signed long long int trio_longlong_t;
188
typedef unsigned long long int trio_ulonglong_t;
189
#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
190
typedef signed __int64 trio_longlong_t;
191
typedef unsigned __int64 trio_ulonglong_t;
193
typedef TRIO_SIGNED long int trio_longlong_t;
194
typedef unsigned long int trio_ulonglong_t;
197
/* Maximal and fixed integer types */
198
#if defined(TRIO_COMPILER_SUPPORTS_C99)
200
typedef intmax_t trio_intmax_t;
201
typedef uintmax_t trio_uintmax_t;
202
typedef int8_t trio_int8_t;
203
typedef int16_t trio_int16_t;
204
typedef int32_t trio_int32_t;
205
typedef int64_t trio_int64_t;
206
#elif defined(TRIO_COMPILER_SUPPORTS_UNIX98)
207
# include <inttypes.h>
208
typedef intmax_t trio_intmax_t;
209
typedef uintmax_t trio_uintmax_t;
210
typedef int8_t trio_int8_t;
211
typedef int16_t trio_int16_t;
212
typedef int32_t trio_int32_t;
213
typedef int64_t trio_int64_t;
214
#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
215
typedef trio_longlong_t trio_intmax_t;
216
typedef trio_ulonglong_t trio_uintmax_t;
217
typedef __int8 trio_int8_t;
218
typedef __int16 trio_int16_t;
219
typedef __int32 trio_int32_t;
220
typedef __int64 trio_int64_t;
222
typedef trio_longlong_t trio_intmax_t;
223
typedef trio_ulonglong_t trio_uintmax_t;
224
# if defined(TRIO_INT8_T)
225
typedef TRIO_INT8_T trio_int8_t;
227
typedef TRIO_SIGNED char trio_int8_t;
229
# if defined(TRIO_INT16_T)
230
typedef TRIO_INT16_T trio_int16_t;
232
typedef TRIO_SIGNED short trio_int16_t;
234
# if defined(TRIO_INT32_T)
235
typedef TRIO_INT32_T trio_int32_t;
237
typedef TRIO_SIGNED int trio_int32_t;
239
# if defined(TRIO_INT64_T)
240
typedef TRIO_INT64_T trio_int64_t;
242
typedef trio_longlong_t trio_int64_t;
246
#if !(defined(TRIO_COMPILER_SUPPORTS_C99) \
247
|| defined(TRIO_COMPILER_SUPPORTS_UNIX01))
248
# define floorl(x) floor((double)(x))
249
# define fmodl(x,y) fmod((double)(x),(double)(y))
250
# define powl(x,y) pow((double)(x),(double)(y))
253
#define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
255
/*************************************************************************
256
* Internal Definitions
260
# define DECIMAL_DIG DBL_DIG
263
/* Long double sizes */
265
# define MAX_MANTISSA_DIGITS LDBL_DIG
266
# define MAX_EXPONENT_DIGITS 4
267
# define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
269
# define MAX_MANTISSA_DIGITS DECIMAL_DIG
270
# define MAX_EXPONENT_DIGITS 3
271
# define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
274
#if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
276
# undef LDBL_MANT_DIG
278
# define LDBL_DIG DBL_DIG
279
# define LDBL_MANT_DIG DBL_MANT_DIG
280
# define LDBL_EPSILON DBL_EPSILON
283
/* The maximal number of digits is for base 2 */
284
#define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
285
/* The width of a pointer. The number of bits in a hex digit is 4 */
286
#define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
288
/* Infinite and Not-A-Number for floating-point */
289
#define INFINITE_LOWER "inf"
290
#define INFINITE_UPPER "INF"
291
#define LONG_INFINITE_LOWER "infinite"
292
#define LONG_INFINITE_UPPER "INFINITE"
293
#define NAN_LOWER "nan"
294
#define NAN_UPPER "NAN"
296
/* Various constants */
301
/* Flags. FLAGS_LAST must be less than ULONG_MAX */
304
FLAGS_SPACE = 2 * FLAGS_STICKY,
305
FLAGS_SHOWSIGN = 2 * FLAGS_SPACE,
306
FLAGS_LEFTADJUST = 2 * FLAGS_SHOWSIGN,
307
FLAGS_ALTERNATIVE = 2 * FLAGS_LEFTADJUST,
308
FLAGS_SHORT = 2 * FLAGS_ALTERNATIVE,
309
FLAGS_SHORTSHORT = 2 * FLAGS_SHORT,
310
FLAGS_LONG = 2 * FLAGS_SHORTSHORT,
311
FLAGS_QUAD = 2 * FLAGS_LONG,
312
FLAGS_LONGDOUBLE = 2 * FLAGS_QUAD,
313
FLAGS_SIZE_T = 2 * FLAGS_LONGDOUBLE,
314
FLAGS_PTRDIFF_T = 2 * FLAGS_SIZE_T,
315
FLAGS_INTMAX_T = 2 * FLAGS_PTRDIFF_T,
316
FLAGS_NILPADDING = 2 * FLAGS_INTMAX_T,
317
FLAGS_UNSIGNED = 2 * FLAGS_NILPADDING,
318
FLAGS_UPPER = 2 * FLAGS_UNSIGNED,
319
FLAGS_WIDTH = 2 * FLAGS_UPPER,
320
FLAGS_WIDTH_PARAMETER = 2 * FLAGS_WIDTH,
321
FLAGS_PRECISION = 2 * FLAGS_WIDTH_PARAMETER,
322
FLAGS_PRECISION_PARAMETER = 2 * FLAGS_PRECISION,
323
FLAGS_BASE = 2 * FLAGS_PRECISION_PARAMETER,
324
FLAGS_BASE_PARAMETER = 2 * FLAGS_BASE,
325
FLAGS_FLOAT_E = 2 * FLAGS_BASE_PARAMETER,
326
FLAGS_FLOAT_G = 2 * FLAGS_FLOAT_E,
327
FLAGS_QUOTE = 2 * FLAGS_FLOAT_G,
328
FLAGS_WIDECHAR = 2 * FLAGS_QUOTE,
329
FLAGS_ALLOC = 2 * FLAGS_WIDECHAR,
330
FLAGS_IGNORE = 2 * FLAGS_ALLOC,
331
FLAGS_IGNORE_PARAMETER = 2 * FLAGS_IGNORE,
332
FLAGS_VARSIZE_PARAMETER = 2 * FLAGS_IGNORE_PARAMETER,
333
FLAGS_FIXED_SIZE = 2 * FLAGS_VARSIZE_PARAMETER,
334
FLAGS_LAST = FLAGS_FIXED_SIZE,
336
FLAGS_EXCLUDE = FLAGS_SHORT,
337
FLAGS_USER_DEFINED = FLAGS_IGNORE,
338
FLAGS_ROUNDING = FLAGS_INTMAX_T,
339
/* Compounded flags */
340
FLAGS_ALL_VARSIZES = FLAGS_LONG | FLAGS_QUAD | FLAGS_INTMAX_T | FLAGS_PTRDIFF_T | FLAGS_SIZE_T,
341
FLAGS_ALL_SIZES = FLAGS_ALL_VARSIZES | FLAGS_SHORTSHORT | FLAGS_SHORT,
348
/* Do not change these */
357
/* Maximal number of allowed parameters */
359
/* Maximal number of characters in class */
360
MAX_CHARACTER_CLASS = UCHAR_MAX + 1,
362
/* Maximal string lengths for user-defined specifiers */
366
/* Maximal length of locale separator strings */
367
MAX_LOCALE_SEPARATOR_LENGTH = MB_LEN_MAX,
368
/* Maximal number of integers in grouping */
369
MAX_LOCALE_GROUPS = 64,
371
/* Initial size of asprintf buffer */
372
DYNAMIC_START_SIZE = 32
375
#define NO_GROUPING ((int)CHAR_MAX)
377
/* Fundamental formatting parameter types */
378
#define FORMAT_UNKNOWN 0
380
#define FORMAT_DOUBLE 2
381
#define FORMAT_CHAR 3
382
#define FORMAT_STRING 4
383
#define FORMAT_POINTER 5
384
#define FORMAT_COUNT 6
385
#define FORMAT_PARAMETER 7
386
#define FORMAT_GROUP 8
388
# define FORMAT_ERRNO 9
391
# define FORMAT_USER_DEFINED 10
394
/* Character constants */
395
#define CHAR_IDENTIFIER '%'
396
#define CHAR_BACKSLASH '\\'
397
#define CHAR_QUOTE '\"'
398
#define CHAR_ADJUST ' '
400
/* Character class expressions */
401
#define CLASS_ALNUM "[:alnum:]"
402
#define CLASS_ALPHA "[:alpha:]"
403
#define CLASS_BLANK "[:blank:]"
404
#define CLASS_CNTRL "[:cntrl:]"
405
#define CLASS_DIGIT "[:digit:]"
406
#define CLASS_GRAPH "[:graph:]"
407
#define CLASS_LOWER "[:lower:]"
408
#define CLASS_PRINT "[:print:]"
409
#define CLASS_PUNCT "[:punct:]"
410
#define CLASS_SPACE "[:space:]"
411
#define CLASS_UPPER "[:upper:]"
412
#define CLASS_XDIGIT "[:xdigit:]"
421
* C Widechar character (wint_t)
435
* S Widechar string (wchar_t *)
444
* D Binary Coded Decimal %D(length,precision) (OS/390)
446
#define SPECIFIER_CHAR 'c'
447
#define SPECIFIER_STRING 's'
448
#define SPECIFIER_DECIMAL 'd'
449
#define SPECIFIER_INTEGER 'i'
450
#define SPECIFIER_UNSIGNED 'u'
451
#define SPECIFIER_OCTAL 'o'
452
#define SPECIFIER_HEX 'x'
453
#define SPECIFIER_HEX_UPPER 'X'
454
#define SPECIFIER_FLOAT_E 'e'
455
#define SPECIFIER_FLOAT_E_UPPER 'E'
456
#define SPECIFIER_FLOAT_F 'f'
457
#define SPECIFIER_FLOAT_F_UPPER 'F'
458
#define SPECIFIER_FLOAT_G 'g'
459
#define SPECIFIER_FLOAT_G_UPPER 'G'
460
#define SPECIFIER_POINTER 'p'
461
#define SPECIFIER_GROUP '['
462
#define SPECIFIER_UNGROUP ']'
463
#define SPECIFIER_COUNT 'n'
465
# define SPECIFIER_CHAR_UPPER 'C'
466
# define SPECIFIER_STRING_UPPER 'S'
469
# define SPECIFIER_HEXFLOAT 'a'
470
# define SPECIFIER_HEXFLOAT_UPPER 'A'
473
# define SPECIFIER_ERRNO 'm'
476
# define SPECIFIER_BINARY 'b'
477
# define SPECIFIER_BINARY_UPPER 'B'
478
# define SPECIFIER_USER_DEFINED_BEGIN '<'
479
# define SPECIFIER_USER_DEFINED_END '>'
480
# define SPECIFIER_USER_DEFINED_SEPARATOR ':'
487
* Numbers = d,i,o,u,x,X
488
* Float = a,A,e,E,f,F,g,G
494
* Use the 9th parameter. 9 can be any number between 1 and
495
* the maximal argument
498
* Set width to 9. 9 can be any number, but must not be postfixed
503
* (unsigned) short int
511
* (unsigned) long int
519
* (unsigned) long long int
527
* Decimal-point is always present
529
* non-printable characters are handled as \number
540
* print: use parameter
541
* scan: no parameter (ignore)
551
* Integer part grouped in thousands
553
* Number grouped in nibbles (4 bits)
562
* @ Parameter (for both print and scan)
566
* The following options exists
568
* I16 = 16-bit integer
569
* I32 = 32-bit integer
570
* I64 = 64-bit integer
572
#define QUALIFIER_POSITION '$'
573
#define QUALIFIER_SHORT 'h'
574
#define QUALIFIER_LONG 'l'
575
#define QUALIFIER_LONG_UPPER 'L'
576
#define QUALIFIER_ALTERNATIVE '#'
577
#define QUALIFIER_SPACE ' '
578
#define QUALIFIER_PLUS '+'
579
#define QUALIFIER_MINUS '-'
580
#define QUALIFIER_DOT '.'
581
#define QUALIFIER_STAR '*'
582
#define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
584
# define QUALIFIER_SIZE_T 'z'
585
# define QUALIFIER_PTRDIFF_T 't'
586
# define QUALIFIER_INTMAX_T 'j'
588
#if TRIO_BSD || TRIO_GNU
589
# define QUALIFIER_QUAD 'q'
592
# define QUALIFIER_SIZE_T_UPPER 'Z'
595
# define QUALIFIER_WIDECHAR 'w'
598
# define QUALIFIER_FIXED_SIZE 'I'
601
# define QUALIFIER_QUOTE '\''
602
# define QUALIFIER_STICKY '!'
603
# define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
604
# define QUALIFIER_PARAM '@' /* Experimental */
605
# define QUALIFIER_COLON ':' /* For scanlists */
606
# define QUALIFIER_EQUAL '=' /* For scanlists */
607
# define QUALIFIER_ROUNDING_UPPER 'R'
611
/*************************************************************************
613
* Internal Structures
615
*************************************************************************/
619
/* An indication of which entry in the data union is used */
623
/* The width qualifier */
625
/* The precision qualifier */
627
/* The base qualifier */
629
/* The size for the variable size qualifier */
631
/* The marker of the end of the specifier */
632
int indexAfterSpecifier;
633
/* The data from the argument list */
637
trio_wchar_t *wstring;
639
trio_pointer_t pointer;
641
trio_intmax_t as_signed;
642
trio_uintmax_t as_unsigned;
645
double *doublePointer;
646
trio_long_double_t longdoubleNumber;
647
trio_long_double_t *longdoublePointer;
650
/* For the user-defined specifier */
651
char user_name[MAX_USER_NAME];
652
char user_data[MAX_USER_DATA];
655
/* Container for customized functions */
658
trio_outstream_t out;
661
trio_pointer_t closure;
664
/* General trio "class" */
665
typedef struct _trio_class_t {
667
* The function to write characters to a stream.
669
void (*OutStream) TRIO_PROTO((struct _trio_class_t *, int));
671
* The function to read characters from a stream.
673
void (*InStream) TRIO_PROTO((struct _trio_class_t *, int *));
675
* The current location in the stream.
677
trio_pointer_t location;
679
* The character currently being processed.
683
* The number of characters that would have been written/read
684
* if there had been sufficient space.
688
* The number of characters that are actually written/read.
689
* Processed and committed will only differ for the *nprintf
690
* and *nscanf functions.
694
* The upper limit of characters that may be written/read.
698
* The last output error that was detected.
703
/* References (for user-defined callbacks) */
704
typedef struct _trio_reference_t {
706
trio_parameter_t *parameter;
709
/* Registered entries (for user-defined callbacks) */
710
typedef struct _trio_userdef_t {
711
struct _trio_userdef_t *next;
712
trio_callback_t callback;
716
/*************************************************************************
720
*************************************************************************/
722
static TRIO_CONST char rcsid[] = "@(#)$Id: trio.c,v 1.2 2003/10/29 14:31:52 m-a Exp $";
725
* Need this to workaround a parser bug in HP C/iX compiler that fails
726
* to resolves macro definitions that includes type 'long double',
727
* e.g: va_arg(arg_ptr, long double)
729
#if defined(TRIO_PLATFORM_MPEIX)
730
static TRIO_CONST trio_long_double_t ___dummy_long_double = 0;
733
static TRIO_CONST char internalNullString[] = "(nil)";
735
#if defined(USE_LOCALE)
736
static struct lconv *internalLocaleValues = NULL;
740
* UNIX98 says "in a locale where the radix character is not defined,
741
* the radix character defaults to a period (.)"
743
static int internalDecimalPointLength = 1;
744
static int internalThousandSeparatorLength = 1;
745
static char internalDecimalPoint = '.';
746
static char internalDecimalPointString[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ".";
747
static char internalThousandSeparator[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ",";
748
static char internalGrouping[MAX_LOCALE_GROUPS] = { (char)NO_GROUPING };
750
static TRIO_CONST char internalDigitsLower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
751
static TRIO_CONST char internalDigitsUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
752
static BOOLEAN_T internalDigitsUnconverted = TRUE;
753
static int internalDigitArray[128];
755
static BOOLEAN_T internalCollationUnconverted = TRUE;
756
static char internalCollationArray[MAX_CHARACTER_CLASS][MAX_CHARACTER_CLASS];
760
static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion = NULL;
761
static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion = NULL;
762
static trio_userdef_t *internalUserDef = NULL;
766
/*************************************************************************
770
************************************************************************/
772
#if defined(TRIO_MINIMAL)
773
# define TRIO_STRING_PUBLIC static
774
# include "triostr.c"
775
#endif /* defined(TRIO_MINIMAL) */
777
/*************************************************************************
781
* Remember to add all new qualifiers to this function.
782
* QUALIFIER_POSITION must not be added.
784
TRIO_PRIVATE BOOLEAN_T
786
TRIO_ARGS1((character),
787
TRIO_CONST char character)
789
/* QUALIFIER_POSITION is not included */
792
case '0': case '1': case '2': case '3': case '4':
793
case '5': case '6': case '7': case '8': case '9':
795
case QUALIFIER_MINUS:
796
case QUALIFIER_SPACE:
799
case QUALIFIER_ALTERNATIVE:
800
case QUALIFIER_SHORT:
802
case QUALIFIER_LONG_UPPER:
803
case QUALIFIER_CIRCUMFLEX:
804
#if defined(QUALIFIER_SIZE_T)
805
case QUALIFIER_SIZE_T:
807
#if defined(QUALIFIER_PTRDIFF_T)
808
case QUALIFIER_PTRDIFF_T:
810
#if defined(QUALIFIER_INTMAX_T)
811
case QUALIFIER_INTMAX_T:
813
#if defined(QUALIFIER_QUAD)
816
#if defined(QUALIFIER_SIZE_T_UPPER)
817
case QUALIFIER_SIZE_T_UPPER:
819
#if defined(QUALIFIER_WIDECHAR)
820
case QUALIFIER_WIDECHAR:
822
#if defined(QUALIFIER_QUOTE)
823
case QUALIFIER_QUOTE:
825
#if defined(QUALIFIER_STICKY)
826
case QUALIFIER_STICKY:
828
#if defined(QUALIFIER_VARSIZE)
829
case QUALIFIER_VARSIZE:
831
#if defined(QUALIFIER_PARAM)
832
case QUALIFIER_PARAM:
834
#if defined(QUALIFIER_FIXED_SIZE)
835
case QUALIFIER_FIXED_SIZE:
837
#if defined(QUALIFIER_ROUNDING_UPPER)
838
case QUALIFIER_ROUNDING_UPPER:
846
/*************************************************************************
849
#if defined(USE_LOCALE)
851
TrioSetLocale(TRIO_NOARGS)
853
internalLocaleValues = (struct lconv *)localeconv();
854
if (internalLocaleValues)
856
if ((internalLocaleValues->decimal_point) &&
857
(internalLocaleValues->decimal_point[0] != NIL))
859
internalDecimalPointLength = trio_length(internalLocaleValues->decimal_point);
860
if (internalDecimalPointLength == 1)
862
internalDecimalPoint = internalLocaleValues->decimal_point[0];
866
internalDecimalPoint = NIL;
867
trio_copy_max(internalDecimalPointString,
868
sizeof(internalDecimalPointString),
869
internalLocaleValues->decimal_point);
872
if ((internalLocaleValues->thousands_sep) &&
873
(internalLocaleValues->thousands_sep[0] != NIL))
875
trio_copy_max(internalThousandSeparator,
876
sizeof(internalThousandSeparator),
877
internalLocaleValues->thousands_sep);
878
internalThousandSeparatorLength = trio_length(internalThousandSeparator);
880
if ((internalLocaleValues->grouping) &&
881
(internalLocaleValues->grouping[0] != NIL))
883
trio_copy_max(internalGrouping,
884
sizeof(internalGrouping),
885
internalLocaleValues->grouping);
889
#endif /* defined(USE_LOCALE) */
892
TrioCalcThousandSeparatorLength
898
int step = NO_GROUPING;
899
char *groupingPointer = internalGrouping;
903
if (*groupingPointer == CHAR_MAX)
905
/* Disable grouping */
908
else if (*groupingPointer == 0)
910
/* Repeat last group */
911
if (step == NO_GROUPING)
913
/* Error in locale */
919
step = *groupingPointer++;
922
count += internalThousandSeparatorLength;
931
TRIO_PRIVATE BOOLEAN_T
932
TrioFollowedBySeparator
933
TRIO_ARGS1((position),
938
char *groupingPointer = internalGrouping;
945
if (*groupingPointer == CHAR_MAX)
947
/* Disable grouping */
950
else if (*groupingPointer != 0)
952
step = *groupingPointer++;
958
return (position == 0);
964
/*************************************************************************
967
* Get the %n$ position.
971
TRIO_ARGS2((format, indexPointer),
972
TRIO_CONST char *format,
978
int index = *indexPointer;
980
number = (int)trio_to_long(&format[index], &tmpformat, BASE_DECIMAL);
981
index = (int)(tmpformat - format);
982
if ((number != 0) && (QUALIFIER_POSITION == format[index++]))
984
*indexPointer = index;
986
* number is decreased by 1, because n$ starts from 1, whereas
987
* the array it is indexing starts from 0.
996
/*************************************************************************
999
* Find registered user-defined specifier.
1000
* The prev argument is used for optimization only.
1002
TRIO_PRIVATE trio_userdef_t *
1004
TRIO_ARGS2((name, prev),
1005
TRIO_CONST char *name,
1006
trio_userdef_t **prev)
1008
trio_userdef_t *def;
1010
if (internalEnterCriticalRegion)
1011
(void)internalEnterCriticalRegion(NULL);
1013
for (def = internalUserDef; def; def = def->next)
1015
/* Case-sensitive string comparison */
1016
if (trio_equal_case(def->name, name))
1023
if (internalLeaveCriticalRegion)
1024
(void)internalLeaveCriticalRegion(NULL);
1030
/*************************************************************************
1034
* Calculate pow(base, exponent), where number and exponent are integers.
1036
TRIO_PRIVATE trio_long_double_t
1038
TRIO_ARGS2((number, exponent),
1042
trio_long_double_t result;
1048
/* Speed up calculation of common cases */
1050
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E-1);
1053
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+0);
1056
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+1);
1059
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+2);
1062
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+3);
1065
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+4);
1068
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+5);
1071
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+6);
1074
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+7);
1077
result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+8);
1080
result = powl((trio_long_double_t)number,
1081
(trio_long_double_t)exponent);
1087
return powl((trio_long_double_t)number, (trio_long_double_t)exponent);
1092
/*************************************************************************
1097
TRIO_ARGS2((number, base),
1105
/* xlC crashes on log(0) */
1106
result = (number == 0.0) ? trio_ninf() : trio_nan();
1112
result = log10(number);
1116
result = log10(number) / log10((double)base);
1122
/*************************************************************************
1132
case BASE_BINARY : return 1.0;
1133
case BASE_OCTAL : return 3.0;
1134
case BASE_DECIMAL: return 3.321928094887362345;
1135
case BASE_HEX : return 4.0;
1136
default : return TrioLogarithm((double)base, 2);
1140
/*************************************************************************
1144
* Parse the format string
1148
TRIO_ARGS5((type, format, parameters, arglist, argarray),
1150
TRIO_CONST char *format,
1151
trio_parameter_t *parameters,
1153
trio_pointer_t *argarray)
1155
/* Count the number of times a parameter is referenced */
1156
unsigned short usedEntries[MAX_PARAMETERS];
1157
/* Parameter counters */
1158
int parameterPosition;
1161
/* Utility variables */
1167
int index; /* Index into formatting string */
1168
int dots; /* Count number of dots in modifier part */
1169
BOOLEAN_T positional; /* Does the specifier have a positional? */
1170
BOOLEAN_T gotSticky = FALSE; /* Are there any sticky modifiers at all? */
1172
* indices specifies the order in which the parameters must be
1173
* read from the va_args (this is necessary to handle positionals)
1175
int indices[MAX_PARAMETERS];
1177
/* Various variables */
1179
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1187
/* One and only one of arglist and argarray must be used */
1188
assert((arglist != NULL) ^ (argarray != NULL));
1191
* The 'parameters' array is not initialized, but we need to
1192
* know which entries we have used.
1194
memset(usedEntries, 0, sizeof(usedEntries));
1198
parameterPosition = 0;
1199
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1200
(void)mblen(NULL, 0);
1203
while (format[index])
1205
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
1206
if (! isascii(format[index]))
1209
* Multibyte characters cannot be legal specifiers or
1210
* modifiers, so we skip over them.
1212
charlen = mblen(&format[index], MB_LEN_MAX);
1213
index += (charlen > 0) ? charlen : 1;
1214
continue; /* while */
1216
#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
1217
if (CHAR_IDENTIFIER == format[index++])
1219
if (CHAR_IDENTIFIER == format[index])
1222
continue; /* while */
1227
currentParam = TrioGetPosition(format, &index);
1228
positional = (NO_POSITION != currentParam);
1231
/* We have no positional, get the next counter */
1232
currentParam = parameterPosition;
1234
if(currentParam >= MAX_PARAMETERS)
1236
/* Bail out completely to make the error more obvious */
1237
return TRIO_ERROR_RETURN(TRIO_ETOOMANY, index);
1240
if (currentParam > maxParam)
1241
maxParam = currentParam;
1243
/* Default values */
1245
precision = NO_PRECISION;
1249
while (TrioIsQualifier(format[index]))
1251
ch = format[index++];
1255
case QUALIFIER_SPACE:
1256
flags |= FLAGS_SPACE;
1259
case QUALIFIER_PLUS:
1260
flags |= FLAGS_SHOWSIGN;
1263
case QUALIFIER_MINUS:
1264
flags |= FLAGS_LEFTADJUST;
1265
flags &= ~FLAGS_NILPADDING;
1268
case QUALIFIER_ALTERNATIVE:
1269
flags |= FLAGS_ALTERNATIVE;
1273
if (dots == 0) /* Precision */
1277
/* Skip if no precision */
1278
if (QUALIFIER_DOT == format[index])
1281
/* After the first dot we have the precision */
1282
flags |= FLAGS_PRECISION;
1283
if ((QUALIFIER_STAR == format[index])
1284
#if defined(QUALIFIER_PARAM)
1285
|| (QUALIFIER_PARAM == format[index])
1290
flags |= FLAGS_PRECISION_PARAMETER;
1292
precision = TrioGetPosition(format, &index);
1293
if (precision == NO_POSITION)
1295
parameterPosition++;
1297
precision = parameterPosition;
1300
precision = currentParam;
1301
currentParam = precision + 1;
1307
currentParam = precision + 1;
1308
if (width > maxParam)
1309
maxParam = precision;
1311
if (currentParam > maxParam)
1312
maxParam = currentParam;
1316
precision = trio_to_long(&format[index],
1319
index = (int)(tmpformat - format);
1322
else if (dots == 1) /* Base */
1326
/* After the second dot we have the base */
1327
flags |= FLAGS_BASE;
1328
if ((QUALIFIER_STAR == format[index])
1329
#if defined(QUALIFIER_PARAM)
1330
|| (QUALIFIER_PARAM == format[index])
1335
flags |= FLAGS_BASE_PARAMETER;
1336
base = TrioGetPosition(format, &index);
1337
if (base == NO_POSITION)
1339
parameterPosition++;
1341
base = parameterPosition;
1344
base = currentParam;
1345
currentParam = base + 1;
1351
currentParam = base + 1;
1352
if (base > maxParam)
1355
if (currentParam > maxParam)
1356
maxParam = currentParam;
1360
base = trio_to_long(&format[index],
1363
if (base > MAX_BASE)
1364
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1365
index = (int)(tmpformat - format);
1370
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1372
break; /* QUALIFIER_DOT */
1374
#if defined(QUALIFIER_PARAM)
1375
case QUALIFIER_PARAM:
1379
case QUALIFIER_STAR:
1380
/* This has different meanings for print and scan */
1381
if (TYPE_PRINT == type)
1383
/* Read with from parameter */
1384
flags |= (FLAGS_WIDTH | FLAGS_WIDTH_PARAMETER);
1385
width = TrioGetPosition(format, &index);
1386
if (width == NO_POSITION)
1388
parameterPosition++;
1390
width = parameterPosition;
1393
width = currentParam;
1394
currentParam = width + 1;
1400
currentParam = width + 1;
1401
if (width > maxParam)
1404
if (currentParam > maxParam)
1405
maxParam = currentParam;
1409
/* Scan, but do not store result */
1410
flags |= FLAGS_IGNORE;
1413
break; /* QUALIFIER_STAR */
1416
if (! (flags & FLAGS_LEFTADJUST))
1417
flags |= FLAGS_NILPADDING;
1419
case '1': case '2': case '3': case '4':
1420
case '5': case '6': case '7': case '8': case '9':
1421
flags |= FLAGS_WIDTH;
1422
/* &format[index - 1] is used to "rewind" the read
1423
* character from format
1425
width = trio_to_long(&format[index - 1],
1428
index = (int)(tmpformat - format);
1431
case QUALIFIER_SHORT:
1432
if (flags & FLAGS_SHORTSHORT)
1433
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1434
else if (flags & FLAGS_SHORT)
1435
flags |= FLAGS_SHORTSHORT;
1437
flags |= FLAGS_SHORT;
1440
case QUALIFIER_LONG:
1441
if (flags & FLAGS_QUAD)
1442
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1443
else if (flags & FLAGS_LONG)
1444
flags |= FLAGS_QUAD;
1446
flags |= FLAGS_LONG;
1449
case QUALIFIER_LONG_UPPER:
1450
flags |= FLAGS_LONGDOUBLE;
1453
#if defined(QUALIFIER_SIZE_T)
1454
case QUALIFIER_SIZE_T:
1455
flags |= FLAGS_SIZE_T;
1456
/* Modify flags for later truncation of number */
1457
if (sizeof(size_t) == sizeof(trio_ulonglong_t))
1458
flags |= FLAGS_QUAD;
1459
else if (sizeof(size_t) == sizeof(long))
1460
flags |= FLAGS_LONG;
1464
#if defined(QUALIFIER_PTRDIFF_T)
1465
case QUALIFIER_PTRDIFF_T:
1466
flags |= FLAGS_PTRDIFF_T;
1467
if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t))
1468
flags |= FLAGS_QUAD;
1469
else if (sizeof(ptrdiff_t) == sizeof(long))
1470
flags |= FLAGS_LONG;
1474
#if defined(QUALIFIER_INTMAX_T)
1475
case QUALIFIER_INTMAX_T:
1476
flags |= FLAGS_INTMAX_T;
1477
if (sizeof(trio_intmax_t) == sizeof(trio_ulonglong_t))
1478
flags |= FLAGS_QUAD;
1479
else if (sizeof(trio_intmax_t) == sizeof(long))
1480
flags |= FLAGS_LONG;
1484
#if defined(QUALIFIER_QUAD)
1485
case QUALIFIER_QUAD:
1486
flags |= FLAGS_QUAD;
1490
#if defined(QUALIFIER_FIXED_SIZE)
1491
case QUALIFIER_FIXED_SIZE:
1492
if (flags & FLAGS_FIXED_SIZE)
1493
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1495
if (flags & (FLAGS_ALL_SIZES | FLAGS_LONGDOUBLE |
1496
FLAGS_WIDECHAR | FLAGS_VARSIZE_PARAMETER))
1497
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1499
if ((format[index] == '6') &&
1500
(format[index + 1] == '4'))
1502
varsize = sizeof(trio_int64_t);
1505
else if ((format[index] == '3') &&
1506
(format[index + 1] == '2'))
1508
varsize = sizeof(trio_int32_t);
1511
else if ((format[index] == '1') &&
1512
(format[index + 1] == '6'))
1514
varsize = sizeof(trio_int16_t);
1517
else if (format[index] == '8')
1519
varsize = sizeof(trio_int8_t);
1523
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1525
flags |= FLAGS_FIXED_SIZE;
1529
#if defined(QUALIFIER_WIDECHAR)
1530
case QUALIFIER_WIDECHAR:
1531
flags |= FLAGS_WIDECHAR;
1535
#if defined(QUALIFIER_SIZE_T_UPPER)
1536
case QUALIFIER_SIZE_T_UPPER:
1540
#if defined(QUALIFIER_QUOTE)
1541
case QUALIFIER_QUOTE:
1542
flags |= FLAGS_QUOTE;
1546
#if defined(QUALIFIER_STICKY)
1547
case QUALIFIER_STICKY:
1548
flags |= FLAGS_STICKY;
1553
#if defined(QUALIFIER_VARSIZE)
1554
case QUALIFIER_VARSIZE:
1555
flags |= FLAGS_VARSIZE_PARAMETER;
1556
parameterPosition++;
1558
varsize = parameterPosition;
1561
varsize = currentParam;
1562
currentParam = varsize + 1;
1564
if (currentParam > maxParam)
1565
maxParam = currentParam;
1569
#if defined(QUALIFIER_ROUNDING_UPPER)
1570
case QUALIFIER_ROUNDING_UPPER:
1571
flags |= FLAGS_ROUNDING;
1576
/* Bail out completely to make the error more obvious */
1577
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1579
} /* while qualifier */
1582
* Parameters only need the type and value. The value is
1585
if (flags & FLAGS_WIDTH_PARAMETER)
1587
usedEntries[width] += 1;
1588
parameters[pos].type = FORMAT_PARAMETER;
1589
parameters[pos].flags = 0;
1590
indices[width] = pos;
1593
if (flags & FLAGS_PRECISION_PARAMETER)
1595
usedEntries[precision] += 1;
1596
parameters[pos].type = FORMAT_PARAMETER;
1597
parameters[pos].flags = 0;
1598
indices[precision] = pos;
1601
if (flags & FLAGS_BASE_PARAMETER)
1603
usedEntries[base] += 1;
1604
parameters[pos].type = FORMAT_PARAMETER;
1605
parameters[pos].flags = 0;
1606
indices[base] = pos;
1609
if (flags & FLAGS_VARSIZE_PARAMETER)
1611
usedEntries[varsize] += 1;
1612
parameters[pos].type = FORMAT_PARAMETER;
1613
parameters[pos].flags = 0;
1614
indices[varsize] = pos;
1618
indices[currentParam] = pos;
1620
switch (format[index++])
1622
#if defined(SPECIFIER_CHAR_UPPER)
1623
case SPECIFIER_CHAR_UPPER:
1624
flags |= FLAGS_WIDECHAR;
1627
case SPECIFIER_CHAR:
1628
if (flags & FLAGS_LONG)
1629
flags |= FLAGS_WIDECHAR;
1630
else if (flags & FLAGS_SHORT)
1631
flags &= ~FLAGS_WIDECHAR;
1632
parameters[pos].type = FORMAT_CHAR;
1635
#if defined(SPECIFIER_STRING_UPPER)
1636
case SPECIFIER_STRING_UPPER:
1637
flags |= FLAGS_WIDECHAR;
1640
case SPECIFIER_STRING:
1641
if (flags & FLAGS_LONG)
1642
flags |= FLAGS_WIDECHAR;
1643
else if (flags & FLAGS_SHORT)
1644
flags &= ~FLAGS_WIDECHAR;
1645
parameters[pos].type = FORMAT_STRING;
1648
case SPECIFIER_GROUP:
1649
if (TYPE_SCAN == type)
1652
parameters[pos].type = FORMAT_GROUP;
1653
if (format[index] == QUALIFIER_CIRCUMFLEX)
1655
if (format[index] == SPECIFIER_UNGROUP)
1657
if (format[index] == QUALIFIER_MINUS)
1659
/* Skip nested brackets */
1660
while (format[index] != NIL)
1662
if (format[index] == SPECIFIER_GROUP)
1666
else if (format[index] == SPECIFIER_UNGROUP)
1679
case SPECIFIER_INTEGER:
1680
parameters[pos].type = FORMAT_INT;
1683
case SPECIFIER_UNSIGNED:
1684
flags |= FLAGS_UNSIGNED;
1685
parameters[pos].type = FORMAT_INT;
1688
case SPECIFIER_DECIMAL:
1689
/* Disable base modifier */
1690
flags &= ~FLAGS_BASE_PARAMETER;
1691
base = BASE_DECIMAL;
1692
parameters[pos].type = FORMAT_INT;
1695
case SPECIFIER_OCTAL:
1696
flags |= FLAGS_UNSIGNED;
1697
flags &= ~FLAGS_BASE_PARAMETER;
1699
parameters[pos].type = FORMAT_INT;
1702
#if defined(SPECIFIER_BINARY)
1703
case SPECIFIER_BINARY_UPPER:
1704
flags |= FLAGS_UPPER;
1706
case SPECIFIER_BINARY:
1707
flags |= FLAGS_NILPADDING;
1708
flags &= ~FLAGS_BASE_PARAMETER;
1710
parameters[pos].type = FORMAT_INT;
1714
case SPECIFIER_HEX_UPPER:
1715
flags |= FLAGS_UPPER;
1718
flags |= FLAGS_UNSIGNED;
1719
flags &= ~FLAGS_BASE_PARAMETER;
1721
parameters[pos].type = FORMAT_INT;
1724
case SPECIFIER_FLOAT_E_UPPER:
1725
flags |= FLAGS_UPPER;
1727
case SPECIFIER_FLOAT_E:
1728
flags |= FLAGS_FLOAT_E;
1729
parameters[pos].type = FORMAT_DOUBLE;
1732
case SPECIFIER_FLOAT_G_UPPER:
1733
flags |= FLAGS_UPPER;
1735
case SPECIFIER_FLOAT_G:
1736
flags |= FLAGS_FLOAT_G;
1737
parameters[pos].type = FORMAT_DOUBLE;
1740
case SPECIFIER_FLOAT_F_UPPER:
1741
flags |= FLAGS_UPPER;
1743
case SPECIFIER_FLOAT_F:
1744
parameters[pos].type = FORMAT_DOUBLE;
1747
case SPECIFIER_POINTER:
1748
if (sizeof(trio_pointer_t) == sizeof(trio_ulonglong_t))
1749
flags |= FLAGS_QUAD;
1750
else if (sizeof(trio_pointer_t) == sizeof(long))
1751
flags |= FLAGS_LONG;
1752
parameters[pos].type = FORMAT_POINTER;
1755
case SPECIFIER_COUNT:
1756
parameters[pos].type = FORMAT_COUNT;
1759
#if defined(SPECIFIER_HEXFLOAT)
1760
# if defined(SPECIFIER_HEXFLOAT_UPPER)
1761
case SPECIFIER_HEXFLOAT_UPPER:
1762
flags |= FLAGS_UPPER;
1765
case SPECIFIER_HEXFLOAT:
1767
parameters[pos].type = FORMAT_DOUBLE;
1771
#if defined(FORMAT_ERRNO)
1772
case SPECIFIER_ERRNO:
1773
parameters[pos].type = FORMAT_ERRNO;
1777
#if defined(SPECIFIER_USER_DEFINED_BEGIN)
1778
case SPECIFIER_USER_DEFINED_BEGIN:
1781
int without_namespace = TRUE;
1783
parameters[pos].type = FORMAT_USER_DEFINED;
1784
parameters[pos].user_name[0] = NIL;
1785
tmpformat = (char *)&format[index];
1787
while ((ch = format[index]))
1790
if (ch == SPECIFIER_USER_DEFINED_END)
1792
if (without_namespace)
1794
/* We must get the handle first */
1795
parameters[pos].type = FORMAT_PARAMETER;
1796
parameters[pos].indexAfterSpecifier = index;
1797
parameters[pos].flags = FLAGS_USER_DEFINED;
1798
/* Adjust parameters for insertion of new one */
1800
usedEntries[currentParam] += 1;
1801
parameters[pos].type = FORMAT_USER_DEFINED;
1803
indices[currentParam] = pos;
1804
if (currentParam > maxParam)
1805
maxParam = currentParam;
1807
/* Copy the user data */
1808
max = (unsigned int)(&format[index] - tmpformat);
1809
if (max > MAX_USER_DATA)
1810
max = MAX_USER_DATA;
1811
trio_copy_max(parameters[pos].user_data,
1816
if (ch == SPECIFIER_USER_DEFINED_SEPARATOR)
1818
without_namespace = FALSE;
1819
/* Copy the namespace for later looking-up */
1820
max = (int)(&format[index] - tmpformat);
1821
if (max > MAX_USER_NAME)
1822
max = MAX_USER_NAME;
1823
trio_copy_max(parameters[pos].user_name,
1826
tmpformat = (char *)&format[index];
1829
if (ch != SPECIFIER_USER_DEFINED_END)
1830
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1833
#endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
1836
/* Bail out completely to make the error more obvious */
1837
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1840
/* Count the number of times this entry has been used */
1841
usedEntries[currentParam] += 1;
1843
/* Find last sticky parameters */
1844
if (gotSticky && !(flags & FLAGS_STICKY))
1846
for (i = pos - 1; i >= 0; i--)
1848
if (parameters[i].type == FORMAT_PARAMETER)
1850
if ((parameters[i].flags & FLAGS_STICKY) &&
1851
(parameters[i].type == parameters[pos].type))
1853
/* Do not overwrite current qualifiers */
1854
flags |= (parameters[i].flags & (unsigned long)~FLAGS_STICKY);
1855
if (width == NO_WIDTH)
1856
width = parameters[i].width;
1857
if (precision == NO_PRECISION)
1858
precision = parameters[i].precision;
1859
if (base == NO_BASE)
1860
base = parameters[i].base;
1866
parameters[pos].indexAfterSpecifier = index;
1867
parameters[pos].flags = flags;
1868
parameters[pos].width = width;
1869
parameters[pos].precision = precision;
1870
parameters[pos].base = (base == NO_BASE) ? BASE_DECIMAL : base;
1871
parameters[pos].varsize = varsize;
1875
parameterPosition++;
1877
} /* if identifier */
1879
} /* while format characters left */
1881
for (num = 0; num <= maxParam; num++)
1883
if (usedEntries[num] != 1)
1885
if (usedEntries[num] == 0) /* gap detected */
1886
return TRIO_ERROR_RETURN(TRIO_EGAP, num);
1887
else /* double references detected */
1888
return TRIO_ERROR_RETURN(TRIO_EDBLREF, num);
1894
* FORMAT_PARAMETERS are only present if they must be read,
1895
* so it makes no sense to check the ignore flag (besides,
1896
* the flags variable is not set for that particular type)
1898
if ((parameters[i].type != FORMAT_PARAMETER) &&
1899
(parameters[i].flags & FLAGS_IGNORE))
1900
continue; /* for all arguments */
1903
* The stack arguments are read according to ANSI C89
1904
* default argument promotions:
1908
* unsigned char = unsigned int
1909
* unsigned short = unsigned int
1912
* In addition to the ANSI C89 these types are read (the
1913
* default argument promotions of C99 has not been
1922
switch (parameters[i].type)
1927
if (flags & FLAGS_WIDECHAR)
1929
parameters[i].data.wstring = (argarray == NULL)
1930
? va_arg(*arglist, trio_wchar_t *)
1931
: (trio_wchar_t *)(argarray[num]);
1936
parameters[i].data.string = (argarray == NULL)
1937
? va_arg(*arglist, char *)
1938
: (char *)(argarray[num]);
1942
#if defined(FORMAT_USER_DEFINED)
1943
case FORMAT_USER_DEFINED:
1945
case FORMAT_POINTER:
1947
case FORMAT_UNKNOWN:
1948
parameters[i].data.pointer = (argarray == NULL)
1949
? va_arg(*arglist, trio_pointer_t )
1955
if (TYPE_SCAN == type)
1957
if (argarray == NULL)
1958
parameters[i].data.pointer =
1959
(trio_pointer_t)va_arg(*arglist, trio_pointer_t);
1962
if (parameters[i].type == FORMAT_CHAR)
1963
parameters[i].data.pointer =
1964
(trio_pointer_t)((char *)argarray[num]);
1965
else if (parameters[i].flags & FLAGS_SHORT)
1966
parameters[i].data.pointer =
1967
(trio_pointer_t)((short *)argarray[num]);
1969
parameters[i].data.pointer =
1970
(trio_pointer_t)((int *)argarray[num]);
1975
#if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
1976
if (parameters[i].flags
1977
& (FLAGS_VARSIZE_PARAMETER | FLAGS_FIXED_SIZE))
1979
if (parameters[i].flags & FLAGS_VARSIZE_PARAMETER)
1982
* Variable sizes are mapped onto the fixed sizes, in
1983
* accordance with integer promotion.
1985
* Please note that this may not be portable, as we
1986
* only guess the size, not the layout of the numbers.
1987
* For example, if int is little-endian, and long is
1988
* big-endian, then this will fail.
1990
varsize = (int)parameters[parameters[i].varsize].data.number.as_unsigned;
1994
/* Used for the I<bits> modifiers */
1995
varsize = parameters[i].varsize;
1997
parameters[i].flags &= ~FLAGS_ALL_VARSIZES;
1999
if (varsize <= (int)sizeof(int))
2001
else if (varsize <= (int)sizeof(long))
2002
parameters[i].flags |= FLAGS_LONG;
2003
#if defined(QUALIFIER_INTMAX_T)
2004
else if (varsize <= (int)sizeof(trio_longlong_t))
2005
parameters[i].flags |= FLAGS_QUAD;
2007
parameters[i].flags |= FLAGS_INTMAX_T;
2010
parameters[i].flags |= FLAGS_QUAD;
2013
#endif /* defined(QUALIFIER_VARSIZE) */
2014
#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
2015
if (parameters[i].flags & FLAGS_SIZE_T)
2016
parameters[i].data.number.as_unsigned = (argarray == NULL)
2017
? (trio_uintmax_t)va_arg(*arglist, size_t)
2018
: (trio_uintmax_t)(*((size_t *)argarray[num]));
2021
#if defined(QUALIFIER_PTRDIFF_T)
2022
if (parameters[i].flags & FLAGS_PTRDIFF_T)
2023
parameters[i].data.number.as_unsigned = (argarray == NULL)
2024
? (trio_uintmax_t)va_arg(*arglist, ptrdiff_t)
2025
: (trio_uintmax_t)(*((ptrdiff_t *)argarray[num]));
2028
#if defined(QUALIFIER_INTMAX_T)
2029
if (parameters[i].flags & FLAGS_INTMAX_T)
2030
parameters[i].data.number.as_unsigned = (argarray == NULL)
2031
? (trio_uintmax_t)va_arg(*arglist, trio_intmax_t)
2032
: (trio_uintmax_t)(*((trio_intmax_t *)argarray[num]));
2035
if (parameters[i].flags & FLAGS_QUAD)
2036
parameters[i].data.number.as_unsigned = (argarray == NULL)
2037
? (trio_uintmax_t)va_arg(*arglist, trio_ulonglong_t)
2038
: (trio_uintmax_t)(*((trio_ulonglong_t *)argarray[num]));
2039
else if (parameters[i].flags & FLAGS_LONG)
2040
parameters[i].data.number.as_unsigned = (argarray == NULL)
2041
? (trio_uintmax_t)va_arg(*arglist, long)
2042
: (trio_uintmax_t)(*((long *)argarray[num]));
2045
if (argarray == NULL)
2046
parameters[i].data.number.as_unsigned = (trio_uintmax_t)va_arg(*arglist, int);
2049
if (parameters[i].type == FORMAT_CHAR)
2050
parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((char *)argarray[num]));
2051
else if (parameters[i].flags & FLAGS_SHORT)
2052
parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((short *)argarray[num]));
2054
parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((int *)argarray[num]));
2060
case FORMAT_PARAMETER:
2062
* The parameter for the user-defined specifier is a pointer,
2063
* whereas the rest (width, precision, base) uses an integer.
2065
if (parameters[i].flags & FLAGS_USER_DEFINED)
2066
parameters[i].data.pointer = (argarray == NULL)
2067
? va_arg(*arglist, trio_pointer_t )
2070
parameters[i].data.number.as_unsigned = (argarray == NULL)
2071
? (trio_uintmax_t)va_arg(*arglist, int)
2072
: (trio_uintmax_t)(*((int *)argarray[num]));
2076
if (TYPE_SCAN == type)
2078
if (parameters[i].flags & FLAGS_LONGDOUBLE)
2079
parameters[i].data.longdoublePointer = (argarray == NULL)
2080
? va_arg(*arglist, trio_long_double_t *)
2081
: (trio_long_double_t *)argarray[num];
2084
if (parameters[i].flags & FLAGS_LONG)
2085
parameters[i].data.doublePointer = (argarray == NULL)
2086
? va_arg(*arglist, double *)
2087
: (double *)argarray[num];
2089
parameters[i].data.doublePointer = (argarray == NULL)
2090
? (double *)va_arg(*arglist, float *)
2091
: (double *)((float *)argarray[num]);
2096
if (parameters[i].flags & FLAGS_LONGDOUBLE)
2097
parameters[i].data.longdoubleNumber = (argarray == NULL)
2098
? va_arg(*arglist, trio_long_double_t)
2099
: (trio_long_double_t)(*((trio_long_double_t *)argarray[num]));
2102
if (argarray == NULL)
2103
parameters[i].data.longdoubleNumber =
2104
(trio_long_double_t)va_arg(*arglist, double);
2107
if (parameters[i].flags & FLAGS_SHORT)
2108
parameters[i].data.longdoubleNumber =
2109
(trio_long_double_t)(*((float *)argarray[num]));
2111
parameters[i].data.longdoubleNumber =
2112
(trio_long_double_t)(*((double *)argarray[num]));
2118
#if defined(FORMAT_ERRNO)
2120
parameters[i].data.errorNumber = save_errno;
2127
} /* for all specifiers */
2132
/*************************************************************************
2136
************************************************************************/
2139
/*************************************************************************
2144
* The complexity of this function is a result of the complexity
2145
* of the dependencies of the flags.
2149
TRIO_ARGS6((self, number, flags, width, precision, base),
2151
trio_uintmax_t number,
2157
BOOLEAN_T isNegative;
2158
BOOLEAN_T isNumberZero;
2159
BOOLEAN_T isPrecisionZero;
2160
BOOLEAN_T ignoreNumber;
2161
char buffer[MAX_CHARS_IN(trio_uintmax_t) * (1 + MAX_LOCALE_SEPARATOR_LENGTH) + 1];
2164
TRIO_CONST char *digits;
2170
assert(VALID(self));
2171
assert(VALID(self->OutStream));
2172
assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
2174
digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
2175
if (base == NO_BASE)
2176
base = BASE_DECIMAL;
2178
isNumberZero = (number == 0);
2179
isPrecisionZero = (precision == 0);
2180
ignoreNumber = (isNumberZero
2182
&& !((flags & FLAGS_ALTERNATIVE) && (base == BASE_OCTAL)));
2184
if (flags & FLAGS_UNSIGNED)
2187
flags &= ~FLAGS_SHOWSIGN;
2191
isNegative = ((trio_intmax_t)number < 0);
2193
number = -((trio_intmax_t)number);
2196
if (flags & FLAGS_QUAD)
2197
number &= (trio_ulonglong_t)-1;
2198
else if (flags & FLAGS_LONG)
2199
number &= (unsigned long)-1;
2201
number &= (unsigned int)-1;
2204
pointer = bufferend = &buffer[sizeof(buffer) - 1];
2206
for (i = 1; i < (int)sizeof(buffer); i++)
2208
*pointer-- = digits[number % base];
2213
if ((flags & FLAGS_QUOTE) && TrioFollowedBySeparator(i + 1))
2216
* We are building the number from the least significant
2217
* to the most significant digit, so we have to copy the
2218
* thousand separator backwards
2220
length = internalThousandSeparatorLength;
2221
if (((int)(pointer - buffer) - length) > 0)
2223
p = &internalThousandSeparator[length - 1];
2224
while (length-- > 0)
2233
width -= (bufferend - pointer) - 1;
2236
/* Adjust precision */
2237
if (NO_PRECISION != precision)
2239
precision -= (bufferend - pointer) - 1;
2242
flags |= FLAGS_NILPADDING;
2245
/* Calculate padding */
2246
count = (! ((flags & FLAGS_LEFTADJUST) || (precision == NO_PRECISION)))
2250
/* Adjust width further */
2251
if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
2253
if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
2262
if (!(flags & FLAGS_NILPADDING) || (count == 0))
2270
/* Output prefixes spaces if needed */
2271
if (! ((flags & FLAGS_LEFTADJUST) ||
2272
((flags & FLAGS_NILPADDING) && (precision == NO_PRECISION))))
2274
while (width-- > count)
2275
self->OutStream(self, CHAR_ADJUST);
2278
/* width has been adjusted for signs and alternatives */
2280
self->OutStream(self, '-');
2281
else if (flags & FLAGS_SHOWSIGN)
2282
self->OutStream(self, '+');
2283
else if (flags & FLAGS_SPACE)
2284
self->OutStream(self, ' ');
2286
/* Prefix is not written when the value is zero */
2287
if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
2292
self->OutStream(self, '0');
2293
self->OutStream(self, (flags & FLAGS_UPPER) ? 'B' : 'b');
2297
if (!(flags & FLAGS_NILPADDING) || (count == 0))
2298
self->OutStream(self, '0');
2302
self->OutStream(self, '0');
2303
self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2311
/* Output prefixed zero padding if needed */
2312
if (flags & FLAGS_NILPADDING)
2314
if (precision == NO_PRECISION)
2316
while (precision-- > 0)
2318
self->OutStream(self, '0');
2325
/* Output the number itself */
2326
while (*(++pointer))
2328
self->OutStream(self, *pointer);
2332
/* Output trailing spaces if needed */
2333
if (flags & FLAGS_LEFTADJUST)
2336
self->OutStream(self, CHAR_ADJUST);
2340
/*************************************************************************
2341
* TrioWriteStringCharacter
2344
* Output a single character of a string
2347
TrioWriteStringCharacter
2348
TRIO_ARGS3((self, ch, flags),
2353
if (flags & FLAGS_ALTERNATIVE)
2358
* Non-printable characters are converted to C escapes or
2359
* \number, if no C escape exists.
2361
self->OutStream(self, CHAR_BACKSLASH);
2364
case '\007': self->OutStream(self, 'a'); break;
2365
case '\b': self->OutStream(self, 'b'); break;
2366
case '\f': self->OutStream(self, 'f'); break;
2367
case '\n': self->OutStream(self, 'n'); break;
2368
case '\r': self->OutStream(self, 'r'); break;
2369
case '\t': self->OutStream(self, 't'); break;
2370
case '\v': self->OutStream(self, 'v'); break;
2371
case '\\': self->OutStream(self, '\\'); break;
2373
self->OutStream(self, 'x');
2374
TrioWriteNumber(self, (trio_uintmax_t)ch,
2375
FLAGS_UNSIGNED | FLAGS_NILPADDING,
2380
else if (ch == CHAR_BACKSLASH)
2382
self->OutStream(self, CHAR_BACKSLASH);
2383
self->OutStream(self, CHAR_BACKSLASH);
2387
self->OutStream(self, ch);
2392
self->OutStream(self, ch);
2396
/*************************************************************************
2404
TRIO_ARGS5((self, string, flags, width, precision),
2406
TRIO_CONST char *string,
2414
assert(VALID(self));
2415
assert(VALID(self->OutStream));
2419
string = internalNullString;
2420
length = sizeof(internalNullString) - 1;
2421
/* Disable quoting for the null pointer */
2422
flags &= (~FLAGS_QUOTE);
2427
length = trio_length(string);
2429
if ((NO_PRECISION != precision) &&
2430
(precision < length))
2436
if (flags & FLAGS_QUOTE)
2437
self->OutStream(self, CHAR_QUOTE);
2439
if (! (flags & FLAGS_LEFTADJUST))
2442
self->OutStream(self, CHAR_ADJUST);
2445
while (length-- > 0)
2447
/* The ctype parameters must be an unsigned char (or EOF) */
2448
ch = (int)((unsigned char)(*string++));
2449
TrioWriteStringCharacter(self, ch, flags);
2452
if (flags & FLAGS_LEFTADJUST)
2455
self->OutStream(self, CHAR_ADJUST);
2457
if (flags & FLAGS_QUOTE)
2458
self->OutStream(self, CHAR_QUOTE);
2461
/*************************************************************************
2462
* TrioWriteWideStringCharacter
2465
* Output a wide string as a multi-byte sequence
2469
TrioWriteWideStringCharacter
2470
TRIO_ARGS4((self, wch, flags, width),
2480
char buffer[MB_LEN_MAX + 1];
2482
if (width == NO_WIDTH)
2483
width = sizeof(buffer);
2485
size = wctomb(buffer, wch);
2486
if ((size <= 0) || (size > width) || (buffer[0] == NIL))
2491
while ((width >= i) && (width-- > 0) && (i-- > 0))
2493
/* The ctype parameters must be an unsigned char (or EOF) */
2494
ch = (int)((unsigned char)(*string++));
2495
TrioWriteStringCharacter(self, ch, flags);
2499
#endif /* TRIO_WIDECHAR */
2501
/*************************************************************************
2502
* TrioWriteWideString
2505
* Output a wide character string as a multi-byte string
2510
TRIO_ARGS5((self, wstring, flags, width, precision),
2512
TRIO_CONST trio_wchar_t *wstring,
2520
assert(VALID(self));
2521
assert(VALID(self->OutStream));
2523
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
2524
(void)mblen(NULL, 0);
2527
if (wstring == NULL)
2529
TrioWriteString(self, NULL, flags, width, precision);
2533
if (NO_PRECISION == precision)
2543
if (flags & FLAGS_QUOTE)
2544
self->OutStream(self, CHAR_QUOTE);
2546
if (! (flags & FLAGS_LEFTADJUST))
2549
self->OutStream(self, CHAR_ADJUST);
2554
size = TrioWriteWideStringCharacter(self, *wstring++, flags, length);
2560
if (flags & FLAGS_LEFTADJUST)
2563
self->OutStream(self, CHAR_ADJUST);
2565
if (flags & FLAGS_QUOTE)
2566
self->OutStream(self, CHAR_QUOTE);
2568
#endif /* TRIO_WIDECHAR */
2570
/*************************************************************************
2573
* http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_211.htm
2575
* "5.2.4.2.2 paragraph #4
2577
* The accuracy [...] is implementation defined, as is the accuracy
2578
* of the conversion between floating-point internal representations
2579
* and string representations performed by the libray routine in
2582
/* FIXME: handle all instances of constant long-double number (L)
2583
* and *l() math functions.
2587
TRIO_ARGS6((self, number, flags, width, precision, base),
2589
trio_long_double_t number,
2595
trio_long_double_t integerNumber;
2596
trio_long_double_t fractionNumber;
2597
trio_long_double_t workNumber;
2602
int integerThreshold;
2603
int fractionThreshold;
2606
unsigned int uExponent = 0;
2608
trio_long_double_t dblBase;
2609
trio_long_double_t dblIntegerBase;
2610
trio_long_double_t dblFractionBase;
2611
trio_long_double_t integerAdjust;
2612
trio_long_double_t fractionAdjust;
2613
BOOLEAN_T isNegative;
2614
BOOLEAN_T isExponentNegative = FALSE;
2615
BOOLEAN_T requireTwoDigitExponent;
2617
TRIO_CONST char *digits;
2618
char *groupingPointer;
2621
BOOLEAN_T hasOnlyZeroes;
2623
register int trailingZeroes;
2624
BOOLEAN_T keepTrailingZeroes;
2625
BOOLEAN_T keepDecimalPoint;
2626
trio_long_double_t epsilon;
2628
assert(VALID(self));
2629
assert(VALID(self->OutStream));
2630
assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
2632
/* Determine sign and look for special quantities */
2633
switch (trio_fpclassify_and_signbit(number, &isNegative))
2636
TrioWriteString(self,
2637
(flags & FLAGS_UPPER)
2640
flags, width, precision);
2643
case TRIO_FP_INFINITE:
2646
/* Negative infinity */
2647
TrioWriteString(self,
2648
(flags & FLAGS_UPPER)
2649
? "-" INFINITE_UPPER
2650
: "-" INFINITE_LOWER,
2651
flags, width, precision);
2656
/* Positive infinity */
2657
TrioWriteString(self,
2658
(flags & FLAGS_UPPER)
2661
flags, width, precision);
2670
/* Normal numbers */
2671
if (flags & FLAGS_LONGDOUBLE)
2673
baseDigits = (base == 10)
2675
: (int)floor(LDBL_MANT_DIG / TrioLogarithmBase(base));
2676
epsilon = LDBL_EPSILON;
2678
else if (flags & FLAGS_SHORT)
2680
baseDigits = (base == BASE_DECIMAL)
2682
: (int)floor(FLT_MANT_DIG / TrioLogarithmBase(base));
2683
epsilon = FLT_EPSILON;
2687
baseDigits = (base == BASE_DECIMAL)
2689
: (int)floor(DBL_MANT_DIG / TrioLogarithmBase(base));
2690
epsilon = DBL_EPSILON;
2693
digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
2694
isHex = (base == BASE_HEX);
2695
if (base == NO_BASE)
2696
base = BASE_DECIMAL;
2697
dblBase = (trio_long_double_t)base;
2698
keepTrailingZeroes = !( (flags & FLAGS_ROUNDING) ||
2699
( (flags & FLAGS_FLOAT_G) &&
2700
!(flags & FLAGS_ALTERNATIVE) ) );
2702
if (flags & FLAGS_ROUNDING)
2703
precision = baseDigits;
2705
if (precision == NO_PRECISION)
2709
keepTrailingZeroes = FALSE;
2710
precision = FLT_MANT_DIG;
2714
precision = FLT_DIG;
2722
flags |= FLAGS_FLOAT_E;
2724
if (flags & FLAGS_FLOAT_G)
2729
if ((number < 1.0E-4) || (number > powl(base,
2730
(trio_long_double_t)precision)))
2732
/* Use scientific notation */
2733
flags |= FLAGS_FLOAT_E;
2735
else if (number < 1.0)
2738
* Use normal notation. If the integer part of the number is
2739
* zero, then adjust the precision to include leading fractional
2742
workNumber = TrioLogarithm(number, base);
2743
workNumber = TRIO_FABS(workNumber);
2744
if (workNumber - floorl(workNumber) < 0.001)
2746
zeroes = (int)floorl(workNumber);
2750
if (flags & FLAGS_FLOAT_E)
2752
/* Scale the number */
2753
workNumber = TrioLogarithm(number, base);
2754
if (trio_isinf(workNumber) == -1)
2758
if (flags & FLAGS_FLOAT_G)
2759
flags &= ~FLAGS_FLOAT_E;
2763
exponent = (int)floorl(workNumber);
2764
number /= powl(dblBase, (trio_long_double_t)exponent);
2765
isExponentNegative = (exponent < 0);
2766
uExponent = (isExponentNegative) ? -exponent : exponent;
2768
uExponent *= 4; /* log16(2) */
2769
/* No thousand separators */
2770
flags &= ~FLAGS_QUOTE;
2774
integerNumber = floorl(number);
2775
fractionNumber = number - integerNumber;
2780
* Precision is number of significant digits for FLOAT_G
2781
* and number of fractional digits for others.
2783
integerDigits = (integerNumber > epsilon)
2784
? 1 + (int)TrioLogarithm(integerNumber, base)
2786
fractionDigits = ((flags & FLAGS_FLOAT_G) && (zeroes == 0))
2787
? precision - integerDigits
2788
: zeroes + precision;
2790
dblFractionBase = TrioPower(base, fractionDigits);
2792
workNumber = number + 0.5 / dblFractionBase;
2793
if (floorl(number) != floorl(workNumber))
2795
if (flags & FLAGS_FLOAT_E)
2797
/* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
2799
isExponentNegative = (exponent < 0);
2800
uExponent = (isExponentNegative) ? -exponent : exponent;
2802
uExponent *= 4; /* log16(2) */
2803
workNumber = (number + 0.5 / dblFractionBase) / dblBase;
2804
integerNumber = floorl(workNumber);
2805
fractionNumber = workNumber - integerNumber;
2809
/* Adjust if number was rounded up one digit (ie. 99 to 100) */
2810
integerNumber = floorl(number + 0.5);
2811
fractionNumber = 0.0;
2812
integerDigits = (integerNumber > epsilon)
2813
? 1 + (int)TrioLogarithm(integerNumber, base)
2818
/* Estimate accuracy */
2819
integerAdjust = fractionAdjust = 0.5;
2820
if (flags & FLAGS_ROUNDING)
2822
if (integerDigits > baseDigits)
2824
integerThreshold = baseDigits;
2826
dblFractionBase = 1.0;
2827
fractionThreshold = 0;
2828
precision = 0; /* Disable decimal-point */
2829
integerAdjust = TrioPower(base, integerDigits - integerThreshold - 1);
2830
fractionAdjust = 0.0;
2834
integerThreshold = integerDigits;
2835
fractionThreshold = fractionDigits - integerThreshold;
2836
fractionAdjust = 1.0;
2841
integerThreshold = INT_MAX;
2842
fractionThreshold = INT_MAX;
2846
* Calculate expected width.
2847
* sign + integer part + thousands separators + decimal point
2848
* + fraction + exponent
2850
fractionAdjust /= dblFractionBase;
2851
hasOnlyZeroes = (floorl((fractionNumber + fractionAdjust) * dblFractionBase) < epsilon);
2852
keepDecimalPoint = ( (flags & FLAGS_ALTERNATIVE) ||
2853
!((precision == 0) ||
2854
(!keepTrailingZeroes && hasOnlyZeroes)) );
2855
if (flags & FLAGS_FLOAT_E)
2857
exponentDigits = (uExponent == 0)
2859
: (int)ceil(TrioLogarithm((double)(uExponent + 1),
2860
(isHex) ? 10.0 : base));
2864
requireTwoDigitExponent = ((base == BASE_DECIMAL) && (exponentDigits == 1));
2866
expectedWidth = integerDigits + fractionDigits
2868
? internalDecimalPointLength
2870
+ ((flags & FLAGS_QUOTE)
2871
? TrioCalcThousandSeparatorLength(integerDigits)
2873
if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
2874
expectedWidth += sizeof("-") - 1;
2875
if (exponentDigits > 0)
2876
expectedWidth += exponentDigits +
2877
((requireTwoDigitExponent ? sizeof("E+0") : sizeof("E+")) - 1);
2879
expectedWidth += sizeof("0X") - 1;
2881
/* Output prefixing */
2882
if (flags & FLAGS_NILPADDING)
2884
/* Leading zeros must be after sign */
2886
self->OutStream(self, '-');
2887
else if (flags & FLAGS_SHOWSIGN)
2888
self->OutStream(self, '+');
2889
else if (flags & FLAGS_SPACE)
2890
self->OutStream(self, ' ');
2893
self->OutStream(self, '0');
2894
self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2896
if (!(flags & FLAGS_LEFTADJUST))
2898
for (i = expectedWidth; i < width; i++)
2900
self->OutStream(self, '0');
2906
/* Leading spaces must be before sign */
2907
if (!(flags & FLAGS_LEFTADJUST))
2909
for (i = expectedWidth; i < width; i++)
2911
self->OutStream(self, CHAR_ADJUST);
2915
self->OutStream(self, '-');
2916
else if (flags & FLAGS_SHOWSIGN)
2917
self->OutStream(self, '+');
2918
else if (flags & FLAGS_SPACE)
2919
self->OutStream(self, ' ');
2922
self->OutStream(self, '0');
2923
self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2927
/* Output the integer part and thousand separators */
2928
dblIntegerBase = 1.0 / TrioPower(base, integerDigits - 1);
2929
for (i = 0; i < integerDigits; i++)
2931
workNumber = floorl(((integerNumber + integerAdjust) * dblIntegerBase));
2932
if (i > integerThreshold)
2934
/* Beyond accuracy */
2935
self->OutStream(self, digits[0]);
2939
self->OutStream(self, digits[(int)fmodl(workNumber, dblBase)]);
2941
dblIntegerBase *= dblBase;
2943
if (((flags & (FLAGS_FLOAT_E | FLAGS_QUOTE)) == FLAGS_QUOTE)
2944
&& TrioFollowedBySeparator(integerDigits - i))
2946
for (groupingPointer = internalThousandSeparator;
2947
*groupingPointer != NIL;
2950
self->OutStream(self, *groupingPointer);
2955
/* Insert decimal point and build the fraction part */
2958
if (keepDecimalPoint)
2960
if (internalDecimalPoint)
2962
self->OutStream(self, internalDecimalPoint);
2966
for (i = 0; i < internalDecimalPointLength; i++)
2968
self->OutStream(self, internalDecimalPointString[i]);
2973
for (i = 0; i < fractionDigits; i++)
2975
if ((integerDigits > integerThreshold) || (i > fractionThreshold))
2977
/* Beyond accuracy */
2982
fractionNumber *= dblBase;
2983
fractionAdjust *= dblBase;
2984
workNumber = floorl(fractionNumber + fractionAdjust);
2985
fractionNumber -= workNumber;
2986
index = (int)fmodl(workNumber, dblBase);
2993
while (trailingZeroes > 0)
2995
/* Not trailing zeroes after all */
2996
self->OutStream(self, digits[0]);
2999
self->OutStream(self, digits[index]);
3004
if (keepTrailingZeroes)
3006
while (trailingZeroes > 0)
3008
self->OutStream(self, digits[0]);
3013
/* Output exponent */
3014
if (exponentDigits > 0)
3016
self->OutStream(self,
3018
? ((flags & FLAGS_UPPER) ? 'P' : 'p')
3019
: ((flags & FLAGS_UPPER) ? 'E' : 'e'));
3020
self->OutStream(self, (isExponentNegative) ? '-' : '+');
3022
/* The exponent must contain at least two digits */
3023
if (requireTwoDigitExponent)
3024
self->OutStream(self, '0');
3028
exponentBase = (int)TrioPower(base, exponentDigits - 1);
3029
for (i = 0; i < exponentDigits; i++)
3031
self->OutStream(self, digits[(uExponent / exponentBase) % base]);
3032
exponentBase /= base;
3035
/* Output trailing spaces */
3036
if (flags & FLAGS_LEFTADJUST)
3038
for (i = expectedWidth; i < width; i++)
3040
self->OutStream(self, CHAR_ADJUST);
3045
/*************************************************************************
3049
* This is the main engine for formatting output
3053
TRIO_ARGS3((data, format, parameters),
3055
TRIO_CONST char *format,
3056
trio_parameter_t *parameters)
3058
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3062
TRIO_CONST char *string;
3063
trio_pointer_t pointer;
3072
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3073
(void)mblen(NULL, 0);
3076
while (format[index])
3078
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
3079
if (! isascii(format[index]))
3081
charlen = mblen(&format[index], MB_LEN_MAX);
3083
* Only valid multibyte characters are handled here. Invalid
3084
* multibyte characters (charlen == -1) are handled as normal
3089
while (charlen-- > 0)
3091
data->OutStream(data, format[index++]);
3093
continue; /* while characters left in formatting string */
3096
#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
3097
if (CHAR_IDENTIFIER == format[index])
3099
if (CHAR_IDENTIFIER == format[index + 1])
3101
data->OutStream(data, CHAR_IDENTIFIER);
3106
/* Skip the parameter entries */
3107
while (parameters[i].type == FORMAT_PARAMETER)
3110
flags = parameters[i].flags;
3113
width = parameters[i].width;
3114
if (flags & FLAGS_WIDTH_PARAMETER)
3116
/* Get width from parameter list */
3117
width = (int)parameters[width].data.number.as_signed;
3121
* A negative width is the same as the - flag and
3124
flags |= FLAGS_LEFTADJUST;
3125
flags &= ~FLAGS_NILPADDING;
3130
/* Find precision */
3131
if (flags & FLAGS_PRECISION)
3133
precision = parameters[i].precision;
3134
if (flags & FLAGS_PRECISION_PARAMETER)
3136
/* Get precision from parameter list */
3137
precision = (int)parameters[precision].data.number.as_signed;
3141
* A negative precision is the same as no
3144
precision = NO_PRECISION;
3150
precision = NO_PRECISION;
3154
base = parameters[i].base;
3155
if (flags & FLAGS_BASE_PARAMETER)
3157
/* Get base from parameter list */
3158
base = (int)parameters[base].data.number.as_signed;
3161
switch (parameters[i].type)
3164
if (flags & FLAGS_QUOTE)
3165
data->OutStream(data, CHAR_QUOTE);
3166
if (! (flags & FLAGS_LEFTADJUST))
3169
data->OutStream(data, CHAR_ADJUST);
3172
if (flags & FLAGS_WIDECHAR)
3174
TrioWriteWideStringCharacter(data,
3175
(trio_wchar_t)parameters[i].data.number.as_signed,
3182
TrioWriteStringCharacter(data,
3183
(int)parameters[i].data.number.as_signed,
3187
if (flags & FLAGS_LEFTADJUST)
3190
data->OutStream(data, CHAR_ADJUST);
3192
if (flags & FLAGS_QUOTE)
3193
data->OutStream(data, CHAR_QUOTE);
3195
break; /* FORMAT_CHAR */
3198
TrioWriteNumber(data,
3199
parameters[i].data.number.as_unsigned,
3205
break; /* FORMAT_INT */
3208
TrioWriteDouble(data,
3209
parameters[i].data.longdoubleNumber,
3214
break; /* FORMAT_DOUBLE */
3218
if (flags & FLAGS_WIDECHAR)
3220
TrioWriteWideString(data,
3221
parameters[i].data.wstring,
3229
TrioWriteString(data,
3230
parameters[i].data.string,
3235
break; /* FORMAT_STRING */
3237
case FORMAT_POINTER:
3239
trio_reference_t reference;
3241
reference.data = data;
3242
reference.parameter = ¶meters[i];
3243
trio_print_pointer(&reference, parameters[i].data.pointer);
3245
break; /* FORMAT_POINTER */
3248
pointer = parameters[i].data.pointer;
3249
if (NULL != pointer)
3252
* C99 paragraph 7.19.6.1.8 says "the number of
3253
* characters written to the output stream so far by
3254
* this call", which is data->committed
3256
#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
3257
if (flags & FLAGS_SIZE_T)
3258
*(size_t *)pointer = (size_t)data->committed;
3261
#if defined(QUALIFIER_PTRDIFF_T)
3262
if (flags & FLAGS_PTRDIFF_T)
3263
*(ptrdiff_t *)pointer = (ptrdiff_t)data->committed;
3266
#if defined(QUALIFIER_INTMAX_T)
3267
if (flags & FLAGS_INTMAX_T)
3268
*(trio_intmax_t *)pointer = (trio_intmax_t)data->committed;
3271
if (flags & FLAGS_QUAD)
3273
*(trio_ulonglong_t *)pointer = (trio_ulonglong_t)data->committed;
3275
else if (flags & FLAGS_LONG)
3277
*(long int *)pointer = (long int)data->committed;
3279
else if (flags & FLAGS_SHORT)
3281
*(short int *)pointer = (short int)data->committed;
3285
*(int *)pointer = (int)data->committed;
3288
break; /* FORMAT_COUNT */
3290
case FORMAT_PARAMETER:
3291
break; /* FORMAT_PARAMETER */
3293
#if defined(FORMAT_ERRNO)
3295
string = trio_error(parameters[i].data.errorNumber);
3298
TrioWriteString(data,
3306
data->OutStream(data, '#');
3307
TrioWriteNumber(data,
3308
(trio_uintmax_t)parameters[i].data.errorNumber,
3314
break; /* FORMAT_ERRNO */
3315
#endif /* defined(FORMAT_ERRNO) */
3317
#if defined(FORMAT_USER_DEFINED)
3318
case FORMAT_USER_DEFINED:
3320
trio_reference_t reference;
3321
trio_userdef_t *def = NULL;
3323
if (parameters[i].user_name[0] == NIL)
3327
(parameters[i - 1].type == FORMAT_PARAMETER))
3328
def = (trio_userdef_t *)parameters[i - 1].data.pointer;
3332
/* Look up namespace */
3333
def = TrioFindNamespace(parameters[i].user_name, NULL);
3336
reference.data = data;
3337
reference.parameter = ¶meters[i];
3338
def->callback(&reference);
3342
#endif /* defined(FORMAT_USER_DEFINED) */
3346
} /* switch parameter type */
3348
/* Prepare for next */
3349
index = parameters[i].indexAfterSpecifier;
3353
else /* not identifier */
3355
data->OutStream(data, format[index++]);
3358
return data->processed;
3361
/*************************************************************************
3366
TRIO_ARGS4((reference, format, arglist, argarray),
3367
trio_reference_t *reference,
3368
TRIO_CONST char *format,
3370
trio_pointer_t *argarray)
3373
trio_parameter_t parameters[MAX_PARAMETERS];
3375
status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
3379
status = TrioFormatProcess(reference->data, format, parameters);
3380
if (reference->data->error != 0)
3382
status = reference->data->error;
3387
/*************************************************************************
3392
TRIO_ARGS6((destination, destinationSize, OutStream, format, arglist, argarray),
3393
trio_pointer_t destination,
3394
size_t destinationSize,
3395
void (*OutStream) TRIO_PROTO((trio_class_t *, int)),
3396
TRIO_CONST char *format,
3398
trio_pointer_t *argarray)
3402
trio_parameter_t parameters[MAX_PARAMETERS];
3404
assert(VALID(OutStream));
3405
assert(VALID(format));
3407
memset(&data, 0, sizeof(data));
3408
data.OutStream = OutStream;
3409
data.location = destination;
3410
data.max = destinationSize;
3413
#if defined(USE_LOCALE)
3414
if (NULL == internalLocaleValues)
3420
status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
3424
status = TrioFormatProcess(&data, format, parameters);
3425
if (data.error != 0)
3427
status = data.error;
3432
/*************************************************************************
3437
TRIO_ARGS2((self, output),
3443
assert(VALID(self));
3444
assert(VALID(self->location));
3446
file = (FILE *)self->location;
3448
if (fputc(output, file) == EOF)
3450
self->error = TRIO_ERROR_RETURN(TRIO_EOF, 0);
3458
/*************************************************************************
3459
* TrioOutStreamFileDescriptor
3462
TrioOutStreamFileDescriptor
3463
TRIO_ARGS2((self, output),
3470
assert(VALID(self));
3472
fd = *((int *)self->location);
3475
if (write(fd, &ch, sizeof(char)) == -1)
3477
self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
3485
/*************************************************************************
3486
* TrioOutStreamCustom
3490
TRIO_ARGS2((self, output),
3495
trio_custom_t *data;
3497
assert(VALID(self));
3498
assert(VALID(self->location));
3500
data = (trio_custom_t *)self->location;
3501
if (data->stream.out)
3503
status = (data->stream.out)(data->closure, output);
3510
if (self->error == 0)
3512
self->error = TRIO_ERROR_RETURN(TRIO_ECUSTOM, -status);
3519
/*************************************************************************
3520
* TrioOutStreamString
3524
TRIO_ARGS2((self, output),
3530
assert(VALID(self));
3531
assert(VALID(self->location));
3533
buffer = (char **)self->location;
3534
**buffer = (char)output;
3540
/*************************************************************************
3541
* TrioOutStreamStringMax
3544
TrioOutStreamStringMax
3545
TRIO_ARGS2((self, output),
3551
assert(VALID(self));
3552
assert(VALID(self->location));
3554
buffer = (char **)self->location;
3556
if (self->processed < self->max)
3558
**buffer = (char)output;
3565
/*************************************************************************
3566
* TrioOutStreamStringDynamic
3569
TrioOutStreamStringDynamic
3570
TRIO_ARGS2((self, output),
3574
assert(VALID(self));
3575
assert(VALID(self->location));
3577
if (self->error == 0)
3579
trio_xstring_append_char((trio_string_t *)self->location,
3583
/* The processed variable must always be increased */
3587
/*************************************************************************
3589
* Formatted printing functions
3591
************************************************************************/
3593
#if defined(TRIO_DOCUMENTATION)
3594
# include "doc/doc_printf.h"
3596
/** @addtogroup Printf
3600
/*************************************************************************
3605
Print to standard output stream.
3607
@param format Formatting string.
3608
@param ... Arguments.
3609
@return Number of printed characters.
3613
TRIO_VARGS2((format, va_alist),
3614
TRIO_CONST char *format,
3620
assert(VALID(format));
3622
TRIO_VA_START(args, format);
3623
status = TrioFormat(stdout, 0, TrioOutStreamFile, format, &args, NULL);
3629
Print to standard output stream.
3631
@param format Formatting string.
3632
@param args Arguments.
3633
@return Number of printed characters.
3637
TRIO_ARGS2((format, args),
3638
TRIO_CONST char *format,
3641
assert(VALID(format));
3643
return TrioFormat(stdout, 0, TrioOutStreamFile, format, &args, NULL);
3647
Print to standard output stream.
3649
@param format Formatting string.
3650
@param args Arguments.
3651
@return Number of printed characters.
3655
TRIO_ARGS2((format, args),
3656
TRIO_CONST char *format,
3657
trio_pointer_t * args)
3659
assert(VALID(format));
3661
return TrioFormat(stdout, 0, TrioOutStreamFile, format, NULL, args);
3664
/*************************************************************************
3671
@param file File pointer.
3672
@param format Formatting string.
3673
@param ... Arguments.
3674
@return Number of printed characters.
3678
TRIO_VARGS3((file, format, va_alist),
3680
TRIO_CONST char *format,
3686
assert(VALID(file));
3687
assert(VALID(format));
3689
TRIO_VA_START(args, format);
3690
status = TrioFormat(file, 0, TrioOutStreamFile, format, &args, NULL);
3698
@param file File pointer.
3699
@param format Formatting string.
3700
@param args Arguments.
3701
@return Number of printed characters.
3705
TRIO_ARGS3((file, format, args),
3707
TRIO_CONST char *format,
3710
assert(VALID(file));
3711
assert(VALID(format));
3713
return TrioFormat(file, 0, TrioOutStreamFile, format, &args, NULL);
3719
@param file File pointer.
3720
@param format Formatting string.
3721
@param args Arguments.
3722
@return Number of printed characters.
3726
TRIO_ARGS3((file, format, args),
3728
TRIO_CONST char *format,
3729
trio_pointer_t * args)
3731
assert(VALID(file));
3732
assert(VALID(format));
3734
return TrioFormat(file, 0, TrioOutStreamFile, format, NULL, args);
3737
/*************************************************************************
3742
Print to file descriptor.
3744
@param fd File descriptor.
3745
@param format Formatting string.
3746
@param ... Arguments.
3747
@return Number of printed characters.
3751
TRIO_VARGS3((fd, format, va_alist),
3753
TRIO_CONST char *format,
3759
assert(VALID(format));
3761
TRIO_VA_START(args, format);
3762
status = TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, &args, NULL);
3768
Print to file descriptor.
3770
@param fd File descriptor.
3771
@param format Formatting string.
3772
@param args Arguments.
3773
@return Number of printed characters.
3777
TRIO_ARGS3((fd, format, args),
3779
TRIO_CONST char *format,
3782
assert(VALID(format));
3784
return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, &args, NULL);
3788
Print to file descriptor.
3790
@param fd File descriptor.
3791
@param format Formatting string.
3792
@param args Arguments.
3793
@return Number of printed characters.
3797
TRIO_ARGS3((fd, format, args),
3799
TRIO_CONST char *format,
3800
trio_pointer_t *args)
3802
assert(VALID(format));
3804
return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, NULL, args);
3807
/*************************************************************************
3812
TRIO_VARGS4((stream, closure, format, va_alist),
3813
trio_outstream_t stream,
3814
trio_pointer_t closure,
3815
TRIO_CONST char *format,
3822
assert(VALID(stream));
3823
assert(VALID(format));
3825
TRIO_VA_START(args, format);
3826
data.stream.out = stream;
3827
data.closure = closure;
3828
status = TrioFormat(&data, 0, TrioOutStreamCustom, format, &args, NULL);
3835
TRIO_ARGS4((stream, closure, format, args),
3836
trio_outstream_t stream,
3837
trio_pointer_t closure,
3838
TRIO_CONST char *format,
3843
assert(VALID(stream));
3844
assert(VALID(format));
3846
data.stream.out = stream;
3847
data.closure = closure;
3848
return TrioFormat(&data, 0, TrioOutStreamCustom, format, &args, NULL);
3853
TRIO_ARGS4((stream, closure, format, args),
3854
trio_outstream_t stream,
3855
trio_pointer_t closure,
3856
TRIO_CONST char *format,
3861
assert(VALID(stream));
3862
assert(VALID(format));
3864
data.stream.out = stream;
3865
data.closure = closure;
3866
return TrioFormat(&data, 0, TrioOutStreamCustom, format, NULL, args);
3869
/*************************************************************************
3876
@param buffer Output string.
3877
@param format Formatting string.
3878
@param ... Arguments.
3879
@return Number of printed characters.
3883
TRIO_VARGS3((buffer, format, va_alist),
3885
TRIO_CONST char *format,
3891
assert(VALID(buffer));
3892
assert(VALID(format));
3894
TRIO_VA_START(args, format);
3895
status = TrioFormat(&buffer, 0, TrioOutStreamString, format, &args, NULL);
3896
*buffer = NIL; /* Terminate with NIL character */
3904
@param buffer Output string.
3905
@param format Formatting string.
3906
@param args Arguments.
3907
@return Number of printed characters.
3911
TRIO_ARGS3((buffer, format, args),
3913
TRIO_CONST char *format,
3918
assert(VALID(buffer));
3919
assert(VALID(format));
3921
status = TrioFormat(&buffer, 0, TrioOutStreamString, format, &args, NULL);
3929
@param buffer Output string.
3930
@param format Formatting string.
3931
@param args Arguments.
3932
@return Number of printed characters.
3936
TRIO_ARGS3((buffer, format, args),
3938
TRIO_CONST char *format,
3939
trio_pointer_t *args)
3943
assert(VALID(buffer));
3944
assert(VALID(format));
3946
status = TrioFormat(&buffer, 0, TrioOutStreamString, format, NULL, args);
3951
/*************************************************************************
3956
Print at most @p max characters to string.
3958
@param buffer Output string.
3959
@param max Maximum number of characters to print.
3960
@param format Formatting string.
3961
@param ... Arguments.
3962
@return Number of printed characters.
3966
TRIO_VARGS4((buffer, max, format, va_alist),
3969
TRIO_CONST char *format,
3975
assert(VALID(buffer));
3976
assert(VALID(format));
3978
TRIO_VA_START(args, format);
3979
status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
3980
TrioOutStreamStringMax, format, &args, NULL);
3988
Print at most @p max characters to string.
3990
@param buffer Output string.
3991
@param max Maximum number of characters to print.
3992
@param format Formatting string.
3993
@param args Arguments.
3994
@return Number of printed characters.
3998
TRIO_ARGS4((buffer, max, format, args),
4001
TRIO_CONST char *format,
4006
assert(VALID(buffer));
4007
assert(VALID(format));
4009
status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
4010
TrioOutStreamStringMax, format, &args, NULL);
4017
Print at most @p max characters to string.
4019
@param buffer Output string.
4020
@param max Maximum number of characters to print.
4021
@param format Formatting string.
4022
@param args Arguments.
4023
@return Number of printed characters.
4027
TRIO_ARGS4((buffer, max, format, args),
4030
TRIO_CONST char *format,
4031
trio_pointer_t *args)
4035
assert(VALID(buffer));
4036
assert(VALID(format));
4038
status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
4039
TrioOutStreamStringMax, format, NULL, args);
4045
/*************************************************************************
4047
* Appends the new string to the buffer string overwriting the '\0'
4048
* character at the end of buffer.
4052
TRIO_VARGS4((buffer, max, format, va_alist),
4055
TRIO_CONST char *format,
4062
TRIO_VA_START(args, format);
4064
assert(VALID(buffer));
4065
assert(VALID(format));
4067
buf_len = trio_length(buffer);
4068
buffer = &buffer[buf_len];
4070
status = TrioFormat(&buffer, max - 1 - buf_len,
4071
TrioOutStreamStringMax, format, &args, NULL);
4079
TRIO_ARGS4((buffer, max, format, args),
4082
TRIO_CONST char *format,
4088
assert(VALID(buffer));
4089
assert(VALID(format));
4091
buf_len = trio_length(buffer);
4092
buffer = &buffer[buf_len];
4093
status = TrioFormat(&buffer, max - 1 - buf_len,
4094
TrioOutStreamStringMax, format, &args, NULL);
4099
/*************************************************************************
4106
TRIO_VARGS2((format, va_alist),
4107
TRIO_CONST char *format,
4111
trio_string_t *info;
4112
char *result = NULL;
4114
assert(VALID(format));
4116
info = trio_xstring_duplicate("");
4119
TRIO_VA_START(args, format);
4120
(void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
4121
format, &args, NULL);
4124
trio_string_terminate(info);
4125
result = trio_string_extract(info);
4126
trio_string_destroy(info);
4134
TRIO_ARGS2((format, args),
4135
TRIO_CONST char *format,
4138
trio_string_t *info;
4139
char *result = NULL;
4141
assert(VALID(format));
4143
info = trio_xstring_duplicate("");
4146
(void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
4147
format, &args, NULL);
4148
trio_string_terminate(info);
4149
result = trio_string_extract(info);
4150
trio_string_destroy(info);
4157
TRIO_VARGS3((result, format, va_alist),
4159
TRIO_CONST char *format,
4164
trio_string_t *info;
4166
assert(VALID(format));
4170
info = trio_xstring_duplicate("");
4173
status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
4177
TRIO_VA_START(args, format);
4178
status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
4179
format, &args, NULL);
4183
trio_string_terminate(info);
4184
*result = trio_string_extract(info);
4186
trio_string_destroy(info);
4193
TRIO_ARGS3((result, format, args),
4195
TRIO_CONST char *format,
4199
trio_string_t *info;
4201
assert(VALID(format));
4205
info = trio_xstring_duplicate("");
4208
status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
4212
status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
4213
format, &args, NULL);
4216
trio_string_terminate(info);
4217
*result = trio_string_extract(info);
4219
trio_string_destroy(info);
4224
/** @} End of Printf documentation module */
4226
/*************************************************************************
4230
************************************************************************/
4232
#if defined(TRIO_DOCUMENTATION)
4233
# include "doc/doc_register.h"
4236
@addtogroup UserDefined
4242
/*************************************************************************
4247
Register new user-defined specifier.
4253
TRIO_PUBLIC trio_pointer_t
4255
TRIO_ARGS2((callback, name),
4256
trio_callback_t callback,
4257
TRIO_CONST char *name)
4259
trio_userdef_t *def;
4260
trio_userdef_t *prev = NULL;
4262
if (callback == NULL)
4267
/* Handle built-in namespaces */
4270
if (trio_equal(name, ":enter"))
4272
internalEnterCriticalRegion = callback;
4274
else if (trio_equal(name, ":leave"))
4276
internalLeaveCriticalRegion = callback;
4281
/* Bail out if namespace is too long */
4282
if (trio_length(name) >= MAX_USER_NAME)
4285
/* Bail out if namespace already is registered */
4286
def = TrioFindNamespace(name, &prev);
4291
def = (trio_userdef_t *)TRIO_MALLOC(sizeof(trio_userdef_t));
4294
if (internalEnterCriticalRegion)
4295
(void)internalEnterCriticalRegion(NULL);
4299
/* Link into internal list */
4301
internalUserDef = def;
4306
def->callback = callback;
4307
def->name = (name == NULL)
4309
: trio_duplicate(name);
4312
if (internalLeaveCriticalRegion)
4313
(void)internalLeaveCriticalRegion(NULL);
4315
return (trio_pointer_t)def;
4319
Unregister an existing user-defined specifier.
4325
TRIO_ARGS1((handle),
4326
trio_pointer_t handle)
4328
trio_userdef_t *self = (trio_userdef_t *)handle;
4329
trio_userdef_t *def;
4330
trio_userdef_t *prev = NULL;
4332
assert(VALID(self));
4336
def = TrioFindNamespace(self->name, &prev);
4339
if (internalEnterCriticalRegion)
4340
(void)internalEnterCriticalRegion(NULL);
4343
internalUserDef = NULL;
4345
prev->next = def->next;
4347
if (internalLeaveCriticalRegion)
4348
(void)internalLeaveCriticalRegion(NULL);
4350
trio_destroy(self->name);
4355
/*************************************************************************
4356
* trio_get_format [public]
4363
#if defined(FORMAT_USER_DEFINED)
4364
assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
4367
return (((trio_reference_t *)ref)->parameter->user_data);
4370
/*************************************************************************
4371
* trio_get_argument [public]
4378
#if defined(FORMAT_USER_DEFINED)
4379
assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
4382
return ((trio_reference_t *)ref)->parameter->data.pointer;
4385
/*************************************************************************
4386
* trio_get_width / trio_set_width [public]
4393
return ((trio_reference_t *)ref)->parameter->width;
4398
TRIO_ARGS2((ref, width),
4402
((trio_reference_t *)ref)->parameter->width = width;
4405
/*************************************************************************
4406
* trio_get_precision / trio_set_precision [public]
4413
return (((trio_reference_t *)ref)->parameter->precision);
4418
TRIO_ARGS2((ref, precision),
4422
((trio_reference_t *)ref)->parameter->precision = precision;
4425
/*************************************************************************
4426
* trio_get_base / trio_set_base [public]
4433
return (((trio_reference_t *)ref)->parameter->base);
4438
TRIO_ARGS2((ref, base),
4442
((trio_reference_t *)ref)->parameter->base = base;
4445
/*************************************************************************
4446
* trio_get_long / trio_set_long [public]
4453
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONG)
4460
TRIO_ARGS2((ref, is_long),
4465
((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONG;
4467
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONG;
4470
/*************************************************************************
4471
* trio_get_longlong / trio_set_longlong [public]
4478
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUAD)
4485
TRIO_ARGS2((ref, is_longlong),
4490
((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUAD;
4492
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUAD;
4495
/*************************************************************************
4496
* trio_get_longdouble / trio_set_longdouble [public]
4503
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONGDOUBLE)
4510
TRIO_ARGS2((ref, is_longdouble),
4515
((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONGDOUBLE;
4517
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONGDOUBLE;
4520
/*************************************************************************
4521
* trio_get_short / trio_set_short [public]
4528
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORT)
4535
TRIO_ARGS2((ref, is_short),
4540
((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORT;
4542
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORT;
4545
/*************************************************************************
4546
* trio_get_shortshort / trio_set_shortshort [public]
4553
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORTSHORT)
4560
TRIO_ARGS2((ref, is_shortshort),
4565
((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORTSHORT;
4567
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORTSHORT;
4570
/*************************************************************************
4571
* trio_get_alternative / trio_set_alternative [public]
4574
trio_get_alternative
4578
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_ALTERNATIVE)
4584
trio_set_alternative
4585
TRIO_ARGS2((ref, is_alternative),
4590
((trio_reference_t *)ref)->parameter->flags |= FLAGS_ALTERNATIVE;
4592
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_ALTERNATIVE;
4595
/*************************************************************************
4596
* trio_get_alignment / trio_set_alignment [public]
4603
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LEFTADJUST)
4610
TRIO_ARGS2((ref, is_leftaligned),
4615
((trio_reference_t *)ref)->parameter->flags |= FLAGS_LEFTADJUST;
4617
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LEFTADJUST;
4620
/*************************************************************************
4621
* trio_get_spacing /trio_set_spacing [public]
4628
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SPACE)
4635
TRIO_ARGS2((ref, is_space),
4640
((trio_reference_t *)ref)->parameter->flags |= FLAGS_SPACE;
4642
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SPACE;
4645
/*************************************************************************
4646
* trio_get_sign / trio_set_sign [public]
4653
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHOWSIGN)
4660
TRIO_ARGS2((ref, is_sign),
4665
((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHOWSIGN;
4667
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHOWSIGN;
4670
/*************************************************************************
4671
* trio_get_padding / trio_set_padding [public]
4678
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_NILPADDING)
4685
TRIO_ARGS2((ref, is_padding),
4690
((trio_reference_t *)ref)->parameter->flags |= FLAGS_NILPADDING;
4692
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_NILPADDING;
4695
/*************************************************************************
4696
* trio_get_quote / trio_set_quote [public]
4703
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUOTE)
4710
TRIO_ARGS2((ref, is_quote),
4715
((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUOTE;
4717
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUOTE;
4720
/*************************************************************************
4721
* trio_get_upper / trio_set_upper [public]
4728
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_UPPER)
4735
TRIO_ARGS2((ref, is_upper),
4740
((trio_reference_t *)ref)->parameter->flags |= FLAGS_UPPER;
4742
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_UPPER;
4745
/*************************************************************************
4746
* trio_get_largest / trio_set_largest [public]
4754
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_INTMAX_T)
4761
TRIO_ARGS2((ref, is_largest),
4766
((trio_reference_t *)ref)->parameter->flags |= FLAGS_INTMAX_T;
4768
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_INTMAX_T;
4772
/*************************************************************************
4773
* trio_get_ptrdiff / trio_set_ptrdiff [public]
4780
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_PTRDIFF_T)
4787
TRIO_ARGS2((ref, is_ptrdiff),
4792
((trio_reference_t *)ref)->parameter->flags |= FLAGS_PTRDIFF_T;
4794
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_PTRDIFF_T;
4797
/*************************************************************************
4798
* trio_get_size / trio_set_size [public]
4806
return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SIZE_T)
4813
TRIO_ARGS2((ref, is_size),
4818
((trio_reference_t *)ref)->parameter->flags |= FLAGS_SIZE_T;
4820
((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SIZE_T;
4824
/*************************************************************************
4825
* trio_print_int [public]
4829
TRIO_ARGS2((ref, number),
4833
trio_reference_t *self = (trio_reference_t *)ref;
4835
TrioWriteNumber(self->data,
4836
(trio_uintmax_t)number,
4837
self->parameter->flags,
4838
self->parameter->width,
4839
self->parameter->precision,
4840
self->parameter->base);
4843
/*************************************************************************
4844
* trio_print_uint [public]
4848
TRIO_ARGS2((ref, number),
4850
unsigned int number)
4852
trio_reference_t *self = (trio_reference_t *)ref;
4854
TrioWriteNumber(self->data,
4855
(trio_uintmax_t)number,
4856
self->parameter->flags | FLAGS_UNSIGNED,
4857
self->parameter->width,
4858
self->parameter->precision,
4859
self->parameter->base);
4862
/*************************************************************************
4863
* trio_print_double [public]
4867
TRIO_ARGS2((ref, number),
4871
trio_reference_t *self = (trio_reference_t *)ref;
4873
TrioWriteDouble(self->data,
4875
self->parameter->flags,
4876
self->parameter->width,
4877
self->parameter->precision,
4878
self->parameter->base);
4881
/*************************************************************************
4882
* trio_print_string [public]
4886
TRIO_ARGS2((ref, string),
4890
trio_reference_t *self = (trio_reference_t *)ref;
4892
TrioWriteString(self->data,
4894
self->parameter->flags,
4895
self->parameter->width,
4896
self->parameter->precision);
4899
/*************************************************************************
4900
* trio_print_ref [public]
4904
TRIO_VARGS3((ref, format, va_alist),
4906
TRIO_CONST char *format,
4912
assert(VALID(format));
4914
TRIO_VA_START(arglist, format);
4915
status = TrioFormatRef((trio_reference_t *)ref, format, &arglist, NULL);
4916
TRIO_VA_END(arglist);
4920
/*************************************************************************
4921
* trio_vprint_ref [public]
4925
TRIO_ARGS3((ref, format, arglist),
4927
TRIO_CONST char *format,
4930
assert(VALID(format));
4932
return TrioFormatRef((trio_reference_t *)ref, format, &arglist, NULL);
4935
/*************************************************************************
4936
* trio_printv_ref [public]
4940
TRIO_ARGS3((ref, format, argarray),
4942
TRIO_CONST char *format,
4943
trio_pointer_t *argarray)
4945
assert(VALID(format));
4947
return TrioFormatRef((trio_reference_t *)ref, format, NULL, argarray);
4950
#endif /* TRIO_EXTENSION */
4952
/*************************************************************************
4953
* trio_print_pointer [public]
4957
TRIO_ARGS2((ref, pointer),
4959
trio_pointer_t pointer)
4961
trio_reference_t *self = (trio_reference_t *)ref;
4963
trio_uintmax_t number;
4965
if (NULL == pointer)
4967
TRIO_CONST char *string = internalNullString;
4969
self->data->OutStream(self->data, *string++);
4974
* The subtraction of the null pointer is a workaround
4975
* to avoid a compiler warning. The performance overhead
4976
* is negligible (and likely to be removed by an
4977
* optimizing compiler). The (char *) casting is done
4978
* to please ANSI C++.
4980
number = (trio_uintmax_t)((char *)pointer - (char *)0);
4981
/* Shrink to size of pointer */
4982
number &= (trio_uintmax_t)-1;
4983
flags = self->parameter->flags;
4984
flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE |
4986
TrioWriteNumber(self->data,
4995
/** @} End of UserDefined documentation module */
4997
/*************************************************************************
5001
************************************************************************/
5003
/*************************************************************************
5004
* trio_locale_set_decimal_point
5006
* Decimal point can only be one character. The input argument is a
5007
* string to enable multibyte characters. At most MB_LEN_MAX characters
5011
trio_locale_set_decimal_point
5012
TRIO_ARGS1((decimalPoint),
5015
#if defined(USE_LOCALE)
5016
if (NULL == internalLocaleValues)
5021
internalDecimalPointLength = trio_length(decimalPoint);
5022
if (internalDecimalPointLength == 1)
5024
internalDecimalPoint = *decimalPoint;
5028
internalDecimalPoint = NIL;
5029
trio_copy_max(internalDecimalPointString,
5030
sizeof(internalDecimalPointString),
5035
/*************************************************************************
5036
* trio_locale_set_thousand_separator
5038
* See trio_locale_set_decimal_point
5041
trio_locale_set_thousand_separator
5042
TRIO_ARGS1((thousandSeparator),
5043
char *thousandSeparator)
5045
#if defined(USE_LOCALE)
5046
if (NULL == internalLocaleValues)
5051
trio_copy_max(internalThousandSeparator,
5052
sizeof(internalThousandSeparator),
5054
internalThousandSeparatorLength = trio_length(internalThousandSeparator);
5057
/*************************************************************************
5058
* trio_locale_set_grouping
5060
* Array of bytes. Reversed order.
5062
* CHAR_MAX : No further grouping
5063
* 0 : Repeat last group for the remaining digits (not necessary
5064
* as C strings are zero-terminated)
5065
* n : Set current group to n
5067
* Same order as the grouping attribute in LC_NUMERIC.
5070
trio_locale_set_grouping
5071
TRIO_ARGS1((grouping),
5074
#if defined(USE_LOCALE)
5075
if (NULL == internalLocaleValues)
5080
trio_copy_max(internalGrouping,
5081
sizeof(internalGrouping),
5086
/*************************************************************************
5090
************************************************************************/
5092
/*************************************************************************
5093
* TrioSkipWhitespaces
5105
self->InStream(self, &ch);
5110
/*************************************************************************
5115
TrioGetCollation(TRIO_NOARGS)
5123
/* This is computationally expensive */
5126
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5130
for (j = 0; j < MAX_CHARACTER_CLASS; j++)
5132
second[0] = (char)j;
5133
if (trio_equal_locale(first, second))
5134
internalCollationArray[i][k++] = (char)j;
5136
internalCollationArray[i][k] = NIL;
5141
/*************************************************************************
5142
* TrioGetCharacterClass
5148
TrioGetCharacterClass
5149
TRIO_ARGS4((format, indexPointer, flagsPointer, characterclass),
5150
TRIO_CONST char *format,
5152
trio_flags_t *flagsPointer,
5153
int *characterclass)
5155
int index = *indexPointer;
5161
*flagsPointer &= ~FLAGS_EXCLUDE;
5163
if (format[index] == QUALIFIER_CIRCUMFLEX)
5165
*flagsPointer |= FLAGS_EXCLUDE;
5169
* If the ungroup character is at the beginning of the scanlist,
5170
* it will be part of the class, and a second ungroup character
5171
* must follow to end the group.
5173
if (format[index] == SPECIFIER_UNGROUP)
5175
characterclass[(int)SPECIFIER_UNGROUP]++;
5179
* Minus is used to specify ranges. To include minus in the class,
5180
* it must be at the beginning of the list
5182
if (format[index] == QUALIFIER_MINUS)
5184
characterclass[(int)QUALIFIER_MINUS]++;
5187
/* Collect characters */
5188
for (ch = format[index];
5189
(ch != SPECIFIER_UNGROUP) && (ch != NIL);
5190
ch = format[++index])
5194
case QUALIFIER_MINUS: /* Scanlist ranges */
5197
* Both C99 and UNIX98 describes ranges as implementation-
5200
* We support the following behaviour (although this may
5201
* change as we become wiser)
5202
* - only increasing ranges, ie. [a-b] but not [b-a]
5203
* - transitive ranges, ie. [a-b-c] == [a-c]
5204
* - trailing minus, ie. [a-] is interpreted as an 'a'
5206
* - duplicates (although we can easily convert these
5209
range_begin = format[index - 1];
5210
range_end = format[++index];
5211
if (range_end == SPECIFIER_UNGROUP)
5213
/* Trailing minus is included */
5214
characterclass[(int)ch]++;
5218
if (range_end == NIL)
5219
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
5220
if (range_begin > range_end)
5221
return TRIO_ERROR_RETURN(TRIO_ERANGE, index);
5223
for (i = (int)range_begin; i <= (int)range_end; i++)
5224
characterclass[i]++;
5231
case SPECIFIER_GROUP:
5233
switch (format[index + 1])
5235
case QUALIFIER_DOT: /* Collating symbol */
5237
* FIXME: This will be easier to implement when multibyte
5238
* characters have been implemented. Until now, we ignore
5241
for (i = index + 2; ; i++)
5243
if (format[i] == NIL)
5244
/* Error in syntax */
5246
else if (format[i] == QUALIFIER_DOT)
5249
if (format[++i] != SPECIFIER_UNGROUP)
5255
case QUALIFIER_EQUAL: /* Equivalence class expressions */
5260
if (internalCollationUnconverted)
5262
/* Lazy evaluation of collation array */
5264
internalCollationUnconverted = FALSE;
5266
for (i = index + 2; ; i++)
5268
if (format[i] == NIL)
5269
/* Error in syntax */
5271
else if (format[i] == QUALIFIER_EQUAL)
5275
/* Mark any equivalent character */
5276
k = (unsigned int)format[i];
5277
for (j = 0; internalCollationArray[k][j] != NIL; j++)
5278
characterclass[(int)internalCollationArray[k][j]]++;
5281
if (format[++i] != SPECIFIER_UNGROUP)
5288
case QUALIFIER_COLON: /* Character class expressions */
5290
if (trio_equal_max(CLASS_ALNUM, sizeof(CLASS_ALNUM) - 1,
5293
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5295
characterclass[i]++;
5296
index += sizeof(CLASS_ALNUM) - 1;
5298
else if (trio_equal_max(CLASS_ALPHA, sizeof(CLASS_ALPHA) - 1,
5301
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5303
characterclass[i]++;
5304
index += sizeof(CLASS_ALPHA) - 1;
5306
else if (trio_equal_max(CLASS_CNTRL, sizeof(CLASS_CNTRL) - 1,
5309
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5311
characterclass[i]++;
5312
index += sizeof(CLASS_CNTRL) - 1;
5314
else if (trio_equal_max(CLASS_DIGIT, sizeof(CLASS_DIGIT) - 1,
5317
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5319
characterclass[i]++;
5320
index += sizeof(CLASS_DIGIT) - 1;
5322
else if (trio_equal_max(CLASS_GRAPH, sizeof(CLASS_GRAPH) - 1,
5325
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5327
characterclass[i]++;
5328
index += sizeof(CLASS_GRAPH) - 1;
5330
else if (trio_equal_max(CLASS_LOWER, sizeof(CLASS_LOWER) - 1,
5333
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5335
characterclass[i]++;
5336
index += sizeof(CLASS_LOWER) - 1;
5338
else if (trio_equal_max(CLASS_PRINT, sizeof(CLASS_PRINT) - 1,
5341
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5343
characterclass[i]++;
5344
index += sizeof(CLASS_PRINT) - 1;
5346
else if (trio_equal_max(CLASS_PUNCT, sizeof(CLASS_PUNCT) - 1,
5349
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5351
characterclass[i]++;
5352
index += sizeof(CLASS_PUNCT) - 1;
5354
else if (trio_equal_max(CLASS_SPACE, sizeof(CLASS_SPACE) - 1,
5357
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5359
characterclass[i]++;
5360
index += sizeof(CLASS_SPACE) - 1;
5362
else if (trio_equal_max(CLASS_UPPER, sizeof(CLASS_UPPER) - 1,
5365
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5367
characterclass[i]++;
5368
index += sizeof(CLASS_UPPER) - 1;
5370
else if (trio_equal_max(CLASS_XDIGIT, sizeof(CLASS_XDIGIT) - 1,
5373
for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5375
characterclass[i]++;
5376
index += sizeof(CLASS_XDIGIT) - 1;
5380
characterclass[(int)ch]++;
5385
characterclass[(int)ch]++;
5390
#endif /* TRIO_EXTENSION */
5393
characterclass[(int)ch]++;
5400
/*************************************************************************
5403
* We implement our own number conversion in preference of strtol and
5404
* strtoul, because we must handle 'long long' and thousand separators.
5406
TRIO_PRIVATE BOOLEAN_T
5408
TRIO_ARGS5((self, target, flags, width, base),
5410
trio_uintmax_t *target,
5415
trio_uintmax_t number = 0;
5418
BOOLEAN_T isNegative = FALSE;
5419
BOOLEAN_T gotNumber = FALSE;
5422
assert(VALID(self));
5423
assert(VALID(self->InStream));
5424
assert((base >= MIN_BASE && base <= MAX_BASE) || (base == NO_BASE));
5426
if (internalDigitsUnconverted)
5428
/* Lazy evaluation of digits array */
5429
memset(internalDigitArray, -1, sizeof(internalDigitArray));
5430
for (j = 0; j < (int)sizeof(internalDigitsLower) - 1; j++)
5432
internalDigitArray[(int)internalDigitsLower[j]] = j;
5433
internalDigitArray[(int)internalDigitsUpper[j]] = j;
5435
internalDigitsUnconverted = FALSE;
5438
TrioSkipWhitespaces(self);
5440
if (!(flags & FLAGS_UNSIGNED))
5443
if (self->current == '+')
5445
self->InStream(self, NULL);
5447
else if (self->current == '-')
5449
self->InStream(self, NULL);
5454
count = self->processed;
5456
if (flags & FLAGS_ALTERNATIVE)
5464
if (self->current == '0')
5466
self->InStream(self, NULL);
5469
if ((base == BASE_HEX) &&
5470
(trio_to_upper(self->current) == 'X'))
5472
self->InStream(self, NULL);
5474
else if ((base == BASE_BINARY) &&
5475
(trio_to_upper(self->current) == 'B'))
5477
self->InStream(self, NULL);
5489
while (((width == NO_WIDTH) || (self->processed - count < width)) &&
5490
(! ((self->current == EOF) || isspace(self->current))))
5492
if (isascii(self->current))
5494
digit = internalDigitArray[self->current];
5495
/* Abort if digit is not allowed in the specified base */
5496
if ((digit == -1) || (digit >= base))
5499
else if (flags & FLAGS_QUOTE)
5501
/* Compare with thousands separator */
5502
for (j = 0; internalThousandSeparator[j] && self->current; j++)
5504
if (internalThousandSeparator[j] != self->current)
5507
self->InStream(self, NULL);
5509
if (internalThousandSeparator[j])
5510
break; /* Mismatch */
5512
continue; /* Match */
5519
gotNumber = TRUE; /* we need at least one digit */
5521
self->InStream(self, NULL);
5524
/* Was anything read at all? */
5529
*target = (isNegative) ? -((trio_intmax_t)number) : number;
5533
/*************************************************************************
5538
TRIO_ARGS4((self, target, flags, width),
5546
trio_uintmax_t number;
5548
assert(VALID(self));
5549
assert(VALID(self->InStream));
5552
(self->current != EOF) && (i < width);
5555
ch = (char)self->current;
5556
self->InStream(self, NULL);
5557
if ((flags & FLAGS_ALTERNATIVE) && (ch == CHAR_BACKSLASH))
5559
switch (self->current)
5561
case '\\': ch = '\\'; break;
5562
case 'a': ch = '\007'; break;
5563
case 'b': ch = '\b'; break;
5564
case 'f': ch = '\f'; break;
5565
case 'n': ch = '\n'; break;
5566
case 'r': ch = '\r'; break;
5567
case 't': ch = '\t'; break;
5568
case 'v': ch = '\v'; break;
5570
if (isdigit(self->current))
5572
/* Read octal number */
5573
if (!TrioReadNumber(self, &number, 0, 3, BASE_OCTAL))
5577
else if (trio_to_upper(self->current) == 'X')
5579
/* Read hexadecimal number */
5580
self->InStream(self, NULL);
5581
if (!TrioReadNumber(self, &number, 0, 2, BASE_HEX))
5587
ch = (char)self->current;
5599
/*************************************************************************
5602
TRIO_PRIVATE BOOLEAN_T
5604
TRIO_ARGS4((self, target, flags, width),
5612
assert(VALID(self));
5613
assert(VALID(self->InStream));
5615
TrioSkipWhitespaces(self);
5618
* Continue until end of string is reached, a whitespace is encountered,
5619
* or width is exceeded
5622
((width == NO_WIDTH) || (i < width)) &&
5623
(! ((self->current == EOF) || isspace(self->current)));
5626
if (TrioReadChar(self, (target ? &target[i] : 0), flags, 1) == 0)
5634
/*************************************************************************
5640
TRIO_ARGS4((self, target, flags, width),
5642
trio_wchar_t *target,
5651
char buffer[MB_LEN_MAX + 1];
5653
assert(VALID(self));
5654
assert(VALID(self->InStream));
5657
(self->current != EOF) && (i < width);
5660
if (isascii(self->current))
5662
if (TrioReadChar(self, buffer, flags, 1) == 0)
5669
* Collect a multibyte character, by enlarging buffer until
5670
* it contains a fully legal multibyte character, or the
5676
buffer[j++] = (char)self->current;
5678
self->InStream(self, NULL);
5680
while ((j < (int)sizeof(buffer)) && (mblen(buffer, (size_t)j) != j));
5684
size = mbtowc(&wch, buffer, sizeof(buffer));
5689
self->InStream(self, NULL);
5693
#endif /* TRIO_WIDECHAR */
5695
/*************************************************************************
5696
* TrioReadWideString
5699
TRIO_PRIVATE BOOLEAN_T
5701
TRIO_ARGS4((self, target, flags, width),
5703
trio_wchar_t *target,
5710
assert(VALID(self));
5711
assert(VALID(self->InStream));
5713
TrioSkipWhitespaces(self);
5715
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
5716
(void)mblen(NULL, 0);
5720
* Continue until end of string is reached, a whitespace is encountered,
5721
* or width is exceeded
5724
((width == NO_WIDTH) || (i < width)) &&
5725
(! ((self->current == EOF) || isspace(self->current)));
5728
size = TrioReadWideChar(self, &target[i], flags, 1);
5735
target[i] = WCONST('\0');
5738
#endif /* TRIO_WIDECHAR */
5740
/*************************************************************************
5743
* FIXME: characterclass does not work with multibyte characters
5745
TRIO_PRIVATE BOOLEAN_T
5747
TRIO_ARGS5((self, target, characterclass, flags, width),
5750
int *characterclass,
5757
assert(VALID(self));
5758
assert(VALID(self->InStream));
5762
((width == NO_WIDTH) || (i < width)) &&
5764
(((flags & FLAGS_EXCLUDE) != 0) ^ (characterclass[ch] == 0))));
5768
target[i] = (char)ch;
5769
self->InStream(self, &ch);
5777
/*************************************************************************
5784
TRIO_PRIVATE BOOLEAN_T
5786
TRIO_ARGS4((self, target, flags, width),
5788
trio_pointer_t target,
5793
char doubleString[512];
5797
BOOLEAN_T isHex = FALSE;
5799
doubleString[0] = 0;
5801
if ((width == NO_WIDTH) || (width > (int)sizeof(doubleString) - 1))
5802
width = sizeof(doubleString) - 1;
5804
TrioSkipWhitespaces(self);
5807
* Read entire double number from stream. trio_to_double requires
5808
* a string as input, but InStream can be anything, so we have to
5809
* collect all characters.
5812
if ((ch == '+') || (ch == '-'))
5814
doubleString[index++] = (char)ch;
5815
self->InStream(self, &ch);
5831
while (isalpha(ch) && (index - start < width))
5833
doubleString[index++] = (char)ch;
5834
self->InStream(self, &ch);
5836
doubleString[index] = NIL;
5838
/* Case insensitive string comparison */
5839
if (trio_equal(&doubleString[start], INFINITE_UPPER) ||
5840
trio_equal(&doubleString[start], LONG_INFINITE_UPPER))
5842
if (flags & FLAGS_LONGDOUBLE)
5844
if ((start == 1) && (doubleString[0] == '-'))
5846
*((trio_long_double_t *)target) = trio_ninf();
5850
*((trio_long_double_t *)target) = trio_pinf();
5855
if ((start == 1) && (doubleString[0] == '-'))
5857
*((double *)target) = trio_ninf();
5861
*((double *)target) = trio_pinf();
5866
if (trio_equal(doubleString, NAN_UPPER))
5868
/* NaN must not have a preceeding + nor - */
5869
if (flags & FLAGS_LONGDOUBLE)
5871
*((trio_long_double_t *)target) = trio_nan();
5875
*((double *)target) = trio_nan();
5882
doubleString[index++] = (char)ch;
5883
self->InStream(self, &ch);
5884
if (trio_to_upper(ch) == 'X')
5887
doubleString[index++] = (char)ch;
5888
self->InStream(self, &ch);
5896
while ((ch != EOF) && (index - start < width))
5899
if (isHex ? isxdigit(ch) : isdigit(ch))
5901
doubleString[index++] = (char)ch;
5902
self->InStream(self, &ch);
5904
else if (flags & FLAGS_QUOTE)
5906
/* Compare with thousands separator */
5907
for (j = 0; internalThousandSeparator[j] && self->current; j++)
5909
if (internalThousandSeparator[j] != self->current)
5912
self->InStream(self, &ch);
5914
if (internalThousandSeparator[j])
5915
break; /* Mismatch */
5917
continue; /* Match */
5925
doubleString[index++] = (char)ch;
5926
self->InStream(self, &ch);
5927
while ((isHex ? isxdigit(ch) : isdigit(ch)) &&
5928
(index - start < width))
5930
doubleString[index++] = (char)ch;
5931
self->InStream(self, &ch);
5933
if (isHex ? (trio_to_upper(ch) == 'P') : (trio_to_upper(ch) == 'E'))
5936
doubleString[index++] = (char)ch;
5937
self->InStream(self, &ch);
5938
if ((ch == '+') || (ch == '-'))
5940
doubleString[index++] = (char)ch;
5941
self->InStream(self, &ch);
5943
while (isdigit(ch) && (index - start < width))
5945
doubleString[index++] = (char)ch;
5946
self->InStream(self, &ch);
5951
if ((index == start) || (*doubleString == NIL))
5954
doubleString[index] = 0;
5956
if (flags & FLAGS_LONGDOUBLE)
5958
*((trio_long_double_t *)target) = trio_to_long_double(doubleString, NULL);
5962
*((double *)target) = trio_to_double(doubleString, NULL);
5967
/*************************************************************************
5970
TRIO_PRIVATE BOOLEAN_T
5972
TRIO_ARGS3((self, target, flags),
5974
trio_pointer_t *target,
5977
trio_uintmax_t number;
5978
char buffer[sizeof(internalNullString)];
5980
flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE | FLAGS_NILPADDING);
5982
if (TrioReadNumber(self,
5989
* The strange assignment of number is a workaround for a compiler
5993
*target = (char *)0 + number;
5996
else if (TrioReadString(self,
5997
(flags & FLAGS_IGNORE)
6001
sizeof(internalNullString) - 1))
6003
if (trio_equal_case(buffer, internalNullString))
6013
/*************************************************************************
6018
TRIO_ARGS3((data, format, parameters),
6020
TRIO_CONST char *format,
6021
trio_parameter_t *parameters)
6023
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6029
int index; /* Index of format string */
6030
int i; /* Index of current parameter */
6034
trio_pointer_t pointer;
6039
data->InStream(data, &ch);
6041
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6042
(void)mblen(NULL, 0);
6045
while (format[index])
6047
#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
6048
if (! isascii(format[index]))
6050
charlen = mblen(&format[index], MB_LEN_MAX);
6053
/* Compare multibyte characters in format string */
6054
for (cnt = 0; cnt < charlen - 1; cnt++)
6056
if (ch != format[index + cnt])
6058
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6060
data->InStream(data, &ch);
6062
continue; /* while characters left in formatting string */
6065
#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
6067
if ((EOF == ch) && (parameters[i].type != FORMAT_COUNT))
6069
return (assignment > 0) ? assignment : EOF;
6072
if (CHAR_IDENTIFIER == format[index])
6074
if (CHAR_IDENTIFIER == format[index + 1])
6076
/* Two % in format matches one % in input stream */
6077
if (CHAR_IDENTIFIER == ch)
6079
data->InStream(data, &ch);
6081
continue; /* while format chars left */
6084
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6087
/* Skip the parameter entries */
6088
while (parameters[i].type == FORMAT_PARAMETER)
6091
flags = parameters[i].flags;
6093
width = parameters[i].width;
6094
if (flags & FLAGS_WIDTH_PARAMETER)
6096
/* Get width from parameter list */
6097
width = (int)parameters[width].data.number.as_signed;
6100
base = parameters[i].base;
6101
if (flags & FLAGS_BASE_PARAMETER)
6103
/* Get base from parameter list */
6104
base = (int)parameters[base].data.number.as_signed;
6107
switch (parameters[i].type)
6111
trio_uintmax_t number;
6114
base = BASE_DECIMAL;
6116
if (!TrioReadNumber(data,
6123
if (!(flags & FLAGS_IGNORE))
6127
pointer = parameters[i].data.pointer;
6128
#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6129
if (flags & FLAGS_SIZE_T)
6130
*(size_t *)pointer = (size_t)number;
6133
#if defined(QUALIFIER_PTRDIFF_T)
6134
if (flags & FLAGS_PTRDIFF_T)
6135
*(ptrdiff_t *)pointer = (ptrdiff_t)number;
6138
#if defined(QUALIFIER_INTMAX_T)
6139
if (flags & FLAGS_INTMAX_T)
6140
*(trio_intmax_t *)pointer = (trio_intmax_t)number;
6143
if (flags & FLAGS_QUAD)
6144
*(trio_ulonglong_t *)pointer = (trio_ulonglong_t)number;
6145
else if (flags & FLAGS_LONG)
6146
*(long int *)pointer = (long int)number;
6147
else if (flags & FLAGS_SHORT)
6148
*(short int *)pointer = (short int)number;
6150
*(int *)pointer = (int)number;
6153
break; /* FORMAT_INT */
6157
if (flags & FLAGS_WIDECHAR)
6159
if (!TrioReadWideString(data,
6160
(flags & FLAGS_IGNORE)
6162
: parameters[i].data.wstring,
6170
if (!TrioReadString(data,
6171
(flags & FLAGS_IGNORE)
6173
: parameters[i].data.string,
6178
if (!(flags & FLAGS_IGNORE))
6180
break; /* FORMAT_STRING */
6184
trio_pointer_t pointer;
6186
if (flags & FLAGS_IGNORE)
6192
pointer = (flags & FLAGS_LONGDOUBLE)
6193
? (trio_pointer_t)parameters[i].data.longdoublePointer
6194
: (trio_pointer_t)parameters[i].data.doublePointer;
6196
if (!TrioReadDouble(data, pointer, flags, width))
6200
if (!(flags & FLAGS_IGNORE))
6204
break; /* FORMAT_DOUBLE */
6208
int characterclass[MAX_CHARACTER_CLASS + 1];
6211
/* Skip over modifiers */
6212
while (format[index] != SPECIFIER_GROUP)
6216
/* Skip over group specifier */
6219
memset(characterclass, 0, sizeof(characterclass));
6220
rc = TrioGetCharacterClass(format,
6227
if (!TrioReadGroup(data,
6228
(flags & FLAGS_IGNORE)
6230
: parameters[i].data.string,
6233
parameters[i].width))
6235
if (!(flags & FLAGS_IGNORE))
6238
break; /* FORMAT_GROUP */
6241
pointer = parameters[i].data.pointer;
6242
if (NULL != pointer)
6244
int count = data->committed;
6246
count--; /* a character is read, but is not consumed yet */
6247
#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6248
if (flags & FLAGS_SIZE_T)
6249
*(size_t *)pointer = (size_t)count;
6252
#if defined(QUALIFIER_PTRDIFF_T)
6253
if (flags & FLAGS_PTRDIFF_T)
6254
*(ptrdiff_t *)pointer = (ptrdiff_t)count;
6257
#if defined(QUALIFIER_INTMAX_T)
6258
if (flags & FLAGS_INTMAX_T)
6259
*(trio_intmax_t *)pointer = (trio_intmax_t)count;
6262
if (flags & FLAGS_QUAD)
6264
*(trio_ulonglong_t *)pointer = (trio_ulonglong_t)count;
6266
else if (flags & FLAGS_LONG)
6268
*(long int *)pointer = (long int)count;
6270
else if (flags & FLAGS_SHORT)
6272
*(short int *)pointer = (short int)count;
6276
*(int *)pointer = (int)count;
6279
break; /* FORMAT_COUNT */
6283
if (flags & FLAGS_WIDECHAR)
6285
if (TrioReadWideChar(data,
6286
(flags & FLAGS_IGNORE)
6288
: parameters[i].data.wstring,
6290
(width == NO_WIDTH) ? 1 : width) == 0)
6296
if (TrioReadChar(data,
6297
(flags & FLAGS_IGNORE)
6299
: parameters[i].data.string,
6301
(width == NO_WIDTH) ? 1 : width) == 0)
6304
if (!(flags & FLAGS_IGNORE))
6306
break; /* FORMAT_CHAR */
6308
case FORMAT_POINTER:
6309
if (!TrioReadPointer(data,
6310
(flags & FLAGS_IGNORE)
6312
: (trio_pointer_t *)parameters[i].data.pointer,
6315
if (!(flags & FLAGS_IGNORE))
6317
break; /* FORMAT_POINTER */
6319
case FORMAT_PARAMETER:
6320
break; /* FORMAT_PARAMETER */
6323
return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6326
index = parameters[i].indexAfterSpecifier;
6329
else /* Not an % identifier */
6331
if (isspace((int)format[index]))
6333
/* Whitespaces may match any amount of whitespaces */
6334
ch = TrioSkipWhitespaces(data);
6336
else if (ch == format[index])
6338
data->InStream(data, &ch);
6349
/*************************************************************************
6354
TRIO_ARGS6((source, sourceSize, InStream, format, arglist, argarray),
6355
trio_pointer_t source,
6357
void (*InStream) TRIO_PROTO((trio_class_t *, int *)),
6358
TRIO_CONST char *format,
6360
trio_pointer_t *argarray)
6363
trio_parameter_t parameters[MAX_PARAMETERS];
6366
assert(VALID(InStream));
6367
assert(VALID(format));
6369
memset(&data, 0, sizeof(data));
6370
data.InStream = InStream;
6371
data.location = (trio_pointer_t)source;
6372
data.max = sourceSize;
6375
#if defined(USE_LOCALE)
6376
if (NULL == internalLocaleValues)
6382
status = TrioParse(TYPE_SCAN, format, parameters, arglist, argarray);
6386
status = TrioScanProcess(&data, format, parameters);
6387
if (data.error != 0)
6389
status = data.error;
6394
/*************************************************************************
6399
TRIO_ARGS2((self, intPointer),
6403
FILE *file = (FILE *)self->location;
6405
assert(VALID(self));
6406
assert(VALID(file));
6408
self->current = fgetc(file);
6409
if (self->current == EOF)
6411
self->error = (ferror(file))
6412
? TRIO_ERROR_RETURN(TRIO_ERRNO, 0)
6413
: TRIO_ERROR_RETURN(TRIO_EOF, 0);
6421
if (VALID(intPointer))
6423
*intPointer = self->current;
6427
/*************************************************************************
6428
* TrioInStreamFileDescriptor
6431
TrioInStreamFileDescriptor
6432
TRIO_ARGS2((self, intPointer),
6436
int fd = *((int *)self->location);
6438
unsigned char input;
6440
assert(VALID(self));
6442
size = read(fd, &input, sizeof(char));
6445
self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
6446
self->current = EOF;
6450
self->current = (size == 0) ? EOF : input;
6452
if (self->current != EOF)
6458
if (VALID(intPointer))
6460
*intPointer = self->current;
6464
/*************************************************************************
6465
* TrioInStreamCustom
6469
TRIO_ARGS2((self, intPointer),
6473
trio_custom_t *data;
6475
assert(VALID(self));
6476
assert(VALID(self->location));
6478
data = (trio_custom_t *)self->location;
6480
self->current = (data->stream.in == NULL)
6482
: (data->stream.in)(data->closure);
6484
if (self->current == NIL)
6486
self->current = EOF;
6494
if (VALID(intPointer))
6496
*intPointer = self->current;
6500
/*************************************************************************
6501
* TrioInStreamString
6505
TRIO_ARGS2((self, intPointer),
6509
unsigned char **buffer;
6511
assert(VALID(self));
6512
assert(VALID(self->location));
6514
buffer = (unsigned char **)self->location;
6515
self->current = (*buffer)[0];
6516
if (self->current == NIL)
6518
self->current = EOF;
6527
if (VALID(intPointer))
6529
*intPointer = self->current;
6533
/*************************************************************************
6535
* Formatted scanning functions
6537
************************************************************************/
6539
#if defined(TRIO_DOCUMENTATION)
6540
# include "doc/doc_scanf.h"
6542
/** @addtogroup Scanf
6546
/*************************************************************************
6551
Scan characters from standard input stream.
6553
@param format Formatting string.
6554
@param ... Arguments.
6555
@return Number of scanned characters.
6559
TRIO_VARGS2((format, va_alist),
6560
TRIO_CONST char *format,
6566
assert(VALID(format));
6568
TRIO_VA_START(args, format);
6569
status = TrioScan((trio_pointer_t)stdin, 0,
6571
format, &args, NULL);
6578
TRIO_ARGS2((format, args),
6579
TRIO_CONST char *format,
6582
assert(VALID(format));
6584
return TrioScan((trio_pointer_t)stdin, 0,
6586
format, &args, NULL);
6591
TRIO_ARGS2((format, args),
6592
TRIO_CONST char *format,
6593
trio_pointer_t *args)
6595
assert(VALID(format));
6597
return TrioScan((trio_pointer_t)stdin, 0,
6599
format, NULL, args);
6602
/*************************************************************************
6607
TRIO_VARGS3((file, format, va_alist),
6609
TRIO_CONST char *format,
6615
assert(VALID(file));
6616
assert(VALID(format));
6618
TRIO_VA_START(args, format);
6619
status = TrioScan((trio_pointer_t)file, 0,
6621
format, &args, NULL);
6628
TRIO_ARGS3((file, format, args),
6630
TRIO_CONST char *format,
6633
assert(VALID(file));
6634
assert(VALID(format));
6636
return TrioScan((trio_pointer_t)file, 0,
6638
format, &args, NULL);
6643
TRIO_ARGS3((file, format, args),
6645
TRIO_CONST char *format,
6646
trio_pointer_t *args)
6648
assert(VALID(file));
6649
assert(VALID(format));
6651
return TrioScan((trio_pointer_t)file, 0,
6653
format, NULL, args);
6656
/*************************************************************************
6661
TRIO_VARGS3((fd, format, va_alist),
6663
TRIO_CONST char *format,
6669
assert(VALID(format));
6671
TRIO_VA_START(args, format);
6672
status = TrioScan((trio_pointer_t)&fd, 0,
6673
TrioInStreamFileDescriptor,
6674
format, &args, NULL);
6681
TRIO_ARGS3((fd, format, args),
6683
TRIO_CONST char *format,
6686
assert(VALID(format));
6688
return TrioScan((trio_pointer_t)&fd, 0,
6689
TrioInStreamFileDescriptor,
6690
format, &args, NULL);
6695
TRIO_ARGS3((fd, format, args),
6697
TRIO_CONST char *format,
6698
trio_pointer_t *args)
6700
assert(VALID(format));
6702
return TrioScan((trio_pointer_t)&fd, 0,
6703
TrioInStreamFileDescriptor,
6704
format, NULL, args);
6707
/*************************************************************************
6712
TRIO_VARGS4((stream, closure, format, va_alist),
6713
trio_instream_t stream,
6714
trio_pointer_t closure,
6715
TRIO_CONST char *format,
6722
assert(VALID(stream));
6723
assert(VALID(format));
6725
TRIO_VA_START(args, format);
6726
data.stream.in = stream;
6727
data.closure = closure;
6728
status = TrioScan(&data, 0, TrioInStreamCustom, format, &args, NULL);
6735
TRIO_ARGS4((stream, closure, format, args),
6736
trio_instream_t stream,
6737
trio_pointer_t closure,
6738
TRIO_CONST char *format,
6743
assert(VALID(stream));
6744
assert(VALID(format));
6746
data.stream.in = stream;
6747
data.closure = closure;
6748
return TrioScan(&data, 0, TrioInStreamCustom, format, &args, NULL);
6753
TRIO_ARGS4((stream, closure, format, args),
6754
trio_instream_t stream,
6755
trio_pointer_t closure,
6756
TRIO_CONST char *format,
6757
trio_pointer_t *args)
6761
assert(VALID(stream));
6762
assert(VALID(format));
6764
data.stream.in = stream;
6765
data.closure = closure;
6766
return TrioScan(&data, 0, TrioInStreamCustom, format, NULL, args);
6769
/*************************************************************************
6774
TRIO_VARGS3((buffer, format, va_alist),
6775
TRIO_CONST char *buffer,
6776
TRIO_CONST char *format,
6782
assert(VALID(buffer));
6783
assert(VALID(format));
6785
TRIO_VA_START(args, format);
6786
status = TrioScan((trio_pointer_t)&buffer, 0,
6788
format, &args, NULL);
6795
TRIO_ARGS3((buffer, format, args),
6796
TRIO_CONST char *buffer,
6797
TRIO_CONST char *format,
6800
assert(VALID(buffer));
6801
assert(VALID(format));
6803
return TrioScan((trio_pointer_t)&buffer, 0,
6805
format, &args, NULL);
6810
TRIO_ARGS3((buffer, format, args),
6811
TRIO_CONST char *buffer,
6812
TRIO_CONST char *format,
6813
trio_pointer_t *args)
6815
assert(VALID(buffer));
6816
assert(VALID(format));
6818
return TrioScan((trio_pointer_t)&buffer, 0,
6820
format, NULL, args);
6823
/** @} End of Scanf documentation module */
6825
/*************************************************************************
6828
TRIO_PUBLIC TRIO_CONST char *
6830
TRIO_ARGS1((errorcode),
6833
/* Textual versions of the error codes */
6834
switch (TRIO_ERROR_CODE(errorcode))
6837
return "End of file";
6839
return "Invalid argument";
6841
return "Too many arguments";
6843
return "Double reference";
6845
return "Reference gap";
6847
return "Out of memory";
6849
return "Invalid range";
6851
return "Custom error";