Add missing files in libxml2 to support xpath and xslt.
This is consistent with libxml2 version 2.7.3. Apparently these files are
omitted somehow in previous downstreaming.
Issue: 2582105
Change-Id: If5699d4d58c1e60f91b7ebc97c47dea4074aea28
diff --git a/trio.c b/trio.c
new file mode 100644
index 0000000..3fac2e6
--- /dev/null
+++ b/trio.c
@@ -0,0 +1,6869 @@
+/*************************************************************************
+ *
+ * $Id: trio.c 3600 2007-04-17 12:44:58Z veillard $
+ *
+ * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ *************************************************************************
+ *
+ * A note to trio contributors:
+ *
+ * Avoid heap allocation at all costs to ensure that the trio functions
+ * are async-safe. The exceptions are the printf/fprintf functions, which
+ * uses fputc, and the asprintf functions and the <alloc> modifier, which
+ * by design are required to allocate form the heap.
+ *
+ ************************************************************************/
+
+/*
+ * TODO:
+ * - Scan is probably too permissive about its modifiers.
+ * - C escapes in %#[] ?
+ * - Multibyte characters (done for format parsing, except scan groups)
+ * - Complex numbers? (C99 _Complex)
+ * - Boolean values? (C99 _Bool)
+ * - C99 NaN(n-char-sequence) missing. The n-char-sequence can be used
+ * to print the mantissa, e.g. NaN(0xc000000000000000)
+ * - Should we support the GNU %a alloc modifier? GNU has an ugly hack
+ * for %a, because C99 used %a for other purposes. If specified as
+ * %as or %a[ it is interpreted as the alloc modifier, otherwise as
+ * the C99 hex-float. This means that you cannot scan %as as a hex-float
+ * immediately followed by an 's'.
+ * - Scanning of collating symbols.
+ */
+
+/*************************************************************************
+ * Trio include files
+ */
+#include "triodef.h"
+#include "trio.h"
+#include "triop.h"
+#include "trionan.h"
+#if !defined(TRIO_MINIMAL)
+# include "triostr.h"
+#endif
+
+/**************************************************************************
+ *
+ * Definitions
+ *
+ *************************************************************************/
+
+#include <math.h>
+#include <limits.h>
+#include <float.h>
+
+#if (defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) \
+ || defined(USE_MULTIBYTE) || TRIO_WIDECHAR) \
+ && !defined(_WIN32_WCE)
+# define TRIO_COMPILER_SUPPORTS_MULTIBYTE
+# if !defined(MB_LEN_MAX)
+# define MB_LEN_MAX 6
+# endif
+#endif
+
+#if (defined(TRIO_COMPILER_MSVC) && (_MSC_VER >= 1100)) || defined(TRIO_COMPILER_BCB)
+# define TRIO_COMPILER_SUPPORTS_MSVC_INT
+#endif
+
+#if defined(_WIN32_WCE)
+#include <wincecompat.h>
+#endif
+
+/*************************************************************************
+ * Generic definitions
+ */
+
+#if !(defined(DEBUG) || defined(NDEBUG))
+# define NDEBUG
+#endif
+
+#include <assert.h>
+#include <ctype.h>
+#if !defined(TRIO_COMPILER_SUPPORTS_C99)
+# define isblank(x) (((x)==32) || ((x)==9))
+#endif
+#if defined(TRIO_COMPILER_ANCIENT)
+# include <varargs.h>
+#else
+# include <stdarg.h>
+#endif
+#include <stddef.h>
+
+#ifdef HAVE_ERRNO_H
+#include <errno.h>
+#endif
+
+#ifndef NULL
+# define NULL 0
+#endif
+#define NIL ((char)0)
+#ifndef FALSE
+# define FALSE (1 == 0)
+# define TRUE (! FALSE)
+#endif
+#define BOOLEAN_T int
+
+/* mincore() can be used for debugging purposes */
+#define VALID(x) (NULL != (x))
+
+#if TRIO_ERRORS
+ /*
+ * Encode the error code and the position. This is decoded
+ * with TRIO_ERROR_CODE and TRIO_ERROR_POSITION.
+ */
+# define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
+#else
+# define TRIO_ERROR_RETURN(x,y) (-1)
+#endif
+
+typedef unsigned long trio_flags_t;
+
+
+/*************************************************************************
+ * Platform specific definitions
+ */
+#if defined(TRIO_PLATFORM_UNIX)
+# include <unistd.h>
+# include <signal.h>
+# include <locale.h>
+# define USE_LOCALE
+#endif /* TRIO_PLATFORM_UNIX */
+#if defined(TRIO_PLATFORM_VMS)
+# include <unistd.h>
+#endif
+#if defined(TRIO_PLATFORM_WIN32)
+# if defined(_WIN32_WCE)
+# include <wincecompat.h>
+# else
+# include <io.h>
+# define read _read
+# define write _write
+# endif
+#endif /* TRIO_PLATFORM_WIN32 */
+
+#if TRIO_WIDECHAR
+# if defined(TRIO_COMPILER_SUPPORTS_ISO94)
+# include <wchar.h>
+# include <wctype.h>
+typedef wchar_t trio_wchar_t;
+typedef wint_t trio_wint_t;
+# else
+typedef char trio_wchar_t;
+typedef int trio_wint_t;
+# define WCONST(x) L ## x
+# define WEOF EOF
+# define iswalnum(x) isalnum(x)
+# define iswalpha(x) isalpha(x)
+# define iswblank(x) isblank(x)
+# define iswcntrl(x) iscntrl(x)
+# define iswdigit(x) isdigit(x)
+# define iswgraph(x) isgraph(x)
+# define iswlower(x) islower(x)
+# define iswprint(x) isprint(x)
+# define iswpunct(x) ispunct(x)
+# define iswspace(x) isspace(x)
+# define iswupper(x) isupper(x)
+# define iswxdigit(x) isxdigit(x)
+# endif
+#endif
+
+
+/*************************************************************************
+ * Compiler dependent definitions
+ */
+
+/* Support for long long */
+#ifndef __cplusplus
+# if !defined(USE_LONGLONG)
+# if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
+# define USE_LONGLONG
+# elif defined(TRIO_COMPILER_SUNPRO)
+# define USE_LONGLONG
+# elif defined(_LONG_LONG) || defined(_LONGLONG)
+# define USE_LONGLONG
+# endif
+# endif
+#endif
+
+/* The extra long numbers */
+#if defined(USE_LONGLONG)
+typedef signed long long int trio_longlong_t;
+typedef unsigned long long int trio_ulonglong_t;
+#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
+typedef signed __int64 trio_longlong_t;
+typedef unsigned __int64 trio_ulonglong_t;
+#else
+typedef TRIO_SIGNED long int trio_longlong_t;
+typedef unsigned long int trio_ulonglong_t;
+#endif
+
+/* Maximal and fixed integer types */
+#if defined(TRIO_COMPILER_SUPPORTS_C99)
+# include <stdint.h>
+typedef intmax_t trio_intmax_t;
+typedef uintmax_t trio_uintmax_t;
+typedef int8_t trio_int8_t;
+typedef int16_t trio_int16_t;
+typedef int32_t trio_int32_t;
+typedef int64_t trio_int64_t;
+#elif defined(TRIO_COMPILER_SUPPORTS_UNIX98)
+# include <inttypes.h>
+typedef intmax_t trio_intmax_t;
+typedef uintmax_t trio_uintmax_t;
+typedef int8_t trio_int8_t;
+typedef int16_t trio_int16_t;
+typedef int32_t trio_int32_t;
+typedef int64_t trio_int64_t;
+#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
+typedef trio_longlong_t trio_intmax_t;
+typedef trio_ulonglong_t trio_uintmax_t;
+typedef __int8 trio_int8_t;
+typedef __int16 trio_int16_t;
+typedef __int32 trio_int32_t;
+typedef __int64 trio_int64_t;
+#else
+typedef trio_longlong_t trio_intmax_t;
+typedef trio_ulonglong_t trio_uintmax_t;
+# if defined(TRIO_INT8_T)
+typedef TRIO_INT8_T trio_int8_t;
+# else
+typedef TRIO_SIGNED char trio_int8_t;
+# endif
+# if defined(TRIO_INT16_T)
+typedef TRIO_INT16_T trio_int16_t;
+# else
+typedef TRIO_SIGNED short trio_int16_t;
+# endif
+# if defined(TRIO_INT32_T)
+typedef TRIO_INT32_T trio_int32_t;
+# else
+typedef TRIO_SIGNED int trio_int32_t;
+# endif
+# if defined(TRIO_INT64_T)
+typedef TRIO_INT64_T trio_int64_t;
+# else
+typedef trio_longlong_t trio_int64_t;
+# endif
+#endif
+
+#if (!(defined(TRIO_COMPILER_SUPPORTS_C99) \
+ || defined(TRIO_COMPILER_SUPPORTS_UNIX01))) \
+ && !defined(_WIN32_WCE)
+# define floorl(x) floor((double)(x))
+# define fmodl(x,y) fmod((double)(x),(double)(y))
+# define powl(x,y) pow((double)(x),(double)(y))
+#endif
+
+#define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
+
+/*************************************************************************
+ * Internal Definitions
+ */
+
+#ifndef DECIMAL_DIG
+# define DECIMAL_DIG DBL_DIG
+#endif
+
+/* Long double sizes */
+#ifdef LDBL_DIG
+# define MAX_MANTISSA_DIGITS LDBL_DIG
+# define MAX_EXPONENT_DIGITS 4
+# define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
+#else
+# define MAX_MANTISSA_DIGITS DECIMAL_DIG
+# define MAX_EXPONENT_DIGITS 3
+# define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
+#endif
+
+#if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
+# undef LDBL_DIG
+# undef LDBL_MANT_DIG
+# undef LDBL_EPSILON
+# define LDBL_DIG DBL_DIG
+# define LDBL_MANT_DIG DBL_MANT_DIG
+# define LDBL_EPSILON DBL_EPSILON
+#endif
+
+/* The maximal number of digits is for base 2 */
+#define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
+/* The width of a pointer. The number of bits in a hex digit is 4 */
+#define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
+
+/* Infinite and Not-A-Number for floating-point */
+#define INFINITE_LOWER "inf"
+#define INFINITE_UPPER "INF"
+#define LONG_INFINITE_LOWER "infinite"
+#define LONG_INFINITE_UPPER "INFINITE"
+#define NAN_LOWER "nan"
+#define NAN_UPPER "NAN"
+
+/* Various constants */
+enum {
+ TYPE_PRINT = 1,
+ TYPE_SCAN = 2,
+
+ /* Flags. FLAGS_LAST must be less than ULONG_MAX */
+ FLAGS_NEW = 0,
+ FLAGS_STICKY = 1,
+ FLAGS_SPACE = 2 * FLAGS_STICKY,
+ FLAGS_SHOWSIGN = 2 * FLAGS_SPACE,
+ FLAGS_LEFTADJUST = 2 * FLAGS_SHOWSIGN,
+ FLAGS_ALTERNATIVE = 2 * FLAGS_LEFTADJUST,
+ FLAGS_SHORT = 2 * FLAGS_ALTERNATIVE,
+ FLAGS_SHORTSHORT = 2 * FLAGS_SHORT,
+ FLAGS_LONG = 2 * FLAGS_SHORTSHORT,
+ FLAGS_QUAD = 2 * FLAGS_LONG,
+ FLAGS_LONGDOUBLE = 2 * FLAGS_QUAD,
+ FLAGS_SIZE_T = 2 * FLAGS_LONGDOUBLE,
+ FLAGS_PTRDIFF_T = 2 * FLAGS_SIZE_T,
+ FLAGS_INTMAX_T = 2 * FLAGS_PTRDIFF_T,
+ FLAGS_NILPADDING = 2 * FLAGS_INTMAX_T,
+ FLAGS_UNSIGNED = 2 * FLAGS_NILPADDING,
+ FLAGS_UPPER = 2 * FLAGS_UNSIGNED,
+ FLAGS_WIDTH = 2 * FLAGS_UPPER,
+ FLAGS_WIDTH_PARAMETER = 2 * FLAGS_WIDTH,
+ FLAGS_PRECISION = 2 * FLAGS_WIDTH_PARAMETER,
+ FLAGS_PRECISION_PARAMETER = 2 * FLAGS_PRECISION,
+ FLAGS_BASE = 2 * FLAGS_PRECISION_PARAMETER,
+ FLAGS_BASE_PARAMETER = 2 * FLAGS_BASE,
+ FLAGS_FLOAT_E = 2 * FLAGS_BASE_PARAMETER,
+ FLAGS_FLOAT_G = 2 * FLAGS_FLOAT_E,
+ FLAGS_QUOTE = 2 * FLAGS_FLOAT_G,
+ FLAGS_WIDECHAR = 2 * FLAGS_QUOTE,
+ FLAGS_ALLOC = 2 * FLAGS_WIDECHAR,
+ FLAGS_IGNORE = 2 * FLAGS_ALLOC,
+ FLAGS_IGNORE_PARAMETER = 2 * FLAGS_IGNORE,
+ FLAGS_VARSIZE_PARAMETER = 2 * FLAGS_IGNORE_PARAMETER,
+ FLAGS_FIXED_SIZE = 2 * FLAGS_VARSIZE_PARAMETER,
+ FLAGS_LAST = FLAGS_FIXED_SIZE,
+ /* Reused flags */
+ FLAGS_EXCLUDE = FLAGS_SHORT,
+ FLAGS_USER_DEFINED = FLAGS_IGNORE,
+ FLAGS_ROUNDING = FLAGS_INTMAX_T,
+ /* Compounded flags */
+ FLAGS_ALL_VARSIZES = FLAGS_LONG | FLAGS_QUAD | FLAGS_INTMAX_T | FLAGS_PTRDIFF_T | FLAGS_SIZE_T,
+ FLAGS_ALL_SIZES = FLAGS_ALL_VARSIZES | FLAGS_SHORTSHORT | FLAGS_SHORT,
+
+ NO_POSITION = -1,
+ NO_WIDTH = 0,
+ NO_PRECISION = -1,
+ NO_SIZE = -1,
+
+ /* Do not change these */
+ NO_BASE = -1,
+ MIN_BASE = 2,
+ MAX_BASE = 36,
+ BASE_BINARY = 2,
+ BASE_OCTAL = 8,
+ BASE_DECIMAL = 10,
+ BASE_HEX = 16,
+
+ /* Maximal number of allowed parameters */
+ MAX_PARAMETERS = 64,
+ /* Maximal number of characters in class */
+ MAX_CHARACTER_CLASS = UCHAR_MAX + 1,
+
+ /* Maximal string lengths for user-defined specifiers */
+ MAX_USER_NAME = 64,
+ MAX_USER_DATA = 256,
+
+ /* Maximal length of locale separator strings */
+ MAX_LOCALE_SEPARATOR_LENGTH = MB_LEN_MAX,
+ /* Maximal number of integers in grouping */
+ MAX_LOCALE_GROUPS = 64,
+
+ /* Initial size of asprintf buffer */
+ DYNAMIC_START_SIZE = 32
+};
+
+#define NO_GROUPING ((int)CHAR_MAX)
+
+/* Fundamental formatting parameter types */
+#define FORMAT_UNKNOWN 0
+#define FORMAT_INT 1
+#define FORMAT_DOUBLE 2
+#define FORMAT_CHAR 3
+#define FORMAT_STRING 4
+#define FORMAT_POINTER 5
+#define FORMAT_COUNT 6
+#define FORMAT_PARAMETER 7
+#define FORMAT_GROUP 8
+#if TRIO_GNU
+# define FORMAT_ERRNO 9
+#endif
+#if TRIO_EXTENSION
+# define FORMAT_USER_DEFINED 10
+#endif
+
+/* Character constants */
+#define CHAR_IDENTIFIER '%'
+#define CHAR_BACKSLASH '\\'
+#define CHAR_QUOTE '\"'
+#define CHAR_ADJUST ' '
+
+/* Character class expressions */
+#define CLASS_ALNUM "[:alnum:]"
+#define CLASS_ALPHA "[:alpha:]"
+#define CLASS_BLANK "[:blank:]"
+#define CLASS_CNTRL "[:cntrl:]"
+#define CLASS_DIGIT "[:digit:]"
+#define CLASS_GRAPH "[:graph:]"
+#define CLASS_LOWER "[:lower:]"
+#define CLASS_PRINT "[:print:]"
+#define CLASS_PUNCT "[:punct:]"
+#define CLASS_SPACE "[:space:]"
+#define CLASS_UPPER "[:upper:]"
+#define CLASS_XDIGIT "[:xdigit:]"
+
+/*
+ * SPECIFIERS:
+ *
+ *
+ * a Hex-float
+ * A Hex-float
+ * c Character
+ * C Widechar character (wint_t)
+ * d Decimal
+ * e Float
+ * E Float
+ * F Float
+ * F Float
+ * g Float
+ * G Float
+ * i Integer
+ * m Error message
+ * n Count
+ * o Octal
+ * p Pointer
+ * s String
+ * S Widechar string (wchar_t *)
+ * u Unsigned
+ * x Hex
+ * X Hex
+ * [] Group
+ * <> User-defined
+ *
+ * Reserved:
+ *
+ * D Binary Coded Decimal %D(length,precision) (OS/390)
+ */
+#define SPECIFIER_CHAR 'c'
+#define SPECIFIER_STRING 's'
+#define SPECIFIER_DECIMAL 'd'
+#define SPECIFIER_INTEGER 'i'
+#define SPECIFIER_UNSIGNED 'u'
+#define SPECIFIER_OCTAL 'o'
+#define SPECIFIER_HEX 'x'
+#define SPECIFIER_HEX_UPPER 'X'
+#define SPECIFIER_FLOAT_E 'e'
+#define SPECIFIER_FLOAT_E_UPPER 'E'
+#define SPECIFIER_FLOAT_F 'f'
+#define SPECIFIER_FLOAT_F_UPPER 'F'
+#define SPECIFIER_FLOAT_G 'g'
+#define SPECIFIER_FLOAT_G_UPPER 'G'
+#define SPECIFIER_POINTER 'p'
+#define SPECIFIER_GROUP '['
+#define SPECIFIER_UNGROUP ']'
+#define SPECIFIER_COUNT 'n'
+#if TRIO_UNIX98
+# define SPECIFIER_CHAR_UPPER 'C'
+# define SPECIFIER_STRING_UPPER 'S'
+#endif
+#if TRIO_C99
+# define SPECIFIER_HEXFLOAT 'a'
+# define SPECIFIER_HEXFLOAT_UPPER 'A'
+#endif
+#if TRIO_GNU
+# define SPECIFIER_ERRNO 'm'
+#endif
+#if TRIO_EXTENSION
+# define SPECIFIER_BINARY 'b'
+# define SPECIFIER_BINARY_UPPER 'B'
+# define SPECIFIER_USER_DEFINED_BEGIN '<'
+# define SPECIFIER_USER_DEFINED_END '>'
+# define SPECIFIER_USER_DEFINED_SEPARATOR ':'
+#endif
+
+/*
+ * QUALIFIERS:
+ *
+ *
+ * Numbers = d,i,o,u,x,X
+ * Float = a,A,e,E,f,F,g,G
+ * String = s
+ * Char = c
+ *
+ *
+ * 9$ Position
+ * Use the 9th parameter. 9 can be any number between 1 and
+ * the maximal argument
+ *
+ * 9 Width
+ * Set width to 9. 9 can be any number, but must not be postfixed
+ * by '$'
+ *
+ * h Short
+ * Numbers:
+ * (unsigned) short int
+ *
+ * hh Short short
+ * Numbers:
+ * (unsigned) char
+ *
+ * l Long
+ * Numbers:
+ * (unsigned) long int
+ * String:
+ * as the S specifier
+ * Char:
+ * as the C specifier
+ *
+ * ll Long Long
+ * Numbers:
+ * (unsigned) long long int
+ *
+ * L Long Double
+ * Float
+ * long double
+ *
+ * # Alternative
+ * Float:
+ * Decimal-point is always present
+ * String:
+ * non-printable characters are handled as \number
+ *
+ * Spacing
+ *
+ * + Sign
+ *
+ * - Alignment
+ *
+ * . Precision
+ *
+ * * Parameter
+ * print: use parameter
+ * scan: no parameter (ignore)
+ *
+ * q Quad
+ *
+ * Z size_t
+ *
+ * w Widechar
+ *
+ * ' Thousands/quote
+ * Numbers:
+ * Integer part grouped in thousands
+ * Binary numbers:
+ * Number grouped in nibbles (4 bits)
+ * String:
+ * Quoted string
+ *
+ * j intmax_t
+ * t prtdiff_t
+ * z size_t
+ *
+ * ! Sticky
+ * @ Parameter (for both print and scan)
+ *
+ * I n-bit Integer
+ * Numbers:
+ * The following options exists
+ * I8 = 8-bit integer
+ * I16 = 16-bit integer
+ * I32 = 32-bit integer
+ * I64 = 64-bit integer
+ */
+#define QUALIFIER_POSITION '$'
+#define QUALIFIER_SHORT 'h'
+#define QUALIFIER_LONG 'l'
+#define QUALIFIER_LONG_UPPER 'L'
+#define QUALIFIER_ALTERNATIVE '#'
+#define QUALIFIER_SPACE ' '
+#define QUALIFIER_PLUS '+'
+#define QUALIFIER_MINUS '-'
+#define QUALIFIER_DOT '.'
+#define QUALIFIER_STAR '*'
+#define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
+#if TRIO_C99
+# define QUALIFIER_SIZE_T 'z'
+# define QUALIFIER_PTRDIFF_T 't'
+# define QUALIFIER_INTMAX_T 'j'
+#endif
+#if TRIO_BSD || TRIO_GNU
+# define QUALIFIER_QUAD 'q'
+#endif
+#if TRIO_GNU
+# define QUALIFIER_SIZE_T_UPPER 'Z'
+#endif
+#if TRIO_MISC
+# define QUALIFIER_WIDECHAR 'w'
+#endif
+#if TRIO_MICROSOFT
+# define QUALIFIER_FIXED_SIZE 'I'
+#endif
+#if TRIO_EXTENSION
+# define QUALIFIER_QUOTE '\''
+# define QUALIFIER_STICKY '!'
+# define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
+# define QUALIFIER_PARAM '@' /* Experimental */
+# define QUALIFIER_COLON ':' /* For scanlists */
+# define QUALIFIER_EQUAL '=' /* For scanlists */
+# define QUALIFIER_ROUNDING_UPPER 'R'
+#endif
+
+
+/*************************************************************************
+ *
+ * Internal Structures
+ *
+ *************************************************************************/
+
+/* Parameters */
+typedef struct {
+ /* An indication of which entry in the data union is used */
+ int type;
+ /* The flags */
+ trio_flags_t flags;
+ /* The width qualifier */
+ int width;
+ /* The precision qualifier */
+ int precision;
+ /* The base qualifier */
+ int base;
+ /* The size for the variable size qualifier */
+ int varsize;
+ /* The marker of the end of the specifier */
+ int indexAfterSpecifier;
+ /* The data from the argument list */
+ union {
+ char *string;
+#if TRIO_WIDECHAR
+ trio_wchar_t *wstring;
+#endif
+ trio_pointer_t pointer;
+ union {
+ trio_intmax_t as_signed;
+ trio_uintmax_t as_unsigned;
+ } number;
+ double doubleNumber;
+ double *doublePointer;
+ trio_long_double_t longdoubleNumber;
+ trio_long_double_t *longdoublePointer;
+ int errorNumber;
+ } data;
+ /* For the user-defined specifier */
+ char user_name[MAX_USER_NAME];
+ char user_data[MAX_USER_DATA];
+} trio_parameter_t;
+
+/* Container for customized functions */
+typedef struct {
+ union {
+ trio_outstream_t out;
+ trio_instream_t in;
+ } stream;
+ trio_pointer_t closure;
+} trio_custom_t;
+
+/* General trio "class" */
+typedef struct _trio_class_t {
+ /*
+ * The function to write characters to a stream.
+ */
+ void (*OutStream) TRIO_PROTO((struct _trio_class_t *, int));
+ /*
+ * The function to read characters from a stream.
+ */
+ void (*InStream) TRIO_PROTO((struct _trio_class_t *, int *));
+ /*
+ * The current location in the stream.
+ */
+ trio_pointer_t location;
+ /*
+ * The character currently being processed.
+ */
+ int current;
+ /*
+ * The number of characters that would have been written/read
+ * if there had been sufficient space.
+ */
+ int processed;
+ /*
+ * The number of characters that are actually written/read.
+ * Processed and committed will only differ for the *nprintf
+ * and *nscanf functions.
+ */
+ int committed;
+ /*
+ * The upper limit of characters that may be written/read.
+ */
+ int max;
+ /*
+ * The last output error that was detected.
+ */
+ int error;
+} trio_class_t;
+
+/* References (for user-defined callbacks) */
+typedef struct _trio_reference_t {
+ trio_class_t *data;
+ trio_parameter_t *parameter;
+} trio_reference_t;
+
+/* Registered entries (for user-defined callbacks) */
+typedef struct _trio_userdef_t {
+ struct _trio_userdef_t *next;
+ trio_callback_t callback;
+ char *name;
+} trio_userdef_t;
+
+/*************************************************************************
+ *
+ * Internal Variables
+ *
+ *************************************************************************/
+
+static TRIO_CONST char rcsid[] = "@(#)$Id: trio.c 3600 2007-04-17 12:44:58Z veillard $";
+
+/*
+ * Need this to workaround a parser bug in HP C/iX compiler that fails
+ * to resolves macro definitions that includes type 'long double',
+ * e.g: va_arg(arg_ptr, long double)
+ */
+#if defined(TRIO_PLATFORM_MPEIX)
+static TRIO_CONST trio_long_double_t ___dummy_long_double = 0;
+#endif
+
+static TRIO_CONST char internalNullString[] = "(nil)";
+
+#if defined(USE_LOCALE)
+static struct lconv *internalLocaleValues = NULL;
+#endif
+
+/*
+ * UNIX98 says "in a locale where the radix character is not defined,
+ * the radix character defaults to a period (.)"
+ */
+static int internalDecimalPointLength = 1;
+static int internalThousandSeparatorLength = 1;
+static char internalDecimalPoint = '.';
+static char internalDecimalPointString[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ".";
+static char internalThousandSeparator[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ",";
+static char internalGrouping[MAX_LOCALE_GROUPS] = { (char)NO_GROUPING };
+
+static TRIO_CONST char internalDigitsLower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
+static TRIO_CONST char internalDigitsUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+static BOOLEAN_T internalDigitsUnconverted = TRUE;
+static int internalDigitArray[128];
+#if TRIO_EXTENSION
+static BOOLEAN_T internalCollationUnconverted = TRUE;
+static char internalCollationArray[MAX_CHARACTER_CLASS][MAX_CHARACTER_CLASS];
+#endif
+
+#if TRIO_EXTENSION
+static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion = NULL;
+static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion = NULL;
+static trio_userdef_t *internalUserDef = NULL;
+#endif
+
+
+/*************************************************************************
+ *
+ * Internal Functions
+ *
+ ************************************************************************/
+
+#if defined(TRIO_MINIMAL)
+# define TRIO_STRING_PUBLIC static
+# include "triostr.c"
+#endif /* defined(TRIO_MINIMAL) */
+
+/*************************************************************************
+ * TrioIsQualifier
+ *
+ * Description:
+ * Remember to add all new qualifiers to this function.
+ * QUALIFIER_POSITION must not be added.
+ */
+TRIO_PRIVATE BOOLEAN_T
+TrioIsQualifier
+TRIO_ARGS1((character),
+ TRIO_CONST char character)
+{
+ /* QUALIFIER_POSITION is not included */
+ switch (character)
+ {
+ case '0': case '1': case '2': case '3': case '4':
+ case '5': case '6': case '7': case '8': case '9':
+ case QUALIFIER_PLUS:
+ case QUALIFIER_MINUS:
+ case QUALIFIER_SPACE:
+ case QUALIFIER_DOT:
+ case QUALIFIER_STAR:
+ case QUALIFIER_ALTERNATIVE:
+ case QUALIFIER_SHORT:
+ case QUALIFIER_LONG:
+ case QUALIFIER_LONG_UPPER:
+ case QUALIFIER_CIRCUMFLEX:
+#if defined(QUALIFIER_SIZE_T)
+ case QUALIFIER_SIZE_T:
+#endif
+#if defined(QUALIFIER_PTRDIFF_T)
+ case QUALIFIER_PTRDIFF_T:
+#endif
+#if defined(QUALIFIER_INTMAX_T)
+ case QUALIFIER_INTMAX_T:
+#endif
+#if defined(QUALIFIER_QUAD)
+ case QUALIFIER_QUAD:
+#endif
+#if defined(QUALIFIER_SIZE_T_UPPER)
+ case QUALIFIER_SIZE_T_UPPER:
+#endif
+#if defined(QUALIFIER_WIDECHAR)
+ case QUALIFIER_WIDECHAR:
+#endif
+#if defined(QUALIFIER_QUOTE)
+ case QUALIFIER_QUOTE:
+#endif
+#if defined(QUALIFIER_STICKY)
+ case QUALIFIER_STICKY:
+#endif
+#if defined(QUALIFIER_VARSIZE)
+ case QUALIFIER_VARSIZE:
+#endif
+#if defined(QUALIFIER_PARAM)
+ case QUALIFIER_PARAM:
+#endif
+#if defined(QUALIFIER_FIXED_SIZE)
+ case QUALIFIER_FIXED_SIZE:
+#endif
+#if defined(QUALIFIER_ROUNDING_UPPER)
+ case QUALIFIER_ROUNDING_UPPER:
+#endif
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
+/*************************************************************************
+ * TrioSetLocale
+ */
+#if defined(USE_LOCALE)
+TRIO_PRIVATE void
+TrioSetLocale(TRIO_NOARGS)
+{
+ internalLocaleValues = (struct lconv *)localeconv();
+ if (internalLocaleValues)
+ {
+ if ((internalLocaleValues->decimal_point) &&
+ (internalLocaleValues->decimal_point[0] != NIL))
+ {
+ internalDecimalPointLength = trio_length(internalLocaleValues->decimal_point);
+ if (internalDecimalPointLength == 1)
+ {
+ internalDecimalPoint = internalLocaleValues->decimal_point[0];
+ }
+ else
+ {
+ internalDecimalPoint = NIL;
+ trio_copy_max(internalDecimalPointString,
+ sizeof(internalDecimalPointString),
+ internalLocaleValues->decimal_point);
+ }
+ }
+ if ((internalLocaleValues->thousands_sep) &&
+ (internalLocaleValues->thousands_sep[0] != NIL))
+ {
+ trio_copy_max(internalThousandSeparator,
+ sizeof(internalThousandSeparator),
+ internalLocaleValues->thousands_sep);
+ internalThousandSeparatorLength = trio_length(internalThousandSeparator);
+ }
+ if ((internalLocaleValues->grouping) &&
+ (internalLocaleValues->grouping[0] != NIL))
+ {
+ trio_copy_max(internalGrouping,
+ sizeof(internalGrouping),
+ internalLocaleValues->grouping);
+ }
+ }
+}
+#endif /* defined(USE_LOCALE) */
+
+TRIO_PRIVATE int
+TrioCalcThousandSeparatorLength
+TRIO_ARGS1((digits),
+ int digits)
+{
+#if TRIO_EXTENSION
+ int count = 0;
+ int step = NO_GROUPING;
+ char *groupingPointer = internalGrouping;
+
+ while (digits > 0)
+ {
+ if (*groupingPointer == CHAR_MAX)
+ {
+ /* Disable grouping */
+ break; /* while */
+ }
+ else if (*groupingPointer == 0)
+ {
+ /* Repeat last group */
+ if (step == NO_GROUPING)
+ {
+ /* Error in locale */
+ break; /* while */
+ }
+ }
+ else
+ {
+ step = *groupingPointer++;
+ }
+ if (digits > step)
+ count += internalThousandSeparatorLength;
+ digits -= step;
+ }
+ return count;
+#else
+ return 0;
+#endif
+}
+
+TRIO_PRIVATE BOOLEAN_T
+TrioFollowedBySeparator
+TRIO_ARGS1((position),
+ int position)
+{
+#if TRIO_EXTENSION
+ int step = 0;
+ char *groupingPointer = internalGrouping;
+
+ position--;
+ if (position == 0)
+ return FALSE;
+ while (position > 0)
+ {
+ if (*groupingPointer == CHAR_MAX)
+ {
+ /* Disable grouping */
+ break; /* while */
+ }
+ else if (*groupingPointer != 0)
+ {
+ step = *groupingPointer++;
+ }
+ if (step == 0)
+ break;
+ position -= step;
+ }
+ return (position == 0);
+#else
+ return FALSE;
+#endif
+}
+
+/*************************************************************************
+ * TrioGetPosition
+ *
+ * Get the %n$ position.
+ */
+TRIO_PRIVATE int
+TrioGetPosition
+TRIO_ARGS2((format, indexPointer),
+ TRIO_CONST char *format,
+ int *indexPointer)
+{
+#if TRIO_UNIX98
+ char *tmpformat;
+ int number = 0;
+ int index = *indexPointer;
+
+ number = (int)trio_to_long(&format[index], &tmpformat, BASE_DECIMAL);
+ index = (int)(tmpformat - format);
+ if ((number != 0) && (QUALIFIER_POSITION == format[index++]))
+ {
+ *indexPointer = index;
+ /*
+ * number is decreased by 1, because n$ starts from 1, whereas
+ * the array it is indexing starts from 0.
+ */
+ return number - 1;
+ }
+#endif
+ return NO_POSITION;
+}
+
+#if TRIO_EXTENSION
+/*************************************************************************
+ * TrioFindNamespace
+ *
+ * Find registered user-defined specifier.
+ * The prev argument is used for optimization only.
+ */
+TRIO_PRIVATE trio_userdef_t *
+TrioFindNamespace
+TRIO_ARGS2((name, prev),
+ TRIO_CONST char *name,
+ trio_userdef_t **prev)
+{
+ trio_userdef_t *def;
+
+ if (internalEnterCriticalRegion)
+ (void)internalEnterCriticalRegion(NULL);
+
+ for (def = internalUserDef; def; def = def->next)
+ {
+ /* Case-sensitive string comparison */
+ if (trio_equal_case(def->name, name))
+ break;
+
+ if (prev)
+ *prev = def;
+ }
+
+ if (internalLeaveCriticalRegion)
+ (void)internalLeaveCriticalRegion(NULL);
+
+ return def;
+}
+#endif
+
+/*************************************************************************
+ * TrioPower
+ *
+ * Description:
+ * Calculate pow(base, exponent), where number and exponent are integers.
+ */
+TRIO_PRIVATE trio_long_double_t
+TrioPower
+TRIO_ARGS2((number, exponent),
+ int number,
+ int exponent)
+{
+ trio_long_double_t result;
+
+ if (number == 10)
+ {
+ switch (exponent)
+ {
+ /* Speed up calculation of common cases */
+ case 0:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E-1);
+ break;
+ case 1:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+0);
+ break;
+ case 2:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+1);
+ break;
+ case 3:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+2);
+ break;
+ case 4:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+3);
+ break;
+ case 5:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+4);
+ break;
+ case 6:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+5);
+ break;
+ case 7:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+6);
+ break;
+ case 8:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+7);
+ break;
+ case 9:
+ result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+8);
+ break;
+ default:
+ result = powl((trio_long_double_t)number,
+ (trio_long_double_t)exponent);
+ break;
+ }
+ }
+ else
+ {
+ return powl((trio_long_double_t)number, (trio_long_double_t)exponent);
+ }
+ return result;
+}
+
+/*************************************************************************
+ * TrioLogarithm
+ */
+TRIO_PRIVATE double
+TrioLogarithm
+TRIO_ARGS2((number, base),
+ double number,
+ int base)
+{
+ double result;
+
+ if (number <= 0.0)
+ {
+ /* xlC crashes on log(0) */
+ result = (number == 0.0) ? trio_ninf() : trio_nan();
+ }
+ else
+ {
+ if (base == 10)
+ {
+ result = log10(number);
+ }
+ else
+ {
+ result = log10(number) / log10((double)base);
+ }
+ }
+ return result;
+}
+
+/*************************************************************************
+ * TrioLogarithmBase
+ */
+TRIO_PRIVATE double
+TrioLogarithmBase
+TRIO_ARGS1((base),
+ int base)
+{
+ switch (base)
+ {
+ case BASE_BINARY : return 1.0;
+ case BASE_OCTAL : return 3.0;
+ case BASE_DECIMAL: return 3.321928094887362345;
+ case BASE_HEX : return 4.0;
+ default : return TrioLogarithm((double)base, 2);
+ }
+}
+
+/*************************************************************************
+ * TrioParse
+ *
+ * Description:
+ * Parse the format string
+ */
+TRIO_PRIVATE int
+TrioParse
+TRIO_ARGS5((type, format, parameters, arglist, argarray),
+ int type,
+ TRIO_CONST char *format,
+ trio_parameter_t *parameters,
+ va_list *arglist,
+ trio_pointer_t *argarray)
+{
+ /* Count the number of times a parameter is referenced */
+ unsigned short usedEntries[MAX_PARAMETERS];
+ /* Parameter counters */
+ int parameterPosition;
+ int currentParam;
+ int maxParam = -1;
+ /* Utility variables */
+ trio_flags_t flags;
+ int width;
+ int precision;
+ int varsize;
+ int base;
+ int index; /* Index into formatting string */
+ int dots; /* Count number of dots in modifier part */
+ BOOLEAN_T positional; /* Does the specifier have a positional? */
+ BOOLEAN_T gotSticky = FALSE; /* Are there any sticky modifiers at all? */
+ /*
+ * indices specifies the order in which the parameters must be
+ * read from the va_args (this is necessary to handle positionals)
+ */
+ int indices[MAX_PARAMETERS];
+ int pos = 0;
+ /* Various variables */
+ char ch;
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ int charlen;
+#endif
+ int save_errno;
+ int i = -1;
+ int num;
+ char *tmpformat;
+
+ /* One and only one of arglist and argarray must be used */
+ assert((arglist != NULL) ^ (argarray != NULL));
+
+ /*
+ * The 'parameters' array is not initialized, but we need to
+ * know which entries we have used.
+ */
+ memset(usedEntries, 0, sizeof(usedEntries));
+
+ save_errno = errno;
+ index = 0;
+ parameterPosition = 0;
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ (void)mblen(NULL, 0);
+#endif
+
+ while (format[index])
+ {
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ if (! isascii(format[index]))
+ {
+ /*
+ * Multibyte characters cannot be legal specifiers or
+ * modifiers, so we skip over them.
+ */
+ charlen = mblen(&format[index], MB_LEN_MAX);
+ index += (charlen > 0) ? charlen : 1;
+ continue; /* while */
+ }
+#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
+ if (CHAR_IDENTIFIER == format[index++])
+ {
+ if (CHAR_IDENTIFIER == format[index])
+ {
+ index++;
+ continue; /* while */
+ }
+
+ flags = FLAGS_NEW;
+ dots = 0;
+ currentParam = TrioGetPosition(format, &index);
+ positional = (NO_POSITION != currentParam);
+ if (!positional)
+ {
+ /* We have no positional, get the next counter */
+ currentParam = parameterPosition;
+ }
+ if(currentParam >= MAX_PARAMETERS)
+ {
+ /* Bail out completely to make the error more obvious */
+ return TRIO_ERROR_RETURN(TRIO_ETOOMANY, index);
+ }
+
+ if (currentParam > maxParam)
+ maxParam = currentParam;
+
+ /* Default values */
+ width = NO_WIDTH;
+ precision = NO_PRECISION;
+ base = NO_BASE;
+ varsize = NO_SIZE;
+
+ while (TrioIsQualifier(format[index]))
+ {
+ ch = format[index++];
+
+ switch (ch)
+ {
+ case QUALIFIER_SPACE:
+ flags |= FLAGS_SPACE;
+ break;
+
+ case QUALIFIER_PLUS:
+ flags |= FLAGS_SHOWSIGN;
+ break;
+
+ case QUALIFIER_MINUS:
+ flags |= FLAGS_LEFTADJUST;
+ flags &= ~FLAGS_NILPADDING;
+ break;
+
+ case QUALIFIER_ALTERNATIVE:
+ flags |= FLAGS_ALTERNATIVE;
+ break;
+
+ case QUALIFIER_DOT:
+ if (dots == 0) /* Precision */
+ {
+ dots++;
+
+ /* Skip if no precision */
+ if (QUALIFIER_DOT == format[index])
+ break;
+
+ /* After the first dot we have the precision */
+ flags |= FLAGS_PRECISION;
+ if ((QUALIFIER_STAR == format[index])
+#if defined(QUALIFIER_PARAM)
+ || (QUALIFIER_PARAM == format[index])
+#endif
+ )
+ {
+ index++;
+ flags |= FLAGS_PRECISION_PARAMETER;
+
+ precision = TrioGetPosition(format, &index);
+ if (precision == NO_POSITION)
+ {
+ parameterPosition++;
+ if (positional)
+ precision = parameterPosition;
+ else
+ {
+ precision = currentParam;
+ currentParam = precision + 1;
+ }
+ }
+ else
+ {
+ if (! positional)
+ currentParam = precision + 1;
+ if (width > maxParam)
+ maxParam = precision;
+ }
+ if (currentParam > maxParam)
+ maxParam = currentParam;
+ }
+ else
+ {
+ precision = trio_to_long(&format[index],
+ &tmpformat,
+ BASE_DECIMAL);
+ index = (int)(tmpformat - format);
+ }
+ }
+ else if (dots == 1) /* Base */
+ {
+ dots++;
+
+ /* After the second dot we have the base */
+ flags |= FLAGS_BASE;
+ if ((QUALIFIER_STAR == format[index])
+#if defined(QUALIFIER_PARAM)
+ || (QUALIFIER_PARAM == format[index])
+#endif
+ )
+ {
+ index++;
+ flags |= FLAGS_BASE_PARAMETER;
+ base = TrioGetPosition(format, &index);
+ if (base == NO_POSITION)
+ {
+ parameterPosition++;
+ if (positional)
+ base = parameterPosition;
+ else
+ {
+ base = currentParam;
+ currentParam = base + 1;
+ }
+ }
+ else
+ {
+ if (! positional)
+ currentParam = base + 1;
+ if (base > maxParam)
+ maxParam = base;
+ }
+ if (currentParam > maxParam)
+ maxParam = currentParam;
+ }
+ else
+ {
+ base = trio_to_long(&format[index],
+ &tmpformat,
+ BASE_DECIMAL);
+ if (base > MAX_BASE)
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ index = (int)(tmpformat - format);
+ }
+ }
+ else
+ {
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ }
+ break; /* QUALIFIER_DOT */
+
+#if defined(QUALIFIER_PARAM)
+ case QUALIFIER_PARAM:
+ type = TYPE_PRINT;
+ /* FALLTHROUGH */
+#endif
+ case QUALIFIER_STAR:
+ /* This has different meanings for print and scan */
+ if (TYPE_PRINT == type)
+ {
+ /* Read with from parameter */
+ flags |= (FLAGS_WIDTH | FLAGS_WIDTH_PARAMETER);
+ width = TrioGetPosition(format, &index);
+ if (width == NO_POSITION)
+ {
+ parameterPosition++;
+ if (positional)
+ width = parameterPosition;
+ else
+ {
+ width = currentParam;
+ currentParam = width + 1;
+ }
+ }
+ else
+ {
+ if (! positional)
+ currentParam = width + 1;
+ if (width > maxParam)
+ maxParam = width;
+ }
+ if (currentParam > maxParam)
+ maxParam = currentParam;
+ }
+ else
+ {
+ /* Scan, but do not store result */
+ flags |= FLAGS_IGNORE;
+ }
+
+ break; /* QUALIFIER_STAR */
+
+ case '0':
+ if (! (flags & FLAGS_LEFTADJUST))
+ flags |= FLAGS_NILPADDING;
+ /* FALLTHROUGH */
+ case '1': case '2': case '3': case '4':
+ case '5': case '6': case '7': case '8': case '9':
+ flags |= FLAGS_WIDTH;
+ /* &format[index - 1] is used to "rewind" the read
+ * character from format
+ */
+ width = trio_to_long(&format[index - 1],
+ &tmpformat,
+ BASE_DECIMAL);
+ index = (int)(tmpformat - format);
+ break;
+
+ case QUALIFIER_SHORT:
+ if (flags & FLAGS_SHORTSHORT)
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ else if (flags & FLAGS_SHORT)
+ flags |= FLAGS_SHORTSHORT;
+ else
+ flags |= FLAGS_SHORT;
+ break;
+
+ case QUALIFIER_LONG:
+ if (flags & FLAGS_QUAD)
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ else if (flags & FLAGS_LONG)
+ flags |= FLAGS_QUAD;
+ else
+ flags |= FLAGS_LONG;
+ break;
+
+ case QUALIFIER_LONG_UPPER:
+ flags |= FLAGS_LONGDOUBLE;
+ break;
+
+#if defined(QUALIFIER_SIZE_T)
+ case QUALIFIER_SIZE_T:
+ flags |= FLAGS_SIZE_T;
+ /* Modify flags for later truncation of number */
+ if (sizeof(size_t) == sizeof(trio_ulonglong_t))
+ flags |= FLAGS_QUAD;
+ else if (sizeof(size_t) == sizeof(long))
+ flags |= FLAGS_LONG;
+ break;
+#endif
+
+#if defined(QUALIFIER_PTRDIFF_T)
+ case QUALIFIER_PTRDIFF_T:
+ flags |= FLAGS_PTRDIFF_T;
+ if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t))
+ flags |= FLAGS_QUAD;
+ else if (sizeof(ptrdiff_t) == sizeof(long))
+ flags |= FLAGS_LONG;
+ break;
+#endif
+
+#if defined(QUALIFIER_INTMAX_T)
+ case QUALIFIER_INTMAX_T:
+ flags |= FLAGS_INTMAX_T;
+ if (sizeof(trio_intmax_t) == sizeof(trio_ulonglong_t))
+ flags |= FLAGS_QUAD;
+ else if (sizeof(trio_intmax_t) == sizeof(long))
+ flags |= FLAGS_LONG;
+ break;
+#endif
+
+#if defined(QUALIFIER_QUAD)
+ case QUALIFIER_QUAD:
+ flags |= FLAGS_QUAD;
+ break;
+#endif
+
+#if defined(QUALIFIER_FIXED_SIZE)
+ case QUALIFIER_FIXED_SIZE:
+ if (flags & FLAGS_FIXED_SIZE)
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+
+ if (flags & (FLAGS_ALL_SIZES | FLAGS_LONGDOUBLE |
+ FLAGS_WIDECHAR | FLAGS_VARSIZE_PARAMETER))
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+
+ if ((format[index] == '6') &&
+ (format[index + 1] == '4'))
+ {
+ varsize = sizeof(trio_int64_t);
+ index += 2;
+ }
+ else if ((format[index] == '3') &&
+ (format[index + 1] == '2'))
+ {
+ varsize = sizeof(trio_int32_t);
+ index += 2;
+ }
+ else if ((format[index] == '1') &&
+ (format[index + 1] == '6'))
+ {
+ varsize = sizeof(trio_int16_t);
+ index += 2;
+ }
+ else if (format[index] == '8')
+ {
+ varsize = sizeof(trio_int8_t);
+ index++;
+ }
+ else
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+
+ flags |= FLAGS_FIXED_SIZE;
+ break;
+#endif
+
+#if defined(QUALIFIER_WIDECHAR)
+ case QUALIFIER_WIDECHAR:
+ flags |= FLAGS_WIDECHAR;
+ break;
+#endif
+
+#if defined(QUALIFIER_SIZE_T_UPPER)
+ case QUALIFIER_SIZE_T_UPPER:
+ break;
+#endif
+
+#if defined(QUALIFIER_QUOTE)
+ case QUALIFIER_QUOTE:
+ flags |= FLAGS_QUOTE;
+ break;
+#endif
+
+#if defined(QUALIFIER_STICKY)
+ case QUALIFIER_STICKY:
+ flags |= FLAGS_STICKY;
+ gotSticky = TRUE;
+ break;
+#endif
+
+#if defined(QUALIFIER_VARSIZE)
+ case QUALIFIER_VARSIZE:
+ flags |= FLAGS_VARSIZE_PARAMETER;
+ parameterPosition++;
+ if (positional)
+ varsize = parameterPosition;
+ else
+ {
+ varsize = currentParam;
+ currentParam = varsize + 1;
+ }
+ if (currentParam > maxParam)
+ maxParam = currentParam;
+ break;
+#endif
+
+#if defined(QUALIFIER_ROUNDING_UPPER)
+ case QUALIFIER_ROUNDING_UPPER:
+ flags |= FLAGS_ROUNDING;
+ break;
+#endif
+
+ default:
+ /* Bail out completely to make the error more obvious */
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ }
+ } /* while qualifier */
+
+ /*
+ * Parameters only need the type and value. The value is
+ * read later.
+ */
+ if (flags & FLAGS_WIDTH_PARAMETER)
+ {
+ usedEntries[width] += 1;
+ parameters[pos].type = FORMAT_PARAMETER;
+ parameters[pos].flags = 0;
+ indices[width] = pos;
+ width = pos++;
+ }
+ if (flags & FLAGS_PRECISION_PARAMETER)
+ {
+ usedEntries[precision] += 1;
+ parameters[pos].type = FORMAT_PARAMETER;
+ parameters[pos].flags = 0;
+ indices[precision] = pos;
+ precision = pos++;
+ }
+ if (flags & FLAGS_BASE_PARAMETER)
+ {
+ usedEntries[base] += 1;
+ parameters[pos].type = FORMAT_PARAMETER;
+ parameters[pos].flags = 0;
+ indices[base] = pos;
+ base = pos++;
+ }
+ if (flags & FLAGS_VARSIZE_PARAMETER)
+ {
+ usedEntries[varsize] += 1;
+ parameters[pos].type = FORMAT_PARAMETER;
+ parameters[pos].flags = 0;
+ indices[varsize] = pos;
+ varsize = pos++;
+ }
+
+ indices[currentParam] = pos;
+
+ switch (format[index++])
+ {
+#if defined(SPECIFIER_CHAR_UPPER)
+ case SPECIFIER_CHAR_UPPER:
+ flags |= FLAGS_WIDECHAR;
+ /* FALLTHROUGH */
+#endif
+ case SPECIFIER_CHAR:
+ if (flags & FLAGS_LONG)
+ flags |= FLAGS_WIDECHAR;
+ else if (flags & FLAGS_SHORT)
+ flags &= ~FLAGS_WIDECHAR;
+ parameters[pos].type = FORMAT_CHAR;
+ break;
+
+#if defined(SPECIFIER_STRING_UPPER)
+ case SPECIFIER_STRING_UPPER:
+ flags |= FLAGS_WIDECHAR;
+ /* FALLTHROUGH */
+#endif
+ case SPECIFIER_STRING:
+ if (flags & FLAGS_LONG)
+ flags |= FLAGS_WIDECHAR;
+ else if (flags & FLAGS_SHORT)
+ flags &= ~FLAGS_WIDECHAR;
+ parameters[pos].type = FORMAT_STRING;
+ break;
+
+ case SPECIFIER_GROUP:
+ if (TYPE_SCAN == type)
+ {
+ int depth = 1;
+ parameters[pos].type = FORMAT_GROUP;
+ if (format[index] == QUALIFIER_CIRCUMFLEX)
+ index++;
+ if (format[index] == SPECIFIER_UNGROUP)
+ index++;
+ if (format[index] == QUALIFIER_MINUS)
+ index++;
+ /* Skip nested brackets */
+ while (format[index] != NIL)
+ {
+ if (format[index] == SPECIFIER_GROUP)
+ {
+ depth++;
+ }
+ else if (format[index] == SPECIFIER_UNGROUP)
+ {
+ if (--depth <= 0)
+ {
+ index++;
+ break;
+ }
+ }
+ index++;
+ }
+ }
+ break;
+
+ case SPECIFIER_INTEGER:
+ parameters[pos].type = FORMAT_INT;
+ break;
+
+ case SPECIFIER_UNSIGNED:
+ flags |= FLAGS_UNSIGNED;
+ parameters[pos].type = FORMAT_INT;
+ break;
+
+ case SPECIFIER_DECIMAL:
+ /* Disable base modifier */
+ flags &= ~FLAGS_BASE_PARAMETER;
+ base = BASE_DECIMAL;
+ parameters[pos].type = FORMAT_INT;
+ break;
+
+ case SPECIFIER_OCTAL:
+ flags |= FLAGS_UNSIGNED;
+ flags &= ~FLAGS_BASE_PARAMETER;
+ base = BASE_OCTAL;
+ parameters[pos].type = FORMAT_INT;
+ break;
+
+#if defined(SPECIFIER_BINARY)
+ case SPECIFIER_BINARY_UPPER:
+ flags |= FLAGS_UPPER;
+ /* FALLTHROUGH */
+ case SPECIFIER_BINARY:
+ flags |= FLAGS_NILPADDING;
+ flags &= ~FLAGS_BASE_PARAMETER;
+ base = BASE_BINARY;
+ parameters[pos].type = FORMAT_INT;
+ break;
+#endif
+
+ case SPECIFIER_HEX_UPPER:
+ flags |= FLAGS_UPPER;
+ /* FALLTHROUGH */
+ case SPECIFIER_HEX:
+ flags |= FLAGS_UNSIGNED;
+ flags &= ~FLAGS_BASE_PARAMETER;
+ base = BASE_HEX;
+ parameters[pos].type = FORMAT_INT;
+ break;
+
+ case SPECIFIER_FLOAT_E_UPPER:
+ flags |= FLAGS_UPPER;
+ /* FALLTHROUGH */
+ case SPECIFIER_FLOAT_E:
+ flags |= FLAGS_FLOAT_E;
+ parameters[pos].type = FORMAT_DOUBLE;
+ break;
+
+ case SPECIFIER_FLOAT_G_UPPER:
+ flags |= FLAGS_UPPER;
+ /* FALLTHROUGH */
+ case SPECIFIER_FLOAT_G:
+ flags |= FLAGS_FLOAT_G;
+ parameters[pos].type = FORMAT_DOUBLE;
+ break;
+
+ case SPECIFIER_FLOAT_F_UPPER:
+ flags |= FLAGS_UPPER;
+ /* FALLTHROUGH */
+ case SPECIFIER_FLOAT_F:
+ parameters[pos].type = FORMAT_DOUBLE;
+ break;
+
+ case SPECIFIER_POINTER:
+ if (sizeof(trio_pointer_t) == sizeof(trio_ulonglong_t))
+ flags |= FLAGS_QUAD;
+ else if (sizeof(trio_pointer_t) == sizeof(long))
+ flags |= FLAGS_LONG;
+ parameters[pos].type = FORMAT_POINTER;
+ break;
+
+ case SPECIFIER_COUNT:
+ parameters[pos].type = FORMAT_COUNT;
+ break;
+
+#if defined(SPECIFIER_HEXFLOAT)
+# if defined(SPECIFIER_HEXFLOAT_UPPER)
+ case SPECIFIER_HEXFLOAT_UPPER:
+ flags |= FLAGS_UPPER;
+ /* FALLTHROUGH */
+# endif
+ case SPECIFIER_HEXFLOAT:
+ base = BASE_HEX;
+ parameters[pos].type = FORMAT_DOUBLE;
+ break;
+#endif
+
+#if defined(FORMAT_ERRNO)
+ case SPECIFIER_ERRNO:
+ parameters[pos].type = FORMAT_ERRNO;
+ break;
+#endif
+
+#if defined(SPECIFIER_USER_DEFINED_BEGIN)
+ case SPECIFIER_USER_DEFINED_BEGIN:
+ {
+ unsigned int max;
+ int without_namespace = TRUE;
+
+ parameters[pos].type = FORMAT_USER_DEFINED;
+ parameters[pos].user_name[0] = NIL;
+ tmpformat = (char *)&format[index];
+
+ while ((ch = format[index]))
+ {
+ index++;
+ if (ch == SPECIFIER_USER_DEFINED_END)
+ {
+ if (without_namespace)
+ {
+ /* We must get the handle first */
+ parameters[pos].type = FORMAT_PARAMETER;
+ parameters[pos].indexAfterSpecifier = index;
+ parameters[pos].flags = FLAGS_USER_DEFINED;
+ /* Adjust parameters for insertion of new one */
+ pos++;
+ usedEntries[currentParam] += 1;
+ parameters[pos].type = FORMAT_USER_DEFINED;
+ currentParam++;
+ indices[currentParam] = pos;
+ if (currentParam > maxParam)
+ maxParam = currentParam;
+ }
+ /* Copy the user data */
+ max = (unsigned int)(&format[index] - tmpformat);
+ if (max > MAX_USER_DATA)
+ max = MAX_USER_DATA;
+ trio_copy_max(parameters[pos].user_data,
+ max,
+ tmpformat);
+ break; /* while */
+ }
+ if (ch == SPECIFIER_USER_DEFINED_SEPARATOR)
+ {
+ without_namespace = FALSE;
+ /* Copy the namespace for later looking-up */
+ max = (int)(&format[index] - tmpformat);
+ if (max > MAX_USER_NAME)
+ max = MAX_USER_NAME;
+ trio_copy_max(parameters[pos].user_name,
+ max,
+ tmpformat);
+ tmpformat = (char *)&format[index];
+ }
+ }
+ if (ch != SPECIFIER_USER_DEFINED_END)
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ }
+ break;
+#endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
+
+ default:
+ /* Bail out completely to make the error more obvious */
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ }
+
+ /* Count the number of times this entry has been used */
+ usedEntries[currentParam] += 1;
+
+ /* Find last sticky parameters */
+ if (gotSticky && !(flags & FLAGS_STICKY))
+ {
+ for (i = pos - 1; i >= 0; i--)
+ {
+ if (parameters[i].type == FORMAT_PARAMETER)
+ continue;
+ if ((parameters[i].flags & FLAGS_STICKY) &&
+ (parameters[i].type == parameters[pos].type))
+ {
+ /* Do not overwrite current qualifiers */
+ flags |= (parameters[i].flags & (unsigned long)~FLAGS_STICKY);
+ if (width == NO_WIDTH)
+ width = parameters[i].width;
+ if (precision == NO_PRECISION)
+ precision = parameters[i].precision;
+ if (base == NO_BASE)
+ base = parameters[i].base;
+ break;
+ }
+ }
+ }
+
+ parameters[pos].indexAfterSpecifier = index;
+ parameters[pos].flags = flags;
+ parameters[pos].width = width;
+ parameters[pos].precision = precision;
+ parameters[pos].base = (base == NO_BASE) ? BASE_DECIMAL : base;
+ parameters[pos].varsize = varsize;
+ pos++;
+
+ if (! positional)
+ parameterPosition++;
+
+ } /* if identifier */
+
+ } /* while format characters left */
+
+ for (num = 0; num <= maxParam; num++)
+ {
+ if (usedEntries[num] != 1)
+ {
+ if (usedEntries[num] == 0) /* gap detected */
+ return TRIO_ERROR_RETURN(TRIO_EGAP, num);
+ else /* double references detected */
+ return TRIO_ERROR_RETURN(TRIO_EDBLREF, num);
+ }
+
+ i = indices[num];
+
+ /*
+ * FORMAT_PARAMETERS are only present if they must be read,
+ * so it makes no sense to check the ignore flag (besides,
+ * the flags variable is not set for that particular type)
+ */
+ if ((parameters[i].type != FORMAT_PARAMETER) &&
+ (parameters[i].flags & FLAGS_IGNORE))
+ continue; /* for all arguments */
+
+ /*
+ * The stack arguments are read according to ANSI C89
+ * default argument promotions:
+ *
+ * char = int
+ * short = int
+ * unsigned char = unsigned int
+ * unsigned short = unsigned int
+ * float = double
+ *
+ * In addition to the ANSI C89 these types are read (the
+ * default argument promotions of C99 has not been
+ * considered yet)
+ *
+ * long long
+ * long double
+ * size_t
+ * ptrdiff_t
+ * intmax_t
+ */
+ switch (parameters[i].type)
+ {
+ case FORMAT_GROUP:
+ case FORMAT_STRING:
+#if TRIO_WIDECHAR
+ if (flags & FLAGS_WIDECHAR)
+ {
+ parameters[i].data.wstring = (argarray == NULL)
+ ? va_arg(*arglist, trio_wchar_t *)
+ : (trio_wchar_t *)(argarray[num]);
+ }
+ else
+#endif
+ {
+ parameters[i].data.string = (argarray == NULL)
+ ? va_arg(*arglist, char *)
+ : (char *)(argarray[num]);
+ }
+ break;
+
+#if defined(FORMAT_USER_DEFINED)
+ case FORMAT_USER_DEFINED:
+#endif
+ case FORMAT_POINTER:
+ case FORMAT_COUNT:
+ case FORMAT_UNKNOWN:
+ parameters[i].data.pointer = (argarray == NULL)
+ ? va_arg(*arglist, trio_pointer_t )
+ : argarray[num];
+ break;
+
+ case FORMAT_CHAR:
+ case FORMAT_INT:
+ if (TYPE_SCAN == type)
+ {
+ if (argarray == NULL)
+ parameters[i].data.pointer =
+ (trio_pointer_t)va_arg(*arglist, trio_pointer_t);
+ else
+ {
+ if (parameters[i].type == FORMAT_CHAR)
+ parameters[i].data.pointer =
+ (trio_pointer_t)((char *)argarray[num]);
+ else if (parameters[i].flags & FLAGS_SHORT)
+ parameters[i].data.pointer =
+ (trio_pointer_t)((short *)argarray[num]);
+ else
+ parameters[i].data.pointer =
+ (trio_pointer_t)((int *)argarray[num]);
+ }
+ }
+ else
+ {
+#if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
+ if (parameters[i].flags
+ & (FLAGS_VARSIZE_PARAMETER | FLAGS_FIXED_SIZE))
+ {
+ if (parameters[i].flags & FLAGS_VARSIZE_PARAMETER)
+ {
+ /*
+ * Variable sizes are mapped onto the fixed sizes, in
+ * accordance with integer promotion.
+ *
+ * Please note that this may not be portable, as we
+ * only guess the size, not the layout of the numbers.
+ * For example, if int is little-endian, and long is
+ * big-endian, then this will fail.
+ */
+ varsize = (int)parameters[parameters[i].varsize].data.number.as_unsigned;
+ }
+ else
+ {
+ /* Used for the I<bits> modifiers */
+ varsize = parameters[i].varsize;
+ }
+ parameters[i].flags &= ~FLAGS_ALL_VARSIZES;
+
+ if (varsize <= (int)sizeof(int))
+ ;
+ else if (varsize <= (int)sizeof(long))
+ parameters[i].flags |= FLAGS_LONG;
+#if defined(QUALIFIER_INTMAX_T)
+ else if (varsize <= (int)sizeof(trio_longlong_t))
+ parameters[i].flags |= FLAGS_QUAD;
+ else
+ parameters[i].flags |= FLAGS_INTMAX_T;
+#else
+ else
+ parameters[i].flags |= FLAGS_QUAD;
+#endif
+ }
+#endif /* defined(QUALIFIER_VARSIZE) */
+#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
+ if (parameters[i].flags & FLAGS_SIZE_T)
+ parameters[i].data.number.as_unsigned = (argarray == NULL)
+ ? (trio_uintmax_t)va_arg(*arglist, size_t)
+ : (trio_uintmax_t)(*((size_t *)argarray[num]));
+ else
+#endif
+#if defined(QUALIFIER_PTRDIFF_T)
+ if (parameters[i].flags & FLAGS_PTRDIFF_T)
+ parameters[i].data.number.as_unsigned = (argarray == NULL)
+ ? (trio_uintmax_t)va_arg(*arglist, ptrdiff_t)
+ : (trio_uintmax_t)(*((ptrdiff_t *)argarray[num]));
+ else
+#endif
+#if defined(QUALIFIER_INTMAX_T)
+ if (parameters[i].flags & FLAGS_INTMAX_T)
+ parameters[i].data.number.as_unsigned = (argarray == NULL)
+ ? (trio_uintmax_t)va_arg(*arglist, trio_intmax_t)
+ : (trio_uintmax_t)(*((trio_intmax_t *)argarray[num]));
+ else
+#endif
+ if (parameters[i].flags & FLAGS_QUAD)
+ parameters[i].data.number.as_unsigned = (argarray == NULL)
+ ? (trio_uintmax_t)va_arg(*arglist, trio_ulonglong_t)
+ : (trio_uintmax_t)(*((trio_ulonglong_t *)argarray[num]));
+ else if (parameters[i].flags & FLAGS_LONG)
+ parameters[i].data.number.as_unsigned = (argarray == NULL)
+ ? (trio_uintmax_t)va_arg(*arglist, long)
+ : (trio_uintmax_t)(*((long *)argarray[num]));
+ else
+ {
+ if (argarray == NULL)
+ parameters[i].data.number.as_unsigned = (trio_uintmax_t)va_arg(*arglist, int);
+ else
+ {
+ if (parameters[i].type == FORMAT_CHAR)
+ parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((char *)argarray[num]));
+ else if (parameters[i].flags & FLAGS_SHORT)
+ parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((short *)argarray[num]));
+ else
+ parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((int *)argarray[num]));
+ }
+ }
+ }
+ break;
+
+ case FORMAT_PARAMETER:
+ /*
+ * The parameter for the user-defined specifier is a pointer,
+ * whereas the rest (width, precision, base) uses an integer.
+ */
+ if (parameters[i].flags & FLAGS_USER_DEFINED)
+ parameters[i].data.pointer = (argarray == NULL)
+ ? va_arg(*arglist, trio_pointer_t )
+ : argarray[num];
+ else
+ parameters[i].data.number.as_unsigned = (argarray == NULL)
+ ? (trio_uintmax_t)va_arg(*arglist, int)
+ : (trio_uintmax_t)(*((int *)argarray[num]));
+ break;
+
+ case FORMAT_DOUBLE:
+ if (TYPE_SCAN == type)
+ {
+ if (parameters[i].flags & FLAGS_LONGDOUBLE)
+ parameters[i].data.longdoublePointer = (argarray == NULL)
+ ? va_arg(*arglist, trio_long_double_t *)
+ : (trio_long_double_t *)argarray[num];
+ else
+ {
+ if (parameters[i].flags & FLAGS_LONG)
+ parameters[i].data.doublePointer = (argarray == NULL)
+ ? va_arg(*arglist, double *)
+ : (double *)argarray[num];
+ else
+ parameters[i].data.doublePointer = (argarray == NULL)
+ ? (double *)va_arg(*arglist, float *)
+ : (double *)((float *)argarray[num]);
+ }
+ }
+ else
+ {
+ if (parameters[i].flags & FLAGS_LONGDOUBLE)
+ parameters[i].data.longdoubleNumber = (argarray == NULL)
+ ? va_arg(*arglist, trio_long_double_t)
+ : (trio_long_double_t)(*((trio_long_double_t *)argarray[num]));
+ else
+ {
+ if (argarray == NULL)
+ parameters[i].data.longdoubleNumber =
+ (trio_long_double_t)va_arg(*arglist, double);
+ else
+ {
+ if (parameters[i].flags & FLAGS_SHORT)
+ parameters[i].data.longdoubleNumber =
+ (trio_long_double_t)(*((float *)argarray[num]));
+ else
+ parameters[i].data.longdoubleNumber =
+ (trio_long_double_t)(*((double *)argarray[num]));
+ }
+ }
+ }
+ break;
+
+#if defined(FORMAT_ERRNO)
+ case FORMAT_ERRNO:
+ parameters[i].data.errorNumber = save_errno;
+ break;
+#endif
+
+ default:
+ break;
+ }
+ } /* for all specifiers */
+ return num;
+}
+
+
+/*************************************************************************
+ *
+ * FORMATTING
+ *
+ ************************************************************************/
+
+
+/*************************************************************************
+ * TrioWriteNumber
+ *
+ * Description:
+ * Output a number.
+ * The complexity of this function is a result of the complexity
+ * of the dependencies of the flags.
+ */
+TRIO_PRIVATE void
+TrioWriteNumber
+TRIO_ARGS6((self, number, flags, width, precision, base),
+ trio_class_t *self,
+ trio_uintmax_t number,
+ trio_flags_t flags,
+ int width,
+ int precision,
+ int base)
+{
+ BOOLEAN_T isNegative;
+ BOOLEAN_T isNumberZero;
+ BOOLEAN_T isPrecisionZero;
+ BOOLEAN_T ignoreNumber;
+ char buffer[MAX_CHARS_IN(trio_uintmax_t) * (1 + MAX_LOCALE_SEPARATOR_LENGTH) + 1];
+ char *bufferend;
+ char *pointer;
+ TRIO_CONST char *digits;
+ int i;
+ int length;
+ char *p;
+ int count;
+
+ assert(VALID(self));
+ assert(VALID(self->OutStream));
+ assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
+
+ digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
+ if (base == NO_BASE)
+ base = BASE_DECIMAL;
+
+ isNumberZero = (number == 0);
+ isPrecisionZero = (precision == 0);
+ ignoreNumber = (isNumberZero
+ && isPrecisionZero
+ && !((flags & FLAGS_ALTERNATIVE) && (base == BASE_OCTAL)));
+
+ if (flags & FLAGS_UNSIGNED)
+ {
+ isNegative = FALSE;
+ flags &= ~FLAGS_SHOWSIGN;
+ }
+ else
+ {
+ isNegative = ((trio_intmax_t)number < 0);
+ if (isNegative)
+ number = -((trio_intmax_t)number);
+ }
+
+ if (flags & FLAGS_QUAD)
+ number &= (trio_ulonglong_t)-1;
+ else if (flags & FLAGS_LONG)
+ number &= (unsigned long)-1;
+ else
+ number &= (unsigned int)-1;
+
+ /* Build number */
+ pointer = bufferend = &buffer[sizeof(buffer) - 1];
+ *pointer-- = NIL;
+ for (i = 1; i < (int)sizeof(buffer); i++)
+ {
+ *pointer-- = digits[number % base];
+ number /= base;
+ if (number == 0)
+ break;
+
+ if ((flags & FLAGS_QUOTE) && TrioFollowedBySeparator(i + 1))
+ {
+ /*
+ * We are building the number from the least significant
+ * to the most significant digit, so we have to copy the
+ * thousand separator backwards
+ */
+ length = internalThousandSeparatorLength;
+ if (((int)(pointer - buffer) - length) > 0)
+ {
+ p = &internalThousandSeparator[length - 1];
+ while (length-- > 0)
+ *pointer-- = *p--;
+ }
+ }
+ }
+
+ if (! ignoreNumber)
+ {
+ /* Adjust width */
+ width -= (bufferend - pointer) - 1;
+ }
+
+ /* Adjust precision */
+ if (NO_PRECISION != precision)
+ {
+ precision -= (bufferend - pointer) - 1;
+ if (precision < 0)
+ precision = 0;
+ flags |= FLAGS_NILPADDING;
+ }
+
+ /* Calculate padding */
+ count = (! ((flags & FLAGS_LEFTADJUST) || (precision == NO_PRECISION)))
+ ? precision
+ : 0;
+
+ /* Adjust width further */
+ if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
+ width--;
+ if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
+ {
+ switch (base)
+ {
+ case BASE_BINARY:
+ case BASE_HEX:
+ width -= 2;
+ break;
+ case BASE_OCTAL:
+ if (!(flags & FLAGS_NILPADDING) || (count == 0))
+ width--;
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* Output prefixes spaces if needed */
+ if (! ((flags & FLAGS_LEFTADJUST) ||
+ ((flags & FLAGS_NILPADDING) && (precision == NO_PRECISION))))
+ {
+ while (width-- > count)
+ self->OutStream(self, CHAR_ADJUST);
+ }
+
+ /* width has been adjusted for signs and alternatives */
+ if (isNegative)
+ self->OutStream(self, '-');
+ else if (flags & FLAGS_SHOWSIGN)
+ self->OutStream(self, '+');
+ else if (flags & FLAGS_SPACE)
+ self->OutStream(self, ' ');
+
+ /* Prefix is not written when the value is zero */
+ if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
+ {
+ switch (base)
+ {
+ case BASE_BINARY:
+ self->OutStream(self, '0');
+ self->OutStream(self, (flags & FLAGS_UPPER) ? 'B' : 'b');
+ break;
+
+ case BASE_OCTAL:
+ if (!(flags & FLAGS_NILPADDING) || (count == 0))
+ self->OutStream(self, '0');
+ break;
+
+ case BASE_HEX:
+ self->OutStream(self, '0');
+ self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
+ break;
+
+ default:
+ break;
+ } /* switch base */
+ }
+
+ /* Output prefixed zero padding if needed */
+ if (flags & FLAGS_NILPADDING)
+ {
+ if (precision == NO_PRECISION)
+ precision = width;
+ while (precision-- > 0)
+ {
+ self->OutStream(self, '0');
+ width--;
+ }
+ }
+
+ if (! ignoreNumber)
+ {
+ /* Output the number itself */
+ while (*(++pointer))
+ {
+ self->OutStream(self, *pointer);
+ }
+ }
+
+ /* Output trailing spaces if needed */
+ if (flags & FLAGS_LEFTADJUST)
+ {
+ while (width-- > 0)
+ self->OutStream(self, CHAR_ADJUST);
+ }
+}
+
+/*************************************************************************
+ * TrioWriteStringCharacter
+ *
+ * Description:
+ * Output a single character of a string
+ */
+TRIO_PRIVATE void
+TrioWriteStringCharacter
+TRIO_ARGS3((self, ch, flags),
+ trio_class_t *self,
+ int ch,
+ trio_flags_t flags)
+{
+ if (flags & FLAGS_ALTERNATIVE)
+ {
+ if (! isprint(ch))
+ {
+ /*
+ * Non-printable characters are converted to C escapes or
+ * \number, if no C escape exists.
+ */
+ self->OutStream(self, CHAR_BACKSLASH);
+ switch (ch)
+ {
+ case '\007': self->OutStream(self, 'a'); break;
+ case '\b': self->OutStream(self, 'b'); break;
+ case '\f': self->OutStream(self, 'f'); break;
+ case '\n': self->OutStream(self, 'n'); break;
+ case '\r': self->OutStream(self, 'r'); break;
+ case '\t': self->OutStream(self, 't'); break;
+ case '\v': self->OutStream(self, 'v'); break;
+ case '\\': self->OutStream(self, '\\'); break;
+ default:
+ self->OutStream(self, 'x');
+ TrioWriteNumber(self, (trio_uintmax_t)ch,
+ FLAGS_UNSIGNED | FLAGS_NILPADDING,
+ 2, 2, BASE_HEX);
+ break;
+ }
+ }
+ else if (ch == CHAR_BACKSLASH)
+ {
+ self->OutStream(self, CHAR_BACKSLASH);
+ self->OutStream(self, CHAR_BACKSLASH);
+ }
+ else
+ {
+ self->OutStream(self, ch);
+ }
+ }
+ else
+ {
+ self->OutStream(self, ch);
+ }
+}
+
+/*************************************************************************
+ * TrioWriteString
+ *
+ * Description:
+ * Output a string
+ */
+TRIO_PRIVATE void
+TrioWriteString
+TRIO_ARGS5((self, string, flags, width, precision),
+ trio_class_t *self,
+ TRIO_CONST char *string,
+ trio_flags_t flags,
+ int width,
+ int precision)
+{
+ int length;
+ int ch;
+
+ assert(VALID(self));
+ assert(VALID(self->OutStream));
+
+ if (string == NULL)
+ {
+ string = internalNullString;
+ length = sizeof(internalNullString) - 1;
+ /* Disable quoting for the null pointer */
+ flags &= (~FLAGS_QUOTE);
+ width = 0;
+ }
+ else
+ {
+ length = trio_length(string);
+ }
+ if ((NO_PRECISION != precision) &&
+ (precision < length))
+ {
+ length = precision;
+ }
+ width -= length;
+
+ if (flags & FLAGS_QUOTE)
+ self->OutStream(self, CHAR_QUOTE);
+
+ if (! (flags & FLAGS_LEFTADJUST))
+ {
+ while (width-- > 0)
+ self->OutStream(self, CHAR_ADJUST);
+ }
+
+ while (length-- > 0)
+ {
+ /* The ctype parameters must be an unsigned char (or EOF) */
+ ch = (int)((unsigned char)(*string++));
+ TrioWriteStringCharacter(self, ch, flags);
+ }
+
+ if (flags & FLAGS_LEFTADJUST)
+ {
+ while (width-- > 0)
+ self->OutStream(self, CHAR_ADJUST);
+ }
+ if (flags & FLAGS_QUOTE)
+ self->OutStream(self, CHAR_QUOTE);
+}
+
+/*************************************************************************
+ * TrioWriteWideStringCharacter
+ *
+ * Description:
+ * Output a wide string as a multi-byte sequence
+ */
+#if TRIO_WIDECHAR
+TRIO_PRIVATE int
+TrioWriteWideStringCharacter
+TRIO_ARGS4((self, wch, flags, width),
+ trio_class_t *self,
+ trio_wchar_t wch,
+ trio_flags_t flags,
+ int width)
+{
+ int size;
+ int i;
+ int ch;
+ char *string;
+ char buffer[MB_LEN_MAX + 1];
+
+ if (width == NO_WIDTH)
+ width = sizeof(buffer);
+
+ size = wctomb(buffer, wch);
+ if ((size <= 0) || (size > width) || (buffer[0] == NIL))
+ return 0;
+
+ string = buffer;
+ i = size;
+ while ((width >= i) && (width-- > 0) && (i-- > 0))
+ {
+ /* The ctype parameters must be an unsigned char (or EOF) */
+ ch = (int)((unsigned char)(*string++));
+ TrioWriteStringCharacter(self, ch, flags);
+ }
+ return size;
+}
+#endif /* TRIO_WIDECHAR */
+
+/*************************************************************************
+ * TrioWriteWideString
+ *
+ * Description:
+ * Output a wide character string as a multi-byte string
+ */
+#if TRIO_WIDECHAR
+TRIO_PRIVATE void
+TrioWriteWideString
+TRIO_ARGS5((self, wstring, flags, width, precision),
+ trio_class_t *self,
+ TRIO_CONST trio_wchar_t *wstring,
+ trio_flags_t flags,
+ int width,
+ int precision)
+{
+ int length;
+ int size;
+
+ assert(VALID(self));
+ assert(VALID(self->OutStream));
+
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ (void)mblen(NULL, 0);
+#endif
+
+ if (wstring == NULL)
+ {
+ TrioWriteString(self, NULL, flags, width, precision);
+ return;
+ }
+
+ if (NO_PRECISION == precision)
+ {
+ length = INT_MAX;
+ }
+ else
+ {
+ length = precision;
+ width -= length;
+ }
+
+ if (flags & FLAGS_QUOTE)
+ self->OutStream(self, CHAR_QUOTE);
+
+ if (! (flags & FLAGS_LEFTADJUST))
+ {
+ while (width-- > 0)
+ self->OutStream(self, CHAR_ADJUST);
+ }
+
+ while (length > 0)
+ {
+ size = TrioWriteWideStringCharacter(self, *wstring++, flags, length);
+ if (size == 0)
+ break; /* while */
+ length -= size;
+ }
+
+ if (flags & FLAGS_LEFTADJUST)
+ {
+ while (width-- > 0)
+ self->OutStream(self, CHAR_ADJUST);
+ }
+ if (flags & FLAGS_QUOTE)
+ self->OutStream(self, CHAR_QUOTE);
+}
+#endif /* TRIO_WIDECHAR */
+
+/*************************************************************************
+ * TrioWriteDouble
+ *
+ * http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_211.htm
+ *
+ * "5.2.4.2.2 paragraph #4
+ *
+ * The accuracy [...] is implementation defined, as is the accuracy
+ * of the conversion between floating-point internal representations
+ * and string representations performed by the libray routine in
+ * <stdio.h>"
+ */
+/* FIXME: handle all instances of constant long-double number (L)
+ * and *l() math functions.
+ */
+TRIO_PRIVATE void
+TrioWriteDouble
+TRIO_ARGS6((self, number, flags, width, precision, base),
+ trio_class_t *self,
+ trio_long_double_t number,
+ trio_flags_t flags,
+ int width,
+ int precision,
+ int base)
+{
+ trio_long_double_t integerNumber;
+ trio_long_double_t fractionNumber;
+ trio_long_double_t workNumber;
+ int integerDigits;
+ int fractionDigits;
+ int exponentDigits;
+ int baseDigits;
+ int integerThreshold;
+ int fractionThreshold;
+ int expectedWidth;
+ int exponent = 0;
+ unsigned int uExponent = 0;
+ int exponentBase;
+ trio_long_double_t dblBase;
+ trio_long_double_t dblIntegerBase;
+ trio_long_double_t dblFractionBase;
+ trio_long_double_t integerAdjust;
+ trio_long_double_t fractionAdjust;
+ BOOLEAN_T isNegative;
+ BOOLEAN_T isExponentNegative = FALSE;
+ BOOLEAN_T requireTwoDigitExponent;
+ BOOLEAN_T isHex;
+ TRIO_CONST char *digits;
+ char *groupingPointer;
+ int i;
+ int index;
+ BOOLEAN_T hasOnlyZeroes;
+ int zeroes = 0;
+ register int trailingZeroes;
+ BOOLEAN_T keepTrailingZeroes;
+ BOOLEAN_T keepDecimalPoint;
+ trio_long_double_t epsilon;
+
+ assert(VALID(self));
+ assert(VALID(self->OutStream));
+ assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
+
+ /* Determine sign and look for special quantities */
+ switch (trio_fpclassify_and_signbit(number, &isNegative))
+ {
+ case TRIO_FP_NAN:
+ TrioWriteString(self,
+ (flags & FLAGS_UPPER)
+ ? NAN_UPPER
+ : NAN_LOWER,
+ flags, width, precision);
+ return;
+
+ case TRIO_FP_INFINITE:
+ if (isNegative)
+ {
+ /* Negative infinity */
+ TrioWriteString(self,
+ (flags & FLAGS_UPPER)
+ ? "-" INFINITE_UPPER
+ : "-" INFINITE_LOWER,
+ flags, width, precision);
+ return;
+ }
+ else
+ {
+ /* Positive infinity */
+ TrioWriteString(self,
+ (flags & FLAGS_UPPER)
+ ? INFINITE_UPPER
+ : INFINITE_LOWER,
+ flags, width, precision);
+ return;
+ }
+
+ default:
+ /* Finitude */
+ break;
+ }
+
+ /* Normal numbers */
+ if (flags & FLAGS_LONGDOUBLE)
+ {
+ baseDigits = (base == 10)
+ ? LDBL_DIG
+ : (int)floor(LDBL_MANT_DIG / TrioLogarithmBase(base));
+ epsilon = LDBL_EPSILON;
+ }
+ else if (flags & FLAGS_SHORT)
+ {
+ baseDigits = (base == BASE_DECIMAL)
+ ? FLT_DIG
+ : (int)floor(FLT_MANT_DIG / TrioLogarithmBase(base));
+ epsilon = FLT_EPSILON;
+ }
+ else
+ {
+ baseDigits = (base == BASE_DECIMAL)
+ ? DBL_DIG
+ : (int)floor(DBL_MANT_DIG / TrioLogarithmBase(base));
+ epsilon = DBL_EPSILON;
+ }
+
+ digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
+ isHex = (base == BASE_HEX);
+ if (base == NO_BASE)
+ base = BASE_DECIMAL;
+ dblBase = (trio_long_double_t)base;
+ keepTrailingZeroes = !( (flags & FLAGS_ROUNDING) ||
+ ( (flags & FLAGS_FLOAT_G) &&
+ !(flags & FLAGS_ALTERNATIVE) ) );
+
+ if (flags & FLAGS_ROUNDING)
+ precision = baseDigits;
+
+ if (precision == NO_PRECISION)
+ {
+ if (isHex)
+ {
+ keepTrailingZeroes = FALSE;
+ precision = FLT_MANT_DIG;
+ }
+ else
+ {
+ precision = FLT_DIG;
+ }
+ }
+
+ if (isNegative)
+ number = -number;
+
+ if (isHex)
+ flags |= FLAGS_FLOAT_E;
+
+ if (flags & FLAGS_FLOAT_G)
+ {
+ if (precision == 0)
+ precision = 1;
+
+ if ((number < 1.0E-4) || (number > powl(base,
+ (trio_long_double_t)precision)))
+ {
+ /* Use scientific notation */
+ flags |= FLAGS_FLOAT_E;
+ }
+ else if (number < 1.0)
+ {
+ /*
+ * Use normal notation. If the integer part of the number is
+ * zero, then adjust the precision to include leading fractional
+ * zeros.
+ */
+ workNumber = TrioLogarithm(number, base);
+ workNumber = TRIO_FABS(workNumber);
+ if (workNumber - floorl(workNumber) < 0.001)
+ workNumber--;
+ zeroes = (int)floorl(workNumber);
+ }
+ }
+
+ if (flags & FLAGS_FLOAT_E)
+ {
+ /* Scale the number */
+ workNumber = TrioLogarithm(number, base);
+ if (trio_isinf(workNumber) == -1)
+ {
+ exponent = 0;
+ /* Undo setting */
+ if (flags & FLAGS_FLOAT_G)
+ flags &= ~FLAGS_FLOAT_E;
+ }
+ else
+ {
+ exponent = (int)floorl(workNumber);
+ number /= powl(dblBase, (trio_long_double_t)exponent);
+ isExponentNegative = (exponent < 0);
+ uExponent = (isExponentNegative) ? -exponent : exponent;
+ if (isHex)
+ uExponent *= 4; /* log16(2) */
+ /* No thousand separators */
+ flags &= ~FLAGS_QUOTE;
+ }
+ }
+
+ integerNumber = floorl(number);
+ fractionNumber = number - integerNumber;
+
+ /*
+ * Truncated number.
+ *
+ * Precision is number of significant digits for FLOAT_G
+ * and number of fractional digits for others.
+ */
+ integerDigits = (integerNumber > epsilon)
+ ? 1 + (int)TrioLogarithm(integerNumber, base)
+ : 1;
+ fractionDigits = ((flags & FLAGS_FLOAT_G) && (zeroes == 0))
+ ? precision - integerDigits
+ : zeroes + precision;
+
+ dblFractionBase = TrioPower(base, fractionDigits);
+
+ workNumber = number + 0.5 / dblFractionBase;
+ if (floorl(number) != floorl(workNumber))
+ {
+ if (flags & FLAGS_FLOAT_E)
+ {
+ /* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
+ exponent++;
+ isExponentNegative = (exponent < 0);
+ uExponent = (isExponentNegative) ? -exponent : exponent;
+ if (isHex)
+ uExponent *= 4; /* log16(2) */
+ workNumber = (number + 0.5 / dblFractionBase) / dblBase;
+ integerNumber = floorl(workNumber);
+ fractionNumber = workNumber - integerNumber;
+ }
+ else
+ {
+ /* Adjust if number was rounded up one digit (ie. 99 to 100) */
+ integerNumber = floorl(number + 0.5);
+ fractionNumber = 0.0;
+ integerDigits = (integerNumber > epsilon)
+ ? 1 + (int)TrioLogarithm(integerNumber, base)
+ : 1;
+ }
+ }
+
+ /* Estimate accuracy */
+ integerAdjust = fractionAdjust = 0.5;
+ if (flags & FLAGS_ROUNDING)
+ {
+ if (integerDigits > baseDigits)
+ {
+ integerThreshold = baseDigits;
+ fractionDigits = 0;
+ dblFractionBase = 1.0;
+ fractionThreshold = 0;
+ precision = 0; /* Disable decimal-point */
+ integerAdjust = TrioPower(base, integerDigits - integerThreshold - 1);
+ fractionAdjust = 0.0;
+ }
+ else
+ {
+ integerThreshold = integerDigits;
+ fractionThreshold = fractionDigits - integerThreshold;
+ fractionAdjust = 1.0;
+ }
+ }
+ else
+ {
+ integerThreshold = INT_MAX;
+ fractionThreshold = INT_MAX;
+ }
+
+ /*
+ * Calculate expected width.
+ * sign + integer part + thousands separators + decimal point
+ * + fraction + exponent
+ */
+ fractionAdjust /= dblFractionBase;
+ hasOnlyZeroes = (floorl((fractionNumber + fractionAdjust) * dblFractionBase) < epsilon);
+ keepDecimalPoint = ( (flags & FLAGS_ALTERNATIVE) ||
+ !((precision == 0) ||
+ (!keepTrailingZeroes && hasOnlyZeroes)) );
+ if (flags & FLAGS_FLOAT_E)
+ {
+ exponentDigits = (uExponent == 0)
+ ? 1
+ : (int)ceil(TrioLogarithm((double)(uExponent + 1),
+ (isHex) ? 10.0 : base));
+ }
+ else
+ exponentDigits = 0;
+ requireTwoDigitExponent = ((base == BASE_DECIMAL) && (exponentDigits == 1));
+
+ expectedWidth = integerDigits + fractionDigits
+ + (keepDecimalPoint
+ ? internalDecimalPointLength
+ : 0)
+ + ((flags & FLAGS_QUOTE)
+ ? TrioCalcThousandSeparatorLength(integerDigits)
+ : 0);
+ if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
+ expectedWidth += sizeof("-") - 1;
+ if (exponentDigits > 0)
+ expectedWidth += exponentDigits +
+ ((requireTwoDigitExponent ? sizeof("E+0") : sizeof("E+")) - 1);
+ if (isHex)
+ expectedWidth += sizeof("0X") - 1;
+
+ /* Output prefixing */
+ if (flags & FLAGS_NILPADDING)
+ {
+ /* Leading zeros must be after sign */
+ if (isNegative)
+ self->OutStream(self, '-');
+ else if (flags & FLAGS_SHOWSIGN)
+ self->OutStream(self, '+');
+ else if (flags & FLAGS_SPACE)
+ self->OutStream(self, ' ');
+ if (isHex)
+ {
+ self->OutStream(self, '0');
+ self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
+ }
+ if (!(flags & FLAGS_LEFTADJUST))
+ {
+ for (i = expectedWidth; i < width; i++)
+ {
+ self->OutStream(self, '0');
+ }
+ }
+ }
+ else
+ {
+ /* Leading spaces must be before sign */
+ if (!(flags & FLAGS_LEFTADJUST))
+ {
+ for (i = expectedWidth; i < width; i++)
+ {
+ self->OutStream(self, CHAR_ADJUST);
+ }
+ }
+ if (isNegative)
+ self->OutStream(self, '-');
+ else if (flags & FLAGS_SHOWSIGN)
+ self->OutStream(self, '+');
+ else if (flags & FLAGS_SPACE)
+ self->OutStream(self, ' ');
+ if (isHex)
+ {
+ self->OutStream(self, '0');
+ self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
+ }
+ }
+
+ /* Output the integer part and thousand separators */
+ dblIntegerBase = 1.0 / TrioPower(base, integerDigits - 1);
+ for (i = 0; i < integerDigits; i++)
+ {
+ workNumber = floorl(((integerNumber + integerAdjust) * dblIntegerBase));
+ if (i > integerThreshold)
+ {
+ /* Beyond accuracy */
+ self->OutStream(self, digits[0]);
+ }
+ else
+ {
+ self->OutStream(self, digits[(int)fmodl(workNumber, dblBase)]);
+ }
+ dblIntegerBase *= dblBase;
+
+ if (((flags & (FLAGS_FLOAT_E | FLAGS_QUOTE)) == FLAGS_QUOTE)
+ && TrioFollowedBySeparator(integerDigits - i))
+ {
+ for (groupingPointer = internalThousandSeparator;
+ *groupingPointer != NIL;
+ groupingPointer++)
+ {
+ self->OutStream(self, *groupingPointer);
+ }
+ }
+ }
+
+ /* Insert decimal point and build the fraction part */
+ trailingZeroes = 0;
+
+ if (keepDecimalPoint)
+ {
+ if (internalDecimalPoint)
+ {
+ self->OutStream(self, internalDecimalPoint);
+ }
+ else
+ {
+ for (i = 0; i < internalDecimalPointLength; i++)
+ {
+ self->OutStream(self, internalDecimalPointString[i]);
+ }
+ }
+ }
+
+ for (i = 0; i < fractionDigits; i++)
+ {
+ if ((integerDigits > integerThreshold) || (i > fractionThreshold))
+ {
+ /* Beyond accuracy */
+ trailingZeroes++;
+ }
+ else
+ {
+ fractionNumber *= dblBase;
+ fractionAdjust *= dblBase;
+ workNumber = floorl(fractionNumber + fractionAdjust);
+ fractionNumber -= workNumber;
+ index = (int)fmodl(workNumber, dblBase);
+ if (index == 0)
+ {
+ trailingZeroes++;
+ }
+ else
+ {
+ while (trailingZeroes > 0)
+ {
+ /* Not trailing zeroes after all */
+ self->OutStream(self, digits[0]);
+ trailingZeroes--;
+ }
+ self->OutStream(self, digits[index]);
+ }
+ }
+ }
+
+ if (keepTrailingZeroes)
+ {
+ while (trailingZeroes > 0)
+ {
+ self->OutStream(self, digits[0]);
+ trailingZeroes--;
+ }
+ }
+
+ /* Output exponent */
+ if (exponentDigits > 0)
+ {
+ self->OutStream(self,
+ isHex
+ ? ((flags & FLAGS_UPPER) ? 'P' : 'p')
+ : ((flags & FLAGS_UPPER) ? 'E' : 'e'));
+ self->OutStream(self, (isExponentNegative) ? '-' : '+');
+
+ /* The exponent must contain at least two digits */
+ if (requireTwoDigitExponent)
+ self->OutStream(self, '0');
+
+ if (isHex)
+ base = 10.0;
+ exponentBase = (int)TrioPower(base, exponentDigits - 1);
+ for (i = 0; i < exponentDigits; i++)
+ {
+ self->OutStream(self, digits[(uExponent / exponentBase) % base]);
+ exponentBase /= base;
+ }
+ }
+ /* Output trailing spaces */
+ if (flags & FLAGS_LEFTADJUST)
+ {
+ for (i = expectedWidth; i < width; i++)
+ {
+ self->OutStream(self, CHAR_ADJUST);
+ }
+ }
+}
+
+/*************************************************************************
+ * TrioFormatProcess
+ *
+ * Description:
+ * This is the main engine for formatting output
+ */
+TRIO_PRIVATE int
+TrioFormatProcess
+TRIO_ARGS3((data, format, parameters),
+ trio_class_t *data,
+ TRIO_CONST char *format,
+ trio_parameter_t *parameters)
+{
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ int charlen;
+#endif
+ int i;
+ TRIO_CONST char *string;
+ trio_pointer_t pointer;
+ trio_flags_t flags;
+ int width;
+ int precision;
+ int base;
+ int index;
+
+ index = 0;
+ i = 0;
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ (void)mblen(NULL, 0);
+#endif
+
+ while (format[index])
+ {
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ if (! isascii(format[index]))
+ {
+ charlen = mblen(&format[index], MB_LEN_MAX);
+ /*
+ * Only valid multibyte characters are handled here. Invalid
+ * multibyte characters (charlen == -1) are handled as normal
+ * characters.
+ */
+ if (charlen != -1)
+ {
+ while (charlen-- > 0)
+ {
+ data->OutStream(data, format[index++]);
+ }
+ continue; /* while characters left in formatting string */
+ }
+ }
+#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
+ if (CHAR_IDENTIFIER == format[index])
+ {
+ if (CHAR_IDENTIFIER == format[index + 1])
+ {
+ data->OutStream(data, CHAR_IDENTIFIER);
+ index += 2;
+ }
+ else
+ {
+ /* Skip the parameter entries */
+ while (parameters[i].type == FORMAT_PARAMETER)
+ i++;
+
+ flags = parameters[i].flags;
+
+ /* Find width */
+ width = parameters[i].width;
+ if (flags & FLAGS_WIDTH_PARAMETER)
+ {
+ /* Get width from parameter list */
+ width = (int)parameters[width].data.number.as_signed;
+ if (width < 0)
+ {
+ /*
+ * A negative width is the same as the - flag and
+ * a positive width.
+ */
+ flags |= FLAGS_LEFTADJUST;
+ flags &= ~FLAGS_NILPADDING;
+ width = -width;
+ }
+ }
+
+ /* Find precision */
+ if (flags & FLAGS_PRECISION)
+ {
+ precision = parameters[i].precision;
+ if (flags & FLAGS_PRECISION_PARAMETER)
+ {
+ /* Get precision from parameter list */
+ precision = (int)parameters[precision].data.number.as_signed;
+ if (precision < 0)
+ {
+ /*
+ * A negative precision is the same as no
+ * precision
+ */
+ precision = NO_PRECISION;
+ }
+ }
+ }
+ else
+ {
+ precision = NO_PRECISION;
+ }
+
+ /* Find base */
+ base = parameters[i].base;
+ if (flags & FLAGS_BASE_PARAMETER)
+ {
+ /* Get base from parameter list */
+ base = (int)parameters[base].data.number.as_signed;
+ }
+
+ switch (parameters[i].type)
+ {
+ case FORMAT_CHAR:
+ if (flags & FLAGS_QUOTE)
+ data->OutStream(data, CHAR_QUOTE);
+ if (! (flags & FLAGS_LEFTADJUST))
+ {
+ while (--width > 0)
+ data->OutStream(data, CHAR_ADJUST);
+ }
+#if TRIO_WIDECHAR
+ if (flags & FLAGS_WIDECHAR)
+ {
+ TrioWriteWideStringCharacter(data,
+ (trio_wchar_t)parameters[i].data.number.as_signed,
+ flags,
+ NO_WIDTH);
+ }
+ else
+#endif
+ {
+ TrioWriteStringCharacter(data,
+ (int)parameters[i].data.number.as_signed,
+ flags);
+ }
+
+ if (flags & FLAGS_LEFTADJUST)
+ {
+ while(--width > 0)
+ data->OutStream(data, CHAR_ADJUST);
+ }
+ if (flags & FLAGS_QUOTE)
+ data->OutStream(data, CHAR_QUOTE);
+
+ break; /* FORMAT_CHAR */
+
+ case FORMAT_INT:
+ TrioWriteNumber(data,
+ parameters[i].data.number.as_unsigned,
+ flags,
+ width,
+ precision,
+ base);
+
+ break; /* FORMAT_INT */
+
+ case FORMAT_DOUBLE:
+ TrioWriteDouble(data,
+ parameters[i].data.longdoubleNumber,
+ flags,
+ width,
+ precision,
+ base);
+ break; /* FORMAT_DOUBLE */
+
+ case FORMAT_STRING:
+#if TRIO_WIDECHAR
+ if (flags & FLAGS_WIDECHAR)
+ {
+ TrioWriteWideString(data,
+ parameters[i].data.wstring,
+ flags,
+ width,
+ precision);
+ }
+ else
+#endif
+ {
+ TrioWriteString(data,
+ parameters[i].data.string,
+ flags,
+ width,
+ precision);
+ }
+ break; /* FORMAT_STRING */
+
+ case FORMAT_POINTER:
+ {
+ trio_reference_t reference;
+
+ reference.data = data;
+ reference.parameter = ¶meters[i];
+ trio_print_pointer(&reference, parameters[i].data.pointer);
+ }
+ break; /* FORMAT_POINTER */
+
+ case FORMAT_COUNT:
+ pointer = parameters[i].data.pointer;
+ if (NULL != pointer)
+ {
+ /*
+ * C99 paragraph 7.19.6.1.8 says "the number of
+ * characters written to the output stream so far by
+ * this call", which is data->committed
+ */
+#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
+ if (flags & FLAGS_SIZE_T)
+ *(size_t *)pointer = (size_t)data->committed;
+ else
+#endif
+#if defined(QUALIFIER_PTRDIFF_T)
+ if (flags & FLAGS_PTRDIFF_T)
+ *(ptrdiff_t *)pointer = (ptrdiff_t)data->committed;
+ else
+#endif
+#if defined(QUALIFIER_INTMAX_T)
+ if (flags & FLAGS_INTMAX_T)
+ *(trio_intmax_t *)pointer = (trio_intmax_t)data->committed;
+ else
+#endif
+ if (flags & FLAGS_QUAD)
+ {
+ *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)data->committed;
+ }
+ else if (flags & FLAGS_LONG)
+ {
+ *(long int *)pointer = (long int)data->committed;
+ }
+ else if (flags & FLAGS_SHORT)
+ {
+ *(short int *)pointer = (short int)data->committed;
+ }
+ else
+ {
+ *(int *)pointer = (int)data->committed;
+ }
+ }
+ break; /* FORMAT_COUNT */
+
+ case FORMAT_PARAMETER:
+ break; /* FORMAT_PARAMETER */
+
+#if defined(FORMAT_ERRNO)
+ case FORMAT_ERRNO:
+ string = trio_error(parameters[i].data.errorNumber);
+ if (string)
+ {
+ TrioWriteString(data,
+ string,
+ flags,
+ width,
+ precision);
+ }
+ else
+ {
+ data->OutStream(data, '#');
+ TrioWriteNumber(data,
+ (trio_uintmax_t)parameters[i].data.errorNumber,
+ flags,
+ width,
+ precision,
+ BASE_DECIMAL);
+ }
+ break; /* FORMAT_ERRNO */
+#endif /* defined(FORMAT_ERRNO) */
+
+#if defined(FORMAT_USER_DEFINED)
+ case FORMAT_USER_DEFINED:
+ {
+ trio_reference_t reference;
+ trio_userdef_t *def = NULL;
+
+ if (parameters[i].user_name[0] == NIL)
+ {
+ /* Use handle */
+ if ((i > 0) ||
+ (parameters[i - 1].type == FORMAT_PARAMETER))
+ def = (trio_userdef_t *)parameters[i - 1].data.pointer;
+ }
+ else
+ {
+ /* Look up namespace */
+ def = TrioFindNamespace(parameters[i].user_name, NULL);
+ }
+ if (def) {
+ reference.data = data;
+ reference.parameter = ¶meters[i];
+ def->callback(&reference);
+ }
+ }
+ break;
+#endif /* defined(FORMAT_USER_DEFINED) */
+
+ default:
+ break;
+ } /* switch parameter type */
+
+ /* Prepare for next */
+ index = parameters[i].indexAfterSpecifier;
+ i++;
+ }
+ }
+ else /* not identifier */
+ {
+ data->OutStream(data, format[index++]);
+ }
+ }
+ return data->processed;
+}
+
+/*************************************************************************
+ * TrioFormatRef
+ */
+TRIO_PRIVATE int
+TrioFormatRef
+TRIO_ARGS4((reference, format, arglist, argarray),
+ trio_reference_t *reference,
+ TRIO_CONST char *format,
+ va_list *arglist,
+ trio_pointer_t *argarray)
+{
+ int status;
+ trio_parameter_t parameters[MAX_PARAMETERS];
+
+ status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
+ if (status < 0)
+ return status;
+
+ status = TrioFormatProcess(reference->data, format, parameters);
+ if (reference->data->error != 0)
+ {
+ status = reference->data->error;
+ }
+ return status;
+}
+
+/*************************************************************************
+ * TrioFormat
+ */
+TRIO_PRIVATE int
+TrioFormat
+TRIO_ARGS6((destination, destinationSize, OutStream, format, arglist, argarray),
+ trio_pointer_t destination,
+ size_t destinationSize,
+ void (*OutStream) TRIO_PROTO((trio_class_t *, int)),
+ TRIO_CONST char *format,
+ va_list *arglist,
+ trio_pointer_t *argarray)
+{
+ int status;
+ trio_class_t data;
+ trio_parameter_t parameters[MAX_PARAMETERS];
+
+ assert(VALID(OutStream));
+ assert(VALID(format));
+
+ memset(&data, 0, sizeof(data));
+ data.OutStream = OutStream;
+ data.location = destination;
+ data.max = destinationSize;
+ data.error = 0;
+
+#if defined(USE_LOCALE)
+ if (NULL == internalLocaleValues)
+ {
+ TrioSetLocale();
+ }
+#endif
+
+ status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
+ if (status < 0)
+ return status;
+
+ status = TrioFormatProcess(&data, format, parameters);
+ if (data.error != 0)
+ {
+ status = data.error;
+ }
+ return status;
+}
+
+/*************************************************************************
+ * TrioOutStreamFile
+ */
+TRIO_PRIVATE void
+TrioOutStreamFile
+TRIO_ARGS2((self, output),
+ trio_class_t *self,
+ int output)
+{
+ FILE *file;
+
+ assert(VALID(self));
+ assert(VALID(self->location));
+
+ file = (FILE *)self->location;
+ self->processed++;
+ if (fputc(output, file) == EOF)
+ {
+ self->error = TRIO_ERROR_RETURN(TRIO_EOF, 0);
+ }
+ else
+ {
+ self->committed++;
+ }
+}
+
+/*************************************************************************
+ * TrioOutStreamFileDescriptor
+ */
+TRIO_PRIVATE void
+TrioOutStreamFileDescriptor
+TRIO_ARGS2((self, output),
+ trio_class_t *self,
+ int output)
+{
+ int fd;
+ char ch;
+
+ assert(VALID(self));
+
+ fd = *((int *)self->location);
+ ch = (char)output;
+ self->processed++;
+ if (write(fd, &ch, sizeof(char)) == -1)
+ {
+ self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
+ }
+ else
+ {
+ self->committed++;
+ }
+}
+
+/*************************************************************************
+ * TrioOutStreamCustom
+ */
+TRIO_PRIVATE void
+TrioOutStreamCustom
+TRIO_ARGS2((self, output),
+ trio_class_t *self,
+ int output)
+{
+ int status;
+ trio_custom_t *data;
+
+ assert(VALID(self));
+ assert(VALID(self->location));
+
+ data = (trio_custom_t *)self->location;
+ if (data->stream.out)
+ {
+ status = (data->stream.out)(data->closure, output);
+ if (status >= 0)
+ {
+ self->committed++;
+ }
+ else
+ {
+ if (self->error == 0)
+ {
+ self->error = TRIO_ERROR_RETURN(TRIO_ECUSTOM, -status);
+ }
+ }
+ }
+ self->processed++;
+}
+
+/*************************************************************************
+ * TrioOutStreamString
+ */
+TRIO_PRIVATE void
+TrioOutStreamString
+TRIO_ARGS2((self, output),
+ trio_class_t *self,
+ int output)
+{
+ char **buffer;
+
+ assert(VALID(self));
+ assert(VALID(self->location));
+
+ buffer = (char **)self->location;
+ **buffer = (char)output;
+ (*buffer)++;
+ self->processed++;
+ self->committed++;
+}
+
+/*************************************************************************
+ * TrioOutStreamStringMax
+ */
+TRIO_PRIVATE void
+TrioOutStreamStringMax
+TRIO_ARGS2((self, output),
+ trio_class_t *self,
+ int output)
+{
+ char **buffer;
+
+ assert(VALID(self));
+ assert(VALID(self->location));
+
+ buffer = (char **)self->location;
+
+ if (self->processed < self->max)
+ {
+ **buffer = (char)output;
+ (*buffer)++;
+ self->committed++;
+ }
+ self->processed++;
+}
+
+/*************************************************************************
+ * TrioOutStreamStringDynamic
+ */
+TRIO_PRIVATE void
+TrioOutStreamStringDynamic
+TRIO_ARGS2((self, output),
+ trio_class_t *self,
+ int output)
+{
+ assert(VALID(self));
+ assert(VALID(self->location));
+
+ if (self->error == 0)
+ {
+ trio_xstring_append_char((trio_string_t *)self->location,
+ (char)output);
+ self->committed++;
+ }
+ /* The processed variable must always be increased */
+ self->processed++;
+}
+
+/*************************************************************************
+ *
+ * Formatted printing functions
+ *
+ ************************************************************************/
+
+#if defined(TRIO_DOCUMENTATION)
+# include "doc/doc_printf.h"
+#endif
+/** @addtogroup Printf
+ @{
+*/
+
+/*************************************************************************
+ * printf
+ */
+
+/**
+ Print to standard output stream.
+
+ @param format Formatting string.
+ @param ... Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_printf
+TRIO_VARGS2((format, va_alist),
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioFormat(stdout, 0, TrioOutStreamFile, format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+/**
+ Print to standard output stream.
+
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_vprintf
+TRIO_ARGS2((format, args),
+ TRIO_CONST char *format,
+ va_list args)
+{
+ assert(VALID(format));
+
+ return TrioFormat(stdout, 0, TrioOutStreamFile, format, &args, NULL);
+}
+
+/**
+ Print to standard output stream.
+
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_printfv
+TRIO_ARGS2((format, args),
+ TRIO_CONST char *format,
+ trio_pointer_t * args)
+{
+ assert(VALID(format));
+
+ return TrioFormat(stdout, 0, TrioOutStreamFile, format, NULL, args);
+}
+
+/*************************************************************************
+ * fprintf
+ */
+
+/**
+ Print to file.
+
+ @param file File pointer.
+ @param format Formatting string.
+ @param ... Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_fprintf
+TRIO_VARGS3((file, format, va_alist),
+ FILE *file,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(file));
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioFormat(file, 0, TrioOutStreamFile, format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+/**
+ Print to file.
+
+ @param file File pointer.
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_vfprintf
+TRIO_ARGS3((file, format, args),
+ FILE *file,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ assert(VALID(file));
+ assert(VALID(format));
+
+ return TrioFormat(file, 0, TrioOutStreamFile, format, &args, NULL);
+}
+
+/**
+ Print to file.
+
+ @param file File pointer.
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_fprintfv
+TRIO_ARGS3((file, format, args),
+ FILE *file,
+ TRIO_CONST char *format,
+ trio_pointer_t * args)
+{
+ assert(VALID(file));
+ assert(VALID(format));
+
+ return TrioFormat(file, 0, TrioOutStreamFile, format, NULL, args);
+}
+
+/*************************************************************************
+ * dprintf
+ */
+
+/**
+ Print to file descriptor.
+
+ @param fd File descriptor.
+ @param format Formatting string.
+ @param ... Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_dprintf
+TRIO_VARGS3((fd, format, va_alist),
+ int fd,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+/**
+ Print to file descriptor.
+
+ @param fd File descriptor.
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_vdprintf
+TRIO_ARGS3((fd, format, args),
+ int fd,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ assert(VALID(format));
+
+ return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, &args, NULL);
+}
+
+/**
+ Print to file descriptor.
+
+ @param fd File descriptor.
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_dprintfv
+TRIO_ARGS3((fd, format, args),
+ int fd,
+ TRIO_CONST char *format,
+ trio_pointer_t *args)
+{
+ assert(VALID(format));
+
+ return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, NULL, args);
+}
+
+/*************************************************************************
+ * cprintf
+ */
+TRIO_PUBLIC int
+trio_cprintf
+TRIO_VARGS4((stream, closure, format, va_alist),
+ trio_outstream_t stream,
+ trio_pointer_t closure,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+ trio_custom_t data;
+
+ assert(VALID(stream));
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ data.stream.out = stream;
+ data.closure = closure;
+ status = TrioFormat(&data, 0, TrioOutStreamCustom, format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+TRIO_PUBLIC int
+trio_vcprintf
+TRIO_ARGS4((stream, closure, format, args),
+ trio_outstream_t stream,
+ trio_pointer_t closure,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ trio_custom_t data;
+
+ assert(VALID(stream));
+ assert(VALID(format));
+
+ data.stream.out = stream;
+ data.closure = closure;
+ return TrioFormat(&data, 0, TrioOutStreamCustom, format, &args, NULL);
+}
+
+TRIO_PUBLIC int
+trio_cprintfv
+TRIO_ARGS4((stream, closure, format, args),
+ trio_outstream_t stream,
+ trio_pointer_t closure,
+ TRIO_CONST char *format,
+ void **args)
+{
+ trio_custom_t data;
+
+ assert(VALID(stream));
+ assert(VALID(format));
+
+ data.stream.out = stream;
+ data.closure = closure;
+ return TrioFormat(&data, 0, TrioOutStreamCustom, format, NULL, args);
+}
+
+/*************************************************************************
+ * sprintf
+ */
+
+/**
+ Print to string.
+
+ @param buffer Output string.
+ @param format Formatting string.
+ @param ... Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_sprintf
+TRIO_VARGS3((buffer, format, va_alist),
+ char *buffer,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioFormat(&buffer, 0, TrioOutStreamString, format, &args, NULL);
+ *buffer = NIL; /* Terminate with NIL character */
+ TRIO_VA_END(args);
+ return status;
+}
+
+/**
+ Print to string.
+
+ @param buffer Output string.
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_vsprintf
+TRIO_ARGS3((buffer, format, args),
+ char *buffer,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ int status;
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ status = TrioFormat(&buffer, 0, TrioOutStreamString, format, &args, NULL);
+ *buffer = NIL;
+ return status;
+}
+
+/**
+ Print to string.
+
+ @param buffer Output string.
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_sprintfv
+TRIO_ARGS3((buffer, format, args),
+ char *buffer,
+ TRIO_CONST char *format,
+ trio_pointer_t *args)
+{
+ int status;
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ status = TrioFormat(&buffer, 0, TrioOutStreamString, format, NULL, args);
+ *buffer = NIL;
+ return status;
+}
+
+/*************************************************************************
+ * snprintf
+ */
+
+/**
+ Print at most @p max characters to string.
+
+ @param buffer Output string.
+ @param max Maximum number of characters to print.
+ @param format Formatting string.
+ @param ... Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_snprintf
+TRIO_VARGS4((buffer, max, format, va_alist),
+ char *buffer,
+ size_t max,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
+ TrioOutStreamStringMax, format, &args, NULL);
+ if (max > 0)
+ *buffer = NIL;
+ TRIO_VA_END(args);
+ return status;
+}
+
+/**
+ Print at most @p max characters to string.
+
+ @param buffer Output string.
+ @param max Maximum number of characters to print.
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_vsnprintf
+TRIO_ARGS4((buffer, max, format, args),
+ char *buffer,
+ size_t max,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ int status;
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
+ TrioOutStreamStringMax, format, &args, NULL);
+ if (max > 0)
+ *buffer = NIL;
+ return status;
+}
+
+/**
+ Print at most @p max characters to string.
+
+ @param buffer Output string.
+ @param max Maximum number of characters to print.
+ @param format Formatting string.
+ @param args Arguments.
+ @return Number of printed characters.
+ */
+TRIO_PUBLIC int
+trio_snprintfv
+TRIO_ARGS4((buffer, max, format, args),
+ char *buffer,
+ size_t max,
+ TRIO_CONST char *format,
+ trio_pointer_t *args)
+{
+ int status;
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
+ TrioOutStreamStringMax, format, NULL, args);
+ if (max > 0)
+ *buffer = NIL;
+ return status;
+}
+
+/*************************************************************************
+ * snprintfcat
+ * Appends the new string to the buffer string overwriting the '\0'
+ * character at the end of buffer.
+ */
+TRIO_PUBLIC int
+trio_snprintfcat
+TRIO_VARGS4((buffer, max, format, va_alist),
+ char *buffer,
+ size_t max,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+ size_t buf_len;
+
+ TRIO_VA_START(args, format);
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ buf_len = trio_length(buffer);
+ buffer = &buffer[buf_len];
+
+ status = TrioFormat(&buffer, max - 1 - buf_len,
+ TrioOutStreamStringMax, format, &args, NULL);
+ TRIO_VA_END(args);
+ *buffer = NIL;
+ return status;
+}
+
+TRIO_PUBLIC int
+trio_vsnprintfcat
+TRIO_ARGS4((buffer, max, format, args),
+ char *buffer,
+ size_t max,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ int status;
+ size_t buf_len;
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ buf_len = trio_length(buffer);
+ buffer = &buffer[buf_len];
+ status = TrioFormat(&buffer, max - 1 - buf_len,
+ TrioOutStreamStringMax, format, &args, NULL);
+ *buffer = NIL;
+ return status;
+}
+
+/*************************************************************************
+ * trio_aprintf
+ */
+
+/* Deprecated */
+TRIO_PUBLIC char *
+trio_aprintf
+TRIO_VARGS2((format, va_alist),
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ va_list args;
+ trio_string_t *info;
+ char *result = NULL;
+
+ assert(VALID(format));
+
+ info = trio_xstring_duplicate("");
+ if (info)
+ {
+ TRIO_VA_START(args, format);
+ (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
+ format, &args, NULL);
+ TRIO_VA_END(args);
+
+ trio_string_terminate(info);
+ result = trio_string_extract(info);
+ trio_string_destroy(info);
+ }
+ return result;
+}
+
+/* Deprecated */
+TRIO_PUBLIC char *
+trio_vaprintf
+TRIO_ARGS2((format, args),
+ TRIO_CONST char *format,
+ va_list args)
+{
+ trio_string_t *info;
+ char *result = NULL;
+
+ assert(VALID(format));
+
+ info = trio_xstring_duplicate("");
+ if (info)
+ {
+ (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
+ format, &args, NULL);
+ trio_string_terminate(info);
+ result = trio_string_extract(info);
+ trio_string_destroy(info);
+ }
+ return result;
+}
+
+TRIO_PUBLIC int
+trio_asprintf
+TRIO_VARGS3((result, format, va_alist),
+ char **result,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ va_list args;
+ int status;
+ trio_string_t *info;
+
+ assert(VALID(format));
+
+ *result = NULL;
+
+ info = trio_xstring_duplicate("");
+ if (info == NULL)
+ {
+ status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
+ }
+ else
+ {
+ TRIO_VA_START(args, format);
+ status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
+ format, &args, NULL);
+ TRIO_VA_END(args);
+ if (status >= 0)
+ {
+ trio_string_terminate(info);
+ *result = trio_string_extract(info);
+ }
+ trio_string_destroy(info);
+ }
+ return status;
+}
+
+TRIO_PUBLIC int
+trio_vasprintf
+TRIO_ARGS3((result, format, args),
+ char **result,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ int status;
+ trio_string_t *info;
+
+ assert(VALID(format));
+
+ *result = NULL;
+
+ info = trio_xstring_duplicate("");
+ if (info == NULL)
+ {
+ status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
+ }
+ else
+ {
+ status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
+ format, &args, NULL);
+ if (status >= 0)
+ {
+ trio_string_terminate(info);
+ *result = trio_string_extract(info);
+ }
+ trio_string_destroy(info);
+ }
+ return status;
+}
+
+/** @} End of Printf documentation module */
+
+/*************************************************************************
+ *
+ * CALLBACK
+ *
+ ************************************************************************/
+
+#if defined(TRIO_DOCUMENTATION)
+# include "doc/doc_register.h"
+#endif
+/**
+ @addtogroup UserDefined
+ @{
+*/
+
+#if TRIO_EXTENSION
+
+/*************************************************************************
+ * trio_register
+ */
+
+/**
+ Register new user-defined specifier.
+
+ @param callback
+ @param name
+ @return Handle.
+ */
+TRIO_PUBLIC trio_pointer_t
+trio_register
+TRIO_ARGS2((callback, name),
+ trio_callback_t callback,
+ TRIO_CONST char *name)
+{
+ trio_userdef_t *def;
+ trio_userdef_t *prev = NULL;
+
+ if (callback == NULL)
+ return NULL;
+
+ if (name)
+ {
+ /* Handle built-in namespaces */
+ if (name[0] == ':')
+ {
+ if (trio_equal(name, ":enter"))
+ {
+ internalEnterCriticalRegion = callback;
+ }
+ else if (trio_equal(name, ":leave"))
+ {
+ internalLeaveCriticalRegion = callback;
+ }
+ return NULL;
+ }
+
+ /* Bail out if namespace is too long */
+ if (trio_length(name) >= MAX_USER_NAME)
+ return NULL;
+
+ /* Bail out if namespace already is registered */
+ def = TrioFindNamespace(name, &prev);
+ if (def)
+ return NULL;
+ }
+
+ def = (trio_userdef_t *)TRIO_MALLOC(sizeof(trio_userdef_t));
+ if (def)
+ {
+ if (internalEnterCriticalRegion)
+ (void)internalEnterCriticalRegion(NULL);
+
+ if (name)
+ {
+ /* Link into internal list */
+ if (prev == NULL)
+ internalUserDef = def;
+ else
+ prev->next = def;
+ }
+ /* Initialize */
+ def->callback = callback;
+ def->name = (name == NULL)
+ ? NULL
+ : trio_duplicate(name);
+ def->next = NULL;
+
+ if (internalLeaveCriticalRegion)
+ (void)internalLeaveCriticalRegion(NULL);
+ }
+ return (trio_pointer_t)def;
+}
+
+/**
+ Unregister an existing user-defined specifier.
+
+ @param handle
+ */
+void
+trio_unregister
+TRIO_ARGS1((handle),
+ trio_pointer_t handle)
+{
+ trio_userdef_t *self = (trio_userdef_t *)handle;
+ trio_userdef_t *def;
+ trio_userdef_t *prev = NULL;
+
+ assert(VALID(self));
+
+ if (self->name)
+ {
+ def = TrioFindNamespace(self->name, &prev);
+ if (def)
+ {
+ if (internalEnterCriticalRegion)
+ (void)internalEnterCriticalRegion(NULL);
+
+ if (prev == NULL)
+ internalUserDef = NULL;
+ else
+ prev->next = def->next;
+
+ if (internalLeaveCriticalRegion)
+ (void)internalLeaveCriticalRegion(NULL);
+ }
+ trio_destroy(self->name);
+ }
+ TRIO_FREE(self);
+}
+
+/*************************************************************************
+ * trio_get_format [public]
+ */
+TRIO_CONST char *
+trio_get_format
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+#if defined(FORMAT_USER_DEFINED)
+ assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
+#endif
+
+ return (((trio_reference_t *)ref)->parameter->user_data);
+}
+
+/*************************************************************************
+ * trio_get_argument [public]
+ */
+trio_pointer_t
+trio_get_argument
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+#if defined(FORMAT_USER_DEFINED)
+ assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
+#endif
+
+ return ((trio_reference_t *)ref)->parameter->data.pointer;
+}
+
+/*************************************************************************
+ * trio_get_width / trio_set_width [public]
+ */
+int
+trio_get_width
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return ((trio_reference_t *)ref)->parameter->width;
+}
+
+void
+trio_set_width
+TRIO_ARGS2((ref, width),
+ trio_pointer_t ref,
+ int width)
+{
+ ((trio_reference_t *)ref)->parameter->width = width;
+}
+
+/*************************************************************************
+ * trio_get_precision / trio_set_precision [public]
+ */
+int
+trio_get_precision
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->precision);
+}
+
+void
+trio_set_precision
+TRIO_ARGS2((ref, precision),
+ trio_pointer_t ref,
+ int precision)
+{
+ ((trio_reference_t *)ref)->parameter->precision = precision;
+}
+
+/*************************************************************************
+ * trio_get_base / trio_set_base [public]
+ */
+int
+trio_get_base
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->base);
+}
+
+void
+trio_set_base
+TRIO_ARGS2((ref, base),
+ trio_pointer_t ref,
+ int base)
+{
+ ((trio_reference_t *)ref)->parameter->base = base;
+}
+
+/*************************************************************************
+ * trio_get_long / trio_set_long [public]
+ */
+int
+trio_get_long
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONG)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_long
+TRIO_ARGS2((ref, is_long),
+ trio_pointer_t ref,
+ int is_long)
+{
+ if (is_long)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONG;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONG;
+}
+
+/*************************************************************************
+ * trio_get_longlong / trio_set_longlong [public]
+ */
+int
+trio_get_longlong
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUAD)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_longlong
+TRIO_ARGS2((ref, is_longlong),
+ trio_pointer_t ref,
+ int is_longlong)
+{
+ if (is_longlong)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUAD;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUAD;
+}
+
+/*************************************************************************
+ * trio_get_longdouble / trio_set_longdouble [public]
+ */
+int
+trio_get_longdouble
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONGDOUBLE)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_longdouble
+TRIO_ARGS2((ref, is_longdouble),
+ trio_pointer_t ref,
+ int is_longdouble)
+{
+ if (is_longdouble)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONGDOUBLE;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONGDOUBLE;
+}
+
+/*************************************************************************
+ * trio_get_short / trio_set_short [public]
+ */
+int
+trio_get_short
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORT)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_short
+TRIO_ARGS2((ref, is_short),
+ trio_pointer_t ref,
+ int is_short)
+{
+ if (is_short)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORT;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORT;
+}
+
+/*************************************************************************
+ * trio_get_shortshort / trio_set_shortshort [public]
+ */
+int
+trio_get_shortshort
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORTSHORT)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_shortshort
+TRIO_ARGS2((ref, is_shortshort),
+ trio_pointer_t ref,
+ int is_shortshort)
+{
+ if (is_shortshort)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORTSHORT;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORTSHORT;
+}
+
+/*************************************************************************
+ * trio_get_alternative / trio_set_alternative [public]
+ */
+int
+trio_get_alternative
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_ALTERNATIVE)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_alternative
+TRIO_ARGS2((ref, is_alternative),
+ trio_pointer_t ref,
+ int is_alternative)
+{
+ if (is_alternative)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_ALTERNATIVE;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_ALTERNATIVE;
+}
+
+/*************************************************************************
+ * trio_get_alignment / trio_set_alignment [public]
+ */
+int
+trio_get_alignment
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LEFTADJUST)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_alignment
+TRIO_ARGS2((ref, is_leftaligned),
+ trio_pointer_t ref,
+ int is_leftaligned)
+{
+ if (is_leftaligned)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LEFTADJUST;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LEFTADJUST;
+}
+
+/*************************************************************************
+ * trio_get_spacing /trio_set_spacing [public]
+ */
+int
+trio_get_spacing
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SPACE)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_spacing
+TRIO_ARGS2((ref, is_space),
+ trio_pointer_t ref,
+ int is_space)
+{
+ if (is_space)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SPACE;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SPACE;
+}
+
+/*************************************************************************
+ * trio_get_sign / trio_set_sign [public]
+ */
+int
+trio_get_sign
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHOWSIGN)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_sign
+TRIO_ARGS2((ref, is_sign),
+ trio_pointer_t ref,
+ int is_sign)
+{
+ if (is_sign)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHOWSIGN;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHOWSIGN;
+}
+
+/*************************************************************************
+ * trio_get_padding / trio_set_padding [public]
+ */
+int
+trio_get_padding
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_NILPADDING)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_padding
+TRIO_ARGS2((ref, is_padding),
+ trio_pointer_t ref,
+ int is_padding)
+{
+ if (is_padding)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_NILPADDING;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_NILPADDING;
+}
+
+/*************************************************************************
+ * trio_get_quote / trio_set_quote [public]
+ */
+int
+trio_get_quote
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUOTE)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_quote
+TRIO_ARGS2((ref, is_quote),
+ trio_pointer_t ref,
+ int is_quote)
+{
+ if (is_quote)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUOTE;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUOTE;
+}
+
+/*************************************************************************
+ * trio_get_upper / trio_set_upper [public]
+ */
+int
+trio_get_upper
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_UPPER)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_upper
+TRIO_ARGS2((ref, is_upper),
+ trio_pointer_t ref,
+ int is_upper)
+{
+ if (is_upper)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_UPPER;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_UPPER;
+}
+
+/*************************************************************************
+ * trio_get_largest / trio_set_largest [public]
+ */
+#if TRIO_C99
+int
+trio_get_largest
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_INTMAX_T)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_largest
+TRIO_ARGS2((ref, is_largest),
+ trio_pointer_t ref,
+ int is_largest)
+{
+ if (is_largest)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_INTMAX_T;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_INTMAX_T;
+}
+#endif
+
+/*************************************************************************
+ * trio_get_ptrdiff / trio_set_ptrdiff [public]
+ */
+int
+trio_get_ptrdiff
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_PTRDIFF_T)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_ptrdiff
+TRIO_ARGS2((ref, is_ptrdiff),
+ trio_pointer_t ref,
+ int is_ptrdiff)
+{
+ if (is_ptrdiff)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_PTRDIFF_T;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_PTRDIFF_T;
+}
+
+/*************************************************************************
+ * trio_get_size / trio_set_size [public]
+ */
+#if TRIO_C99
+int
+trio_get_size
+TRIO_ARGS1((ref),
+ trio_pointer_t ref)
+{
+ return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SIZE_T)
+ ? TRUE
+ : FALSE;
+}
+
+void
+trio_set_size
+TRIO_ARGS2((ref, is_size),
+ trio_pointer_t ref,
+ int is_size)
+{
+ if (is_size)
+ ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SIZE_T;
+ else
+ ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SIZE_T;
+}
+#endif
+
+/*************************************************************************
+ * trio_print_int [public]
+ */
+void
+trio_print_int
+TRIO_ARGS2((ref, number),
+ trio_pointer_t ref,
+ int number)
+{
+ trio_reference_t *self = (trio_reference_t *)ref;
+
+ TrioWriteNumber(self->data,
+ (trio_uintmax_t)number,
+ self->parameter->flags,
+ self->parameter->width,
+ self->parameter->precision,
+ self->parameter->base);
+}
+
+/*************************************************************************
+ * trio_print_uint [public]
+ */
+void
+trio_print_uint
+TRIO_ARGS2((ref, number),
+ trio_pointer_t ref,
+ unsigned int number)
+{
+ trio_reference_t *self = (trio_reference_t *)ref;
+
+ TrioWriteNumber(self->data,
+ (trio_uintmax_t)number,
+ self->parameter->flags | FLAGS_UNSIGNED,
+ self->parameter->width,
+ self->parameter->precision,
+ self->parameter->base);
+}
+
+/*************************************************************************
+ * trio_print_double [public]
+ */
+void
+trio_print_double
+TRIO_ARGS2((ref, number),
+ trio_pointer_t ref,
+ double number)
+{
+ trio_reference_t *self = (trio_reference_t *)ref;
+
+ TrioWriteDouble(self->data,
+ number,
+ self->parameter->flags,
+ self->parameter->width,
+ self->parameter->precision,
+ self->parameter->base);
+}
+
+/*************************************************************************
+ * trio_print_string [public]
+ */
+void
+trio_print_string
+TRIO_ARGS2((ref, string),
+ trio_pointer_t ref,
+ char *string)
+{
+ trio_reference_t *self = (trio_reference_t *)ref;
+
+ TrioWriteString(self->data,
+ string,
+ self->parameter->flags,
+ self->parameter->width,
+ self->parameter->precision);
+}
+
+/*************************************************************************
+ * trio_print_ref [public]
+ */
+int
+trio_print_ref
+TRIO_VARGS3((ref, format, va_alist),
+ trio_pointer_t ref,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list arglist;
+
+ assert(VALID(format));
+
+ TRIO_VA_START(arglist, format);
+ status = TrioFormatRef((trio_reference_t *)ref, format, &arglist, NULL);
+ TRIO_VA_END(arglist);
+ return status;
+}
+
+/*************************************************************************
+ * trio_vprint_ref [public]
+ */
+int
+trio_vprint_ref
+TRIO_ARGS3((ref, format, arglist),
+ trio_pointer_t ref,
+ TRIO_CONST char *format,
+ va_list arglist)
+{
+ assert(VALID(format));
+
+ return TrioFormatRef((trio_reference_t *)ref, format, &arglist, NULL);
+}
+
+/*************************************************************************
+ * trio_printv_ref [public]
+ */
+int
+trio_printv_ref
+TRIO_ARGS3((ref, format, argarray),
+ trio_pointer_t ref,
+ TRIO_CONST char *format,
+ trio_pointer_t *argarray)
+{
+ assert(VALID(format));
+
+ return TrioFormatRef((trio_reference_t *)ref, format, NULL, argarray);
+}
+
+#endif /* TRIO_EXTENSION */
+
+/*************************************************************************
+ * trio_print_pointer [public]
+ */
+void
+trio_print_pointer
+TRIO_ARGS2((ref, pointer),
+ trio_pointer_t ref,
+ trio_pointer_t pointer)
+{
+ trio_reference_t *self = (trio_reference_t *)ref;
+ trio_flags_t flags;
+ trio_uintmax_t number;
+
+ if (NULL == pointer)
+ {
+ TRIO_CONST char *string = internalNullString;
+ while (*string)
+ self->data->OutStream(self->data, *string++);
+ }
+ else
+ {
+ /*
+ * The subtraction of the null pointer is a workaround
+ * to avoid a compiler warning. The performance overhead
+ * is negligible (and likely to be removed by an
+ * optimizing compiler). The (char *) casting is done
+ * to please ANSI C++.
+ */
+ number = (trio_uintmax_t)((char *)pointer - (char *)0);
+ /* Shrink to size of pointer */
+ number &= (trio_uintmax_t)-1;
+ flags = self->parameter->flags;
+ flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE |
+ FLAGS_NILPADDING);
+ TrioWriteNumber(self->data,
+ number,
+ flags,
+ POINTER_WIDTH,
+ NO_PRECISION,
+ BASE_HEX);
+ }
+}
+
+/** @} End of UserDefined documentation module */
+
+/*************************************************************************
+ *
+ * LOCALES
+ *
+ ************************************************************************/
+
+/*************************************************************************
+ * trio_locale_set_decimal_point
+ *
+ * Decimal point can only be one character. The input argument is a
+ * string to enable multibyte characters. At most MB_LEN_MAX characters
+ * will be used.
+ */
+TRIO_PUBLIC void
+trio_locale_set_decimal_point
+TRIO_ARGS1((decimalPoint),
+ char *decimalPoint)
+{
+#if defined(USE_LOCALE)
+ if (NULL == internalLocaleValues)
+ {
+ TrioSetLocale();
+ }
+#endif
+ internalDecimalPointLength = trio_length(decimalPoint);
+ if (internalDecimalPointLength == 1)
+ {
+ internalDecimalPoint = *decimalPoint;
+ }
+ else
+ {
+ internalDecimalPoint = NIL;
+ trio_copy_max(internalDecimalPointString,
+ sizeof(internalDecimalPointString),
+ decimalPoint);
+ }
+}
+
+/*************************************************************************
+ * trio_locale_set_thousand_separator
+ *
+ * See trio_locale_set_decimal_point
+ */
+TRIO_PUBLIC void
+trio_locale_set_thousand_separator
+TRIO_ARGS1((thousandSeparator),
+ char *thousandSeparator)
+{
+#if defined(USE_LOCALE)
+ if (NULL == internalLocaleValues)
+ {
+ TrioSetLocale();
+ }
+#endif
+ trio_copy_max(internalThousandSeparator,
+ sizeof(internalThousandSeparator),
+ thousandSeparator);
+ internalThousandSeparatorLength = trio_length(internalThousandSeparator);
+}
+
+/*************************************************************************
+ * trio_locale_set_grouping
+ *
+ * Array of bytes. Reversed order.
+ *
+ * CHAR_MAX : No further grouping
+ * 0 : Repeat last group for the remaining digits (not necessary
+ * as C strings are zero-terminated)
+ * n : Set current group to n
+ *
+ * Same order as the grouping attribute in LC_NUMERIC.
+ */
+TRIO_PUBLIC void
+trio_locale_set_grouping
+TRIO_ARGS1((grouping),
+ char *grouping)
+{
+#if defined(USE_LOCALE)
+ if (NULL == internalLocaleValues)
+ {
+ TrioSetLocale();
+ }
+#endif
+ trio_copy_max(internalGrouping,
+ sizeof(internalGrouping),
+ grouping);
+}
+
+
+/*************************************************************************
+ *
+ * SCANNING
+ *
+ ************************************************************************/
+
+/*************************************************************************
+ * TrioSkipWhitespaces
+ */
+TRIO_PRIVATE int
+TrioSkipWhitespaces
+TRIO_ARGS1((self),
+ trio_class_t *self)
+{
+ int ch;
+
+ ch = self->current;
+ while (isspace(ch))
+ {
+ self->InStream(self, &ch);
+ }
+ return ch;
+}
+
+/*************************************************************************
+ * TrioGetCollation
+ */
+#if TRIO_EXTENSION
+TRIO_PRIVATE void
+TrioGetCollation(TRIO_NOARGS)
+{
+ int i;
+ int j;
+ int k;
+ char first[2];
+ char second[2];
+
+ /* This is computationally expensive */
+ first[1] = NIL;
+ second[1] = NIL;
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ {
+ k = 0;
+ first[0] = (char)i;
+ for (j = 0; j < MAX_CHARACTER_CLASS; j++)
+ {
+ second[0] = (char)j;
+ if (trio_equal_locale(first, second))
+ internalCollationArray[i][k++] = (char)j;
+ }
+ internalCollationArray[i][k] = NIL;
+ }
+}
+#endif
+
+/*************************************************************************
+ * TrioGetCharacterClass
+ *
+ * FIXME:
+ * multibyte
+ */
+TRIO_PRIVATE int
+TrioGetCharacterClass
+TRIO_ARGS4((format, indexPointer, flagsPointer, characterclass),
+ TRIO_CONST char *format,
+ int *indexPointer,
+ trio_flags_t *flagsPointer,
+ int *characterclass)
+{
+ int index = *indexPointer;
+ int i;
+ char ch;
+ char range_begin;
+ char range_end;
+
+ *flagsPointer &= ~FLAGS_EXCLUDE;
+
+ if (format[index] == QUALIFIER_CIRCUMFLEX)
+ {
+ *flagsPointer |= FLAGS_EXCLUDE;
+ index++;
+ }
+ /*
+ * If the ungroup character is at the beginning of the scanlist,
+ * it will be part of the class, and a second ungroup character
+ * must follow to end the group.
+ */
+ if (format[index] == SPECIFIER_UNGROUP)
+ {
+ characterclass[(int)SPECIFIER_UNGROUP]++;
+ index++;
+ }
+ /*
+ * Minus is used to specify ranges. To include minus in the class,
+ * it must be at the beginning of the list
+ */
+ if (format[index] == QUALIFIER_MINUS)
+ {
+ characterclass[(int)QUALIFIER_MINUS]++;
+ index++;
+ }
+ /* Collect characters */
+ for (ch = format[index];
+ (ch != SPECIFIER_UNGROUP) && (ch != NIL);
+ ch = format[++index])
+ {
+ switch (ch)
+ {
+ case QUALIFIER_MINUS: /* Scanlist ranges */
+
+ /*
+ * Both C99 and UNIX98 describes ranges as implementation-
+ * defined.
+ *
+ * We support the following behaviour (although this may
+ * change as we become wiser)
+ * - only increasing ranges, ie. [a-b] but not [b-a]
+ * - transitive ranges, ie. [a-b-c] == [a-c]
+ * - trailing minus, ie. [a-] is interpreted as an 'a'
+ * and a '-'
+ * - duplicates (although we can easily convert these
+ * into errors)
+ */
+ range_begin = format[index - 1];
+ range_end = format[++index];
+ if (range_end == SPECIFIER_UNGROUP)
+ {
+ /* Trailing minus is included */
+ characterclass[(int)ch]++;
+ ch = range_end;
+ break; /* for */
+ }
+ if (range_end == NIL)
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ if (range_begin > range_end)
+ return TRIO_ERROR_RETURN(TRIO_ERANGE, index);
+
+ for (i = (int)range_begin; i <= (int)range_end; i++)
+ characterclass[i]++;
+
+ ch = range_end;
+ break;
+
+#if TRIO_EXTENSION
+
+ case SPECIFIER_GROUP:
+
+ switch (format[index + 1])
+ {
+ case QUALIFIER_DOT: /* Collating symbol */
+ /*
+ * FIXME: This will be easier to implement when multibyte
+ * characters have been implemented. Until now, we ignore
+ * this feature.
+ */
+ for (i = index + 2; ; i++)
+ {
+ if (format[i] == NIL)
+ /* Error in syntax */
+ return -1;
+ else if (format[i] == QUALIFIER_DOT)
+ break; /* for */
+ }
+ if (format[++i] != SPECIFIER_UNGROUP)
+ return -1;
+
+ index = i;
+ break;
+
+ case QUALIFIER_EQUAL: /* Equivalence class expressions */
+ {
+ unsigned int j;
+ unsigned int k;
+
+ if (internalCollationUnconverted)
+ {
+ /* Lazy evaluation of collation array */
+ TrioGetCollation();
+ internalCollationUnconverted = FALSE;
+ }
+ for (i = index + 2; ; i++)
+ {
+ if (format[i] == NIL)
+ /* Error in syntax */
+ return -1;
+ else if (format[i] == QUALIFIER_EQUAL)
+ break; /* for */
+ else
+ {
+ /* Mark any equivalent character */
+ k = (unsigned int)format[i];
+ for (j = 0; internalCollationArray[k][j] != NIL; j++)
+ characterclass[(int)internalCollationArray[k][j]]++;
+ }
+ }
+ if (format[++i] != SPECIFIER_UNGROUP)
+ return -1;
+
+ index = i;
+ }
+ break;
+
+ case QUALIFIER_COLON: /* Character class expressions */
+
+ if (trio_equal_max(CLASS_ALNUM, sizeof(CLASS_ALNUM) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (isalnum(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_ALNUM) - 1;
+ }
+ else if (trio_equal_max(CLASS_ALPHA, sizeof(CLASS_ALPHA) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (isalpha(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_ALPHA) - 1;
+ }
+ else if (trio_equal_max(CLASS_CNTRL, sizeof(CLASS_CNTRL) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (iscntrl(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_CNTRL) - 1;
+ }
+ else if (trio_equal_max(CLASS_DIGIT, sizeof(CLASS_DIGIT) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (isdigit(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_DIGIT) - 1;
+ }
+ else if (trio_equal_max(CLASS_GRAPH, sizeof(CLASS_GRAPH) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (isgraph(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_GRAPH) - 1;
+ }
+ else if (trio_equal_max(CLASS_LOWER, sizeof(CLASS_LOWER) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (islower(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_LOWER) - 1;
+ }
+ else if (trio_equal_max(CLASS_PRINT, sizeof(CLASS_PRINT) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (isprint(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_PRINT) - 1;
+ }
+ else if (trio_equal_max(CLASS_PUNCT, sizeof(CLASS_PUNCT) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (ispunct(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_PUNCT) - 1;
+ }
+ else if (trio_equal_max(CLASS_SPACE, sizeof(CLASS_SPACE) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (isspace(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_SPACE) - 1;
+ }
+ else if (trio_equal_max(CLASS_UPPER, sizeof(CLASS_UPPER) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (isupper(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_UPPER) - 1;
+ }
+ else if (trio_equal_max(CLASS_XDIGIT, sizeof(CLASS_XDIGIT) - 1,
+ &format[index]))
+ {
+ for (i = 0; i < MAX_CHARACTER_CLASS; i++)
+ if (isxdigit(i))
+ characterclass[i]++;
+ index += sizeof(CLASS_XDIGIT) - 1;
+ }
+ else
+ {
+ characterclass[(int)ch]++;
+ }
+ break;
+
+ default:
+ characterclass[(int)ch]++;
+ break;
+ }
+ break;
+
+#endif /* TRIO_EXTENSION */
+
+ default:
+ characterclass[(int)ch]++;
+ break;
+ }
+ }
+ return 0;
+}
+
+/*************************************************************************
+ * TrioReadNumber
+ *
+ * We implement our own number conversion in preference of strtol and
+ * strtoul, because we must handle 'long long' and thousand separators.
+ */
+TRIO_PRIVATE BOOLEAN_T
+TrioReadNumber
+TRIO_ARGS5((self, target, flags, width, base),
+ trio_class_t *self,
+ trio_uintmax_t *target,
+ trio_flags_t flags,
+ int width,
+ int base)
+{
+ trio_uintmax_t number = 0;
+ int digit;
+ int count;
+ BOOLEAN_T isNegative = FALSE;
+ BOOLEAN_T gotNumber = FALSE;
+ int j;
+
+ assert(VALID(self));
+ assert(VALID(self->InStream));
+ assert((base >= MIN_BASE && base <= MAX_BASE) || (base == NO_BASE));
+
+ if (internalDigitsUnconverted)
+ {
+ /* Lazy evaluation of digits array */
+ memset(internalDigitArray, -1, sizeof(internalDigitArray));
+ for (j = 0; j < (int)sizeof(internalDigitsLower) - 1; j++)
+ {
+ internalDigitArray[(int)internalDigitsLower[j]] = j;
+ internalDigitArray[(int)internalDigitsUpper[j]] = j;
+ }
+ internalDigitsUnconverted = FALSE;
+ }
+
+ TrioSkipWhitespaces(self);
+
+ if (!(flags & FLAGS_UNSIGNED))
+ {
+ /* Leading sign */
+ if (self->current == '+')
+ {
+ self->InStream(self, NULL);
+ }
+ else if (self->current == '-')
+ {
+ self->InStream(self, NULL);
+ isNegative = TRUE;
+ }
+ }
+
+ count = self->processed;
+
+ if (flags & FLAGS_ALTERNATIVE)
+ {
+ switch (base)
+ {
+ case NO_BASE:
+ case BASE_OCTAL:
+ case BASE_HEX:
+ case BASE_BINARY:
+ if (self->current == '0')
+ {
+ self->InStream(self, NULL);
+ if (self->current)
+ {
+ if ((base == BASE_HEX) &&
+ (trio_to_upper(self->current) == 'X'))
+ {
+ self->InStream(self, NULL);
+ }
+ else if ((base == BASE_BINARY) &&
+ (trio_to_upper(self->current) == 'B'))
+ {
+ self->InStream(self, NULL);
+ }
+ }
+ }
+ else
+ return FALSE;
+ break;
+ default:
+ break;
+ }
+ }
+
+ while (((width == NO_WIDTH) || (self->processed - count < width)) &&
+ (! ((self->current == EOF) || isspace(self->current))))
+ {
+ if (isascii(self->current))
+ {
+ digit = internalDigitArray[self->current];
+ /* Abort if digit is not allowed in the specified base */
+ if ((digit == -1) || (digit >= base))
+ break;
+ }
+ else if (flags & FLAGS_QUOTE)
+ {
+ /* Compare with thousands separator */
+ for (j = 0; internalThousandSeparator[j] && self->current; j++)
+ {
+ if (internalThousandSeparator[j] != self->current)
+ break;
+
+ self->InStream(self, NULL);
+ }
+ if (internalThousandSeparator[j])
+ break; /* Mismatch */
+ else
+ continue; /* Match */
+ }
+ else
+ break;
+
+ number *= base;
+ number += digit;
+ gotNumber = TRUE; /* we need at least one digit */
+
+ self->InStream(self, NULL);
+ }
+
+ /* Was anything read at all? */
+ if (!gotNumber)
+ return FALSE;
+
+ if (target)
+ *target = (isNegative) ? -((trio_intmax_t)number) : number;
+ return TRUE;
+}
+
+/*************************************************************************
+ * TrioReadChar
+ */
+TRIO_PRIVATE int
+TrioReadChar
+TRIO_ARGS4((self, target, flags, width),
+ trio_class_t *self,
+ char *target,
+ trio_flags_t flags,
+ int width)
+{
+ int i;
+ char ch;
+ trio_uintmax_t number;
+
+ assert(VALID(self));
+ assert(VALID(self->InStream));
+
+ for (i = 0;
+ (self->current != EOF) && (i < width);
+ i++)
+ {
+ ch = (char)self->current;
+ self->InStream(self, NULL);
+ if ((flags & FLAGS_ALTERNATIVE) && (ch == CHAR_BACKSLASH))
+ {
+ switch (self->current)
+ {
+ case '\\': ch = '\\'; break;
+ case 'a': ch = '\007'; break;
+ case 'b': ch = '\b'; break;
+ case 'f': ch = '\f'; break;
+ case 'n': ch = '\n'; break;
+ case 'r': ch = '\r'; break;
+ case 't': ch = '\t'; break;
+ case 'v': ch = '\v'; break;
+ default:
+ if (isdigit(self->current))
+ {
+ /* Read octal number */
+ if (!TrioReadNumber(self, &number, 0, 3, BASE_OCTAL))
+ return 0;
+ ch = (char)number;
+ }
+ else if (trio_to_upper(self->current) == 'X')
+ {
+ /* Read hexadecimal number */
+ self->InStream(self, NULL);
+ if (!TrioReadNumber(self, &number, 0, 2, BASE_HEX))
+ return 0;
+ ch = (char)number;
+ }
+ else
+ {
+ ch = (char)self->current;
+ }
+ break;
+ }
+ }
+
+ if (target)
+ target[i] = ch;
+ }
+ return i + 1;
+}
+
+/*************************************************************************
+ * TrioReadString
+ */
+TRIO_PRIVATE BOOLEAN_T
+TrioReadString
+TRIO_ARGS4((self, target, flags, width),
+ trio_class_t *self,
+ char *target,
+ trio_flags_t flags,
+ int width)
+{
+ int i;
+
+ assert(VALID(self));
+ assert(VALID(self->InStream));
+
+ TrioSkipWhitespaces(self);
+
+ /*
+ * Continue until end of string is reached, a whitespace is encountered,
+ * or width is exceeded
+ */
+ for (i = 0;
+ ((width == NO_WIDTH) || (i < width)) &&
+ (! ((self->current == EOF) || isspace(self->current)));
+ i++)
+ {
+ if (TrioReadChar(self, (target ? &target[i] : 0), flags, 1) == 0)
+ break; /* for */
+ }
+ if (target)
+ target[i] = NIL;
+ return TRUE;
+}
+
+/*************************************************************************
+ * TrioReadWideChar
+ */
+#if TRIO_WIDECHAR
+TRIO_PRIVATE int
+TrioReadWideChar
+TRIO_ARGS4((self, target, flags, width),
+ trio_class_t *self,
+ trio_wchar_t *target,
+ trio_flags_t flags,
+ int width)
+{
+ int i;
+ int j;
+ int size;
+ int amount = 0;
+ trio_wchar_t wch;
+ char buffer[MB_LEN_MAX + 1];
+
+ assert(VALID(self));
+ assert(VALID(self->InStream));
+
+ for (i = 0;
+ (self->current != EOF) && (i < width);
+ i++)
+ {
+ if (isascii(self->current))
+ {
+ if (TrioReadChar(self, buffer, flags, 1) == 0)
+ return 0;
+ buffer[1] = NIL;
+ }
+ else
+ {
+ /*
+ * Collect a multibyte character, by enlarging buffer until
+ * it contains a fully legal multibyte character, or the
+ * buffer is full.
+ */
+ j = 0;
+ do
+ {
+ buffer[j++] = (char)self->current;
+ buffer[j] = NIL;
+ self->InStream(self, NULL);
+ }
+ while ((j < (int)sizeof(buffer)) && (mblen(buffer, (size_t)j) != j));
+ }
+ if (target)
+ {
+ size = mbtowc(&wch, buffer, sizeof(buffer));
+ if (size > 0)
+ target[i] = wch;
+ }
+ amount += size;
+ self->InStream(self, NULL);
+ }
+ return amount;
+}
+#endif /* TRIO_WIDECHAR */
+
+/*************************************************************************
+ * TrioReadWideString
+ */
+#if TRIO_WIDECHAR
+TRIO_PRIVATE BOOLEAN_T
+TrioReadWideString
+TRIO_ARGS4((self, target, flags, width),
+ trio_class_t *self,
+ trio_wchar_t *target,
+ trio_flags_t flags,
+ int width)
+{
+ int i;
+ int size;
+
+ assert(VALID(self));
+ assert(VALID(self->InStream));
+
+ TrioSkipWhitespaces(self);
+
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ (void)mblen(NULL, 0);
+#endif
+
+ /*
+ * Continue until end of string is reached, a whitespace is encountered,
+ * or width is exceeded
+ */
+ for (i = 0;
+ ((width == NO_WIDTH) || (i < width)) &&
+ (! ((self->current == EOF) || isspace(self->current)));
+ )
+ {
+ size = TrioReadWideChar(self, &target[i], flags, 1);
+ if (size == 0)
+ break; /* for */
+
+ i += size;
+ }
+ if (target)
+ target[i] = WCONST('\0');
+ return TRUE;
+}
+#endif /* TRIO_WIDECHAR */
+
+/*************************************************************************
+ * TrioReadGroup
+ *
+ * FIXME: characterclass does not work with multibyte characters
+ */
+TRIO_PRIVATE BOOLEAN_T
+TrioReadGroup
+TRIO_ARGS5((self, target, characterclass, flags, width),
+ trio_class_t *self,
+ char *target,
+ int *characterclass,
+ trio_flags_t flags,
+ int width)
+{
+ int ch;
+ int i;
+
+ assert(VALID(self));
+ assert(VALID(self->InStream));
+
+ ch = self->current;
+ for (i = 0;
+ ((width == NO_WIDTH) || (i < width)) &&
+ (! ((ch == EOF) ||
+ (((flags & FLAGS_EXCLUDE) != 0) ^ (characterclass[ch] == 0))));
+ i++)
+ {
+ if (target)
+ target[i] = (char)ch;
+ self->InStream(self, &ch);
+ }
+
+ if (target)
+ target[i] = NIL;
+ return TRUE;
+}
+
+/*************************************************************************
+ * TrioReadDouble
+ *
+ * FIXME:
+ * add long double
+ * handle base
+ */
+TRIO_PRIVATE BOOLEAN_T
+TrioReadDouble
+TRIO_ARGS4((self, target, flags, width),
+ trio_class_t *self,
+ trio_pointer_t target,
+ trio_flags_t flags,
+ int width)
+{
+ int ch;
+ char doubleString[512];
+ int index = 0;
+ int start;
+ int j;
+ BOOLEAN_T isHex = FALSE;
+
+ doubleString[0] = 0;
+
+ if ((width == NO_WIDTH) || (width > (int)sizeof(doubleString) - 1))
+ width = sizeof(doubleString) - 1;
+
+ TrioSkipWhitespaces(self);
+
+ /*
+ * Read entire double number from stream. trio_to_double requires
+ * a string as input, but InStream can be anything, so we have to
+ * collect all characters.
+ */
+ ch = self->current;
+ if ((ch == '+') || (ch == '-'))
+ {
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ width--;
+ }
+
+ start = index;
+ switch (ch)
+ {
+ case 'n':
+ case 'N':
+ /* Not-a-number */
+ if (index != 0)
+ break;
+ /* FALLTHROUGH */
+ case 'i':
+ case 'I':
+ /* Infinity */
+ while (isalpha(ch) && (index - start < width))
+ {
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ }
+ doubleString[index] = NIL;
+
+ /* Case insensitive string comparison */
+ if (trio_equal(&doubleString[start], INFINITE_UPPER) ||
+ trio_equal(&doubleString[start], LONG_INFINITE_UPPER))
+ {
+ if (flags & FLAGS_LONGDOUBLE)
+ {
+ if ((start == 1) && (doubleString[0] == '-'))
+ {
+ *((trio_long_double_t *)target) = trio_ninf();
+ }
+ else
+ {
+ *((trio_long_double_t *)target) = trio_pinf();
+ }
+ }
+ else
+ {
+ if ((start == 1) && (doubleString[0] == '-'))
+ {
+ *((double *)target) = trio_ninf();
+ }
+ else
+ {
+ *((double *)target) = trio_pinf();
+ }
+ }
+ return TRUE;
+ }
+ if (trio_equal(doubleString, NAN_UPPER))
+ {
+ /* NaN must not have a preceeding + nor - */
+ if (flags & FLAGS_LONGDOUBLE)
+ {
+ *((trio_long_double_t *)target) = trio_nan();
+ }
+ else
+ {
+ *((double *)target) = trio_nan();
+ }
+ return TRUE;
+ }
+ return FALSE;
+
+ case '0':
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ if (trio_to_upper(ch) == 'X')
+ {
+ isHex = TRUE;
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ while ((ch != EOF) && (index - start < width))
+ {
+ /* Integer part */
+ if (isHex ? isxdigit(ch) : isdigit(ch))
+ {
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ }
+ else if (flags & FLAGS_QUOTE)
+ {
+ /* Compare with thousands separator */
+ for (j = 0; internalThousandSeparator[j] && self->current; j++)
+ {
+ if (internalThousandSeparator[j] != self->current)
+ break;
+
+ self->InStream(self, &ch);
+ }
+ if (internalThousandSeparator[j])
+ break; /* Mismatch */
+ else
+ continue; /* Match */
+ }
+ else
+ break; /* while */
+ }
+ if (ch == '.')
+ {
+ /* Decimal part */
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ while ((isHex ? isxdigit(ch) : isdigit(ch)) &&
+ (index - start < width))
+ {
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ }
+ if (isHex ? (trio_to_upper(ch) == 'P') : (trio_to_upper(ch) == 'E'))
+ {
+ /* Exponent */
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ if ((ch == '+') || (ch == '-'))
+ {
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ }
+ while (isdigit(ch) && (index - start < width))
+ {
+ doubleString[index++] = (char)ch;
+ self->InStream(self, &ch);
+ }
+ }
+ }
+
+ if ((index == start) || (*doubleString == NIL))
+ return FALSE;
+
+ doubleString[index] = 0;
+
+ if (flags & FLAGS_LONGDOUBLE)
+ {
+ *((trio_long_double_t *)target) = trio_to_long_double(doubleString, NULL);
+ }
+ else
+ {
+ *((double *)target) = trio_to_double(doubleString, NULL);
+ }
+ return TRUE;
+}
+
+/*************************************************************************
+ * TrioReadPointer
+ */
+TRIO_PRIVATE BOOLEAN_T
+TrioReadPointer
+TRIO_ARGS3((self, target, flags),
+ trio_class_t *self,
+ trio_pointer_t *target,
+ trio_flags_t flags)
+{
+ trio_uintmax_t number;
+ char buffer[sizeof(internalNullString)];
+
+ flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE | FLAGS_NILPADDING);
+
+ if (TrioReadNumber(self,
+ &number,
+ flags,
+ POINTER_WIDTH,
+ BASE_HEX))
+ {
+ /*
+ * The strange assignment of number is a workaround for a compiler
+ * warning
+ */
+ if (target)
+ *target = (char *)0 + number;
+ return TRUE;
+ }
+ else if (TrioReadString(self,
+ (flags & FLAGS_IGNORE)
+ ? NULL
+ : buffer,
+ 0,
+ sizeof(internalNullString) - 1))
+ {
+ if (trio_equal_case(buffer, internalNullString))
+ {
+ if (target)
+ *target = NULL;
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+/*************************************************************************
+ * TrioScanProcess
+ */
+TRIO_PRIVATE int
+TrioScanProcess
+TRIO_ARGS3((data, format, parameters),
+ trio_class_t *data,
+ TRIO_CONST char *format,
+ trio_parameter_t *parameters)
+{
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ int charlen;
+ int cnt;
+#endif
+ int assignment;
+ int ch;
+ int index; /* Index of format string */
+ int i; /* Index of current parameter */
+ trio_flags_t flags;
+ int width;
+ int base;
+ trio_pointer_t pointer;
+
+ assignment = 0;
+ i = 0;
+ index = 0;
+ data->InStream(data, &ch);
+
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ (void)mblen(NULL, 0);
+#endif
+
+ while (format[index])
+ {
+#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
+ if (! isascii(format[index]))
+ {
+ charlen = mblen(&format[index], MB_LEN_MAX);
+ if (charlen != -1)
+ {
+ /* Compare multibyte characters in format string */
+ for (cnt = 0; cnt < charlen - 1; cnt++)
+ {
+ if (ch != format[index + cnt])
+ {
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ }
+ data->InStream(data, &ch);
+ }
+ continue; /* while characters left in formatting string */
+ }
+ }
+#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
+
+ if ((EOF == ch) && (parameters[i].type != FORMAT_COUNT))
+ {
+ return (assignment > 0) ? assignment : EOF;
+ }
+
+ if (CHAR_IDENTIFIER == format[index])
+ {
+ if (CHAR_IDENTIFIER == format[index + 1])
+ {
+ /* Two % in format matches one % in input stream */
+ if (CHAR_IDENTIFIER == ch)
+ {
+ data->InStream(data, &ch);
+ index += 2;
+ continue; /* while format chars left */
+ }
+ else
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ }
+
+ /* Skip the parameter entries */
+ while (parameters[i].type == FORMAT_PARAMETER)
+ i++;
+
+ flags = parameters[i].flags;
+ /* Find width */
+ width = parameters[i].width;
+ if (flags & FLAGS_WIDTH_PARAMETER)
+ {
+ /* Get width from parameter list */
+ width = (int)parameters[width].data.number.as_signed;
+ }
+ /* Find base */
+ base = parameters[i].base;
+ if (flags & FLAGS_BASE_PARAMETER)
+ {
+ /* Get base from parameter list */
+ base = (int)parameters[base].data.number.as_signed;
+ }
+
+ switch (parameters[i].type)
+ {
+ case FORMAT_INT:
+ {
+ trio_uintmax_t number;
+
+ if (0 == base)
+ base = BASE_DECIMAL;
+
+ if (!TrioReadNumber(data,
+ &number,
+ flags,
+ width,
+ base))
+ return assignment;
+
+ if (!(flags & FLAGS_IGNORE))
+ {
+ assignment++;
+
+ pointer = parameters[i].data.pointer;
+#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
+ if (flags & FLAGS_SIZE_T)
+ *(size_t *)pointer = (size_t)number;
+ else
+#endif
+#if defined(QUALIFIER_PTRDIFF_T)
+ if (flags & FLAGS_PTRDIFF_T)
+ *(ptrdiff_t *)pointer = (ptrdiff_t)number;
+ else
+#endif
+#if defined(QUALIFIER_INTMAX_T)
+ if (flags & FLAGS_INTMAX_T)
+ *(trio_intmax_t *)pointer = (trio_intmax_t)number;
+ else
+#endif
+ if (flags & FLAGS_QUAD)
+ *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)number;
+ else if (flags & FLAGS_LONG)
+ *(long int *)pointer = (long int)number;
+ else if (flags & FLAGS_SHORT)
+ *(short int *)pointer = (short int)number;
+ else
+ *(int *)pointer = (int)number;
+ }
+ }
+ break; /* FORMAT_INT */
+
+ case FORMAT_STRING:
+#if TRIO_WIDECHAR
+ if (flags & FLAGS_WIDECHAR)
+ {
+ if (!TrioReadWideString(data,
+ (flags & FLAGS_IGNORE)
+ ? NULL
+ : parameters[i].data.wstring,
+ flags,
+ width))
+ return assignment;
+ }
+ else
+#endif
+ {
+ if (!TrioReadString(data,
+ (flags & FLAGS_IGNORE)
+ ? NULL
+ : parameters[i].data.string,
+ flags,
+ width))
+ return assignment;
+ }
+ if (!(flags & FLAGS_IGNORE))
+ assignment++;
+ break; /* FORMAT_STRING */
+
+ case FORMAT_DOUBLE:
+ {
+ trio_pointer_t pointer;
+
+ if (flags & FLAGS_IGNORE)
+ {
+ pointer = NULL;
+ }
+ else
+ {
+ pointer = (flags & FLAGS_LONGDOUBLE)
+ ? (trio_pointer_t)parameters[i].data.longdoublePointer
+ : (trio_pointer_t)parameters[i].data.doublePointer;
+ }
+ if (!TrioReadDouble(data, pointer, flags, width))
+ {
+ return assignment;
+ }
+ if (!(flags & FLAGS_IGNORE))
+ {
+ assignment++;
+ }
+ break; /* FORMAT_DOUBLE */
+ }
+ case FORMAT_GROUP:
+ {
+ int characterclass[MAX_CHARACTER_CLASS + 1];
+ int rc;
+
+ /* Skip over modifiers */
+ while (format[index] != SPECIFIER_GROUP)
+ {
+ index++;
+ }
+ /* Skip over group specifier */
+ index++;
+
+ memset(characterclass, 0, sizeof(characterclass));
+ rc = TrioGetCharacterClass(format,
+ &index,
+ &flags,
+ characterclass);
+ if (rc < 0)
+ return rc;
+
+ if (!TrioReadGroup(data,
+ (flags & FLAGS_IGNORE)
+ ? NULL
+ : parameters[i].data.string,
+ characterclass,
+ flags,
+ parameters[i].width))
+ return assignment;
+ if (!(flags & FLAGS_IGNORE))
+ assignment++;
+ }
+ break; /* FORMAT_GROUP */
+
+ case FORMAT_COUNT:
+ pointer = parameters[i].data.pointer;
+ if (NULL != pointer)
+ {
+ int count = data->committed;
+ if (ch != EOF)
+ count--; /* a character is read, but is not consumed yet */
+#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
+ if (flags & FLAGS_SIZE_T)
+ *(size_t *)pointer = (size_t)count;
+ else
+#endif
+#if defined(QUALIFIER_PTRDIFF_T)
+ if (flags & FLAGS_PTRDIFF_T)
+ *(ptrdiff_t *)pointer = (ptrdiff_t)count;
+ else
+#endif
+#if defined(QUALIFIER_INTMAX_T)
+ if (flags & FLAGS_INTMAX_T)
+ *(trio_intmax_t *)pointer = (trio_intmax_t)count;
+ else
+#endif
+ if (flags & FLAGS_QUAD)
+ {
+ *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)count;
+ }
+ else if (flags & FLAGS_LONG)
+ {
+ *(long int *)pointer = (long int)count;
+ }
+ else if (flags & FLAGS_SHORT)
+ {
+ *(short int *)pointer = (short int)count;
+ }
+ else
+ {
+ *(int *)pointer = (int)count;
+ }
+ }
+ break; /* FORMAT_COUNT */
+
+ case FORMAT_CHAR:
+#if TRIO_WIDECHAR
+ if (flags & FLAGS_WIDECHAR)
+ {
+ if (TrioReadWideChar(data,
+ (flags & FLAGS_IGNORE)
+ ? NULL
+ : parameters[i].data.wstring,
+ flags,
+ (width == NO_WIDTH) ? 1 : width) == 0)
+ return assignment;
+ }
+ else
+#endif
+ {
+ if (TrioReadChar(data,
+ (flags & FLAGS_IGNORE)
+ ? NULL
+ : parameters[i].data.string,
+ flags,
+ (width == NO_WIDTH) ? 1 : width) == 0)
+ return assignment;
+ }
+ if (!(flags & FLAGS_IGNORE))
+ assignment++;
+ break; /* FORMAT_CHAR */
+
+ case FORMAT_POINTER:
+ if (!TrioReadPointer(data,
+ (flags & FLAGS_IGNORE)
+ ? NULL
+ : (trio_pointer_t *)parameters[i].data.pointer,
+ flags))
+ return assignment;
+ if (!(flags & FLAGS_IGNORE))
+ assignment++;
+ break; /* FORMAT_POINTER */
+
+ case FORMAT_PARAMETER:
+ break; /* FORMAT_PARAMETER */
+
+ default:
+ return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
+ }
+ ch = data->current;
+ index = parameters[i].indexAfterSpecifier;
+ i++;
+ }
+ else /* Not an % identifier */
+ {
+ if (isspace((int)format[index]))
+ {
+ /* Whitespaces may match any amount of whitespaces */
+ ch = TrioSkipWhitespaces(data);
+ }
+ else if (ch == format[index])
+ {
+ data->InStream(data, &ch);
+ }
+ else
+ return assignment;
+
+ index++;
+ }
+ }
+ return assignment;
+}
+
+/*************************************************************************
+ * TrioScan
+ */
+TRIO_PRIVATE int
+TrioScan
+TRIO_ARGS6((source, sourceSize, InStream, format, arglist, argarray),
+ trio_pointer_t source,
+ size_t sourceSize,
+ void (*InStream) TRIO_PROTO((trio_class_t *, int *)),
+ TRIO_CONST char *format,
+ va_list *arglist,
+ trio_pointer_t *argarray)
+{
+ int status;
+ trio_parameter_t parameters[MAX_PARAMETERS];
+ trio_class_t data;
+
+ assert(VALID(InStream));
+ assert(VALID(format));
+
+ memset(&data, 0, sizeof(data));
+ data.InStream = InStream;
+ data.location = (trio_pointer_t)source;
+ data.max = sourceSize;
+ data.error = 0;
+
+#if defined(USE_LOCALE)
+ if (NULL == internalLocaleValues)
+ {
+ TrioSetLocale();
+ }
+#endif
+
+ status = TrioParse(TYPE_SCAN, format, parameters, arglist, argarray);
+ if (status < 0)
+ return status;
+
+ status = TrioScanProcess(&data, format, parameters);
+ if (data.error != 0)
+ {
+ status = data.error;
+ }
+ return status;
+}
+
+/*************************************************************************
+ * TrioInStreamFile
+ */
+TRIO_PRIVATE void
+TrioInStreamFile
+TRIO_ARGS2((self, intPointer),
+ trio_class_t *self,
+ int *intPointer)
+{
+ FILE *file = (FILE *)self->location;
+
+ assert(VALID(self));
+ assert(VALID(file));
+
+ self->current = fgetc(file);
+ if (self->current == EOF)
+ {
+ self->error = (ferror(file))
+ ? TRIO_ERROR_RETURN(TRIO_ERRNO, 0)
+ : TRIO_ERROR_RETURN(TRIO_EOF, 0);
+ }
+ else
+ {
+ self->processed++;
+ self->committed++;
+ }
+
+ if (VALID(intPointer))
+ {
+ *intPointer = self->current;
+ }
+}
+
+/*************************************************************************
+ * TrioInStreamFileDescriptor
+ */
+TRIO_PRIVATE void
+TrioInStreamFileDescriptor
+TRIO_ARGS2((self, intPointer),
+ trio_class_t *self,
+ int *intPointer)
+{
+ int fd = *((int *)self->location);
+ int size;
+ unsigned char input;
+
+ assert(VALID(self));
+
+ size = read(fd, &input, sizeof(char));
+ if (size == -1)
+ {
+ self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
+ self->current = EOF;
+ }
+ else
+ {
+ self->current = (size == 0) ? EOF : input;
+ }
+ if (self->current != EOF)
+ {
+ self->committed++;
+ self->processed++;
+ }
+
+ if (VALID(intPointer))
+ {
+ *intPointer = self->current;
+ }
+}
+
+/*************************************************************************
+ * TrioInStreamCustom
+ */
+TRIO_PRIVATE void
+TrioInStreamCustom
+TRIO_ARGS2((self, intPointer),
+ trio_class_t *self,
+ int *intPointer)
+{
+ trio_custom_t *data;
+
+ assert(VALID(self));
+ assert(VALID(self->location));
+
+ data = (trio_custom_t *)self->location;
+
+ self->current = (data->stream.in == NULL)
+ ? NIL
+ : (data->stream.in)(data->closure);
+
+ if (self->current == NIL)
+ {
+ self->current = EOF;
+ }
+ else
+ {
+ self->processed++;
+ self->committed++;
+ }
+
+ if (VALID(intPointer))
+ {
+ *intPointer = self->current;
+ }
+}
+
+/*************************************************************************
+ * TrioInStreamString
+ */
+TRIO_PRIVATE void
+TrioInStreamString
+TRIO_ARGS2((self, intPointer),
+ trio_class_t *self,
+ int *intPointer)
+{
+ unsigned char **buffer;
+
+ assert(VALID(self));
+ assert(VALID(self->location));
+
+ buffer = (unsigned char **)self->location;
+ self->current = (*buffer)[0];
+ if (self->current == NIL)
+ {
+ self->current = EOF;
+ }
+ else
+ {
+ (*buffer)++;
+ self->processed++;
+ self->committed++;
+ }
+
+ if (VALID(intPointer))
+ {
+ *intPointer = self->current;
+ }
+}
+
+/*************************************************************************
+ *
+ * Formatted scanning functions
+ *
+ ************************************************************************/
+
+#if defined(TRIO_DOCUMENTATION)
+# include "doc/doc_scanf.h"
+#endif
+/** @addtogroup Scanf
+ @{
+*/
+
+/*************************************************************************
+ * scanf
+ */
+
+/**
+ Scan characters from standard input stream.
+
+ @param format Formatting string.
+ @param ... Arguments.
+ @return Number of scanned characters.
+ */
+TRIO_PUBLIC int
+trio_scanf
+TRIO_VARGS2((format, va_alist),
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioScan((trio_pointer_t)stdin, 0,
+ TrioInStreamFile,
+ format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+TRIO_PUBLIC int
+trio_vscanf
+TRIO_ARGS2((format, args),
+ TRIO_CONST char *format,
+ va_list args)
+{
+ assert(VALID(format));
+
+ return TrioScan((trio_pointer_t)stdin, 0,
+ TrioInStreamFile,
+ format, &args, NULL);
+}
+
+TRIO_PUBLIC int
+trio_scanfv
+TRIO_ARGS2((format, args),
+ TRIO_CONST char *format,
+ trio_pointer_t *args)
+{
+ assert(VALID(format));
+
+ return TrioScan((trio_pointer_t)stdin, 0,
+ TrioInStreamFile,
+ format, NULL, args);
+}
+
+/*************************************************************************
+ * fscanf
+ */
+TRIO_PUBLIC int
+trio_fscanf
+TRIO_VARGS3((file, format, va_alist),
+ FILE *file,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(file));
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioScan((trio_pointer_t)file, 0,
+ TrioInStreamFile,
+ format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+TRIO_PUBLIC int
+trio_vfscanf
+TRIO_ARGS3((file, format, args),
+ FILE *file,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ assert(VALID(file));
+ assert(VALID(format));
+
+ return TrioScan((trio_pointer_t)file, 0,
+ TrioInStreamFile,
+ format, &args, NULL);
+}
+
+TRIO_PUBLIC int
+trio_fscanfv
+TRIO_ARGS3((file, format, args),
+ FILE *file,
+ TRIO_CONST char *format,
+ trio_pointer_t *args)
+{
+ assert(VALID(file));
+ assert(VALID(format));
+
+ return TrioScan((trio_pointer_t)file, 0,
+ TrioInStreamFile,
+ format, NULL, args);
+}
+
+/*************************************************************************
+ * dscanf
+ */
+TRIO_PUBLIC int
+trio_dscanf
+TRIO_VARGS3((fd, format, va_alist),
+ int fd,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioScan((trio_pointer_t)&fd, 0,
+ TrioInStreamFileDescriptor,
+ format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+TRIO_PUBLIC int
+trio_vdscanf
+TRIO_ARGS3((fd, format, args),
+ int fd,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ assert(VALID(format));
+
+ return TrioScan((trio_pointer_t)&fd, 0,
+ TrioInStreamFileDescriptor,
+ format, &args, NULL);
+}
+
+TRIO_PUBLIC int
+trio_dscanfv
+TRIO_ARGS3((fd, format, args),
+ int fd,
+ TRIO_CONST char *format,
+ trio_pointer_t *args)
+{
+ assert(VALID(format));
+
+ return TrioScan((trio_pointer_t)&fd, 0,
+ TrioInStreamFileDescriptor,
+ format, NULL, args);
+}
+
+/*************************************************************************
+ * cscanf
+ */
+TRIO_PUBLIC int
+trio_cscanf
+TRIO_VARGS4((stream, closure, format, va_alist),
+ trio_instream_t stream,
+ trio_pointer_t closure,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+ trio_custom_t data;
+
+ assert(VALID(stream));
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ data.stream.in = stream;
+ data.closure = closure;
+ status = TrioScan(&data, 0, TrioInStreamCustom, format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+TRIO_PUBLIC int
+trio_vcscanf
+TRIO_ARGS4((stream, closure, format, args),
+ trio_instream_t stream,
+ trio_pointer_t closure,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ trio_custom_t data;
+
+ assert(VALID(stream));
+ assert(VALID(format));
+
+ data.stream.in = stream;
+ data.closure = closure;
+ return TrioScan(&data, 0, TrioInStreamCustom, format, &args, NULL);
+}
+
+TRIO_PUBLIC int
+trio_cscanfv
+TRIO_ARGS4((stream, closure, format, args),
+ trio_instream_t stream,
+ trio_pointer_t closure,
+ TRIO_CONST char *format,
+ trio_pointer_t *args)
+{
+ trio_custom_t data;
+
+ assert(VALID(stream));
+ assert(VALID(format));
+
+ data.stream.in = stream;
+ data.closure = closure;
+ return TrioScan(&data, 0, TrioInStreamCustom, format, NULL, args);
+}
+
+/*************************************************************************
+ * sscanf
+ */
+TRIO_PUBLIC int
+trio_sscanf
+TRIO_VARGS3((buffer, format, va_alist),
+ TRIO_CONST char *buffer,
+ TRIO_CONST char *format,
+ TRIO_VA_DECL)
+{
+ int status;
+ va_list args;
+
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ TRIO_VA_START(args, format);
+ status = TrioScan((trio_pointer_t)&buffer, 0,
+ TrioInStreamString,
+ format, &args, NULL);
+ TRIO_VA_END(args);
+ return status;
+}
+
+TRIO_PUBLIC int
+trio_vsscanf
+TRIO_ARGS3((buffer, format, args),
+ TRIO_CONST char *buffer,
+ TRIO_CONST char *format,
+ va_list args)
+{
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ return TrioScan((trio_pointer_t)&buffer, 0,
+ TrioInStreamString,
+ format, &args, NULL);
+}
+
+TRIO_PUBLIC int
+trio_sscanfv
+TRIO_ARGS3((buffer, format, args),
+ TRIO_CONST char *buffer,
+ TRIO_CONST char *format,
+ trio_pointer_t *args)
+{
+ assert(VALID(buffer));
+ assert(VALID(format));
+
+ return TrioScan((trio_pointer_t)&buffer, 0,
+ TrioInStreamString,
+ format, NULL, args);
+}
+
+/** @} End of Scanf documentation module */
+
+/*************************************************************************
+ * trio_strerror
+ */
+TRIO_PUBLIC TRIO_CONST char *
+trio_strerror
+TRIO_ARGS1((errorcode),
+ int errorcode)
+{
+ /* Textual versions of the error codes */
+ switch (TRIO_ERROR_CODE(errorcode))
+ {
+ case TRIO_EOF:
+ return "End of file";
+ case TRIO_EINVAL:
+ return "Invalid argument";
+ case TRIO_ETOOMANY:
+ return "Too many arguments";
+ case TRIO_EDBLREF:
+ return "Double reference";
+ case TRIO_EGAP:
+ return "Reference gap";
+ case TRIO_ENOMEM:
+ return "Out of memory";
+ case TRIO_ERANGE:
+ return "Invalid range";
+ case TRIO_ECUSTOM:
+ return "Custom error";
+ default:
+ return "Unknown";
+ }
+}
diff --git a/trio.h b/trio.h
new file mode 100644
index 0000000..d998f35
--- /dev/null
+++ b/trio.h
@@ -0,0 +1,216 @@
+/*************************************************************************
+ *
+ * $Id: trio.h 1886 2003-04-03 15:28:28Z veillard $
+ *
+ * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ *************************************************************************
+ *
+ * http://ctrio.sourceforge.net/
+ *
+ ************************************************************************/
+
+#ifndef TRIO_TRIO_H
+#define TRIO_TRIO_H
+
+#if !defined(WITHOUT_TRIO)
+
+/*
+ * Use autoconf defines if present. Packages using trio must define
+ * HAVE_CONFIG_H as a compiler option themselves.
+ */
+#if defined(HAVE_CONFIG_H)
+# include <config.h>
+#endif
+
+#include "triodef.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#if defined(TRIO_COMPILER_ANCIENT)
+# include <varargs.h>
+#else
+# include <stdarg.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Error codes.
+ *
+ * Remember to add a textual description to trio_strerror.
+ */
+enum {
+ TRIO_EOF = 1,
+ TRIO_EINVAL = 2,
+ TRIO_ETOOMANY = 3,
+ TRIO_EDBLREF = 4,
+ TRIO_EGAP = 5,
+ TRIO_ENOMEM = 6,
+ TRIO_ERANGE = 7,
+ TRIO_ERRNO = 8,
+ TRIO_ECUSTOM = 9
+};
+
+/* Error macros */
+#define TRIO_ERROR_CODE(x) ((-(x)) & 0x00FF)
+#define TRIO_ERROR_POSITION(x) ((-(x)) >> 8)
+#define TRIO_ERROR_NAME(x) trio_strerror(x)
+
+typedef int (*trio_outstream_t) TRIO_PROTO((trio_pointer_t, int));
+typedef int (*trio_instream_t) TRIO_PROTO((trio_pointer_t));
+
+TRIO_CONST char *trio_strerror TRIO_PROTO((int));
+
+/*************************************************************************
+ * Print Functions
+ */
+
+int trio_printf TRIO_PROTO((TRIO_CONST char *format, ...));
+int trio_vprintf TRIO_PROTO((TRIO_CONST char *format, va_list args));
+int trio_printfv TRIO_PROTO((TRIO_CONST char *format, void **args));
+
+int trio_fprintf TRIO_PROTO((FILE *file, TRIO_CONST char *format, ...));
+int trio_vfprintf TRIO_PROTO((FILE *file, TRIO_CONST char *format, va_list args));
+int trio_fprintfv TRIO_PROTO((FILE *file, TRIO_CONST char *format, void **args));
+
+int trio_dprintf TRIO_PROTO((int fd, TRIO_CONST char *format, ...));
+int trio_vdprintf TRIO_PROTO((int fd, TRIO_CONST char *format, va_list args));
+int trio_dprintfv TRIO_PROTO((int fd, TRIO_CONST char *format, void **args));
+
+int trio_cprintf TRIO_PROTO((trio_outstream_t stream, trio_pointer_t closure,
+ TRIO_CONST char *format, ...));
+int trio_vcprintf TRIO_PROTO((trio_outstream_t stream, trio_pointer_t closure,
+ TRIO_CONST char *format, va_list args));
+int trio_cprintfv TRIO_PROTO((trio_outstream_t stream, trio_pointer_t closure,
+ TRIO_CONST char *format, void **args));
+
+int trio_sprintf TRIO_PROTO((char *buffer, TRIO_CONST char *format, ...));
+int trio_vsprintf TRIO_PROTO((char *buffer, TRIO_CONST char *format, va_list args));
+int trio_sprintfv TRIO_PROTO((char *buffer, TRIO_CONST char *format, void **args));
+
+int trio_snprintf TRIO_PROTO((char *buffer, size_t max, TRIO_CONST char *format, ...));
+int trio_vsnprintf TRIO_PROTO((char *buffer, size_t bufferSize, TRIO_CONST char *format,
+ va_list args));
+int trio_snprintfv TRIO_PROTO((char *buffer, size_t bufferSize, TRIO_CONST char *format,
+ void **args));
+
+int trio_snprintfcat TRIO_PROTO((char *buffer, size_t max, TRIO_CONST char *format, ...));
+int trio_vsnprintfcat TRIO_PROTO((char *buffer, size_t bufferSize, TRIO_CONST char *format,
+ va_list args));
+
+char *trio_aprintf TRIO_PROTO((TRIO_CONST char *format, ...));
+char *trio_vaprintf TRIO_PROTO((TRIO_CONST char *format, va_list args));
+
+int trio_asprintf TRIO_PROTO((char **ret, TRIO_CONST char *format, ...));
+int trio_vasprintf TRIO_PROTO((char **ret, TRIO_CONST char *format, va_list args));
+
+/*************************************************************************
+ * Scan Functions
+ */
+int trio_scanf TRIO_PROTO((TRIO_CONST char *format, ...));
+int trio_vscanf TRIO_PROTO((TRIO_CONST char *format, va_list args));
+int trio_scanfv TRIO_PROTO((TRIO_CONST char *format, void **args));
+
+int trio_fscanf TRIO_PROTO((FILE *file, TRIO_CONST char *format, ...));
+int trio_vfscanf TRIO_PROTO((FILE *file, TRIO_CONST char *format, va_list args));
+int trio_fscanfv TRIO_PROTO((FILE *file, TRIO_CONST char *format, void **args));
+
+int trio_dscanf TRIO_PROTO((int fd, TRIO_CONST char *format, ...));
+int trio_vdscanf TRIO_PROTO((int fd, TRIO_CONST char *format, va_list args));
+int trio_dscanfv TRIO_PROTO((int fd, TRIO_CONST char *format, void **args));
+
+int trio_cscanf TRIO_PROTO((trio_instream_t stream, trio_pointer_t closure,
+ TRIO_CONST char *format, ...));
+int trio_vcscanf TRIO_PROTO((trio_instream_t stream, trio_pointer_t closure,
+ TRIO_CONST char *format, va_list args));
+int trio_cscanfv TRIO_PROTO((trio_instream_t stream, trio_pointer_t closure,
+ TRIO_CONST char *format, void **args));
+
+int trio_sscanf TRIO_PROTO((TRIO_CONST char *buffer, TRIO_CONST char *format, ...));
+int trio_vsscanf TRIO_PROTO((TRIO_CONST char *buffer, TRIO_CONST char *format, va_list args));
+int trio_sscanfv TRIO_PROTO((TRIO_CONST char *buffer, TRIO_CONST char *format, void **args));
+
+/*************************************************************************
+ * Locale Functions
+ */
+void trio_locale_set_decimal_point TRIO_PROTO((char *decimalPoint));
+void trio_locale_set_thousand_separator TRIO_PROTO((char *thousandSeparator));
+void trio_locale_set_grouping TRIO_PROTO((char *grouping));
+
+/*************************************************************************
+ * Renaming
+ */
+#ifdef TRIO_REPLACE_STDIO
+/* Replace the <stdio.h> functions */
+#ifndef HAVE_PRINTF
+# define printf trio_printf
+#endif
+#ifndef HAVE_VPRINTF
+# define vprintf trio_vprintf
+#endif
+#ifndef HAVE_FPRINTF
+# define fprintf trio_fprintf
+#endif
+#ifndef HAVE_VFPRINTF
+# define vfprintf trio_vfprintf
+#endif
+#ifndef HAVE_SPRINTF
+# define sprintf trio_sprintf
+#endif
+#ifndef HAVE_VSPRINTF
+# define vsprintf trio_vsprintf
+#endif
+#ifndef HAVE_SNPRINTF
+# define snprintf trio_snprintf
+#endif
+#ifndef HAVE_VSNPRINTF
+# define vsnprintf trio_vsnprintf
+#endif
+#ifndef HAVE_SCANF
+# define scanf trio_scanf
+#endif
+#ifndef HAVE_VSCANF
+# define vscanf trio_vscanf
+#endif
+#ifndef HAVE_FSCANF
+# define fscanf trio_fscanf
+#endif
+#ifndef HAVE_VFSCANF
+# define vfscanf trio_vfscanf
+#endif
+#ifndef HAVE_SSCANF
+# define sscanf trio_sscanf
+#endif
+#ifndef HAVE_VSSCANF
+# define vsscanf trio_vsscanf
+#endif
+/* These aren't stdio functions, but we make them look similar */
+#define dprintf trio_dprintf
+#define vdprintf trio_vdprintf
+#define aprintf trio_aprintf
+#define vaprintf trio_vaprintf
+#define asprintf trio_asprintf
+#define vasprintf trio_vasprintf
+#define dscanf trio_dscanf
+#define vdscanf trio_vdscanf
+#endif
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* WITHOUT_TRIO */
+
+#endif /* TRIO_TRIO_H */
diff --git a/triodef.h b/triodef.h
new file mode 100644
index 0000000..9ab3af1
--- /dev/null
+++ b/triodef.h
@@ -0,0 +1,222 @@
+/*************************************************************************
+ *
+ * $Id: triodef.h 3473 2006-05-31 13:35:28Z veillard $
+ *
+ * Copyright (C) 2001 Bjorn Reese <breese@users.sourceforge.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ ************************************************************************/
+
+#ifndef TRIO_TRIODEF_H
+#define TRIO_TRIODEF_H
+
+/*************************************************************************
+ * Platform and compiler support detection
+ */
+#if defined(__GNUC__)
+# define TRIO_COMPILER_GCC
+#elif defined(__SUNPRO_C)
+# define TRIO_COMPILER_SUNPRO
+#elif defined(__SUNPRO_CC)
+# define TRIO_COMPILER_SUNPRO
+# define __SUNPRO_C __SUNPRO_CC
+#elif defined(__xlC__) || defined(__IBMC__) || defined(__IBMCPP__)
+# define TRIO_COMPILER_XLC
+#elif defined(_AIX) && !defined(__GNUC__)
+# define TRIO_COMPILER_XLC /* Workaround for old xlc */
+#elif defined(__DECC) || defined(__DECCXX)
+# define TRIO_COMPILER_DECC
+#elif defined(__osf__) && defined(__LANGUAGE_C__)
+# define TRIO_COMPILER_DECC /* Workaround for old DEC C compilers */
+#elif defined(_MSC_VER)
+# define TRIO_COMPILER_MSVC
+#elif defined(__BORLANDC__)
+# define TRIO_COMPILER_BCB
+#endif
+
+#if defined(VMS) || defined(__VMS)
+/*
+ * VMS is placed first to avoid identifying the platform as Unix
+ * based on the DECC compiler later on.
+ */
+# define TRIO_PLATFORM_VMS
+#elif defined(unix) || defined(__unix) || defined(__unix__)
+# define TRIO_PLATFORM_UNIX
+#elif defined(TRIO_COMPILER_XLC) || defined(_AIX)
+# define TRIO_PLATFORM_UNIX
+#elif defined(TRIO_COMPILER_DECC) || defined(__osf___)
+# define TRIO_PLATFORM_UNIX
+#elif defined(__NetBSD__)
+# define TRIO_PLATFORM_UNIX
+#elif defined(__Lynx__)
+# define TRIO_PLATFORM_UNIX
+#elif defined(__QNX__)
+# define TRIO_PLATFORM_UNIX
+# define TRIO_PLATFORM_QNX
+#elif defined(__CYGWIN__)
+# define TRIO_PLATFORM_UNIX
+#elif defined(AMIGA) && defined(TRIO_COMPILER_GCC)
+# define TRIO_PLATFORM_UNIX
+#elif defined(TRIO_COMPILER_MSVC) || defined(WIN32) || defined(_WIN32)
+# define TRIO_PLATFORM_WIN32
+#elif defined(mpeix) || defined(__mpexl)
+# define TRIO_PLATFORM_MPEIX
+#endif
+
+#if defined(_AIX)
+# define TRIO_PLATFORM_AIX
+#elif defined(__hpux)
+# define TRIO_PLATFORM_HPUX
+#elif defined(sun) || defined(__sun__)
+# if defined(__SVR4) || defined(__svr4__)
+# define TRIO_PLATFORM_SOLARIS
+# else
+# define TRIO_PLATFORM_SUNOS
+# endif
+#endif
+
+#if defined(__STDC__) || defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
+# define TRIO_COMPILER_SUPPORTS_C89
+# if defined(__STDC_VERSION__)
+# define TRIO_COMPILER_SUPPORTS_C90
+# if (__STDC_VERSION__ >= 199409L)
+# define TRIO_COMPILER_SUPPORTS_C94
+# endif
+# if (__STDC_VERSION__ >= 199901L)
+# define TRIO_COMPILER_SUPPORTS_C99
+# endif
+# elif defined(TRIO_COMPILER_SUNPRO)
+# if (__SUNPRO_C >= 0x420)
+# define TRIO_COMPILER_SUPPORTS_C94
+# endif
+# endif
+#endif
+
+#if defined(_XOPEN_SOURCE)
+# if defined(_XOPEN_SOURCE_EXTENDED)
+# define TRIO_COMPILER_SUPPORTS_UNIX95
+# endif
+# if (_XOPEN_VERSION >= 500)
+# define TRIO_COMPILER_SUPPORTS_UNIX98
+# endif
+# if (_XOPEN_VERSION >= 600)
+# define TRIO_COMPILER_SUPPORTS_UNIX01
+# endif
+#endif
+
+/*************************************************************************
+ * Generic defines
+ */
+
+#if !defined(TRIO_PUBLIC)
+# define TRIO_PUBLIC
+#endif
+#if !defined(TRIO_PRIVATE)
+# define TRIO_PRIVATE static
+#endif
+
+#if !(defined(TRIO_COMPILER_SUPPORTS_C89) || defined(__cplusplus))
+# define TRIO_COMPILER_ANCIENT
+#endif
+
+#if defined(TRIO_COMPILER_ANCIENT)
+# define TRIO_CONST
+# define TRIO_VOLATILE
+# define TRIO_SIGNED
+typedef double trio_long_double_t;
+typedef char * trio_pointer_t;
+# define TRIO_SUFFIX_LONG(x) x
+# define TRIO_PROTO(x) ()
+# define TRIO_NOARGS
+# define TRIO_ARGS1(list,a1) list a1;
+# define TRIO_ARGS2(list,a1,a2) list a1; a2;
+# define TRIO_ARGS3(list,a1,a2,a3) list a1; a2; a3;
+# define TRIO_ARGS4(list,a1,a2,a3,a4) list a1; a2; a3; a4;
+# define TRIO_ARGS5(list,a1,a2,a3,a4,a5) list a1; a2; a3; a4; a5;
+# define TRIO_ARGS6(list,a1,a2,a3,a4,a5,a6) list a1; a2; a3; a4; a5; a6;
+# define TRIO_VARGS2(list,a1,a2) list a1; a2
+# define TRIO_VARGS3(list,a1,a2,a3) list a1; a2; a3
+# define TRIO_VARGS4(list,a1,a2,a3,a4) list a1; a2; a3; a4
+# define TRIO_VARGS5(list,a1,a2,a3,a4,a5) list a1; a2; a3; a4; a5
+# define TRIO_VA_DECL va_dcl
+# define TRIO_VA_START(x,y) va_start(x)
+# define TRIO_VA_END(x) va_end(x)
+#else /* ANSI C */
+# define TRIO_CONST const
+# define TRIO_VOLATILE volatile
+# define TRIO_SIGNED signed
+typedef long double trio_long_double_t;
+typedef void * trio_pointer_t;
+# define TRIO_SUFFIX_LONG(x) x ## L
+# define TRIO_PROTO(x) x
+# define TRIO_NOARGS void
+# define TRIO_ARGS1(list,a1) (a1)
+# define TRIO_ARGS2(list,a1,a2) (a1,a2)
+# define TRIO_ARGS3(list,a1,a2,a3) (a1,a2,a3)
+# define TRIO_ARGS4(list,a1,a2,a3,a4) (a1,a2,a3,a4)
+# define TRIO_ARGS5(list,a1,a2,a3,a4,a5) (a1,a2,a3,a4,a5)
+# define TRIO_ARGS6(list,a1,a2,a3,a4,a5,a6) (a1,a2,a3,a4,a5,a6)
+# define TRIO_VARGS2 TRIO_ARGS2
+# define TRIO_VARGS3 TRIO_ARGS3
+# define TRIO_VARGS4 TRIO_ARGS4
+# define TRIO_VARGS5 TRIO_ARGS5
+# define TRIO_VA_DECL ...
+# define TRIO_VA_START(x,y) va_start(x,y)
+# define TRIO_VA_END(x) va_end(x)
+#endif
+
+#if defined(TRIO_COMPILER_SUPPORTS_C99) || defined(__cplusplus)
+# define TRIO_INLINE inline
+#elif defined(TRIO_COMPILER_GCC)
+# define TRIO_INLINE __inline__
+#elif defined(TRIO_COMPILER_MSVC)
+# define TRIO_INLINE _inline
+#elif defined(TRIO_COMPILER_BCB)
+# define TRIO_INLINE __inline
+#else
+# define TRIO_INLINE
+#endif
+
+/*************************************************************************
+ * Workarounds
+ */
+
+#if defined(TRIO_PLATFORM_VMS)
+/*
+ * Computations done with constants at compile time can trigger these
+ * even when compiling with IEEE enabled.
+ */
+# pragma message disable (UNDERFLOW, FLOATOVERFL)
+
+# if (__CRTL_VER < 80000000)
+/*
+ * Although the compiler supports C99 language constructs, the C
+ * run-time library does not contain all C99 functions.
+ *
+ * This was the case for 70300022. Update the 80000000 value when
+ * it has been accurately determined what version of the library
+ * supports C99.
+ */
+# if defined(TRIO_COMPILER_SUPPORTS_C99)
+# undef TRIO_COMPILER_SUPPORTS_C99
+# endif
+# endif
+#endif
+
+/*
+ * Not all preprocessors supports the LL token.
+ */
+#if defined(TRIO_COMPILER_BCB)
+#else
+# define TRIO_COMPILER_SUPPORTS_LL
+#endif
+
+#endif /* TRIO_TRIODEF_H */
diff --git a/trionan.c b/trionan.c
new file mode 100644
index 0000000..e160370
--- /dev/null
+++ b/trionan.c
@@ -0,0 +1,914 @@
+/*************************************************************************
+ *
+ * $Id: trionan.c 3790 2008-09-01 13:08:57Z veillard $
+ *
+ * Copyright (C) 2001 Bjorn Reese <breese@users.sourceforge.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ ************************************************************************
+ *
+ * Functions to handle special quantities in floating-point numbers
+ * (that is, NaNs and infinity). They provide the capability to detect
+ * and fabricate special quantities.
+ *
+ * Although written to be as portable as possible, it can never be
+ * guaranteed to work on all platforms, as not all hardware supports
+ * special quantities.
+ *
+ * The approach used here (approximately) is to:
+ *
+ * 1. Use C99 functionality when available.
+ * 2. Use IEEE 754 bit-patterns if possible.
+ * 3. Use platform-specific techniques.
+ *
+ ************************************************************************/
+
+/*
+ * TODO:
+ * o Put all the magic into trio_fpclassify_and_signbit(), and use this from
+ * trio_isnan() etc.
+ */
+
+/*************************************************************************
+ * Include files
+ */
+#include "triodef.h"
+#include "trionan.h"
+
+#include <math.h>
+#include <string.h>
+#include <limits.h>
+#include <float.h>
+#if defined(TRIO_PLATFORM_UNIX)
+# include <signal.h>
+#endif
+#if defined(TRIO_COMPILER_DECC)
+# if defined(__linux__)
+# include <cpml.h>
+# else
+# include <fp_class.h>
+# endif
+#endif
+#include <assert.h>
+
+#if defined(TRIO_DOCUMENTATION)
+# include "doc/doc_nan.h"
+#endif
+/** @addtogroup SpecialQuantities
+ @{
+*/
+
+/*************************************************************************
+ * Definitions
+ */
+
+#define TRIO_TRUE (1 == 1)
+#define TRIO_FALSE (0 == 1)
+
+/*
+ * We must enable IEEE floating-point on Alpha
+ */
+#if defined(__alpha) && !defined(_IEEE_FP)
+# if defined(TRIO_COMPILER_DECC)
+# if defined(TRIO_PLATFORM_VMS)
+# error "Must be compiled with option /IEEE_MODE=UNDERFLOW_TO_ZERO/FLOAT=IEEE"
+# else
+# if !defined(_CFE)
+# error "Must be compiled with option -ieee"
+# endif
+# endif
+# elif defined(TRIO_COMPILER_GCC) && (defined(__osf__) || defined(__linux__))
+# error "Must be compiled with option -mieee"
+# endif
+#endif /* __alpha && ! _IEEE_FP */
+
+/*
+ * In ANSI/IEEE 754-1985 64-bits double format numbers have the
+ * following properties (amoungst others)
+ *
+ * o FLT_RADIX == 2: binary encoding
+ * o DBL_MAX_EXP == 1024: 11 bits exponent, where one bit is used
+ * to indicate special numbers (e.g. NaN and Infinity), so the
+ * maximum exponent is 10 bits wide (2^10 == 1024).
+ * o DBL_MANT_DIG == 53: The mantissa is 52 bits wide, but because
+ * numbers are normalized the initial binary 1 is represented
+ * implicitly (the so-called "hidden bit"), which leaves us with
+ * the ability to represent 53 bits wide mantissa.
+ */
+#if (FLT_RADIX == 2) && (DBL_MAX_EXP == 1024) && (DBL_MANT_DIG == 53)
+# define USE_IEEE_754
+#endif
+
+
+/*************************************************************************
+ * Constants
+ */
+
+static TRIO_CONST char rcsid[] = "@(#)$Id: trionan.c 3790 2008-09-01 13:08:57Z veillard $";
+
+#if defined(USE_IEEE_754)
+
+/*
+ * Endian-agnostic indexing macro.
+ *
+ * The value of internalEndianMagic, when converted into a 64-bit
+ * integer, becomes 0x0706050403020100 (we could have used a 64-bit
+ * integer value instead of a double, but not all platforms supports
+ * that type). The value is automatically encoded with the correct
+ * endianess by the compiler, which means that we can support any
+ * kind of endianess. The individual bytes are then used as an index
+ * for the IEEE 754 bit-patterns and masks.
+ */
+#define TRIO_DOUBLE_INDEX(x) (((unsigned char *)&internalEndianMagic)[7-(x)])
+
+#if (defined(__BORLANDC__) && __BORLANDC__ >= 0x0590)
+static TRIO_CONST double internalEndianMagic = 7.949928895127362e-275;
+#else
+static TRIO_CONST double internalEndianMagic = 7.949928895127363e-275;
+#endif
+
+/* Mask for the exponent */
+static TRIO_CONST unsigned char ieee_754_exponent_mask[] = {
+ 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+/* Mask for the mantissa */
+static TRIO_CONST unsigned char ieee_754_mantissa_mask[] = {
+ 0x00, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+};
+
+/* Mask for the sign bit */
+static TRIO_CONST unsigned char ieee_754_sign_mask[] = {
+ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+/* Bit-pattern for negative zero */
+static TRIO_CONST unsigned char ieee_754_negzero_array[] = {
+ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+/* Bit-pattern for infinity */
+static TRIO_CONST unsigned char ieee_754_infinity_array[] = {
+ 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+/* Bit-pattern for quiet NaN */
+static TRIO_CONST unsigned char ieee_754_qnan_array[] = {
+ 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+};
+
+
+/*************************************************************************
+ * Functions
+ */
+
+/*
+ * trio_make_double
+ */
+TRIO_PRIVATE double
+trio_make_double
+TRIO_ARGS1((values),
+ TRIO_CONST unsigned char *values)
+{
+ TRIO_VOLATILE double result;
+ int i;
+
+ for (i = 0; i < (int)sizeof(double); i++) {
+ ((TRIO_VOLATILE unsigned char *)&result)[TRIO_DOUBLE_INDEX(i)] = values[i];
+ }
+ return result;
+}
+
+/*
+ * trio_is_special_quantity
+ */
+TRIO_PRIVATE int
+trio_is_special_quantity
+TRIO_ARGS2((number, has_mantissa),
+ double number,
+ int *has_mantissa)
+{
+ unsigned int i;
+ unsigned char current;
+ int is_special_quantity = TRIO_TRUE;
+
+ *has_mantissa = 0;
+
+ for (i = 0; i < (unsigned int)sizeof(double); i++) {
+ current = ((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)];
+ is_special_quantity
+ &= ((current & ieee_754_exponent_mask[i]) == ieee_754_exponent_mask[i]);
+ *has_mantissa |= (current & ieee_754_mantissa_mask[i]);
+ }
+ return is_special_quantity;
+}
+
+/*
+ * trio_is_negative
+ */
+TRIO_PRIVATE int
+trio_is_negative
+TRIO_ARGS1((number),
+ double number)
+{
+ unsigned int i;
+ int is_negative = TRIO_FALSE;
+
+ for (i = 0; i < (unsigned int)sizeof(double); i++) {
+ is_negative |= (((unsigned char *)&number)[TRIO_DOUBLE_INDEX(i)]
+ & ieee_754_sign_mask[i]);
+ }
+ return is_negative;
+}
+
+#endif /* USE_IEEE_754 */
+
+
+/**
+ Generate negative zero.
+
+ @return Floating-point representation of negative zero.
+*/
+TRIO_PUBLIC double
+trio_nzero(TRIO_NOARGS)
+{
+#if defined(USE_IEEE_754)
+ return trio_make_double(ieee_754_negzero_array);
+#else
+ TRIO_VOLATILE double zero = 0.0;
+
+ return -zero;
+#endif
+}
+
+/**
+ Generate positive infinity.
+
+ @return Floating-point representation of positive infinity.
+*/
+TRIO_PUBLIC double
+trio_pinf(TRIO_NOARGS)
+{
+ /* Cache the result */
+ static double result = 0.0;
+
+ if (result == 0.0) {
+
+#if defined(INFINITY) && defined(__STDC_IEC_559__)
+ result = (double)INFINITY;
+
+#elif defined(USE_IEEE_754)
+ result = trio_make_double(ieee_754_infinity_array);
+
+#else
+ /*
+ * If HUGE_VAL is different from DBL_MAX, then HUGE_VAL is used
+ * as infinity. Otherwise we have to resort to an overflow
+ * operation to generate infinity.
+ */
+# if defined(TRIO_PLATFORM_UNIX)
+ void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
+# endif
+
+ result = HUGE_VAL;
+ if (HUGE_VAL == DBL_MAX) {
+ /* Force overflow */
+ result += HUGE_VAL;
+ }
+
+# if defined(TRIO_PLATFORM_UNIX)
+ signal(SIGFPE, signal_handler);
+# endif
+
+#endif
+ }
+ return result;
+}
+
+/**
+ Generate negative infinity.
+
+ @return Floating-point value of negative infinity.
+*/
+TRIO_PUBLIC double
+trio_ninf(TRIO_NOARGS)
+{
+ static double result = 0.0;
+
+ if (result == 0.0) {
+ /*
+ * Negative infinity is calculated by negating positive infinity,
+ * which can be done because it is legal to do calculations on
+ * infinity (for example, 1 / infinity == 0).
+ */
+ result = -trio_pinf();
+ }
+ return result;
+}
+
+/**
+ Generate NaN.
+
+ @return Floating-point representation of NaN.
+*/
+TRIO_PUBLIC double
+trio_nan(TRIO_NOARGS)
+{
+ /* Cache the result */
+ static double result = 0.0;
+
+ if (result == 0.0) {
+
+#if defined(TRIO_COMPILER_SUPPORTS_C99)
+ result = nan("");
+
+#elif defined(NAN) && defined(__STDC_IEC_559__)
+ result = (double)NAN;
+
+#elif defined(USE_IEEE_754)
+ result = trio_make_double(ieee_754_qnan_array);
+
+#else
+ /*
+ * There are several ways to generate NaN. The one used here is
+ * to divide infinity by infinity. I would have preferred to add
+ * negative infinity to positive infinity, but that yields wrong
+ * result (infinity) on FreeBSD.
+ *
+ * This may fail if the hardware does not support NaN, or if
+ * the Invalid Operation floating-point exception is unmasked.
+ */
+# if defined(TRIO_PLATFORM_UNIX)
+ void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
+# endif
+
+ result = trio_pinf() / trio_pinf();
+
+# if defined(TRIO_PLATFORM_UNIX)
+ signal(SIGFPE, signal_handler);
+# endif
+
+#endif
+ }
+ return result;
+}
+
+/**
+ Check for NaN.
+
+ @param number An arbitrary floating-point number.
+ @return Boolean value indicating whether or not the number is a NaN.
+*/
+TRIO_PUBLIC int
+trio_isnan
+TRIO_ARGS1((number),
+ double number)
+{
+#if (defined(TRIO_COMPILER_SUPPORTS_C99) && defined(isnan)) \
+ || defined(TRIO_COMPILER_SUPPORTS_UNIX95)
+ /*
+ * C99 defines isnan() as a macro. UNIX95 defines isnan() as a
+ * function. This function was already present in XPG4, but this
+ * is a bit tricky to detect with compiler defines, so we choose
+ * the conservative approach and only use it for UNIX95.
+ */
+ return isnan(number);
+
+#elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
+ /*
+ * Microsoft Visual C++ and Borland C++ Builder have an _isnan()
+ * function.
+ */
+ return _isnan(number) ? TRIO_TRUE : TRIO_FALSE;
+
+#elif defined(USE_IEEE_754)
+ /*
+ * Examine IEEE 754 bit-pattern. A NaN must have a special exponent
+ * pattern, and a non-empty mantissa.
+ */
+ int has_mantissa;
+ int is_special_quantity;
+
+ is_special_quantity = trio_is_special_quantity(number, &has_mantissa);
+
+ return (is_special_quantity && has_mantissa);
+
+#else
+ /*
+ * Fallback solution
+ */
+ int status;
+ double integral, fraction;
+
+# if defined(TRIO_PLATFORM_UNIX)
+ void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
+# endif
+
+ status = (/*
+ * NaN is the only number which does not compare to itself
+ */
+ ((TRIO_VOLATILE double)number != (TRIO_VOLATILE double)number) ||
+ /*
+ * Fallback solution if NaN compares to NaN
+ */
+ ((number != 0.0) &&
+ (fraction = modf(number, &integral),
+ integral == fraction)));
+
+# if defined(TRIO_PLATFORM_UNIX)
+ signal(SIGFPE, signal_handler);
+# endif
+
+ return status;
+
+#endif
+}
+
+/**
+ Check for infinity.
+
+ @param number An arbitrary floating-point number.
+ @return 1 if positive infinity, -1 if negative infinity, 0 otherwise.
+*/
+TRIO_PUBLIC int
+trio_isinf
+TRIO_ARGS1((number),
+ double number)
+{
+#if defined(TRIO_COMPILER_DECC) && !defined(__linux__)
+ /*
+ * DECC has an isinf() macro, but it works differently than that
+ * of C99, so we use the fp_class() function instead.
+ */
+ return ((fp_class(number) == FP_POS_INF)
+ ? 1
+ : ((fp_class(number) == FP_NEG_INF) ? -1 : 0));
+
+#elif defined(isinf)
+ /*
+ * C99 defines isinf() as a macro.
+ */
+ return isinf(number)
+ ? ((number > 0.0) ? 1 : -1)
+ : 0;
+
+#elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
+ /*
+ * Microsoft Visual C++ and Borland C++ Builder have an _fpclass()
+ * function that can be used to detect infinity.
+ */
+ return ((_fpclass(number) == _FPCLASS_PINF)
+ ? 1
+ : ((_fpclass(number) == _FPCLASS_NINF) ? -1 : 0));
+
+#elif defined(USE_IEEE_754)
+ /*
+ * Examine IEEE 754 bit-pattern. Infinity must have a special exponent
+ * pattern, and an empty mantissa.
+ */
+ int has_mantissa;
+ int is_special_quantity;
+
+ is_special_quantity = trio_is_special_quantity(number, &has_mantissa);
+
+ return (is_special_quantity && !has_mantissa)
+ ? ((number < 0.0) ? -1 : 1)
+ : 0;
+
+#else
+ /*
+ * Fallback solution.
+ */
+ int status;
+
+# if defined(TRIO_PLATFORM_UNIX)
+ void (*signal_handler)(int) = signal(SIGFPE, SIG_IGN);
+# endif
+
+ double infinity = trio_pinf();
+
+ status = ((number == infinity)
+ ? 1
+ : ((number == -infinity) ? -1 : 0));
+
+# if defined(TRIO_PLATFORM_UNIX)
+ signal(SIGFPE, signal_handler);
+# endif
+
+ return status;
+
+#endif
+}
+
+#if 0
+ /* Temporary fix - this routine is not used anywhere */
+/**
+ Check for finity.
+
+ @param number An arbitrary floating-point number.
+ @return Boolean value indicating whether or not the number is a finite.
+*/
+TRIO_PUBLIC int
+trio_isfinite
+TRIO_ARGS1((number),
+ double number)
+{
+#if defined(TRIO_COMPILER_SUPPORTS_C99) && defined(isfinite)
+ /*
+ * C99 defines isfinite() as a macro.
+ */
+ return isfinite(number);
+
+#elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
+ /*
+ * Microsoft Visual C++ and Borland C++ Builder use _finite().
+ */
+ return _finite(number);
+
+#elif defined(USE_IEEE_754)
+ /*
+ * Examine IEEE 754 bit-pattern. For finity we do not care about the
+ * mantissa.
+ */
+ int dummy;
+
+ return (! trio_is_special_quantity(number, &dummy));
+
+#else
+ /*
+ * Fallback solution.
+ */
+ return ((trio_isinf(number) == 0) && (trio_isnan(number) == 0));
+
+#endif
+}
+
+#endif
+
+/*
+ * The sign of NaN is always false
+ */
+TRIO_PUBLIC int
+trio_fpclassify_and_signbit
+TRIO_ARGS2((number, is_negative),
+ double number,
+ int *is_negative)
+{
+#if defined(fpclassify) && defined(signbit)
+ /*
+ * C99 defines fpclassify() and signbit() as a macros
+ */
+ *is_negative = signbit(number);
+ switch (fpclassify(number)) {
+ case FP_NAN:
+ return TRIO_FP_NAN;
+ case FP_INFINITE:
+ return TRIO_FP_INFINITE;
+ case FP_SUBNORMAL:
+ return TRIO_FP_SUBNORMAL;
+ case FP_ZERO:
+ return TRIO_FP_ZERO;
+ default:
+ return TRIO_FP_NORMAL;
+ }
+
+#else
+# if defined(TRIO_COMPILER_DECC)
+ /*
+ * DECC has an fp_class() function.
+ */
+# define TRIO_FPCLASSIFY(n) fp_class(n)
+# define TRIO_QUIET_NAN FP_QNAN
+# define TRIO_SIGNALLING_NAN FP_SNAN
+# define TRIO_POSITIVE_INFINITY FP_POS_INF
+# define TRIO_NEGATIVE_INFINITY FP_NEG_INF
+# define TRIO_POSITIVE_SUBNORMAL FP_POS_DENORM
+# define TRIO_NEGATIVE_SUBNORMAL FP_NEG_DENORM
+# define TRIO_POSITIVE_ZERO FP_POS_ZERO
+# define TRIO_NEGATIVE_ZERO FP_NEG_ZERO
+# define TRIO_POSITIVE_NORMAL FP_POS_NORM
+# define TRIO_NEGATIVE_NORMAL FP_NEG_NORM
+
+# elif defined(TRIO_COMPILER_MSVC) || defined(TRIO_COMPILER_BCB)
+ /*
+ * Microsoft Visual C++ and Borland C++ Builder have an _fpclass()
+ * function.
+ */
+# define TRIO_FPCLASSIFY(n) _fpclass(n)
+# define TRIO_QUIET_NAN _FPCLASS_QNAN
+# define TRIO_SIGNALLING_NAN _FPCLASS_SNAN
+# define TRIO_POSITIVE_INFINITY _FPCLASS_PINF
+# define TRIO_NEGATIVE_INFINITY _FPCLASS_NINF
+# define TRIO_POSITIVE_SUBNORMAL _FPCLASS_PD
+# define TRIO_NEGATIVE_SUBNORMAL _FPCLASS_ND
+# define TRIO_POSITIVE_ZERO _FPCLASS_PZ
+# define TRIO_NEGATIVE_ZERO _FPCLASS_NZ
+# define TRIO_POSITIVE_NORMAL _FPCLASS_PN
+# define TRIO_NEGATIVE_NORMAL _FPCLASS_NN
+
+# elif defined(FP_PLUS_NORM)
+ /*
+ * HP-UX 9.x and 10.x have an fpclassify() function, that is different
+ * from the C99 fpclassify() macro supported on HP-UX 11.x.
+ *
+ * AIX has class() for C, and _class() for C++, which returns the
+ * same values as the HP-UX fpclassify() function.
+ */
+# if defined(TRIO_PLATFORM_AIX)
+# if defined(__cplusplus)
+# define TRIO_FPCLASSIFY(n) _class(n)
+# else
+# define TRIO_FPCLASSIFY(n) class(n)
+# endif
+# else
+# define TRIO_FPCLASSIFY(n) fpclassify(n)
+# endif
+# define TRIO_QUIET_NAN FP_QNAN
+# define TRIO_SIGNALLING_NAN FP_SNAN
+# define TRIO_POSITIVE_INFINITY FP_PLUS_INF
+# define TRIO_NEGATIVE_INFINITY FP_MINUS_INF
+# define TRIO_POSITIVE_SUBNORMAL FP_PLUS_DENORM
+# define TRIO_NEGATIVE_SUBNORMAL FP_MINUS_DENORM
+# define TRIO_POSITIVE_ZERO FP_PLUS_ZERO
+# define TRIO_NEGATIVE_ZERO FP_MINUS_ZERO
+# define TRIO_POSITIVE_NORMAL FP_PLUS_NORM
+# define TRIO_NEGATIVE_NORMAL FP_MINUS_NORM
+# endif
+
+# if defined(TRIO_FPCLASSIFY)
+ switch (TRIO_FPCLASSIFY(number)) {
+ case TRIO_QUIET_NAN:
+ case TRIO_SIGNALLING_NAN:
+ *is_negative = TRIO_FALSE; /* NaN has no sign */
+ return TRIO_FP_NAN;
+ case TRIO_POSITIVE_INFINITY:
+ *is_negative = TRIO_FALSE;
+ return TRIO_FP_INFINITE;
+ case TRIO_NEGATIVE_INFINITY:
+ *is_negative = TRIO_TRUE;
+ return TRIO_FP_INFINITE;
+ case TRIO_POSITIVE_SUBNORMAL:
+ *is_negative = TRIO_FALSE;
+ return TRIO_FP_SUBNORMAL;
+ case TRIO_NEGATIVE_SUBNORMAL:
+ *is_negative = TRIO_TRUE;
+ return TRIO_FP_SUBNORMAL;
+ case TRIO_POSITIVE_ZERO:
+ *is_negative = TRIO_FALSE;
+ return TRIO_FP_ZERO;
+ case TRIO_NEGATIVE_ZERO:
+ *is_negative = TRIO_TRUE;
+ return TRIO_FP_ZERO;
+ case TRIO_POSITIVE_NORMAL:
+ *is_negative = TRIO_FALSE;
+ return TRIO_FP_NORMAL;
+ case TRIO_NEGATIVE_NORMAL:
+ *is_negative = TRIO_TRUE;
+ return TRIO_FP_NORMAL;
+ default:
+ /* Just in case... */
+ *is_negative = (number < 0.0);
+ return TRIO_FP_NORMAL;
+ }
+
+# else
+ /*
+ * Fallback solution.
+ */
+ int rc;
+
+ if (number == 0.0) {
+ /*
+ * In IEEE 754 the sign of zero is ignored in comparisons, so we
+ * have to handle this as a special case by examining the sign bit
+ * directly.
+ */
+# if defined(USE_IEEE_754)
+ *is_negative = trio_is_negative(number);
+# else
+ *is_negative = TRIO_FALSE; /* FIXME */
+# endif
+ return TRIO_FP_ZERO;
+ }
+ if (trio_isnan(number)) {
+ *is_negative = TRIO_FALSE;
+ return TRIO_FP_NAN;
+ }
+ if ((rc = trio_isinf(number))) {
+ *is_negative = (rc == -1);
+ return TRIO_FP_INFINITE;
+ }
+ if ((number > 0.0) && (number < DBL_MIN)) {
+ *is_negative = TRIO_FALSE;
+ return TRIO_FP_SUBNORMAL;
+ }
+ if ((number < 0.0) && (number > -DBL_MIN)) {
+ *is_negative = TRIO_TRUE;
+ return TRIO_FP_SUBNORMAL;
+ }
+ *is_negative = (number < 0.0);
+ return TRIO_FP_NORMAL;
+
+# endif
+#endif
+}
+
+/**
+ Examine the sign of a number.
+
+ @param number An arbitrary floating-point number.
+ @return Boolean value indicating whether or not the number has the
+ sign bit set (i.e. is negative).
+*/
+TRIO_PUBLIC int
+trio_signbit
+TRIO_ARGS1((number),
+ double number)
+{
+ int is_negative;
+
+ (void)trio_fpclassify_and_signbit(number, &is_negative);
+ return is_negative;
+}
+
+#if 0
+ /* Temporary fix - this routine is not used in libxml */
+/**
+ Examine the class of a number.
+
+ @param number An arbitrary floating-point number.
+ @return Enumerable value indicating the class of @p number
+*/
+TRIO_PUBLIC int
+trio_fpclassify
+TRIO_ARGS1((number),
+ double number)
+{
+ int dummy;
+
+ return trio_fpclassify_and_signbit(number, &dummy);
+}
+
+#endif
+
+/** @} SpecialQuantities */
+
+/*************************************************************************
+ * For test purposes.
+ *
+ * Add the following compiler option to include this test code.
+ *
+ * Unix : -DSTANDALONE
+ * VMS : /DEFINE=(STANDALONE)
+ */
+#if defined(STANDALONE)
+# include <stdio.h>
+
+static TRIO_CONST char *
+getClassification
+TRIO_ARGS1((type),
+ int type)
+{
+ switch (type) {
+ case TRIO_FP_INFINITE:
+ return "FP_INFINITE";
+ case TRIO_FP_NAN:
+ return "FP_NAN";
+ case TRIO_FP_NORMAL:
+ return "FP_NORMAL";
+ case TRIO_FP_SUBNORMAL:
+ return "FP_SUBNORMAL";
+ case TRIO_FP_ZERO:
+ return "FP_ZERO";
+ default:
+ return "FP_UNKNOWN";
+ }
+}
+
+static void
+print_class
+TRIO_ARGS2((prefix, number),
+ TRIO_CONST char *prefix,
+ double number)
+{
+ printf("%-6s: %s %-15s %g\n",
+ prefix,
+ trio_signbit(number) ? "-" : "+",
+ getClassification(TRIO_FPCLASSIFY(number)),
+ number);
+}
+
+int main(TRIO_NOARGS)
+{
+ double my_nan;
+ double my_pinf;
+ double my_ninf;
+# if defined(TRIO_PLATFORM_UNIX)
+ void (*signal_handler) TRIO_PROTO((int));
+# endif
+
+ my_nan = trio_nan();
+ my_pinf = trio_pinf();
+ my_ninf = trio_ninf();
+
+ print_class("Nan", my_nan);
+ print_class("PInf", my_pinf);
+ print_class("NInf", my_ninf);
+ print_class("PZero", 0.0);
+ print_class("NZero", -0.0);
+ print_class("PNorm", 1.0);
+ print_class("NNorm", -1.0);
+ print_class("PSub", 1.01e-307 - 1.00e-307);
+ print_class("NSub", 1.00e-307 - 1.01e-307);
+
+ printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
+ my_nan,
+ ((unsigned char *)&my_nan)[0],
+ ((unsigned char *)&my_nan)[1],
+ ((unsigned char *)&my_nan)[2],
+ ((unsigned char *)&my_nan)[3],
+ ((unsigned char *)&my_nan)[4],
+ ((unsigned char *)&my_nan)[5],
+ ((unsigned char *)&my_nan)[6],
+ ((unsigned char *)&my_nan)[7],
+ trio_isnan(my_nan), trio_isinf(my_nan));
+ printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
+ my_pinf,
+ ((unsigned char *)&my_pinf)[0],
+ ((unsigned char *)&my_pinf)[1],
+ ((unsigned char *)&my_pinf)[2],
+ ((unsigned char *)&my_pinf)[3],
+ ((unsigned char *)&my_pinf)[4],
+ ((unsigned char *)&my_pinf)[5],
+ ((unsigned char *)&my_pinf)[6],
+ ((unsigned char *)&my_pinf)[7],
+ trio_isnan(my_pinf), trio_isinf(my_pinf));
+ printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
+ my_ninf,
+ ((unsigned char *)&my_ninf)[0],
+ ((unsigned char *)&my_ninf)[1],
+ ((unsigned char *)&my_ninf)[2],
+ ((unsigned char *)&my_ninf)[3],
+ ((unsigned char *)&my_ninf)[4],
+ ((unsigned char *)&my_ninf)[5],
+ ((unsigned char *)&my_ninf)[6],
+ ((unsigned char *)&my_ninf)[7],
+ trio_isnan(my_ninf), trio_isinf(my_ninf));
+
+# if defined(TRIO_PLATFORM_UNIX)
+ signal_handler = signal(SIGFPE, SIG_IGN);
+# endif
+
+ my_pinf = DBL_MAX + DBL_MAX;
+ my_ninf = -my_pinf;
+ my_nan = my_pinf / my_pinf;
+
+# if defined(TRIO_PLATFORM_UNIX)
+ signal(SIGFPE, signal_handler);
+# endif
+
+ printf("NaN : %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
+ my_nan,
+ ((unsigned char *)&my_nan)[0],
+ ((unsigned char *)&my_nan)[1],
+ ((unsigned char *)&my_nan)[2],
+ ((unsigned char *)&my_nan)[3],
+ ((unsigned char *)&my_nan)[4],
+ ((unsigned char *)&my_nan)[5],
+ ((unsigned char *)&my_nan)[6],
+ ((unsigned char *)&my_nan)[7],
+ trio_isnan(my_nan), trio_isinf(my_nan));
+ printf("PInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
+ my_pinf,
+ ((unsigned char *)&my_pinf)[0],
+ ((unsigned char *)&my_pinf)[1],
+ ((unsigned char *)&my_pinf)[2],
+ ((unsigned char *)&my_pinf)[3],
+ ((unsigned char *)&my_pinf)[4],
+ ((unsigned char *)&my_pinf)[5],
+ ((unsigned char *)&my_pinf)[6],
+ ((unsigned char *)&my_pinf)[7],
+ trio_isnan(my_pinf), trio_isinf(my_pinf));
+ printf("NInf: %4g 0x%02x%02x%02x%02x%02x%02x%02x%02x (%2d, %2d)\n",
+ my_ninf,
+ ((unsigned char *)&my_ninf)[0],
+ ((unsigned char *)&my_ninf)[1],
+ ((unsigned char *)&my_ninf)[2],
+ ((unsigned char *)&my_ninf)[3],
+ ((unsigned char *)&my_ninf)[4],
+ ((unsigned char *)&my_ninf)[5],
+ ((unsigned char *)&my_ninf)[6],
+ ((unsigned char *)&my_ninf)[7],
+ trio_isnan(my_ninf), trio_isinf(my_ninf));
+
+ return 0;
+}
+#endif
diff --git a/trionan.h b/trionan.h
new file mode 100644
index 0000000..92de0fd
--- /dev/null
+++ b/trionan.h
@@ -0,0 +1,84 @@
+/*************************************************************************
+ *
+ * $Id: trionan.h 2026 2003-08-06 04:43:55Z wbrack $
+ *
+ * Copyright (C) 2001 Bjorn Reese <breese@users.sourceforge.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ ************************************************************************/
+
+#ifndef TRIO_NAN_H
+#define TRIO_NAN_H
+
+#include "triodef.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+enum {
+ TRIO_FP_INFINITE,
+ TRIO_FP_NAN,
+ TRIO_FP_NORMAL,
+ TRIO_FP_SUBNORMAL,
+ TRIO_FP_ZERO
+};
+
+/*
+ * Return NaN (Not-a-Number).
+ */
+TRIO_PUBLIC double trio_nan TRIO_PROTO((void));
+
+/*
+ * Return positive infinity.
+ */
+TRIO_PUBLIC double trio_pinf TRIO_PROTO((void));
+
+/*
+ * Return negative infinity.
+ */
+TRIO_PUBLIC double trio_ninf TRIO_PROTO((void));
+
+/*
+ * Return negative zero.
+ */
+TRIO_PUBLIC double trio_nzero TRIO_PROTO((TRIO_NOARGS));
+
+/*
+ * If number is a NaN return non-zero, otherwise return zero.
+ */
+TRIO_PUBLIC int trio_isnan TRIO_PROTO((double number));
+
+/*
+ * If number is positive infinity return 1, if number is negative
+ * infinity return -1, otherwise return 0.
+ */
+TRIO_PUBLIC int trio_isinf TRIO_PROTO((double number));
+
+/*
+ * If number is finite return non-zero, otherwise return zero.
+ */
+#if 0
+ /* Temporary fix - these 2 routines not used in libxml */
+TRIO_PUBLIC int trio_isfinite TRIO_PROTO((double number));
+
+TRIO_PUBLIC int trio_fpclassify TRIO_PROTO((double number));
+#endif
+
+TRIO_PUBLIC int trio_signbit TRIO_PROTO((double number));
+
+TRIO_PUBLIC int trio_fpclassify_and_signbit TRIO_PROTO((double number, int *is_negative));
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* TRIO_NAN_H */
diff --git a/triop.h b/triop.h
new file mode 100644
index 0000000..a062c96
--- /dev/null
+++ b/triop.h
@@ -0,0 +1,150 @@
+/*************************************************************************
+ *
+ * $Id: triop.h 1886 2003-04-03 15:28:28Z veillard $
+ *
+ * Copyright (C) 2000 Bjorn Reese and Daniel Stenberg.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ ************************************************************************
+ *
+ * Private functions, types, etc. used for callback functions.
+ *
+ * The ref pointer is an opaque type and should remain as such.
+ * Private data must only be accessible through the getter and
+ * setter functions.
+ *
+ ************************************************************************/
+
+#ifndef TRIO_TRIOP_H
+#define TRIO_TRIOP_H
+
+#include "triodef.h"
+
+#include <stdlib.h>
+#if defined(TRIO_COMPILER_ANCIENT)
+# include <varargs.h>
+#else
+# include <stdarg.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef TRIO_C99
+# define TRIO_C99 1
+#endif
+#ifndef TRIO_BSD
+# define TRIO_BSD 1
+#endif
+#ifndef TRIO_GNU
+# define TRIO_GNU 1
+#endif
+#ifndef TRIO_MISC
+# define TRIO_MISC 1
+#endif
+#ifndef TRIO_UNIX98
+# define TRIO_UNIX98 1
+#endif
+#ifndef TRIO_MICROSOFT
+# define TRIO_MICROSOFT 1
+#endif
+#ifndef TRIO_EXTENSION
+# define TRIO_EXTENSION 1
+#endif
+#ifndef TRIO_WIDECHAR /* Does not work yet. Do not enable */
+# define TRIO_WIDECHAR 0
+#endif
+#ifndef TRIO_ERRORS
+# define TRIO_ERRORS 1
+#endif
+
+#ifndef TRIO_MALLOC
+# define TRIO_MALLOC(n) malloc(n)
+#endif
+#ifndef TRIO_REALLOC
+# define TRIO_REALLOC(x,n) realloc((x),(n))
+#endif
+#ifndef TRIO_FREE
+# define TRIO_FREE(x) free(x)
+#endif
+
+
+/*************************************************************************
+ * User-defined specifiers
+ */
+
+typedef int (*trio_callback_t) TRIO_PROTO((trio_pointer_t));
+
+trio_pointer_t trio_register TRIO_PROTO((trio_callback_t callback, const char *name));
+void trio_unregister TRIO_PROTO((trio_pointer_t handle));
+
+TRIO_CONST char *trio_get_format TRIO_PROTO((trio_pointer_t ref));
+trio_pointer_t trio_get_argument TRIO_PROTO((trio_pointer_t ref));
+
+/* Modifiers */
+int trio_get_width TRIO_PROTO((trio_pointer_t ref));
+void trio_set_width TRIO_PROTO((trio_pointer_t ref, int width));
+int trio_get_precision TRIO_PROTO((trio_pointer_t ref));
+void trio_set_precision TRIO_PROTO((trio_pointer_t ref, int precision));
+int trio_get_base TRIO_PROTO((trio_pointer_t ref));
+void trio_set_base TRIO_PROTO((trio_pointer_t ref, int base));
+int trio_get_padding TRIO_PROTO((trio_pointer_t ref));
+void trio_set_padding TRIO_PROTO((trio_pointer_t ref, int is_padding));
+int trio_get_short TRIO_PROTO((trio_pointer_t ref)); /* h */
+void trio_set_shortshort TRIO_PROTO((trio_pointer_t ref, int is_shortshort));
+int trio_get_shortshort TRIO_PROTO((trio_pointer_t ref)); /* hh */
+void trio_set_short TRIO_PROTO((trio_pointer_t ref, int is_short));
+int trio_get_long TRIO_PROTO((trio_pointer_t ref)); /* l */
+void trio_set_long TRIO_PROTO((trio_pointer_t ref, int is_long));
+int trio_get_longlong TRIO_PROTO((trio_pointer_t ref)); /* ll */
+void trio_set_longlong TRIO_PROTO((trio_pointer_t ref, int is_longlong));
+int trio_get_longdouble TRIO_PROTO((trio_pointer_t ref)); /* L */
+void trio_set_longdouble TRIO_PROTO((trio_pointer_t ref, int is_longdouble));
+int trio_get_alternative TRIO_PROTO((trio_pointer_t ref)); /* # */
+void trio_set_alternative TRIO_PROTO((trio_pointer_t ref, int is_alternative));
+int trio_get_alignment TRIO_PROTO((trio_pointer_t ref)); /* - */
+void trio_set_alignment TRIO_PROTO((trio_pointer_t ref, int is_leftaligned));
+int trio_get_spacing TRIO_PROTO((trio_pointer_t ref)); /* TRIO_PROTO((space) */
+void trio_set_spacing TRIO_PROTO((trio_pointer_t ref, int is_space));
+int trio_get_sign TRIO_PROTO((trio_pointer_t ref)); /* + */
+void trio_set_sign TRIO_PROTO((trio_pointer_t ref, int is_showsign));
+int trio_get_quote TRIO_PROTO((trio_pointer_t ref)); /* ' */
+void trio_set_quote TRIO_PROTO((trio_pointer_t ref, int is_quote));
+int trio_get_upper TRIO_PROTO((trio_pointer_t ref));
+void trio_set_upper TRIO_PROTO((trio_pointer_t ref, int is_upper));
+#if TRIO_C99
+int trio_get_largest TRIO_PROTO((trio_pointer_t ref)); /* j */
+void trio_set_largest TRIO_PROTO((trio_pointer_t ref, int is_largest));
+int trio_get_ptrdiff TRIO_PROTO((trio_pointer_t ref)); /* t */
+void trio_set_ptrdiff TRIO_PROTO((trio_pointer_t ref, int is_ptrdiff));
+int trio_get_size TRIO_PROTO((trio_pointer_t ref)); /* z / Z */
+void trio_set_size TRIO_PROTO((trio_pointer_t ref, int is_size));
+#endif
+
+/* Printing */
+int trio_print_ref TRIO_PROTO((trio_pointer_t ref, const char *format, ...));
+int trio_vprint_ref TRIO_PROTO((trio_pointer_t ref, const char *format, va_list args));
+int trio_printv_ref TRIO_PROTO((trio_pointer_t ref, const char *format, trio_pointer_t *args));
+
+void trio_print_int TRIO_PROTO((trio_pointer_t ref, int number));
+void trio_print_uint TRIO_PROTO((trio_pointer_t ref, unsigned int number));
+/* void trio_print_long TRIO_PROTO((trio_pointer_t ref, long number)); */
+/* void trio_print_ulong TRIO_PROTO((trio_pointer_t ref, unsigned long number)); */
+void trio_print_double TRIO_PROTO((trio_pointer_t ref, double number));
+void trio_print_string TRIO_PROTO((trio_pointer_t ref, char *string));
+void trio_print_pointer TRIO_PROTO((trio_pointer_t ref, trio_pointer_t pointer));
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* TRIO_TRIOP_H */
diff --git a/triostr.c b/triostr.c
new file mode 100644
index 0000000..dc5592c
--- /dev/null
+++ b/triostr.c
@@ -0,0 +1,2106 @@
+/*************************************************************************
+ *
+ * $Id: triostr.c 3600 2007-04-17 12:44:58Z veillard $
+ *
+ * Copyright (C) 2001 Bjorn Reese and Daniel Stenberg.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ ************************************************************************/
+
+/*************************************************************************
+ * Include files
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <math.h>
+#include "triodef.h"
+#include "triostr.h"
+
+/*************************************************************************
+ * Definitions
+ */
+
+#if !defined(TRIO_STRING_PUBLIC)
+# define TRIO_STRING_PUBLIC TRIO_PUBLIC
+#endif
+#if !defined(TRIO_STRING_PRIVATE)
+# define TRIO_STRING_PRIVATE TRIO_PRIVATE
+#endif
+
+#if !defined(NULL)
+# define NULL 0
+#endif
+#if !defined(NIL)
+# define NIL ((char)0)
+#endif
+#if !defined(FALSE)
+# define FALSE (1 == 0)
+# define TRUE (! FALSE)
+#endif
+#if !defined(BOOLEAN_T)
+# define BOOLEAN_T int
+#endif
+
+#if defined(TRIO_COMPILER_SUPPORTS_C99)
+# define USE_STRTOD
+# define USE_STRTOF
+#elif defined(TRIO_COMPILER_MSVC)
+# define USE_STRTOD
+#endif
+
+#if defined(TRIO_PLATFORM_UNIX)
+# define USE_STRCASECMP
+# define USE_STRNCASECMP
+# if defined(TRIO_PLATFORM_SUNOS)
+# define USE_SYS_ERRLIST
+# else
+# define USE_STRERROR
+# endif
+# if defined(TRIO_PLATFORM_QNX)
+# define strcasecmp(x,y) stricmp(x,y)
+# define strncasecmp(x,y,n) strnicmp(x,y,n)
+# endif
+#elif defined(TRIO_PLATFORM_WIN32)
+# define USE_STRCASECMP
+# if defined(_WIN32_WCE)
+# define strcasecmp(x,y) _stricmp(x,y)
+# else
+# define strcasecmp(x,y) strcmpi(x,y)
+# endif
+#endif
+
+#if !(defined(TRIO_PLATFORM_SUNOS))
+# define USE_TOLOWER
+# define USE_TOUPPER
+#endif
+
+/*************************************************************************
+ * Structures
+ */
+
+struct _trio_string_t
+{
+ char *content;
+ size_t length;
+ size_t allocated;
+};
+
+/*************************************************************************
+ * Constants
+ */
+
+#if !defined(TRIO_MINIMAL)
+static TRIO_CONST char rcsid[] = "@(#)$Id: triostr.c 3600 2007-04-17 12:44:58Z veillard $";
+#endif
+
+/*************************************************************************
+ * Static String Functions
+ */
+
+#if defined(TRIO_DOCUMENTATION)
+# include "doc/doc_static.h"
+#endif
+/** @addtogroup StaticStrings
+ @{
+*/
+
+/**
+ Create new string.
+
+ @param size Size of new string.
+ @return Pointer to string, or NULL if allocation failed.
+*/
+TRIO_STRING_PUBLIC char *
+trio_create
+TRIO_ARGS1((size),
+ size_t size)
+{
+ return (char *)TRIO_MALLOC(size);
+}
+
+
+/**
+ Destroy string.
+
+ @param string String to be freed.
+*/
+TRIO_STRING_PUBLIC void
+trio_destroy
+TRIO_ARGS1((string),
+ char *string)
+{
+ if (string)
+ {
+ TRIO_FREE(string);
+ }
+}
+
+
+/**
+ Count the number of characters in a string.
+
+ @param string String to measure.
+ @return Number of characters in @string.
+*/
+TRIO_STRING_PUBLIC size_t
+trio_length
+TRIO_ARGS1((string),
+ TRIO_CONST char *string)
+{
+ return strlen(string);
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Append @p source at the end of @p target.
+
+ @param target Target string.
+ @param source Source string.
+ @return Boolean value indicating success or failure.
+
+ @pre @p target must point to a memory chunk with sufficient room to
+ contain the @p target string and @p source string.
+ @pre No boundary checking is performed, so insufficient memory will
+ result in a buffer overrun.
+ @post @p target will be zero terminated.
+*/
+TRIO_STRING_PUBLIC int
+trio_append
+TRIO_ARGS2((target, source),
+ char *target,
+ TRIO_CONST char *source)
+{
+ assert(target);
+ assert(source);
+
+ return (strcat(target, source) != NULL);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Append at most @p max characters from @p source to @p target.
+
+ @param target Target string.
+ @param max Maximum number of characters to append.
+ @param source Source string.
+ @return Boolean value indicating success or failure.
+
+ @pre @p target must point to a memory chuck with sufficient room to
+ contain the @p target string and the @p source string (at most @p max
+ characters).
+ @pre No boundary checking is performed, so insufficient memory will
+ result in a buffer overrun.
+ @post @p target will be zero terminated.
+*/
+TRIO_STRING_PUBLIC int
+trio_append_max
+TRIO_ARGS3((target, max, source),
+ char *target,
+ size_t max,
+ TRIO_CONST char *source)
+{
+ size_t length;
+
+ assert(target);
+ assert(source);
+
+ length = trio_length(target);
+
+ if (max > length)
+ {
+ strncat(target, source, max - length - 1);
+ }
+ return TRUE;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Determine if a string contains a substring.
+
+ @param string String to be searched.
+ @param substring String to be found.
+ @return Boolean value indicating success or failure.
+*/
+TRIO_STRING_PUBLIC int
+trio_contains
+TRIO_ARGS2((string, substring),
+ TRIO_CONST char *string,
+ TRIO_CONST char *substring)
+{
+ assert(string);
+ assert(substring);
+
+ return (0 != strstr(string, substring));
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Copy @p source to @p target.
+
+ @param target Target string.
+ @param source Source string.
+ @return Boolean value indicating success or failure.
+
+ @pre @p target must point to a memory chunk with sufficient room to
+ contain the @p source string.
+ @pre No boundary checking is performed, so insufficient memory will
+ result in a buffer overrun.
+ @post @p target will be zero terminated.
+*/
+TRIO_STRING_PUBLIC int
+trio_copy
+TRIO_ARGS2((target, source),
+ char *target,
+ TRIO_CONST char *source)
+{
+ assert(target);
+ assert(source);
+
+ (void)strcpy(target, source);
+ return TRUE;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/**
+ Copy at most @p max characters from @p source to @p target.
+
+ @param target Target string.
+ @param max Maximum number of characters to append.
+ @param source Source string.
+ @return Boolean value indicating success or failure.
+
+ @pre @p target must point to a memory chunk with sufficient room to
+ contain the @p source string (at most @p max characters).
+ @pre No boundary checking is performed, so insufficient memory will
+ result in a buffer overrun.
+ @post @p target will be zero terminated.
+*/
+TRIO_STRING_PUBLIC int
+trio_copy_max
+TRIO_ARGS3((target, max, source),
+ char *target,
+ size_t max,
+ TRIO_CONST char *source)
+{
+ assert(target);
+ assert(source);
+ assert(max > 0); /* Includes != 0 */
+
+ (void)strncpy(target, source, max - 1);
+ target[max - 1] = (char)0;
+ return TRUE;
+}
+
+
+/*
+ * TrioDuplicateMax
+ */
+TRIO_STRING_PRIVATE char *
+TrioDuplicateMax
+TRIO_ARGS2((source, size),
+ TRIO_CONST char *source,
+ size_t size)
+{
+ char *target;
+
+ assert(source);
+
+ /* Make room for string plus a terminating zero */
+ size++;
+ target = trio_create(size);
+ if (target)
+ {
+ trio_copy_max(target, size, source);
+ }
+ return target;
+}
+
+
+/**
+ Duplicate @p source.
+
+ @param source Source string.
+ @return A copy of the @p source string.
+
+ @post @p target will be zero terminated.
+*/
+TRIO_STRING_PUBLIC char *
+trio_duplicate
+TRIO_ARGS1((source),
+ TRIO_CONST char *source)
+{
+ return TrioDuplicateMax(source, trio_length(source));
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Duplicate at most @p max characters of @p source.
+
+ @param source Source string.
+ @param max Maximum number of characters to duplicate.
+ @return A copy of the @p source string.
+
+ @post @p target will be zero terminated.
+*/
+TRIO_STRING_PUBLIC char *
+trio_duplicate_max TRIO_ARGS2((source, max),
+ TRIO_CONST char *source,
+ size_t max)
+{
+ size_t length;
+
+ assert(source);
+ assert(max > 0);
+
+ length = trio_length(source);
+ if (length > max)
+ {
+ length = max;
+ }
+ return TrioDuplicateMax(source, length);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/**
+ Compare if two strings are equal.
+
+ @param first First string.
+ @param second Second string.
+ @return Boolean indicating whether the two strings are equal or not.
+
+ Case-insensitive comparison.
+*/
+TRIO_STRING_PUBLIC int
+trio_equal
+TRIO_ARGS2((first, second),
+ TRIO_CONST char *first,
+ TRIO_CONST char *second)
+{
+ assert(first);
+ assert(second);
+
+ if ((first != NULL) && (second != NULL))
+ {
+#if defined(USE_STRCASECMP)
+ return (0 == strcasecmp(first, second));
+#else
+ while ((*first != NIL) && (*second != NIL))
+ {
+ if (trio_to_upper(*first) != trio_to_upper(*second))
+ {
+ break;
+ }
+ first++;
+ second++;
+ }
+ return ((*first == NIL) && (*second == NIL));
+#endif
+ }
+ return FALSE;
+}
+
+
+/**
+ Compare if two strings are equal.
+
+ @param first First string.
+ @param second Second string.
+ @return Boolean indicating whether the two strings are equal or not.
+
+ Case-sensitive comparison.
+*/
+TRIO_STRING_PUBLIC int
+trio_equal_case
+TRIO_ARGS2((first, second),
+ TRIO_CONST char *first,
+ TRIO_CONST char *second)
+{
+ assert(first);
+ assert(second);
+
+ if ((first != NULL) && (second != NULL))
+ {
+ return (0 == strcmp(first, second));
+ }
+ return FALSE;
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Compare if two strings up until the first @p max characters are equal.
+
+ @param first First string.
+ @param max Maximum number of characters to compare.
+ @param second Second string.
+ @return Boolean indicating whether the two strings are equal or not.
+
+ Case-sensitive comparison.
+*/
+TRIO_STRING_PUBLIC int
+trio_equal_case_max
+TRIO_ARGS3((first, max, second),
+ TRIO_CONST char *first,
+ size_t max,
+ TRIO_CONST char *second)
+{
+ assert(first);
+ assert(second);
+
+ if ((first != NULL) && (second != NULL))
+ {
+ return (0 == strncmp(first, second, max));
+ }
+ return FALSE;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/**
+ Compare if two strings are equal.
+
+ @param first First string.
+ @param second Second string.
+ @return Boolean indicating whether the two strings are equal or not.
+
+ Collating characters are considered equal.
+*/
+TRIO_STRING_PUBLIC int
+trio_equal_locale
+TRIO_ARGS2((first, second),
+ TRIO_CONST char *first,
+ TRIO_CONST char *second)
+{
+ assert(first);
+ assert(second);
+
+#if defined(LC_COLLATE)
+ return (strcoll(first, second) == 0);
+#else
+ return trio_equal(first, second);
+#endif
+}
+
+
+/**
+ Compare if two strings up until the first @p max characters are equal.
+
+ @param first First string.
+ @param max Maximum number of characters to compare.
+ @param second Second string.
+ @return Boolean indicating whether the two strings are equal or not.
+
+ Case-insensitive comparison.
+*/
+TRIO_STRING_PUBLIC int
+trio_equal_max
+TRIO_ARGS3((first, max, second),
+ TRIO_CONST char *first,
+ size_t max,
+ TRIO_CONST char *second)
+{
+ assert(first);
+ assert(second);
+
+ if ((first != NULL) && (second != NULL))
+ {
+#if defined(USE_STRNCASECMP)
+ return (0 == strncasecmp(first, second, max));
+#else
+ /* Not adequately tested yet */
+ size_t cnt = 0;
+ while ((*first != NIL) && (*second != NIL) && (cnt <= max))
+ {
+ if (trio_to_upper(*first) != trio_to_upper(*second))
+ {
+ break;
+ }
+ first++;
+ second++;
+ cnt++;
+ }
+ return ((cnt == max) || ((*first == NIL) && (*second == NIL)));
+#endif
+ }
+ return FALSE;
+}
+
+
+/**
+ Provide a textual description of an error code (errno).
+
+ @param error_number Error number.
+ @return Textual description of @p error_number.
+*/
+TRIO_STRING_PUBLIC TRIO_CONST char *
+trio_error
+TRIO_ARGS1((error_number),
+ int error_number)
+{
+#if defined(USE_STRERROR)
+
+ return strerror(error_number);
+
+#elif defined(USE_SYS_ERRLIST)
+
+ extern char *sys_errlist[];
+ extern int sys_nerr;
+
+ return ((error_number < 0) || (error_number >= sys_nerr))
+ ? "unknown"
+ : sys_errlist[error_number];
+
+#else
+
+ return "unknown";
+
+#endif
+}
+
+
+#if !defined(TRIO_MINIMAL) && !defined(_WIN32_WCE)
+/**
+ Format the date/time according to @p format.
+
+ @param target Target string.
+ @param max Maximum number of characters to format.
+ @param format Formatting string.
+ @param datetime Date/time structure.
+ @return Number of formatted characters.
+
+ The formatting string accepts the same specifiers as the standard C
+ function strftime.
+*/
+TRIO_STRING_PUBLIC size_t
+trio_format_date_max
+TRIO_ARGS4((target, max, format, datetime),
+ char *target,
+ size_t max,
+ TRIO_CONST char *format,
+ TRIO_CONST struct tm *datetime)
+{
+ assert(target);
+ assert(format);
+ assert(datetime);
+ assert(max > 0);
+
+ return strftime(target, max, format, datetime);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Calculate a hash value for a string.
+
+ @param string String to be calculated on.
+ @param type Hash function.
+ @return Calculated hash value.
+
+ @p type can be one of the following
+ @li @c TRIO_HASH_PLAIN Plain hash function.
+*/
+TRIO_STRING_PUBLIC unsigned long
+trio_hash
+TRIO_ARGS2((string, type),
+ TRIO_CONST char *string,
+ int type)
+{
+ unsigned long value = 0L;
+ char ch;
+
+ assert(string);
+
+ switch (type)
+ {
+ case TRIO_HASH_PLAIN:
+ while ( (ch = *string++) != NIL )
+ {
+ value *= 31;
+ value += (unsigned long)ch;
+ }
+ break;
+ default:
+ assert(FALSE);
+ break;
+ }
+ return value;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Find first occurrence of a character in a string.
+
+ @param string String to be searched.
+ @param character Character to be found.
+ @param A pointer to the found character, or NULL if character was not found.
+ */
+TRIO_STRING_PUBLIC char *
+trio_index
+TRIO_ARGS2((string, character),
+ TRIO_CONST char *string,
+ int character)
+{
+ assert(string);
+
+ return strchr(string, character);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Find last occurrence of a character in a string.
+
+ @param string String to be searched.
+ @param character Character to be found.
+ @param A pointer to the found character, or NULL if character was not found.
+ */
+TRIO_STRING_PUBLIC char *
+trio_index_last
+TRIO_ARGS2((string, character),
+ TRIO_CONST char *string,
+ int character)
+{
+ assert(string);
+
+ return strchr(string, character);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Convert the alphabetic letters in the string to lower-case.
+
+ @param target String to be converted.
+ @return Number of processed characters (converted or not).
+*/
+TRIO_STRING_PUBLIC int
+trio_lower
+TRIO_ARGS1((target),
+ char *target)
+{
+ assert(target);
+
+ return trio_span_function(target, target, trio_to_lower);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Compare two strings using wildcards.
+
+ @param string String to be searched.
+ @param pattern Pattern, including wildcards, to search for.
+ @return Boolean value indicating success or failure.
+
+ Case-insensitive comparison.
+
+ The following wildcards can be used
+ @li @c * Match any number of characters.
+ @li @c ? Match a single character.
+*/
+TRIO_STRING_PUBLIC int
+trio_match
+TRIO_ARGS2((string, pattern),
+ TRIO_CONST char *string,
+ TRIO_CONST char *pattern)
+{
+ assert(string);
+ assert(pattern);
+
+ for (; ('*' != *pattern); ++pattern, ++string)
+ {
+ if (NIL == *string)
+ {
+ return (NIL == *pattern);
+ }
+ if ((trio_to_upper((int)*string) != trio_to_upper((int)*pattern))
+ && ('?' != *pattern))
+ {
+ return FALSE;
+ }
+ }
+ /* two-line patch to prevent *too* much recursiveness: */
+ while ('*' == pattern[1])
+ pattern++;
+
+ do
+ {
+ if ( trio_match(string, &pattern[1]) )
+ {
+ return TRUE;
+ }
+ }
+ while (*string++);
+
+ return FALSE;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Compare two strings using wildcards.
+
+ @param string String to be searched.
+ @param pattern Pattern, including wildcards, to search for.
+ @return Boolean value indicating success or failure.
+
+ Case-sensitive comparison.
+
+ The following wildcards can be used
+ @li @c * Match any number of characters.
+ @li @c ? Match a single character.
+*/
+TRIO_STRING_PUBLIC int
+trio_match_case
+TRIO_ARGS2((string, pattern),
+ TRIO_CONST char *string,
+ TRIO_CONST char *pattern)
+{
+ assert(string);
+ assert(pattern);
+
+ for (; ('*' != *pattern); ++pattern, ++string)
+ {
+ if (NIL == *string)
+ {
+ return (NIL == *pattern);
+ }
+ if ((*string != *pattern)
+ && ('?' != *pattern))
+ {
+ return FALSE;
+ }
+ }
+ /* two-line patch to prevent *too* much recursiveness: */
+ while ('*' == pattern[1])
+ pattern++;
+
+ do
+ {
+ if ( trio_match_case(string, &pattern[1]) )
+ {
+ return TRUE;
+ }
+ }
+ while (*string++);
+
+ return FALSE;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Execute a function on each character in string.
+
+ @param target Target string.
+ @param source Source string.
+ @param Function Function to be executed.
+ @return Number of processed characters.
+*/
+TRIO_STRING_PUBLIC size_t
+trio_span_function
+TRIO_ARGS3((target, source, Function),
+ char *target,
+ TRIO_CONST char *source,
+ int (*Function) TRIO_PROTO((int)))
+{
+ size_t count = 0;
+
+ assert(target);
+ assert(source);
+ assert(Function);
+
+ while (*source != NIL)
+ {
+ *target++ = Function(*source++);
+ count++;
+ }
+ return count;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Search for a substring in a string.
+
+ @param string String to be searched.
+ @param substring String to be found.
+ @return Pointer to first occurrence of @p substring in @p string, or NULL
+ if no match was found.
+*/
+TRIO_STRING_PUBLIC char *
+trio_substring
+TRIO_ARGS2((string, substring),
+ TRIO_CONST char *string,
+ TRIO_CONST char *substring)
+{
+ assert(string);
+ assert(substring);
+
+ return strstr(string, substring);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Search for a substring in the first @p max characters of a string.
+
+ @param string String to be searched.
+ @param max Maximum characters to be searched.
+ @param substring String to be found.
+ @return Pointer to first occurrence of @p substring in @p string, or NULL
+ if no match was found.
+*/
+TRIO_STRING_PUBLIC char *
+trio_substring_max
+TRIO_ARGS3((string, max, substring),
+ TRIO_CONST char *string,
+ size_t max,
+ TRIO_CONST char *substring)
+{
+ size_t count;
+ size_t size;
+ char *result = NULL;
+
+ assert(string);
+ assert(substring);
+
+ size = trio_length(substring);
+ if (size <= max)
+ {
+ for (count = 0; count <= max - size; count++)
+ {
+ if (trio_equal_max(substring, size, &string[count]))
+ {
+ result = (char *)&string[count];
+ break;
+ }
+ }
+ }
+ return result;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Tokenize string.
+
+ @param string String to be tokenized.
+ @param tokens String containing list of delimiting characters.
+ @return Start of new token.
+
+ @warning @p string will be destroyed.
+*/
+TRIO_STRING_PUBLIC char *
+trio_tokenize
+TRIO_ARGS2((string, delimiters),
+ char *string,
+ TRIO_CONST char *delimiters)
+{
+ assert(delimiters);
+
+ return strtok(string, delimiters);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/**
+ Convert string to floating-point number.
+
+ @param source String to be converted.
+ @param endp Pointer to end of the converted string.
+ @return A floating-point number.
+
+ The following Extended Backus-Naur form is used
+ @verbatim
+ double ::= [ <sign> ]
+ ( <number> |
+ <number> <decimal_point> <number> |
+ <decimal_point> <number> )
+ [ <exponential> [ <sign> ] <number> ]
+ number ::= 1*( <digit> )
+ digit ::= ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' )
+ exponential ::= ( 'e' | 'E' )
+ sign ::= ( '-' | '+' )
+ decimal_point ::= '.'
+ @endverbatim
+*/
+/* FIXME: Add EBNF for hex-floats */
+TRIO_STRING_PUBLIC trio_long_double_t
+trio_to_long_double
+TRIO_ARGS2((source, endp),
+ TRIO_CONST char *source,
+ char **endp)
+{
+#if defined(USE_STRTOLD)
+ return strtold(source, endp);
+#else
+ int isNegative = FALSE;
+ int isExponentNegative = FALSE;
+ trio_long_double_t integer = 0.0;
+ trio_long_double_t fraction = 0.0;
+ unsigned long exponent = 0;
+ trio_long_double_t base;
+ trio_long_double_t fracdiv = 1.0;
+ trio_long_double_t value = 0.0;
+
+ /* First try hex-floats */
+ if ((source[0] == '0') && ((source[1] == 'x') || (source[1] == 'X')))
+ {
+ base = 16.0;
+ source += 2;
+ while (isxdigit((int)*source))
+ {
+ integer *= base;
+ integer += (isdigit((int)*source)
+ ? (*source - '0')
+ : 10 + (trio_to_upper((int)*source) - 'A'));
+ source++;
+ }
+ if (*source == '.')
+ {
+ source++;
+ while (isxdigit((int)*source))
+ {
+ fracdiv /= base;
+ fraction += fracdiv * (isdigit((int)*source)
+ ? (*source - '0')
+ : 10 + (trio_to_upper((int)*source) - 'A'));
+ source++;
+ }
+ if ((*source == 'p') || (*source == 'P'))
+ {
+ source++;
+ if ((*source == '+') || (*source == '-'))
+ {
+ isExponentNegative = (*source == '-');
+ source++;
+ }
+ while (isdigit((int)*source))
+ {
+ exponent *= 10;
+ exponent += (*source - '0');
+ source++;
+ }
+ }
+ }
+ /* For later use with exponent */
+ base = 2.0;
+ }
+ else /* Then try normal decimal floats */
+ {
+ base = 10.0;
+ isNegative = (*source == '-');
+ /* Skip sign */
+ if ((*source == '+') || (*source == '-'))
+ source++;
+
+ /* Integer part */
+ while (isdigit((int)*source))
+ {
+ integer *= base;
+ integer += (*source - '0');
+ source++;
+ }
+
+ if (*source == '.')
+ {
+ source++; /* skip decimal point */
+ while (isdigit((int)*source))
+ {
+ fracdiv /= base;
+ fraction += (*source - '0') * fracdiv;
+ source++;
+ }
+ }
+ if ((*source == 'e')
+ || (*source == 'E')
+#if TRIO_MICROSOFT
+ || (*source == 'd')
+ || (*source == 'D')
+#endif
+ )
+ {
+ source++; /* Skip exponential indicator */
+ isExponentNegative = (*source == '-');
+ if ((*source == '+') || (*source == '-'))
+ source++;
+ while (isdigit((int)*source))
+ {
+ exponent *= (int)base;
+ exponent += (*source - '0');
+ source++;
+ }
+ }
+ }
+
+ value = integer + fraction;
+ if (exponent != 0)
+ {
+ if (isExponentNegative)
+ value /= pow(base, (double)exponent);
+ else
+ value *= pow(base, (double)exponent);
+ }
+ if (isNegative)
+ value = -value;
+
+ if (endp)
+ *endp = (char *)source;
+ return value;
+#endif
+}
+
+
+/**
+ Convert string to floating-point number.
+
+ @param source String to be converted.
+ @param endp Pointer to end of the converted string.
+ @return A floating-point number.
+
+ See @ref trio_to_long_double.
+*/
+TRIO_STRING_PUBLIC double
+trio_to_double
+TRIO_ARGS2((source, endp),
+ TRIO_CONST char *source,
+ char **endp)
+{
+#if defined(USE_STRTOD)
+ return strtod(source, endp);
+#else
+ return (double)trio_to_long_double(source, endp);
+#endif
+}
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Convert string to floating-point number.
+
+ @param source String to be converted.
+ @param endp Pointer to end of the converted string.
+ @return A floating-point number.
+
+ See @ref trio_to_long_double.
+*/
+TRIO_STRING_PUBLIC float
+trio_to_float
+TRIO_ARGS2((source, endp),
+ TRIO_CONST char *source,
+ char **endp)
+{
+#if defined(USE_STRTOF)
+ return strtof(source, endp);
+#else
+ return (float)trio_to_long_double(source, endp);
+#endif
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/**
+ Convert string to signed integer.
+
+ @param string String to be converted.
+ @param endp Pointer to end of converted string.
+ @param base Radix number of number.
+*/
+TRIO_STRING_PUBLIC long
+trio_to_long
+TRIO_ARGS3((string, endp, base),
+ TRIO_CONST char *string,
+ char **endp,
+ int base)
+{
+ assert(string);
+ assert((base >= 2) && (base <= 36));
+
+ return strtol(string, endp, base);
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Convert one alphabetic letter to lower-case.
+
+ @param source The letter to be converted.
+ @return The converted letter.
+*/
+TRIO_STRING_PUBLIC int
+trio_to_lower
+TRIO_ARGS1((source),
+ int source)
+{
+#if defined(USE_TOLOWER)
+
+ return tolower(source);
+
+#else
+
+ /* Does not handle locales or non-contiguous alphabetic characters */
+ return ((source >= (int)'A') && (source <= (int)'Z'))
+ ? source - 'A' + 'a'
+ : source;
+
+#endif
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Convert string to unsigned integer.
+
+ @param string String to be converted.
+ @param endp Pointer to end of converted string.
+ @param base Radix number of number.
+*/
+TRIO_STRING_PUBLIC unsigned long
+trio_to_unsigned_long
+TRIO_ARGS3((string, endp, base),
+ TRIO_CONST char *string,
+ char **endp,
+ int base)
+{
+ assert(string);
+ assert((base >= 2) && (base <= 36));
+
+ return strtoul(string, endp, base);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/**
+ Convert one alphabetic letter to upper-case.
+
+ @param source The letter to be converted.
+ @return The converted letter.
+*/
+TRIO_STRING_PUBLIC int
+trio_to_upper
+TRIO_ARGS1((source),
+ int source)
+{
+#if defined(USE_TOUPPER)
+
+ return toupper(source);
+
+#else
+
+ /* Does not handle locales or non-contiguous alphabetic characters */
+ return ((source >= (int)'a') && (source <= (int)'z'))
+ ? source - 'a' + 'A'
+ : source;
+
+#endif
+}
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Convert the alphabetic letters in the string to upper-case.
+
+ @param target The string to be converted.
+ @return The number of processed characters (converted or not).
+*/
+TRIO_STRING_PUBLIC int
+trio_upper
+TRIO_ARGS1((target),
+ char *target)
+{
+ assert(target);
+
+ return trio_span_function(target, target, trio_to_upper);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/** @} End of StaticStrings */
+
+
+/*************************************************************************
+ * Dynamic String Functions
+ */
+
+#if defined(TRIO_DOCUMENTATION)
+# include "doc/doc_dynamic.h"
+#endif
+/** @addtogroup DynamicStrings
+ @{
+*/
+
+/*
+ * TrioStringAlloc
+ */
+TRIO_STRING_PRIVATE trio_string_t *
+TrioStringAlloc(TRIO_NOARGS)
+{
+ trio_string_t *self;
+
+ self = (trio_string_t *)TRIO_MALLOC(sizeof(trio_string_t));
+ if (self)
+ {
+ self->content = NULL;
+ self->length = 0;
+ self->allocated = 0;
+ }
+ return self;
+}
+
+
+/*
+ * TrioStringGrow
+ *
+ * The size of the string will be increased by 'delta' characters. If
+ * 'delta' is zero, the size will be doubled.
+ */
+TRIO_STRING_PRIVATE BOOLEAN_T
+TrioStringGrow
+TRIO_ARGS2((self, delta),
+ trio_string_t *self,
+ size_t delta)
+{
+ BOOLEAN_T status = FALSE;
+ char *new_content;
+ size_t new_size;
+
+ new_size = (delta == 0)
+ ? ( (self->allocated == 0) ? 1 : self->allocated * 2 )
+ : self->allocated + delta;
+
+ new_content = (char *)TRIO_REALLOC(self->content, new_size);
+ if (new_content)
+ {
+ self->content = new_content;
+ self->allocated = new_size;
+ status = TRUE;
+ }
+ return status;
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * TrioStringGrowTo
+ *
+ * The size of the string will be increased to 'length' plus one characters.
+ * If 'length' is less than the original size, the original size will be
+ * used (that is, the size of the string is never decreased).
+ */
+TRIO_STRING_PRIVATE BOOLEAN_T
+TrioStringGrowTo
+TRIO_ARGS2((self, length),
+ trio_string_t *self,
+ size_t length)
+{
+ length++; /* Room for terminating zero */
+ return (self->allocated < length)
+ ? TrioStringGrow(self, length - self->allocated)
+ : TRUE;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Create a new dynamic string.
+
+ @param initial_size Initial size of the buffer.
+ @return Newly allocated dynamic string, or NULL if memory allocation failed.
+*/
+TRIO_STRING_PUBLIC trio_string_t *
+trio_string_create
+TRIO_ARGS1((initial_size),
+ int initial_size)
+{
+ trio_string_t *self;
+
+ self = TrioStringAlloc();
+ if (self)
+ {
+ if (TrioStringGrow(self,
+ (size_t)((initial_size > 0) ? initial_size : 1)))
+ {
+ self->content[0] = (char)0;
+ self->allocated = initial_size;
+ }
+ else
+ {
+ trio_string_destroy(self);
+ self = NULL;
+ }
+ }
+ return self;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/**
+ Deallocate the dynamic string and its contents.
+
+ @param self Dynamic string
+*/
+TRIO_STRING_PUBLIC void
+trio_string_destroy
+TRIO_ARGS1((self),
+ trio_string_t *self)
+{
+ assert(self);
+
+ if (self)
+ {
+ trio_destroy(self->content);
+ TRIO_FREE(self);
+ }
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Get a pointer to the content.
+
+ @param self Dynamic string.
+ @param offset Offset into content.
+ @return Pointer to the content.
+
+ @p Offset can be zero, positive, or negative. If @p offset is zero,
+ then the start of the content will be returned. If @p offset is positive,
+ then a pointer to @p offset number of characters from the beginning of the
+ content is returned. If @p offset is negative, then a pointer to @p offset
+ number of characters from the ending of the string, starting at the
+ terminating zero, is returned.
+*/
+TRIO_STRING_PUBLIC char *
+trio_string_get
+TRIO_ARGS2((self, offset),
+ trio_string_t *self,
+ int offset)
+{
+ char *result = NULL;
+
+ assert(self);
+
+ if (self->content != NULL)
+ {
+ if (self->length == 0)
+ {
+ (void)trio_string_length(self);
+ }
+ if (offset >= 0)
+ {
+ if (offset > (int)self->length)
+ {
+ offset = self->length;
+ }
+ }
+ else
+ {
+ offset += self->length + 1;
+ if (offset < 0)
+ {
+ offset = 0;
+ }
+ }
+ result = &(self->content[offset]);
+ }
+ return result;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/**
+ Extract the content.
+
+ @param self Dynamic String
+ @return Content of dynamic string.
+
+ The content is removed from the dynamic string. This enables destruction
+ of the dynamic string without deallocation of the content.
+*/
+TRIO_STRING_PUBLIC char *
+trio_string_extract
+TRIO_ARGS1((self),
+ trio_string_t *self)
+{
+ char *result;
+
+ assert(self);
+
+ result = self->content;
+ /* FIXME: Allocate new empty buffer? */
+ self->content = NULL;
+ self->length = self->allocated = 0;
+ return result;
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Set the content of the dynamic string.
+
+ @param self Dynamic String
+ @param buffer The new content.
+
+ Sets the content of the dynamic string to a copy @p buffer.
+ An existing content will be deallocated first, if necessary.
+
+ @remark
+ This function will make a copy of @p buffer.
+ You are responsible for deallocating @p buffer yourself.
+*/
+TRIO_STRING_PUBLIC void
+trio_xstring_set
+TRIO_ARGS2((self, buffer),
+ trio_string_t *self,
+ char *buffer)
+{
+ assert(self);
+
+ trio_destroy(self->content);
+ self->content = trio_duplicate(buffer);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/*
+ * trio_string_size
+ */
+TRIO_STRING_PUBLIC int
+trio_string_size
+TRIO_ARGS1((self),
+ trio_string_t *self)
+{
+ assert(self);
+
+ return self->allocated;
+}
+
+
+/*
+ * trio_string_terminate
+ */
+TRIO_STRING_PUBLIC void
+trio_string_terminate
+TRIO_ARGS1((self),
+ trio_string_t *self)
+{
+ trio_xstring_append_char(self, 0);
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Append the second string to the first.
+
+ @param self Dynamic string to be modified.
+ @param other Dynamic string to copy from.
+ @return Boolean value indicating success or failure.
+*/
+TRIO_STRING_PUBLIC int
+trio_string_append
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ trio_string_t *other)
+{
+ size_t length;
+
+ assert(self);
+ assert(other);
+
+ length = self->length + other->length;
+ if (!TrioStringGrowTo(self, length))
+ goto error;
+ trio_copy(&self->content[self->length], other->content);
+ self->length = length;
+ return TRUE;
+
+ error:
+ return FALSE;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_append
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_append
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ TRIO_CONST char *other)
+{
+ size_t length;
+
+ assert(self);
+ assert(other);
+
+ length = self->length + trio_length(other);
+ if (!TrioStringGrowTo(self, length))
+ goto error;
+ trio_copy(&self->content[self->length], other);
+ self->length = length;
+ return TRUE;
+
+ error:
+ return FALSE;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/*
+ * trio_xstring_append_char
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_append_char
+TRIO_ARGS2((self, character),
+ trio_string_t *self,
+ char character)
+{
+ assert(self);
+
+ if ((int)self->length >= trio_string_size(self))
+ {
+ if (!TrioStringGrow(self, 0))
+ goto error;
+ }
+ self->content[self->length] = character;
+ self->length++;
+ return TRUE;
+
+ error:
+ return FALSE;
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/**
+ Search for the first occurrence of second parameter in the first.
+
+ @param self Dynamic string to be modified.
+ @param other Dynamic string to copy from.
+ @return Boolean value indicating success or failure.
+*/
+TRIO_STRING_PUBLIC int
+trio_string_contains
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_contains(self->content, other->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_contains
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_contains
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_contains(self->content, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_copy
+ */
+TRIO_STRING_PUBLIC int
+trio_string_copy
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ self->length = 0;
+ return trio_string_append(self, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_copy
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_copy
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ self->length = 0;
+ return trio_xstring_append(self, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_duplicate
+ */
+TRIO_STRING_PUBLIC trio_string_t *
+trio_string_duplicate
+TRIO_ARGS1((other),
+ trio_string_t *other)
+{
+ trio_string_t *self;
+
+ assert(other);
+
+ self = TrioStringAlloc();
+ if (self)
+ {
+ self->content = TrioDuplicateMax(other->content, other->length);
+ if (self->content)
+ {
+ self->length = other->length;
+ self->allocated = self->length + 1;
+ }
+ else
+ {
+ self->length = self->allocated = 0;
+ }
+ }
+ return self;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+/*
+ * trio_xstring_duplicate
+ */
+TRIO_STRING_PUBLIC trio_string_t *
+trio_xstring_duplicate
+TRIO_ARGS1((other),
+ TRIO_CONST char *other)
+{
+ trio_string_t *self;
+
+ assert(other);
+
+ self = TrioStringAlloc();
+ if (self)
+ {
+ self->content = TrioDuplicateMax(other, trio_length(other));
+ if (self->content)
+ {
+ self->length = trio_length(self->content);
+ self->allocated = self->length + 1;
+ }
+ else
+ {
+ self->length = self->allocated = 0;
+ }
+ }
+ return self;
+}
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_equal
+ */
+TRIO_STRING_PUBLIC int
+trio_string_equal
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_equal(self->content, other->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_equal
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_equal
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_equal(self->content, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_equal_max
+ */
+TRIO_STRING_PUBLIC int
+trio_string_equal_max
+TRIO_ARGS3((self, max, other),
+ trio_string_t *self,
+ size_t max,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_equal_max(self->content, max, other->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_equal_max
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_equal_max
+TRIO_ARGS3((self, max, other),
+ trio_string_t *self,
+ size_t max,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_equal_max(self->content, max, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_equal_case
+ */
+TRIO_STRING_PUBLIC int
+trio_string_equal_case
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_equal_case(self->content, other->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_equal_case
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_equal_case
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_equal_case(self->content, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_equal_case_max
+ */
+TRIO_STRING_PUBLIC int
+trio_string_equal_case_max
+TRIO_ARGS3((self, max, other),
+ trio_string_t *self,
+ size_t max,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_equal_case_max(self->content, max, other->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_equal_case_max
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_equal_case_max
+TRIO_ARGS3((self, max, other),
+ trio_string_t *self,
+ size_t max,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_equal_case_max(self->content, max, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL) && !defined(_WIN32_WCE)
+/*
+ * trio_string_format_data_max
+ */
+TRIO_STRING_PUBLIC size_t
+trio_string_format_date_max
+TRIO_ARGS4((self, max, format, datetime),
+ trio_string_t *self,
+ size_t max,
+ TRIO_CONST char *format,
+ TRIO_CONST struct tm *datetime)
+{
+ assert(self);
+
+ return trio_format_date_max(self->content, max, format, datetime);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_index
+ */
+TRIO_STRING_PUBLIC char *
+trio_string_index
+TRIO_ARGS2((self, character),
+ trio_string_t *self,
+ int character)
+{
+ assert(self);
+
+ return trio_index(self->content, character);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_index_last
+ */
+TRIO_STRING_PUBLIC char *
+trio_string_index_last
+TRIO_ARGS2((self, character),
+ trio_string_t *self,
+ int character)
+{
+ assert(self);
+
+ return trio_index_last(self->content, character);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_length
+ */
+TRIO_STRING_PUBLIC int
+trio_string_length
+TRIO_ARGS1((self),
+ trio_string_t *self)
+{
+ assert(self);
+
+ if (self->length == 0)
+ {
+ self->length = trio_length(self->content);
+ }
+ return self->length;
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_lower
+ */
+TRIO_STRING_PUBLIC int
+trio_string_lower
+TRIO_ARGS1((self),
+ trio_string_t *self)
+{
+ assert(self);
+
+ return trio_lower(self->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_match
+ */
+TRIO_STRING_PUBLIC int
+trio_string_match
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_match(self->content, other->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_match
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_match
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_match(self->content, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_match_case
+ */
+TRIO_STRING_PUBLIC int
+trio_string_match_case
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_match_case(self->content, other->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_match_case
+ */
+TRIO_STRING_PUBLIC int
+trio_xstring_match_case
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_match_case(self->content, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_substring
+ */
+TRIO_STRING_PUBLIC char *
+trio_string_substring
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ trio_string_t *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_substring(self->content, other->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_xstring_substring
+ */
+TRIO_STRING_PUBLIC char *
+trio_xstring_substring
+TRIO_ARGS2((self, other),
+ trio_string_t *self,
+ TRIO_CONST char *other)
+{
+ assert(self);
+ assert(other);
+
+ return trio_substring(self->content, other);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+
+#if !defined(TRIO_MINIMAL)
+/*
+ * trio_string_upper
+ */
+TRIO_STRING_PUBLIC int
+trio_string_upper
+TRIO_ARGS1((self),
+ trio_string_t *self)
+{
+ assert(self);
+
+ return trio_upper(self->content);
+}
+#endif /* !defined(TRIO_MINIMAL) */
+
+/** @} End of DynamicStrings */
diff --git a/triostr.h b/triostr.h
new file mode 100644
index 0000000..6b8d30f
--- /dev/null
+++ b/triostr.h
@@ -0,0 +1,144 @@
+/*************************************************************************
+ *
+ * $Id: triostr.h 3600 2007-04-17 12:44:58Z veillard $
+ *
+ * Copyright (C) 2001 Bjorn Reese and Daniel Stenberg.
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
+ * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
+ *
+ ************************************************************************/
+
+#ifndef TRIO_TRIOSTR_H
+#define TRIO_TRIOSTR_H
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include "triodef.h"
+#include "triop.h"
+
+enum {
+ TRIO_HASH_NONE = 0,
+ TRIO_HASH_PLAIN,
+ TRIO_HASH_TWOSIGNED
+};
+
+#if !defined(TRIO_STRING_PUBLIC)
+# if !defined(TRIO_PUBLIC)
+# define TRIO_PUBLIC
+# endif
+# define TRIO_STRING_PUBLIC TRIO_PUBLIC
+#endif
+
+/*************************************************************************
+ * String functions
+ */
+
+TRIO_STRING_PUBLIC int trio_copy_max TRIO_PROTO((char *target, size_t max, const char *source));
+TRIO_STRING_PUBLIC char *trio_create TRIO_PROTO((size_t size));
+TRIO_STRING_PUBLIC void trio_destroy TRIO_PROTO((char *string));
+TRIO_STRING_PUBLIC char *trio_duplicate TRIO_PROTO((const char *source));
+TRIO_STRING_PUBLIC int trio_equal TRIO_PROTO((const char *first, const char *second));
+TRIO_STRING_PUBLIC int trio_equal_case TRIO_PROTO((const char *first, const char *second));
+TRIO_STRING_PUBLIC int trio_equal_locale TRIO_PROTO((const char *first, const char *second));
+TRIO_STRING_PUBLIC int trio_equal_max TRIO_PROTO((const char *first, size_t max, const char *second));
+TRIO_STRING_PUBLIC TRIO_CONST char *trio_error TRIO_PROTO((int));
+TRIO_STRING_PUBLIC size_t trio_length TRIO_PROTO((const char *string));
+TRIO_STRING_PUBLIC double trio_to_double TRIO_PROTO((const char *source, char **endp));
+TRIO_STRING_PUBLIC long trio_to_long TRIO_PROTO((const char *source, char **endp, int base));
+TRIO_STRING_PUBLIC trio_long_double_t trio_to_long_double TRIO_PROTO((const char *source, char **endp));
+TRIO_STRING_PUBLIC int trio_to_upper TRIO_PROTO((int source));
+
+#if !defined(TRIO_MINIMAL)
+
+TRIO_STRING_PUBLIC int trio_append TRIO_PROTO((char *target, const char *source));
+TRIO_STRING_PUBLIC int trio_append_max TRIO_PROTO((char *target, size_t max, const char *source));
+TRIO_STRING_PUBLIC int trio_contains TRIO_PROTO((const char *string, const char *substring));
+TRIO_STRING_PUBLIC int trio_copy TRIO_PROTO((char *target, const char *source));
+TRIO_STRING_PUBLIC char *trio_duplicate_max TRIO_PROTO((const char *source, size_t max));
+TRIO_STRING_PUBLIC int trio_equal_case_max TRIO_PROTO((const char *first, size_t max, const char *second));
+#if !defined(_WIN32_WCE)
+TRIO_STRING_PUBLIC size_t trio_format_date_max TRIO_PROTO((char *target, size_t max, const char *format, const struct tm *datetime));
+#endif
+TRIO_STRING_PUBLIC unsigned long trio_hash TRIO_PROTO((const char *string, int type));
+TRIO_STRING_PUBLIC char *trio_index TRIO_PROTO((const char *string, int character));
+TRIO_STRING_PUBLIC char *trio_index_last TRIO_PROTO((const char *string, int character));
+TRIO_STRING_PUBLIC int trio_lower TRIO_PROTO((char *target));
+TRIO_STRING_PUBLIC int trio_match TRIO_PROTO((const char *string, const char *pattern));
+TRIO_STRING_PUBLIC int trio_match_case TRIO_PROTO((const char *string, const char *pattern));
+TRIO_STRING_PUBLIC size_t trio_span_function TRIO_PROTO((char *target, const char *source, int (*Function) TRIO_PROTO((int))));
+TRIO_STRING_PUBLIC char *trio_substring TRIO_PROTO((const char *string, const char *substring));
+TRIO_STRING_PUBLIC char *trio_substring_max TRIO_PROTO((const char *string, size_t max, const char *substring));
+TRIO_STRING_PUBLIC float trio_to_float TRIO_PROTO((const char *source, char **endp));
+TRIO_STRING_PUBLIC int trio_to_lower TRIO_PROTO((int source));
+TRIO_STRING_PUBLIC unsigned long trio_to_unsigned_long TRIO_PROTO((const char *source, char **endp, int base));
+TRIO_STRING_PUBLIC char *trio_tokenize TRIO_PROTO((char *string, const char *delimiters));
+TRIO_STRING_PUBLIC int trio_upper TRIO_PROTO((char *target));
+
+#endif /* !defined(TRIO_MINIMAL) */
+
+/*************************************************************************
+ * Dynamic string functions
+ */
+
+/*
+ * Opaque type for dynamic strings
+ */
+
+typedef struct _trio_string_t trio_string_t;
+
+TRIO_STRING_PUBLIC void trio_string_destroy TRIO_PROTO((trio_string_t *self));
+TRIO_STRING_PUBLIC char *trio_string_extract TRIO_PROTO((trio_string_t *self));
+TRIO_STRING_PUBLIC int trio_string_size TRIO_PROTO((trio_string_t *self));
+TRIO_STRING_PUBLIC void trio_string_terminate TRIO_PROTO((trio_string_t *self));
+TRIO_STRING_PUBLIC int trio_xstring_append_char TRIO_PROTO((trio_string_t *self, char character));
+TRIO_STRING_PUBLIC trio_string_t *trio_xstring_duplicate TRIO_PROTO((const char *other));
+
+#if !defined(TRIO_MINIMAL)
+
+TRIO_STRING_PUBLIC trio_string_t *trio_string_create TRIO_PROTO((int initial_size));
+TRIO_STRING_PUBLIC char *trio_string_get TRIO_PROTO((trio_string_t *self, int offset));
+TRIO_STRING_PUBLIC void trio_xstring_set TRIO_PROTO((trio_string_t *self, char *buffer));
+
+TRIO_STRING_PUBLIC int trio_string_append TRIO_PROTO((trio_string_t *self, trio_string_t *other));
+TRIO_STRING_PUBLIC int trio_string_contains TRIO_PROTO((trio_string_t *self, trio_string_t *other));
+TRIO_STRING_PUBLIC int trio_string_copy TRIO_PROTO((trio_string_t *self, trio_string_t *other));
+TRIO_STRING_PUBLIC trio_string_t *trio_string_duplicate TRIO_PROTO((trio_string_t *other));
+TRIO_STRING_PUBLIC int trio_string_equal TRIO_PROTO((trio_string_t *self, trio_string_t *other));
+TRIO_STRING_PUBLIC int trio_string_equal_max TRIO_PROTO((trio_string_t *self, size_t max, trio_string_t *second));
+TRIO_STRING_PUBLIC int trio_string_equal_case TRIO_PROTO((trio_string_t *self, trio_string_t *other));
+TRIO_STRING_PUBLIC int trio_string_equal_case_max TRIO_PROTO((trio_string_t *self, size_t max, trio_string_t *other));
+#if !defined(_WIN32_WCE)
+TRIO_STRING_PUBLIC size_t trio_string_format_date_max TRIO_PROTO((trio_string_t *self, size_t max, const char *format, const struct tm *datetime));
+#endif
+TRIO_STRING_PUBLIC char *trio_string_index TRIO_PROTO((trio_string_t *self, int character));
+TRIO_STRING_PUBLIC char *trio_string_index_last TRIO_PROTO((trio_string_t *self, int character));
+TRIO_STRING_PUBLIC int trio_string_length TRIO_PROTO((trio_string_t *self));
+TRIO_STRING_PUBLIC int trio_string_lower TRIO_PROTO((trio_string_t *self));
+TRIO_STRING_PUBLIC int trio_string_match TRIO_PROTO((trio_string_t *self, trio_string_t *other));
+TRIO_STRING_PUBLIC int trio_string_match_case TRIO_PROTO((trio_string_t *self, trio_string_t *other));
+TRIO_STRING_PUBLIC char *trio_string_substring TRIO_PROTO((trio_string_t *self, trio_string_t *other));
+TRIO_STRING_PUBLIC int trio_string_upper TRIO_PROTO((trio_string_t *self));
+
+TRIO_STRING_PUBLIC int trio_xstring_append TRIO_PROTO((trio_string_t *self, const char *other));
+TRIO_STRING_PUBLIC int trio_xstring_contains TRIO_PROTO((trio_string_t *self, const char *other));
+TRIO_STRING_PUBLIC int trio_xstring_copy TRIO_PROTO((trio_string_t *self, const char *other));
+TRIO_STRING_PUBLIC int trio_xstring_equal TRIO_PROTO((trio_string_t *self, const char *other));
+TRIO_STRING_PUBLIC int trio_xstring_equal_max TRIO_PROTO((trio_string_t *self, size_t max, const char *other));
+TRIO_STRING_PUBLIC int trio_xstring_equal_case TRIO_PROTO((trio_string_t *self, const char *other));
+TRIO_STRING_PUBLIC int trio_xstring_equal_case_max TRIO_PROTO((trio_string_t *self, size_t max, const char *other));
+TRIO_STRING_PUBLIC int trio_xstring_match TRIO_PROTO((trio_string_t *self, const char *other));
+TRIO_STRING_PUBLIC int trio_xstring_match_case TRIO_PROTO((trio_string_t *self, const char *other));
+TRIO_STRING_PUBLIC char *trio_xstring_substring TRIO_PROTO((trio_string_t *self, const char *other));
+
+#endif /* !defined(TRIO_MINIMAL) */
+
+#endif /* TRIO_TRIOSTR_H */