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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
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-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/************************************************************************
The printf-like implementation in this file is based on the one found
in the sqlite3 distribution is in the Public Domain.
This copy was forked for use with the clob API in Feb 2008 by Stephan
Beal (http://wanderinghorse.net/home/stephan/) and modified to send
its output to arbitrary targets via a callback mechanism. Also
refactored the %X specifier handlers a bit to make adding/removing
specific handlers easier.
All code in this file is released into the Public Domain.
The printf implementation (fsl_appendfv()) is pretty easy to extend
(e.g. adding or removing %-specifiers for fsl_appendfv()) if you're
willing to poke around a bit and see how the specifiers are declared
and dispatched. For an example, grep for 'etSTRING' and follow it
through the process of declaration to implementation.
See below for several FSLPRINTF_OMIT_xxx macros which can be set to
remove certain features/extensions.
************************************************************************/
#include <stdio.h> /* FILE */
#include <string.h> /* strlen() */
#include <stdlib.h> /* free/malloc() */
#include <ctype.h>
#include <stdint.h>
#include <assert.h>
#include "fossil/fossil2.h"
/* FIXME: determine this type at compile time */
typedef long double LONGDOUBLE_TYPE;
/*
If FSLPRINTF_OMIT_FLOATING_POINT is defined to a true value, then
floating point conversions are disabled.
*/
#ifndef FSLPRINTF_OMIT_FLOATING_POINT
# define FSLPRINTF_OMIT_FLOATING_POINT 0
#endif
/*
If FSLPRINTF_OMIT_SIZE is defined to a true value, then
the %n specifier is disabled.
*/
#ifndef FSLPRINTF_OMIT_SIZE
# define FSLPRINTF_OMIT_SIZE 0
#endif
/*
If FSLPRINTF_OMIT_SQL is defined to a true value, then
the %q and %Q specifiers are disabled.
*/
#ifndef FSLPRINTF_OMIT_SQL
# define FSLPRINTF_OMIT_SQL 0
#endif
/*
If FSLPRINTF_OMIT_HTML is defined to a true value then the %h (HTML
escape), %t (URL escape), and %T (URL unescape) specifiers are
disabled.
*/
#ifndef FSLPRINTF_OMIT_HTML
# define FSLPRINTF_OMIT_HTML 0
#endif
/*
Most C compilers handle variable-sized arrays, so we enable
that by default. Some (e.g. tcc) do not, so we provide a way
to disable it: set FSLPRINTF_HAVE_VARARRAY to 0
One approach would be to look at:
defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
but some compilers support variable-sized arrays even when not
explicitly running in c99 mode.
*/
#if !defined(FSLPRINTF_HAVE_VARARRAY)
# if defined(__TINYC__)
# define FSLPRINTF_HAVE_VARARRAY 0
# else
# if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
# define FSLPRINTF_HAVE_VARARRAY 1 /*use 1 in C99 mode */
# else
# define FSLPRINTF_HAVE_VARARRAY 0
# endif
# endif
#endif
/**
FSLPRINTF_CHARARRAY is a helper to allocate variable-sized arrays.
This exists mainly so this code can compile with the tcc compiler.
*/
#if FSLPRINTF_HAVE_VARARRAY
# define FSLPRINTF_CHARARRAY(V,N) char V[N+1]; memset(V,0,N+1);
# define FSLPRINTF_CHARARRAY_FREE(V)
#else
# define FSLPRINTF_CHARARRAY(V,N) char * V = (char *)malloc(N+1); memset(V,0,N+1);
# define FSLPRINTF_CHARARRAY_FREE(V) free(V)
#endif
/*
Conversion types fall into various categories as defined by the
following enumeration.
*/
enum PrintfCategory {etRADIX = 1, /* Integer types. %d, %x, %o, and so forth */
etFLOAT = 2, /* Floating point. %f */
etEXP = 3, /* Exponentional notation. %e and %E */
etGENERIC = 4, /* Floating or exponential, depending on exponent. %g */
etSIZE = 5, /* Return number of characters processed so far. %n */
etSTRING = 6, /* Strings. %s */
etDYNSTRING = 7, /* Dynamically allocated strings. %z */
etPERCENT = 8, /* Percent symbol. %% */
etCHARX = 9, /* Characters. %c */
/* The rest are extensions, not normally found in printf() */
etCHARLIT = 10, /* Literal characters. %' */
#if !FSLPRINTF_OMIT_SQL
etSQLESCAPE = 11, /* Strings with '\'' doubled. %q */
etSQLESCAPE2 = 12, /* Strings with '\'' doubled and enclosed in '',
NULL pointers replaced by SQL NULL. %Q */
etSQLESCAPE3 = 16, /* %w -> Strings with '\"' doubled */
#endif /* !FSLPRINTF_OMIT_SQL */
etPOINTER = 15, /* The %p conversion */
etORDINAL = 17, /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
#if ! FSLPRINTF_OMIT_HTML
etHTML = 18, /* %h -> basic HTML escaping. */
etURLENCODE = 19, /* %t -> URL encoding. */
etURLDECODE = 20, /* %T -> URL decoding. */
#endif
etPLACEHOLDER = 100
};
/*
An "etByte" is an 8-bit unsigned value.
*/
typedef unsigned char etByte;
/*
Each builtin conversion character (ex: the 'd' in "%d") is described
by an instance of the following structure
*/
typedef struct et_info { /* Information about each format field */
char fmttype; /* The format field code letter */
etByte base; /* The base for radix conversion */
etByte flags; /* One or more of FLAG_ constants below */
etByte type; /* Conversion paradigm */
etByte charset; /* Offset into aDigits[] of the digits string */
etByte prefix; /* Offset into aPrefix[] of the prefix string */
} et_info;
/*
Allowed values for et_info.flags
*/
enum et_info_flags { FLAG_SIGNED = 1, /* True if the value to convert is signed */
FLAG_EXTENDED = 2, /* True if for internal/extended use only. */
FLAG_STRING = 4 /* Allow infinity precision */
};
/*
Historically, the following table was searched linearly, so the most
common conversions were kept at the front.
Change 2008 Oct 31 by Stephan Beal: we reserve an array or ordered
entries for all chars in the range [32..126]. Format character
checks can now be done in constant time by addressing that array
directly. This takes more static memory, but reduces the time and
per-call overhead costs of fsl_appendfv().
*/
static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
static const char aPrefix[] = "-x0\000X0";
static const et_info fmtinfo[] = {
/**
If FSLPRINTF_FMTINFO_FIXED is 1 then we use the original
implementation: a linear list of entries. Search time is linear. If
FSLPRINTF_FMTINFO_FIXED is 0 then we use a fixed-size array which
we index directly using the format char as the key.
*/
#define FSLPRINTF_FMTINFO_FIXED 0
#if FSLPRINTF_FMTINFO_FIXED
{ 'd', 10, FLAG_SIGNED, etRADIX, 0, 0 },
{ 's', 0, FLAG_STRING, etSTRING, 0, 0 },
{ 'g', 0, FLAG_SIGNED, etGENERIC, 30, 0 },
{ 'z', 0, FLAG_STRING, etDYNSTRING, 0, 0 },
{ 'c', 0, 0, etCHARX, 0, 0 },
{ 'o', 8, 0, etRADIX, 0, 2 },
{ 'u', 10, 0, etRADIX, 0, 0 },
{ 'x', 16, 0, etRADIX, 16, 1 },
{ 'X', 16, 0, etRADIX, 0, 4 },
{ 'i', 10, FLAG_SIGNED, etRADIX, 0, 0 },
#if !FSLPRINTF_OMIT_FLOATING_POINT
{ 'f', 0, FLAG_SIGNED, etFLOAT, 0, 0 },
{ 'e', 0, FLAG_SIGNED, etEXP, 30, 0 },
{ 'E', 0, FLAG_SIGNED, etEXP, 14, 0 },
{ 'G', 0, FLAG_SIGNED, etGENERIC, 14, 0 },
#endif /* !FSLPRINTF_OMIT_FLOATING_POINT */
{ '%', 0, 0, etPERCENT, 0, 0 },
{ 'p', 16, 0, etPOINTER, 0, 1 },
{ 'r', 10, (FLAG_EXTENDED|FLAG_SIGNED), etORDINAL, 0, 0 },
#if ! FSLPRINTF_OMIT_SQL
{ 'q', 0, FLAG_STRING, etSQLESCAPE, 0, 0 },
{ 'Q', 0, FLAG_STRING, etSQLESCAPE2, 0, 0 },
{ 'w', 0, FLAG_STRING, etSQLESCAPE3, 0, 0 },
#endif /* !FSLPRINTF_OMIT_SQL */
#if ! FSLPRINTF_OMIT_HTML
{ 'h', 0, FLAG_STRING, etHTML, 0, 0 },
{ 't', 0, FLAG_STRING, etURLENCODE, 0, 0 },
{ 'T', 0, FLAG_STRING, etURLDECODE, 0, 0 },
#endif /* !FSLPRINTF_OMIT_HTML */
#if !FSLPRINTF_OMIT_SIZE
{ 'n', 0, 0, etSIZE, 0, 0 },
#endif
#else /* FSLPRINTF_FMTINFO_FIXED */
/*
These entries MUST stay in ASCII order, sorted
on their fmttype member!
*/
{' '/*32*/, 0, 0, 0, 0, 0 },
{'!'/*33*/, 0, 0, 0, 0, 0 },
{'"'/*34*/, 0, 0, 0, 0, 0 },
{'#'/*35*/, 0, 0, 0, 0, 0 },
{'$'/*36*/, 0, 0, 0, 0, 0 },
{'%'/*37*/, 0, 0, etPERCENT, 0, 0 },
{'&'/*38*/, 0, 0, 0, 0, 0 },
{'\''/*39*/, 0, 0, 0, 0, 0 },
{'('/*40*/, 0, 0, 0, 0, 0 },
{')'/*41*/, 0, 0, 0, 0, 0 },
{'*'/*42*/, 0, 0, 0, 0, 0 },
{'+'/*43*/, 0, 0, 0, 0, 0 },
{','/*44*/, 0, 0, 0, 0, 0 },
{'-'/*45*/, 0, 0, 0, 0, 0 },
{'.'/*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LAG_STRING, etSQLESCAPE2, 0, 0 },
#endif
{'R'/*82*/, 0, 0, 0, 0, 0 },
{'S'/*83*/, 0, 0, 0, 0, 0 },
{'T'/*84*/, 0, FLAG_STRING, etURLDECODE, 0, 0 },
{'U'/*85*/, 0, 0, 0, 0, 0 },
{'V'/*86*/, 0, 0, 0, 0, 0 },
{'W'/*87*/, 0, 0, 0, 0, 0 },
{'X'/*88*/, 16, 0, etRADIX, 0, 4 },
{'Y'/*89*/, 0, 0, 0, 0, 0 },
{'Z'/*90*/, 0, 0, 0, 0, 0 },
{'['/*91*/, 0, 0, 0, 0, 0 },
{'\\'/*92*/, 0, 0, 0, 0, 0 },
{']'/*93*/, 0, 0, 0, 0, 0 },
{'^'/*94*/, 0, 0, 0, 0, 0 },
{'_'/*95*/, 0, 0, 0, 0, 0 },
{'`'/*96*/, 0, 0, 0, 0, 0 },
{'a'/*97*/, 0, 0, 0, 0, 0 },
{'b'/*98*/, 0, 0, 0, 0, 0 },
{'c'/*99*/, 0, 0, etCHARX, 0, 0 },
{'d'/*100*/, 10, FLAG_SIGNED, etRADIX, 0, 0 },
{'e'/*101*/, 0, FLAG_SIGNED, etEXP, 30, 0 },
{'f'/*102*/, 0, FLAG_SIGNED, etFLOAT, 0, 0},
{'g'/*103*/, 0, FLAG_SIGNED, etGENERIC, 30, 0 },
{'h'/*104*/, 0, FLAG_STRING, etHTML, 0, 0 },
{'i'/*105*/, 10, FLAG_SIGNED, etRADIX, 0, 0},
{'j'/*106*/, 0, 0, 0, 0, 0 },
{'k'/*107*/, 0, 0, 0, 0, 0 },
{'l'/*108*/, 0, 0, 0, 0, 0 },
{'m'/*109*/, 0, 0, 0, 0, 0 },
{'n'/*110*/, 0, 0, etSIZE, 0, 0 },
{'o'/*111*/, 8, 0, etRADIX, 0, 2 },
{'p'/*112*/, 16, 0, etPOINTER, 0, 1 },
#if FSLPRINTF_OMIT_SQL
{'q'/*113*/, 0, 0, 0, 0, 0 },
#else
{'q'/*113*/, 0, FLAG_STRING, etSQLESCAPE, 0, 0 },
#endif
{'r'/*114*/, 10, (FLAG_EXTENDED|FLAG_SIGNED), etORDINAL, 0, 0},
{'s'/*115*/, 0, FLAG_STRING, etSTRING, 0, 0 },
{'t'/*116*/, 0, FLAG_STRING, etURLENCODE, 0, 0 },
{'u'/*117*/, 10, 0, etRADIX, 0, 0 },
{'v'/*118*/, 0, 0, 0, 0, 0 },
#if FSLPRINTF_OMIT_SQL
{'w'/*119*/, 0, 0, 0, 0, 0 },
#else
{'w'/*119*/, 0, FLAG_STRING, etSQLESCAPE3, 0, 0 },
#endif
{'x'/*120*/, 16, 0, etRADIX, 16, 1 },
{'y'/*121*/, 0, 0, 0, 0, 0 },
{'z'/*122*/, 0, FLAG_STRING, etDYNSTRING, 0, 0},
{'{'/*123*/, 0, 0, 0, 0, 0 },
{'|'/*124*/, 0, 0, 0, 0, 0 },
{'}'/*125*/, 0, 0, 0, 0, 0 },
{'~'/*126*/, 0, 0, 0, 0, 0 },
#endif /* FSLPRINTF_FMTINFO_FIXED */
};
#define etNINFO (sizeof(fmtinfo)/sizeof(fmtinfo[0]))
#if ! FSLPRINTF_OMIT_FLOATING_POINT
/*
"*val" is a double such that 0.1 <= *val < 10.0
Return the ascii code for the leading digit of *val, then
multiply "*val" by 10.0 to renormalize.
**
Example:
input: *val = 3.14159
output: *val = 1.4159 function return = '3'
**
The counter *cnt is incremented each time. After counter exceeds
16 (the number of significant digits in a 64-bit float) '0' is
always returned.
*/
static int et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
int digit;
LONGDOUBLE_TYPE d;
if( (*cnt)++ >= 16 ) return '0';
digit = (int)*val;
d = digit;
digit += '0';
*val = (*val - d)*10.0;
return digit;
}
#endif /* !FSLPRINTF_OMIT_FLOATING_POINT */
/*
On machines with a small(?) stack size, you can redefine the
FSLPRINTF_BUF_SIZE to be less than 350. But beware - for smaller
values some %f conversions may go into an infinite loop.
*/
#ifndef FSLPRINTF_BUF_SIZE
# define FSLPRINTF_BUF_SIZE 350 /* Size of the output buffer for numeric conversions */
#endif
#if ! defined(__STDC__) && !defined(__TINYC__)
#ifdef FSLPRINTF_INT64_TYPE
typedef FSLPRINTF_INT64_TYPE int64_t;
typedef unsigned FSLPRINTF_INT64_TYPE uint64_t;
#elif defined(_MSC_VER) || defined(__BORLANDC__)
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
typedef long long int int64_t;
typedef unsigned long long int uint64_t;
#endif
#endif
#if 0
/ Not yet used. */
enum PrintfArgTypes {
TypeInt = 0,
TypeIntP = 1,
TypeFloat = 2,
TypeFloatP = 3,
TypeCString = 4
};
#endif
#if 0
/ Not yet used. */
typedef struct fsl_appendf_spec_handler_def
{
char letter; / e.g. %s */
int xtype; /* reference to the etXXXX values, or fmtinfo[*].type. */
int ntype; /* reference to PrintfArgTypes enum. */
} spec_handler;
#endif
/**
fsl_appendf_spec_handler is an almost-generic interface for farming
work out of fsl_appendfv()'s code into external functions. It doesn't
actually save much (if any) overall code, but it makes the fsl_appendfv()
code more manageable.
REQUIREMENTS of implementations:
- Expects an implementation-specific vargp pointer.
fsl_appendfv() passes a pointer to the converted value of
an entry from the format va_list. If it passes a type
other than the expected one, undefined results.
- If it calls pf then it must return the return value
from that function.
- If it calls pf it must do: pf( pfArg, D, N ), where D is
the data to export and N is the number of bytes to export.
It may call pf() an arbitrary number of times
- If pf() successfully is called, the return value must be the
accumulated totals of its return value(s), plus (possibly, but
unlikely) an imnplementation-specific amount.
- If it does not call pf() then it must return 0 (success)
or a negative number (an error) or do all of the export
processing itself and return the number of bytes exported.
SIGNIFICANT LIMITATIONS:
- Has no way of iterating over the format string, so handling
precisions and such here can't work too well. (Nevermind:
precision/justification is handled in fsl_appendfv().)
*/
typedef long (*fsl_appendf_spec_handler)( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * vargp );
/**
fsl_appendf_spec_handler for etSTRING types. It assumes that varg
is a NUL-terminated (char [const] *)
*/
static long spech_string( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
char const * ch = (char const *) varg;
return ch ? pf( pfArg, ch, pfLen ) : 0;
}
/**
fsl_appendf_spec_handler for etDYNSTRING types. It assumes that
varg is a non-const (char *). It behaves identically to
spec_string() and then calls free() on that (char *).
*/
static long spech_dynstring( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
long ret = spech_string( pf, pfArg, pfLen, varg );
fsl_free( varg );
return ret;
}
#if !FSLPRINTF_OMIT_HTML
static long spech_string_to_html( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
char const * ch = (char const *) varg;
unsigned int i;
long ret = 0;
if( ! ch ) return 0;
ret = 0;
for( i = 0; (i<pfLen) && *ch; ++ch, ++i )
{
switch( *ch )
{
case '<': ret += pf( pfArg, "<", 4 );
break;
case '&': ret += pf( pfArg, "&", 5 );
break;
default:
ret += pf( pfArg, ch, 1 );
break;
};
}
return ret;
}
static int httpurl_needs_escape( int c )
{
/*
Definition of "safe" and "unsafe" chars
was taken from:
http://www.codeguru.com/cpp/cpp/cpp_mfc/article.php/c4029/
*/
return ( (c >= 32 && c <=47)
|| ( c>=58 && c<=64)
|| ( c>=91 && c<=96)
|| ( c>=123 && c<=126)
|| ( c<32 || c>=127)
);
}
/**
The handler for the etURLENCODE specifier.
It expects varg to be a string value, which it will preceed to
encode using an URL encoding algothrim (certain characters are
converted to %XX, where XX is their hex value) and passes the
encoded string to pf(). It returns the total length of the output
string.
*/
static long spech_urlencode( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
char const * str = (char const *) varg;
long ret = 0;
char ch = 0;
char const * hex = "0123456789ABCDEF";
#define xbufsz 10
char xbuf[xbufsz];
int slen = 0;
if( ! str ) return 0;
memset( xbuf, 0, xbufsz );
ch = *str;
#define xbufsz 10
slen = 0;
for( ; ch; ch = *(++str) )
{
if( ! httpurl_needs_escape( ch ) )
{
ret += pf( pfArg, str, 1 );
continue;
}
else {
xbuf[0] = '%';
xbuf[1] = hex[((ch>>4)&0xf)];
xbuf[2] = hex[(ch&0xf)];
xbuf[3] = 0;
slen = 3;
ret += pf( pfArg, xbuf, slen );
}
}
#undef xbufsz
return ret;
}
/*
hexchar_to_int():
For 'a'-'f', 'A'-'F' and '0'-'9', returns the appropriate decimal
number. For any other character it returns -1.
*/
static int hexchar_to_int( int ch )
{
if( (ch>='a' && ch<='f') ) return ch-'a'+10;
else if( (ch>='A' && ch<='F') ) return ch-'A'+10;
else if( (ch>='0' && ch<='9') ) return ch-'0';
return -1;
}
/**
The handler for the etURLDECODE specifier.
It expects varg to be a ([const] char *), possibly encoded
with URL encoding. It decodes the string using a URL decode
algorithm and passes the decoded string to
pf(). It returns the total length of the output string.
If the input string contains malformed %XX codes then this
function will return prematurely.
*/
static long spech_urldecode( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
char const * str = (char const *) varg;
long ret = 0;
char ch = 0;
char ch2 = 0;
char xbuf[4];
int decoded;
if( ! str ) return 0;
ch = *str;
while( ch )
{
if( ch == '%' )
{
ch = *(++str);
ch2 = *(++str);
if( isxdigit(ch) &&
isxdigit(ch2) )
{
decoded = (hexchar_to_int( ch ) * 16)
+ hexchar_to_int( ch2 );
xbuf[0] = (char)decoded;
xbuf[1] = 0;
ret += pf( pfArg, xbuf, 1 );
ch = *(++str);
continue;
}
else
{
xbuf[0] = '%';
xbuf[1] = ch;
xbuf[2] = ch2;
xbuf[3] = 0;
ret += pf( pfArg, xbuf, 3 );
ch = *(++str);
continue;
}
}
else if( ch == '+' )
{
xbuf[0] = ' ';
xbuf[1] = 0;
ret += pf( pfArg, xbuf, 1 );
ch = *(++str);
continue;
}
xbuf[0] = ch;
xbuf[1] = 0;
ret += pf( pfArg, xbuf, 1 );
ch = *(++str);
}
return ret;
}
#endif /* !FSLPRINTF_OMIT_HTML */
#if !FSLPRINTF_OMIT_SQL
/**
Quotes the (char *) varg as an SQL string 'should'
be quoted. The exact type of the conversion
is specified by xtype, which must be one of
etSQLESCAPE, etSQLESCAPE2, or etSQLESCAPE3.
Search this file for those constants to find
the associated documentation.
*/
static long spech_sqlstring_main( int xtype,
fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
unsigned int i, n;
int j, ch, isnull;
int needQuote;
char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
char const * escarg = (char const *) varg;
char * bufpt = NULL;
long ret;
isnull = escarg==0;
if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
for(i=n=0; (i<pfLen) && ((ch=escarg[i])!=0); ++i){
if( ch==q ) ++n;
}
needQuote = !isnull && xtype==etSQLESCAPE2;
n += i + 1 + needQuote*2;
/* FIXME: use a static buffer here instead of malloc()! Shame on you! */
bufpt = (char *)malloc( n );
if( ! bufpt ) return -1;
j = 0;
if( needQuote ) bufpt[j++] = q;
for(i=0; (ch=escarg[i])!=0; i++){
bufpt[j++] = ch;
if( ch==q ) bufpt[j++] = ch;
}
if( needQuote ) bufpt[j++] = q;
bufpt[j] = 0;
ret = pf( pfArg, bufpt, j );
free( bufpt );
return ret;
}
static long spech_sqlstring1( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
return spech_sqlstring_main( etSQLESCAPE, pf, pfArg, pfLen, varg );
}
static long spech_sqlstring2( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
return spech_sqlstring_main( etSQLESCAPE2, pf, pfArg, pfLen, varg );
}
static long spech_sqlstring3( fsl_appendf_f pf,
void * pfArg,
unsigned int pfLen,
void * varg )
{
return spech_sqlstring_main( etSQLESCAPE3, pf, pfArg, pfLen, varg );
}
#endif /* !FSLPRINTF_OMIT_SQL */
/*
The root printf program. All variations call this core. It
implements most of the common printf behaviours plus (optionally)
some extended ones.
INPUTS:
pfAppend : The is a fsl_appendf_f function which is responsible
for accumulating the output. If pfAppend returns a negative integer
then processing stops immediately.
pfAppendArg : is ignored by this function but passed as the first
argument to pfAppend. pfAppend will presumably use it as a data
store for accumulating its string.
fmt : This is the format string, as in the usual printf().
ap : This is a pointer to a list of arguments. Same as in
vprintf() and friends.
OUTPUTS:
The return value is the total number of characters sent to the
function "func". Returns -1 on a error.
Note that the order in which automatic variables are declared below
seems to make a big difference in determining how fast this beast
will run.
Much of this code dates back to the early 1980's, supposedly.
Known change history (most historic info has been lost):
10 Feb 2008 by Stephan Beal: refactored to remove the 'useExtended'
flag (which is now always on). Added the fsl_appendf_f typedef to
make this function generic enough to drop into other source trees
without much work.
31 Oct 2008 by Stephan Beal: refactored the et_info lookup to be
constant-time instead of linear.
*/
long fsl_appendfv(
fsl_appendf_f pfAppend, /* Accumulate results here */
void * pfAppendArg, /* Passed as first arg to pfAppend. */
const char *fmt, /* Format string */
va_list ap /* arguments */
){
/**
HISTORIC NOTE (author and year unknown):
Note that the order in which automatic variables are declared below
seems to make a big difference in determining how fast this beast
will run.
*/
#if FSLPRINTF_FMTINFO_FIXED
const int useExtended = 1; /* Allow extended %-conversions */
#endif
long outCount = 0; /* accumulated output count */
int pfrc = 0; /* result from calling pfAppend */
int c; /* Next character in the format string */
char *bufpt = 0; /* Pointer to the conversion buffer */
int precision; /* Precision of the current field */
int length; /* Length of the field */
int idx; /* A general purpose loop counter */
int width; /* Width of the current field */
etByte flag_leftjustify; /* True if "-" flag is present */
etByte flag_plussign; /* True if "+" flag is present */
etByte flag_blanksign; /* True if " " flag is present */
etByte flag_alternateform; /* True if "#" flag is present */
etByte flag_altform2; /* True if "!" flag is present */
etByte flag_zeropad; /* True if field width constant starts with zero */
etByte flag_long; /* True if "l" flag is present */
etByte flag_longlong; /* True if the "ll" flag is present */
etByte done; /* Loop termination flag */
uint64_t longvalue; /* Value for integer types */
LONGDOUBLE_TYPE realvalue; /* Value for real types */
const et_info *infop = 0; /* Pointer to the appropriate info structure */
char buf[FSLPRINTF_BUF_SIZE]; /* Conversion buffer */
char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
etByte xtype = 0; /* Conversion paradigm */
char * zExtra = 0; /* Extra memory used for etTCLESCAPE conversions */
#if ! FSLPRINTF_OMIT_FLOATING_POINT
int exp, e2; /* exponent of real numbers */
double rounder; /* Used for rounding floating point values */
etByte flag_dp; /* True if decimal point should be shown */
etByte flag_rtz; /* True if trailing zeros should be removed */
etByte flag_exp; /* True to force display of the exponent */
int nsd; /* Number of significant digits returned */
#endif
/* FSLPRINTF_RETURN, FSLPRINTF_CHECKERR, and FSLPRINTF_SPACES
are internal helpers.
*/
#define FSLPRINTF_RETURN if( zExtra ) free(zExtra); return outCount;
#define FSLPRINTF_CHECKERR(FREEME) if( pfrc<0 ) { FSLPRINTF_CHARARRAY_FREE(FREEME); FSLPRINTF_RETURN; } else outCount += pfrc;
#define FSLPRINTF_SPACES(N) \
if(1){ \
FSLPRINTF_CHARARRAY(zSpaces,N); \
memset( zSpaces,' ',N); \
pfrc = pfAppend(pfAppendArg, zSpaces, N); \
FSLPRINTF_CHECKERR(zSpaces); \
FSLPRINTF_CHARARRAY_FREE(zSpaces); \
}
length = 0;
bufpt = 0;
for(; (c=(*fmt))!=0; ++fmt){
if( c!='%' ){
int amt;
bufpt = (char *)fmt;
amt = 1;
while( (c=(*++fmt))!='%' && c!=0 ) amt++;
pfrc = pfAppend( pfAppendArg, bufpt, amt);
FSLPRINTF_CHECKERR(0);
if( c==0 ) break;
}
if( (c=(*++fmt))==0 ){
pfrc = pfAppend( pfAppendArg, "%", 1);
FSLPRINTF_CHECKERR(0);
break;
}
/* Find out what flags are present */
flag_leftjustify = flag_plussign = flag_blanksign =
flag_alternateform = flag_altform2 = flag_zeropad = 0;
done = 0;
do{
switch( c ){
case '-': flag_leftjustify = 1; break;
case '+': flag_plussign = 1; break;
case ' ': flag_blanksign = 1; break;
case '#': flag_alternateform = 1; break;
case '!': flag_altform2 = 1; break;
case '0': flag_zeropad = 1; break;
default: done = 1; break;
}
}while( !done && (c=(*++fmt))!=0 );
/* Get the field width */
width = 0;
if( c=='*' ){
width = va_arg(ap,int);
if( width<0 ){
flag_leftjustify = 1;
width = -width;
}
c = *++fmt;
}else{
while( c>='0' && c<='9' ){
width = width*10 + c - '0';
c = *++fmt;
}
}
if( width > FSLPRINTF_BUF_SIZE-10 ){
width = FSLPRINTF_BUF_SIZE-10;
}
/* Get the precision */
if( c=='.' ){
precision = 0;
c = *++fmt;
if( c=='*' ){
precision = va_arg(ap,int);
if( precision<0 ) precision = -precision;
c = *++fmt;
}else{
while( c>='0' && c<='9' ){
precision = precision*10 + c - '0';
c = *++fmt;
}
}
}else{
precision = -1;
}
/* Get the conversion type modifier */
if( c=='l' ){
flag_long = 1;
c = *++fmt;
if( c=='l' ){
flag_longlong = 1;
c = *++fmt;
}else{
flag_longlong = 0;
}
}else{
flag_long = flag_longlong = 0;
}
/* Fetch the info entry for the field */
infop = 0;
#if FSLPRINTF_FMTINFO_FIXED
for(idx=0; idx<etNINFO; idx++){
if( c==fmtinfo[idx].fmttype ){
infop = &fmtinfo[idx];
if( useExtended || (infop->flags & FLAG_EXTENDED)==0 ){
xtype = infop->type;
}else{
FSLPRINTF_RETURN;
}
break;
}
}
#else
#define FMTNDX(N) (N - fmtinfo[0].fmttype)
#define FMTINFO(N) (fmtinfo[ FMTNDX(N) ])
infop = ((c>=(fmtinfo[0].fmttype)) && (c<fmtinfo[etNINFO-1].fmttype))
? &FMTINFO(c)
: 0;
/*fprintf(stderr,"char '%c'/%d @ %d, type=%c/%d\n",c,c,FMTNDX(c),infop->fmttype,infop->type);*/
if( infop ) xtype = infop->type;
#undef FMTINFO
#undef FMTNDX
#endif /* FSLPRINTF_FMTINFO_FIXED */
zExtra = 0;
if( (!infop) || (!infop->type) ){
FSLPRINTF_RETURN;
}
/* Limit the precision to prevent overflowing buf[] during conversion */
if( precision>FSLPRINTF_BUF_SIZE-40 && (infop->flags & FLAG_STRING)==0 ){
precision = FSLPRINTF_BUF_SIZE-40;
}
/*
At this point, variables are initialized as follows:
**
flag_alternateform TRUE if a '#' is present.
flag_altform2 TRUE if a '!' is present.
flag_plussign TRUE if a '+' is present.
flag_leftjustify TRUE if a '-' is present or if the
field width was negative.
flag_zeropad TRUE if the width began with 0.
flag_long TRUE if the letter 'l' (ell) prefixed
the conversion character.
flag_longlong TRUE if the letter 'll' (ell ell) prefixed
the conversion character.
flag_blanksign TRUE if a ' ' is present.
width The specified field width. This is
always non-negative. Zero is the default.
precision The specified precision. The default
is -1.
xtype The class of the conversion.
infop Pointer to the appropriate info struct.
*/
switch( xtype ){
case etPOINTER:
flag_longlong = sizeof(char*)==sizeof(int64_t);
flag_long = sizeof(char*)==sizeof(long int);
/* Fall through into the next case */
case etORDINAL:
case etRADIX:
if( infop->flags & FLAG_SIGNED ){
int64_t v;
if( flag_longlong ) v = va_arg(ap,int64_t);
else if( flag_long ) v = va_arg(ap,long int);
else v = va_arg(ap,int);
if( v<0 ){
longvalue = -v;
prefix = '-';
}else{
longvalue = v;
if( flag_plussign ) prefix = '+';
else if( flag_blanksign ) prefix = ' ';
else prefix = 0;
}
}else{
if( flag_longlong ) longvalue = va_arg(ap,uint64_t);
else if( flag_long ) longvalue = va_arg(ap,unsigned long int);
else longvalue = va_arg(ap,unsigned int);
prefix = 0;
}
if( longvalue==0 ) flag_alternateform = 0;
if( flag_zeropad && precision<width-(prefix!=0) ){
precision = width-(prefix!=0);
}
bufpt = &buf[FSLPRINTF_BUF_SIZE-1];
if( xtype==etORDINAL ){
/** i sure would like to shake the hand of whoever figured this out: */
static const char zOrd[] = "thstndrd";
int x = longvalue % 10;
if( x>=4 || (longvalue/10)%10==1 ){
x = 0;
}
buf[FSLPRINTF_BUF_SIZE-3] = zOrd[x*2];
buf[FSLPRINTF_BUF_SIZE-2] = zOrd[x*2+1];
bufpt -= 2;
}
{
const char *cset;
int base;
cset = &aDigits[infop->charset];
base = infop->base;
do{ /* Convert to ascii */
*(--bufpt) = cset[longvalue%base];
longvalue = longvalue/base;
}while( longvalue>0 );
}
length = &buf[FSLPRINTF_BUF_SIZE-1]-bufpt;
for(idx=precision-length; idx>0; idx--){
*(--bufpt) = '0'; /* Zero pad */
}
if( prefix ) *(--bufpt) = prefix; /* Add sign */
if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
const char *pre;
char x;
pre = &aPrefix[infop->prefix];
if( *bufpt!=pre[0] ){
for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
}
}
length = &buf[FSLPRINTF_BUF_SIZE-1]-bufpt;
break;
case etFLOAT:
case etEXP:
case etGENERIC:
realvalue = va_arg(ap,double);
#if ! FSLPRINTF_OMIT_FLOATING_POINT
if( precision<0 ) precision = 6; /* Set default precision */
if( precision>FSLPRINTF_BUF_SIZE/2-10 ) precision = FSLPRINTF_BUF_SIZE/2-10;
if( realvalue<0.0 ){
realvalue = -realvalue;
prefix = '-';
}else{
if( flag_plussign ) prefix = '+';
else if( flag_blanksign ) prefix = ' ';
else prefix = 0;
}
if( xtype==etGENERIC && precision>0 ) precision--;
#if 0
/* Rounding works like BSD when the constant 0.4999 is used. Wierd! */
for(idx=precision, rounder=0.4999; idx>0; idx--, rounder*=0.1);
#else
/* It makes more sense to use 0.5 */
for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
#endif
if( xtype==etFLOAT ) realvalue += rounder;
/* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
exp = 0;
#if 1
if( (realvalue)!=(realvalue) ){
/* from sqlite3: #define sqlite3_isnan(X) ((X)!=(X)) */
/* This weird array thing is to avoid constness violations
when assinging, e.g. "NaN" to bufpt.
*/
static char NaN[4] = {'N','a','N','\0'};
bufpt = NaN;
length = 3;
break;
}
#endif
if( realvalue>0.0 ){
while( realvalue>=1e32 && exp<=350 ){ realvalue *= 1e-32; exp+=32; }
while( realvalue>=1e8 && exp<=350 ){ realvalue *= 1e-8; exp+=8; }
while( realvalue>=10.0 && exp<=350 ){ realvalue *= 0.1; exp++; }
while( realvalue<1e-8 && exp>=-350 ){ realvalue *= 1e8; exp-=8; }
while( realvalue<1.0 && exp>=-350 ){ realvalue *= 10.0; exp--; }
if( exp>350 || exp<-350 ){
if( prefix=='-' ){
static char Inf[5] = {'-','I','n','f','\0'};
bufpt = Inf;
}else if( prefix=='+' ){
static char Inf[5] = {'+','I','n','f','\0'};
bufpt = Inf;
}else{
static char Inf[4] = {'I','n','f','\0'};
bufpt = Inf;
}
length = strlen(bufpt);
break;
}
}
bufpt = buf;
/*
If the field type is etGENERIC, then convert to either etEXP
or etFLOAT, as appropriate.
*/
flag_exp = xtype==etEXP;
if( xtype!=etFLOAT ){
realvalue += rounder;
if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
}
if( xtype==etGENERIC ){
flag_rtz = !flag_alternateform;
if( exp<-4 || exp>precision ){
xtype = etEXP;
}else{
precision = precision - exp;
xtype = etFLOAT;
}
}else{
flag_rtz = 0;
}
if( xtype==etEXP ){
e2 = 0;
}else{
e2 = exp;
}
nsd = 0;
flag_dp = (precision>0) | flag_alternateform | flag_altform2;
/* The sign in front of the number */
if( prefix ){
*(bufpt++) = prefix;
}
/* Digits prior to the decimal point */
if( e2<0 ){
*(bufpt++) = '0';
}else{
for(; e2>=0; e2--){
*(bufpt++) = et_getdigit(&realvalue,&nsd);
}
}
/* The decimal point */
if( flag_dp ){
*(bufpt++) = '.';
}
/* "0" digits after the decimal point but before the first
significant digit of the number */
for(e2++; e2<0 && precision>0; precision--, e2++){
*(bufpt++) = '0';
}
/* Significant digits after the decimal point */
while( (precision--)>0 ){
*(bufpt++) = et_getdigit(&realvalue,&nsd);
}
/* Remove trailing zeros and the "." if no digits follow the "." */
if( flag_rtz && flag_dp ){
while( bufpt[-1]=='0' ) *(--bufpt) = 0;
/* assert( bufpt>buf ); */
if( bufpt[-1]=='.' ){
if( flag_altform2 ){
*(bufpt++) = '0';
}else{
*(--bufpt) = 0;
}
}
}
/* Add the "eNNN" suffix */
if( flag_exp || (xtype==etEXP && exp) ){
*(bufpt++) = aDigits[infop->charset];
if( exp<0 ){
*(bufpt++) = '-'; exp = -exp;
}else{
*(bufpt++) = '+';
}
if( exp>=100 ){
*(bufpt++) = (exp/100)+'0'; /* 100's digit */
exp %= 100;
}
*(bufpt++) = exp/10+'0'; /* 10's digit */
*(bufpt++) = exp%10+'0'; /* 1's digit */
}
*bufpt = 0;
/* The converted number is in buf[] and zero terminated. Output it.
Note that the number is in the usual order, not reversed as with
integer conversions. */
length = bufpt-buf;
bufpt = buf;
/* Special case: Add leading zeros if the flag_zeropad flag is
set and we are not left justified */
if( flag_zeropad && !flag_leftjustify && length < width){
int i;
int nPad = width - length;
for(i=width; i>=nPad; i--){
bufpt[i] = bufpt[i-nPad];
}
i = prefix!=0;
while( nPad-- ) bufpt[i++] = '0';
length = width;
}
#endif /* !FSLPRINTF_OMIT_FLOATING_POINT */
break;
#if !FSLPRINTF_OMIT_SIZE
case etSIZE:
*(va_arg(ap,int*)) = outCount;
length = width = 0;
break;
#endif
case etPERCENT:
buf[0] = '%';
bufpt = buf;
length = 1;
break;
case etCHARLIT:
case etCHARX:
c = buf[0] = (xtype==etCHARX ? va_arg(ap,int) : *++fmt);
if( precision>=0 ){
for(idx=1; idx<precision; idx++) buf[idx] = c;
length = precision;
}else{
length =1;
}
bufpt = buf;
break;
case etSTRING: {
bufpt = va_arg(ap,char*);
length = bufpt ? strlen(bufpt) : 0;
if( precision>=0 && precision<length ) length = precision;
break;
}
case etDYNSTRING: {
/* etDYNSTRING needs to be handled separately because it
free()s its argument (which isn't available outside this
block). This means, though, that %-#z does not work.
*/
bufpt = va_arg(ap,char*);
length = bufpt ? strlen(bufpt) : 0;
pfrc = spech_dynstring( pfAppend, pfAppendArg,
(precision<length) ? precision : length,
bufpt );
FSLPRINTF_CHECKERR(0);
length = 0;
break;
}
#if ! FSLPRINTF_OMIT_HTML
case etHTML:{
bufpt = va_arg(ap,char*);
length = bufpt ? strlen(bufpt) : 0;
pfrc = spech_string_to_html( pfAppend, pfAppendArg,
(precision<length) ? precision : length,
bufpt );
FSLPRINTF_CHECKERR(0);
length = 0;
break;
}
case etURLENCODE:{
bufpt = va_arg(ap,char*);
length = bufpt ? strlen(bufpt) : 0;
pfrc = spech_urlencode( pfAppend, pfAppendArg,
(precision<length) ? precision : length,
bufpt );
FSLPRINTF_CHECKERR(0);
length = 0;
break;
}
case etURLDECODE:{
bufpt = va_arg(ap,char*);
length = bufpt ? strlen(bufpt) : 0;
pfrc = spech_urldecode( pfAppend, pfAppendArg,
(precision<length) ? precision : length,
bufpt );
FSLPRINTF_CHECKERR(0);
length = 0;
break;
}
#endif /* FSLPRINTF_OMIT_HTML */
#if ! FSLPRINTF_OMIT_SQL
case etSQLESCAPE:
case etSQLESCAPE2:
case etSQLESCAPE3: {
fsl_appendf_spec_handler spf =
(xtype==etSQLESCAPE)
? spech_sqlstring1
: ((xtype==etSQLESCAPE2)
? spech_sqlstring2
: spech_sqlstring3
);
bufpt = va_arg(ap,char*);
length = bufpt ? strlen(bufpt) : 0;
pfrc = spf( pfAppend, pfAppendArg,
(precision<length) ? precision : length,
bufpt );
FSLPRINTF_CHECKERR(0);
length = 0;
}
#endif /* !FSLPRINTF_OMIT_SQL */
}/* End switch over the format type */
/*
The text of the conversion is pointed to by "bufpt" and is
"length" characters long. The field width is "width". Do
the output.
*/
if( !flag_leftjustify ){
int nspace;
nspace = width-length;
if( nspace>0 ){
FSLPRINTF_SPACES(nspace);
}
}
if( length>0 ){
pfrc = pfAppend( pfAppendArg, bufpt, length);
FSLPRINTF_CHECKERR(0);
}
if( flag_leftjustify ){
int nspace;
nspace = width-length;
if( nspace>0 ){
FSLPRINTF_SPACES(nspace);
}
}
if( zExtra ){
free(zExtra);
zExtra = 0;
}
}/* End for loop over the format string */
FSLPRINTF_RETURN;
} /* End of function */
#undef FSLPRINTF_SPACES
#undef FSLPRINTF_CHECKERR
#undef FSLPRINTF_RETURN
#undef FSLPRINTF_OMIT_FLOATING_POINT
#undef FSLPRINTF_OMIT_SIZE
#undef FSLPRINTF_OMIT_SQL
#undef FSLPRINTF_BUF_SIZE
#undef FSLPRINTF_OMIT_HTML
long fsl_appendf(fsl_appendf_f pfAppend, /* Accumulate results here */
void * pfAppendArg, /* Passed as first arg to pfAppend. */
const char *fmt, /* Format string */
... ){
long ret;
va_list vargs;
va_start( vargs, fmt );
ret = fsl_appendfv( pfAppend, pfAppendArg, fmt, vargs );
va_end(vargs);
return ret;
}
/*
** fsl_appendf_f() impl which requires that state be-a writable
** (FILE*).
*/
static long fsl_appendf_f_FILE( void * state,
char const * s, long n ){
if( !state ) return -1;
else return (1==fwrite( s, (size_t)n, 1, (FILE *)state ))
? n : -2;
}
long fsl_appendf_FILE( FILE * fp, char const * fmt, ... ){
if(!fp || !fmt) return -1;
else {
long ret;
va_list vargs;
va_start( vargs, fmt );
ret = fsl_appendfv( fsl_appendf_f_FILE, fp, fmt, vargs );
va_end(vargs);
return ret;
}
}
char * fsl_mprintfv( char const * fmt, va_list vargs ){
if( !fmt ) return 0;
else{
fsl_buffer buf = fsl_buffer_empty;
int const rc = fsl_buffer_appendfv( &buf, fmt, vargs );
if(rc){
fsl_buffer_reserve(&buf, 0);
assert(0==buf.mem);
}
return (char*)buf.mem /*transfer ownership*/;
}
}
char * fsl_mprintf( char const * fmt, ... ){
char * ret;
va_list vargs;
va_start( vargs, fmt );
ret = fsl_mprintfv( fmt, vargs );
va_end( vargs );
return ret;
}
|