1 | /* This file is part of Libspectre. |
2 | * |
3 | * Copyright (C) 2007, 2012 Albert Astals Cid <aacid@kde.org> |
4 | * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org> |
5 | * |
6 | * Libspectre is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2, or (at your option) |
9 | * any later version. |
10 | * |
11 | * Libspectre is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | /* This function comes from spectre-utils from libspectre */ |
22 | |
23 | #include "gstrtod.h" |
24 | |
25 | #include <clocale> |
26 | #include <cerrno> |
27 | #include <cstdlib> |
28 | #include <cstring> |
29 | |
30 | #define ascii_isspace(c) (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v') |
31 | #define ascii_isdigit(c) (c >= '0' && c <= '9') |
32 | |
33 | double gatof(const char *nptr) |
34 | { |
35 | return gstrtod(nptr, endptr: nullptr); |
36 | } |
37 | |
38 | double gstrtod(const char *nptr, char **endptr) |
39 | { |
40 | char *fail_pos; |
41 | double val; |
42 | struct lconv *locale_data; |
43 | const char *decimal_point; |
44 | int decimal_point_len; |
45 | const char *p, *decimal_point_pos; |
46 | const char *end = nullptr; /* Silence gcc */ |
47 | int strtod_errno; |
48 | |
49 | fail_pos = nullptr; |
50 | |
51 | locale_data = localeconv(); |
52 | decimal_point = locale_data->decimal_point; |
53 | decimal_point_len = strlen(s: decimal_point); |
54 | |
55 | decimal_point_pos = nullptr; |
56 | end = nullptr; |
57 | |
58 | if (decimal_point[0] != '.' || decimal_point[1] != 0) { |
59 | p = nptr; |
60 | /* Skip leading space */ |
61 | while (ascii_isspace(*p)) { |
62 | p++; |
63 | } |
64 | |
65 | /* Skip leading optional sign */ |
66 | if (*p == '+' || *p == '-') { |
67 | p++; |
68 | } |
69 | |
70 | if (ascii_isdigit(*p) || *p == '.') { |
71 | while (ascii_isdigit(*p)) { |
72 | p++; |
73 | } |
74 | |
75 | if (*p == '.') { |
76 | decimal_point_pos = p++; |
77 | } |
78 | |
79 | while (ascii_isdigit(*p)) { |
80 | p++; |
81 | } |
82 | |
83 | if (*p == 'e' || *p == 'E') { |
84 | p++; |
85 | } |
86 | if (*p == '+' || *p == '-') { |
87 | p++; |
88 | } |
89 | while (ascii_isdigit(*p)) { |
90 | p++; |
91 | } |
92 | |
93 | end = p; |
94 | } |
95 | /* For the other cases, we need not convert the decimal point */ |
96 | } |
97 | |
98 | if (decimal_point_pos) { |
99 | char *copy, *c; |
100 | |
101 | /* We need to convert the '.' to the locale specific decimal point */ |
102 | copy = (char *)malloc(size: end - nptr + 1 + decimal_point_len); |
103 | |
104 | c = copy; |
105 | memcpy(dest: c, src: nptr, n: decimal_point_pos - nptr); |
106 | c += decimal_point_pos - nptr; |
107 | memcpy(dest: c, src: decimal_point, n: decimal_point_len); |
108 | c += decimal_point_len; |
109 | memcpy(dest: c, src: decimal_point_pos + 1, n: end - (decimal_point_pos + 1)); |
110 | c += end - (decimal_point_pos + 1); |
111 | *c = 0; |
112 | |
113 | errno = 0; |
114 | val = strtod(nptr: copy, endptr: &fail_pos); |
115 | strtod_errno = errno; |
116 | |
117 | if (fail_pos) { |
118 | if (fail_pos - copy > decimal_point_pos - nptr) { |
119 | fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1); |
120 | } else { |
121 | fail_pos = (char *)nptr + (fail_pos - copy); |
122 | } |
123 | } |
124 | |
125 | free(ptr: copy); |
126 | } else if (end) { |
127 | char *copy; |
128 | |
129 | copy = (char *)malloc(size: end - (char *)nptr + 1); |
130 | memcpy(dest: copy, src: nptr, n: end - nptr); |
131 | *(copy + (end - (char *)nptr)) = 0; |
132 | |
133 | errno = 0; |
134 | val = strtod(nptr: copy, endptr: &fail_pos); |
135 | strtod_errno = errno; |
136 | |
137 | if (fail_pos) { |
138 | fail_pos = (char *)nptr + (fail_pos - copy); |
139 | } |
140 | |
141 | free(ptr: copy); |
142 | } else { |
143 | errno = 0; |
144 | val = strtod(nptr: nptr, endptr: &fail_pos); |
145 | strtod_errno = errno; |
146 | } |
147 | |
148 | if (endptr) { |
149 | *endptr = fail_pos; |
150 | } |
151 | |
152 | errno = strtod_errno; |
153 | |
154 | return val; |
155 | } |
156 | |