e6668c9e409081c30a3c9ad7d83da7f6e6c489bc
[reactos.git] / reactos / lib / sdk / crt / misc / i10output.c
1 #include <precomp.h>
2
3 #define I10_OUTPUT_MAX_PREC 21
4 /* Internal structure used by $I10_OUTPUT */
5 struct _I10_OUTPUT_DATA {
6 short pos;
7 char sign;
8 BYTE len;
9 char str[I10_OUTPUT_MAX_PREC+1]; /* add space for '\0' */
10 };
11
12 /*********************************************************************
13 * $I10_OUTPUT (MSVCRT.@)
14 * ld - long double to be printed to data
15 * prec - precision of part, we're interested in
16 * flag - 0 for first prec digits, 1 for fractional part
17 * data - data to be populated
18 *
19 * return value
20 * 0 if given double is NaN or INF
21 * 1 otherwise
22 *
23 * FIXME
24 * Native sets last byte of data->str to '0' or '9', I don't know what
25 * it means. Current implementation sets it always to '0'.
26 */
27 int CDECL MSVCRT_I10_OUTPUT(long double ld, int prec, int flag, struct _I10_OUTPUT_DATA *data)
28 {
29 static const char inf_str[] = "1#INF";
30 static const char nan_str[] = "1#QNAN";
31
32 double d = ld;
33 char format[8];
34 char buf[I10_OUTPUT_MAX_PREC+9]; /* 9 = strlen("0.e+0000") + '\0' */
35 char *p;
36
37 TRACE("(%lf %d %x %p)\n", d, prec, flag, data);
38
39 if(d<0) {
40 data->sign = '-';
41 d = -d;
42 } else
43 data->sign = ' ';
44
45 if(!_finite(d)) {
46 data->pos = 1;
47 data->len = 5;
48 memcpy(data->str, inf_str, sizeof(inf_str));
49
50 return 0;
51 }
52
53 if(_isnan(d)) {
54 data->pos = 1;
55 data->len = 6;
56 memcpy(data->str, nan_str, sizeof(nan_str));
57
58 return 0;
59 }
60
61 if(flag&1) {
62 int exp = (int)(1+floor(log10(d)));
63
64 prec += exp;
65 if(exp < 0)
66 prec--;
67 }
68 prec--;
69
70 if(prec+1 > I10_OUTPUT_MAX_PREC)
71 prec = I10_OUTPUT_MAX_PREC-1;
72 else if(prec < 0) {
73 d = 0.0;
74 prec = 0;
75 }
76
77 sprintf(format, "%%.%dle", prec);
78 sprintf(buf, format, d);
79
80 buf[1] = buf[0];
81 data->pos = atoi(buf+prec+3);
82 if(buf[1] != '0')
83 data->pos++;
84
85 for(p = buf+prec+1; p>buf+1 && *p=='0'; p--);
86 data->len = (BYTE)(p - buf);
87
88 memcpy(data->str, buf+1, data->len);
89 data->str[data->len] = '\0';
90
91 if(buf[1]!='0' && prec-data->len+1>0)
92 memcpy(data->str+data->len+1, buf+data->len+1, prec-data->len+1);
93
94 return 1;
95 }
96 #undef I10_OUTPUT_MAX_PREC
97
98
99