1 /* This file is part of Libspectre.
3 * Copyright (C) 2007, 2012 Albert Astals Cid <aacid@kde.org>
4 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
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)
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.
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.
21 /* This function comes from spectre-utils from libspectre */
30 #define ascii_isspace(c) \
31 (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
32 #define ascii_isdigit(c) \
33 (c >= '0' && c <= '9')
35 double gatof(const char *nptr
)
37 return gstrtod(nptr
, NULL
);
40 double gstrtod(const char *nptr
, char **endptr
)
44 struct lconv
*locale_data
;
45 const char *decimal_point
;
46 int decimal_point_len
;
47 const char *p
, *decimal_point_pos
;
48 const char *end
= NULL
; /* Silence gcc */
53 locale_data
= localeconv ();
54 decimal_point
= locale_data
->decimal_point
;
55 decimal_point_len
= strlen (decimal_point
);
57 decimal_point_pos
= NULL
;
60 if (decimal_point
[0] != '.' || decimal_point
[1] != 0) {
62 /* Skip leading space */
63 while (ascii_isspace (*p
))
66 /* Skip leading optional sign */
67 if (*p
== '+' || *p
== '-')
70 if (ascii_isdigit (*p
) || *p
== '.') {
71 while (ascii_isdigit (*p
))
75 decimal_point_pos
= p
++;
77 while (ascii_isdigit (*p
))
80 if (*p
== 'e' || *p
== 'E')
82 if (*p
== '+' || *p
== '-')
84 while (ascii_isdigit (*p
))
89 /* For the other cases, we need not convert the decimal point */
92 if (decimal_point_pos
) {
95 /* We need to convert the '.' to the locale specific decimal point */
96 copy
= (char *) malloc (end
- nptr
+ 1 + decimal_point_len
);
99 memcpy (c
, nptr
, decimal_point_pos
- nptr
);
100 c
+= decimal_point_pos
- nptr
;
101 memcpy (c
, decimal_point
, decimal_point_len
);
102 c
+= decimal_point_len
;
103 memcpy (c
, decimal_point_pos
+ 1, end
- (decimal_point_pos
+ 1));
104 c
+= end
- (decimal_point_pos
+ 1);
108 val
= strtod (copy
, &fail_pos
);
109 strtod_errno
= errno
;
112 if (fail_pos
- copy
> decimal_point_pos
- nptr
)
113 fail_pos
= (char *)nptr
+ (fail_pos
- copy
) - (decimal_point_len
- 1);
115 fail_pos
= (char *)nptr
+ (fail_pos
- copy
);
122 copy
= (char *) malloc (end
- (char *)nptr
+ 1);
123 memcpy (copy
, nptr
, end
- nptr
);
124 *(copy
+ (end
- (char *)nptr
)) = 0;
127 val
= strtod (copy
, &fail_pos
);
128 strtod_errno
= errno
;
131 fail_pos
= (char *)nptr
+ (fail_pos
- copy
);
137 val
= strtod (nptr
, &fail_pos
);
138 strtod_errno
= errno
;
144 errno
= strtod_errno
;