Debian Codegraph @

libgm2/libm2pim/ldtoa.cc

raw
1 /* ldtoa.c convert long double to ascii and visa versa.
2
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27 #define GM2
28
29 #include <config.h>
30 #include <m2rts.h>
31
32 #define EXPORT(FUNC) m2pim ## _ldtoa_ ## FUNC
33 #define IMPORT(MODULE,FUNC) m2pim ## _ ## MODULE ## _ ## FUNC
34 #define M2EXPORT(FUNC) m2pim ## _M2_ldtoa_ ## FUNC
35 #define M2LIBNAME "m2pim"
36
37 #if defined(HAVE_STRINGS_H)
38 #include <strings.h>
39 #endif
40
41 #if defined(HAVE_STRING_H)
42 #include <string.h>
43 #endif
44
45 #if defined(HAVE_STDDEF_H)
46 /* Obtain a definition for NULL. */
47 #include <stddef.h>
48 #endif
49
50 #if defined(HAVE_STDIO_H)
51 /* Obtain a definition for NULL. */
52 #include <stdio.h>
53 #endif
54
55 #if defined(HAVE_TIME_H)
56 /* Obtain a definition for NULL. */
57 #include <time.h>
58 #endif
59
60 #if defined(HAVE_STRING_H)
61 /* Obtain a definition for NULL. */
62 #include <string.h>
63 #endif
64
65 #if defined(HAVE_WCHAR_H)
66 /* Obtain a definition for NULL. */
67 #include <wchar.h>
68 #endif
69
70 #if defined(HAVE_STDLIB_H)
71 #if !defined(_ISOC99_SOURCE)
72 #define _ISOC99_SOURCE
73 #endif
74 #include <stdlib.h>
75 #endif
76
77 #if defined(HAVE_ERRNO_H)
78 #include <errno.h>
79 #endif
80
81 #if defined(HAVE_SYS_ERRNO_H)
82 #include <sys/errno.h>
83 #endif
84
85 #if defined(HAVE_STDLIB_H)
86 /* Obtain a prototype for free and malloc. */
87 #include <stdlib.h>
88 #endif
89
90 #if !defined(NULL)
91 #define NULL (void *)0
92 #endif
93
94 #define MAX_FP_DIGITS 500
95
96 typedef enum Mode { maxsignicant, decimaldigits } Mode;
97
98 extern "C" int IMPORT(dtoa,calcmaxsig) (char *p, int ndigits);
99 extern "C" int IMPORT(dtoa,calcdecimal) (char *p, int str_size, int ndigits);
100 extern "C" int IMPORT(dtoa,calcsign) (char *p, int str_size);
101
102 /* maxsignicant return a string containing max(1,ndigits) significant
103 digits. The return string contains the string produced by snprintf.
104
105 decimaldigits: return a string produced by fcvt. The string will
106 contain ndigits past the decimal point (ndigits may be negative). */
107
108 extern "C" long double
109 EXPORT(strtold) (const char *s, bool *error)
110 {
111 char *endp;
112 long double d;
113
114 #if defined(HAVE_ERRNO_H)
115 errno = 0;
116 #endif
117 #if defined(HAVE_STRTOLD)
118 d = strtold (s, &endp);
119 #else
120 /* Fall back to using strtod. */
121 d = (long double)strtod (s, &endp);
122 #endif
123 if (endp != NULL && (*endp == '\0'))
124 #if defined(HAVE_ERRNO_H)
125 *error = (errno != 0);
126 #else
127 *error = false;
128 #endif
129 else
130 *error = true;
131 return d;
132 }
133
134 extern "C" char *
135 EXPORT(ldtoa) (long double d, int mode, int ndigits, int *decpt, bool *sign)
136 {
137 char format[50];
138 char *p;
139 int r;
140 switch (mode)
141 {
142
143 case maxsignicant:
144 ndigits += 20; /* Enough for exponent. */
145 p = (char *) malloc (ndigits);
146 snprintf (format, 50, "%s%d%s", "%.", ndigits - 20, "LE");
147 snprintf (p, ndigits, format, d);
148 *sign = IMPORT(dtoa,calcsign) (p, ndigits);
149 *decpt = IMPORT(dtoa,calcmaxsig) (p, ndigits);
150 return p;
151 case decimaldigits:
152 p = (char *) malloc (MAX_FP_DIGITS + 20);
153 snprintf (format, 50, "%s%d%s", "%.", MAX_FP_DIGITS, "LE");
154 snprintf (p, MAX_FP_DIGITS + 20, format, d);
155 *sign = IMPORT(dtoa,calcsign) (p, MAX_FP_DIGITS + 20);
156 *decpt = IMPORT(dtoa,calcdecimal) (p, MAX_FP_DIGITS + 20, ndigits);
157 return p;
158 default:
159 abort ();
160 }
161 }
162
163 #if defined(GM2)
164 /* GNU Modula-2 linking hooks. */
165
166 extern "C" void
167 M2EXPORT(init) (int, char **, char **)
168 {
169 }
170
171 extern "C" void
172 M2EXPORT(fini) (int, char **, char **)
173 {
174 }
175
176 extern "C" void
177 M2EXPORT(dep) (void)
178 {
179 }
180
181 extern "C" void __attribute__((__constructor__))
182 M2EXPORT(ctor) (void)
183 {
184 m2pim_M2RTS_RegisterModule ("ldtoa", M2LIBNAME,
185 M2EXPORT(init), M2EXPORT(fini),
186 M2EXPORT(dep));
187 }
188 #endif