1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <float.h>
5 #include <math.h>
6 #include <malloc.h>
7 // #include <msvcrt/locale.h>
8
9 // replace fjgpp fcvtbuf from project http://www.jbox.dk/sanos/source/lib/fcvt.c.html
10 // with small modification's to match ReactOS arch
11
12 // Floating point to string conversion routines
13 //
14 // Copyright (C) 2002 Michael Ringgaard. All rights reserved.
15 //
16 // Redistribution and use in source and binary forms, with or without
17 // modification, are permitted provided that the following conditions
18 // are met:
19 //
20 // 1. Redistributions of source code must retain the above copyright
21 // notice, this list of conditions and the following disclaimer.
22 // 2. Redistributions in binary form must reproduce the above copyright
23 // notice, this list of conditions and the following disclaimer in the
24 // documentation and/or other materials provided with the distribution.
25 // 3. Neither the name of the project nor the names of its contributors
26 // may be used to endorse or promote products derived from this software
27 // without specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
30 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
33 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 // SUCH DAMAGE.
40 //
41
42
43 //#include <math.h>
44 #define CVTBUFSIZE 2 * DBL_MAX_10_EXP + 10
cvt(double arg,int ndigits,int * decpt,int * sign,char * buf,int eflag)45 static char *cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag)
46 {
47 int r2;
48 double fi, fj;
49 char *p, *p1;
50
51 if (ndigits >= CVTBUFSIZE - 1) ndigits = CVTBUFSIZE - 2;
52 r2 = 0;
53 *sign = 0;
54 p = &buf[0];
55 if (arg < 0)
56 {
57 *sign = 1;
58 arg = -arg;
59 }
60 arg = modf(arg, &fi);
61 p1 = &buf[CVTBUFSIZE];
62
63 if (fi != 0)
64 {
65 p1 = &buf[CVTBUFSIZE];
66 while (fi != 0)
67 {
68 fj = modf(fi / 10, &fi);
69 *--p1 = (int)((fj + .03) * 10) + '0';
70 r2++;
71 }
72 while (p1 < &buf[CVTBUFSIZE]) *p++ = *p1++;
73 }
74 else if (arg > 0)
75 {
76 while ((fj = arg * 10) < 1)
77 {
78 arg = fj;
79 r2--;
80 }
81 }
82 p1 = &buf[ndigits];
83 if (eflag == 0) p1 += r2;
84 *decpt = r2;
85 if (p1 < &buf[0])
86 {
87 buf[0] = '\0';
88 return buf;
89 }
90 while (p <= p1 && p < &buf[CVTBUFSIZE])
91 {
92 arg *= 10;
93 arg = modf(arg, &fj);
94 *p++ = (int) fj + '0';
95 }
96 if (p1 >= &buf[CVTBUFSIZE])
97 {
98 buf[CVTBUFSIZE - 1] = '\0';
99 return buf;
100 }
101 p = p1;
102 *p1 += 5;
103 while (*p1 > '9')
104 {
105 *p1 = '0';
106 if (p1 > buf)
107 ++*--p1;
108 else
109 {
110 *p1 = '1';
111 (*decpt)++;
112 if (eflag == 0)
113 {
114 if (p > buf) *p = '0';
115 p++;
116 }
117 }
118 }
119 *p = '\0';
120 return buf;
121 }
122
fcvtbuf(double arg,int ndigits,int * decpt,int * sign,char * buf)123 char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
124 {
125 return cvt(arg, ndigits, decpt, sign, buf, 0);
126 }
127