ReactOS 0.4.16-dev-847-g386fccd
fcvtbuf.c
Go to the documentation of this file.
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
45static 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 (_isnan(arg))
52 {
53 snprintf(buf, ndigits, "1.#QNAN");
54 *decpt = 0;
55 *sign = 0;
56 return buf;
57 }
58 if (!_finite(arg))
59 {
60 snprintf(buf, ndigits, "1.#INF");
61 *decpt = 0;
62 *sign = 0;
63 return buf;
64 }
65
66 if (ndigits >= CVTBUFSIZE - 1) ndigits = CVTBUFSIZE - 2;
67 r2 = 0;
68 *sign = 0;
69 p = &buf[0];
70 if (arg < 0)
71 {
72 *sign = 1;
73 arg = -arg;
74 }
75 arg = modf(arg, &fi);
76 p1 = &buf[CVTBUFSIZE];
77
78 if (fi != 0)
79 {
80 p1 = &buf[CVTBUFSIZE];
81 while (fi != 0)
82 {
83 fj = modf(fi / 10, &fi);
84 *--p1 = (int)((fj + .03) * 10) + '0';
85 r2++;
86 }
87 while (p1 < &buf[CVTBUFSIZE]) *p++ = *p1++;
88 }
89 else if (arg > 0)
90 {
91 while ((fj = arg * 10) < 1)
92 {
93 arg = fj;
94 r2--;
95 }
96 }
97 p1 = &buf[ndigits];
98 if (eflag == 0) p1 += r2;
99 *decpt = r2;
100 if (p1 < &buf[0])
101 {
102 buf[0] = '\0';
103 return buf;
104 }
105 while (p <= p1 && p < &buf[CVTBUFSIZE])
106 {
107 arg *= 10;
108 arg = modf(arg, &fj);
109 *p++ = (int) fj + '0';
110 }
111 if (p1 >= &buf[CVTBUFSIZE])
112 {
113 buf[CVTBUFSIZE - 1] = '\0';
114 return buf;
115 }
116 p = p1;
117 *p1 += 5;
118 while (*p1 > '9')
119 {
120 *p1 = '0';
121 if (p1 > buf)
122 ++*--p1;
123 else
124 {
125 *p1 = '1';
126 (*decpt)++;
127 if (eflag == 0)
128 {
129 if (p > buf) *p = '0';
130 p++;
131 }
132 }
133 }
134 *p = '\0';
135 return buf;
136}
137
138char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
139{
140 return cvt(arg, ndigits, decpt, sign, buf, 0);
141}
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
static char * cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag)
Definition: fcvtbuf.c:45
char * fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
Definition: fcvtbuf.c:138
#define CVTBUFSIZE
Definition: fcvtbuf.c:44
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLfloat GLfloat p
Definition: glext.h:8902
_Check_return_ __MINGW_NOTHROW _CRTIMP int __cdecl _isnan(_In_ double)
_Check_return_ __MINGW_NOTHROW _CRTIMP int __cdecl _finite(_In_ double)
_Check_return_ _CRTIMP double __cdecl modf(_In_ double x, _Out_ double *y)
#define sign(x)
Definition: mapdesc.cc:613
static DNS_RECORDW r2
Definition: record.c:38
static size_t double int ndigits
Definition: printf.c:52
static size_t double int int * decpt
Definition: printf.c:52
void * arg
Definition: msvc.h:10
#define snprintf
Definition: wintirpc.h:48