4 // Convert string to double
6 // Copyright (C) 2002 Michael Ringgaard. All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. Neither the name of the project nor the names of its contributors
18 // may be used to endorse or promote products derived from this software
19 // without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
22 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
25 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 // Tweaked version for Viking project to read in values containing ',' or '.' as the decimal separator
35 // Modified functions to prevent clashing with the system ones
43 double strtod_i8n(const char *str, char **endptr) {
47 char *p = (char *) str;
53 // Skip leading whitespace
54 while (isspace(*p)) p++;
56 // Handle optional sign
59 case '-': negative = 1; // Fall through to increment position
68 // Process string of digits
70 number = number * 10. + (*p - '0');
75 // Process decimal part
76 if (*p == '.' || *p == ',') {
80 number = number * 10. + (*p - '0');
86 exponent -= num_decimals;
89 if (num_digits == 0) {
95 if (negative) number = -number;
97 // Process an exponent string
98 if (*p == 'e' || *p == 'E') {
99 // Handle optional sign
102 case '-': negative = 1; // Fall through to increment pos
106 // Process string of digits
108 while (isdigit(*p)) {
109 n = n * 10 + (*p - '0');
120 if (exponent < DBL_MIN_EXP || exponent > DBL_MAX_EXP) {
141 if (number == HUGE_VAL) errno = ERANGE;
142 if (endptr) *endptr = p;
147 float strtof_i8n(const char *str, char **endptr) {
148 return (float) strtod_i8n(str, endptr);
152 long double strtold_i8n(const char *str, char **endptr) {
153 return strtod_i8n(str, endptr);
156 double atof_i8n(const char *str) {
157 return strtod_i8n(str, NULL);