-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpsnprntf.cpp
1158 lines (1016 loc) · 29.2 KB
/
psnprntf.cpp
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
//=============================================================================
// psnprintf
//
// Portable snprintf
//
// Amongst other things, tries to always guarantee the buffer will be null-
// terminated, which some implementations do not do.
//=============================================================================
#include <string.h> /* for memset */
#include <stdarg.h> /* for va_list */
#include <stdlib.h> /* for fcvt */
#include <inttypes.h> /* haleyjd */
#include "psnprntf.h"
/* Windows stdlib defines fcvt differently <sigh> */
/* haleyjd: not all platforms have fcvt. As a guy on comp.os.msdos.djgpp
* so eloquently put it, this is driving out Baal by invoking
* Beelzebub. So, I've copied DJGPP's (barely) portable fcvt, and it
* can be made to compile by defining NO_FCVT. This is not good, but
* it'll do.
*/
#ifndef NO_FCVT
#ifdef __FreeBSD__ // [Kate] Update as necessary
#define NO_FCVT
#endif
#ifdef WIN32
#ifndef CYGWIN
#define FCVT _fcvt
#else
#define FCVT fcvt
#endif
#else
#define FCVT fcvt
#endif
#endif
#ifdef NO_FCVT
#include "m_fcvt.h"
#define FCVT M_Fcvt
#endif
int psnprintf(char *str, size_t n, const char *format, ...)
{
va_list args;
int ret;
va_start(args, format);
ret = pvsnprintf(str, n, format, args);
va_end(args);
return ret;
}
#define STATE_NONE 0
#define STATE_OPERATOR 1 /* Just received % */
#define STATE_FLAG 2 /* Just received a flag or prefix or width */
#define STATE_WIDTH 3
#define STATE_BEFORE_PRECISION 4 /* just got dot */
#define STATE_PRECISION 5 /* got at least one number after dot */
#define STATE_PREFIX 6 /* just received prefix (h, l or L) */
#define UNKNOWN_WIDTH 0
#define VARIABLE_WIDTH -2
#define UNKNOWN_PRECISION -1
#define VARIABLE_PRECISION -2
/* Following macros give reusable switch cases, used in combination
* depending on current state. Sucks to do these as macros, but should
* give the compiler lots of freedom to optimize.
*/
#define CHECK_FLAG \
case '-': \
info.flags |= FLAG_LEFT_ALIGN; \
state = STATE_FLAG; \
break; \
case '+': \
info.flags |= FLAG_SIGNED; \
state = STATE_FLAG; \
break; \
case '0': \
info.flags |= FLAG_ZERO_PAD; \
state = STATE_FLAG; \
break; \
case ' ': \
info.flags |= FLAG_SIGN_PAD; \
state = STATE_FLAG; \
break; \
case '#': \
info.flags |= FLAG_HASH; \
state = STATE_FLAG; \
break;
#define CHECK_WIDTH \
case '1': \
case '2': \
case '3': \
case '4': \
case '5': \
case '6': \
case '7': \
case '8': \
case '9': \
info.width = *info.fmt - '0'; /* convert to integer */ \
state = STATE_WIDTH; \
break; \
case '*': \
info.width = VARIABLE_WIDTH; \
state = STATE_WIDTH; \
break;
#define CHECK_PRECISION \
case '.': \
info.precision = 0; \
state = STATE_BEFORE_PRECISION; \
break;
#define CHECK_PREFIX \
case 'h': \
case 'l': \
case 'L': \
info.prefix = *info.fmt; \
state = STATE_PREFIX; \
break;
#define GET_VARS \
if (info.width == VARIABLE_WIDTH) \
info.width = va_arg(ap, int); \
if (info.precision == VARIABLE_PRECISION) \
info.precision = va_arg(ap, int);
#define CHECK_TYPE \
case 'd': \
case 'i': \
case 'u': \
case 'o': \
case 'x': \
case 'X': \
case 'p': \
GET_VARS \
if(*info.fmt == 'p') \
ip.p = va_arg(ap, void *); \
else \
ip.i = va_arg(ap, int); \
ncount += pvsnfmt_int(&info, &ip); \
state = STATE_NONE; \
break; \
case 'e': \
case 'E': \
case 'f': \
case 'g': \
case 'G': \
GET_VARS \
ncount += pvsnfmt_double(&info, (double)(va_arg(ap, double))); \
state = STATE_NONE; \
break; \
case 'c': \
GET_VARS \
ncount += pvsnfmt_char(&info, (char)(va_arg(ap, int))); \
state = STATE_NONE; \
break; \
case 's': \
GET_VARS \
ncount += pvsnfmt_str(&info, (const char *)(va_arg(ap, const char *))); \
state = STATE_NONE; \
break; \
case 'n': \
*(va_arg(ap, int *)) = ncount; \
state = STATE_NONE; \
break;
#define PUTCHAR(ch) \
if (nmax > 1) \
{ \
*info.pinsertion++ = ch; \
nmax--; \
} \
ncount++;
int pvsnprintf(char *str, size_t nmax, const char *format, va_list ap)
{
/* nmax gives total size of buffer including null
* null is ALWAYS added, even if buffer too small for format
* (contrary to C99)
*/
int ncount = 0; /* number of characters printed so far */
int state = STATE_NONE;
/* haleyjd 08/01/09: rewrite to use structure */
pvsnfmt_vars info;
pvsnfmt_intparm_t ip;
info.pinsertion = str;
info.nmax = nmax;
info.fmt = format;
info.flags = 0;
info.width = 0;
info.precision = 0;
info.prefix = 0;
while(*info.fmt)
{
switch (state)
{
case STATE_NONE:
switch (*(info.fmt))
{
case '%':
state = STATE_OPERATOR;
info.flags = FLAG_DEFAULT;
info.width = UNKNOWN_WIDTH;
info.precision = UNKNOWN_PRECISION;
info.prefix = '\0';
break;
default:
PUTCHAR(*(info.fmt))
}
break;
case STATE_OPERATOR:
switch (*(info.fmt))
{
CHECK_FLAG
CHECK_WIDTH
CHECK_PRECISION
CHECK_PREFIX
CHECK_TYPE
default:
PUTCHAR(*info.fmt) /* Unknown format, just print it (e.g. "%%") */
state = STATE_NONE;
}
break;
case STATE_FLAG:
switch (*info.fmt)
{
CHECK_FLAG
CHECK_WIDTH
CHECK_PRECISION
CHECK_PREFIX
CHECK_TYPE
}
break;
case STATE_WIDTH:
if (*info.fmt >= '0' && *info.fmt <= '9' && info.width != -1)
{
info.width = info.width * 10 + (*info.fmt - '0');
break;
}
switch (*info.fmt)
{
CHECK_PRECISION
CHECK_PREFIX
CHECK_TYPE
}
break;
case STATE_BEFORE_PRECISION:
if (*info.fmt >= '0' && *info.fmt <= '9')
{
info.precision = *info.fmt - '0';
state = STATE_PRECISION;
}
else if (*info.fmt == '*')
{
info.precision = VARIABLE_PRECISION;
state = STATE_PRECISION;
}
switch (*info.fmt)
{
CHECK_PREFIX
CHECK_TYPE
}
break;
case STATE_PRECISION:
if (*info.fmt >= '0' && *info.fmt <= '9' && info.precision != -1)
{
info.precision = info.precision * 10 + (*info.fmt - '0');
break;
}
switch (*info.fmt)
{
CHECK_PREFIX
CHECK_TYPE
}
break;
case STATE_PREFIX:
switch (*info.fmt)
{
CHECK_TYPE
}
} /* switch state */
info.fmt++;
} /* while *pfmt */
/* Add null if there is room
* NOTE there is always room even if str doesn't fit unless
* nmax initially passed in as 0. fmt functions take care to
* always leave at least one free byte at end.
*/
if (nmax > 0)
*info.pinsertion = '\0';
return ncount;
}
int pvsnfmt_char(pvsnfmt_vars *info, char c)
{
if(info->nmax > 1)
{
*(info->pinsertion) = c;
info->pinsertion += 1;
info->nmax -= 1;
}
return 1;
}
/* strnlen not available on all platforms.. maybe autoconf it? */
size_t pstrnlen(const char *s, size_t count)
{
const char *p = s;
while (*p && count-- > 0)
p++;
return p - s;
}
/* Format a string into the buffer. Parameters:
* *&pinsertion Reference to pointer to buffer (can be reference to NULL)
* &nmax Reference to size of buffer. This is may be modified
* fmt Format character ('s')
* flags 0 or combination of flags (see .h file for #defines)
* width Width of string, as defined in printf
* precision Precision of string, as defined in printf
* ap Argument list
*/
int pvsnfmt_str(pvsnfmt_vars *info, const char *s)
{
const char *str = s;
int nprinted;
int len;
int pad = 0;
int width, flags;
width = info->width;
flags = info->flags;
/* Get width magnitude, set aligment flag */
if(width < 0)
{
width = -width;
flags |= FLAG_LEFT_ALIGN;
}
/* Truncate due to precision */
if (info->precision < 0)
len = strlen(str);
else
len = pstrnlen(str, info->precision);
/* Determine padding length */
if (width > len)
pad = width - len;
/* Exit if just counting (not printing) */
if (info->nmax <= 1)
return len + pad;
/* If right-aligned, print pad */
if ( !(flags & FLAG_LEFT_ALIGN) )
{
char padchar;
if (flags & FLAG_ZERO_PAD)
padchar = '0';
else
padchar = ' ';
if ((int) info->nmax - 1 < pad)
nprinted = info->nmax - 1;
else
nprinted = pad;
memset(info->pinsertion, padchar, nprinted);
info->pinsertion += nprinted;
info->nmax -= nprinted;
}
/* Output string */
if (info->nmax <= 1)
nprinted = 0;
else if ((int) info->nmax - 1 < len)
nprinted = info->nmax - 1;
else
nprinted = len;
memcpy(info->pinsertion, str, nprinted);
info->pinsertion += nprinted;
info->nmax -= nprinted;
/* If left aligned, add pad */
if (flags & FLAG_LEFT_ALIGN)
{
if (info->nmax <= 1)
nprinted = 0;
else if ((int)info->nmax - 1 < pad)
nprinted = info->nmax - 1;
else
nprinted = pad;
if(nprinted)
memset(info->pinsertion, ' ', nprinted);
info->pinsertion += nprinted;
info->nmax -= nprinted;
}
return len + pad; /* Return total length of pad + string even if some
* was truncated
*/
}
/* Format an integer into the buffer. Parameters:
* *&pinsertion Reference to pointer to buffer (can be reference to NULL)
* &nmax Reference to size of buffer. This is may be modified
* fmt Format character (one of "diuoxX")
* flags 0 or combination of flags (see .h file for #defines)
* width Width of integer, as defined in printf
* precision Precision of integer, as defined in printf
* ap Argument list
*/
int pvsnfmt_int(pvsnfmt_vars *info, pvsnfmt_intparm_t *ip)
{
int number = 0;
unsigned int unumber = 0;
char numbersigned = 1;
char iszero = 0; /* bool */
int base = 10; /* haleyjd: default to something valid */
int len = 0; /* length of number component (no sign or padding) */
char char10 = 0;
char sign = 0;
int widthpad = 0;
int addprefix = 0; /* optional "0x" = 2 */
int totallen;
int temp; /* haleyjd 07/19/03: bug fix */
/* Stack used to hold digits, which are generated backwards
* and need to be popped off in the correct order
*/
char numstack[22]; /* largest 64 bit number has 22 octal digits */
char *stackpos = numstack;
char fmt = *info->fmt;
int flags = info->flags;
int precision = info->precision;
#define PUSH(x) \
*stackpos++ = (char)(x)
#define POP() \
*(--stackpos)
// haleyjd: init this array for safety
memset(numstack, 0, sizeof(numstack));
/* Retrieve value */
switch (info->prefix)
{
case 'h':
switch (fmt)
{
case 'd':
case 'i':
number = (signed short int) ip->i;
break;
case 'u':
case 'o':
case 'x':
case 'X':
unumber = (unsigned short int) ip->i;
numbersigned = 0;
break;
case 'p':
unumber = (unsigned int)((size_t)ip->p); // FIXME: Not x64 friendly
numbersigned = 0;
break;
}
break;
case 'l':
switch (fmt)
{
case 'd':
case 'i':
number = ip->i;
break;
case 'u':
case 'o':
case 'x':
case 'X':
unumber = (unsigned int) ip->i;
numbersigned = 0;
break;
case 'p':
unumber = (unsigned int)((size_t)ip->p); // FIXME: Not x64 friendly
numbersigned = 0;
break;
}
break;
default:
switch (fmt)
{
case 'd':
case 'i':
number = ip->i;
break;
case 'u':
case 'o':
case 'x':
case 'X':
unumber = (unsigned int) ip->i;
numbersigned = 0;
break;
case 'p':
unumber = (unsigned int)((size_t)ip->p); // FIXME: Not x64 friendly
numbersigned = 0;
break;
}
} /* switch fmt to retrieve number */
if (fmt == 'p')
{
fmt = 'x';
flags |= FLAG_HASH;
}
/* Discover base */
switch (fmt)
{
case 'd':
case 'i':
case 'u':
base = 10;
break;
case 'o':
base = 8;
break;
case 'X':
base = 16;
char10 = 'A';
break;
case 'x':
base = 16;
char10 = 'a';
}
if (numbersigned)
{
if (number < 0)
{
/* Deal with negativity */
sign = '-';
number = -number;
}
else if (flags & FLAG_SIGNED)
{
sign = '+';
}
else if (flags & FLAG_SIGN_PAD)
{
sign = ' ';
}
}
/* Create number */
if (numbersigned)
{
if (number == 0)
iszero = 1;
do
{
PUSH(number % base);
number /= base;
len++;
} while (number != 0);
}
else
{
if (unumber == 0)
iszero = 1;
do
{
PUSH(unumber % base);
unumber /= base;
len++;
} while (unumber != 0);
}
/* Octal hash character (alternate form) */
if (fmt == 'o' && (flags & FLAG_HASH) && precision <= len &&
precision != 0 && !iszero )
{
precision = len + 1;
}
/* Determine width of sign, if any. */
if ( (fmt == 'x' || fmt == 'X') && (flags & FLAG_HASH) && !iszero )
addprefix = 2;
else if (sign != 0)
addprefix = 1;
/* Make up precision (zero pad on left) */
while (len < precision)
{
PUSH(0);
len++;
}
if (len + addprefix < info->width)
{
totallen = info->width;
widthpad = info->width - (len + addprefix);
}
else
totallen = len + addprefix;
if (info->nmax <= 1)
return totallen;
/* Write sign or "0x" */
if (flags & FLAG_ZERO_PAD)
{
if (addprefix == 2) /* 0x */
{
if (info->nmax > 1)
{
*(info->pinsertion) = '0';
info->pinsertion += 1;
info->nmax -= 1;
}
if (info->nmax > 1)
{
*(info->pinsertion) = fmt;
info->pinsertion += 1;
info->nmax -= 1;
}
}
else if (addprefix == 1) /* sign */
{
if (info->nmax > 1)
{
*(info->pinsertion) = sign;
info->pinsertion += 1;
info->nmax -= 1;
}
}
}
/* Width pad */
if ( !(flags & FLAG_LEFT_ALIGN) )
{
/* haleyjd 07/19/03: bug fix: nmax + 1 => nmax - 1 */
if (info->nmax <= 1)
widthpad = 0;
else if ((int) info->nmax - 1 < widthpad)
widthpad = info->nmax - 1;
if (flags & FLAG_ZERO_PAD)
memset(info->pinsertion, '0', widthpad);
else
memset(info->pinsertion, ' ', widthpad);
info->pinsertion += widthpad;
info->nmax -= widthpad;
}
/* Write sign or "0x" */
if ( !(flags & FLAG_ZERO_PAD) )
{
if (addprefix == 2) /* 0x */
{
if (info->nmax > 1)
{
*(info->pinsertion) = '0';
info->pinsertion += 1;
info->nmax -= 1;
}
if (info->nmax > 1)
{
*(info->pinsertion) = fmt;
info->pinsertion += 1;
info->nmax -= 1;
}
}
else if (addprefix == 1) /* sign */
{
if (info->nmax > 1)
{
*(info->pinsertion) = sign;
info->pinsertion += 1;
info->nmax -= 1;
}
}
}
/* haleyjd 07/19/03: bug fix: nmax + 1 => nmax - 1 */
/* Write number */
if (info->nmax <= 1)
len = 0;
else if ((int) info->nmax - 1 < len)
len = info->nmax - 1;
/* haleyjd 07/19/03: bug fix: Do NOT use len as the counter
* variable for this loop. This messes up the length calculations
* afterward, and allows writing off the end of the string buffer.
* Special thanks to schepe for nailing this down.
*/
temp = len;
for (; temp > 0; temp--)
{
char n = POP();
if (n <= 9)
{
*(info->pinsertion) = n + '0';
info->pinsertion += 1;
}
else
{
*(info->pinsertion) = n - 10 + char10;
info->pinsertion += 1;
}
}
info->nmax -= len;
if (flags & FLAG_LEFT_ALIGN)
{
/* haleyjd 07/19/03: bug fix: nmax + 1 => nmax - 1 */
if (info->nmax <= 1)
widthpad = 0;
else if ((int) info->nmax - 1 < widthpad)
widthpad = info->nmax - 1;
if(widthpad)
memset(info->pinsertion, ' ', widthpad);
info->pinsertion += widthpad;
info->nmax -= widthpad;
}
return totallen;
}
/*
* WARNING: Assumes 64 bit doubles
*/
typedef union DBLBITS_u
{
double D;
struct word_s
{
uint32_t W0;
uint32_t W1;
} WORDS; /* haleyjd: this must be named */
} DBLBITS;
#define EXP_MASK 0x7FF00000
#define SIGN_MASK 0x80000000
#define MANTISSA_MASK ~(SIGN_MASK | EXP_MASK)
#define QNAN_MASK 0x00080000 /* first bit of mantissa */
/* These functions not defined on all platforms, so
* do the checks manually
* WARNING: Assumes 64-bit IEEE representation
* (so it won't run on your Cray :)
*/
#define ISNAN(x) \
( (((DBLBITS *) &x)->WORDS.W1 & EXP_MASK) == EXP_MASK \
&& ((((DBLBITS *) &x)->WORDS.W1 & MANTISSA_MASK) != 0 || ((DBLBITS *) &x)->WORDS.W0 != 0) )
#define ISQNAN(x) \
( ISNAN(x) && (((DBLBITS *) &x)->WORDS.W1 & QNAN_MASK) )
#define ISSNAN(x) \
( ISNAN(x) && !ISQNAN(x) )
#define ISINF(x) \
( (((DBLBITS *) &x)->WORDS.W1 & EXP_MASK) == EXP_MASK \
&& ((((DBLBITS *) &x)->WORDS.W1 & MANTISSA_MASK) == 0 && ((DBLBITS *) &x)->WORDS.W0 == 0) )
/* Format a double-precision float into the buffer. Parameters:
* *&pinsertion Reference to pointer to buffer (can be reference to NULL)
* &nmax Reference to size of buffer. This is may be modified
* fmt Format character (one of "eEfgG")
* flags 0 or combination of flags (see .h file for #defines)
* width Width of double, as defined in printf
* precision Precision of double, as defined in printf
* ap Argument list
*/
int pvsnfmt_double(pvsnfmt_vars *info, double d)
{
char *digits;
int sign = 0;
int dec;
double value = d;
int len;
int pad = 0;
//int signwidth = 0;
int totallen;
char signchar = 0;
int leadingzeros = 0;
int printdigits; /* temporary var used in different contexts */
int flags = info->flags;
int width = info->width;
const char fmt = *(info->fmt);
int precision = info->precision;
/* Check for special values first */
const char *special = 0;
if (ISSNAN(value))
special = "NaN";
else if (ISQNAN(value))
special = "NaN";
else if ( ISINF(value) )
{
if (value < 0)
sign = 1;
special = "Inf";
}
if (special)
{
totallen = len = strlen(special);
/* Sign (this is silly for NaN but conforming to printf */
if (flags & (FLAG_SIGNED | FLAG_SIGN_PAD) || sign)
{
if (sign)
signchar = '-';
else if (flags & FLAG_SIGN_PAD)
signchar = ' ';
else
signchar = '+';
totallen++;
}
/* Padding */
if (totallen < width)
pad = width - totallen;
else
pad = 0;
totallen += pad ;
// haleyjd 05/07/08: this was forgotten!
if (info->nmax <= 1)
return totallen;
/* Sign now if zeropad */
if (flags & FLAG_ZERO_PAD && signchar)
{
if (info->nmax > 1)
{
*(info->pinsertion) = signchar;
info->pinsertion += 1;
info->nmax -= 1;
}
}
/* Right align */
if ( !(flags & FLAG_LEFT_ALIGN) )
{
if (info->nmax <= 1)
pad = 0;
else if ((int) info->nmax - 1 < pad )
pad = info->nmax - 1;
if(pad)
{
if (flags & FLAG_ZERO_PAD)
memset(info->pinsertion, '0', pad );
else
memset(info->pinsertion, ' ', pad );
}
info->pinsertion += pad ;
info->nmax -= pad ;
}
/* Sign now if not zeropad */
if (!(flags & FLAG_ZERO_PAD) && signchar)
{
if (info->nmax > 1)
{
*(info->pinsertion) = signchar;
info->pinsertion += 1;
info->nmax -= 1;
}
}
if (info->nmax <= 0)
len = 0;
else if ((int) info->nmax - 1 < len)
len = info->nmax - 1;
if(len)
memcpy(info->pinsertion, special, len);
info->pinsertion += len;
info->nmax -= len;
/* Left align */
if (flags & FLAG_LEFT_ALIGN)
{
if (info->nmax <= 1)
pad = 0;
else if ((int) info->nmax - 1 < pad )
pad = info->nmax - 1;
if(pad)
memset(info->pinsertion, ' ', pad );
info->pinsertion += pad ;
info->nmax -= pad ;
}
return totallen;
}
if (fmt == 'f')
{
if (precision == UNKNOWN_PRECISION)
precision = 6;
digits = FCVT(value, precision, &dec, &sign);
len = strlen(digits);
if (dec > 0)
totallen = dec;
else
totallen = 0;
/* plus 1 for decimal place */
if (dec <= 0)
totallen += 2; /* and trailing ".0" */
else if (precision > 0 || flags & FLAG_HASH)
totallen += 1;
/* Determine sign width (0 or 1) */
if (flags & (FLAG_SIGNED | FLAG_SIGN_PAD) || sign)
{
if (sign)
signchar = '-';
else if (flags & FLAG_SIGN_PAD)
signchar = ' ';
else
signchar = '+';
totallen++;
}
/* Determine if leading zeros required */
if (dec <= 0)
{
leadingzeros = 1 - dec; /* add one for zero before decimal point (0.) */
}
if (leadingzeros - 1 > precision)
totallen += precision;
else if (len - dec > 0)
totallen += precision;
else
totallen += leadingzeros;
/* Determine padding width */
if (totallen < width)
pad = width - totallen;
totallen += pad;
if (info->nmax <= 1)
return totallen;
/* Now that the length has been calculated, print as much of it
* as possible into the buffer
*/
/* Print sign now if padding with zeros */
if (flags & FLAG_ZERO_PAD && signchar != 0)
{
if (info->nmax > 1)