-
Notifications
You must be signed in to change notification settings - Fork 1
/
trio.c
7892 lines (7166 loc) · 180 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, 2009 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
* MERCHANTABILITY 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"
#if defined(TRIO_EMBED_NAN)
# define TRIO_PUBLIC_NAN static
# if TRIO_FEATURE_FLOAT
# define TRIO_FUNC_NAN
# define TRIO_FUNC_NINF
# define TRIO_FUNC_PINF
# define TRIO_FUNC_FPCLASSIFY_AND_SIGNBIT
# define TRIO_FUNC_ISINF
# endif
#endif
#include "trionan.h"
#if defined(TRIO_EMBED_STRING)
# define TRIO_PUBLIC_STRING static
# define TRIO_FUNC_LENGTH
# define TRIO_FUNC_LENGTH_MAX
# define TRIO_FUNC_TO_LONG
# if TRIO_FEATURE_LOCALE
# define TRIO_FUNC_COPY_MAX
# endif
# if TRIO_FEATURE_DYNAMICSTRING
# define TRIO_FUNC_XSTRING_DUPLICATE
# endif
# if TRIO_EXTENSION && TRIO_FEATURE_SCANF
# define TRIO_FUNC_EQUAL_LOCALE
# endif
# if TRIO_FEATURE_ERRNO
# define TRIO_FUNC_ERROR
# endif
# if TRIO_FEATURE_FLOAT && TRIO_FEATURE_SCANF
# define TRIO_FUNC_TO_DOUBLE
# endif
# if TRIO_FEATURE_DYNAMICSTRING
# define TRIO_FUNC_STRING_EXTRACT
# endif
# if TRIO_FEATURE_DYNAMICSTRING
# define TRIO_FUNC_STRING_TERMINATE
# endif
# if TRIO_FEATURE_USER_DEFINED
# define TRIO_FUNC_DUPLICATE
# endif
# if TRIO_FEATURE_DYNAMICSTRING
# define TRIO_FUNC_STRING_DESTROY
# endif
# if TRIO_FEATURE_USER_DEFINED
# define TRIO_FUNC_DESTROY
# endif
# if TRIO_FEATURE_USER_DEFINED || (TRIO_FEATURE_FLOAT && TRIO_FEATURE_SCANF)
# define TRIO_FUNC_EQUAL
# endif
# if TRIO_FEATURE_USER_DEFINED || TRIO_FEATURE_SCANF
# define TRIO_FUNC_EQUAL_CASE
# endif
# if (TRIO_EXTENSION && TRIO_FEATURE_SCANF)
# define TRIO_FUNC_EQUAL_MAX
# endif
# if TRIO_FEATURE_SCANF
# define TRIO_FUNC_TO_UPPER
# endif
# if TRIO_FEATURE_DYNAMICSTRING
# define TRIO_FUNC_XSTRING_APPEND_CHAR
# endif
#endif
#include "triostr.h"
/**************************************************************************
*
* Definitions
*
*************************************************************************/
#include <limits.h>
#if TRIO_FEATURE_FLOAT
# include <math.h>
# include <float.h>
#endif
#if defined(__STDC_ISO_10646__) || defined(MB_LEN_MAX) || defined(USE_MULTIBYTE) || TRIO_FEATURE_WIDECHAR
# if !defined(TRIO_PLATFORM_WINCE)
# define TRIO_COMPILER_SUPPORTS_MULTIBYTE
# if !defined(MB_LEN_MAX)
# define MB_LEN_MAX 6
# endif
# endif
#endif
#if (defined(TRIO_COMPILER_VISUALC) && (TRIO_COMPILER_VISUALC >= 1100)) || defined(TRIO_COMPILER_BORLAND)
# define TRIO_COMPILER_SUPPORTS_VISUALC_INT
#endif
#if TRIO_FEATURE_FLOAT
# if defined(PREDEF_STANDARD_C99) \
|| defined(PREDEF_STANDARD_UNIX03)
# if !defined(HAVE_FLOORL) && !defined(TRIO_NO_FLOORL)
# define HAVE_FLOORL
# endif
# if !defined(HAVE_CEILL) && !defined(TRIO_NO_CEILL)
# define HAVE_CEILL
# endif
# if !defined(HAVE_POWL) && !defined(TRIO_NO_POWL)
# define HAVE_POWL
# endif
# if !defined(HAVE_FMODL) && !defined(TRIO_NO_FMODL)
# define HAVE_FMODL
# endif
# if !defined(HAVE_LOG10L) && !defined(TRIO_NO_LOG10L)
# define HAVE_LOG10L
# endif
# endif
# if defined(TRIO_COMPILER_VISUALC)
# if defined(floorl)
# define HAVE_FLOORL
# endif
# if defined(ceill)
# define HAVE_CEILL
# endif
# if defined(powl)
# define HAVE_POWL
# endif
# if defined(fmodl)
# define HAVE_FMODL
# endif
# if defined(log10l)
# define HAVE_LOG10L
# endif
# endif
#endif
/*************************************************************************
* Generic definitions
*/
#if !(defined(DEBUG) || defined(NDEBUG))
# define NDEBUG
#endif
#include <assert.h>
#include <ctype.h>
#if !defined(isascii) && (defined(__STRICT_ANSI__) || defined(PREDEF_STANDARD_C99))
# define isascii(x) ((x) & 0x7F)
#endif
#if defined(TRIO_COMPILER_ANCIENT)
# include <varargs.h>
#else
# include <stdarg.h>
#endif
#include <stddef.h>
#if defined(TRIO_PLATFORM_WINCE)
extern int errno;
#else
# 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_FEATURE_ERRORCODE
/*
* Encode the error code and the position. This is decoded
* with TRIO_ERROR_CODE and TRIO_ERROR_POSITION.
*/
# define TRIO_ERROR_RETURN(x,y) (- ((x) + ((y) << 8)))
#else
# define TRIO_ERROR_RETURN(x,y) (-1)
#endif
typedef unsigned long trio_flags_t;
/*************************************************************************
* Platform specific definitions
*/
#if defined(TRIO_PLATFORM_UNIX)
# include <unistd.h>
# include <signal.h>
# include <locale.h>
# if !defined(TRIO_FEATURE_LOCALE)
# define USE_LOCALE
# endif
#endif /* TRIO_PLATFORM_UNIX */
#if defined(TRIO_PLATFORM_VMS)
# include <unistd.h>
#endif
#if defined(TRIO_PLATFORM_WIN32)
# if defined(TRIO_PLATFORM_WINCE)
int read(int handle, char *buffer, unsigned int length);
int write(int handle, const char *buffer, unsigned int length);
# else
# include <io.h>
# define read _read
# define write _write
# endif
#endif /* TRIO_PLATFORM_WIN32 */
#if TRIO_FEATURE_WIDECHAR
# if defined(PREDEF_STANDARD_C94)
# 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 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
# else
# if defined(TRIO_COMPILER_SUNPRO)
# define USE_LONGLONG
# else
# if defined(TRIO_COMPILER_MSVC) && (_MSC_VER >= 1400)
# define USE_LONGLONG
# else
# if defined(_LONG_LONG) || defined(_LONGLONG)
# define USE_LONGLONG
# endif
# endif
# endif
# 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;
#else
# if defined(TRIO_COMPILER_SUPPORTS_VISUALC_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
#endif
/* Maximal and fixed integer types */
#if defined(PREDEF_STANDARD_C99)
# include <stdint.h>
typedef intmax_t trio_intmax_t;
typedef uintmax_t trio_uintmax_t;
typedef int8_t trio_int8_t;
typedef int16_t trio_int16_t;
typedef int32_t trio_int32_t;
typedef int64_t trio_int64_t;
#else
# if defined(PREDEF_STANDARD_UNIX98)
# include <inttypes.h>
typedef intmax_t trio_intmax_t;
typedef uintmax_t trio_uintmax_t;
typedef int8_t trio_int8_t;
typedef int16_t trio_int16_t;
typedef int32_t trio_int32_t;
typedef int64_t trio_int64_t;
# else
# if defined(TRIO_COMPILER_SUPPORTS_VISUALC_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
# endif
#endif
#if defined(HAVE_FLOORL)
# define trio_floor(x) floorl((x))
#else
# define trio_floor(x) floor((double)(x))
#endif
#if defined(HAVE_CEILL)
# define trio_ceil(x) ceill((x))
#else
# define trio_ceil(x) ceil((double)(x))
#endif
#if defined(HAVE_FMODL)
# define trio_fmod(x,y) fmodl((x),(y))
#else
# define trio_fmod(x,y) fmod((double)(x),(double)(y))
#endif
#if defined(HAVE_POWL)
# define trio_pow(x,y) powl((x),(y))
#else
# define trio_pow(x,y) pow((double)(x),(double)(y))
#endif
#if defined(HAVE_LOG10L)
# define trio_log10(x) log10l((x))
#else
# define trio_log10(x) log10((double)(x))
#endif
#if TRIO_FEATURE_FLOAT
# define TRIO_FABS(x) (((x) < 0.0) ? -(x) : (x))
#endif
/*************************************************************************
* Internal Definitions
*/
#if TRIO_FEATURE_FLOAT
# if !defined(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
#endif /* TRIO_FEATURE_FLOAT */
/* 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)
#if TRIO_FEATURE_FLOAT
/* 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"
#endif
/* Various constants */
enum {
TYPE_PRINT = 1,
#if TRIO_FEATURE_SCANF
TYPE_SCAN = 2,
#endif
/* 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_IGNORE = 2 * FLAGS_WIDECHAR,
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_USER_DEFINED_PARAMETER = FLAGS_IGNORE_PARAMETER,
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,
#if TRIO_FEATURE_USER_DEFINED
/* Maximal string lengths for user-defined specifiers */
MAX_USER_NAME = 64,
MAX_USER_DATA = 256,
#endif
/* Maximal length of locale separator strings */
MAX_LOCALE_SEPARATOR_LENGTH = MB_LEN_MAX,
/* Maximal number of integers in grouping */
MAX_LOCALE_GROUPS = 64
};
#define NO_GROUPING ((int)CHAR_MAX)
/* Fundamental formatting parameter types */
#define FORMAT_SENTINEL -1 /* marks end of parameters array */
#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
#define FORMAT_ERRNO 9
#define FORMAT_USER_DEFINED 10
/* Character constants */
#define CHAR_IDENTIFIER '%'
#define CHAR_ALT_IDENTIFIER '$'
#define CHAR_BACKSLASH '\\'
#define CHAR_QUOTE '\"'
#define CHAR_ADJUST ' '
#if TRIO_EXTENSION
/* 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:]"
#endif
/*
* 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'
#if TRIO_FEATURE_FLOAT
# 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'
#endif
#define SPECIFIER_POINTER 'p'
#if TRIO_FEATURE_SCANF
# define SPECIFIER_GROUP '['
# define SPECIFIER_UNGROUP ']'
#endif
#define SPECIFIER_COUNT 'n'
#if TRIO_UNIX98
# define SPECIFIER_CHAR_UPPER 'C'
# define SPECIFIER_STRING_UPPER 'S'
#endif
#define SPECIFIER_HEXFLOAT 'a'
#define SPECIFIER_HEXFLOAT_UPPER 'A'
#define SPECIFIER_ERRNO 'm'
#if TRIO_FEATURE_BINARY
# define SPECIFIER_BINARY 'b'
# define SPECIFIER_BINARY_UPPER 'B'
#endif
#if TRIO_FEATURE_USER_DEFINED
# define SPECIFIER_USER_DEFINED_BEGIN '<'
# define SPECIFIER_USER_DEFINED_END '>'
# define SPECIFIER_USER_DEFINED_SEPARATOR ':'
# define SPECIFIER_USER_DEFINED_EXTRA '|'
#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 */
#define QUALIFIER_SIZE_T 'z'
#define QUALIFIER_PTRDIFF_T 't'
#define QUALIFIER_INTMAX_T 'j'
#define QUALIFIER_QUAD 'q'
#define QUALIFIER_SIZE_T_UPPER 'Z'
#if TRIO_MISC
# define QUALIFIER_WIDECHAR 'w'
#endif
#define QUALIFIER_FIXED_SIZE 'I'
#define QUALIFIER_QUOTE '\''
#define QUALIFIER_STICKY '!'
#define QUALIFIER_VARSIZE '&' /* This should remain undocumented */
#define QUALIFIER_ROUNDING_UPPER 'R'
#if TRIO_EXTENSION
# define QUALIFIER_PARAM '@' /* Experimental */
# define QUALIFIER_COLON ':' /* For scanlists */
# define QUALIFIER_EQUAL '=' /* For scanlists */
#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;
/* Base from specifier */
int baseSpecifier;
/* The size for the variable size qualifier */
int varsize;
/* Offset of the first character of the specifier */
int beginOffset;
/* Offset of the first character after the specifier */
int endOffset;
/* Position in the argument list that this parameter refers to */
int position;
/* The data from the argument list */
union {
char *string;
#if TRIO_FEATURE_WIDECHAR
trio_wchar_t *wstring;
#endif
trio_pointer_t pointer;
union {
trio_intmax_t as_signed;
trio_uintmax_t as_unsigned;
} number;
#if TRIO_FEATURE_FLOAT
double doubleNumber;
double *doublePointer;
trio_long_double_t longdoubleNumber;
trio_long_double_t *longdoublePointer;
#endif
int errorNumber;
} data;
#if TRIO_FEATURE_USER_DEFINED
/* For the user-defined specifier */
union {
char trio_namespace[MAX_USER_NAME];
int handler; /* if flags & FLAGS_USER_DEFINED_PARAMETER */
} user_defined;
char user_data[MAX_USER_DATA];
#endif
} 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 function to undo read characters from a stream.
*/
void (*UndoStream) TRIO_PROTO((struct _trio_class_t *));
/*
* 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;
union {
/*
* The number of characters that are actually written. Processed and
* committed will only differ for the *nprintf functions.
*/
int committed;
/*
* The number of look-ahead characters read.
*/
int cached;
} actually;
/*
* 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;
#if TRIO_FEATURE_USER_DEFINED
/* 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;
#endif
/*************************************************************************
*
* Internal Variables
*
*************************************************************************/
#if TRIO_FEATURE_FLOAT
/*
* 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
#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 (.)"
*/
#if TRIO_FEATURE_FLOAT || TRIO_FEATURE_LOCALE || defined(USE_LOCALE)
static int internalDecimalPointLength = 1;
static char internalDecimalPoint = '.';
static char internalDecimalPointString[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ".";
#endif
#if TRIO_FEATURE_QUOTE || TRIO_FEATURE_LOCALE || TRIO_EXTENSION
static int internalThousandSeparatorLength = 1;
static char internalThousandSeparator[MAX_LOCALE_SEPARATOR_LENGTH + 1] = ",";
static char internalGrouping[MAX_LOCALE_GROUPS] = { (char)NO_GROUPING };
#endif
static TRIO_CONST char internalDigitsLower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static TRIO_CONST char internalDigitsUpper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
#if TRIO_FEATURE_SCANF
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
#endif
#if TRIO_FEATURE_USER_DEFINED
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_EMBED_NAN)
# include "trionan.c"
#endif
#if defined(TRIO_EMBED_STRING)
# include "triostr.c"
#endif
/*************************************************************************
* TrioInitializeParameter
*
* Description:
* Initialize a trio_parameter_t struct.
*/
static void
TrioInitializeParameter
TRIO_ARGS1((parameter),
trio_parameter_t *parameter)
{
parameter->type = FORMAT_UNKNOWN;
parameter->flags = 0;
parameter->width = 0;
parameter->precision = 0;
parameter->base = 0;
parameter->baseSpecifier = 0;
parameter->varsize = 0;
parameter->beginOffset = 0;
parameter->endOffset = 0;
parameter->position = 0;
parameter->data.pointer = 0;
#if TRIO_FEATURE_USER_DEFINED
parameter->user_defined.handler = 0;
parameter->user_data[0] = 0;
#endif
}
/*************************************************************************
* TrioCopyParameter
*
* Description:
* Copies one trio_parameter_t struct to another.
*/
static void
TrioCopyParameter
TRIO_ARGS2((target, source),
trio_parameter_t *target,
TRIO_CONST trio_parameter_t *source)
{
#if TRIO_FEATURE_USER_DEFINED