blob: c8b9c84ebaa25c096a2a9eac54c75931718d82b2 [file] [log] [blame]
Daniel Veillard92ad2102001-03-27 12:47:33 +00001/*************************************************************************
2 *
3 * $Id$
4 *
5 * Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
12 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
13 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
14 * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
15 *
16 *************************************************************************
17 *
Bjorn Reese70a9da52001-04-21 16:57:29 +000018 * A note to trio contributors:
19 *
Daniel Veillard92ad2102001-03-27 12:47:33 +000020 * Avoid heap allocation at all costs to ensure that the trio functions
21 * are async-safe. The exceptions are the printf/fprintf functions, which
22 * uses fputc, and the asprintf functions and the <alloc> modifier, which
23 * by design are required to allocate form the heap.
24 *
25 ************************************************************************/
26
Daniel Veillard92ad2102001-03-27 12:47:33 +000027/*
Bjorn Reese70a9da52001-04-21 16:57:29 +000028 * TODO:
Daniel Veillard92ad2102001-03-27 12:47:33 +000029 * - Scan is probably too permissive about its modifiers.
Daniel Veillard92ad2102001-03-27 12:47:33 +000030 * - C escapes in %#[] ?
31 * - Multibyte characters (done for format parsing, except scan groups)
Daniel Veillard92ad2102001-03-27 12:47:33 +000032 * - Complex numbers? (C99 _Complex)
33 * - Boolean values? (C99 _Bool)
Daniel Veillarda48ed3d2003-04-03 15:28:28 +000034 * - C99 NaN(n-char-sequence) missing. The n-char-sequence can be used
35 * to print the mantissa, e.g. NaN(0xc000000000000000)
Daniel Veillard92ad2102001-03-27 12:47:33 +000036 * - Should we support the GNU %a alloc modifier? GNU has an ugly hack
37 * for %a, because C99 used %a for other purposes. If specified as
38 * %as or %a[ it is interpreted as the alloc modifier, otherwise as
39 * the C99 hex-float. This means that you cannot scan %as as a hex-float
40 * immediately followed by an 's'.
Bjorn Reese906ec8a2001-06-05 12:46:33 +000041 * - Scanning of collating symbols.
Daniel Veillard92ad2102001-03-27 12:47:33 +000042 */
43
Daniel Veillard92ad2102001-03-27 12:47:33 +000044/*************************************************************************
Bjorn Reese906ec8a2001-06-05 12:46:33 +000045 * Trio include files
Daniel Veillard92ad2102001-03-27 12:47:33 +000046 */
Bjorn Reese45029602001-08-21 09:23:53 +000047#include "triodef.h"
Daniel Veillard92ad2102001-03-27 12:47:33 +000048#include "trio.h"
Bjorn Reese70a9da52001-04-21 16:57:29 +000049#include "triop.h"
Bjorn Reese45029602001-08-21 09:23:53 +000050#include "trionan.h"
Daniel Veillardb7c29c32002-09-25 22:44:43 +000051#if !defined(TRIO_MINIMAL)
52# include "triostr.h"
Daniel Veillard92ad2102001-03-27 12:47:33 +000053#endif
54
Daniel Veillardb7c29c32002-09-25 22:44:43 +000055/**************************************************************************
56 *
57 * Definitions
58 *
59 *************************************************************************/
60
Daniel Veillarda48ed3d2003-04-03 15:28:28 +000061#include <math.h>
62#include <limits.h>
63#include <float.h>
64
Daniel Veillard59d3ed82007-04-17 12:44:58 +000065#if (defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) \
66 || defined(USE_MULTIBYTE) || TRIO_WIDECHAR) \
67 && !defined(_WIN32_WCE)
Bjorn Reese906ec8a2001-06-05 12:46:33 +000068# define TRIO_COMPILER_SUPPORTS_MULTIBYTE
69# if !defined(MB_LEN_MAX)
70# define MB_LEN_MAX 6
71# endif
72#endif
73
Daniel Veillarda48ed3d2003-04-03 15:28:28 +000074#if (defined(TRIO_COMPILER_MSVC) && (_MSC_VER >= 1100)) || defined(TRIO_COMPILER_BCB)
75# define TRIO_COMPILER_SUPPORTS_MSVC_INT
76#endif
77
Daniel Veillard59d3ed82007-04-17 12:44:58 +000078#if defined(_WIN32_WCE)
79#include <wincecompat.h>
80#endif
81
Bjorn Reese906ec8a2001-06-05 12:46:33 +000082/*************************************************************************
83 * Generic definitions
84 */
85
86#if !(defined(DEBUG) || defined(NDEBUG))
87# define NDEBUG
88#endif
Daniel Veillarda48ed3d2003-04-03 15:28:28 +000089
Bjorn Reese906ec8a2001-06-05 12:46:33 +000090#include <assert.h>
91#include <ctype.h>
92#if !defined(TRIO_COMPILER_SUPPORTS_C99)
93# define isblank(x) (((x)==32) || ((x)==9))
94#endif
Daniel Veillardb7c29c32002-09-25 22:44:43 +000095#if defined(TRIO_COMPILER_ANCIENT)
96# include <varargs.h>
97#else
98# include <stdarg.h>
99#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000100#include <stddef.h>
Daniel Veillard59d3ed82007-04-17 12:44:58 +0000101
Jacob (Jouk) Jansen4d041a22013-12-17 15:32:57 +0800102#if defined( HAVE_ERRNO_H ) || defined( __VMS )
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000103#include <errno.h>
Daniel Veillard59d3ed82007-04-17 12:44:58 +0000104#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000105
106#ifndef NULL
107# define NULL 0
108#endif
109#define NIL ((char)0)
110#ifndef FALSE
111# define FALSE (1 == 0)
112# define TRUE (! FALSE)
113#endif
114#define BOOLEAN_T int
115
116/* mincore() can be used for debugging purposes */
117#define VALID(x) (NULL != (x))
118
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000119#if TRIO_ERRORS
120 /*
121 * Encode the error code and the position. This is decoded
122 * with TRIO_ERROR_CODE and TRIO_ERROR_POSITION.
123 */
124# define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
125#else
126# define TRIO_ERROR_RETURN(x,y) (-1)
127#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000128
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +0800129#ifndef VA_LIST_IS_ARRAY
130#define TRIO_VA_LIST_PTR va_list *
131#define TRIO_VA_LIST_ADDR(l) (&(l))
132#define TRIO_VA_LIST_DEREF(l) (*(l))
133#else
134#define TRIO_VA_LIST_PTR va_list
135#define TRIO_VA_LIST_ADDR(l) (l)
136#define TRIO_VA_LIST_DEREF(l) (l)
137#endif
138
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000139typedef unsigned long trio_flags_t;
140
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000141
142/*************************************************************************
143 * Platform specific definitions
144 */
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +0800145#if defined(TRIO_PLATFORM_UNIX) || defined(TRIO_PLATFORM_OS400)
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000146# include <unistd.h>
147# include <signal.h>
148# include <locale.h>
149# define USE_LOCALE
Bjorn Reese026d29f2002-01-19 15:40:18 +0000150#endif /* TRIO_PLATFORM_UNIX */
151#if defined(TRIO_PLATFORM_VMS)
152# include <unistd.h>
153#endif
154#if defined(TRIO_PLATFORM_WIN32)
Daniel Veillard59d3ed82007-04-17 12:44:58 +0000155# if defined(_WIN32_WCE)
156# include <wincecompat.h>
157# else
158# include <io.h>
159# define read _read
160# define write _write
161# endif
Bjorn Reese026d29f2002-01-19 15:40:18 +0000162#endif /* TRIO_PLATFORM_WIN32 */
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000163
164#if TRIO_WIDECHAR
165# if defined(TRIO_COMPILER_SUPPORTS_ISO94)
166# include <wchar.h>
167# include <wctype.h>
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000168typedef wchar_t trio_wchar_t;
169typedef wint_t trio_wint_t;
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000170# else
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000171typedef char trio_wchar_t;
172typedef int trio_wint_t;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000173# define WCONST(x) L ## x
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000174# define WEOF EOF
175# define iswalnum(x) isalnum(x)
176# define iswalpha(x) isalpha(x)
177# define iswblank(x) isblank(x)
178# define iswcntrl(x) iscntrl(x)
179# define iswdigit(x) isdigit(x)
180# define iswgraph(x) isgraph(x)
181# define iswlower(x) islower(x)
182# define iswprint(x) isprint(x)
183# define iswpunct(x) ispunct(x)
184# define iswspace(x) isspace(x)
185# define iswupper(x) isupper(x)
186# define iswxdigit(x) isxdigit(x)
187# endif
188#endif
189
190
191/*************************************************************************
192 * Compiler dependent definitions
193 */
194
195/* Support for long long */
196#ifndef __cplusplus
197# if !defined(USE_LONGLONG)
Bjorn Reese026d29f2002-01-19 15:40:18 +0000198# if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000199# define USE_LONGLONG
Bjorn Reese026d29f2002-01-19 15:40:18 +0000200# elif defined(TRIO_COMPILER_SUNPRO)
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000201# define USE_LONGLONG
202# elif defined(_LONG_LONG) || defined(_LONGLONG)
203# define USE_LONGLONG
204# endif
Daniel Veillard92ad2102001-03-27 12:47:33 +0000205# endif
206#endif
207
208/* The extra long numbers */
209#if defined(USE_LONGLONG)
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000210typedef signed long long int trio_longlong_t;
211typedef unsigned long long int trio_ulonglong_t;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000212#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000213typedef signed __int64 trio_longlong_t;
214typedef unsigned __int64 trio_ulonglong_t;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000215#else
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000216typedef TRIO_SIGNED long int trio_longlong_t;
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000217typedef unsigned long int trio_ulonglong_t;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000218#endif
219
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000220/* Maximal and fixed integer types */
Jacob (Jouk) Jansen4d041a22013-12-17 15:32:57 +0800221#if defined(TRIO_COMPILER_SUPPORTS_C99) && !defined( __VMS )
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000222# include <stdint.h>
223typedef intmax_t trio_intmax_t;
224typedef uintmax_t trio_uintmax_t;
225typedef int8_t trio_int8_t;
226typedef int16_t trio_int16_t;
227typedef int32_t trio_int32_t;
228typedef int64_t trio_int64_t;
Jacob (Jouk) Jansen4d041a22013-12-17 15:32:57 +0800229#elif defined(TRIO_COMPILER_SUPPORTS_UNIX98) || defined( __VMS )
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000230# include <inttypes.h>
Jacob (Jouk) Jansen4d041a22013-12-17 15:32:57 +0800231#ifdef __VMS
232typedef long long int intmax_t;
233typedef unsigned long long int uintmax_t;
234#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000235typedef intmax_t trio_intmax_t;
236typedef uintmax_t trio_uintmax_t;
237typedef int8_t trio_int8_t;
238typedef int16_t trio_int16_t;
239typedef int32_t trio_int32_t;
240typedef int64_t trio_int64_t;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000241#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000242typedef trio_longlong_t trio_intmax_t;
243typedef trio_ulonglong_t trio_uintmax_t;
244typedef __int8 trio_int8_t;
245typedef __int16 trio_int16_t;
246typedef __int32 trio_int32_t;
247typedef __int64 trio_int64_t;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000248#else
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000249typedef trio_longlong_t trio_intmax_t;
250typedef trio_ulonglong_t trio_uintmax_t;
251# if defined(TRIO_INT8_T)
252typedef TRIO_INT8_T trio_int8_t;
253# else
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000254typedef TRIO_SIGNED char trio_int8_t;
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000255# endif
256# if defined(TRIO_INT16_T)
257typedef TRIO_INT16_T trio_int16_t;
258# else
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000259typedef TRIO_SIGNED short trio_int16_t;
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000260# endif
261# if defined(TRIO_INT32_T)
262typedef TRIO_INT32_T trio_int32_t;
263# else
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000264typedef TRIO_SIGNED int trio_int32_t;
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000265# endif
266# if defined(TRIO_INT64_T)
267typedef TRIO_INT64_T trio_int64_t;
268# else
269typedef trio_longlong_t trio_int64_t;
270# endif
Daniel Veillard92ad2102001-03-27 12:47:33 +0000271#endif
272
Daniel Veillard59d3ed82007-04-17 12:44:58 +0000273#if (!(defined(TRIO_COMPILER_SUPPORTS_C99) \
274 || defined(TRIO_COMPILER_SUPPORTS_UNIX01))) \
275 && !defined(_WIN32_WCE)
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000276# define floorl(x) floor((double)(x))
277# define fmodl(x,y) fmod((double)(x),(double)(y))
278# define powl(x,y) pow((double)(x),(double)(y))
279#endif
280
281#define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000282
283/*************************************************************************
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000284 * Internal Definitions
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000285 */
286
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000287#ifndef DECIMAL_DIG
288# define DECIMAL_DIG DBL_DIG
289#endif
290
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000291/* Long double sizes */
292#ifdef LDBL_DIG
293# define MAX_MANTISSA_DIGITS LDBL_DIG
294# define MAX_EXPONENT_DIGITS 4
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000295# define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000296#else
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000297# define MAX_MANTISSA_DIGITS DECIMAL_DIG
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000298# define MAX_EXPONENT_DIGITS 3
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000299# define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
300#endif
301
302#if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
303# undef LDBL_DIG
304# undef LDBL_MANT_DIG
305# undef LDBL_EPSILON
306# define LDBL_DIG DBL_DIG
307# define LDBL_MANT_DIG DBL_MANT_DIG
308# define LDBL_EPSILON DBL_EPSILON
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000309#endif
310
311/* The maximal number of digits is for base 2 */
312#define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
Daniel Veillard92ad2102001-03-27 12:47:33 +0000313/* The width of a pointer. The number of bits in a hex digit is 4 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000314#define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
Daniel Veillard92ad2102001-03-27 12:47:33 +0000315
316/* Infinite and Not-A-Number for floating-point */
Bjorn Reese70a9da52001-04-21 16:57:29 +0000317#define INFINITE_LOWER "inf"
318#define INFINITE_UPPER "INF"
319#define LONG_INFINITE_LOWER "infinite"
320#define LONG_INFINITE_UPPER "INFINITE"
321#define NAN_LOWER "nan"
322#define NAN_UPPER "NAN"
Daniel Veillard92ad2102001-03-27 12:47:33 +0000323
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800324#if !defined(HAVE_ISASCII) && !defined(isascii)
Jacob (Jouk) Jansen4d041a22013-12-17 15:32:57 +0800325#ifndef __VMS
326# define isascii(x) ((unsigned int)(x) < 128)
327#endif
Daniel Richard G5706b6d2012-08-06 11:32:54 +0800328#endif
329
Daniel Veillard92ad2102001-03-27 12:47:33 +0000330/* Various constants */
331enum {
332 TYPE_PRINT = 1,
333 TYPE_SCAN = 2,
334
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000335 /* Flags. FLAGS_LAST must be less than ULONG_MAX */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000336 FLAGS_NEW = 0,
337 FLAGS_STICKY = 1,
338 FLAGS_SPACE = 2 * FLAGS_STICKY,
339 FLAGS_SHOWSIGN = 2 * FLAGS_SPACE,
340 FLAGS_LEFTADJUST = 2 * FLAGS_SHOWSIGN,
341 FLAGS_ALTERNATIVE = 2 * FLAGS_LEFTADJUST,
342 FLAGS_SHORT = 2 * FLAGS_ALTERNATIVE,
343 FLAGS_SHORTSHORT = 2 * FLAGS_SHORT,
344 FLAGS_LONG = 2 * FLAGS_SHORTSHORT,
345 FLAGS_QUAD = 2 * FLAGS_LONG,
346 FLAGS_LONGDOUBLE = 2 * FLAGS_QUAD,
347 FLAGS_SIZE_T = 2 * FLAGS_LONGDOUBLE,
348 FLAGS_PTRDIFF_T = 2 * FLAGS_SIZE_T,
349 FLAGS_INTMAX_T = 2 * FLAGS_PTRDIFF_T,
350 FLAGS_NILPADDING = 2 * FLAGS_INTMAX_T,
351 FLAGS_UNSIGNED = 2 * FLAGS_NILPADDING,
352 FLAGS_UPPER = 2 * FLAGS_UNSIGNED,
353 FLAGS_WIDTH = 2 * FLAGS_UPPER,
354 FLAGS_WIDTH_PARAMETER = 2 * FLAGS_WIDTH,
355 FLAGS_PRECISION = 2 * FLAGS_WIDTH_PARAMETER,
356 FLAGS_PRECISION_PARAMETER = 2 * FLAGS_PRECISION,
357 FLAGS_BASE = 2 * FLAGS_PRECISION_PARAMETER,
358 FLAGS_BASE_PARAMETER = 2 * FLAGS_BASE,
359 FLAGS_FLOAT_E = 2 * FLAGS_BASE_PARAMETER,
360 FLAGS_FLOAT_G = 2 * FLAGS_FLOAT_E,
361 FLAGS_QUOTE = 2 * FLAGS_FLOAT_G,
362 FLAGS_WIDECHAR = 2 * FLAGS_QUOTE,
363 FLAGS_ALLOC = 2 * FLAGS_WIDECHAR,
364 FLAGS_IGNORE = 2 * FLAGS_ALLOC,
365 FLAGS_IGNORE_PARAMETER = 2 * FLAGS_IGNORE,
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000366 FLAGS_VARSIZE_PARAMETER = 2 * FLAGS_IGNORE_PARAMETER,
367 FLAGS_FIXED_SIZE = 2 * FLAGS_VARSIZE_PARAMETER,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000368 FLAGS_LAST = FLAGS_FIXED_SIZE,
Daniel Veillard92ad2102001-03-27 12:47:33 +0000369 /* Reused flags */
370 FLAGS_EXCLUDE = FLAGS_SHORT,
Bjorn Reese70a9da52001-04-21 16:57:29 +0000371 FLAGS_USER_DEFINED = FLAGS_IGNORE,
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000372 FLAGS_ROUNDING = FLAGS_INTMAX_T,
Daniel Veillard92ad2102001-03-27 12:47:33 +0000373 /* Compounded flags */
Bjorn Reese70a9da52001-04-21 16:57:29 +0000374 FLAGS_ALL_VARSIZES = FLAGS_LONG | FLAGS_QUAD | FLAGS_INTMAX_T | FLAGS_PTRDIFF_T | FLAGS_SIZE_T,
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000375 FLAGS_ALL_SIZES = FLAGS_ALL_VARSIZES | FLAGS_SHORTSHORT | FLAGS_SHORT,
Daniel Veillard92ad2102001-03-27 12:47:33 +0000376
377 NO_POSITION = -1,
378 NO_WIDTH = 0,
379 NO_PRECISION = -1,
380 NO_SIZE = -1,
381
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000382 /* Do not change these */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000383 NO_BASE = -1,
384 MIN_BASE = 2,
385 MAX_BASE = 36,
386 BASE_BINARY = 2,
387 BASE_OCTAL = 8,
388 BASE_DECIMAL = 10,
389 BASE_HEX = 16,
390
391 /* Maximal number of allowed parameters */
392 MAX_PARAMETERS = 64,
393 /* Maximal number of characters in class */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000394 MAX_CHARACTER_CLASS = UCHAR_MAX + 1,
Daniel Veillard92ad2102001-03-27 12:47:33 +0000395
Bjorn Reese70a9da52001-04-21 16:57:29 +0000396 /* Maximal string lengths for user-defined specifiers */
397 MAX_USER_NAME = 64,
398 MAX_USER_DATA = 256,
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800399
Daniel Veillard92ad2102001-03-27 12:47:33 +0000400 /* Maximal length of locale separator strings */
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000401 MAX_LOCALE_SEPARATOR_LENGTH = MB_LEN_MAX,
Daniel Veillard92ad2102001-03-27 12:47:33 +0000402 /* Maximal number of integers in grouping */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000403 MAX_LOCALE_GROUPS = 64,
404
405 /* Initial size of asprintf buffer */
406 DYNAMIC_START_SIZE = 32
Daniel Veillard92ad2102001-03-27 12:47:33 +0000407};
408
409#define NO_GROUPING ((int)CHAR_MAX)
410
411/* Fundamental formatting parameter types */
412#define FORMAT_UNKNOWN 0
413#define FORMAT_INT 1
414#define FORMAT_DOUBLE 2
415#define FORMAT_CHAR 3
416#define FORMAT_STRING 4
417#define FORMAT_POINTER 5
418#define FORMAT_COUNT 6
419#define FORMAT_PARAMETER 7
420#define FORMAT_GROUP 8
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000421#if TRIO_GNU
Bjorn Reese70a9da52001-04-21 16:57:29 +0000422# define FORMAT_ERRNO 9
423#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000424#if TRIO_EXTENSION
Bjorn Reese70a9da52001-04-21 16:57:29 +0000425# define FORMAT_USER_DEFINED 10
Daniel Veillard92ad2102001-03-27 12:47:33 +0000426#endif
427
428/* Character constants */
429#define CHAR_IDENTIFIER '%'
430#define CHAR_BACKSLASH '\\'
431#define CHAR_QUOTE '\"'
432#define CHAR_ADJUST ' '
433
434/* Character class expressions */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000435#define CLASS_ALNUM "[:alnum:]"
436#define CLASS_ALPHA "[:alpha:]"
437#define CLASS_BLANK "[:blank:]"
438#define CLASS_CNTRL "[:cntrl:]"
439#define CLASS_DIGIT "[:digit:]"
440#define CLASS_GRAPH "[:graph:]"
441#define CLASS_LOWER "[:lower:]"
442#define CLASS_PRINT "[:print:]"
443#define CLASS_PUNCT "[:punct:]"
444#define CLASS_SPACE "[:space:]"
445#define CLASS_UPPER "[:upper:]"
446#define CLASS_XDIGIT "[:xdigit:]"
Daniel Veillard92ad2102001-03-27 12:47:33 +0000447
448/*
449 * SPECIFIERS:
450 *
451 *
452 * a Hex-float
453 * A Hex-float
454 * c Character
455 * C Widechar character (wint_t)
456 * d Decimal
457 * e Float
458 * E Float
459 * F Float
460 * F Float
461 * g Float
462 * G Float
463 * i Integer
464 * m Error message
465 * n Count
466 * o Octal
467 * p Pointer
468 * s String
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000469 * S Widechar string (wchar_t *)
Daniel Veillard92ad2102001-03-27 12:47:33 +0000470 * u Unsigned
471 * x Hex
472 * X Hex
Bjorn Reese70a9da52001-04-21 16:57:29 +0000473 * [] Group
474 * <> User-defined
Daniel Veillard92ad2102001-03-27 12:47:33 +0000475 *
476 * Reserved:
477 *
478 * D Binary Coded Decimal %D(length,precision) (OS/390)
479 */
480#define SPECIFIER_CHAR 'c'
481#define SPECIFIER_STRING 's'
482#define SPECIFIER_DECIMAL 'd'
483#define SPECIFIER_INTEGER 'i'
484#define SPECIFIER_UNSIGNED 'u'
485#define SPECIFIER_OCTAL 'o'
486#define SPECIFIER_HEX 'x'
487#define SPECIFIER_HEX_UPPER 'X'
488#define SPECIFIER_FLOAT_E 'e'
489#define SPECIFIER_FLOAT_E_UPPER 'E'
490#define SPECIFIER_FLOAT_F 'f'
491#define SPECIFIER_FLOAT_F_UPPER 'F'
492#define SPECIFIER_FLOAT_G 'g'
493#define SPECIFIER_FLOAT_G_UPPER 'G'
494#define SPECIFIER_POINTER 'p'
495#define SPECIFIER_GROUP '['
496#define SPECIFIER_UNGROUP ']'
497#define SPECIFIER_COUNT 'n'
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000498#if TRIO_UNIX98
Daniel Veillard92ad2102001-03-27 12:47:33 +0000499# define SPECIFIER_CHAR_UPPER 'C'
500# define SPECIFIER_STRING_UPPER 'S'
501#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000502#if TRIO_C99
Daniel Veillard92ad2102001-03-27 12:47:33 +0000503# define SPECIFIER_HEXFLOAT 'a'
504# define SPECIFIER_HEXFLOAT_UPPER 'A'
505#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000506#if TRIO_GNU
Daniel Veillard92ad2102001-03-27 12:47:33 +0000507# define SPECIFIER_ERRNO 'm'
508#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000509#if TRIO_EXTENSION
Daniel Veillard92ad2102001-03-27 12:47:33 +0000510# define SPECIFIER_BINARY 'b'
511# define SPECIFIER_BINARY_UPPER 'B'
Bjorn Reese70a9da52001-04-21 16:57:29 +0000512# define SPECIFIER_USER_DEFINED_BEGIN '<'
513# define SPECIFIER_USER_DEFINED_END '>'
514# define SPECIFIER_USER_DEFINED_SEPARATOR ':'
Daniel Veillard92ad2102001-03-27 12:47:33 +0000515#endif
516
517/*
518 * QUALIFIERS:
519 *
520 *
521 * Numbers = d,i,o,u,x,X
522 * Float = a,A,e,E,f,F,g,G
523 * String = s
524 * Char = c
525 *
526 *
527 * 9$ Position
528 * Use the 9th parameter. 9 can be any number between 1 and
529 * the maximal argument
530 *
531 * 9 Width
532 * Set width to 9. 9 can be any number, but must not be postfixed
533 * by '$'
534 *
535 * h Short
536 * Numbers:
537 * (unsigned) short int
538 *
539 * hh Short short
540 * Numbers:
541 * (unsigned) char
542 *
543 * l Long
544 * Numbers:
545 * (unsigned) long int
546 * String:
547 * as the S specifier
548 * Char:
549 * as the C specifier
550 *
551 * ll Long Long
552 * Numbers:
553 * (unsigned) long long int
554 *
555 * L Long Double
556 * Float
557 * long double
558 *
559 * # Alternative
560 * Float:
561 * Decimal-point is always present
562 * String:
563 * non-printable characters are handled as \number
564 *
565 * Spacing
566 *
567 * + Sign
568 *
569 * - Alignment
570 *
571 * . Precision
572 *
573 * * Parameter
574 * print: use parameter
575 * scan: no parameter (ignore)
576 *
577 * q Quad
578 *
579 * Z size_t
580 *
581 * w Widechar
582 *
583 * ' Thousands/quote
584 * Numbers:
585 * Integer part grouped in thousands
586 * Binary numbers:
587 * Number grouped in nibbles (4 bits)
588 * String:
589 * Quoted string
590 *
591 * j intmax_t
592 * t prtdiff_t
593 * z size_t
594 *
595 * ! Sticky
596 * @ Parameter (for both print and scan)
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000597 *
598 * I n-bit Integer
599 * Numbers:
600 * The following options exists
601 * I8 = 8-bit integer
602 * I16 = 16-bit integer
603 * I32 = 32-bit integer
604 * I64 = 64-bit integer
Daniel Veillard92ad2102001-03-27 12:47:33 +0000605 */
606#define QUALIFIER_POSITION '$'
607#define QUALIFIER_SHORT 'h'
608#define QUALIFIER_LONG 'l'
609#define QUALIFIER_LONG_UPPER 'L'
610#define QUALIFIER_ALTERNATIVE '#'
611#define QUALIFIER_SPACE ' '
612#define QUALIFIER_PLUS '+'
613#define QUALIFIER_MINUS '-'
614#define QUALIFIER_DOT '.'
615#define QUALIFIER_STAR '*'
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000616#define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000617#if TRIO_C99
Daniel Veillard92ad2102001-03-27 12:47:33 +0000618# define QUALIFIER_SIZE_T 'z'
619# define QUALIFIER_PTRDIFF_T 't'
620# define QUALIFIER_INTMAX_T 'j'
621#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000622#if TRIO_BSD || TRIO_GNU
Daniel Veillard92ad2102001-03-27 12:47:33 +0000623# define QUALIFIER_QUAD 'q'
624#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000625#if TRIO_GNU
Daniel Veillard92ad2102001-03-27 12:47:33 +0000626# define QUALIFIER_SIZE_T_UPPER 'Z'
627#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000628#if TRIO_MISC
Daniel Veillard92ad2102001-03-27 12:47:33 +0000629# define QUALIFIER_WIDECHAR 'w'
630#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000631#if TRIO_MICROSOFT
632# define QUALIFIER_FIXED_SIZE 'I'
633#endif
634#if TRIO_EXTENSION
Daniel Veillard92ad2102001-03-27 12:47:33 +0000635# define QUALIFIER_QUOTE '\''
636# define QUALIFIER_STICKY '!'
637# define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
638# define QUALIFIER_PARAM '@' /* Experimental */
639# define QUALIFIER_COLON ':' /* For scanlists */
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000640# define QUALIFIER_EQUAL '=' /* For scanlists */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000641# define QUALIFIER_ROUNDING_UPPER 'R'
Daniel Veillard92ad2102001-03-27 12:47:33 +0000642#endif
643
Bjorn Reese70a9da52001-04-21 16:57:29 +0000644
645/*************************************************************************
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000646 *
647 * Internal Structures
648 *
649 *************************************************************************/
Bjorn Reese70a9da52001-04-21 16:57:29 +0000650
651/* Parameters */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000652typedef struct {
Bjorn Reese026d29f2002-01-19 15:40:18 +0000653 /* An indication of which entry in the data union is used */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000654 int type;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000655 /* The flags */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +0000656 trio_flags_t flags;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000657 /* The width qualifier */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000658 int width;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000659 /* The precision qualifier */
Bjorn Reese70a9da52001-04-21 16:57:29 +0000660 int precision;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000661 /* The base qualifier */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000662 int base;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000663 /* The size for the variable size qualifier */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000664 int varsize;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000665 /* The marker of the end of the specifier */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000666 int indexAfterSpecifier;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000667 /* The data from the argument list */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000668 union {
669 char *string;
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000670#if TRIO_WIDECHAR
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000671 trio_wchar_t *wstring;
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000672#endif
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000673 trio_pointer_t pointer;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000674 union {
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000675 trio_intmax_t as_signed;
676 trio_uintmax_t as_unsigned;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000677 } number;
678 double doubleNumber;
679 double *doublePointer;
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000680 trio_long_double_t longdoubleNumber;
681 trio_long_double_t *longdoublePointer;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000682 int errorNumber;
683 } data;
Bjorn Reese70a9da52001-04-21 16:57:29 +0000684 /* For the user-defined specifier */
685 char user_name[MAX_USER_NAME];
686 char user_data[MAX_USER_DATA];
Bjorn Reese026d29f2002-01-19 15:40:18 +0000687} trio_parameter_t;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000688
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000689/* Container for customized functions */
690typedef struct {
691 union {
692 trio_outstream_t out;
693 trio_instream_t in;
694 } stream;
695 trio_pointer_t closure;
696} trio_custom_t;
697
Bjorn Reese70a9da52001-04-21 16:57:29 +0000698/* General trio "class" */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000699typedef struct _trio_class_t {
Bjorn Reese70a9da52001-04-21 16:57:29 +0000700 /*
Bjorn Reese026d29f2002-01-19 15:40:18 +0000701 * The function to write characters to a stream.
702 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000703 void (*OutStream) TRIO_PROTO((struct _trio_class_t *, int));
Bjorn Reese026d29f2002-01-19 15:40:18 +0000704 /*
705 * The function to read characters from a stream.
706 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000707 void (*InStream) TRIO_PROTO((struct _trio_class_t *, int *));
Bjorn Reese026d29f2002-01-19 15:40:18 +0000708 /*
709 * The current location in the stream.
710 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000711 trio_pointer_t location;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000712 /*
713 * The character currently being processed.
714 */
715 int current;
716 /*
717 * The number of characters that would have been written/read
718 * if there had been sufficient space.
Daniel Veillard92ad2102001-03-27 12:47:33 +0000719 */
Bjorn Reese70a9da52001-04-21 16:57:29 +0000720 int processed;
721 /*
722 * The number of characters that are actually written/read.
Bjorn Reese026d29f2002-01-19 15:40:18 +0000723 * Processed and committed will only differ for the *nprintf
Daniel Veillard92ad2102001-03-27 12:47:33 +0000724 * and *nscanf functions.
725 */
Bjorn Reese70a9da52001-04-21 16:57:29 +0000726 int committed;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000727 /*
728 * The upper limit of characters that may be written/read.
729 */
Bjorn Reese70a9da52001-04-21 16:57:29 +0000730 int max;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000731 /*
732 * The last output error that was detected.
733 */
734 int error;
735} trio_class_t;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000736
Bjorn Reese70a9da52001-04-21 16:57:29 +0000737/* References (for user-defined callbacks) */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000738typedef struct _trio_reference_t {
739 trio_class_t *data;
740 trio_parameter_t *parameter;
741} trio_reference_t;
Bjorn Reese70a9da52001-04-21 16:57:29 +0000742
743/* Registered entries (for user-defined callbacks) */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000744typedef struct _trio_userdef_t {
745 struct _trio_userdef_t *next;
Bjorn Reese70a9da52001-04-21 16:57:29 +0000746 trio_callback_t callback;
747 char *name;
Bjorn Reese026d29f2002-01-19 15:40:18 +0000748} trio_userdef_t;
Bjorn Reese70a9da52001-04-21 16:57:29 +0000749
Daniel Veillard92ad2102001-03-27 12:47:33 +0000750/*************************************************************************
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000751 *
752 * Internal Variables
753 *
754 *************************************************************************/
Daniel Veillard92ad2102001-03-27 12:47:33 +0000755
Bjorn Reese026d29f2002-01-19 15:40:18 +0000756static TRIO_CONST char rcsid[] = "@(#)$Id$";
757
758/*
759 * Need this to workaround a parser bug in HP C/iX compiler that fails
760 * to resolves macro definitions that includes type 'long double',
761 * e.g: va_arg(arg_ptr, long double)
762 */
763#if defined(TRIO_PLATFORM_MPEIX)
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000764static TRIO_CONST trio_long_double_t ___dummy_long_double = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +0000765#endif
Bjorn Reese026d29f2002-01-19 15:40:18 +0000766
767static TRIO_CONST char internalNullString[] = "(nil)";
Daniel Veillard92ad2102001-03-27 12:47:33 +0000768
Bjorn Reese70a9da52001-04-21 16:57:29 +0000769#if defined(USE_LOCALE)
770static struct lconv *internalLocaleValues = NULL;
771#endif
Daniel Veillard92ad2102001-03-27 12:47:33 +0000772
Bjorn Reese70a9da52001-04-21 16:57:29 +0000773/*
774 * UNIX98 says "in a locale where the radix character is not defined,
775 * the radix character defaults to a period (.)"
776 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000777static int internalDecimalPointLength = 1;
778static int internalThousandSeparatorLength = 1;
779static char internalDecimalPoint = '.';
780static char internalDecimalPointString[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ".";
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000781static char internalThousandSeparator[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ",";
Bjorn Reese70a9da52001-04-21 16:57:29 +0000782static char internalGrouping[MAX_LOCALE_GROUPS] = { (char)NO_GROUPING };
783
Bjorn Reese026d29f2002-01-19 15:40:18 +0000784static TRIO_CONST char internalDigitsLower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
785static TRIO_CONST char internalDigitsUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Bjorn Reese70a9da52001-04-21 16:57:29 +0000786static BOOLEAN_T internalDigitsUnconverted = TRUE;
787static int internalDigitArray[128];
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000788#if TRIO_EXTENSION
789static BOOLEAN_T internalCollationUnconverted = TRUE;
790static char internalCollationArray[MAX_CHARACTER_CLASS][MAX_CHARACTER_CLASS];
791#endif
Bjorn Reese70a9da52001-04-21 16:57:29 +0000792
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000793#if TRIO_EXTENSION
Bjorn Reese026d29f2002-01-19 15:40:18 +0000794static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion = NULL;
795static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion = NULL;
796static trio_userdef_t *internalUserDef = NULL;
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000797#endif
Bjorn Reese70a9da52001-04-21 16:57:29 +0000798
799
800/*************************************************************************
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000801 *
802 * Internal Functions
803 *
804 ************************************************************************/
805
806#if defined(TRIO_MINIMAL)
807# define TRIO_STRING_PUBLIC static
808# include "triostr.c"
809#endif /* defined(TRIO_MINIMAL) */
Daniel Veillard92ad2102001-03-27 12:47:33 +0000810
811/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +0000812 * TrioIsQualifier
Daniel Veillard92ad2102001-03-27 12:47:33 +0000813 *
814 * Description:
815 * Remember to add all new qualifiers to this function.
816 * QUALIFIER_POSITION must not be added.
817 */
Bjorn Reese026d29f2002-01-19 15:40:18 +0000818TRIO_PRIVATE BOOLEAN_T
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000819TrioIsQualifier
820TRIO_ARGS1((character),
821 TRIO_CONST char character)
Daniel Veillard92ad2102001-03-27 12:47:33 +0000822{
823 /* QUALIFIER_POSITION is not included */
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000824 switch (character)
Daniel Veillard92ad2102001-03-27 12:47:33 +0000825 {
826 case '0': case '1': case '2': case '3': case '4':
827 case '5': case '6': case '7': case '8': case '9':
828 case QUALIFIER_PLUS:
829 case QUALIFIER_MINUS:
830 case QUALIFIER_SPACE:
831 case QUALIFIER_DOT:
832 case QUALIFIER_STAR:
833 case QUALIFIER_ALTERNATIVE:
834 case QUALIFIER_SHORT:
835 case QUALIFIER_LONG:
836 case QUALIFIER_LONG_UPPER:
837 case QUALIFIER_CIRCUMFLEX:
838#if defined(QUALIFIER_SIZE_T)
839 case QUALIFIER_SIZE_T:
840#endif
841#if defined(QUALIFIER_PTRDIFF_T)
842 case QUALIFIER_PTRDIFF_T:
843#endif
844#if defined(QUALIFIER_INTMAX_T)
845 case QUALIFIER_INTMAX_T:
846#endif
847#if defined(QUALIFIER_QUAD)
848 case QUALIFIER_QUAD:
849#endif
850#if defined(QUALIFIER_SIZE_T_UPPER)
851 case QUALIFIER_SIZE_T_UPPER:
852#endif
853#if defined(QUALIFIER_WIDECHAR)
854 case QUALIFIER_WIDECHAR:
855#endif
Daniel Veillard92ad2102001-03-27 12:47:33 +0000856#if defined(QUALIFIER_QUOTE)
857 case QUALIFIER_QUOTE:
858#endif
859#if defined(QUALIFIER_STICKY)
860 case QUALIFIER_STICKY:
861#endif
862#if defined(QUALIFIER_VARSIZE)
863 case QUALIFIER_VARSIZE:
864#endif
865#if defined(QUALIFIER_PARAM)
866 case QUALIFIER_PARAM:
867#endif
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000868#if defined(QUALIFIER_FIXED_SIZE)
869 case QUALIFIER_FIXED_SIZE:
870#endif
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000871#if defined(QUALIFIER_ROUNDING_UPPER)
872 case QUALIFIER_ROUNDING_UPPER:
873#endif
Daniel Veillard92ad2102001-03-27 12:47:33 +0000874 return TRUE;
875 default:
876 return FALSE;
877 }
878}
879
880/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +0000881 * TrioSetLocale
Bjorn Reese70a9da52001-04-21 16:57:29 +0000882 */
883#if defined(USE_LOCALE)
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000884TRIO_PRIVATE void
885TrioSetLocale(TRIO_NOARGS)
Bjorn Reese70a9da52001-04-21 16:57:29 +0000886{
887 internalLocaleValues = (struct lconv *)localeconv();
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000888 if (internalLocaleValues)
Bjorn Reese70a9da52001-04-21 16:57:29 +0000889 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000890 if ((internalLocaleValues->decimal_point) &&
891 (internalLocaleValues->decimal_point[0] != NIL))
892 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000893 internalDecimalPointLength = trio_length(internalLocaleValues->decimal_point);
894 if (internalDecimalPointLength == 1)
895 {
896 internalDecimalPoint = internalLocaleValues->decimal_point[0];
897 }
898 else
899 {
900 internalDecimalPoint = NIL;
901 trio_copy_max(internalDecimalPointString,
902 sizeof(internalDecimalPointString),
903 internalLocaleValues->decimal_point);
904 }
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000905 }
906 if ((internalLocaleValues->thousands_sep) &&
907 (internalLocaleValues->thousands_sep[0] != NIL))
908 {
Bjorn Reese026d29f2002-01-19 15:40:18 +0000909 trio_copy_max(internalThousandSeparator,
910 sizeof(internalThousandSeparator),
911 internalLocaleValues->thousands_sep);
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000912 internalThousandSeparatorLength = trio_length(internalThousandSeparator);
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000913 }
914 if ((internalLocaleValues->grouping) &&
915 (internalLocaleValues->grouping[0] != NIL))
916 {
Bjorn Reese026d29f2002-01-19 15:40:18 +0000917 trio_copy_max(internalGrouping,
918 sizeof(internalGrouping),
919 internalLocaleValues->grouping);
Bjorn Reese906ec8a2001-06-05 12:46:33 +0000920 }
Bjorn Reese70a9da52001-04-21 16:57:29 +0000921 }
922}
923#endif /* defined(USE_LOCALE) */
924
Daniel Veillardb7c29c32002-09-25 22:44:43 +0000925TRIO_PRIVATE int
926TrioCalcThousandSeparatorLength
927TRIO_ARGS1((digits),
928 int digits)
929{
930#if TRIO_EXTENSION
931 int count = 0;
932 int step = NO_GROUPING;
933 char *groupingPointer = internalGrouping;
934
935 while (digits > 0)
936 {
937 if (*groupingPointer == CHAR_MAX)
938 {
939 /* Disable grouping */
940 break; /* while */
941 }
942 else if (*groupingPointer == 0)
943 {
944 /* Repeat last group */
945 if (step == NO_GROUPING)
946 {
947 /* Error in locale */
948 break; /* while */
949 }
950 }
951 else
952 {
953 step = *groupingPointer++;
954 }
955 if (digits > step)
956 count += internalThousandSeparatorLength;
957 digits -= step;
958 }
959 return count;
960#else
961 return 0;
962#endif
963}
964
965TRIO_PRIVATE BOOLEAN_T
966TrioFollowedBySeparator
967TRIO_ARGS1((position),
968 int position)
969{
970#if TRIO_EXTENSION
971 int step = 0;
972 char *groupingPointer = internalGrouping;
973
974 position--;
975 if (position == 0)
976 return FALSE;
977 while (position > 0)
978 {
979 if (*groupingPointer == CHAR_MAX)
980 {
981 /* Disable grouping */
982 break; /* while */
983 }
984 else if (*groupingPointer != 0)
985 {
986 step = *groupingPointer++;
987 }
988 if (step == 0)
989 break;
990 position -= step;
991 }
992 return (position == 0);
993#else
994 return FALSE;
995#endif
996}
997
Bjorn Reese70a9da52001-04-21 16:57:29 +0000998/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +0000999 * TrioGetPosition
Daniel Veillard92ad2102001-03-27 12:47:33 +00001000 *
1001 * Get the %n$ position.
1002 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00001003TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001004TrioGetPosition
1005TRIO_ARGS2((format, indexPointer),
1006 TRIO_CONST char *format,
1007 int *indexPointer)
Daniel Veillard92ad2102001-03-27 12:47:33 +00001008{
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001009#if TRIO_UNIX98
Daniel Veillard92ad2102001-03-27 12:47:33 +00001010 char *tmpformat;
1011 int number = 0;
1012 int index = *indexPointer;
1013
Bjorn Reese026d29f2002-01-19 15:40:18 +00001014 number = (int)trio_to_long(&format[index], &tmpformat, BASE_DECIMAL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00001015 index = (int)(tmpformat - format);
1016 if ((number != 0) && (QUALIFIER_POSITION == format[index++]))
1017 {
1018 *indexPointer = index;
Bjorn Reese70a9da52001-04-21 16:57:29 +00001019 /*
1020 * number is decreased by 1, because n$ starts from 1, whereas
Daniel Veillard92ad2102001-03-27 12:47:33 +00001021 * the array it is indexing starts from 0.
1022 */
1023 return number - 1;
1024 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001025#endif
Daniel Veillard92ad2102001-03-27 12:47:33 +00001026 return NO_POSITION;
1027}
1028
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001029#if TRIO_EXTENSION
Daniel Veillard92ad2102001-03-27 12:47:33 +00001030/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00001031 * TrioFindNamespace
Bjorn Reese70a9da52001-04-21 16:57:29 +00001032 *
1033 * Find registered user-defined specifier.
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001034 * The prev argument is used for optimization only.
Bjorn Reese70a9da52001-04-21 16:57:29 +00001035 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00001036TRIO_PRIVATE trio_userdef_t *
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001037TrioFindNamespace
1038TRIO_ARGS2((name, prev),
1039 TRIO_CONST char *name,
1040 trio_userdef_t **prev)
Bjorn Reese70a9da52001-04-21 16:57:29 +00001041{
Bjorn Reese026d29f2002-01-19 15:40:18 +00001042 trio_userdef_t *def;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001043
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001044 if (internalEnterCriticalRegion)
1045 (void)internalEnterCriticalRegion(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001046
Bjorn Reese70a9da52001-04-21 16:57:29 +00001047 for (def = internalUserDef; def; def = def->next)
1048 {
1049 /* Case-sensitive string comparison */
Bjorn Reese026d29f2002-01-19 15:40:18 +00001050 if (trio_equal_case(def->name, name))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001051 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001052
Bjorn Reese70a9da52001-04-21 16:57:29 +00001053 if (prev)
1054 *prev = def;
1055 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001056
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001057 if (internalLeaveCriticalRegion)
1058 (void)internalLeaveCriticalRegion(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001059
Bjorn Reese70a9da52001-04-21 16:57:29 +00001060 return def;
1061}
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001062#endif
1063
1064/*************************************************************************
1065 * TrioPower
1066 *
1067 * Description:
1068 * Calculate pow(base, exponent), where number and exponent are integers.
1069 */
1070TRIO_PRIVATE trio_long_double_t
1071TrioPower
1072TRIO_ARGS2((number, exponent),
1073 int number,
1074 int exponent)
1075{
1076 trio_long_double_t result;
1077
1078 if (number == 10)
1079 {
1080 switch (exponent)
1081 {
1082 /* Speed up calculation of common cases */
1083 case 0:
1084 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E-1);
1085 break;
1086 case 1:
1087 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+0);
1088 break;
1089 case 2:
1090 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+1);
1091 break;
1092 case 3:
1093 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+2);
1094 break;
1095 case 4:
1096 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+3);
1097 break;
1098 case 5:
1099 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+4);
1100 break;
1101 case 6:
1102 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+5);
1103 break;
1104 case 7:
1105 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+6);
1106 break;
1107 case 8:
1108 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+7);
1109 break;
1110 case 9:
1111 result = (trio_long_double_t)number * TRIO_SUFFIX_LONG(1E+8);
1112 break;
1113 default:
1114 result = powl((trio_long_double_t)number,
1115 (trio_long_double_t)exponent);
1116 break;
1117 }
1118 }
1119 else
1120 {
1121 return powl((trio_long_double_t)number, (trio_long_double_t)exponent);
1122 }
1123 return result;
1124}
1125
1126/*************************************************************************
1127 * TrioLogarithm
1128 */
1129TRIO_PRIVATE double
1130TrioLogarithm
1131TRIO_ARGS2((number, base),
1132 double number,
1133 int base)
1134{
1135 double result;
1136
1137 if (number <= 0.0)
1138 {
1139 /* xlC crashes on log(0) */
1140 result = (number == 0.0) ? trio_ninf() : trio_nan();
1141 }
1142 else
1143 {
1144 if (base == 10)
1145 {
1146 result = log10(number);
1147 }
1148 else
1149 {
1150 result = log10(number) / log10((double)base);
1151 }
1152 }
1153 return result;
1154}
1155
1156/*************************************************************************
1157 * TrioLogarithmBase
1158 */
1159TRIO_PRIVATE double
1160TrioLogarithmBase
1161TRIO_ARGS1((base),
1162 int base)
1163{
1164 switch (base)
1165 {
1166 case BASE_BINARY : return 1.0;
1167 case BASE_OCTAL : return 3.0;
1168 case BASE_DECIMAL: return 3.321928094887362345;
1169 case BASE_HEX : return 4.0;
1170 default : return TrioLogarithm((double)base, 2);
1171 }
1172}
Bjorn Reese70a9da52001-04-21 16:57:29 +00001173
1174/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00001175 * TrioParse
Daniel Veillard92ad2102001-03-27 12:47:33 +00001176 *
1177 * Description:
1178 * Parse the format string
1179 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00001180TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001181TrioParse
1182TRIO_ARGS5((type, format, parameters, arglist, argarray),
1183 int type,
1184 TRIO_CONST char *format,
1185 trio_parameter_t *parameters,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08001186 TRIO_VA_LIST_PTR arglist,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001187 trio_pointer_t *argarray)
Daniel Veillard92ad2102001-03-27 12:47:33 +00001188{
Daniel Veillard92ad2102001-03-27 12:47:33 +00001189 /* Count the number of times a parameter is referenced */
1190 unsigned short usedEntries[MAX_PARAMETERS];
Daniel Veillard92ad2102001-03-27 12:47:33 +00001191 /* Parameter counters */
1192 int parameterPosition;
1193 int currentParam;
1194 int maxParam = -1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001195 /* Utility variables */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00001196 trio_flags_t flags;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001197 int width;
1198 int precision;
1199 int varsize;
1200 int base;
1201 int index; /* Index into formatting string */
1202 int dots; /* Count number of dots in modifier part */
1203 BOOLEAN_T positional; /* Does the specifier have a positional? */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001204 BOOLEAN_T gotSticky = FALSE; /* Are there any sticky modifiers at all? */
Bjorn Reese70a9da52001-04-21 16:57:29 +00001205 /*
1206 * indices specifies the order in which the parameters must be
Daniel Veillard92ad2102001-03-27 12:47:33 +00001207 * read from the va_args (this is necessary to handle positionals)
1208 */
1209 int indices[MAX_PARAMETERS];
1210 int pos = 0;
1211 /* Various variables */
1212 char ch;
Bjorn Reese026d29f2002-01-19 15:40:18 +00001213#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillard92ad2102001-03-27 12:47:33 +00001214 int charlen;
Bjorn Reese026d29f2002-01-19 15:40:18 +00001215#endif
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001216 int save_errno;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001217 int i = -1;
1218 int num;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001219 char *tmpformat;
1220
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001221 /* One and only one of arglist and argarray must be used */
1222 assert((arglist != NULL) ^ (argarray != NULL));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001223
Bjorn Reese70a9da52001-04-21 16:57:29 +00001224 /*
1225 * The 'parameters' array is not initialized, but we need to
Daniel Veillard92ad2102001-03-27 12:47:33 +00001226 * know which entries we have used.
1227 */
1228 memset(usedEntries, 0, sizeof(usedEntries));
Daniel Veillard92ad2102001-03-27 12:47:33 +00001229
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001230 save_errno = errno;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001231 index = 0;
1232 parameterPosition = 0;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001233#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001234 (void)mblen(NULL, 0);
Daniel Veillard92ad2102001-03-27 12:47:33 +00001235#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001236
Daniel Veillard92ad2102001-03-27 12:47:33 +00001237 while (format[index])
1238 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001239#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillard92ad2102001-03-27 12:47:33 +00001240 if (! isascii(format[index]))
1241 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00001242 /*
1243 * Multibyte characters cannot be legal specifiers or
Daniel Veillard92ad2102001-03-27 12:47:33 +00001244 * modifiers, so we skip over them.
1245 */
1246 charlen = mblen(&format[index], MB_LEN_MAX);
1247 index += (charlen > 0) ? charlen : 1;
1248 continue; /* while */
1249 }
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001250#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
Daniel Veillard92ad2102001-03-27 12:47:33 +00001251 if (CHAR_IDENTIFIER == format[index++])
1252 {
1253 if (CHAR_IDENTIFIER == format[index])
1254 {
1255 index++;
1256 continue; /* while */
1257 }
1258
1259 flags = FLAGS_NEW;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001260 dots = 0;
1261 currentParam = TrioGetPosition(format, &index);
1262 positional = (NO_POSITION != currentParam);
1263 if (!positional)
1264 {
1265 /* We have no positional, get the next counter */
1266 currentParam = parameterPosition;
1267 }
1268 if(currentParam >= MAX_PARAMETERS)
1269 {
1270 /* Bail out completely to make the error more obvious */
1271 return TRIO_ERROR_RETURN(TRIO_ETOOMANY, index);
1272 }
1273
1274 if (currentParam > maxParam)
1275 maxParam = currentParam;
1276
1277 /* Default values */
1278 width = NO_WIDTH;
1279 precision = NO_PRECISION;
1280 base = NO_BASE;
1281 varsize = NO_SIZE;
1282
Bjorn Reese70a9da52001-04-21 16:57:29 +00001283 while (TrioIsQualifier(format[index]))
Daniel Veillard92ad2102001-03-27 12:47:33 +00001284 {
1285 ch = format[index++];
1286
Daniel Veillard92ad2102001-03-27 12:47:33 +00001287 switch (ch)
1288 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00001289 case QUALIFIER_SPACE:
1290 flags |= FLAGS_SPACE;
1291 break;
1292
1293 case QUALIFIER_PLUS:
1294 flags |= FLAGS_SHOWSIGN;
1295 break;
1296
1297 case QUALIFIER_MINUS:
1298 flags |= FLAGS_LEFTADJUST;
1299 flags &= ~FLAGS_NILPADDING;
1300 break;
1301
1302 case QUALIFIER_ALTERNATIVE:
1303 flags |= FLAGS_ALTERNATIVE;
1304 break;
1305
1306 case QUALIFIER_DOT:
1307 if (dots == 0) /* Precision */
1308 {
1309 dots++;
1310
1311 /* Skip if no precision */
1312 if (QUALIFIER_DOT == format[index])
1313 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001314
Daniel Veillard92ad2102001-03-27 12:47:33 +00001315 /* After the first dot we have the precision */
1316 flags |= FLAGS_PRECISION;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001317 if ((QUALIFIER_STAR == format[index])
1318#if defined(QUALIFIER_PARAM)
1319 || (QUALIFIER_PARAM == format[index])
1320#endif
1321 )
Daniel Veillard92ad2102001-03-27 12:47:33 +00001322 {
1323 index++;
1324 flags |= FLAGS_PRECISION_PARAMETER;
1325
1326 precision = TrioGetPosition(format, &index);
1327 if (precision == NO_POSITION)
1328 {
1329 parameterPosition++;
1330 if (positional)
1331 precision = parameterPosition;
1332 else
1333 {
1334 precision = currentParam;
1335 currentParam = precision + 1;
1336 }
1337 }
1338 else
1339 {
1340 if (! positional)
1341 currentParam = precision + 1;
1342 if (width > maxParam)
1343 maxParam = precision;
1344 }
1345 if (currentParam > maxParam)
1346 maxParam = currentParam;
1347 }
1348 else
1349 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00001350 precision = trio_to_long(&format[index],
1351 &tmpformat,
1352 BASE_DECIMAL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00001353 index = (int)(tmpformat - format);
1354 }
1355 }
1356 else if (dots == 1) /* Base */
1357 {
1358 dots++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001359
Daniel Veillard92ad2102001-03-27 12:47:33 +00001360 /* After the second dot we have the base */
1361 flags |= FLAGS_BASE;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001362 if ((QUALIFIER_STAR == format[index])
1363#if defined(QUALIFIER_PARAM)
1364 || (QUALIFIER_PARAM == format[index])
1365#endif
1366 )
Daniel Veillard92ad2102001-03-27 12:47:33 +00001367 {
1368 index++;
1369 flags |= FLAGS_BASE_PARAMETER;
1370 base = TrioGetPosition(format, &index);
1371 if (base == NO_POSITION)
1372 {
1373 parameterPosition++;
1374 if (positional)
1375 base = parameterPosition;
1376 else
1377 {
1378 base = currentParam;
1379 currentParam = base + 1;
1380 }
1381 }
1382 else
1383 {
1384 if (! positional)
1385 currentParam = base + 1;
1386 if (base > maxParam)
1387 maxParam = base;
1388 }
1389 if (currentParam > maxParam)
1390 maxParam = currentParam;
1391 }
1392 else
1393 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00001394 base = trio_to_long(&format[index],
1395 &tmpformat,
1396 BASE_DECIMAL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00001397 if (base > MAX_BASE)
1398 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1399 index = (int)(tmpformat - format);
1400 }
1401 }
1402 else
1403 {
1404 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1405 }
1406 break; /* QUALIFIER_DOT */
1407
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001408#if defined(QUALIFIER_PARAM)
Daniel Veillard92ad2102001-03-27 12:47:33 +00001409 case QUALIFIER_PARAM:
1410 type = TYPE_PRINT;
1411 /* FALLTHROUGH */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001412#endif
Daniel Veillard92ad2102001-03-27 12:47:33 +00001413 case QUALIFIER_STAR:
1414 /* This has different meanings for print and scan */
1415 if (TYPE_PRINT == type)
1416 {
1417 /* Read with from parameter */
1418 flags |= (FLAGS_WIDTH | FLAGS_WIDTH_PARAMETER);
1419 width = TrioGetPosition(format, &index);
1420 if (width == NO_POSITION)
1421 {
1422 parameterPosition++;
1423 if (positional)
1424 width = parameterPosition;
1425 else
1426 {
1427 width = currentParam;
1428 currentParam = width + 1;
1429 }
1430 }
1431 else
1432 {
1433 if (! positional)
1434 currentParam = width + 1;
1435 if (width > maxParam)
1436 maxParam = width;
1437 }
1438 if (currentParam > maxParam)
1439 maxParam = currentParam;
1440 }
1441 else
1442 {
1443 /* Scan, but do not store result */
1444 flags |= FLAGS_IGNORE;
1445 }
1446
1447 break; /* QUALIFIER_STAR */
1448
1449 case '0':
1450 if (! (flags & FLAGS_LEFTADJUST))
1451 flags |= FLAGS_NILPADDING;
1452 /* FALLTHROUGH */
1453 case '1': case '2': case '3': case '4':
1454 case '5': case '6': case '7': case '8': case '9':
1455 flags |= FLAGS_WIDTH;
1456 /* &format[index - 1] is used to "rewind" the read
1457 * character from format
1458 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00001459 width = trio_to_long(&format[index - 1],
1460 &tmpformat,
1461 BASE_DECIMAL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00001462 index = (int)(tmpformat - format);
1463 break;
1464
1465 case QUALIFIER_SHORT:
1466 if (flags & FLAGS_SHORTSHORT)
1467 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1468 else if (flags & FLAGS_SHORT)
1469 flags |= FLAGS_SHORTSHORT;
1470 else
1471 flags |= FLAGS_SHORT;
1472 break;
1473
1474 case QUALIFIER_LONG:
1475 if (flags & FLAGS_QUAD)
1476 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1477 else if (flags & FLAGS_LONG)
1478 flags |= FLAGS_QUAD;
1479 else
1480 flags |= FLAGS_LONG;
1481 break;
1482
1483 case QUALIFIER_LONG_UPPER:
1484 flags |= FLAGS_LONGDOUBLE;
1485 break;
1486
1487#if defined(QUALIFIER_SIZE_T)
1488 case QUALIFIER_SIZE_T:
1489 flags |= FLAGS_SIZE_T;
1490 /* Modify flags for later truncation of number */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001491 if (sizeof(size_t) == sizeof(trio_ulonglong_t))
Daniel Veillard92ad2102001-03-27 12:47:33 +00001492 flags |= FLAGS_QUAD;
1493 else if (sizeof(size_t) == sizeof(long))
1494 flags |= FLAGS_LONG;
1495 break;
1496#endif
1497
1498#if defined(QUALIFIER_PTRDIFF_T)
1499 case QUALIFIER_PTRDIFF_T:
1500 flags |= FLAGS_PTRDIFF_T;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001501 if (sizeof(ptrdiff_t) == sizeof(trio_ulonglong_t))
Daniel Veillard92ad2102001-03-27 12:47:33 +00001502 flags |= FLAGS_QUAD;
1503 else if (sizeof(ptrdiff_t) == sizeof(long))
1504 flags |= FLAGS_LONG;
1505 break;
1506#endif
1507
1508#if defined(QUALIFIER_INTMAX_T)
1509 case QUALIFIER_INTMAX_T:
1510 flags |= FLAGS_INTMAX_T;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001511 if (sizeof(trio_intmax_t) == sizeof(trio_ulonglong_t))
Daniel Veillard92ad2102001-03-27 12:47:33 +00001512 flags |= FLAGS_QUAD;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001513 else if (sizeof(trio_intmax_t) == sizeof(long))
Daniel Veillard92ad2102001-03-27 12:47:33 +00001514 flags |= FLAGS_LONG;
1515 break;
1516#endif
1517
1518#if defined(QUALIFIER_QUAD)
1519 case QUALIFIER_QUAD:
1520 flags |= FLAGS_QUAD;
1521 break;
1522#endif
1523
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001524#if defined(QUALIFIER_FIXED_SIZE)
1525 case QUALIFIER_FIXED_SIZE:
1526 if (flags & FLAGS_FIXED_SIZE)
1527 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1528
1529 if (flags & (FLAGS_ALL_SIZES | FLAGS_LONGDOUBLE |
1530 FLAGS_WIDECHAR | FLAGS_VARSIZE_PARAMETER))
1531 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1532
1533 if ((format[index] == '6') &&
1534 (format[index + 1] == '4'))
1535 {
1536 varsize = sizeof(trio_int64_t);
1537 index += 2;
1538 }
1539 else if ((format[index] == '3') &&
1540 (format[index + 1] == '2'))
1541 {
1542 varsize = sizeof(trio_int32_t);
1543 index += 2;
1544 }
1545 else if ((format[index] == '1') &&
1546 (format[index + 1] == '6'))
1547 {
1548 varsize = sizeof(trio_int16_t);
1549 index += 2;
1550 }
1551 else if (format[index] == '8')
1552 {
1553 varsize = sizeof(trio_int8_t);
1554 index++;
1555 }
1556 else
1557 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001558
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001559 flags |= FLAGS_FIXED_SIZE;
1560 break;
1561#endif
1562
Daniel Veillard92ad2102001-03-27 12:47:33 +00001563#if defined(QUALIFIER_WIDECHAR)
1564 case QUALIFIER_WIDECHAR:
1565 flags |= FLAGS_WIDECHAR;
1566 break;
1567#endif
1568
1569#if defined(QUALIFIER_SIZE_T_UPPER)
1570 case QUALIFIER_SIZE_T_UPPER:
1571 break;
1572#endif
1573
1574#if defined(QUALIFIER_QUOTE)
1575 case QUALIFIER_QUOTE:
1576 flags |= FLAGS_QUOTE;
1577 break;
1578#endif
1579
1580#if defined(QUALIFIER_STICKY)
1581 case QUALIFIER_STICKY:
1582 flags |= FLAGS_STICKY;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001583 gotSticky = TRUE;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001584 break;
1585#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001586
Daniel Veillard92ad2102001-03-27 12:47:33 +00001587#if defined(QUALIFIER_VARSIZE)
1588 case QUALIFIER_VARSIZE:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001589 flags |= FLAGS_VARSIZE_PARAMETER;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001590 parameterPosition++;
1591 if (positional)
1592 varsize = parameterPosition;
1593 else
1594 {
1595 varsize = currentParam;
1596 currentParam = varsize + 1;
1597 }
1598 if (currentParam > maxParam)
1599 maxParam = currentParam;
1600 break;
1601#endif
1602
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001603#if defined(QUALIFIER_ROUNDING_UPPER)
1604 case QUALIFIER_ROUNDING_UPPER:
1605 flags |= FLAGS_ROUNDING;
1606 break;
1607#endif
1608
Daniel Veillard92ad2102001-03-27 12:47:33 +00001609 default:
1610 /* Bail out completely to make the error more obvious */
1611 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1612 }
1613 } /* while qualifier */
1614
Bjorn Reese70a9da52001-04-21 16:57:29 +00001615 /*
1616 * Parameters only need the type and value. The value is
Daniel Veillard92ad2102001-03-27 12:47:33 +00001617 * read later.
1618 */
1619 if (flags & FLAGS_WIDTH_PARAMETER)
1620 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00001621 usedEntries[width] += 1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001622 parameters[pos].type = FORMAT_PARAMETER;
Bjorn Reese026d29f2002-01-19 15:40:18 +00001623 parameters[pos].flags = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001624 indices[width] = pos;
1625 width = pos++;
1626 }
1627 if (flags & FLAGS_PRECISION_PARAMETER)
1628 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00001629 usedEntries[precision] += 1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001630 parameters[pos].type = FORMAT_PARAMETER;
Bjorn Reese026d29f2002-01-19 15:40:18 +00001631 parameters[pos].flags = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001632 indices[precision] = pos;
1633 precision = pos++;
1634 }
1635 if (flags & FLAGS_BASE_PARAMETER)
1636 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00001637 usedEntries[base] += 1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001638 parameters[pos].type = FORMAT_PARAMETER;
Bjorn Reese026d29f2002-01-19 15:40:18 +00001639 parameters[pos].flags = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001640 indices[base] = pos;
1641 base = pos++;
1642 }
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001643 if (flags & FLAGS_VARSIZE_PARAMETER)
Daniel Veillard92ad2102001-03-27 12:47:33 +00001644 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00001645 usedEntries[varsize] += 1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001646 parameters[pos].type = FORMAT_PARAMETER;
Bjorn Reese026d29f2002-01-19 15:40:18 +00001647 parameters[pos].flags = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001648 indices[varsize] = pos;
1649 varsize = pos++;
1650 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001651
Daniel Veillard92ad2102001-03-27 12:47:33 +00001652 indices[currentParam] = pos;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001653
Daniel Veillard92ad2102001-03-27 12:47:33 +00001654 switch (format[index++])
1655 {
1656#if defined(SPECIFIER_CHAR_UPPER)
1657 case SPECIFIER_CHAR_UPPER:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001658 flags |= FLAGS_WIDECHAR;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001659 /* FALLTHROUGH */
1660#endif
1661 case SPECIFIER_CHAR:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001662 if (flags & FLAGS_LONG)
1663 flags |= FLAGS_WIDECHAR;
1664 else if (flags & FLAGS_SHORT)
1665 flags &= ~FLAGS_WIDECHAR;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001666 parameters[pos].type = FORMAT_CHAR;
1667 break;
1668
1669#if defined(SPECIFIER_STRING_UPPER)
1670 case SPECIFIER_STRING_UPPER:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001671 flags |= FLAGS_WIDECHAR;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001672 /* FALLTHROUGH */
1673#endif
1674 case SPECIFIER_STRING:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001675 if (flags & FLAGS_LONG)
1676 flags |= FLAGS_WIDECHAR;
1677 else if (flags & FLAGS_SHORT)
1678 flags &= ~FLAGS_WIDECHAR;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001679 parameters[pos].type = FORMAT_STRING;
1680 break;
1681
1682 case SPECIFIER_GROUP:
1683 if (TYPE_SCAN == type)
1684 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001685 int depth = 1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001686 parameters[pos].type = FORMAT_GROUP;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001687 if (format[index] == QUALIFIER_CIRCUMFLEX)
1688 index++;
1689 if (format[index] == SPECIFIER_UNGROUP)
1690 index++;
1691 if (format[index] == QUALIFIER_MINUS)
1692 index++;
1693 /* Skip nested brackets */
1694 while (format[index] != NIL)
Daniel Veillard92ad2102001-03-27 12:47:33 +00001695 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001696 if (format[index] == SPECIFIER_GROUP)
1697 {
1698 depth++;
1699 }
1700 else if (format[index] == SPECIFIER_UNGROUP)
1701 {
1702 if (--depth <= 0)
1703 {
1704 index++;
1705 break;
1706 }
1707 }
1708 index++;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001709 }
1710 }
1711 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001712
Daniel Veillard92ad2102001-03-27 12:47:33 +00001713 case SPECIFIER_INTEGER:
1714 parameters[pos].type = FORMAT_INT;
1715 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001716
Daniel Veillard92ad2102001-03-27 12:47:33 +00001717 case SPECIFIER_UNSIGNED:
1718 flags |= FLAGS_UNSIGNED;
1719 parameters[pos].type = FORMAT_INT;
1720 break;
1721
1722 case SPECIFIER_DECIMAL:
1723 /* Disable base modifier */
1724 flags &= ~FLAGS_BASE_PARAMETER;
1725 base = BASE_DECIMAL;
1726 parameters[pos].type = FORMAT_INT;
1727 break;
1728
1729 case SPECIFIER_OCTAL:
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00001730 flags |= FLAGS_UNSIGNED;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001731 flags &= ~FLAGS_BASE_PARAMETER;
1732 base = BASE_OCTAL;
1733 parameters[pos].type = FORMAT_INT;
1734 break;
1735
1736#if defined(SPECIFIER_BINARY)
1737 case SPECIFIER_BINARY_UPPER:
1738 flags |= FLAGS_UPPER;
1739 /* FALLTHROUGH */
1740 case SPECIFIER_BINARY:
1741 flags |= FLAGS_NILPADDING;
1742 flags &= ~FLAGS_BASE_PARAMETER;
1743 base = BASE_BINARY;
1744 parameters[pos].type = FORMAT_INT;
1745 break;
1746#endif
1747
1748 case SPECIFIER_HEX_UPPER:
1749 flags |= FLAGS_UPPER;
1750 /* FALLTHROUGH */
1751 case SPECIFIER_HEX:
1752 flags |= FLAGS_UNSIGNED;
1753 flags &= ~FLAGS_BASE_PARAMETER;
1754 base = BASE_HEX;
1755 parameters[pos].type = FORMAT_INT;
1756 break;
1757
1758 case SPECIFIER_FLOAT_E_UPPER:
1759 flags |= FLAGS_UPPER;
1760 /* FALLTHROUGH */
1761 case SPECIFIER_FLOAT_E:
1762 flags |= FLAGS_FLOAT_E;
1763 parameters[pos].type = FORMAT_DOUBLE;
1764 break;
1765
1766 case SPECIFIER_FLOAT_G_UPPER:
1767 flags |= FLAGS_UPPER;
1768 /* FALLTHROUGH */
1769 case SPECIFIER_FLOAT_G:
1770 flags |= FLAGS_FLOAT_G;
1771 parameters[pos].type = FORMAT_DOUBLE;
1772 break;
1773
1774 case SPECIFIER_FLOAT_F_UPPER:
1775 flags |= FLAGS_UPPER;
1776 /* FALLTHROUGH */
1777 case SPECIFIER_FLOAT_F:
1778 parameters[pos].type = FORMAT_DOUBLE;
1779 break;
1780
1781 case SPECIFIER_POINTER:
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001782 if (sizeof(trio_pointer_t) == sizeof(trio_ulonglong_t))
1783 flags |= FLAGS_QUAD;
1784 else if (sizeof(trio_pointer_t) == sizeof(long))
1785 flags |= FLAGS_LONG;
Daniel Veillard92ad2102001-03-27 12:47:33 +00001786 parameters[pos].type = FORMAT_POINTER;
1787 break;
1788
1789 case SPECIFIER_COUNT:
1790 parameters[pos].type = FORMAT_COUNT;
1791 break;
1792
1793#if defined(SPECIFIER_HEXFLOAT)
1794# if defined(SPECIFIER_HEXFLOAT_UPPER)
1795 case SPECIFIER_HEXFLOAT_UPPER:
1796 flags |= FLAGS_UPPER;
1797 /* FALLTHROUGH */
1798# endif
1799 case SPECIFIER_HEXFLOAT:
1800 base = BASE_HEX;
1801 parameters[pos].type = FORMAT_DOUBLE;
1802 break;
1803#endif
1804
1805#if defined(FORMAT_ERRNO)
1806 case SPECIFIER_ERRNO:
1807 parameters[pos].type = FORMAT_ERRNO;
1808 break;
1809#endif
1810
Bjorn Reese70a9da52001-04-21 16:57:29 +00001811#if defined(SPECIFIER_USER_DEFINED_BEGIN)
1812 case SPECIFIER_USER_DEFINED_BEGIN:
1813 {
1814 unsigned int max;
1815 int without_namespace = TRUE;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001816
Bjorn Reese70a9da52001-04-21 16:57:29 +00001817 parameters[pos].type = FORMAT_USER_DEFINED;
1818 parameters[pos].user_name[0] = NIL;
1819 tmpformat = (char *)&format[index];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001820
Bjorn Reese70a9da52001-04-21 16:57:29 +00001821 while ((ch = format[index]))
1822 {
1823 index++;
1824 if (ch == SPECIFIER_USER_DEFINED_END)
1825 {
1826 if (without_namespace)
1827 {
1828 /* We must get the handle first */
1829 parameters[pos].type = FORMAT_PARAMETER;
1830 parameters[pos].indexAfterSpecifier = index;
1831 parameters[pos].flags = FLAGS_USER_DEFINED;
1832 /* Adjust parameters for insertion of new one */
1833 pos++;
Bjorn Reese70a9da52001-04-21 16:57:29 +00001834 usedEntries[currentParam] += 1;
Bjorn Reese70a9da52001-04-21 16:57:29 +00001835 parameters[pos].type = FORMAT_USER_DEFINED;
1836 currentParam++;
1837 indices[currentParam] = pos;
1838 if (currentParam > maxParam)
1839 maxParam = currentParam;
1840 }
1841 /* Copy the user data */
1842 max = (unsigned int)(&format[index] - tmpformat);
1843 if (max > MAX_USER_DATA)
1844 max = MAX_USER_DATA;
Bjorn Reese026d29f2002-01-19 15:40:18 +00001845 trio_copy_max(parameters[pos].user_data,
1846 max,
1847 tmpformat);
Bjorn Reese70a9da52001-04-21 16:57:29 +00001848 break; /* while */
1849 }
1850 if (ch == SPECIFIER_USER_DEFINED_SEPARATOR)
1851 {
1852 without_namespace = FALSE;
1853 /* Copy the namespace for later looking-up */
1854 max = (int)(&format[index] - tmpformat);
1855 if (max > MAX_USER_NAME)
1856 max = MAX_USER_NAME;
Bjorn Reese026d29f2002-01-19 15:40:18 +00001857 trio_copy_max(parameters[pos].user_name,
1858 max,
1859 tmpformat);
Bjorn Reese70a9da52001-04-21 16:57:29 +00001860 tmpformat = (char *)&format[index];
1861 }
1862 }
1863 if (ch != SPECIFIER_USER_DEFINED_END)
1864 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1865 }
1866 break;
1867#endif /* defined(SPECIFIER_USER_DEFINED_BEGIN) */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001868
Daniel Veillard92ad2102001-03-27 12:47:33 +00001869 default:
1870 /* Bail out completely to make the error more obvious */
1871 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
1872 }
1873
Daniel Veillard92ad2102001-03-27 12:47:33 +00001874 /* Count the number of times this entry has been used */
1875 usedEntries[currentParam] += 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001876
Daniel Veillard92ad2102001-03-27 12:47:33 +00001877 /* Find last sticky parameters */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001878 if (gotSticky && !(flags & FLAGS_STICKY))
Daniel Veillard92ad2102001-03-27 12:47:33 +00001879 {
1880 for (i = pos - 1; i >= 0; i--)
1881 {
1882 if (parameters[i].type == FORMAT_PARAMETER)
1883 continue;
1884 if ((parameters[i].flags & FLAGS_STICKY) &&
1885 (parameters[i].type == parameters[pos].type))
1886 {
1887 /* Do not overwrite current qualifiers */
Bjorn Reese70a9da52001-04-21 16:57:29 +00001888 flags |= (parameters[i].flags & (unsigned long)~FLAGS_STICKY);
Daniel Veillard92ad2102001-03-27 12:47:33 +00001889 if (width == NO_WIDTH)
1890 width = parameters[i].width;
1891 if (precision == NO_PRECISION)
1892 precision = parameters[i].precision;
1893 if (base == NO_BASE)
1894 base = parameters[i].base;
1895 break;
1896 }
1897 }
1898 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001899
Daniel Veillard92ad2102001-03-27 12:47:33 +00001900 parameters[pos].indexAfterSpecifier = index;
1901 parameters[pos].flags = flags;
1902 parameters[pos].width = width;
1903 parameters[pos].precision = precision;
1904 parameters[pos].base = (base == NO_BASE) ? BASE_DECIMAL : base;
1905 parameters[pos].varsize = varsize;
1906 pos++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001907
Bjorn Reese70a9da52001-04-21 16:57:29 +00001908 if (! positional)
Daniel Veillard92ad2102001-03-27 12:47:33 +00001909 parameterPosition++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001910
Daniel Veillard92ad2102001-03-27 12:47:33 +00001911 } /* if identifier */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001912
Daniel Veillard92ad2102001-03-27 12:47:33 +00001913 } /* while format characters left */
1914
1915 for (num = 0; num <= maxParam; num++)
1916 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00001917 if (usedEntries[num] != 1)
1918 {
1919 if (usedEntries[num] == 0) /* gap detected */
1920 return TRIO_ERROR_RETURN(TRIO_EGAP, num);
1921 else /* double references detected */
1922 return TRIO_ERROR_RETURN(TRIO_EDBLREF, num);
1923 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001924
Daniel Veillard92ad2102001-03-27 12:47:33 +00001925 i = indices[num];
1926
Bjorn Reese70a9da52001-04-21 16:57:29 +00001927 /*
1928 * FORMAT_PARAMETERS are only present if they must be read,
Daniel Veillard92ad2102001-03-27 12:47:33 +00001929 * so it makes no sense to check the ignore flag (besides,
1930 * the flags variable is not set for that particular type)
1931 */
1932 if ((parameters[i].type != FORMAT_PARAMETER) &&
1933 (parameters[i].flags & FLAGS_IGNORE))
1934 continue; /* for all arguments */
1935
Bjorn Reese70a9da52001-04-21 16:57:29 +00001936 /*
1937 * The stack arguments are read according to ANSI C89
Daniel Veillard92ad2102001-03-27 12:47:33 +00001938 * default argument promotions:
1939 *
1940 * char = int
1941 * short = int
1942 * unsigned char = unsigned int
1943 * unsigned short = unsigned int
1944 * float = double
1945 *
1946 * In addition to the ANSI C89 these types are read (the
1947 * default argument promotions of C99 has not been
1948 * considered yet)
1949 *
1950 * long long
1951 * long double
1952 * size_t
1953 * ptrdiff_t
1954 * intmax_t
1955 */
1956 switch (parameters[i].type)
1957 {
1958 case FORMAT_GROUP:
1959 case FORMAT_STRING:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001960#if TRIO_WIDECHAR
1961 if (flags & FLAGS_WIDECHAR)
1962 {
1963 parameters[i].data.wstring = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08001964 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_wchar_t *)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001965 : (trio_wchar_t *)(argarray[num]);
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001966 }
1967 else
1968#endif
1969 {
1970 parameters[i].data.string = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08001971 ? va_arg(TRIO_VA_LIST_DEREF(arglist), char *)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001972 : (char *)(argarray[num]);
1973 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00001974 break;
1975
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001976#if defined(FORMAT_USER_DEFINED)
1977 case FORMAT_USER_DEFINED:
1978#endif
Daniel Veillard92ad2102001-03-27 12:47:33 +00001979 case FORMAT_POINTER:
1980 case FORMAT_COUNT:
1981 case FORMAT_UNKNOWN:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001982 parameters[i].data.pointer = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08001983 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t )
Bjorn Reese70a9da52001-04-21 16:57:29 +00001984 : argarray[num];
Daniel Veillard92ad2102001-03-27 12:47:33 +00001985 break;
1986
1987 case FORMAT_CHAR:
1988 case FORMAT_INT:
1989 if (TYPE_SCAN == type)
1990 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00001991 if (argarray == NULL)
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001992 parameters[i].data.pointer =
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08001993 (trio_pointer_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t);
Bjorn Reese70a9da52001-04-21 16:57:29 +00001994 else
1995 {
1996 if (parameters[i].type == FORMAT_CHAR)
1997 parameters[i].data.pointer =
Daniel Veillardb7c29c32002-09-25 22:44:43 +00001998 (trio_pointer_t)((char *)argarray[num]);
Bjorn Reese70a9da52001-04-21 16:57:29 +00001999 else if (parameters[i].flags & FLAGS_SHORT)
2000 parameters[i].data.pointer =
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002001 (trio_pointer_t)((short *)argarray[num]);
Bjorn Reese70a9da52001-04-21 16:57:29 +00002002 else
2003 parameters[i].data.pointer =
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002004 (trio_pointer_t)((int *)argarray[num]);
Bjorn Reese70a9da52001-04-21 16:57:29 +00002005 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002006 }
2007 else
2008 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002009#if defined(QUALIFIER_VARSIZE) || defined(QUALIFIER_FIXED_SIZE)
Bjorn Reese026d29f2002-01-19 15:40:18 +00002010 if (parameters[i].flags
2011 & (FLAGS_VARSIZE_PARAMETER | FLAGS_FIXED_SIZE))
Daniel Veillard92ad2102001-03-27 12:47:33 +00002012 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002013 if (parameters[i].flags & FLAGS_VARSIZE_PARAMETER)
2014 {
2015 /*
2016 * Variable sizes are mapped onto the fixed sizes, in
2017 * accordance with integer promotion.
2018 *
2019 * Please note that this may not be portable, as we
2020 * only guess the size, not the layout of the numbers.
2021 * For example, if int is little-endian, and long is
2022 * big-endian, then this will fail.
2023 */
2024 varsize = (int)parameters[parameters[i].varsize].data.number.as_unsigned;
2025 }
2026 else
2027 {
2028 /* Used for the I<bits> modifiers */
2029 varsize = parameters[i].varsize;
2030 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00002031 parameters[i].flags &= ~FLAGS_ALL_VARSIZES;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002032
Daniel Veillard92ad2102001-03-27 12:47:33 +00002033 if (varsize <= (int)sizeof(int))
2034 ;
2035 else if (varsize <= (int)sizeof(long))
2036 parameters[i].flags |= FLAGS_LONG;
2037#if defined(QUALIFIER_INTMAX_T)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002038 else if (varsize <= (int)sizeof(trio_longlong_t))
Daniel Veillard92ad2102001-03-27 12:47:33 +00002039 parameters[i].flags |= FLAGS_QUAD;
2040 else
2041 parameters[i].flags |= FLAGS_INTMAX_T;
2042#else
2043 else
2044 parameters[i].flags |= FLAGS_QUAD;
2045#endif
2046 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00002047#endif /* defined(QUALIFIER_VARSIZE) */
Daniel Veillard92ad2102001-03-27 12:47:33 +00002048#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
2049 if (parameters[i].flags & FLAGS_SIZE_T)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002050 parameters[i].data.number.as_unsigned = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002051 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), size_t)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002052 : (trio_uintmax_t)(*((size_t *)argarray[num]));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002053 else
2054#endif
2055#if defined(QUALIFIER_PTRDIFF_T)
2056 if (parameters[i].flags & FLAGS_PTRDIFF_T)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002057 parameters[i].data.number.as_unsigned = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002058 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), ptrdiff_t)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002059 : (trio_uintmax_t)(*((ptrdiff_t *)argarray[num]));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002060 else
2061#endif
2062#if defined(QUALIFIER_INTMAX_T)
2063 if (parameters[i].flags & FLAGS_INTMAX_T)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002064 parameters[i].data.number.as_unsigned = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002065 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_intmax_t)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002066 : (trio_uintmax_t)(*((trio_intmax_t *)argarray[num]));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002067 else
2068#endif
2069 if (parameters[i].flags & FLAGS_QUAD)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002070 parameters[i].data.number.as_unsigned = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002071 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), trio_ulonglong_t)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002072 : (trio_uintmax_t)(*((trio_ulonglong_t *)argarray[num]));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002073 else if (parameters[i].flags & FLAGS_LONG)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002074 parameters[i].data.number.as_unsigned = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002075 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), long)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002076 : (trio_uintmax_t)(*((long *)argarray[num]));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002077 else
Bjorn Reese70a9da52001-04-21 16:57:29 +00002078 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002079 if (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002080 parameters[i].data.number.as_unsigned = (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), int);
Bjorn Reese70a9da52001-04-21 16:57:29 +00002081 else
2082 {
2083 if (parameters[i].type == FORMAT_CHAR)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002084 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((char *)argarray[num]));
Bjorn Reese70a9da52001-04-21 16:57:29 +00002085 else if (parameters[i].flags & FLAGS_SHORT)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002086 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((short *)argarray[num]));
Bjorn Reese70a9da52001-04-21 16:57:29 +00002087 else
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002088 parameters[i].data.number.as_unsigned = (trio_uintmax_t)(*((int *)argarray[num]));
Bjorn Reese70a9da52001-04-21 16:57:29 +00002089 }
2090 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002091 }
2092 break;
2093
2094 case FORMAT_PARAMETER:
Bjorn Reese70a9da52001-04-21 16:57:29 +00002095 /*
2096 * The parameter for the user-defined specifier is a pointer,
2097 * whereas the rest (width, precision, base) uses an integer.
2098 */
2099 if (parameters[i].flags & FLAGS_USER_DEFINED)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002100 parameters[i].data.pointer = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002101 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_pointer_t )
Bjorn Reese70a9da52001-04-21 16:57:29 +00002102 : argarray[num];
2103 else
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002104 parameters[i].data.number.as_unsigned = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002105 ? (trio_uintmax_t)va_arg(TRIO_VA_LIST_DEREF(arglist), int)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002106 : (trio_uintmax_t)(*((int *)argarray[num]));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002107 break;
2108
2109 case FORMAT_DOUBLE:
2110 if (TYPE_SCAN == type)
2111 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002112 if (parameters[i].flags & FLAGS_LONGDOUBLE)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002113 parameters[i].data.longdoublePointer = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002114 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_long_double_t *)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002115 : (trio_long_double_t *)argarray[num];
Daniel Veillard92ad2102001-03-27 12:47:33 +00002116 else
Bjorn Reese70a9da52001-04-21 16:57:29 +00002117 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002118 if (parameters[i].flags & FLAGS_LONG)
2119 parameters[i].data.doublePointer = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002120 ? va_arg(TRIO_VA_LIST_DEREF(arglist), double *)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002121 : (double *)argarray[num];
2122 else
2123 parameters[i].data.doublePointer = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002124 ? (double *)va_arg(TRIO_VA_LIST_DEREF(arglist), float *)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002125 : (double *)((float *)argarray[num]);
Bjorn Reese70a9da52001-04-21 16:57:29 +00002126 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002127 }
2128 else
2129 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002130 if (parameters[i].flags & FLAGS_LONGDOUBLE)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002131 parameters[i].data.longdoubleNumber = (argarray == NULL)
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002132 ? va_arg(TRIO_VA_LIST_DEREF(arglist), trio_long_double_t)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002133 : (trio_long_double_t)(*((trio_long_double_t *)argarray[num]));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002134 else
Bjorn Reese70a9da52001-04-21 16:57:29 +00002135 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002136 if (argarray == NULL)
Bjorn Reese026d29f2002-01-19 15:40:18 +00002137 parameters[i].data.longdoubleNumber =
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08002138 (trio_long_double_t)va_arg(TRIO_VA_LIST_DEREF(arglist), double);
Bjorn Reese70a9da52001-04-21 16:57:29 +00002139 else
2140 {
2141 if (parameters[i].flags & FLAGS_SHORT)
Bjorn Reese026d29f2002-01-19 15:40:18 +00002142 parameters[i].data.longdoubleNumber =
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002143 (trio_long_double_t)(*((float *)argarray[num]));
Bjorn Reese70a9da52001-04-21 16:57:29 +00002144 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00002145 parameters[i].data.longdoubleNumber =
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002146 (trio_long_double_t)(*((double *)argarray[num]));
Bjorn Reese70a9da52001-04-21 16:57:29 +00002147 }
2148 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002149 }
2150 break;
2151
2152#if defined(FORMAT_ERRNO)
2153 case FORMAT_ERRNO:
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002154 parameters[i].data.errorNumber = save_errno;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002155 break;
2156#endif
2157
2158 default:
2159 break;
2160 }
2161 } /* for all specifiers */
2162 return num;
2163}
2164
2165
2166/*************************************************************************
2167 *
Bjorn Reese026d29f2002-01-19 15:40:18 +00002168 * FORMATTING
Daniel Veillard92ad2102001-03-27 12:47:33 +00002169 *
2170 ************************************************************************/
2171
2172
2173/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00002174 * TrioWriteNumber
Daniel Veillard92ad2102001-03-27 12:47:33 +00002175 *
2176 * Description:
2177 * Output a number.
2178 * The complexity of this function is a result of the complexity
2179 * of the dependencies of the flags.
2180 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00002181TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002182TrioWriteNumber
2183TRIO_ARGS6((self, number, flags, width, precision, base),
2184 trio_class_t *self,
2185 trio_uintmax_t number,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002186 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002187 int width,
2188 int precision,
2189 int base)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002190{
2191 BOOLEAN_T isNegative;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002192 BOOLEAN_T isNumberZero;
2193 BOOLEAN_T isPrecisionZero;
2194 BOOLEAN_T ignoreNumber;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002195 char buffer[MAX_CHARS_IN(trio_uintmax_t) * (1 + MAX_LOCALE_SEPARATOR_LENGTH) + 1];
Daniel Veillard92ad2102001-03-27 12:47:33 +00002196 char *bufferend;
2197 char *pointer;
Bjorn Reese026d29f2002-01-19 15:40:18 +00002198 TRIO_CONST char *digits;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002199 int i;
2200 int length;
2201 char *p;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002202 int count;
2203
2204 assert(VALID(self));
2205 assert(VALID(self->OutStream));
Bjorn Reese026d29f2002-01-19 15:40:18 +00002206 assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002207
Bjorn Reese70a9da52001-04-21 16:57:29 +00002208 digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002209 if (base == NO_BASE)
2210 base = BASE_DECIMAL;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002211
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002212 isNumberZero = (number == 0);
2213 isPrecisionZero = (precision == 0);
2214 ignoreNumber = (isNumberZero
2215 && isPrecisionZero
2216 && !((flags & FLAGS_ALTERNATIVE) && (base == BASE_OCTAL)));
2217
2218 if (flags & FLAGS_UNSIGNED)
2219 {
2220 isNegative = FALSE;
2221 flags &= ~FLAGS_SHOWSIGN;
2222 }
2223 else
2224 {
2225 isNegative = ((trio_intmax_t)number < 0);
2226 if (isNegative)
2227 number = -((trio_intmax_t)number);
2228 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002229
2230 if (flags & FLAGS_QUAD)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002231 number &= (trio_ulonglong_t)-1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002232 else if (flags & FLAGS_LONG)
2233 number &= (unsigned long)-1;
2234 else
2235 number &= (unsigned int)-1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002236
Daniel Veillard92ad2102001-03-27 12:47:33 +00002237 /* Build number */
2238 pointer = bufferend = &buffer[sizeof(buffer) - 1];
2239 *pointer-- = NIL;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002240 for (i = 1; i < (int)sizeof(buffer); i++)
2241 {
2242 *pointer-- = digits[number % base];
2243 number /= base;
2244 if (number == 0)
2245 break;
2246
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002247 if ((flags & FLAGS_QUOTE) && TrioFollowedBySeparator(i + 1))
Daniel Veillard92ad2102001-03-27 12:47:33 +00002248 {
2249 /*
2250 * We are building the number from the least significant
2251 * to the most significant digit, so we have to copy the
2252 * thousand separator backwards
2253 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002254 length = internalThousandSeparatorLength;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002255 if (((int)(pointer - buffer) - length) > 0)
2256 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00002257 p = &internalThousandSeparator[length - 1];
Daniel Veillard92ad2102001-03-27 12:47:33 +00002258 while (length-- > 0)
2259 *pointer-- = *p--;
2260 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002261 }
2262 }
2263
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002264 if (! ignoreNumber)
2265 {
2266 /* Adjust width */
2267 width -= (bufferend - pointer) - 1;
2268 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002269
2270 /* Adjust precision */
2271 if (NO_PRECISION != precision)
2272 {
2273 precision -= (bufferend - pointer) - 1;
2274 if (precision < 0)
2275 precision = 0;
2276 flags |= FLAGS_NILPADDING;
2277 }
2278
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002279 /* Calculate padding */
2280 count = (! ((flags & FLAGS_LEFTADJUST) || (precision == NO_PRECISION)))
2281 ? precision
2282 : 0;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002283
Daniel Veillard92ad2102001-03-27 12:47:33 +00002284 /* Adjust width further */
2285 if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
2286 width--;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002287 if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002288 {
2289 switch (base)
2290 {
2291 case BASE_BINARY:
2292 case BASE_HEX:
2293 width -= 2;
2294 break;
2295 case BASE_OCTAL:
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002296 if (!(flags & FLAGS_NILPADDING) || (count == 0))
2297 width--;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002298 break;
2299 default:
2300 break;
2301 }
2302 }
2303
2304 /* Output prefixes spaces if needed */
2305 if (! ((flags & FLAGS_LEFTADJUST) ||
2306 ((flags & FLAGS_NILPADDING) && (precision == NO_PRECISION))))
2307 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00002308 while (width-- > count)
2309 self->OutStream(self, CHAR_ADJUST);
2310 }
2311
2312 /* width has been adjusted for signs and alternatives */
2313 if (isNegative)
2314 self->OutStream(self, '-');
2315 else if (flags & FLAGS_SHOWSIGN)
2316 self->OutStream(self, '+');
2317 else if (flags & FLAGS_SPACE)
2318 self->OutStream(self, ' ');
2319
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002320 /* Prefix is not written when the value is zero */
2321 if ((flags & FLAGS_ALTERNATIVE) && !isNumberZero)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002322 {
2323 switch (base)
2324 {
2325 case BASE_BINARY:
2326 self->OutStream(self, '0');
2327 self->OutStream(self, (flags & FLAGS_UPPER) ? 'B' : 'b');
2328 break;
2329
2330 case BASE_OCTAL:
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002331 if (!(flags & FLAGS_NILPADDING) || (count == 0))
2332 self->OutStream(self, '0');
Daniel Veillard92ad2102001-03-27 12:47:33 +00002333 break;
2334
2335 case BASE_HEX:
2336 self->OutStream(self, '0');
2337 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2338 break;
2339
2340 default:
2341 break;
2342 } /* switch base */
2343 }
2344
2345 /* Output prefixed zero padding if needed */
2346 if (flags & FLAGS_NILPADDING)
2347 {
2348 if (precision == NO_PRECISION)
2349 precision = width;
2350 while (precision-- > 0)
2351 {
2352 self->OutStream(self, '0');
2353 width--;
2354 }
2355 }
2356
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002357 if (! ignoreNumber)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002358 {
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002359 /* Output the number itself */
2360 while (*(++pointer))
2361 {
2362 self->OutStream(self, *pointer);
2363 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002364 }
2365
2366 /* Output trailing spaces if needed */
2367 if (flags & FLAGS_LEFTADJUST)
2368 {
2369 while (width-- > 0)
2370 self->OutStream(self, CHAR_ADJUST);
2371 }
2372}
2373
2374/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00002375 * TrioWriteStringCharacter
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002376 *
2377 * Description:
2378 * Output a single character of a string
2379 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00002380TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002381TrioWriteStringCharacter
2382TRIO_ARGS3((self, ch, flags),
2383 trio_class_t *self,
2384 int ch,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002385 trio_flags_t flags)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002386{
2387 if (flags & FLAGS_ALTERNATIVE)
2388 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002389 if (! isprint(ch))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002390 {
2391 /*
2392 * Non-printable characters are converted to C escapes or
2393 * \number, if no C escape exists.
2394 */
2395 self->OutStream(self, CHAR_BACKSLASH);
2396 switch (ch)
2397 {
2398 case '\007': self->OutStream(self, 'a'); break;
2399 case '\b': self->OutStream(self, 'b'); break;
2400 case '\f': self->OutStream(self, 'f'); break;
2401 case '\n': self->OutStream(self, 'n'); break;
2402 case '\r': self->OutStream(self, 'r'); break;
2403 case '\t': self->OutStream(self, 't'); break;
2404 case '\v': self->OutStream(self, 'v'); break;
2405 case '\\': self->OutStream(self, '\\'); break;
2406 default:
2407 self->OutStream(self, 'x');
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002408 TrioWriteNumber(self, (trio_uintmax_t)ch,
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002409 FLAGS_UNSIGNED | FLAGS_NILPADDING,
2410 2, 2, BASE_HEX);
2411 break;
2412 }
2413 }
2414 else if (ch == CHAR_BACKSLASH)
2415 {
2416 self->OutStream(self, CHAR_BACKSLASH);
2417 self->OutStream(self, CHAR_BACKSLASH);
2418 }
2419 else
2420 {
2421 self->OutStream(self, ch);
2422 }
2423 }
2424 else
2425 {
2426 self->OutStream(self, ch);
2427 }
2428}
2429
2430/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00002431 * TrioWriteString
Daniel Veillard92ad2102001-03-27 12:47:33 +00002432 *
2433 * Description:
2434 * Output a string
2435 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00002436TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002437TrioWriteString
2438TRIO_ARGS5((self, string, flags, width, precision),
2439 trio_class_t *self,
2440 TRIO_CONST char *string,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002441 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002442 int width,
2443 int precision)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002444{
2445 int length;
2446 int ch;
2447
2448 assert(VALID(self));
2449 assert(VALID(self->OutStream));
2450
2451 if (string == NULL)
2452 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00002453 string = internalNullString;
2454 length = sizeof(internalNullString) - 1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002455 /* Disable quoting for the null pointer */
2456 flags &= (~FLAGS_QUOTE);
2457 width = 0;
2458 }
2459 else
2460 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00002461 length = trio_length(string);
Daniel Veillard92ad2102001-03-27 12:47:33 +00002462 }
2463 if ((NO_PRECISION != precision) &&
2464 (precision < length))
2465 {
2466 length = precision;
2467 }
2468 width -= length;
2469
2470 if (flags & FLAGS_QUOTE)
2471 self->OutStream(self, CHAR_QUOTE);
2472
2473 if (! (flags & FLAGS_LEFTADJUST))
2474 {
2475 while (width-- > 0)
2476 self->OutStream(self, CHAR_ADJUST);
2477 }
2478
2479 while (length-- > 0)
2480 {
2481 /* The ctype parameters must be an unsigned char (or EOF) */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002482 ch = (int)((unsigned char)(*string++));
2483 TrioWriteStringCharacter(self, ch, flags);
Daniel Veillard92ad2102001-03-27 12:47:33 +00002484 }
2485
2486 if (flags & FLAGS_LEFTADJUST)
2487 {
2488 while (width-- > 0)
2489 self->OutStream(self, CHAR_ADJUST);
2490 }
2491 if (flags & FLAGS_QUOTE)
2492 self->OutStream(self, CHAR_QUOTE);
2493}
2494
2495/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00002496 * TrioWriteWideStringCharacter
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002497 *
2498 * Description:
2499 * Output a wide string as a multi-byte sequence
2500 */
2501#if TRIO_WIDECHAR
Bjorn Reese026d29f2002-01-19 15:40:18 +00002502TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002503TrioWriteWideStringCharacter
2504TRIO_ARGS4((self, wch, flags, width),
2505 trio_class_t *self,
2506 trio_wchar_t wch,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002507 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002508 int width)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002509{
2510 int size;
2511 int i;
2512 int ch;
2513 char *string;
2514 char buffer[MB_LEN_MAX + 1];
2515
2516 if (width == NO_WIDTH)
2517 width = sizeof(buffer);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002518
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002519 size = wctomb(buffer, wch);
2520 if ((size <= 0) || (size > width) || (buffer[0] == NIL))
2521 return 0;
2522
2523 string = buffer;
2524 i = size;
2525 while ((width >= i) && (width-- > 0) && (i-- > 0))
2526 {
2527 /* The ctype parameters must be an unsigned char (or EOF) */
2528 ch = (int)((unsigned char)(*string++));
2529 TrioWriteStringCharacter(self, ch, flags);
2530 }
2531 return size;
2532}
2533#endif /* TRIO_WIDECHAR */
2534
2535/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00002536 * TrioWriteWideString
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002537 *
2538 * Description:
2539 * Output a wide character string as a multi-byte string
2540 */
2541#if TRIO_WIDECHAR
Bjorn Reese026d29f2002-01-19 15:40:18 +00002542TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002543TrioWriteWideString
2544TRIO_ARGS5((self, wstring, flags, width, precision),
2545 trio_class_t *self,
2546 TRIO_CONST trio_wchar_t *wstring,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002547 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002548 int width,
2549 int precision)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002550{
2551 int length;
2552 int size;
2553
2554 assert(VALID(self));
2555 assert(VALID(self->OutStream));
2556
2557#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002558 (void)mblen(NULL, 0);
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002559#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002560
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002561 if (wstring == NULL)
2562 {
2563 TrioWriteString(self, NULL, flags, width, precision);
2564 return;
2565 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002566
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002567 if (NO_PRECISION == precision)
2568 {
2569 length = INT_MAX;
2570 }
2571 else
2572 {
2573 length = precision;
2574 width -= length;
2575 }
2576
2577 if (flags & FLAGS_QUOTE)
2578 self->OutStream(self, CHAR_QUOTE);
2579
2580 if (! (flags & FLAGS_LEFTADJUST))
2581 {
2582 while (width-- > 0)
2583 self->OutStream(self, CHAR_ADJUST);
2584 }
2585
2586 while (length > 0)
2587 {
2588 size = TrioWriteWideStringCharacter(self, *wstring++, flags, length);
2589 if (size == 0)
2590 break; /* while */
2591 length -= size;
2592 }
2593
2594 if (flags & FLAGS_LEFTADJUST)
2595 {
2596 while (width-- > 0)
2597 self->OutStream(self, CHAR_ADJUST);
2598 }
2599 if (flags & FLAGS_QUOTE)
2600 self->OutStream(self, CHAR_QUOTE);
2601}
2602#endif /* TRIO_WIDECHAR */
2603
2604/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00002605 * TrioWriteDouble
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002606 *
2607 * http://wwwold.dkuug.dk/JTC1/SC22/WG14/www/docs/dr_211.htm
2608 *
2609 * "5.2.4.2.2 paragraph #4
2610 *
2611 * The accuracy [...] is implementation defined, as is the accuracy
2612 * of the conversion between floating-point internal representations
2613 * and string representations performed by the libray routine in
2614 * <stdio.h>"
2615 */
2616/* FIXME: handle all instances of constant long-double number (L)
2617 * and *l() math functions.
Daniel Veillard92ad2102001-03-27 12:47:33 +00002618 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00002619TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002620TrioWriteDouble
2621TRIO_ARGS6((self, number, flags, width, precision, base),
2622 trio_class_t *self,
2623 trio_long_double_t number,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002624 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002625 int width,
2626 int precision,
2627 int base)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002628{
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002629 trio_long_double_t integerNumber;
2630 trio_long_double_t fractionNumber;
2631 trio_long_double_t workNumber;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002632 int integerDigits;
2633 int fractionDigits;
2634 int exponentDigits;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002635 int baseDigits;
2636 int integerThreshold;
2637 int fractionThreshold;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002638 int expectedWidth;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002639 int exponent = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00002640 unsigned int uExponent = 0;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002641 int exponentBase;
2642 trio_long_double_t dblBase;
2643 trio_long_double_t dblIntegerBase;
2644 trio_long_double_t dblFractionBase;
2645 trio_long_double_t integerAdjust;
2646 trio_long_double_t fractionAdjust;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002647 BOOLEAN_T isNegative;
2648 BOOLEAN_T isExponentNegative = FALSE;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002649 BOOLEAN_T requireTwoDigitExponent;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002650 BOOLEAN_T isHex;
Bjorn Reese026d29f2002-01-19 15:40:18 +00002651 TRIO_CONST char *digits;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002652 char *groupingPointer;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002653 int i;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002654 int index;
2655 BOOLEAN_T hasOnlyZeroes;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002656 int zeroes = 0;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002657 register int trailingZeroes;
2658 BOOLEAN_T keepTrailingZeroes;
2659 BOOLEAN_T keepDecimalPoint;
2660 trio_long_double_t epsilon;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002661
Daniel Veillard92ad2102001-03-27 12:47:33 +00002662 assert(VALID(self));
2663 assert(VALID(self->OutStream));
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002664 assert(((base >= MIN_BASE) && (base <= MAX_BASE)) || (base == NO_BASE));
Daniel Veillard92ad2102001-03-27 12:47:33 +00002665
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002666 /* Determine sign and look for special quantities */
2667 switch (trio_fpclassify_and_signbit(number, &isNegative))
Daniel Veillard92ad2102001-03-27 12:47:33 +00002668 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002669 case TRIO_FP_NAN:
Daniel Veillard92ad2102001-03-27 12:47:33 +00002670 TrioWriteString(self,
2671 (flags & FLAGS_UPPER)
2672 ? NAN_UPPER
2673 : NAN_LOWER,
Bjorn Reese70a9da52001-04-21 16:57:29 +00002674 flags, width, precision);
Daniel Veillard92ad2102001-03-27 12:47:33 +00002675 return;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002676
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002677 case TRIO_FP_INFINITE:
2678 if (isNegative)
2679 {
2680 /* Negative infinity */
2681 TrioWriteString(self,
2682 (flags & FLAGS_UPPER)
2683 ? "-" INFINITE_UPPER
2684 : "-" INFINITE_LOWER,
2685 flags, width, precision);
2686 return;
2687 }
2688 else
2689 {
2690 /* Positive infinity */
2691 TrioWriteString(self,
2692 (flags & FLAGS_UPPER)
2693 ? INFINITE_UPPER
2694 : INFINITE_LOWER,
2695 flags, width, precision);
2696 return;
2697 }
2698
2699 default:
2700 /* Finitude */
2701 break;
2702 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002703
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002704 /* Normal numbers */
2705 if (flags & FLAGS_LONGDOUBLE)
2706 {
2707 baseDigits = (base == 10)
2708 ? LDBL_DIG
2709 : (int)floor(LDBL_MANT_DIG / TrioLogarithmBase(base));
2710 epsilon = LDBL_EPSILON;
2711 }
2712 else if (flags & FLAGS_SHORT)
2713 {
2714 baseDigits = (base == BASE_DECIMAL)
2715 ? FLT_DIG
2716 : (int)floor(FLT_MANT_DIG / TrioLogarithmBase(base));
2717 epsilon = FLT_EPSILON;
2718 }
2719 else
2720 {
2721 baseDigits = (base == BASE_DECIMAL)
2722 ? DBL_DIG
2723 : (int)floor(DBL_MANT_DIG / TrioLogarithmBase(base));
2724 epsilon = DBL_EPSILON;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002725 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002726
Bjorn Reese70a9da52001-04-21 16:57:29 +00002727 digits = (flags & FLAGS_UPPER) ? internalDigitsUpper : internalDigitsLower;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002728 isHex = (base == BASE_HEX);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002729 if (base == NO_BASE)
2730 base = BASE_DECIMAL;
2731 dblBase = (trio_long_double_t)base;
2732 keepTrailingZeroes = !( (flags & FLAGS_ROUNDING) ||
2733 ( (flags & FLAGS_FLOAT_G) &&
2734 !(flags & FLAGS_ALTERNATIVE) ) );
2735
2736 if (flags & FLAGS_ROUNDING)
2737 precision = baseDigits;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002738
Daniel Veillard92ad2102001-03-27 12:47:33 +00002739 if (precision == NO_PRECISION)
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002740 {
2741 if (isHex)
2742 {
2743 keepTrailingZeroes = FALSE;
2744 precision = FLT_MANT_DIG;
2745 }
2746 else
2747 {
2748 precision = FLT_DIG;
2749 }
2750 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002751
Daniel Veillard92ad2102001-03-27 12:47:33 +00002752 if (isNegative)
2753 number = -number;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002754
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002755 if (isHex)
2756 flags |= FLAGS_FLOAT_E;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002757
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002758 if (flags & FLAGS_FLOAT_G)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002759 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00002760 if (precision == 0)
2761 precision = 1;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002762
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002763 if ((number < 1.0E-4) || (number > powl(base,
2764 (trio_long_double_t)precision)))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002765 {
2766 /* Use scientific notation */
2767 flags |= FLAGS_FLOAT_E;
2768 }
2769 else if (number < 1.0)
2770 {
2771 /*
2772 * Use normal notation. If the integer part of the number is
2773 * zero, then adjust the precision to include leading fractional
2774 * zeros.
2775 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002776 workNumber = TrioLogarithm(number, base);
2777 workNumber = TRIO_FABS(workNumber);
2778 if (workNumber - floorl(workNumber) < 0.001)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002779 workNumber--;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002780 zeroes = (int)floorl(workNumber);
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002781 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002782 }
2783
2784 if (flags & FLAGS_FLOAT_E)
2785 {
2786 /* Scale the number */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002787 workNumber = TrioLogarithm(number, base);
Bjorn Reese45029602001-08-21 09:23:53 +00002788 if (trio_isinf(workNumber) == -1)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002789 {
2790 exponent = 0;
2791 /* Undo setting */
2792 if (flags & FLAGS_FLOAT_G)
2793 flags &= ~FLAGS_FLOAT_E;
2794 }
2795 else
2796 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002797 exponent = (int)floorl(workNumber);
2798 number /= powl(dblBase, (trio_long_double_t)exponent);
Daniel Veillard92ad2102001-03-27 12:47:33 +00002799 isExponentNegative = (exponent < 0);
2800 uExponent = (isExponentNegative) ? -exponent : exponent;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002801 if (isHex)
2802 uExponent *= 4; /* log16(2) */
Daniel Veillard92ad2102001-03-27 12:47:33 +00002803 /* No thousand separators */
2804 flags &= ~FLAGS_QUOTE;
2805 }
2806 }
2807
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002808 integerNumber = floorl(number);
2809 fractionNumber = number - integerNumber;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002810
Daniel Veillard92ad2102001-03-27 12:47:33 +00002811 /*
2812 * Truncated number.
2813 *
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002814 * Precision is number of significant digits for FLOAT_G
2815 * and number of fractional digits for others.
Daniel Veillard92ad2102001-03-27 12:47:33 +00002816 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002817 integerDigits = (integerNumber > epsilon)
2818 ? 1 + (int)TrioLogarithm(integerNumber, base)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002819 : 1;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002820 fractionDigits = ((flags & FLAGS_FLOAT_G) && (zeroes == 0))
Daniel Veillard92ad2102001-03-27 12:47:33 +00002821 ? precision - integerDigits
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002822 : zeroes + precision;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002823
2824 dblFractionBase = TrioPower(base, fractionDigits);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002825
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002826 workNumber = number + 0.5 / dblFractionBase;
2827 if (floorl(number) != floorl(workNumber))
Daniel Veillard92ad2102001-03-27 12:47:33 +00002828 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002829 if (flags & FLAGS_FLOAT_E)
2830 {
2831 /* Adjust if number was rounded up one digit (ie. 0.99 to 1.00) */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002832 exponent++;
2833 isExponentNegative = (exponent < 0);
2834 uExponent = (isExponentNegative) ? -exponent : exponent;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002835 if (isHex)
2836 uExponent *= 4; /* log16(2) */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002837 workNumber = (number + 0.5 / dblFractionBase) / dblBase;
2838 integerNumber = floorl(workNumber);
2839 fractionNumber = workNumber - integerNumber;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002840 }
2841 else
2842 {
2843 /* Adjust if number was rounded up one digit (ie. 99 to 100) */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002844 integerNumber = floorl(number + 0.5);
2845 fractionNumber = 0.0;
2846 integerDigits = (integerNumber > epsilon)
2847 ? 1 + (int)TrioLogarithm(integerNumber, base)
2848 : 1;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002849 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002850 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00002851
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002852 /* Estimate accuracy */
2853 integerAdjust = fractionAdjust = 0.5;
2854 if (flags & FLAGS_ROUNDING)
2855 {
2856 if (integerDigits > baseDigits)
2857 {
2858 integerThreshold = baseDigits;
2859 fractionDigits = 0;
2860 dblFractionBase = 1.0;
2861 fractionThreshold = 0;
2862 precision = 0; /* Disable decimal-point */
2863 integerAdjust = TrioPower(base, integerDigits - integerThreshold - 1);
2864 fractionAdjust = 0.0;
2865 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00002866 else
Daniel Veillard92ad2102001-03-27 12:47:33 +00002867 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002868 integerThreshold = integerDigits;
2869 fractionThreshold = fractionDigits - integerThreshold;
2870 fractionAdjust = 1.0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002871 }
2872 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002873 else
Daniel Veillard92ad2102001-03-27 12:47:33 +00002874 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002875 integerThreshold = INT_MAX;
2876 fractionThreshold = INT_MAX;
Daniel Veillard92ad2102001-03-27 12:47:33 +00002877 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002878
Bjorn Reese70a9da52001-04-21 16:57:29 +00002879 /*
2880 * Calculate expected width.
Daniel Veillard92ad2102001-03-27 12:47:33 +00002881 * sign + integer part + thousands separators + decimal point
2882 * + fraction + exponent
2883 */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002884 fractionAdjust /= dblFractionBase;
2885 hasOnlyZeroes = (floorl((fractionNumber + fractionAdjust) * dblFractionBase) < epsilon);
2886 keepDecimalPoint = ( (flags & FLAGS_ALTERNATIVE) ||
2887 !((precision == 0) ||
2888 (!keepTrailingZeroes && hasOnlyZeroes)) );
2889 if (flags & FLAGS_FLOAT_E)
2890 {
2891 exponentDigits = (uExponent == 0)
2892 ? 1
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00002893 : (int)ceil(TrioLogarithm((double)(uExponent + 1),
2894 (isHex) ? 10.0 : base));
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002895 }
2896 else
2897 exponentDigits = 0;
2898 requireTwoDigitExponent = ((base == BASE_DECIMAL) && (exponentDigits == 1));
2899
2900 expectedWidth = integerDigits + fractionDigits
2901 + (keepDecimalPoint
2902 ? internalDecimalPointLength
2903 : 0)
2904 + ((flags & FLAGS_QUOTE)
2905 ? TrioCalcThousandSeparatorLength(integerDigits)
2906 : 0);
2907 if (isNegative || (flags & FLAGS_SHOWSIGN) || (flags & FLAGS_SPACE))
Daniel Veillard92ad2102001-03-27 12:47:33 +00002908 expectedWidth += sizeof("-") - 1;
2909 if (exponentDigits > 0)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00002910 expectedWidth += exponentDigits +
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002911 ((requireTwoDigitExponent ? sizeof("E+0") : sizeof("E+")) - 1);
Daniel Veillard92ad2102001-03-27 12:47:33 +00002912 if (isHex)
2913 expectedWidth += sizeof("0X") - 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002914
Daniel Veillard92ad2102001-03-27 12:47:33 +00002915 /* Output prefixing */
2916 if (flags & FLAGS_NILPADDING)
2917 {
2918 /* Leading zeros must be after sign */
2919 if (isNegative)
2920 self->OutStream(self, '-');
2921 else if (flags & FLAGS_SHOWSIGN)
2922 self->OutStream(self, '+');
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002923 else if (flags & FLAGS_SPACE)
2924 self->OutStream(self, ' ');
Daniel Veillard92ad2102001-03-27 12:47:33 +00002925 if (isHex)
2926 {
2927 self->OutStream(self, '0');
2928 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2929 }
2930 if (!(flags & FLAGS_LEFTADJUST))
2931 {
2932 for (i = expectedWidth; i < width; i++)
2933 {
2934 self->OutStream(self, '0');
2935 }
2936 }
2937 }
2938 else
2939 {
2940 /* Leading spaces must be before sign */
2941 if (!(flags & FLAGS_LEFTADJUST))
2942 {
2943 for (i = expectedWidth; i < width; i++)
2944 {
2945 self->OutStream(self, CHAR_ADJUST);
2946 }
2947 }
2948 if (isNegative)
2949 self->OutStream(self, '-');
2950 else if (flags & FLAGS_SHOWSIGN)
2951 self->OutStream(self, '+');
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002952 else if (flags & FLAGS_SPACE)
2953 self->OutStream(self, ' ');
Daniel Veillard92ad2102001-03-27 12:47:33 +00002954 if (isHex)
2955 {
2956 self->OutStream(self, '0');
2957 self->OutStream(self, (flags & FLAGS_UPPER) ? 'X' : 'x');
2958 }
2959 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002960
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002961 /* Output the integer part and thousand separators */
2962 dblIntegerBase = 1.0 / TrioPower(base, integerDigits - 1);
2963 for (i = 0; i < integerDigits; i++)
Daniel Veillard92ad2102001-03-27 12:47:33 +00002964 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002965 workNumber = floorl(((integerNumber + integerAdjust) * dblIntegerBase));
2966 if (i > integerThreshold)
2967 {
2968 /* Beyond accuracy */
2969 self->OutStream(self, digits[0]);
2970 }
2971 else
2972 {
2973 self->OutStream(self, digits[(int)fmodl(workNumber, dblBase)]);
2974 }
2975 dblIntegerBase *= dblBase;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002976
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002977 if (((flags & (FLAGS_FLOAT_E | FLAGS_QUOTE)) == FLAGS_QUOTE)
2978 && TrioFollowedBySeparator(integerDigits - i))
2979 {
2980 for (groupingPointer = internalThousandSeparator;
2981 *groupingPointer != NIL;
2982 groupingPointer++)
2983 {
2984 self->OutStream(self, *groupingPointer);
2985 }
2986 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00002987 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002988
Daniel Veillardb7c29c32002-09-25 22:44:43 +00002989 /* Insert decimal point and build the fraction part */
2990 trailingZeroes = 0;
2991
2992 if (keepDecimalPoint)
2993 {
2994 if (internalDecimalPoint)
2995 {
2996 self->OutStream(self, internalDecimalPoint);
2997 }
2998 else
2999 {
3000 for (i = 0; i < internalDecimalPointLength; i++)
3001 {
3002 self->OutStream(self, internalDecimalPointString[i]);
3003 }
3004 }
3005 }
3006
3007 for (i = 0; i < fractionDigits; i++)
3008 {
3009 if ((integerDigits > integerThreshold) || (i > fractionThreshold))
3010 {
3011 /* Beyond accuracy */
3012 trailingZeroes++;
3013 }
3014 else
3015 {
3016 fractionNumber *= dblBase;
3017 fractionAdjust *= dblBase;
3018 workNumber = floorl(fractionNumber + fractionAdjust);
3019 fractionNumber -= workNumber;
3020 index = (int)fmodl(workNumber, dblBase);
3021 if (index == 0)
3022 {
3023 trailingZeroes++;
3024 }
3025 else
3026 {
3027 while (trailingZeroes > 0)
3028 {
3029 /* Not trailing zeroes after all */
3030 self->OutStream(self, digits[0]);
3031 trailingZeroes--;
3032 }
3033 self->OutStream(self, digits[index]);
3034 }
3035 }
3036 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003037
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003038 if (keepTrailingZeroes)
3039 {
3040 while (trailingZeroes > 0)
3041 {
3042 self->OutStream(self, digits[0]);
3043 trailingZeroes--;
3044 }
3045 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003046
Daniel Veillard92ad2102001-03-27 12:47:33 +00003047 /* Output exponent */
3048 if (exponentDigits > 0)
3049 {
3050 self->OutStream(self,
3051 isHex
3052 ? ((flags & FLAGS_UPPER) ? 'P' : 'p')
3053 : ((flags & FLAGS_UPPER) ? 'E' : 'e'));
3054 self->OutStream(self, (isExponentNegative) ? '-' : '+');
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003055
3056 /* The exponent must contain at least two digits */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003057 if (requireTwoDigitExponent)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003058 self->OutStream(self, '0');
3059
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00003060 if (isHex)
3061 base = 10.0;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003062 exponentBase = (int)TrioPower(base, exponentDigits - 1);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003063 for (i = 0; i < exponentDigits; i++)
3064 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003065 self->OutStream(self, digits[(uExponent / exponentBase) % base]);
3066 exponentBase /= base;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003067 }
3068 }
3069 /* Output trailing spaces */
3070 if (flags & FLAGS_LEFTADJUST)
3071 {
3072 for (i = expectedWidth; i < width; i++)
3073 {
3074 self->OutStream(self, CHAR_ADJUST);
3075 }
3076 }
3077}
3078
3079/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003080 * TrioFormatProcess
3081 *
3082 * Description:
3083 * This is the main engine for formatting output
Daniel Veillard92ad2102001-03-27 12:47:33 +00003084 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003085TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003086TrioFormatProcess
3087TRIO_ARGS3((data, format, parameters),
3088 trio_class_t *data,
3089 TRIO_CONST char *format,
3090 trio_parameter_t *parameters)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003091{
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003092#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003093 int charlen;
3094#endif
Daniel Veillard92ad2102001-03-27 12:47:33 +00003095 int i;
Bjorn Reese026d29f2002-01-19 15:40:18 +00003096 TRIO_CONST char *string;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003097 trio_pointer_t pointer;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00003098 trio_flags_t flags;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003099 int width;
3100 int precision;
3101 int base;
3102 int index;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003103
Daniel Veillard92ad2102001-03-27 12:47:33 +00003104 index = 0;
3105 i = 0;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003106#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003107 (void)mblen(NULL, 0);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003108#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003109
Daniel Veillard92ad2102001-03-27 12:47:33 +00003110 while (format[index])
3111 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003112#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003113 if (! isascii(format[index]))
3114 {
3115 charlen = mblen(&format[index], MB_LEN_MAX);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003116 /*
3117 * Only valid multibyte characters are handled here. Invalid
3118 * multibyte characters (charlen == -1) are handled as normal
3119 * characters.
3120 */
3121 if (charlen != -1)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003122 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003123 while (charlen-- > 0)
3124 {
3125 data->OutStream(data, format[index++]);
3126 }
3127 continue; /* while characters left in formatting string */
Daniel Veillard92ad2102001-03-27 12:47:33 +00003128 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003129 }
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003130#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
Daniel Veillard92ad2102001-03-27 12:47:33 +00003131 if (CHAR_IDENTIFIER == format[index])
3132 {
3133 if (CHAR_IDENTIFIER == format[index + 1])
3134 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00003135 data->OutStream(data, CHAR_IDENTIFIER);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003136 index += 2;
3137 }
3138 else
3139 {
3140 /* Skip the parameter entries */
3141 while (parameters[i].type == FORMAT_PARAMETER)
3142 i++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003143
Daniel Veillard92ad2102001-03-27 12:47:33 +00003144 flags = parameters[i].flags;
3145
3146 /* Find width */
3147 width = parameters[i].width;
3148 if (flags & FLAGS_WIDTH_PARAMETER)
3149 {
3150 /* Get width from parameter list */
Bjorn Reese70a9da52001-04-21 16:57:29 +00003151 width = (int)parameters[width].data.number.as_signed;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00003152 if (width < 0)
3153 {
3154 /*
3155 * A negative width is the same as the - flag and
3156 * a positive width.
3157 */
3158 flags |= FLAGS_LEFTADJUST;
3159 flags &= ~FLAGS_NILPADDING;
3160 width = -width;
3161 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003162 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003163
Daniel Veillard92ad2102001-03-27 12:47:33 +00003164 /* Find precision */
3165 if (flags & FLAGS_PRECISION)
3166 {
3167 precision = parameters[i].precision;
3168 if (flags & FLAGS_PRECISION_PARAMETER)
3169 {
3170 /* Get precision from parameter list */
Bjorn Reese70a9da52001-04-21 16:57:29 +00003171 precision = (int)parameters[precision].data.number.as_signed;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00003172 if (precision < 0)
3173 {
3174 /*
3175 * A negative precision is the same as no
3176 * precision
3177 */
3178 precision = NO_PRECISION;
3179 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003180 }
3181 }
3182 else
3183 {
3184 precision = NO_PRECISION;
3185 }
3186
3187 /* Find base */
3188 base = parameters[i].base;
3189 if (flags & FLAGS_BASE_PARAMETER)
3190 {
3191 /* Get base from parameter list */
Bjorn Reese70a9da52001-04-21 16:57:29 +00003192 base = (int)parameters[base].data.number.as_signed;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003193 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003194
Daniel Veillard92ad2102001-03-27 12:47:33 +00003195 switch (parameters[i].type)
3196 {
3197 case FORMAT_CHAR:
3198 if (flags & FLAGS_QUOTE)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003199 data->OutStream(data, CHAR_QUOTE);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003200 if (! (flags & FLAGS_LEFTADJUST))
3201 {
3202 while (--width > 0)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003203 data->OutStream(data, CHAR_ADJUST);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003204 }
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003205#if TRIO_WIDECHAR
3206 if (flags & FLAGS_WIDECHAR)
3207 {
3208 TrioWriteWideStringCharacter(data,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003209 (trio_wchar_t)parameters[i].data.number.as_signed,
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003210 flags,
3211 NO_WIDTH);
3212 }
3213 else
3214#endif
Bjorn Reese026d29f2002-01-19 15:40:18 +00003215 {
3216 TrioWriteStringCharacter(data,
3217 (int)parameters[i].data.number.as_signed,
3218 flags);
3219 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003220
3221 if (flags & FLAGS_LEFTADJUST)
3222 {
3223 while(--width > 0)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003224 data->OutStream(data, CHAR_ADJUST);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003225 }
3226 if (flags & FLAGS_QUOTE)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003227 data->OutStream(data, CHAR_QUOTE);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003228
3229 break; /* FORMAT_CHAR */
3230
3231 case FORMAT_INT:
Daniel Veillard92ad2102001-03-27 12:47:33 +00003232 TrioWriteNumber(data,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003233 parameters[i].data.number.as_unsigned,
Daniel Veillard92ad2102001-03-27 12:47:33 +00003234 flags,
3235 width,
3236 precision,
3237 base);
3238
3239 break; /* FORMAT_INT */
3240
3241 case FORMAT_DOUBLE:
3242 TrioWriteDouble(data,
3243 parameters[i].data.longdoubleNumber,
3244 flags,
3245 width,
3246 precision,
3247 base);
3248 break; /* FORMAT_DOUBLE */
3249
3250 case FORMAT_STRING:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003251#if TRIO_WIDECHAR
3252 if (flags & FLAGS_WIDECHAR)
3253 {
3254 TrioWriteWideString(data,
3255 parameters[i].data.wstring,
3256 flags,
3257 width,
3258 precision);
3259 }
3260 else
3261#endif
3262 {
3263 TrioWriteString(data,
3264 parameters[i].data.string,
3265 flags,
3266 width,
3267 precision);
3268 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003269 break; /* FORMAT_STRING */
3270
3271 case FORMAT_POINTER:
Bjorn Reese70a9da52001-04-21 16:57:29 +00003272 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00003273 trio_reference_t reference;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003274
Bjorn Reese70a9da52001-04-21 16:57:29 +00003275 reference.data = data;
3276 reference.parameter = &parameters[i];
3277 trio_print_pointer(&reference, parameters[i].data.pointer);
3278 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003279 break; /* FORMAT_POINTER */
3280
3281 case FORMAT_COUNT:
3282 pointer = parameters[i].data.pointer;
3283 if (NULL != pointer)
3284 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00003285 /*
3286 * C99 paragraph 7.19.6.1.8 says "the number of
Daniel Veillard92ad2102001-03-27 12:47:33 +00003287 * characters written to the output stream so far by
3288 * this call", which is data->committed
3289 */
3290#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
3291 if (flags & FLAGS_SIZE_T)
3292 *(size_t *)pointer = (size_t)data->committed;
3293 else
3294#endif
3295#if defined(QUALIFIER_PTRDIFF_T)
3296 if (flags & FLAGS_PTRDIFF_T)
3297 *(ptrdiff_t *)pointer = (ptrdiff_t)data->committed;
3298 else
3299#endif
3300#if defined(QUALIFIER_INTMAX_T)
3301 if (flags & FLAGS_INTMAX_T)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003302 *(trio_intmax_t *)pointer = (trio_intmax_t)data->committed;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003303 else
3304#endif
3305 if (flags & FLAGS_QUAD)
3306 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003307 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)data->committed;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003308 }
3309 else if (flags & FLAGS_LONG)
3310 {
3311 *(long int *)pointer = (long int)data->committed;
3312 }
3313 else if (flags & FLAGS_SHORT)
3314 {
3315 *(short int *)pointer = (short int)data->committed;
3316 }
3317 else
3318 {
3319 *(int *)pointer = (int)data->committed;
3320 }
3321 }
3322 break; /* FORMAT_COUNT */
3323
3324 case FORMAT_PARAMETER:
3325 break; /* FORMAT_PARAMETER */
3326
3327#if defined(FORMAT_ERRNO)
3328 case FORMAT_ERRNO:
Bjorn Reese026d29f2002-01-19 15:40:18 +00003329 string = trio_error(parameters[i].data.errorNumber);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003330 if (string)
3331 {
3332 TrioWriteString(data,
3333 string,
3334 flags,
3335 width,
3336 precision);
3337 }
3338 else
3339 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00003340 data->OutStream(data, '#');
Daniel Veillard92ad2102001-03-27 12:47:33 +00003341 TrioWriteNumber(data,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003342 (trio_uintmax_t)parameters[i].data.errorNumber,
Daniel Veillard92ad2102001-03-27 12:47:33 +00003343 flags,
3344 width,
3345 precision,
3346 BASE_DECIMAL);
3347 }
3348 break; /* FORMAT_ERRNO */
Bjorn Reese70a9da52001-04-21 16:57:29 +00003349#endif /* defined(FORMAT_ERRNO) */
Daniel Veillard92ad2102001-03-27 12:47:33 +00003350
Bjorn Reese70a9da52001-04-21 16:57:29 +00003351#if defined(FORMAT_USER_DEFINED)
3352 case FORMAT_USER_DEFINED:
3353 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00003354 trio_reference_t reference;
3355 trio_userdef_t *def = NULL;
Bjorn Reese70a9da52001-04-21 16:57:29 +00003356
3357 if (parameters[i].user_name[0] == NIL)
3358 {
3359 /* Use handle */
3360 if ((i > 0) ||
3361 (parameters[i - 1].type == FORMAT_PARAMETER))
Bjorn Reese026d29f2002-01-19 15:40:18 +00003362 def = (trio_userdef_t *)parameters[i - 1].data.pointer;
Bjorn Reese70a9da52001-04-21 16:57:29 +00003363 }
3364 else
3365 {
3366 /* Look up namespace */
3367 def = TrioFindNamespace(parameters[i].user_name, NULL);
3368 }
3369 if (def) {
3370 reference.data = data;
3371 reference.parameter = &parameters[i];
3372 def->callback(&reference);
3373 }
3374 }
3375 break;
3376#endif /* defined(FORMAT_USER_DEFINED) */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003377
Daniel Veillard92ad2102001-03-27 12:47:33 +00003378 default:
3379 break;
3380 } /* switch parameter type */
3381
3382 /* Prepare for next */
3383 index = parameters[i].indexAfterSpecifier;
3384 i++;
3385 }
3386 }
3387 else /* not identifier */
3388 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00003389 data->OutStream(data, format[index++]);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003390 }
3391 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003392 return data->processed;
3393}
3394
3395/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003396 * TrioFormatRef
Bjorn Reese70a9da52001-04-21 16:57:29 +00003397 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003398TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003399TrioFormatRef
3400TRIO_ARGS4((reference, format, arglist, argarray),
3401 trio_reference_t *reference,
3402 TRIO_CONST char *format,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003403 TRIO_VA_LIST_PTR arglist,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003404 trio_pointer_t *argarray)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003405{
3406 int status;
Bjorn Reese026d29f2002-01-19 15:40:18 +00003407 trio_parameter_t parameters[MAX_PARAMETERS];
Bjorn Reese70a9da52001-04-21 16:57:29 +00003408
Bjorn Reese026d29f2002-01-19 15:40:18 +00003409 status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003410 if (status < 0)
3411 return status;
3412
Bjorn Reese026d29f2002-01-19 15:40:18 +00003413 status = TrioFormatProcess(reference->data, format, parameters);
3414 if (reference->data->error != 0)
3415 {
3416 status = reference->data->error;
3417 }
3418 return status;
Bjorn Reese70a9da52001-04-21 16:57:29 +00003419}
3420
3421/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003422 * TrioFormat
Bjorn Reese70a9da52001-04-21 16:57:29 +00003423 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003424TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003425TrioFormat
3426TRIO_ARGS6((destination, destinationSize, OutStream, format, arglist, argarray),
3427 trio_pointer_t destination,
Bjorn Reese70a9da52001-04-21 16:57:29 +00003428 size_t destinationSize,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003429 void (*OutStream) TRIO_PROTO((trio_class_t *, int)),
Bjorn Reese026d29f2002-01-19 15:40:18 +00003430 TRIO_CONST char *format,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003431 TRIO_VA_LIST_PTR arglist,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003432 trio_pointer_t *argarray)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003433{
3434 int status;
Bjorn Reese026d29f2002-01-19 15:40:18 +00003435 trio_class_t data;
3436 trio_parameter_t parameters[MAX_PARAMETERS];
Bjorn Reese70a9da52001-04-21 16:57:29 +00003437
3438 assert(VALID(OutStream));
3439 assert(VALID(format));
Bjorn Reese70a9da52001-04-21 16:57:29 +00003440
3441 memset(&data, 0, sizeof(data));
3442 data.OutStream = OutStream;
3443 data.location = destination;
3444 data.max = destinationSize;
Bjorn Reese026d29f2002-01-19 15:40:18 +00003445 data.error = 0;
Bjorn Reese70a9da52001-04-21 16:57:29 +00003446
3447#if defined(USE_LOCALE)
3448 if (NULL == internalLocaleValues)
3449 {
3450 TrioSetLocale();
3451 }
3452#endif
3453
Bjorn Reese026d29f2002-01-19 15:40:18 +00003454 status = TrioParse(TYPE_PRINT, format, parameters, arglist, argarray);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003455 if (status < 0)
3456 return status;
3457
Bjorn Reese026d29f2002-01-19 15:40:18 +00003458 status = TrioFormatProcess(&data, format, parameters);
3459 if (data.error != 0)
3460 {
3461 status = data.error;
3462 }
3463 return status;
Bjorn Reese70a9da52001-04-21 16:57:29 +00003464}
3465
3466/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003467 * TrioOutStreamFile
Daniel Veillard92ad2102001-03-27 12:47:33 +00003468 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003469TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003470TrioOutStreamFile
3471TRIO_ARGS2((self, output),
3472 trio_class_t *self,
3473 int output)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003474{
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003475 FILE *file;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003476
3477 assert(VALID(self));
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003478 assert(VALID(self->location));
Daniel Veillard92ad2102001-03-27 12:47:33 +00003479
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003480 file = (FILE *)self->location;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003481 self->processed++;
Bjorn Reese026d29f2002-01-19 15:40:18 +00003482 if (fputc(output, file) == EOF)
3483 {
3484 self->error = TRIO_ERROR_RETURN(TRIO_EOF, 0);
3485 }
3486 else
3487 {
3488 self->committed++;
3489 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003490}
3491
3492/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003493 * TrioOutStreamFileDescriptor
Daniel Veillard92ad2102001-03-27 12:47:33 +00003494 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003495TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003496TrioOutStreamFileDescriptor
3497TRIO_ARGS2((self, output),
3498 trio_class_t *self,
3499 int output)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003500{
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003501 int fd;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003502 char ch;
3503
3504 assert(VALID(self));
3505
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003506 fd = *((int *)self->location);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003507 ch = (char)output;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003508 self->processed++;
Bjorn Reese026d29f2002-01-19 15:40:18 +00003509 if (write(fd, &ch, sizeof(char)) == -1)
3510 {
3511 self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
3512 }
3513 else
3514 {
3515 self->committed++;
3516 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00003517}
3518
3519/*************************************************************************
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003520 * TrioOutStreamCustom
3521 */
3522TRIO_PRIVATE void
3523TrioOutStreamCustom
3524TRIO_ARGS2((self, output),
3525 trio_class_t *self,
3526 int output)
3527{
3528 int status;
3529 trio_custom_t *data;
3530
3531 assert(VALID(self));
3532 assert(VALID(self->location));
3533
3534 data = (trio_custom_t *)self->location;
3535 if (data->stream.out)
3536 {
3537 status = (data->stream.out)(data->closure, output);
3538 if (status >= 0)
3539 {
3540 self->committed++;
3541 }
3542 else
3543 {
3544 if (self->error == 0)
3545 {
3546 self->error = TRIO_ERROR_RETURN(TRIO_ECUSTOM, -status);
3547 }
3548 }
3549 }
3550 self->processed++;
3551}
3552
3553/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003554 * TrioOutStreamString
Daniel Veillard92ad2102001-03-27 12:47:33 +00003555 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003556TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003557TrioOutStreamString
3558TRIO_ARGS2((self, output),
3559 trio_class_t *self,
3560 int output)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003561{
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003562 char **buffer;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003563
3564 assert(VALID(self));
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003565 assert(VALID(self->location));
Daniel Veillard92ad2102001-03-27 12:47:33 +00003566
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003567 buffer = (char **)self->location;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003568 **buffer = (char)output;
3569 (*buffer)++;
3570 self->processed++;
3571 self->committed++;
3572}
3573
3574/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003575 * TrioOutStreamStringMax
Daniel Veillard92ad2102001-03-27 12:47:33 +00003576 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003577TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003578TrioOutStreamStringMax
3579TRIO_ARGS2((self, output),
3580 trio_class_t *self,
3581 int output)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003582{
3583 char **buffer;
3584
3585 assert(VALID(self));
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003586 assert(VALID(self->location));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003587
Daniel Veillard92ad2102001-03-27 12:47:33 +00003588 buffer = (char **)self->location;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003589
3590 if (self->processed < self->max)
3591 {
3592 **buffer = (char)output;
3593 (*buffer)++;
3594 self->committed++;
3595 }
3596 self->processed++;
3597}
3598
3599/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003600 * TrioOutStreamStringDynamic
Daniel Veillard92ad2102001-03-27 12:47:33 +00003601 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003602TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003603TrioOutStreamStringDynamic
3604TRIO_ARGS2((self, output),
3605 trio_class_t *self,
3606 int output)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003607{
Daniel Veillard92ad2102001-03-27 12:47:33 +00003608 assert(VALID(self));
3609 assert(VALID(self->location));
3610
Bjorn Reese026d29f2002-01-19 15:40:18 +00003611 if (self->error == 0)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003612 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00003613 trio_xstring_append_char((trio_string_t *)self->location,
3614 (char)output);
3615 self->committed++;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003616 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00003617 /* The processed variable must always be increased */
Daniel Veillard92ad2102001-03-27 12:47:33 +00003618 self->processed++;
Daniel Veillard92ad2102001-03-27 12:47:33 +00003619}
3620
Daniel Veillard92ad2102001-03-27 12:47:33 +00003621/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00003622 *
3623 * Formatted printing functions
3624 *
3625 ************************************************************************/
3626
3627#if defined(TRIO_DOCUMENTATION)
3628# include "doc/doc_printf.h"
3629#endif
3630/** @addtogroup Printf
3631 @{
3632*/
3633
3634/*************************************************************************
Bjorn Reese70a9da52001-04-21 16:57:29 +00003635 * printf
Daniel Veillard92ad2102001-03-27 12:47:33 +00003636 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003637
3638/**
3639 Print to standard output stream.
3640
3641 @param format Formatting string.
3642 @param ... Arguments.
3643 @return Number of printed characters.
3644 */
3645TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003646trio_printf
3647TRIO_VARGS2((format, va_alist),
3648 TRIO_CONST char *format,
3649 TRIO_VA_DECL)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003650{
3651 int status;
3652 va_list args;
3653
3654 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003655
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003656 TRIO_VA_START(args, format);
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003657 status = TrioFormat(stdout, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003658 TRIO_VA_END(args);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003659 return status;
3660}
Daniel Veillard92ad2102001-03-27 12:47:33 +00003661
Bjorn Reese026d29f2002-01-19 15:40:18 +00003662/**
3663 Print to standard output stream.
3664
3665 @param format Formatting string.
3666 @param args Arguments.
3667 @return Number of printed characters.
3668 */
3669TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003670trio_vprintf
3671TRIO_ARGS2((format, args),
3672 TRIO_CONST char *format,
3673 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003674{
3675 assert(VALID(format));
Bjorn Reese70a9da52001-04-21 16:57:29 +00003676
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003677 return TrioFormat(stdout, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003678}
3679
Bjorn Reese026d29f2002-01-19 15:40:18 +00003680/**
3681 Print to standard output stream.
3682
3683 @param format Formatting string.
3684 @param args Arguments.
3685 @return Number of printed characters.
3686 */
3687TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003688trio_printfv
3689TRIO_ARGS2((format, args),
3690 TRIO_CONST char *format,
3691 trio_pointer_t * args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003692{
3693 assert(VALID(format));
Bjorn Reese70a9da52001-04-21 16:57:29 +00003694
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003695 return TrioFormat(stdout, 0, TrioOutStreamFile, format, NULL, args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003696}
3697
Daniel Veillard92ad2102001-03-27 12:47:33 +00003698/*************************************************************************
Bjorn Reese70a9da52001-04-21 16:57:29 +00003699 * fprintf
Daniel Veillard92ad2102001-03-27 12:47:33 +00003700 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003701
3702/**
3703 Print to file.
3704
3705 @param file File pointer.
3706 @param format Formatting string.
3707 @param ... Arguments.
3708 @return Number of printed characters.
3709 */
3710TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003711trio_fprintf
3712TRIO_VARGS3((file, format, va_alist),
3713 FILE *file,
3714 TRIO_CONST char *format,
3715 TRIO_VA_DECL)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003716{
3717 int status;
3718 va_list args;
3719
3720 assert(VALID(file));
3721 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003722
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003723 TRIO_VA_START(args, format);
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003724 status = TrioFormat(file, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003725 TRIO_VA_END(args);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003726 return status;
3727}
Daniel Veillard92ad2102001-03-27 12:47:33 +00003728
Bjorn Reese026d29f2002-01-19 15:40:18 +00003729/**
3730 Print to file.
3731
3732 @param file File pointer.
3733 @param format Formatting string.
3734 @param args Arguments.
3735 @return Number of printed characters.
3736 */
3737TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003738trio_vfprintf
3739TRIO_ARGS3((file, format, args),
3740 FILE *file,
3741 TRIO_CONST char *format,
3742 va_list args)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003743{
3744 assert(VALID(file));
3745 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003746
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003747 return TrioFormat(file, 0, TrioOutStreamFile, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003748}
Daniel Veillard92ad2102001-03-27 12:47:33 +00003749
Bjorn Reese026d29f2002-01-19 15:40:18 +00003750/**
3751 Print to file.
3752
3753 @param file File pointer.
3754 @param format Formatting string.
3755 @param args Arguments.
3756 @return Number of printed characters.
3757 */
3758TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003759trio_fprintfv
3760TRIO_ARGS3((file, format, args),
3761 FILE *file,
3762 TRIO_CONST char *format,
3763 trio_pointer_t * args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003764{
3765 assert(VALID(file));
3766 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003767
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003768 return TrioFormat(file, 0, TrioOutStreamFile, format, NULL, args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003769}
3770
Daniel Veillard92ad2102001-03-27 12:47:33 +00003771/*************************************************************************
Bjorn Reese70a9da52001-04-21 16:57:29 +00003772 * dprintf
Daniel Veillard92ad2102001-03-27 12:47:33 +00003773 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003774
3775/**
3776 Print to file descriptor.
3777
3778 @param fd File descriptor.
3779 @param format Formatting string.
3780 @param ... Arguments.
3781 @return Number of printed characters.
3782 */
3783TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003784trio_dprintf
3785TRIO_VARGS3((fd, format, va_alist),
3786 int fd,
3787 TRIO_CONST char *format,
3788 TRIO_VA_DECL)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003789{
3790 int status;
3791 va_list args;
3792
3793 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003794
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003795 TRIO_VA_START(args, format);
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003796 status = TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003797 TRIO_VA_END(args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003798 return status;
3799}
3800
Bjorn Reese026d29f2002-01-19 15:40:18 +00003801/**
3802 Print to file descriptor.
3803
3804 @param fd File descriptor.
3805 @param format Formatting string.
3806 @param args Arguments.
3807 @return Number of printed characters.
3808 */
3809TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003810trio_vdprintf
3811TRIO_ARGS3((fd, format, args),
3812 int fd,
3813 TRIO_CONST char *format,
3814 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003815{
3816 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003817
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003818 return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003819}
3820
Bjorn Reese026d29f2002-01-19 15:40:18 +00003821/**
3822 Print to file descriptor.
3823
3824 @param fd File descriptor.
3825 @param format Formatting string.
3826 @param args Arguments.
3827 @return Number of printed characters.
3828 */
3829TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003830trio_dprintfv
3831TRIO_ARGS3((fd, format, args),
3832 int fd,
3833 TRIO_CONST char *format,
3834 trio_pointer_t *args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003835{
Bjorn Reese906ec8a2001-06-05 12:46:33 +00003836 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003837
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003838 return TrioFormat(&fd, 0, TrioOutStreamFileDescriptor, format, NULL, args);
3839}
3840
3841/*************************************************************************
3842 * cprintf
3843 */
3844TRIO_PUBLIC int
3845trio_cprintf
3846TRIO_VARGS4((stream, closure, format, va_alist),
3847 trio_outstream_t stream,
3848 trio_pointer_t closure,
3849 TRIO_CONST char *format,
3850 TRIO_VA_DECL)
3851{
3852 int status;
3853 va_list args;
3854 trio_custom_t data;
3855
3856 assert(VALID(stream));
3857 assert(VALID(format));
3858
3859 TRIO_VA_START(args, format);
3860 data.stream.out = stream;
3861 data.closure = closure;
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003862 status = TrioFormat(&data, 0, TrioOutStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003863 TRIO_VA_END(args);
3864 return status;
3865}
3866
3867TRIO_PUBLIC int
3868trio_vcprintf
3869TRIO_ARGS4((stream, closure, format, args),
3870 trio_outstream_t stream,
3871 trio_pointer_t closure,
3872 TRIO_CONST char *format,
3873 va_list args)
3874{
3875 trio_custom_t data;
3876
3877 assert(VALID(stream));
3878 assert(VALID(format));
3879
3880 data.stream.out = stream;
3881 data.closure = closure;
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003882 return TrioFormat(&data, 0, TrioOutStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003883}
3884
3885TRIO_PUBLIC int
3886trio_cprintfv
3887TRIO_ARGS4((stream, closure, format, args),
3888 trio_outstream_t stream,
3889 trio_pointer_t closure,
3890 TRIO_CONST char *format,
3891 void **args)
3892{
3893 trio_custom_t data;
3894
3895 assert(VALID(stream));
3896 assert(VALID(format));
3897
3898 data.stream.out = stream;
3899 data.closure = closure;
3900 return TrioFormat(&data, 0, TrioOutStreamCustom, format, NULL, args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003901}
3902
3903/*************************************************************************
3904 * sprintf
3905 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003906
3907/**
3908 Print to string.
3909
3910 @param buffer Output string.
3911 @param format Formatting string.
3912 @param ... Arguments.
3913 @return Number of printed characters.
3914 */
3915TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003916trio_sprintf
3917TRIO_VARGS3((buffer, format, va_alist),
3918 char *buffer,
3919 TRIO_CONST char *format,
3920 TRIO_VA_DECL)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003921{
3922 int status;
3923 va_list args;
3924
3925 assert(VALID(buffer));
3926 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003927
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003928 TRIO_VA_START(args, format);
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003929 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003930 *buffer = NIL; /* Terminate with NIL character */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003931 TRIO_VA_END(args);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003932 return status;
3933}
Daniel Veillard92ad2102001-03-27 12:47:33 +00003934
Bjorn Reese026d29f2002-01-19 15:40:18 +00003935/**
3936 Print to string.
3937
3938 @param buffer Output string.
3939 @param format Formatting string.
3940 @param args Arguments.
3941 @return Number of printed characters.
3942 */
3943TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003944trio_vsprintf
3945TRIO_ARGS3((buffer, format, args),
3946 char *buffer,
3947 TRIO_CONST char *format,
3948 va_list args)
Daniel Veillard92ad2102001-03-27 12:47:33 +00003949{
3950 int status;
3951
3952 assert(VALID(buffer));
3953 assert(VALID(format));
Daniel Veillard92ad2102001-03-27 12:47:33 +00003954
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08003955 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00003956 *buffer = NIL;
3957 return status;
3958}
Daniel Veillard92ad2102001-03-27 12:47:33 +00003959
Bjorn Reese026d29f2002-01-19 15:40:18 +00003960/**
3961 Print to string.
3962
3963 @param buffer Output string.
3964 @param format Formatting string.
3965 @param args Arguments.
3966 @return Number of printed characters.
3967 */
3968TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003969trio_sprintfv
3970TRIO_ARGS3((buffer, format, args),
3971 char *buffer,
3972 TRIO_CONST char *format,
3973 trio_pointer_t *args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00003974{
3975 int status;
3976
3977 assert(VALID(buffer));
3978 assert(VALID(format));
Bjorn Reese70a9da52001-04-21 16:57:29 +00003979
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003980 status = TrioFormat(&buffer, 0, TrioOutStreamString, format, NULL, args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00003981 *buffer = NIL;
3982 return status;
3983}
3984
Daniel Veillard92ad2102001-03-27 12:47:33 +00003985/*************************************************************************
Bjorn Reese70a9da52001-04-21 16:57:29 +00003986 * snprintf
Daniel Veillard92ad2102001-03-27 12:47:33 +00003987 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00003988
3989/**
3990 Print at most @p max characters to string.
3991
3992 @param buffer Output string.
3993 @param max Maximum number of characters to print.
3994 @param format Formatting string.
3995 @param ... Arguments.
3996 @return Number of printed characters.
3997 */
3998TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00003999trio_snprintf
4000TRIO_VARGS4((buffer, max, format, va_alist),
4001 char *buffer,
4002 size_t max,
4003 TRIO_CONST char *format,
4004 TRIO_VA_DECL)
Daniel Veillard92ad2102001-03-27 12:47:33 +00004005{
4006 int status;
4007 va_list args;
4008
4009 assert(VALID(buffer));
4010 assert(VALID(format));
4011
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004012 TRIO_VA_START(args, format);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004013 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004014 TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004015 if (max > 0)
Daniel Veillard92ad2102001-03-27 12:47:33 +00004016 *buffer = NIL;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004017 TRIO_VA_END(args);
Daniel Veillard92ad2102001-03-27 12:47:33 +00004018 return status;
4019}
Daniel Veillard92ad2102001-03-27 12:47:33 +00004020
Bjorn Reese026d29f2002-01-19 15:40:18 +00004021/**
4022 Print at most @p max characters to string.
4023
4024 @param buffer Output string.
4025 @param max Maximum number of characters to print.
4026 @param format Formatting string.
4027 @param args Arguments.
4028 @return Number of printed characters.
4029 */
4030TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004031trio_vsnprintf
4032TRIO_ARGS4((buffer, max, format, args),
4033 char *buffer,
4034 size_t max,
4035 TRIO_CONST char *format,
4036 va_list args)
Daniel Veillard92ad2102001-03-27 12:47:33 +00004037{
4038 int status;
4039
4040 assert(VALID(buffer));
4041 assert(VALID(format));
Daniel Veillard92ad2102001-03-27 12:47:33 +00004042
Bjorn Reese026d29f2002-01-19 15:40:18 +00004043 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004044 TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004045 if (max > 0)
Daniel Veillard92ad2102001-03-27 12:47:33 +00004046 *buffer = NIL;
4047 return status;
4048}
Bjorn Reese70a9da52001-04-21 16:57:29 +00004049
Bjorn Reese026d29f2002-01-19 15:40:18 +00004050/**
4051 Print at most @p max characters to string.
4052
4053 @param buffer Output string.
4054 @param max Maximum number of characters to print.
4055 @param format Formatting string.
4056 @param args Arguments.
4057 @return Number of printed characters.
4058 */
4059TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004060trio_snprintfv
4061TRIO_ARGS4((buffer, max, format, args),
4062 char *buffer,
4063 size_t max,
4064 TRIO_CONST char *format,
4065 trio_pointer_t *args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004066{
4067 int status;
4068
4069 assert(VALID(buffer));
4070 assert(VALID(format));
Bjorn Reese70a9da52001-04-21 16:57:29 +00004071
Bjorn Reese026d29f2002-01-19 15:40:18 +00004072 status = TrioFormat(&buffer, max > 0 ? max - 1 : 0,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004073 TrioOutStreamStringMax, format, NULL, args);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004074 if (max > 0)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004075 *buffer = NIL;
4076 return status;
4077}
4078
4079/*************************************************************************
4080 * snprintfcat
4081 * Appends the new string to the buffer string overwriting the '\0'
4082 * character at the end of buffer.
4083 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00004084TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004085trio_snprintfcat
4086TRIO_VARGS4((buffer, max, format, va_alist),
4087 char *buffer,
4088 size_t max,
4089 TRIO_CONST char *format,
4090 TRIO_VA_DECL)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004091{
4092 int status;
4093 va_list args;
4094 size_t buf_len;
4095
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004096 TRIO_VA_START(args, format);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004097
4098 assert(VALID(buffer));
4099 assert(VALID(format));
4100
Bjorn Reese026d29f2002-01-19 15:40:18 +00004101 buf_len = trio_length(buffer);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004102 buffer = &buffer[buf_len];
4103
Bjorn Reese026d29f2002-01-19 15:40:18 +00004104 status = TrioFormat(&buffer, max - 1 - buf_len,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004105 TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004106 TRIO_VA_END(args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004107 *buffer = NIL;
4108 return status;
4109}
4110
Bjorn Reese026d29f2002-01-19 15:40:18 +00004111TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004112trio_vsnprintfcat
4113TRIO_ARGS4((buffer, max, format, args),
4114 char *buffer,
4115 size_t max,
4116 TRIO_CONST char *format,
4117 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004118{
4119 int status;
4120 size_t buf_len;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004121
Bjorn Reese70a9da52001-04-21 16:57:29 +00004122 assert(VALID(buffer));
4123 assert(VALID(format));
Bjorn Reese70a9da52001-04-21 16:57:29 +00004124
Bjorn Reese026d29f2002-01-19 15:40:18 +00004125 buf_len = trio_length(buffer);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004126 buffer = &buffer[buf_len];
Bjorn Reese026d29f2002-01-19 15:40:18 +00004127 status = TrioFormat(&buffer, max - 1 - buf_len,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004128 TrioOutStreamStringMax, format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004129 *buffer = NIL;
4130 return status;
4131}
4132
4133/*************************************************************************
4134 * trio_aprintf
4135 */
4136
4137/* Deprecated */
Bjorn Reese026d29f2002-01-19 15:40:18 +00004138TRIO_PUBLIC char *
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004139trio_aprintf
4140TRIO_VARGS2((format, va_alist),
4141 TRIO_CONST char *format,
4142 TRIO_VA_DECL)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004143{
4144 va_list args;
Bjorn Reese026d29f2002-01-19 15:40:18 +00004145 trio_string_t *info;
4146 char *result = NULL;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004147
4148 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004149
Bjorn Reese026d29f2002-01-19 15:40:18 +00004150 info = trio_xstring_duplicate("");
4151 if (info)
4152 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004153 TRIO_VA_START(args, format);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004154 (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004155 format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004156 TRIO_VA_END(args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004157
Bjorn Reese026d29f2002-01-19 15:40:18 +00004158 trio_string_terminate(info);
4159 result = trio_string_extract(info);
4160 trio_string_destroy(info);
4161 }
4162 return result;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004163}
4164
4165/* Deprecated */
Bjorn Reese026d29f2002-01-19 15:40:18 +00004166TRIO_PUBLIC char *
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004167trio_vaprintf
4168TRIO_ARGS2((format, args),
4169 TRIO_CONST char *format,
4170 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004171{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004172 trio_string_t *info;
4173 char *result = NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004174
Bjorn Reese70a9da52001-04-21 16:57:29 +00004175 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004176
Bjorn Reese026d29f2002-01-19 15:40:18 +00004177 info = trio_xstring_duplicate("");
4178 if (info)
4179 {
4180 (void)TrioFormat(info, 0, TrioOutStreamStringDynamic,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004181 format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004182 trio_string_terminate(info);
4183 result = trio_string_extract(info);
4184 trio_string_destroy(info);
4185 }
4186 return result;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004187}
4188
Bjorn Reese026d29f2002-01-19 15:40:18 +00004189TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004190trio_asprintf
4191TRIO_VARGS3((result, format, va_alist),
4192 char **result,
4193 TRIO_CONST char *format,
4194 TRIO_VA_DECL)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004195{
4196 va_list args;
4197 int status;
Bjorn Reese026d29f2002-01-19 15:40:18 +00004198 trio_string_t *info;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004199
4200 assert(VALID(format));
4201
Bjorn Reese026d29f2002-01-19 15:40:18 +00004202 *result = NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004203
Bjorn Reese026d29f2002-01-19 15:40:18 +00004204 info = trio_xstring_duplicate("");
4205 if (info == NULL)
4206 {
4207 status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
4208 }
4209 else
4210 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004211 TRIO_VA_START(args, format);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004212 status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004213 format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004214 TRIO_VA_END(args);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004215 if (status >= 0)
4216 {
4217 trio_string_terminate(info);
4218 *result = trio_string_extract(info);
4219 }
4220 trio_string_destroy(info);
4221 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00004222 return status;
4223}
4224
Bjorn Reese026d29f2002-01-19 15:40:18 +00004225TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004226trio_vasprintf
4227TRIO_ARGS3((result, format, args),
4228 char **result,
4229 TRIO_CONST char *format,
4230 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004231{
4232 int status;
Bjorn Reese026d29f2002-01-19 15:40:18 +00004233 trio_string_t *info;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004234
Bjorn Reese70a9da52001-04-21 16:57:29 +00004235 assert(VALID(format));
Bjorn Reese70a9da52001-04-21 16:57:29 +00004236
Bjorn Reese026d29f2002-01-19 15:40:18 +00004237 *result = NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004238
Bjorn Reese026d29f2002-01-19 15:40:18 +00004239 info = trio_xstring_duplicate("");
4240 if (info == NULL)
4241 {
4242 status = TRIO_ERROR_RETURN(TRIO_ENOMEM, 0);
4243 }
4244 else
4245 {
4246 status = TrioFormat(info, 0, TrioOutStreamStringDynamic,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004247 format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese026d29f2002-01-19 15:40:18 +00004248 if (status >= 0)
4249 {
4250 trio_string_terminate(info);
4251 *result = trio_string_extract(info);
4252 }
4253 trio_string_destroy(info);
4254 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00004255 return status;
4256}
4257
Bjorn Reese026d29f2002-01-19 15:40:18 +00004258/** @} End of Printf documentation module */
Daniel Veillard92ad2102001-03-27 12:47:33 +00004259
4260/*************************************************************************
4261 *
Bjorn Reese026d29f2002-01-19 15:40:18 +00004262 * CALLBACK
Daniel Veillard92ad2102001-03-27 12:47:33 +00004263 *
4264 ************************************************************************/
4265
Bjorn Reese026d29f2002-01-19 15:40:18 +00004266#if defined(TRIO_DOCUMENTATION)
4267# include "doc/doc_register.h"
4268#endif
4269/**
4270 @addtogroup UserDefined
4271 @{
4272*/
4273
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004274#if TRIO_EXTENSION
4275
Bjorn Reese70a9da52001-04-21 16:57:29 +00004276/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00004277 * trio_register
Bjorn Reese70a9da52001-04-21 16:57:29 +00004278 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00004279
4280/**
4281 Register new user-defined specifier.
4282
4283 @param callback
4284 @param name
4285 @return Handle.
4286 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004287TRIO_PUBLIC trio_pointer_t
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004288trio_register
4289TRIO_ARGS2((callback, name),
4290 trio_callback_t callback,
4291 TRIO_CONST char *name)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004292{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004293 trio_userdef_t *def;
4294 trio_userdef_t *prev = NULL;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004295
4296 if (callback == NULL)
4297 return NULL;
4298
4299 if (name)
4300 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004301 /* Handle built-in namespaces */
4302 if (name[0] == ':')
4303 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00004304 if (trio_equal(name, ":enter"))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004305 {
4306 internalEnterCriticalRegion = callback;
4307 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00004308 else if (trio_equal(name, ":leave"))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004309 {
4310 internalLeaveCriticalRegion = callback;
4311 }
4312 return NULL;
4313 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004314
Bjorn Reese70a9da52001-04-21 16:57:29 +00004315 /* Bail out if namespace is too long */
Bjorn Reese026d29f2002-01-19 15:40:18 +00004316 if (trio_length(name) >= MAX_USER_NAME)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004317 return NULL;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004318
Bjorn Reese70a9da52001-04-21 16:57:29 +00004319 /* Bail out if namespace already is registered */
4320 def = TrioFindNamespace(name, &prev);
4321 if (def)
4322 return NULL;
4323 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004324
Bjorn Reese026d29f2002-01-19 15:40:18 +00004325 def = (trio_userdef_t *)TRIO_MALLOC(sizeof(trio_userdef_t));
Bjorn Reese70a9da52001-04-21 16:57:29 +00004326 if (def)
4327 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004328 if (internalEnterCriticalRegion)
4329 (void)internalEnterCriticalRegion(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004330
Bjorn Reese70a9da52001-04-21 16:57:29 +00004331 if (name)
4332 {
4333 /* Link into internal list */
4334 if (prev == NULL)
4335 internalUserDef = def;
4336 else
4337 prev->next = def;
4338 }
4339 /* Initialize */
4340 def->callback = callback;
4341 def->name = (name == NULL)
4342 ? NULL
Bjorn Reese026d29f2002-01-19 15:40:18 +00004343 : trio_duplicate(name);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004344 def->next = NULL;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004345
4346 if (internalLeaveCriticalRegion)
4347 (void)internalLeaveCriticalRegion(NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004348 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004349 return (trio_pointer_t)def;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004350}
4351
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004352/**
4353 Unregister an existing user-defined specifier.
4354
4355 @param handle
Bjorn Reese70a9da52001-04-21 16:57:29 +00004356 */
4357void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004358trio_unregister
4359TRIO_ARGS1((handle),
4360 trio_pointer_t handle)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004361{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004362 trio_userdef_t *self = (trio_userdef_t *)handle;
4363 trio_userdef_t *def;
4364 trio_userdef_t *prev = NULL;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004365
4366 assert(VALID(self));
4367
4368 if (self->name)
4369 {
4370 def = TrioFindNamespace(self->name, &prev);
4371 if (def)
4372 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004373 if (internalEnterCriticalRegion)
4374 (void)internalEnterCriticalRegion(NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004375
Bjorn Reese70a9da52001-04-21 16:57:29 +00004376 if (prev == NULL)
4377 internalUserDef = NULL;
4378 else
4379 prev->next = def->next;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004380
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004381 if (internalLeaveCriticalRegion)
4382 (void)internalLeaveCriticalRegion(NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004383 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00004384 trio_destroy(self->name);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004385 }
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004386 TRIO_FREE(self);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004387}
4388
4389/*************************************************************************
4390 * trio_get_format [public]
4391 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00004392TRIO_CONST char *
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004393trio_get_format
4394TRIO_ARGS1((ref),
4395 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004396{
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004397#if defined(FORMAT_USER_DEFINED)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004398 assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004399#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004400
Bjorn Reese026d29f2002-01-19 15:40:18 +00004401 return (((trio_reference_t *)ref)->parameter->user_data);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004402}
4403
4404/*************************************************************************
4405 * trio_get_argument [public]
4406 */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004407trio_pointer_t
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004408trio_get_argument
4409TRIO_ARGS1((ref),
4410 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004411{
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004412#if defined(FORMAT_USER_DEFINED)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004413 assert(((trio_reference_t *)ref)->parameter->type == FORMAT_USER_DEFINED);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004414#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004415
Bjorn Reese026d29f2002-01-19 15:40:18 +00004416 return ((trio_reference_t *)ref)->parameter->data.pointer;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004417}
4418
4419/*************************************************************************
4420 * trio_get_width / trio_set_width [public]
4421 */
4422int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004423trio_get_width
4424TRIO_ARGS1((ref),
4425 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004426{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004427 return ((trio_reference_t *)ref)->parameter->width;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004428}
4429
4430void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004431trio_set_width
4432TRIO_ARGS2((ref, width),
4433 trio_pointer_t ref,
4434 int width)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004435{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004436 ((trio_reference_t *)ref)->parameter->width = width;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004437}
4438
4439/*************************************************************************
4440 * trio_get_precision / trio_set_precision [public]
4441 */
4442int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004443trio_get_precision
4444TRIO_ARGS1((ref),
4445 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004446{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004447 return (((trio_reference_t *)ref)->parameter->precision);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004448}
4449
4450void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004451trio_set_precision
4452TRIO_ARGS2((ref, precision),
4453 trio_pointer_t ref,
4454 int precision)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004455{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004456 ((trio_reference_t *)ref)->parameter->precision = precision;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004457}
4458
4459/*************************************************************************
4460 * trio_get_base / trio_set_base [public]
4461 */
4462int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004463trio_get_base
4464TRIO_ARGS1((ref),
4465 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004466{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004467 return (((trio_reference_t *)ref)->parameter->base);
Bjorn Reese70a9da52001-04-21 16:57:29 +00004468}
4469
4470void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004471trio_set_base
4472TRIO_ARGS2((ref, base),
4473 trio_pointer_t ref,
4474 int base)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004475{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004476 ((trio_reference_t *)ref)->parameter->base = base;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004477}
4478
4479/*************************************************************************
4480 * trio_get_long / trio_set_long [public]
4481 */
4482int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004483trio_get_long
4484TRIO_ARGS1((ref),
4485 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004486{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004487 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONG)
4488 ? TRUE
4489 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004490}
4491
4492void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004493trio_set_long
4494TRIO_ARGS2((ref, is_long),
4495 trio_pointer_t ref,
4496 int is_long)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004497{
4498 if (is_long)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004499 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONG;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004500 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004501 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONG;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004502}
4503
4504/*************************************************************************
4505 * trio_get_longlong / trio_set_longlong [public]
4506 */
4507int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004508trio_get_longlong
4509TRIO_ARGS1((ref),
4510 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004511{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004512 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUAD)
4513 ? TRUE
4514 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004515}
4516
4517void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004518trio_set_longlong
4519TRIO_ARGS2((ref, is_longlong),
4520 trio_pointer_t ref,
4521 int is_longlong)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004522{
4523 if (is_longlong)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004524 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUAD;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004525 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004526 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUAD;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004527}
4528
4529/*************************************************************************
4530 * trio_get_longdouble / trio_set_longdouble [public]
4531 */
4532int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004533trio_get_longdouble
4534TRIO_ARGS1((ref),
4535 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004536{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004537 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LONGDOUBLE)
4538 ? TRUE
4539 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004540}
4541
4542void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004543trio_set_longdouble
4544TRIO_ARGS2((ref, is_longdouble),
4545 trio_pointer_t ref,
4546 int is_longdouble)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004547{
4548 if (is_longdouble)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004549 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LONGDOUBLE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004550 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004551 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LONGDOUBLE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004552}
4553
4554/*************************************************************************
4555 * trio_get_short / trio_set_short [public]
4556 */
4557int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004558trio_get_short
4559TRIO_ARGS1((ref),
4560 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004561{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004562 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORT)
4563 ? TRUE
4564 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004565}
4566
4567void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004568trio_set_short
4569TRIO_ARGS2((ref, is_short),
4570 trio_pointer_t ref,
4571 int is_short)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004572{
4573 if (is_short)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004574 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORT;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004575 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004576 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORT;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004577}
4578
4579/*************************************************************************
4580 * trio_get_shortshort / trio_set_shortshort [public]
4581 */
4582int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004583trio_get_shortshort
4584TRIO_ARGS1((ref),
4585 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004586{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004587 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHORTSHORT)
4588 ? TRUE
4589 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004590}
4591
4592void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004593trio_set_shortshort
4594TRIO_ARGS2((ref, is_shortshort),
4595 trio_pointer_t ref,
4596 int is_shortshort)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004597{
4598 if (is_shortshort)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004599 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHORTSHORT;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004600 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004601 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHORTSHORT;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004602}
4603
4604/*************************************************************************
4605 * trio_get_alternative / trio_set_alternative [public]
4606 */
4607int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004608trio_get_alternative
4609TRIO_ARGS1((ref),
4610 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004611{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004612 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_ALTERNATIVE)
4613 ? TRUE
4614 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004615}
4616
4617void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004618trio_set_alternative
4619TRIO_ARGS2((ref, is_alternative),
4620 trio_pointer_t ref,
4621 int is_alternative)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004622{
4623 if (is_alternative)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004624 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_ALTERNATIVE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004625 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004626 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_ALTERNATIVE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004627}
4628
4629/*************************************************************************
4630 * trio_get_alignment / trio_set_alignment [public]
4631 */
4632int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004633trio_get_alignment
4634TRIO_ARGS1((ref),
4635 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004636{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004637 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_LEFTADJUST)
4638 ? TRUE
4639 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004640}
4641
4642void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004643trio_set_alignment
4644TRIO_ARGS2((ref, is_leftaligned),
4645 trio_pointer_t ref,
4646 int is_leftaligned)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004647{
4648 if (is_leftaligned)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004649 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_LEFTADJUST;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004650 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004651 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_LEFTADJUST;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004652}
4653
4654/*************************************************************************
4655 * trio_get_spacing /trio_set_spacing [public]
4656 */
4657int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004658trio_get_spacing
4659TRIO_ARGS1((ref),
4660 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004661{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004662 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SPACE)
4663 ? TRUE
4664 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004665}
4666
4667void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004668trio_set_spacing
4669TRIO_ARGS2((ref, is_space),
4670 trio_pointer_t ref,
4671 int is_space)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004672{
4673 if (is_space)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004674 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SPACE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004675 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004676 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SPACE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004677}
4678
4679/*************************************************************************
4680 * trio_get_sign / trio_set_sign [public]
4681 */
4682int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004683trio_get_sign
4684TRIO_ARGS1((ref),
4685 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004686{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004687 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SHOWSIGN)
4688 ? TRUE
4689 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004690}
4691
4692void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004693trio_set_sign
4694TRIO_ARGS2((ref, is_sign),
4695 trio_pointer_t ref,
4696 int is_sign)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004697{
4698 if (is_sign)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004699 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SHOWSIGN;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004700 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004701 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SHOWSIGN;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004702}
4703
4704/*************************************************************************
4705 * trio_get_padding / trio_set_padding [public]
4706 */
4707int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004708trio_get_padding
4709TRIO_ARGS1((ref),
4710 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004711{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004712 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_NILPADDING)
4713 ? TRUE
4714 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004715}
4716
4717void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004718trio_set_padding
4719TRIO_ARGS2((ref, is_padding),
4720 trio_pointer_t ref,
4721 int is_padding)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004722{
4723 if (is_padding)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004724 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_NILPADDING;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004725 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004726 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_NILPADDING;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004727}
4728
4729/*************************************************************************
4730 * trio_get_quote / trio_set_quote [public]
4731 */
4732int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004733trio_get_quote
4734TRIO_ARGS1((ref),
4735 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004736{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004737 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_QUOTE)
4738 ? TRUE
4739 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004740}
4741
4742void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004743trio_set_quote
4744TRIO_ARGS2((ref, is_quote),
4745 trio_pointer_t ref,
4746 int is_quote)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004747{
4748 if (is_quote)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004749 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_QUOTE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004750 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004751 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_QUOTE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004752}
4753
4754/*************************************************************************
4755 * trio_get_upper / trio_set_upper [public]
4756 */
4757int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004758trio_get_upper
4759TRIO_ARGS1((ref),
4760 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004761{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004762 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_UPPER)
4763 ? TRUE
4764 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004765}
4766
4767void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004768trio_set_upper
4769TRIO_ARGS2((ref, is_upper),
4770 trio_pointer_t ref,
4771 int is_upper)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004772{
4773 if (is_upper)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004774 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_UPPER;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004775 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004776 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_UPPER;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004777}
4778
4779/*************************************************************************
4780 * trio_get_largest / trio_set_largest [public]
4781 */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004782#if TRIO_C99
Bjorn Reese70a9da52001-04-21 16:57:29 +00004783int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004784trio_get_largest
4785TRIO_ARGS1((ref),
4786 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004787{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004788 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_INTMAX_T)
4789 ? TRUE
4790 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004791}
4792
4793void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004794trio_set_largest
4795TRIO_ARGS2((ref, is_largest),
4796 trio_pointer_t ref,
4797 int is_largest)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004798{
4799 if (is_largest)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004800 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_INTMAX_T;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004801 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004802 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_INTMAX_T;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004803}
4804#endif
4805
4806/*************************************************************************
4807 * trio_get_ptrdiff / trio_set_ptrdiff [public]
4808 */
Bjorn Reese70a9da52001-04-21 16:57:29 +00004809int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004810trio_get_ptrdiff
4811TRIO_ARGS1((ref),
4812 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004813{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004814 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_PTRDIFF_T)
4815 ? TRUE
4816 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004817}
4818
4819void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004820trio_set_ptrdiff
4821TRIO_ARGS2((ref, is_ptrdiff),
4822 trio_pointer_t ref,
4823 int is_ptrdiff)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004824{
4825 if (is_ptrdiff)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004826 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_PTRDIFF_T;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004827 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004828 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_PTRDIFF_T;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004829}
Bjorn Reese70a9da52001-04-21 16:57:29 +00004830
4831/*************************************************************************
4832 * trio_get_size / trio_set_size [public]
4833 */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004834#if TRIO_C99
Bjorn Reese70a9da52001-04-21 16:57:29 +00004835int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004836trio_get_size
4837TRIO_ARGS1((ref),
4838 trio_pointer_t ref)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004839{
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004840 return (((trio_reference_t *)ref)->parameter->flags & FLAGS_SIZE_T)
4841 ? TRUE
4842 : FALSE;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004843}
4844
4845void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004846trio_set_size
4847TRIO_ARGS2((ref, is_size),
4848 trio_pointer_t ref,
4849 int is_size)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004850{
4851 if (is_size)
Bjorn Reese026d29f2002-01-19 15:40:18 +00004852 ((trio_reference_t *)ref)->parameter->flags |= FLAGS_SIZE_T;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004853 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00004854 ((trio_reference_t *)ref)->parameter->flags &= ~FLAGS_SIZE_T;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004855}
4856#endif
4857
4858/*************************************************************************
4859 * trio_print_int [public]
4860 */
4861void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004862trio_print_int
4863TRIO_ARGS2((ref, number),
4864 trio_pointer_t ref,
4865 int number)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004866{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004867 trio_reference_t *self = (trio_reference_t *)ref;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004868
4869 TrioWriteNumber(self->data,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004870 (trio_uintmax_t)number,
Bjorn Reese70a9da52001-04-21 16:57:29 +00004871 self->parameter->flags,
4872 self->parameter->width,
4873 self->parameter->precision,
4874 self->parameter->base);
4875}
4876
4877/*************************************************************************
4878 * trio_print_uint [public]
4879 */
4880void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004881trio_print_uint
4882TRIO_ARGS2((ref, number),
4883 trio_pointer_t ref,
4884 unsigned int number)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004885{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004886 trio_reference_t *self = (trio_reference_t *)ref;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004887
4888 TrioWriteNumber(self->data,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004889 (trio_uintmax_t)number,
Bjorn Reese70a9da52001-04-21 16:57:29 +00004890 self->parameter->flags | FLAGS_UNSIGNED,
4891 self->parameter->width,
4892 self->parameter->precision,
4893 self->parameter->base);
4894}
4895
4896/*************************************************************************
4897 * trio_print_double [public]
4898 */
4899void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004900trio_print_double
4901TRIO_ARGS2((ref, number),
4902 trio_pointer_t ref,
4903 double number)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004904{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004905 trio_reference_t *self = (trio_reference_t *)ref;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004906
4907 TrioWriteDouble(self->data,
4908 number,
4909 self->parameter->flags,
4910 self->parameter->width,
4911 self->parameter->precision,
4912 self->parameter->base);
4913}
4914
4915/*************************************************************************
4916 * trio_print_string [public]
4917 */
4918void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004919trio_print_string
4920TRIO_ARGS2((ref, string),
4921 trio_pointer_t ref,
4922 char *string)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004923{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004924 trio_reference_t *self = (trio_reference_t *)ref;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004925
4926 TrioWriteString(self->data,
4927 string,
4928 self->parameter->flags,
4929 self->parameter->width,
4930 self->parameter->precision);
4931}
4932
4933/*************************************************************************
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004934 * trio_print_ref [public]
4935 */
4936int
4937trio_print_ref
4938TRIO_VARGS3((ref, format, va_alist),
4939 trio_pointer_t ref,
4940 TRIO_CONST char *format,
4941 TRIO_VA_DECL)
4942{
4943 int status;
4944 va_list arglist;
4945
4946 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004947
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004948 TRIO_VA_START(arglist, format);
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004949 status = TrioFormatRef((trio_reference_t *)ref, format, TRIO_VA_LIST_ADDR(arglist), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004950 TRIO_VA_END(arglist);
4951 return status;
4952}
4953
4954/*************************************************************************
4955 * trio_vprint_ref [public]
4956 */
4957int
4958trio_vprint_ref
4959TRIO_ARGS3((ref, format, arglist),
4960 trio_pointer_t ref,
4961 TRIO_CONST char *format,
4962 va_list arglist)
4963{
4964 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004965
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08004966 return TrioFormatRef((trio_reference_t *)ref, format, TRIO_VA_LIST_ADDR(arglist), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004967}
4968
4969/*************************************************************************
4970 * trio_printv_ref [public]
4971 */
4972int
4973trio_printv_ref
4974TRIO_ARGS3((ref, format, argarray),
4975 trio_pointer_t ref,
4976 TRIO_CONST char *format,
4977 trio_pointer_t *argarray)
4978{
4979 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08004980
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004981 return TrioFormatRef((trio_reference_t *)ref, format, NULL, argarray);
4982}
4983
4984#endif /* TRIO_EXTENSION */
4985
4986/*************************************************************************
Bjorn Reese70a9da52001-04-21 16:57:29 +00004987 * trio_print_pointer [public]
4988 */
4989void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00004990trio_print_pointer
4991TRIO_ARGS2((ref, pointer),
4992 trio_pointer_t ref,
4993 trio_pointer_t pointer)
Bjorn Reese70a9da52001-04-21 16:57:29 +00004994{
Bjorn Reese026d29f2002-01-19 15:40:18 +00004995 trio_reference_t *self = (trio_reference_t *)ref;
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00004996 trio_flags_t flags;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00004997 trio_uintmax_t number;
Bjorn Reese70a9da52001-04-21 16:57:29 +00004998
4999 if (NULL == pointer)
5000 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00005001 TRIO_CONST char *string = internalNullString;
Bjorn Reese70a9da52001-04-21 16:57:29 +00005002 while (*string)
5003 self->data->OutStream(self->data, *string++);
5004 }
5005 else
5006 {
5007 /*
5008 * The subtraction of the null pointer is a workaround
5009 * to avoid a compiler warning. The performance overhead
5010 * is negligible (and likely to be removed by an
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005011 * optimizing compiler). The (char *) casting is done
Bjorn Reese70a9da52001-04-21 16:57:29 +00005012 * to please ANSI C++.
5013 */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005014 number = (trio_uintmax_t)((char *)pointer - (char *)0);
Bjorn Reese70a9da52001-04-21 16:57:29 +00005015 /* Shrink to size of pointer */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005016 number &= (trio_uintmax_t)-1;
Bjorn Reese70a9da52001-04-21 16:57:29 +00005017 flags = self->parameter->flags;
5018 flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE |
5019 FLAGS_NILPADDING);
5020 TrioWriteNumber(self->data,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005021 number,
Bjorn Reese70a9da52001-04-21 16:57:29 +00005022 flags,
5023 POINTER_WIDTH,
5024 NO_PRECISION,
5025 BASE_HEX);
5026 }
5027}
5028
Bjorn Reese026d29f2002-01-19 15:40:18 +00005029/** @} End of UserDefined documentation module */
Bjorn Reese70a9da52001-04-21 16:57:29 +00005030
5031/*************************************************************************
5032 *
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005033 * LOCALES
5034 *
5035 ************************************************************************/
5036
5037/*************************************************************************
5038 * trio_locale_set_decimal_point
5039 *
5040 * Decimal point can only be one character. The input argument is a
5041 * string to enable multibyte characters. At most MB_LEN_MAX characters
5042 * will be used.
5043 */
5044TRIO_PUBLIC void
5045trio_locale_set_decimal_point
5046TRIO_ARGS1((decimalPoint),
5047 char *decimalPoint)
5048{
5049#if defined(USE_LOCALE)
5050 if (NULL == internalLocaleValues)
5051 {
5052 TrioSetLocale();
5053 }
5054#endif
5055 internalDecimalPointLength = trio_length(decimalPoint);
5056 if (internalDecimalPointLength == 1)
5057 {
5058 internalDecimalPoint = *decimalPoint;
5059 }
5060 else
5061 {
5062 internalDecimalPoint = NIL;
5063 trio_copy_max(internalDecimalPointString,
5064 sizeof(internalDecimalPointString),
5065 decimalPoint);
5066 }
5067}
5068
5069/*************************************************************************
5070 * trio_locale_set_thousand_separator
5071 *
5072 * See trio_locale_set_decimal_point
5073 */
5074TRIO_PUBLIC void
5075trio_locale_set_thousand_separator
5076TRIO_ARGS1((thousandSeparator),
5077 char *thousandSeparator)
5078{
5079#if defined(USE_LOCALE)
5080 if (NULL == internalLocaleValues)
5081 {
5082 TrioSetLocale();
5083 }
5084#endif
5085 trio_copy_max(internalThousandSeparator,
5086 sizeof(internalThousandSeparator),
5087 thousandSeparator);
5088 internalThousandSeparatorLength = trio_length(internalThousandSeparator);
5089}
5090
5091/*************************************************************************
5092 * trio_locale_set_grouping
5093 *
5094 * Array of bytes. Reversed order.
5095 *
5096 * CHAR_MAX : No further grouping
5097 * 0 : Repeat last group for the remaining digits (not necessary
5098 * as C strings are zero-terminated)
5099 * n : Set current group to n
5100 *
5101 * Same order as the grouping attribute in LC_NUMERIC.
5102 */
5103TRIO_PUBLIC void
5104trio_locale_set_grouping
5105TRIO_ARGS1((grouping),
5106 char *grouping)
5107{
5108#if defined(USE_LOCALE)
5109 if (NULL == internalLocaleValues)
5110 {
5111 TrioSetLocale();
5112 }
5113#endif
5114 trio_copy_max(internalGrouping,
5115 sizeof(internalGrouping),
5116 grouping);
5117}
5118
5119
5120/*************************************************************************
5121 *
Bjorn Reese026d29f2002-01-19 15:40:18 +00005122 * SCANNING
Bjorn Reese70a9da52001-04-21 16:57:29 +00005123 *
5124 ************************************************************************/
5125
Daniel Veillard92ad2102001-03-27 12:47:33 +00005126/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005127 * TrioSkipWhitespaces
Daniel Veillard92ad2102001-03-27 12:47:33 +00005128 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00005129TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005130TrioSkipWhitespaces
5131TRIO_ARGS1((self),
5132 trio_class_t *self)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005133{
5134 int ch;
5135
5136 ch = self->current;
5137 while (isspace(ch))
5138 {
5139 self->InStream(self, &ch);
5140 }
5141 return ch;
5142}
5143
5144/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005145 * TrioGetCollation
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005146 */
5147#if TRIO_EXTENSION
Bjorn Reese026d29f2002-01-19 15:40:18 +00005148TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005149TrioGetCollation(TRIO_NOARGS)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005150{
5151 int i;
5152 int j;
5153 int k;
5154 char first[2];
5155 char second[2];
5156
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005157 /* This is computationally expensive */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005158 first[1] = NIL;
5159 second[1] = NIL;
5160 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5161 {
5162 k = 0;
5163 first[0] = (char)i;
5164 for (j = 0; j < MAX_CHARACTER_CLASS; j++)
5165 {
5166 second[0] = (char)j;
Bjorn Reese026d29f2002-01-19 15:40:18 +00005167 if (trio_equal_locale(first, second))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005168 internalCollationArray[i][k++] = (char)j;
5169 }
5170 internalCollationArray[i][k] = NIL;
5171 }
5172}
5173#endif
5174
5175/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005176 * TrioGetCharacterClass
Daniel Veillard92ad2102001-03-27 12:47:33 +00005177 *
5178 * FIXME:
5179 * multibyte
5180 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00005181TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005182TrioGetCharacterClass
5183TRIO_ARGS4((format, indexPointer, flagsPointer, characterclass),
5184 TRIO_CONST char *format,
5185 int *indexPointer,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005186 trio_flags_t *flagsPointer,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005187 int *characterclass)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005188{
5189 int index = *indexPointer;
5190 int i;
5191 char ch;
5192 char range_begin;
5193 char range_end;
5194
5195 *flagsPointer &= ~FLAGS_EXCLUDE;
5196
5197 if (format[index] == QUALIFIER_CIRCUMFLEX)
5198 {
5199 *flagsPointer |= FLAGS_EXCLUDE;
5200 index++;
5201 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00005202 /*
5203 * If the ungroup character is at the beginning of the scanlist,
Daniel Veillard92ad2102001-03-27 12:47:33 +00005204 * it will be part of the class, and a second ungroup character
5205 * must follow to end the group.
5206 */
5207 if (format[index] == SPECIFIER_UNGROUP)
5208 {
5209 characterclass[(int)SPECIFIER_UNGROUP]++;
5210 index++;
5211 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00005212 /*
5213 * Minus is used to specify ranges. To include minus in the class,
Daniel Veillard92ad2102001-03-27 12:47:33 +00005214 * it must be at the beginning of the list
5215 */
5216 if (format[index] == QUALIFIER_MINUS)
5217 {
5218 characterclass[(int)QUALIFIER_MINUS]++;
5219 index++;
5220 }
5221 /* Collect characters */
5222 for (ch = format[index];
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005223 (ch != SPECIFIER_UNGROUP) && (ch != NIL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00005224 ch = format[++index])
5225 {
5226 switch (ch)
5227 {
5228 case QUALIFIER_MINUS: /* Scanlist ranges */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005229
Bjorn Reese70a9da52001-04-21 16:57:29 +00005230 /*
5231 * Both C99 and UNIX98 describes ranges as implementation-
Daniel Veillard92ad2102001-03-27 12:47:33 +00005232 * defined.
5233 *
5234 * We support the following behaviour (although this may
5235 * change as we become wiser)
5236 * - only increasing ranges, ie. [a-b] but not [b-a]
5237 * - transitive ranges, ie. [a-b-c] == [a-c]
5238 * - trailing minus, ie. [a-] is interpreted as an 'a'
5239 * and a '-'
5240 * - duplicates (although we can easily convert these
5241 * into errors)
5242 */
5243 range_begin = format[index - 1];
5244 range_end = format[++index];
5245 if (range_end == SPECIFIER_UNGROUP)
5246 {
5247 /* Trailing minus is included */
5248 characterclass[(int)ch]++;
5249 ch = range_end;
5250 break; /* for */
5251 }
5252 if (range_end == NIL)
5253 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
5254 if (range_begin > range_end)
5255 return TRIO_ERROR_RETURN(TRIO_ERANGE, index);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005256
Daniel Veillard92ad2102001-03-27 12:47:33 +00005257 for (i = (int)range_begin; i <= (int)range_end; i++)
5258 characterclass[i]++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005259
Daniel Veillard92ad2102001-03-27 12:47:33 +00005260 ch = range_end;
5261 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005262
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005263#if TRIO_EXTENSION
5264
5265 case SPECIFIER_GROUP:
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005266
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005267 switch (format[index + 1])
Daniel Veillard92ad2102001-03-27 12:47:33 +00005268 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005269 case QUALIFIER_DOT: /* Collating symbol */
5270 /*
5271 * FIXME: This will be easier to implement when multibyte
5272 * characters have been implemented. Until now, we ignore
5273 * this feature.
5274 */
5275 for (i = index + 2; ; i++)
5276 {
5277 if (format[i] == NIL)
5278 /* Error in syntax */
5279 return -1;
5280 else if (format[i] == QUALIFIER_DOT)
5281 break; /* for */
5282 }
5283 if (format[++i] != SPECIFIER_UNGROUP)
5284 return -1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005285
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005286 index = i;
5287 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005288
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005289 case QUALIFIER_EQUAL: /* Equivalence class expressions */
5290 {
5291 unsigned int j;
5292 unsigned int k;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005293
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005294 if (internalCollationUnconverted)
5295 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005296 /* Lazy evaluation of collation array */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005297 TrioGetCollation();
5298 internalCollationUnconverted = FALSE;
5299 }
5300 for (i = index + 2; ; i++)
5301 {
5302 if (format[i] == NIL)
5303 /* Error in syntax */
5304 return -1;
5305 else if (format[i] == QUALIFIER_EQUAL)
5306 break; /* for */
5307 else
5308 {
5309 /* Mark any equivalent character */
5310 k = (unsigned int)format[i];
5311 for (j = 0; internalCollationArray[k][j] != NIL; j++)
5312 characterclass[(int)internalCollationArray[k][j]]++;
5313 }
5314 }
5315 if (format[++i] != SPECIFIER_UNGROUP)
5316 return -1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005317
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005318 index = i;
5319 }
5320 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005321
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005322 case QUALIFIER_COLON: /* Character class expressions */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005323
Bjorn Reese026d29f2002-01-19 15:40:18 +00005324 if (trio_equal_max(CLASS_ALNUM, sizeof(CLASS_ALNUM) - 1,
5325 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005326 {
5327 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5328 if (isalnum(i))
5329 characterclass[i]++;
5330 index += sizeof(CLASS_ALNUM) - 1;
5331 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005332 else if (trio_equal_max(CLASS_ALPHA, sizeof(CLASS_ALPHA) - 1,
5333 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005334 {
5335 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5336 if (isalpha(i))
5337 characterclass[i]++;
5338 index += sizeof(CLASS_ALPHA) - 1;
5339 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005340 else if (trio_equal_max(CLASS_CNTRL, sizeof(CLASS_CNTRL) - 1,
5341 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005342 {
5343 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5344 if (iscntrl(i))
5345 characterclass[i]++;
5346 index += sizeof(CLASS_CNTRL) - 1;
5347 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005348 else if (trio_equal_max(CLASS_DIGIT, sizeof(CLASS_DIGIT) - 1,
5349 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005350 {
5351 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5352 if (isdigit(i))
5353 characterclass[i]++;
5354 index += sizeof(CLASS_DIGIT) - 1;
5355 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005356 else if (trio_equal_max(CLASS_GRAPH, sizeof(CLASS_GRAPH) - 1,
5357 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005358 {
5359 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5360 if (isgraph(i))
5361 characterclass[i]++;
5362 index += sizeof(CLASS_GRAPH) - 1;
5363 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005364 else if (trio_equal_max(CLASS_LOWER, sizeof(CLASS_LOWER) - 1,
5365 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005366 {
5367 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5368 if (islower(i))
5369 characterclass[i]++;
5370 index += sizeof(CLASS_LOWER) - 1;
5371 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005372 else if (trio_equal_max(CLASS_PRINT, sizeof(CLASS_PRINT) - 1,
5373 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005374 {
5375 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5376 if (isprint(i))
5377 characterclass[i]++;
5378 index += sizeof(CLASS_PRINT) - 1;
5379 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005380 else if (trio_equal_max(CLASS_PUNCT, sizeof(CLASS_PUNCT) - 1,
5381 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005382 {
5383 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5384 if (ispunct(i))
5385 characterclass[i]++;
5386 index += sizeof(CLASS_PUNCT) - 1;
5387 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005388 else if (trio_equal_max(CLASS_SPACE, sizeof(CLASS_SPACE) - 1,
5389 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005390 {
5391 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5392 if (isspace(i))
5393 characterclass[i]++;
5394 index += sizeof(CLASS_SPACE) - 1;
5395 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005396 else if (trio_equal_max(CLASS_UPPER, sizeof(CLASS_UPPER) - 1,
5397 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005398 {
5399 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5400 if (isupper(i))
5401 characterclass[i]++;
5402 index += sizeof(CLASS_UPPER) - 1;
5403 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005404 else if (trio_equal_max(CLASS_XDIGIT, sizeof(CLASS_XDIGIT) - 1,
5405 &format[index]))
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005406 {
5407 for (i = 0; i < MAX_CHARACTER_CLASS; i++)
5408 if (isxdigit(i))
5409 characterclass[i]++;
5410 index += sizeof(CLASS_XDIGIT) - 1;
5411 }
5412 else
5413 {
5414 characterclass[(int)ch]++;
5415 }
5416 break;
5417
5418 default:
Daniel Veillard92ad2102001-03-27 12:47:33 +00005419 characterclass[(int)ch]++;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005420 break;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005421 }
5422 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005423
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005424#endif /* TRIO_EXTENSION */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005425
Daniel Veillard92ad2102001-03-27 12:47:33 +00005426 default:
5427 characterclass[(int)ch]++;
5428 break;
5429 }
5430 }
5431 return 0;
5432}
5433
5434/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005435 * TrioReadNumber
Daniel Veillard92ad2102001-03-27 12:47:33 +00005436 *
5437 * We implement our own number conversion in preference of strtol and
5438 * strtoul, because we must handle 'long long' and thousand separators.
5439 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00005440TRIO_PRIVATE BOOLEAN_T
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005441TrioReadNumber
5442TRIO_ARGS5((self, target, flags, width, base),
5443 trio_class_t *self,
5444 trio_uintmax_t *target,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005445 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005446 int width,
5447 int base)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005448{
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005449 trio_uintmax_t number = 0;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005450 int digit;
5451 int count;
5452 BOOLEAN_T isNegative = FALSE;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005453 BOOLEAN_T gotNumber = FALSE;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005454 int j;
5455
5456 assert(VALID(self));
5457 assert(VALID(self->InStream));
5458 assert((base >= MIN_BASE && base <= MAX_BASE) || (base == NO_BASE));
5459
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005460 if (internalDigitsUnconverted)
5461 {
5462 /* Lazy evaluation of digits array */
5463 memset(internalDigitArray, -1, sizeof(internalDigitArray));
5464 for (j = 0; j < (int)sizeof(internalDigitsLower) - 1; j++)
5465 {
5466 internalDigitArray[(int)internalDigitsLower[j]] = j;
5467 internalDigitArray[(int)internalDigitsUpper[j]] = j;
5468 }
5469 internalDigitsUnconverted = FALSE;
5470 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005471
Daniel Veillard92ad2102001-03-27 12:47:33 +00005472 TrioSkipWhitespaces(self);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005473
Daniel Veillard92ad2102001-03-27 12:47:33 +00005474 if (!(flags & FLAGS_UNSIGNED))
5475 {
5476 /* Leading sign */
5477 if (self->current == '+')
5478 {
5479 self->InStream(self, NULL);
5480 }
5481 else if (self->current == '-')
5482 {
5483 self->InStream(self, NULL);
5484 isNegative = TRUE;
5485 }
5486 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005487
Daniel Veillard92ad2102001-03-27 12:47:33 +00005488 count = self->processed;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005489
Daniel Veillard92ad2102001-03-27 12:47:33 +00005490 if (flags & FLAGS_ALTERNATIVE)
5491 {
5492 switch (base)
5493 {
5494 case NO_BASE:
5495 case BASE_OCTAL:
5496 case BASE_HEX:
5497 case BASE_BINARY:
5498 if (self->current == '0')
5499 {
5500 self->InStream(self, NULL);
5501 if (self->current)
5502 {
5503 if ((base == BASE_HEX) &&
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005504 (trio_to_upper(self->current) == 'X'))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005505 {
5506 self->InStream(self, NULL);
5507 }
5508 else if ((base == BASE_BINARY) &&
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005509 (trio_to_upper(self->current) == 'B'))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005510 {
5511 self->InStream(self, NULL);
5512 }
5513 }
5514 }
5515 else
5516 return FALSE;
5517 break;
5518 default:
5519 break;
5520 }
5521 }
5522
5523 while (((width == NO_WIDTH) || (self->processed - count < width)) &&
5524 (! ((self->current == EOF) || isspace(self->current))))
5525 {
5526 if (isascii(self->current))
5527 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00005528 digit = internalDigitArray[self->current];
Daniel Veillard92ad2102001-03-27 12:47:33 +00005529 /* Abort if digit is not allowed in the specified base */
5530 if ((digit == -1) || (digit >= base))
5531 break;
5532 }
5533 else if (flags & FLAGS_QUOTE)
5534 {
5535 /* Compare with thousands separator */
Bjorn Reese70a9da52001-04-21 16:57:29 +00005536 for (j = 0; internalThousandSeparator[j] && self->current; j++)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005537 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00005538 if (internalThousandSeparator[j] != self->current)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005539 break;
5540
5541 self->InStream(self, NULL);
5542 }
Bjorn Reese70a9da52001-04-21 16:57:29 +00005543 if (internalThousandSeparator[j])
Daniel Veillard92ad2102001-03-27 12:47:33 +00005544 break; /* Mismatch */
5545 else
5546 continue; /* Match */
5547 }
5548 else
5549 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005550
Daniel Veillard92ad2102001-03-27 12:47:33 +00005551 number *= base;
5552 number += digit;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005553 gotNumber = TRUE; /* we need at least one digit */
Daniel Veillard92ad2102001-03-27 12:47:33 +00005554
5555 self->InStream(self, NULL);
5556 }
5557
5558 /* Was anything read at all? */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005559 if (!gotNumber)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005560 return FALSE;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005561
Daniel Veillard92ad2102001-03-27 12:47:33 +00005562 if (target)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005563 *target = (isNegative) ? -((trio_intmax_t)number) : number;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005564 return TRUE;
5565}
5566
5567/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005568 * TrioReadChar
Daniel Veillard92ad2102001-03-27 12:47:33 +00005569 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00005570TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005571TrioReadChar
5572TRIO_ARGS4((self, target, flags, width),
5573 trio_class_t *self,
5574 char *target,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005575 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005576 int width)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005577{
5578 int i;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005579 char ch;
5580 trio_uintmax_t number;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005581
Daniel Veillard92ad2102001-03-27 12:47:33 +00005582 assert(VALID(self));
5583 assert(VALID(self->InStream));
5584
5585 for (i = 0;
5586 (self->current != EOF) && (i < width);
5587 i++)
5588 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005589 ch = (char)self->current;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005590 self->InStream(self, NULL);
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005591 if ((flags & FLAGS_ALTERNATIVE) && (ch == CHAR_BACKSLASH))
5592 {
5593 switch (self->current)
5594 {
5595 case '\\': ch = '\\'; break;
5596 case 'a': ch = '\007'; break;
5597 case 'b': ch = '\b'; break;
5598 case 'f': ch = '\f'; break;
5599 case 'n': ch = '\n'; break;
5600 case 'r': ch = '\r'; break;
5601 case 't': ch = '\t'; break;
5602 case 'v': ch = '\v'; break;
5603 default:
5604 if (isdigit(self->current))
5605 {
5606 /* Read octal number */
5607 if (!TrioReadNumber(self, &number, 0, 3, BASE_OCTAL))
5608 return 0;
5609 ch = (char)number;
5610 }
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005611 else if (trio_to_upper(self->current) == 'X')
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005612 {
5613 /* Read hexadecimal number */
5614 self->InStream(self, NULL);
5615 if (!TrioReadNumber(self, &number, 0, 2, BASE_HEX))
5616 return 0;
5617 ch = (char)number;
5618 }
5619 else
5620 {
5621 ch = (char)self->current;
5622 }
5623 break;
5624 }
5625 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005626
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005627 if (target)
5628 target[i] = ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005629 }
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005630 return i + 1;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005631}
5632
5633/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005634 * TrioReadString
Daniel Veillard92ad2102001-03-27 12:47:33 +00005635 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00005636TRIO_PRIVATE BOOLEAN_T
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005637TrioReadString
5638TRIO_ARGS4((self, target, flags, width),
5639 trio_class_t *self,
5640 char *target,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005641 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005642 int width)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005643{
5644 int i;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005645
Daniel Veillard92ad2102001-03-27 12:47:33 +00005646 assert(VALID(self));
5647 assert(VALID(self->InStream));
5648
5649 TrioSkipWhitespaces(self);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005650
Bjorn Reese70a9da52001-04-21 16:57:29 +00005651 /*
5652 * Continue until end of string is reached, a whitespace is encountered,
Daniel Veillard92ad2102001-03-27 12:47:33 +00005653 * or width is exceeded
5654 */
5655 for (i = 0;
5656 ((width == NO_WIDTH) || (i < width)) &&
5657 (! ((self->current == EOF) || isspace(self->current)));
5658 i++)
5659 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005660 if (TrioReadChar(self, (target ? &target[i] : 0), flags, 1) == 0)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005661 break; /* for */
Daniel Veillard92ad2102001-03-27 12:47:33 +00005662 }
5663 if (target)
5664 target[i] = NIL;
5665 return TRUE;
5666}
5667
5668/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005669 * TrioReadWideChar
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005670 */
5671#if TRIO_WIDECHAR
Bjorn Reese026d29f2002-01-19 15:40:18 +00005672TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005673TrioReadWideChar
5674TRIO_ARGS4((self, target, flags, width),
5675 trio_class_t *self,
5676 trio_wchar_t *target,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005677 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005678 int width)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005679{
5680 int i;
5681 int j;
5682 int size;
5683 int amount = 0;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005684 trio_wchar_t wch;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005685 char buffer[MB_LEN_MAX + 1];
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005686
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005687 assert(VALID(self));
5688 assert(VALID(self->InStream));
5689
5690 for (i = 0;
5691 (self->current != EOF) && (i < width);
5692 i++)
5693 {
5694 if (isascii(self->current))
5695 {
5696 if (TrioReadChar(self, buffer, flags, 1) == 0)
5697 return 0;
5698 buffer[1] = NIL;
5699 }
5700 else
5701 {
5702 /*
5703 * Collect a multibyte character, by enlarging buffer until
5704 * it contains a fully legal multibyte character, or the
5705 * buffer is full.
5706 */
5707 j = 0;
5708 do
5709 {
5710 buffer[j++] = (char)self->current;
5711 buffer[j] = NIL;
5712 self->InStream(self, NULL);
5713 }
5714 while ((j < (int)sizeof(buffer)) && (mblen(buffer, (size_t)j) != j));
5715 }
5716 if (target)
5717 {
5718 size = mbtowc(&wch, buffer, sizeof(buffer));
5719 if (size > 0)
5720 target[i] = wch;
5721 }
5722 amount += size;
5723 self->InStream(self, NULL);
5724 }
5725 return amount;
5726}
5727#endif /* TRIO_WIDECHAR */
5728
5729/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005730 * TrioReadWideString
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005731 */
5732#if TRIO_WIDECHAR
Bjorn Reese026d29f2002-01-19 15:40:18 +00005733TRIO_PRIVATE BOOLEAN_T
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005734TrioReadWideString
5735TRIO_ARGS4((self, target, flags, width),
5736 trio_class_t *self,
5737 trio_wchar_t *target,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005738 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005739 int width)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005740{
5741 int i;
5742 int size;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005743
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005744 assert(VALID(self));
5745 assert(VALID(self->InStream));
5746
5747 TrioSkipWhitespaces(self);
5748
5749#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005750 (void)mblen(NULL, 0);
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005751#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005752
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005753 /*
5754 * Continue until end of string is reached, a whitespace is encountered,
5755 * or width is exceeded
5756 */
5757 for (i = 0;
5758 ((width == NO_WIDTH) || (i < width)) &&
5759 (! ((self->current == EOF) || isspace(self->current)));
5760 )
5761 {
5762 size = TrioReadWideChar(self, &target[i], flags, 1);
5763 if (size == 0)
5764 break; /* for */
5765
5766 i += size;
5767 }
5768 if (target)
Bjorn Reese026d29f2002-01-19 15:40:18 +00005769 target[i] = WCONST('\0');
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005770 return TRUE;
5771}
5772#endif /* TRIO_WIDECHAR */
5773
5774/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005775 * TrioReadGroup
Daniel Veillard92ad2102001-03-27 12:47:33 +00005776 *
5777 * FIXME: characterclass does not work with multibyte characters
5778 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00005779TRIO_PRIVATE BOOLEAN_T
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005780TrioReadGroup
5781TRIO_ARGS5((self, target, characterclass, flags, width),
5782 trio_class_t *self,
5783 char *target,
5784 int *characterclass,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005785 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005786 int width)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005787{
Bjorn Reese70a9da52001-04-21 16:57:29 +00005788 int ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005789 int i;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005790
Daniel Veillard92ad2102001-03-27 12:47:33 +00005791 assert(VALID(self));
5792 assert(VALID(self->InStream));
5793
5794 ch = self->current;
5795 for (i = 0;
5796 ((width == NO_WIDTH) || (i < width)) &&
5797 (! ((ch == EOF) ||
5798 (((flags & FLAGS_EXCLUDE) != 0) ^ (characterclass[ch] == 0))));
5799 i++)
5800 {
5801 if (target)
5802 target[i] = (char)ch;
5803 self->InStream(self, &ch);
5804 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005805
Daniel Veillard92ad2102001-03-27 12:47:33 +00005806 if (target)
5807 target[i] = NIL;
5808 return TRUE;
5809}
5810
5811/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00005812 * TrioReadDouble
Daniel Veillard92ad2102001-03-27 12:47:33 +00005813 *
5814 * FIXME:
Daniel Veillard92ad2102001-03-27 12:47:33 +00005815 * add long double
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005816 * handle base
Daniel Veillard92ad2102001-03-27 12:47:33 +00005817 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00005818TRIO_PRIVATE BOOLEAN_T
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005819TrioReadDouble
5820TRIO_ARGS4((self, target, flags, width),
5821 trio_class_t *self,
5822 trio_pointer_t target,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005823 trio_flags_t flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005824 int width)
Daniel Veillard92ad2102001-03-27 12:47:33 +00005825{
5826 int ch;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005827 char doubleString[512];
Daniel Veillard92ad2102001-03-27 12:47:33 +00005828 int index = 0;
5829 int start;
Bjorn Reese70a9da52001-04-21 16:57:29 +00005830 int j;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005831 BOOLEAN_T isHex = FALSE;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005832
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005833 doubleString[0] = 0;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005834
Bjorn Reese70a9da52001-04-21 16:57:29 +00005835 if ((width == NO_WIDTH) || (width > (int)sizeof(doubleString) - 1))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005836 width = sizeof(doubleString) - 1;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005837
Daniel Veillard92ad2102001-03-27 12:47:33 +00005838 TrioSkipWhitespaces(self);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005839
Bjorn Reese70a9da52001-04-21 16:57:29 +00005840 /*
Bjorn Reese026d29f2002-01-19 15:40:18 +00005841 * Read entire double number from stream. trio_to_double requires
5842 * a string as input, but InStream can be anything, so we have to
Daniel Veillard92ad2102001-03-27 12:47:33 +00005843 * collect all characters.
5844 */
5845 ch = self->current;
5846 if ((ch == '+') || (ch == '-'))
5847 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005848 doubleString[index++] = (char)ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005849 self->InStream(self, &ch);
5850 width--;
5851 }
5852
5853 start = index;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005854 switch (ch)
5855 {
Daniel Veillard92ad2102001-03-27 12:47:33 +00005856 case 'n':
5857 case 'N':
5858 /* Not-a-number */
5859 if (index != 0)
5860 break;
5861 /* FALLTHROUGH */
Daniel Veillard92ad2102001-03-27 12:47:33 +00005862 case 'i':
5863 case 'I':
5864 /* Infinity */
5865 while (isalpha(ch) && (index - start < width))
5866 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005867 doubleString[index++] = (char)ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005868 self->InStream(self, &ch);
5869 }
5870 doubleString[index] = NIL;
5871
Daniel Veillard92ad2102001-03-27 12:47:33 +00005872 /* Case insensitive string comparison */
Bjorn Reese026d29f2002-01-19 15:40:18 +00005873 if (trio_equal(&doubleString[start], INFINITE_UPPER) ||
5874 trio_equal(&doubleString[start], LONG_INFINITE_UPPER))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005875 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005876 if (flags & FLAGS_LONGDOUBLE)
5877 {
5878 if ((start == 1) && (doubleString[0] == '-'))
5879 {
5880 *((trio_long_double_t *)target) = trio_ninf();
5881 }
5882 else
5883 {
5884 *((trio_long_double_t *)target) = trio_pinf();
5885 }
5886 }
5887 else
5888 {
5889 if ((start == 1) && (doubleString[0] == '-'))
5890 {
5891 *((double *)target) = trio_ninf();
5892 }
5893 else
5894 {
5895 *((double *)target) = trio_pinf();
5896 }
5897 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00005898 return TRUE;
5899 }
Bjorn Reese026d29f2002-01-19 15:40:18 +00005900 if (trio_equal(doubleString, NAN_UPPER))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005901 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00005902 /* NaN must not have a preceeding + nor - */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005903 if (flags & FLAGS_LONGDOUBLE)
5904 {
5905 *((trio_long_double_t *)target) = trio_nan();
5906 }
5907 else
5908 {
5909 *((double *)target) = trio_nan();
5910 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00005911 return TRUE;
5912 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00005913 return FALSE;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005914
5915 case '0':
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005916 doubleString[index++] = (char)ch;
5917 self->InStream(self, &ch);
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005918 if (trio_to_upper(ch) == 'X')
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005919 {
5920 isHex = TRUE;
5921 doubleString[index++] = (char)ch;
5922 self->InStream(self, &ch);
5923 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005924 break;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005925
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005926 default:
5927 break;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005928 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005929
Bjorn Reese70a9da52001-04-21 16:57:29 +00005930 while ((ch != EOF) && (index - start < width))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005931 {
5932 /* Integer part */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005933 if (isHex ? isxdigit(ch) : isdigit(ch))
Bjorn Reese70a9da52001-04-21 16:57:29 +00005934 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005935 doubleString[index++] = (char)ch;
Bjorn Reese70a9da52001-04-21 16:57:29 +00005936 self->InStream(self, &ch);
5937 }
5938 else if (flags & FLAGS_QUOTE)
5939 {
5940 /* Compare with thousands separator */
5941 for (j = 0; internalThousandSeparator[j] && self->current; j++)
5942 {
5943 if (internalThousandSeparator[j] != self->current)
5944 break;
5945
5946 self->InStream(self, &ch);
5947 }
5948 if (internalThousandSeparator[j])
5949 break; /* Mismatch */
5950 else
5951 continue; /* Match */
5952 }
5953 else
5954 break; /* while */
Daniel Veillard92ad2102001-03-27 12:47:33 +00005955 }
5956 if (ch == '.')
5957 {
5958 /* Decimal part */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005959 doubleString[index++] = (char)ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005960 self->InStream(self, &ch);
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005961 while ((isHex ? isxdigit(ch) : isdigit(ch)) &&
5962 (index - start < width))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005963 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005964 doubleString[index++] = (char)ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005965 self->InStream(self, &ch);
5966 }
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005967 if (isHex ? (trio_to_upper(ch) == 'P') : (trio_to_upper(ch) == 'E'))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005968 {
5969 /* Exponent */
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005970 doubleString[index++] = (char)ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005971 self->InStream(self, &ch);
5972 if ((ch == '+') || (ch == '-'))
5973 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005974 doubleString[index++] = (char)ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005975 self->InStream(self, &ch);
5976 }
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00005977 while (isdigit(ch) && (index - start < width))
Daniel Veillard92ad2102001-03-27 12:47:33 +00005978 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00005979 doubleString[index++] = (char)ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00005980 self->InStream(self, &ch);
5981 }
5982 }
5983 }
5984
5985 if ((index == start) || (*doubleString == NIL))
5986 return FALSE;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005987
5988 doubleString[index] = 0;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08005989
Daniel Veillard92ad2102001-03-27 12:47:33 +00005990 if (flags & FLAGS_LONGDOUBLE)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005991 {
5992 *((trio_long_double_t *)target) = trio_to_long_double(doubleString, NULL);
5993 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00005994 else
5995 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00005996 *((double *)target) = trio_to_double(doubleString, NULL);
Daniel Veillard92ad2102001-03-27 12:47:33 +00005997 }
5998 return TRUE;
5999}
6000
6001/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00006002 * TrioReadPointer
Daniel Veillard92ad2102001-03-27 12:47:33 +00006003 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006004TRIO_PRIVATE BOOLEAN_T
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006005TrioReadPointer
6006TRIO_ARGS3((self, target, flags),
6007 trio_class_t *self,
6008 trio_pointer_t *target,
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00006009 trio_flags_t flags)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006010{
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006011 trio_uintmax_t number;
Bjorn Reese026d29f2002-01-19 15:40:18 +00006012 char buffer[sizeof(internalNullString)];
Daniel Veillard92ad2102001-03-27 12:47:33 +00006013
6014 flags |= (FLAGS_UNSIGNED | FLAGS_ALTERNATIVE | FLAGS_NILPADDING);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006015
Daniel Veillard92ad2102001-03-27 12:47:33 +00006016 if (TrioReadNumber(self,
6017 &number,
6018 flags,
6019 POINTER_WIDTH,
6020 BASE_HEX))
6021 {
Bjorn Reese70a9da52001-04-21 16:57:29 +00006022 /*
6023 * The strange assignment of number is a workaround for a compiler
6024 * warning
6025 */
Daniel Veillard92ad2102001-03-27 12:47:33 +00006026 if (target)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006027 *target = (char *)0 + number;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006028 return TRUE;
6029 }
6030 else if (TrioReadString(self,
6031 (flags & FLAGS_IGNORE)
6032 ? NULL
6033 : buffer,
6034 0,
Bjorn Reese026d29f2002-01-19 15:40:18 +00006035 sizeof(internalNullString) - 1))
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006036 {
Bjorn Reese026d29f2002-01-19 15:40:18 +00006037 if (trio_equal_case(buffer, internalNullString))
Daniel Veillard92ad2102001-03-27 12:47:33 +00006038 {
6039 if (target)
6040 *target = NULL;
6041 return TRUE;
6042 }
6043 }
6044 return FALSE;
6045}
6046
6047/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00006048 * TrioScanProcess
Daniel Veillard92ad2102001-03-27 12:47:33 +00006049 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006050TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006051TrioScanProcess
6052TRIO_ARGS3((data, format, parameters),
6053 trio_class_t *data,
6054 TRIO_CONST char *format,
6055 trio_parameter_t *parameters)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006056{
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006057#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006058 int charlen;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006059 int cnt;
Bjorn Reese026d29f2002-01-19 15:40:18 +00006060#endif
6061 int assignment;
6062 int ch;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006063 int index; /* Index of format string */
6064 int i; /* Index of current parameter */
Daniel Veillarda48ed3d2003-04-03 15:28:28 +00006065 trio_flags_t flags;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006066 int width;
6067 int base;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006068 trio_pointer_t pointer;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006069
Daniel Veillard92ad2102001-03-27 12:47:33 +00006070 assignment = 0;
6071 i = 0;
6072 index = 0;
6073 data->InStream(data, &ch);
6074
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006075#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006076 (void)mblen(NULL, 0);
Daniel Veillard92ad2102001-03-27 12:47:33 +00006077#endif
6078
6079 while (format[index])
6080 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006081#if defined(TRIO_COMPILER_SUPPORTS_MULTIBYTE)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006082 if (! isascii(format[index]))
6083 {
6084 charlen = mblen(&format[index], MB_LEN_MAX);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006085 if (charlen != -1)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006086 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006087 /* Compare multibyte characters in format string */
6088 for (cnt = 0; cnt < charlen - 1; cnt++)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006089 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006090 if (ch != format[index + cnt])
6091 {
6092 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6093 }
6094 data->InStream(data, &ch);
Daniel Veillard92ad2102001-03-27 12:47:33 +00006095 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006096 continue; /* while characters left in formatting string */
Daniel Veillard92ad2102001-03-27 12:47:33 +00006097 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00006098 }
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006099#endif /* TRIO_COMPILER_SUPPORTS_MULTIBYTE */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006100
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006101 if ((EOF == ch) && (parameters[i].type != FORMAT_COUNT))
6102 {
6103 return (assignment > 0) ? assignment : EOF;
6104 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006105
Daniel Veillard92ad2102001-03-27 12:47:33 +00006106 if (CHAR_IDENTIFIER == format[index])
6107 {
6108 if (CHAR_IDENTIFIER == format[index + 1])
6109 {
6110 /* Two % in format matches one % in input stream */
6111 if (CHAR_IDENTIFIER == ch)
6112 {
6113 data->InStream(data, &ch);
6114 index += 2;
6115 continue; /* while format chars left */
6116 }
6117 else
6118 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6119 }
6120
6121 /* Skip the parameter entries */
6122 while (parameters[i].type == FORMAT_PARAMETER)
6123 i++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006124
Daniel Veillard92ad2102001-03-27 12:47:33 +00006125 flags = parameters[i].flags;
6126 /* Find width */
6127 width = parameters[i].width;
6128 if (flags & FLAGS_WIDTH_PARAMETER)
6129 {
6130 /* Get width from parameter list */
Bjorn Reese70a9da52001-04-21 16:57:29 +00006131 width = (int)parameters[width].data.number.as_signed;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006132 }
6133 /* Find base */
6134 base = parameters[i].base;
6135 if (flags & FLAGS_BASE_PARAMETER)
6136 {
6137 /* Get base from parameter list */
Bjorn Reese70a9da52001-04-21 16:57:29 +00006138 base = (int)parameters[base].data.number.as_signed;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006139 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006140
Daniel Veillard92ad2102001-03-27 12:47:33 +00006141 switch (parameters[i].type)
6142 {
6143 case FORMAT_INT:
6144 {
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006145 trio_uintmax_t number;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006146
6147 if (0 == base)
6148 base = BASE_DECIMAL;
6149
6150 if (!TrioReadNumber(data,
6151 &number,
6152 flags,
6153 width,
6154 base))
6155 return assignment;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006156
Daniel Veillard92ad2102001-03-27 12:47:33 +00006157 if (!(flags & FLAGS_IGNORE))
6158 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006159 assignment++;
6160
Daniel Veillard92ad2102001-03-27 12:47:33 +00006161 pointer = parameters[i].data.pointer;
6162#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6163 if (flags & FLAGS_SIZE_T)
6164 *(size_t *)pointer = (size_t)number;
6165 else
6166#endif
6167#if defined(QUALIFIER_PTRDIFF_T)
6168 if (flags & FLAGS_PTRDIFF_T)
6169 *(ptrdiff_t *)pointer = (ptrdiff_t)number;
6170 else
6171#endif
6172#if defined(QUALIFIER_INTMAX_T)
6173 if (flags & FLAGS_INTMAX_T)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006174 *(trio_intmax_t *)pointer = (trio_intmax_t)number;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006175 else
6176#endif
6177 if (flags & FLAGS_QUAD)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006178 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)number;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006179 else if (flags & FLAGS_LONG)
6180 *(long int *)pointer = (long int)number;
6181 else if (flags & FLAGS_SHORT)
6182 *(short int *)pointer = (short int)number;
6183 else
6184 *(int *)pointer = (int)number;
6185 }
6186 }
6187 break; /* FORMAT_INT */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006188
Daniel Veillard92ad2102001-03-27 12:47:33 +00006189 case FORMAT_STRING:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006190#if TRIO_WIDECHAR
6191 if (flags & FLAGS_WIDECHAR)
6192 {
6193 if (!TrioReadWideString(data,
6194 (flags & FLAGS_IGNORE)
6195 ? NULL
6196 : parameters[i].data.wstring,
6197 flags,
6198 width))
6199 return assignment;
6200 }
6201 else
6202#endif
6203 {
6204 if (!TrioReadString(data,
6205 (flags & FLAGS_IGNORE)
6206 ? NULL
6207 : parameters[i].data.string,
6208 flags,
6209 width))
6210 return assignment;
6211 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006212 if (!(flags & FLAGS_IGNORE))
6213 assignment++;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006214 break; /* FORMAT_STRING */
Daniel Veillard92ad2102001-03-27 12:47:33 +00006215
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006216 case FORMAT_DOUBLE:
6217 {
6218 trio_pointer_t pointer;
6219
6220 if (flags & FLAGS_IGNORE)
6221 {
6222 pointer = NULL;
6223 }
6224 else
6225 {
6226 pointer = (flags & FLAGS_LONGDOUBLE)
6227 ? (trio_pointer_t)parameters[i].data.longdoublePointer
6228 : (trio_pointer_t)parameters[i].data.doublePointer;
6229 }
6230 if (!TrioReadDouble(data, pointer, flags, width))
6231 {
6232 return assignment;
6233 }
6234 if (!(flags & FLAGS_IGNORE))
6235 {
6236 assignment++;
6237 }
6238 break; /* FORMAT_DOUBLE */
6239 }
Daniel Veillard92ad2102001-03-27 12:47:33 +00006240 case FORMAT_GROUP:
6241 {
6242 int characterclass[MAX_CHARACTER_CLASS + 1];
6243 int rc;
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006244
6245 /* Skip over modifiers */
6246 while (format[index] != SPECIFIER_GROUP)
6247 {
6248 index++;
6249 }
6250 /* Skip over group specifier */
6251 index++;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006252
Daniel Veillard92ad2102001-03-27 12:47:33 +00006253 memset(characterclass, 0, sizeof(characterclass));
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006254 rc = TrioGetCharacterClass(format,
6255 &index,
6256 &flags,
Daniel Veillard92ad2102001-03-27 12:47:33 +00006257 characterclass);
6258 if (rc < 0)
6259 return rc;
6260
6261 if (!TrioReadGroup(data,
6262 (flags & FLAGS_IGNORE)
6263 ? NULL
6264 : parameters[i].data.string,
6265 characterclass,
6266 flags,
6267 parameters[i].width))
6268 return assignment;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006269 if (!(flags & FLAGS_IGNORE))
6270 assignment++;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006271 }
6272 break; /* FORMAT_GROUP */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006273
Daniel Veillard92ad2102001-03-27 12:47:33 +00006274 case FORMAT_COUNT:
6275 pointer = parameters[i].data.pointer;
6276 if (NULL != pointer)
6277 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006278 int count = data->committed;
6279 if (ch != EOF)
6280 count--; /* a character is read, but is not consumed yet */
Daniel Veillard92ad2102001-03-27 12:47:33 +00006281#if defined(QUALIFIER_SIZE_T) || defined(QUALIFIER_SIZE_T_UPPER)
6282 if (flags & FLAGS_SIZE_T)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006283 *(size_t *)pointer = (size_t)count;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006284 else
6285#endif
6286#if defined(QUALIFIER_PTRDIFF_T)
6287 if (flags & FLAGS_PTRDIFF_T)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006288 *(ptrdiff_t *)pointer = (ptrdiff_t)count;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006289 else
6290#endif
6291#if defined(QUALIFIER_INTMAX_T)
6292 if (flags & FLAGS_INTMAX_T)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006293 *(trio_intmax_t *)pointer = (trio_intmax_t)count;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006294 else
6295#endif
6296 if (flags & FLAGS_QUAD)
6297 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006298 *(trio_ulonglong_t *)pointer = (trio_ulonglong_t)count;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006299 }
6300 else if (flags & FLAGS_LONG)
6301 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006302 *(long int *)pointer = (long int)count;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006303 }
6304 else if (flags & FLAGS_SHORT)
6305 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006306 *(short int *)pointer = (short int)count;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006307 }
6308 else
6309 {
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006310 *(int *)pointer = (int)count;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006311 }
6312 }
6313 break; /* FORMAT_COUNT */
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006314
Daniel Veillard92ad2102001-03-27 12:47:33 +00006315 case FORMAT_CHAR:
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006316#if TRIO_WIDECHAR
6317 if (flags & FLAGS_WIDECHAR)
6318 {
6319 if (TrioReadWideChar(data,
6320 (flags & FLAGS_IGNORE)
6321 ? NULL
6322 : parameters[i].data.wstring,
6323 flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006324 (width == NO_WIDTH) ? 1 : width) == 0)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006325 return assignment;
6326 }
6327 else
6328#endif
6329 {
6330 if (TrioReadChar(data,
6331 (flags & FLAGS_IGNORE)
6332 ? NULL
6333 : parameters[i].data.string,
6334 flags,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006335 (width == NO_WIDTH) ? 1 : width) == 0)
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006336 return assignment;
6337 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006338 if (!(flags & FLAGS_IGNORE))
6339 assignment++;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006340 break; /* FORMAT_CHAR */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006341
Daniel Veillard92ad2102001-03-27 12:47:33 +00006342 case FORMAT_POINTER:
6343 if (!TrioReadPointer(data,
6344 (flags & FLAGS_IGNORE)
6345 ? NULL
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006346 : (trio_pointer_t *)parameters[i].data.pointer,
Daniel Veillard92ad2102001-03-27 12:47:33 +00006347 flags))
6348 return assignment;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006349 if (!(flags & FLAGS_IGNORE))
6350 assignment++;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006351 break; /* FORMAT_POINTER */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006352
Daniel Veillard92ad2102001-03-27 12:47:33 +00006353 case FORMAT_PARAMETER:
6354 break; /* FORMAT_PARAMETER */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006355
Daniel Veillard92ad2102001-03-27 12:47:33 +00006356 default:
6357 return TRIO_ERROR_RETURN(TRIO_EINVAL, index);
6358 }
6359 ch = data->current;
6360 index = parameters[i].indexAfterSpecifier;
6361 i++;
6362 }
6363 else /* Not an % identifier */
6364 {
6365 if (isspace((int)format[index]))
6366 {
6367 /* Whitespaces may match any amount of whitespaces */
6368 ch = TrioSkipWhitespaces(data);
6369 }
6370 else if (ch == format[index])
6371 {
6372 data->InStream(data, &ch);
6373 }
6374 else
Bjorn Reese026d29f2002-01-19 15:40:18 +00006375 return assignment;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006376
Daniel Veillard92ad2102001-03-27 12:47:33 +00006377 index++;
6378 }
6379 }
6380 return assignment;
6381}
6382
6383/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00006384 * TrioScan
Daniel Veillard92ad2102001-03-27 12:47:33 +00006385 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006386TRIO_PRIVATE int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006387TrioScan
6388TRIO_ARGS6((source, sourceSize, InStream, format, arglist, argarray),
6389 trio_pointer_t source,
6390 size_t sourceSize,
6391 void (*InStream) TRIO_PROTO((trio_class_t *, int *)),
6392 TRIO_CONST char *format,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006393 TRIO_VA_LIST_PTR arglist,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006394 trio_pointer_t *argarray)
Bjorn Reese026d29f2002-01-19 15:40:18 +00006395{
6396 int status;
6397 trio_parameter_t parameters[MAX_PARAMETERS];
6398 trio_class_t data;
6399
6400 assert(VALID(InStream));
6401 assert(VALID(format));
6402
6403 memset(&data, 0, sizeof(data));
6404 data.InStream = InStream;
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006405 data.location = (trio_pointer_t)source;
Bjorn Reese026d29f2002-01-19 15:40:18 +00006406 data.max = sourceSize;
6407 data.error = 0;
6408
6409#if defined(USE_LOCALE)
6410 if (NULL == internalLocaleValues)
6411 {
6412 TrioSetLocale();
6413 }
6414#endif
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006415
Bjorn Reese026d29f2002-01-19 15:40:18 +00006416 status = TrioParse(TYPE_SCAN, format, parameters, arglist, argarray);
6417 if (status < 0)
6418 return status;
6419
6420 status = TrioScanProcess(&data, format, parameters);
6421 if (data.error != 0)
6422 {
6423 status = data.error;
6424 }
6425 return status;
6426}
6427
6428/*************************************************************************
6429 * TrioInStreamFile
6430 */
6431TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006432TrioInStreamFile
6433TRIO_ARGS2((self, intPointer),
6434 trio_class_t *self,
6435 int *intPointer)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006436{
Gaurav Guptae036cb32014-07-14 21:22:07 +08006437 FILE *file;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006438
6439 assert(VALID(self));
Gaurav Guptae036cb32014-07-14 21:22:07 +08006440 assert(VALID(self->location));
Daniel Veillard92ad2102001-03-27 12:47:33 +00006441 assert(VALID(file));
6442
Gaurav Guptae036cb32014-07-14 21:22:07 +08006443 file = (FILE *)self->location;
6444
Daniel Veillard92ad2102001-03-27 12:47:33 +00006445 self->current = fgetc(file);
Bjorn Reese026d29f2002-01-19 15:40:18 +00006446 if (self->current == EOF)
6447 {
6448 self->error = (ferror(file))
6449 ? TRIO_ERROR_RETURN(TRIO_ERRNO, 0)
6450 : TRIO_ERROR_RETURN(TRIO_EOF, 0);
6451 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006452 else
6453 {
6454 self->processed++;
6455 self->committed++;
6456 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006457
Daniel Veillard92ad2102001-03-27 12:47:33 +00006458 if (VALID(intPointer))
6459 {
6460 *intPointer = self->current;
6461 }
6462}
6463
6464/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00006465 * TrioInStreamFileDescriptor
Daniel Veillard92ad2102001-03-27 12:47:33 +00006466 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006467TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006468TrioInStreamFileDescriptor
6469TRIO_ARGS2((self, intPointer),
6470 trio_class_t *self,
6471 int *intPointer)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006472{
Gaurav Guptae036cb32014-07-14 21:22:07 +08006473 int fd;
Daniel Veillard92ad2102001-03-27 12:47:33 +00006474 int size;
6475 unsigned char input;
6476
6477 assert(VALID(self));
Gaurav Guptae036cb32014-07-14 21:22:07 +08006478 assert(VALID(self->location));
6479
6480 fd = *((int *)self->location);
Daniel Veillard92ad2102001-03-27 12:47:33 +00006481
6482 size = read(fd, &input, sizeof(char));
Bjorn Reese026d29f2002-01-19 15:40:18 +00006483 if (size == -1)
6484 {
6485 self->error = TRIO_ERROR_RETURN(TRIO_ERRNO, 0);
6486 self->current = EOF;
6487 }
6488 else
6489 {
6490 self->current = (size == 0) ? EOF : input;
6491 }
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006492 if (self->current != EOF)
6493 {
6494 self->committed++;
6495 self->processed++;
6496 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006497
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006498 if (VALID(intPointer))
6499 {
6500 *intPointer = self->current;
6501 }
6502}
6503
6504/*************************************************************************
6505 * TrioInStreamCustom
6506 */
6507TRIO_PRIVATE void
6508TrioInStreamCustom
6509TRIO_ARGS2((self, intPointer),
6510 trio_class_t *self,
6511 int *intPointer)
6512{
6513 trio_custom_t *data;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006514
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006515 assert(VALID(self));
6516 assert(VALID(self->location));
6517
6518 data = (trio_custom_t *)self->location;
6519
6520 self->current = (data->stream.in == NULL)
6521 ? NIL
6522 : (data->stream.in)(data->closure);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006523
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006524 if (self->current == NIL)
6525 {
6526 self->current = EOF;
6527 }
6528 else
6529 {
6530 self->processed++;
6531 self->committed++;
6532 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006533
Daniel Veillard92ad2102001-03-27 12:47:33 +00006534 if (VALID(intPointer))
6535 {
6536 *intPointer = self->current;
6537 }
6538}
6539
6540/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00006541 * TrioInStreamString
Daniel Veillard92ad2102001-03-27 12:47:33 +00006542 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006543TRIO_PRIVATE void
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006544TrioInStreamString
6545TRIO_ARGS2((self, intPointer),
6546 trio_class_t *self,
6547 int *intPointer)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006548{
6549 unsigned char **buffer;
6550
6551 assert(VALID(self));
Daniel Veillard92ad2102001-03-27 12:47:33 +00006552 assert(VALID(self->location));
6553
6554 buffer = (unsigned char **)self->location;
6555 self->current = (*buffer)[0];
6556 if (self->current == NIL)
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006557 {
6558 self->current = EOF;
6559 }
6560 else
6561 {
6562 (*buffer)++;
6563 self->processed++;
6564 self->committed++;
6565 }
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006566
Daniel Veillard92ad2102001-03-27 12:47:33 +00006567 if (VALID(intPointer))
6568 {
6569 *intPointer = self->current;
6570 }
6571}
6572
6573/*************************************************************************
Bjorn Reese026d29f2002-01-19 15:40:18 +00006574 *
6575 * Formatted scanning functions
6576 *
6577 ************************************************************************/
6578
6579#if defined(TRIO_DOCUMENTATION)
6580# include "doc/doc_scanf.h"
6581#endif
6582/** @addtogroup Scanf
6583 @{
6584*/
6585
6586/*************************************************************************
Bjorn Reese70a9da52001-04-21 16:57:29 +00006587 * scanf
Daniel Veillard92ad2102001-03-27 12:47:33 +00006588 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006589
6590/**
6591 Scan characters from standard input stream.
6592
6593 @param format Formatting string.
6594 @param ... Arguments.
6595 @return Number of scanned characters.
6596 */
6597TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006598trio_scanf
6599TRIO_VARGS2((format, va_alist),
6600 TRIO_CONST char *format,
6601 TRIO_VA_DECL)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006602{
6603 int status;
6604 va_list args;
6605
6606 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006607
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006608 TRIO_VA_START(args, format);
6609 status = TrioScan((trio_pointer_t)stdin, 0,
6610 TrioInStreamFile,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006611 format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006612 TRIO_VA_END(args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006613 return status;
6614}
6615
Bjorn Reese026d29f2002-01-19 15:40:18 +00006616TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006617trio_vscanf
6618TRIO_ARGS2((format, args),
6619 TRIO_CONST char *format,
6620 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006621{
6622 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006623
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006624 return TrioScan((trio_pointer_t)stdin, 0,
6625 TrioInStreamFile,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006626 format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006627}
6628
Bjorn Reese026d29f2002-01-19 15:40:18 +00006629TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006630trio_scanfv
6631TRIO_ARGS2((format, args),
6632 TRIO_CONST char *format,
6633 trio_pointer_t *args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006634{
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006635 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006636
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006637 return TrioScan((trio_pointer_t)stdin, 0,
6638 TrioInStreamFile,
6639 format, NULL, args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006640}
6641
6642/*************************************************************************
6643 * fscanf
6644 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006645TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006646trio_fscanf
6647TRIO_VARGS3((file, format, va_alist),
6648 FILE *file,
Bjorn Reese026d29f2002-01-19 15:40:18 +00006649 TRIO_CONST char *format,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006650 TRIO_VA_DECL)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006651{
6652 int status;
6653 va_list args;
6654
6655 assert(VALID(file));
6656 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006657
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006658 TRIO_VA_START(args, format);
6659 status = TrioScan((trio_pointer_t)file, 0,
6660 TrioInStreamFile,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006661 format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006662 TRIO_VA_END(args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006663 return status;
6664}
6665
Bjorn Reese026d29f2002-01-19 15:40:18 +00006666TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006667trio_vfscanf
6668TRIO_ARGS3((file, format, args),
6669 FILE *file,
6670 TRIO_CONST char *format,
6671 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006672{
6673 assert(VALID(file));
6674 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006675
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006676 return TrioScan((trio_pointer_t)file, 0,
6677 TrioInStreamFile,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006678 format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006679}
6680
Bjorn Reese026d29f2002-01-19 15:40:18 +00006681TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006682trio_fscanfv
6683TRIO_ARGS3((file, format, args),
6684 FILE *file,
6685 TRIO_CONST char *format,
6686 trio_pointer_t *args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006687{
6688 assert(VALID(file));
6689 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006690
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006691 return TrioScan((trio_pointer_t)file, 0,
6692 TrioInStreamFile,
6693 format, NULL, args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006694}
6695
6696/*************************************************************************
6697 * dscanf
6698 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006699TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006700trio_dscanf
6701TRIO_VARGS3((fd, format, va_alist),
6702 int fd,
Bjorn Reese026d29f2002-01-19 15:40:18 +00006703 TRIO_CONST char *format,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006704 TRIO_VA_DECL)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006705{
6706 int status;
6707 va_list args;
6708
6709 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006710
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006711 TRIO_VA_START(args, format);
6712 status = TrioScan((trio_pointer_t)&fd, 0,
6713 TrioInStreamFileDescriptor,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006714 format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006715 TRIO_VA_END(args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006716 return status;
6717}
6718
Bjorn Reese026d29f2002-01-19 15:40:18 +00006719TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006720trio_vdscanf
6721TRIO_ARGS3((fd, format, args),
6722 int fd,
6723 TRIO_CONST char *format,
6724 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006725{
6726 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006727
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006728 return TrioScan((trio_pointer_t)&fd, 0,
6729 TrioInStreamFileDescriptor,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006730 format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006731}
6732
Bjorn Reese026d29f2002-01-19 15:40:18 +00006733TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006734trio_dscanfv
6735TRIO_ARGS3((fd, format, args),
6736 int fd,
6737 TRIO_CONST char *format,
6738 trio_pointer_t *args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006739{
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006740 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006741
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006742 return TrioScan((trio_pointer_t)&fd, 0,
6743 TrioInStreamFileDescriptor,
6744 format, NULL, args);
6745}
6746
6747/*************************************************************************
6748 * cscanf
6749 */
6750TRIO_PUBLIC int
6751trio_cscanf
6752TRIO_VARGS4((stream, closure, format, va_alist),
6753 trio_instream_t stream,
6754 trio_pointer_t closure,
6755 TRIO_CONST char *format,
6756 TRIO_VA_DECL)
6757{
6758 int status;
6759 va_list args;
6760 trio_custom_t data;
6761
6762 assert(VALID(stream));
6763 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006764
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006765 TRIO_VA_START(args, format);
6766 data.stream.in = stream;
6767 data.closure = closure;
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006768 status = TrioScan(&data, 0, TrioInStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006769 TRIO_VA_END(args);
6770 return status;
6771}
6772
6773TRIO_PUBLIC int
6774trio_vcscanf
6775TRIO_ARGS4((stream, closure, format, args),
6776 trio_instream_t stream,
6777 trio_pointer_t closure,
6778 TRIO_CONST char *format,
6779 va_list args)
6780{
6781 trio_custom_t data;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006782
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006783 assert(VALID(stream));
6784 assert(VALID(format));
6785
6786 data.stream.in = stream;
6787 data.closure = closure;
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006788 return TrioScan(&data, 0, TrioInStreamCustom, format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006789}
6790
6791TRIO_PUBLIC int
6792trio_cscanfv
6793TRIO_ARGS4((stream, closure, format, args),
6794 trio_instream_t stream,
6795 trio_pointer_t closure,
6796 TRIO_CONST char *format,
6797 trio_pointer_t *args)
6798{
6799 trio_custom_t data;
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006800
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006801 assert(VALID(stream));
6802 assert(VALID(format));
6803
6804 data.stream.in = stream;
6805 data.closure = closure;
6806 return TrioScan(&data, 0, TrioInStreamCustom, format, NULL, args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006807}
6808
6809/*************************************************************************
6810 * sscanf
6811 */
Bjorn Reese026d29f2002-01-19 15:40:18 +00006812TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006813trio_sscanf
6814TRIO_VARGS3((buffer, format, va_alist),
6815 TRIO_CONST char *buffer,
Bjorn Reese026d29f2002-01-19 15:40:18 +00006816 TRIO_CONST char *format,
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006817 TRIO_VA_DECL)
Daniel Veillard92ad2102001-03-27 12:47:33 +00006818{
6819 int status;
6820 va_list args;
6821
6822 assert(VALID(buffer));
6823 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006824
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006825 TRIO_VA_START(args, format);
6826 status = TrioScan((trio_pointer_t)&buffer, 0,
6827 TrioInStreamString,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006828 format, TRIO_VA_LIST_ADDR(args), NULL);
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006829 TRIO_VA_END(args);
Daniel Veillard92ad2102001-03-27 12:47:33 +00006830 return status;
6831}
6832
Bjorn Reese026d29f2002-01-19 15:40:18 +00006833TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006834trio_vsscanf
6835TRIO_ARGS3((buffer, format, args),
6836 TRIO_CONST char *buffer,
6837 TRIO_CONST char *format,
6838 va_list args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006839{
6840 assert(VALID(buffer));
6841 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006842
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006843 return TrioScan((trio_pointer_t)&buffer, 0,
6844 TrioInStreamString,
Patrick Monnerat0f7a26d2013-12-12 15:04:43 +08006845 format, TRIO_VA_LIST_ADDR(args), NULL);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006846}
6847
Bjorn Reese026d29f2002-01-19 15:40:18 +00006848TRIO_PUBLIC int
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006849trio_sscanfv
6850TRIO_ARGS3((buffer, format, args),
6851 TRIO_CONST char *buffer,
6852 TRIO_CONST char *format,
6853 trio_pointer_t *args)
Bjorn Reese70a9da52001-04-21 16:57:29 +00006854{
6855 assert(VALID(buffer));
6856 assert(VALID(format));
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006857
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006858 return TrioScan((trio_pointer_t)&buffer, 0,
6859 TrioInStreamString,
6860 format, NULL, args);
Bjorn Reese70a9da52001-04-21 16:57:29 +00006861}
Bjorn Reese906ec8a2001-06-05 12:46:33 +00006862
Bjorn Reese026d29f2002-01-19 15:40:18 +00006863/** @} End of Scanf documentation module */
Daniel Veillardb7c29c32002-09-25 22:44:43 +00006864
6865/*************************************************************************
6866 * trio_strerror
6867 */
6868TRIO_PUBLIC TRIO_CONST char *
6869trio_strerror
6870TRIO_ARGS1((errorcode),
6871 int errorcode)
6872{
6873 /* Textual versions of the error codes */
6874 switch (TRIO_ERROR_CODE(errorcode))
6875 {
6876 case TRIO_EOF:
6877 return "End of file";
6878 case TRIO_EINVAL:
6879 return "Invalid argument";
6880 case TRIO_ETOOMANY:
6881 return "Too many arguments";
6882 case TRIO_EDBLREF:
6883 return "Double reference";
6884 case TRIO_EGAP:
6885 return "Reference gap";
6886 case TRIO_ENOMEM:
6887 return "Out of memory";
6888 case TRIO_ERANGE:
6889 return "Invalid range";
6890 case TRIO_ECUSTOM:
6891 return "Custom error";
6892 default:
6893 return "Unknown";
6894 }
6895}