Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/c-strtod.c @ 11070:42d47e0b9b24
Improve error handling of c_strtod.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Wed, 21 Jan 2009 23:35:43 +0100 |
parents | bbbbbf4cd1c5 |
children | 2017a2abcff4 |
rev | line source |
---|---|
5114 | 1 /* Convert string to double, using the C locale. |
2 | |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
3 Copyright (C) 2003, 2004, 2006, 2009 Free Software Foundation, Inc. |
5114 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
5114 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
8 (at your option) any later version. |
5114 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5114 | 17 |
18 /* Written by Paul Eggert. */ | |
19 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
20 #include <config.h> |
5114 | 21 |
22 #include "c-strtod.h" | |
23 | |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
24 #include <errno.h> |
5114 | 25 #include <locale.h> |
26 #include <stdlib.h> | |
27 | |
28 #include "xalloc.h" | |
29 | |
30 #if LONG | |
31 # define C_STRTOD c_strtold | |
32 # define DOUBLE long double | |
33 # define STRTOD_L strtold_l | |
34 #else | |
35 # define C_STRTOD c_strtod | |
36 # define DOUBLE double | |
37 # define STRTOD_L strtod_l | |
38 #endif | |
39 | |
5556
c2d95ab34ea3
(STRTOD): Depend on HAVE_C99_STRTOLD, not HAVE_DECL_STRTOLD.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5223
diff
changeset
|
40 /* c_strtold falls back on strtod if strtold doesn't conform to C99. */ |
c2d95ab34ea3
(STRTOD): Depend on HAVE_C99_STRTOLD, not HAVE_DECL_STRTOLD.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5223
diff
changeset
|
41 #if LONG && HAVE_C99_STRTOLD |
5114 | 42 # define STRTOD strtold |
43 #else | |
44 # define STRTOD strtod | |
45 #endif | |
46 | |
47 DOUBLE | |
48 C_STRTOD (char const *nptr, char **endptr) | |
49 { | |
50 DOUBLE r; | |
51 | |
52 #ifdef LC_ALL_MASK | |
53 | |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
54 locale_t c_locale; |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
55 int saved_errno; |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
56 |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
57 c_locale = newlocale (LC_ALL_MASK, "C", (locale_t) 0); |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
58 if (!c_locale) |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
59 return 0; /* errno is set here */ |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
60 |
5114 | 61 r = STRTOD_L (nptr, endptr, c_locale); |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
62 |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
63 saved_errno = errno; |
5114 | 64 freelocale (c_locale); |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
65 errno = saved_errno; |
5114 | 66 |
67 #else | |
68 | |
5223 | 69 char *saved_locale = setlocale (LC_NUMERIC, NULL); |
5114 | 70 |
71 if (saved_locale) | |
72 { | |
73 saved_locale = xstrdup (saved_locale); | |
5223 | 74 setlocale (LC_NUMERIC, "C"); |
5114 | 75 } |
76 | |
77 r = STRTOD (nptr, endptr); | |
78 | |
79 if (saved_locale) | |
80 { | |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
81 int saved_errno = errno; |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
82 |
5223 | 83 setlocale (LC_NUMERIC, saved_locale); |
5114 | 84 free (saved_locale); |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
85 errno = saved_errno; |
5114 | 86 } |
87 | |
88 #endif | |
89 | |
90 return r; | |
91 } |