forked from kiyolee/libxml2-win-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trio.c
6895 lines (6261 loc) · 157 KB
/
trio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************
*
* $Id$
*
* Copyright (C) 1998 Bjorn Reese and Daniel Stenberg.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
*************************************************************************
*
* A note to trio contributors:
*
* Avoid heap allocation at all costs to ensure that the trio functions
* are async-safe. The exceptions are the printf/fprintf functions, which
* uses fputc, and the asprintf functions and the <alloc> modifier, which
* by design are required to allocate form the heap.
*
************************************************************************/
/*
* TODO:
* - Scan is probably too permissive about its modifiers.
* - C escapes in %#[] ?
* - Multibyte characters (done for format parsing, except scan groups)
* - Complex numbers? (C99 _Complex)
* - Boolean values? (C99 _Bool)
* - C99 NaN(n-char-sequence) missing. The n-char-sequence can be used
* to print the mantissa, e.g. NaN(0xc000000000000000)
* - Should we support the GNU %a alloc modifier? GNU has an ugly hack
* for %a, because C99 used %a for other purposes. If specified as
* %as or %a[ it is interpreted as the alloc modifier, otherwise as
* the C99 hex-float. This means that you cannot scan %as as a hex-float
* immediately followed by an 's'.
* - Scanning of collating symbols.
*/
/*************************************************************************
* Trio include files
*/
#include "triodef.h"
#include "trio.h"
#include "triop.h"
#include "trionan.h"
#if !defined(TRIO_MINIMAL)
# include "triostr.h"
#endif
/**************************************************************************
*
* Definitions
*
*************************************************************************/
#include <math.h>
#include <limits.h>
#include <float.h>
#if (defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) \
|| defined(USE_MULTIBYTE) || TRIO_WIDECHAR) \
&& !defined(_WIN32_WCE)
# define TRIO_COMPILER_SUPPORTS_MULTIBYTE
# if !defined(MB_LEN_MAX)
# define MB_LEN_MAX 6
# endif
#endif
#if (defined(TRIO_COMPILER_MSVC) && (_MSC_VER >= 1100)) || defined(TRIO_COMPILER_BCB)
# define TRIO_COMPILER_SUPPORTS_MSVC_INT
#endif
#if defined(_WIN32_WCE)
#include <wincecompat.h>
#endif
/*************************************************************************
* Generic definitions
*/
#if !(defined(DEBUG) || defined(NDEBUG))
# define NDEBUG
#endif
#include <assert.h>
#include <ctype.h>
#if !defined(TRIO_COMPILER_SUPPORTS_C99)
# define isblank(x) (((x)==32) || ((x)==9))
#endif
#if defined(TRIO_COMPILER_ANCIENT)
# include <varargs.h>
#else
# include <stdarg.h>
#endif
#include <stddef.h>
#if defined( HAVE_ERRNO_H ) || defined( __VMS )
#include <errno.h>
#endif
#ifndef NULL
# define NULL 0
#endif
#define NIL ((char)0)
#ifndef FALSE
# define FALSE (1 == 0)
# define TRUE (! FALSE)
#endif
#define BOOLEAN_T int
/* mincore() can be used for debugging purposes */
#define VALID(x) (NULL != (x))
#if TRIO_ERRORS
/*
* Encode the error code and the position. This is decoded
* with TRIO_ERROR_CODE and TRIO_ERROR_POSITION.
*/
# define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
#else
# define TRIO_ERROR_RETURN(x,y) (-1)
#endif
#ifndef VA_LIST_IS_ARRAY
#define TRIO_VA_LIST_PTR va_list *
#define TRIO_VA_LIST_ADDR(l) (&(l))
#define TRIO_VA_LIST_DEREF(l) (*(l))
#else
#define TRIO_VA_LIST_PTR va_list
#define TRIO_VA_LIST_ADDR(l) (l)
#define TRIO_VA_LIST_DEREF(l) (l)
#endif
typedef unsigned long trio_flags_t;
/*************************************************************************
* Platform specific definitions
*/
#if defined(TRIO_PLATFORM_UNIX) || defined(TRIO_PLATFORM_OS400)
# include <unistd.h>
# include <signal.h>
# include <locale.h>
# define USE_LOCALE
#endif /* TRIO_PLATFORM_UNIX */
#if defined(TRIO_PLATFORM_VMS)
# include <unistd.h>
#endif
#if defined(TRIO_PLATFORM_WIN32)
# if defined(_WIN32_WCE)
# include <wincecompat.h>
# else
# include <io.h>
# define read _read
# define write _write
# endif
#endif /* TRIO_PLATFORM_WIN32 */
#if TRIO_WIDECHAR
# if defined(TRIO_COMPILER_SUPPORTS_ISO94)
# include <wchar.h>
# include <wctype.h>
typedef wchar_t trio_wchar_t;
typedef wint_t trio_wint_t;
# else
typedef char trio_wchar_t;
typedef int trio_wint_t;
# define WCONST(x) L ## x
# define WEOF EOF
# define iswalnum(x) isalnum(x)
# define iswalpha(x) isalpha(x)
# define iswblank(x) isblank(x)
# define iswcntrl(x) iscntrl(x)
# define iswdigit(x) isdigit(x)
# define iswgraph(x) isgraph(x)
# define iswlower(x) islower(x)
# define iswprint(x) isprint(x)
# define iswpunct(x) ispunct(x)
# define iswspace(x) isspace(x)
# define iswupper(x) isupper(x)
# define iswxdigit(x) isxdigit(x)
# endif
#endif
/*************************************************************************
* Compiler dependent definitions
*/
/* Support for long long */
#ifndef __cplusplus
# if !defined(USE_LONGLONG)
# if defined(TRIO_COMPILER_GCC) && !defined(__STRICT_ANSI__)
# define USE_LONGLONG
# elif defined(TRIO_COMPILER_SUNPRO)
# define USE_LONGLONG
# elif defined(_LONG_LONG) || defined(_LONGLONG)
# define USE_LONGLONG
# endif
# endif
#endif
/* The extra long numbers */
#if defined(USE_LONGLONG)
typedef signed long long int trio_longlong_t;
typedef unsigned long long int trio_ulonglong_t;
#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
typedef signed __int64 trio_longlong_t;
typedef unsigned __int64 trio_ulonglong_t;
#else
typedef TRIO_SIGNED long int trio_longlong_t;
typedef unsigned long int trio_ulonglong_t;
#endif
/* Maximal and fixed integer types */
#if defined(TRIO_COMPILER_SUPPORTS_C99) && !defined( __VMS )
# include <stdint.h>
typedef intmax_t trio_intmax_t;
typedef uintmax_t trio_uintmax_t;
typedef int8_t trio_int8_t;
typedef int16_t trio_int16_t;
typedef int32_t trio_int32_t;
typedef int64_t trio_int64_t;
#elif defined(TRIO_COMPILER_SUPPORTS_UNIX98) || defined( __VMS )
# include <inttypes.h>
#ifdef __VMS
typedef long long int intmax_t;
typedef unsigned long long int uintmax_t;
#endif
typedef intmax_t trio_intmax_t;
typedef uintmax_t trio_uintmax_t;
typedef int8_t trio_int8_t;
typedef int16_t trio_int16_t;
typedef int32_t trio_int32_t;
typedef int64_t trio_int64_t;
#elif defined(TRIO_COMPILER_SUPPORTS_MSVC_INT)
typedef trio_longlong_t trio_intmax_t;
typedef trio_ulonglong_t trio_uintmax_t;
typedef __int8 trio_int8_t;
typedef __int16 trio_int16_t;
typedef __int32 trio_int32_t;
typedef __int64 trio_int64_t;
#else
typedef trio_longlong_t trio_intmax_t;
typedef trio_ulonglong_t trio_uintmax_t;
# if defined(TRIO_INT8_T)
typedef TRIO_INT8_T trio_int8_t;
# else
typedef TRIO_SIGNED char trio_int8_t;
# endif
# if defined(TRIO_INT16_T)
typedef TRIO_INT16_T trio_int16_t;
# else
typedef TRIO_SIGNED short trio_int16_t;
# endif
# if defined(TRIO_INT32_T)
typedef TRIO_INT32_T trio_int32_t;
# else
typedef TRIO_SIGNED int trio_int32_t;
# endif
# if defined(TRIO_INT64_T)
typedef TRIO_INT64_T trio_int64_t;
# else
typedef trio_longlong_t trio_int64_t;
# endif
#endif
#if (!(defined(TRIO_COMPILER_SUPPORTS_C99) \
|| defined(TRIO_COMPILER_SUPPORTS_UNIX01))) \
&& !defined(_WIN32_WCE)
# define floorl(x) floor((double)(x))
# define fmodl(x,y) fmod((double)(x),(double)(y))
# define powl(x,y) pow((double)(x),(double)(y))
#endif
#define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
/*************************************************************************
* Internal Definitions
*/
#ifndef DECIMAL_DIG
# define DECIMAL_DIG DBL_DIG
#endif
/* Long double sizes */
#ifdef LDBL_DIG
# define MAX_MANTISSA_DIGITS LDBL_DIG
# define MAX_EXPONENT_DIGITS 4
# define MAX_DOUBLE_DIGITS LDBL_MAX_10_EXP
#else
# define MAX_MANTISSA_DIGITS DECIMAL_DIG
# define MAX_EXPONENT_DIGITS 3
# define MAX_DOUBLE_DIGITS DBL_MAX_10_EXP
#endif
#if defined(TRIO_COMPILER_ANCIENT) || !defined(LDBL_DIG)
# undef LDBL_DIG
# undef LDBL_MANT_DIG
# undef LDBL_EPSILON
# define LDBL_DIG DBL_DIG
# define LDBL_MANT_DIG DBL_MANT_DIG
# define LDBL_EPSILON DBL_EPSILON
#endif
/* The maximal number of digits is for base 2 */
#define MAX_CHARS_IN(x) (sizeof(x) * CHAR_BIT)
/* The width of a pointer. The number of bits in a hex digit is 4 */
#define POINTER_WIDTH ((sizeof("0x") - 1) + sizeof(trio_pointer_t) * CHAR_BIT / 4)
/* Infinite and Not-A-Number for floating-point */
#define INFINITE_LOWER "inf"
#define INFINITE_UPPER "INF"
#define LONG_INFINITE_LOWER "infinite"
#define LONG_INFINITE_UPPER "INFINITE"
#define NAN_LOWER "nan"
#define NAN_UPPER "NAN"
#if !defined(HAVE_ISASCII) && !defined(isascii)
#ifndef __VMS
# define isascii(x) ((unsigned int)(x) < 128)
#endif
#endif
/* Various constants */
enum {
TYPE_PRINT = 1,
TYPE_SCAN = 2,
/* Flags. FLAGS_LAST must be less than ULONG_MAX */
FLAGS_NEW = 0,
FLAGS_STICKY = 1,
FLAGS_SPACE = 2 * FLAGS_STICKY,
FLAGS_SHOWSIGN = 2 * FLAGS_SPACE,
FLAGS_LEFTADJUST = 2 * FLAGS_SHOWSIGN,
FLAGS_ALTERNATIVE = 2 * FLAGS_LEFTADJUST,
FLAGS_SHORT = 2 * FLAGS_ALTERNATIVE,
FLAGS_SHORTSHORT = 2 * FLAGS_SHORT,
FLAGS_LONG = 2 * FLAGS_SHORTSHORT,
FLAGS_QUAD = 2 * FLAGS_LONG,
FLAGS_LONGDOUBLE = 2 * FLAGS_QUAD,
FLAGS_SIZE_T = 2 * FLAGS_LONGDOUBLE,
FLAGS_PTRDIFF_T = 2 * FLAGS_SIZE_T,
FLAGS_INTMAX_T = 2 * FLAGS_PTRDIFF_T,
FLAGS_NILPADDING = 2 * FLAGS_INTMAX_T,
FLAGS_UNSIGNED = 2 * FLAGS_NILPADDING,
FLAGS_UPPER = 2 * FLAGS_UNSIGNED,
FLAGS_WIDTH = 2 * FLAGS_UPPER,
FLAGS_WIDTH_PARAMETER = 2 * FLAGS_WIDTH,
FLAGS_PRECISION = 2 * FLAGS_WIDTH_PARAMETER,
FLAGS_PRECISION_PARAMETER = 2 * FLAGS_PRECISION,
FLAGS_BASE = 2 * FLAGS_PRECISION_PARAMETER,
FLAGS_BASE_PARAMETER = 2 * FLAGS_BASE,
FLAGS_FLOAT_E = 2 * FLAGS_BASE_PARAMETER,
FLAGS_FLOAT_G = 2 * FLAGS_FLOAT_E,
FLAGS_QUOTE = 2 * FLAGS_FLOAT_G,
FLAGS_WIDECHAR = 2 * FLAGS_QUOTE,
FLAGS_ALLOC = 2 * FLAGS_WIDECHAR,
FLAGS_IGNORE = 2 * FLAGS_ALLOC,
FLAGS_IGNORE_PARAMETER = 2 * FLAGS_IGNORE,
FLAGS_VARSIZE_PARAMETER = 2 * FLAGS_IGNORE_PARAMETER,
FLAGS_FIXED_SIZE = 2 * FLAGS_VARSIZE_PARAMETER,
FLAGS_LAST = FLAGS_FIXED_SIZE,
/* Reused flags */
FLAGS_EXCLUDE = FLAGS_SHORT,
FLAGS_USER_DEFINED = FLAGS_IGNORE,
FLAGS_ROUNDING = FLAGS_INTMAX_T,
/* Compounded flags */
FLAGS_ALL_VARSIZES = FLAGS_LONG | FLAGS_QUAD | FLAGS_INTMAX_T | FLAGS_PTRDIFF_T | FLAGS_SIZE_T,
FLAGS_ALL_SIZES = FLAGS_ALL_VARSIZES | FLAGS_SHORTSHORT | FLAGS_SHORT,
NO_POSITION = -1,
NO_WIDTH = 0,
NO_PRECISION = -1,
NO_SIZE = -1,
/* Do not change these */
NO_BASE = -1,
MIN_BASE = 2,
MAX_BASE = 36,
BASE_BINARY = 2,
BASE_OCTAL = 8,
BASE_DECIMAL = 10,
BASE_HEX = 16,
/* Maximal number of allowed parameters */
MAX_PARAMETERS = 64,
/* Maximal number of characters in class */
MAX_CHARACTER_CLASS = UCHAR_MAX + 1,
/* Maximal string lengths for user-defined specifiers */
MAX_USER_NAME = 64,
MAX_USER_DATA = 256,
/* Maximal length of locale separator strings */
MAX_LOCALE_SEPARATOR_LENGTH = MB_LEN_MAX,
/* Maximal number of integers in grouping */
MAX_LOCALE_GROUPS = 64,
/* Initial size of asprintf buffer */
DYNAMIC_START_SIZE = 32
};
#define NO_GROUPING ((int)CHAR_MAX)
/* Fundamental formatting parameter types */
#define FORMAT_UNKNOWN 0
#define FORMAT_INT 1
#define FORMAT_DOUBLE 2
#define FORMAT_CHAR 3
#define FORMAT_STRING 4
#define FORMAT_POINTER 5
#define FORMAT_COUNT 6
#define FORMAT_PARAMETER 7
#define FORMAT_GROUP 8
#if TRIO_GNU
# define FORMAT_ERRNO 9
#endif
#if TRIO_EXTENSION
# define FORMAT_USER_DEFINED 10
#endif
/* Character constants */
#define CHAR_IDENTIFIER '%'
#define CHAR_BACKSLASH '\\'
#define CHAR_QUOTE '\"'
#define CHAR_ADJUST ' '
/* Character class expressions */
#define CLASS_ALNUM "[:alnum:]"
#define CLASS_ALPHA "[:alpha:]"
#define CLASS_BLANK "[:blank:]"
#define CLASS_CNTRL "[:cntrl:]"
#define CLASS_DIGIT "[:digit:]"
#define CLASS_GRAPH "[:graph:]"
#define CLASS_LOWER "[:lower:]"
#define CLASS_PRINT "[:print:]"
#define CLASS_PUNCT "[:punct:]"
#define CLASS_SPACE "[:space:]"
#define CLASS_UPPER "[:upper:]"
#define CLASS_XDIGIT "[:xdigit:]"
/*
* SPECIFIERS:
*
*
* a Hex-float
* A Hex-float
* c Character
* C Widechar character (wint_t)
* d Decimal
* e Float
* E Float
* F Float
* F Float
* g Float
* G Float
* i Integer
* m Error message
* n Count
* o Octal
* p Pointer
* s String
* S Widechar string (wchar_t *)
* u Unsigned
* x Hex
* X Hex
* [] Group
* <> User-defined
*
* Reserved:
*
* D Binary Coded Decimal %D(length,precision) (OS/390)
*/
#define SPECIFIER_CHAR 'c'
#define SPECIFIER_STRING 's'
#define SPECIFIER_DECIMAL 'd'
#define SPECIFIER_INTEGER 'i'
#define SPECIFIER_UNSIGNED 'u'
#define SPECIFIER_OCTAL 'o'
#define SPECIFIER_HEX 'x'
#define SPECIFIER_HEX_UPPER 'X'
#define SPECIFIER_FLOAT_E 'e'
#define SPECIFIER_FLOAT_E_UPPER 'E'
#define SPECIFIER_FLOAT_F 'f'
#define SPECIFIER_FLOAT_F_UPPER 'F'
#define SPECIFIER_FLOAT_G 'g'
#define SPECIFIER_FLOAT_G_UPPER 'G'
#define SPECIFIER_POINTER 'p'
#define SPECIFIER_GROUP '['
#define SPECIFIER_UNGROUP ']'
#define SPECIFIER_COUNT 'n'
#if TRIO_UNIX98
# define SPECIFIER_CHAR_UPPER 'C'
# define SPECIFIER_STRING_UPPER 'S'
#endif
#if TRIO_C99
# define SPECIFIER_HEXFLOAT 'a'
# define SPECIFIER_HEXFLOAT_UPPER 'A'
#endif
#if TRIO_GNU
# define SPECIFIER_ERRNO 'm'
#endif
#if TRIO_EXTENSION
# define SPECIFIER_BINARY 'b'
# define SPECIFIER_BINARY_UPPER 'B'
# define SPECIFIER_USER_DEFINED_BEGIN '<'
# define SPECIFIER_USER_DEFINED_END '>'
# define SPECIFIER_USER_DEFINED_SEPARATOR ':'
#endif
/*
* QUALIFIERS:
*
*
* Numbers = d,i,o,u,x,X
* Float = a,A,e,E,f,F,g,G
* String = s
* Char = c
*
*
* 9$ Position
* Use the 9th parameter. 9 can be any number between 1 and
* the maximal argument
*
* 9 Width
* Set width to 9. 9 can be any number, but must not be postfixed
* by '$'
*
* h Short
* Numbers:
* (unsigned) short int
*
* hh Short short
* Numbers:
* (unsigned) char
*
* l Long
* Numbers:
* (unsigned) long int
* String:
* as the S specifier
* Char:
* as the C specifier
*
* ll Long Long
* Numbers:
* (unsigned) long long int
*
* L Long Double
* Float
* long double
*
* # Alternative
* Float:
* Decimal-point is always present
* String:
* non-printable characters are handled as \number
*
* Spacing
*
* + Sign
*
* - Alignment
*
* . Precision
*
* * Parameter
* print: use parameter
* scan: no parameter (ignore)
*
* q Quad
*
* Z size_t
*
* w Widechar
*
* ' Thousands/quote
* Numbers:
* Integer part grouped in thousands
* Binary numbers:
* Number grouped in nibbles (4 bits)
* String:
* Quoted string
*
* j intmax_t
* t prtdiff_t
* z size_t
*
* ! Sticky
* @ Parameter (for both print and scan)
*
* I n-bit Integer
* Numbers:
* The following options exists
* I8 = 8-bit integer
* I16 = 16-bit integer
* I32 = 32-bit integer
* I64 = 64-bit integer
*/
#define QUALIFIER_POSITION '$'
#define QUALIFIER_SHORT 'h'
#define QUALIFIER_LONG 'l'
#define QUALIFIER_LONG_UPPER 'L'
#define QUALIFIER_ALTERNATIVE '#'
#define QUALIFIER_SPACE ' '
#define QUALIFIER_PLUS '+'
#define QUALIFIER_MINUS '-'
#define QUALIFIER_DOT '.'
#define QUALIFIER_STAR '*'
#define QUALIFIER_CIRCUMFLEX '^' /* For scanlists */
#if TRIO_C99
# define QUALIFIER_SIZE_T 'z'
# define QUALIFIER_PTRDIFF_T 't'
# define QUALIFIER_INTMAX_T 'j'
#endif
#if TRIO_BSD || TRIO_GNU
# define QUALIFIER_QUAD 'q'
#endif
#if TRIO_GNU
# define QUALIFIER_SIZE_T_UPPER 'Z'
#endif
#if TRIO_MISC
# define QUALIFIER_WIDECHAR 'w'
#endif
#if TRIO_MICROSOFT
# define QUALIFIER_FIXED_SIZE 'I'
#endif
#if TRIO_EXTENSION
# define QUALIFIER_QUOTE '\''
# define QUALIFIER_STICKY '!'
# define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
# define QUALIFIER_PARAM '@' /* Experimental */
# define QUALIFIER_COLON ':' /* For scanlists */
# define QUALIFIER_EQUAL '=' /* For scanlists */
# define QUALIFIER_ROUNDING_UPPER 'R'
#endif
/*************************************************************************
*
* Internal Structures
*
*************************************************************************/
/* Parameters */
typedef struct {
/* An indication of which entry in the data union is used */
int type;
/* The flags */
trio_flags_t flags;
/* The width qualifier */
int width;
/* The precision qualifier */
int precision;
/* The base qualifier */
int base;
/* The size for the variable size qualifier */
int varsize;
/* The marker of the end of the specifier */
int indexAfterSpecifier;
/* The data from the argument list */
union {
char *string;
#if TRIO_WIDECHAR
trio_wchar_t *wstring;
#endif
trio_pointer_t pointer;
union {
trio_intmax_t as_signed;
trio_uintmax_t as_unsigned;
} number;
double doubleNumber;
double *doublePointer;
trio_long_double_t longdoubleNumber;
trio_long_double_t *longdoublePointer;
int errorNumber;
} data;
/* For the user-defined specifier */
char user_name[MAX_USER_NAME];
char user_data[MAX_USER_DATA];
} trio_parameter_t;
/* Container for customized functions */
typedef struct {
union {
trio_outstream_t out;
trio_instream_t in;
} stream;
trio_pointer_t closure;
} trio_custom_t;
/* General trio "class" */
typedef struct _trio_class_t {
/*
* The function to write characters to a stream.
*/
void (*OutStream) TRIO_PROTO((struct _trio_class_t *, int));
/*
* The function to read characters from a stream.
*/
void (*InStream) TRIO_PROTO((struct _trio_class_t *, int *));
/*
* The current location in the stream.
*/
trio_pointer_t location;
/*
* The character currently being processed.
*/
int current;
/*
* The number of characters that would have been written/read
* if there had been sufficient space.
*/
int processed;
/*
* The number of characters that are actually written/read.
* Processed and committed will only differ for the *nprintf
* and *nscanf functions.
*/
int committed;
/*
* The upper limit of characters that may be written/read.
*/
int max;
/*
* The last output error that was detected.
*/
int error;
} trio_class_t;
/* References (for user-defined callbacks) */
typedef struct _trio_reference_t {
trio_class_t *data;
trio_parameter_t *parameter;
} trio_reference_t;
/* Registered entries (for user-defined callbacks) */
typedef struct _trio_userdef_t {
struct _trio_userdef_t *next;
trio_callback_t callback;
char *name;
} trio_userdef_t;
/*************************************************************************
*
* Internal Variables
*
*************************************************************************/
static TRIO_CONST char rcsid[] = "@(#)$Id$";
/*
* Need this to workaround a parser bug in HP C/iX compiler that fails
* to resolves macro definitions that includes type 'long double',
* e.g: va_arg(arg_ptr, long double)
*/
#if defined(TRIO_PLATFORM_MPEIX)
static TRIO_CONST trio_long_double_t ___dummy_long_double = 0;
#endif
static TRIO_CONST char internalNullString[] = "(nil)";
#if defined(USE_LOCALE)
static struct lconv *internalLocaleValues = NULL;
#endif
/*
* UNIX98 says "in a locale where the radix character is not defined,
* the radix character defaults to a period (.)"
*/
static int internalDecimalPointLength = 1;
static int internalThousandSeparatorLength = 1;
static char internalDecimalPoint = '.';
static char internalDecimalPointString[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ".";
static char internalThousandSeparator[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ",";
static char internalGrouping[MAX_LOCALE_GROUPS] = { (char)NO_GROUPING };
static TRIO_CONST char internalDigitsLower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static TRIO_CONST char internalDigitsUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static BOOLEAN_T internalDigitsUnconverted = TRUE;
static int internalDigitArray[128];
#if TRIO_EXTENSION
static BOOLEAN_T internalCollationUnconverted = TRUE;
static char internalCollationArray[MAX_CHARACTER_CLASS][MAX_CHARACTER_CLASS];
#endif
#if TRIO_EXTENSION
static TRIO_VOLATILE trio_callback_t internalEnterCriticalRegion = NULL;
static TRIO_VOLATILE trio_callback_t internalLeaveCriticalRegion = NULL;
static trio_userdef_t *internalUserDef = NULL;
#endif
/*************************************************************************
*
* Internal Functions
*
************************************************************************/
#if defined(TRIO_MINIMAL)
# define TRIO_STRING_PUBLIC static
# include "triostr.c"
#endif /* defined(TRIO_MINIMAL) */
/*************************************************************************
* TrioIsQualifier
*
* Description:
* Remember to add all new qualifiers to this function.
* QUALIFIER_POSITION must not be added.
*/
TRIO_PRIVATE BOOLEAN_T
TrioIsQualifier
TRIO_ARGS1((character),
TRIO_CONST char character)
{
/* QUALIFIER_POSITION is not included */
switch (character)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case QUALIFIER_PLUS:
case QUALIFIER_MINUS:
case QUALIFIER_SPACE:
case QUALIFIER_DOT:
case QUALIFIER_STAR:
case QUALIFIER_ALTERNATIVE:
case QUALIFIER_SHORT:
case QUALIFIER_LONG:
case QUALIFIER_LONG_UPPER:
case QUALIFIER_CIRCUMFLEX:
#if defined(QUALIFIER_SIZE_T)
case QUALIFIER_SIZE_T:
#endif
#if defined(QUALIFIER_PTRDIFF_T)
case QUALIFIER_PTRDIFF_T:
#endif
#if defined(QUALIFIER_INTMAX_T)
case QUALIFIER_INTMAX_T:
#endif
#if defined(QUALIFIER_QUAD)
case QUALIFIER_QUAD:
#endif
#if defined(QUALIFIER_SIZE_T_UPPER)
case QUALIFIER_SIZE_T_UPPER:
#endif
#if defined(QUALIFIER_WIDECHAR)
case QUALIFIER_WIDECHAR:
#endif
#if defined(QUALIFIER_QUOTE)
case QUALIFIER_QUOTE:
#endif
#if defined(QUALIFIER_STICKY)
case QUALIFIER_STICKY:
#endif
#if defined(QUALIFIER_VARSIZE)
case QUALIFIER_VARSIZE:
#endif
#if defined(QUALIFIER_PARAM)
case QUALIFIER_PARAM:
#endif
#if defined(QUALIFIER_FIXED_SIZE)
case QUALIFIER_FIXED_SIZE:
#endif
#if defined(QUALIFIER_ROUNDING_UPPER)
case QUALIFIER_ROUNDING_UPPER:
#endif
return TRUE;
default:
return FALSE;
}
}
/*************************************************************************
* TrioSetLocale
*/
#if defined(USE_LOCALE)
TRIO_PRIVATE void
TrioSetLocale(TRIO_NOARGS)
{
internalLocaleValues = (struct lconv *)localeconv();
if (internalLocaleValues)
{
if ((internalLocaleValues->decimal_point) &&
(internalLocaleValues->decimal_point[0] != NIL))
{
internalDecimalPointLength = trio_length(internalLocaleValues->decimal_point);
if (internalDecimalPointLength == 1)
{
internalDecimalPoint = internalLocaleValues->decimal_point[0];
}
else
{
internalDecimalPoint = NIL;
trio_copy_max(internalDecimalPointString,
sizeof(internalDecimalPointString),
internalLocaleValues->decimal_point);
}
}
if ((internalLocaleValues->thousands_sep) &&
(internalLocaleValues->thousands_sep[0] != NIL))
{
trio_copy_max(internalThousandSeparator,
sizeof(internalThousandSeparator),
internalLocaleValues->thousands_sep);
internalThousandSeparatorLength = trio_length(internalThousandSeparator);
}
if ((internalLocaleValues->grouping) &&
(internalLocaleValues->grouping[0] != NIL))
{
trio_copy_max(internalGrouping,
sizeof(internalGrouping),
internalLocaleValues->grouping);
}
}
}
#endif /* defined(USE_LOCALE) */
TRIO_PRIVATE int
TrioCalcThousandSeparatorLength
TRIO_ARGS1((digits),
int digits)
{
#if TRIO_EXTENSION
int count = 0;
int step = NO_GROUPING;
char *groupingPointer = internalGrouping;
while (digits > 0)
{
if (*groupingPointer == CHAR_MAX)
{
/* Disable grouping */
break; /* while */
}
else if (*groupingPointer == 0)
{
/* Repeat last group */
if (step == NO_GROUPING)
{
/* Error in locale */
break; /* while */
}
}
else
{
step = *groupingPointer++;
}
if (digits > step)
count += internalThousandSeparatorLength;
digits -= step;
}
return count;
#else
return 0;
#endif
}
TRIO_PRIVATE BOOLEAN_T
TrioFollowedBySeparator
TRIO_ARGS1((position),
int position)
{
#if TRIO_EXTENSION
int step = 0;
char *groupingPointer = internalGrouping;
position--;
if (position == 0)
return FALSE;
while (position > 0)
{
if (*groupingPointer == CHAR_MAX)
{
/* Disable grouping */
break; /* while */
}
else if (*groupingPointer != 0)
{
step = *groupingPointer++;
}
if (step == 0)
break;
position -= step;
}
return (position == 0);
#else
return FALSE;
#endif
}
/*************************************************************************
* TrioGetPosition
*