Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/c-strtod.c @ 16511:2074f2bf7216
math code: Add comments.
* lib/acosl.c: Add comment about related glibc source files.
* lib/asinl.c: Likewise.
* lib/atanl.c: Likewise.
* lib/expl.c: Likewise.
* lib/logl.c: Likewise.
* lib/sincosl.c: Likewise.
* lib/sinl.c: Likewise.
* lib/tanl.c: Likewise.
* lib/trigl.c: Likewise.
* lib/cosl.c: Likewise. Fix comments.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Wed, 29 Feb 2012 12:30:07 +0100 |
parents | 8250f2777afc |
children | dcca7ac14066 |
rev | line source |
---|---|
5114 | 1 /* Convert string to double, using the C locale. |
2 | |
16201
8250f2777afc
maint: update all copyright year number ranges
Jim Meyering <meyering@redhat.com>
parents:
14079
diff
changeset
|
3 Copyright (C) 2003-2004, 2006, 2009-2012 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> | |
11075
ab7203b2967c
Change c_strtod, c_strtold to no longer call xalloc_die().
Bruno Haible <bruno@clisp.org>
parents:
11074
diff
changeset
|
27 #include <string.h> |
5114 | 28 |
29 #if LONG | |
30 # define C_STRTOD c_strtold | |
31 # define DOUBLE long double | |
32 # define STRTOD_L strtold_l | |
33 #else | |
34 # define C_STRTOD c_strtod | |
35 # define DOUBLE double | |
36 # define STRTOD_L strtod_l | |
37 #endif | |
38 | |
5556
c2d95ab34ea3
(STRTOD): Depend on HAVE_C99_STRTOLD, not HAVE_DECL_STRTOLD.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5223
diff
changeset
|
39 /* 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
|
40 #if LONG && HAVE_C99_STRTOLD |
5114 | 41 # define STRTOD strtold |
42 #else | |
43 # define STRTOD strtod | |
44 #endif | |
45 | |
13877
5fb52cb372b0
c-strtold: Avoid link error on AIX 7.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
46 #if defined LC_ALL_MASK && (LONG ? HAVE_STRTOLD_L : HAVE_STRTOD_L) |
11072
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
47 |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
48 /* Cache for the C locale object. |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
49 Marked volatile so that different threads see the same value |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
50 (avoids locking). */ |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
51 static volatile locale_t c_locale_cache; |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
52 |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
53 /* Return the C locale object, or (locale_t) 0 with errno set |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
54 if it cannot be created. */ |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
55 static inline locale_t |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
56 c_locale (void) |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
57 { |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
58 if (!c_locale_cache) |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
59 c_locale_cache = newlocale (LC_ALL_MASK, "C", (locale_t) 0); |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
60 return c_locale_cache; |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
61 } |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
62 |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
63 #endif |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
64 |
5114 | 65 DOUBLE |
66 C_STRTOD (char const *nptr, char **endptr) | |
67 { | |
68 DOUBLE r; | |
69 | |
13877
5fb52cb372b0
c-strtold: Avoid link error on AIX 7.
Bruno Haible <bruno@clisp.org>
parents:
12559
diff
changeset
|
70 #if defined LC_ALL_MASK && (LONG ? HAVE_STRTOLD_L : HAVE_STRTOD_L) |
5114 | 71 |
11072
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
72 locale_t locale = c_locale (); |
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
73 if (!locale) |
11074
8cf858130820
c-strtod: when ENDPTR is non-NULL, set *ENDPTR in new failure path
Jim Meyering <meyering@redhat.com>
parents:
11072
diff
changeset
|
74 { |
8cf858130820
c-strtod: when ENDPTR is non-NULL, set *ENDPTR in new failure path
Jim Meyering <meyering@redhat.com>
parents:
11072
diff
changeset
|
75 if (endptr) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11082
diff
changeset
|
76 *endptr = (char *) nptr; |
11074
8cf858130820
c-strtod: when ENDPTR is non-NULL, set *ENDPTR in new failure path
Jim Meyering <meyering@redhat.com>
parents:
11072
diff
changeset
|
77 return 0; /* errno is set here */ |
8cf858130820
c-strtod: when ENDPTR is non-NULL, set *ENDPTR in new failure path
Jim Meyering <meyering@redhat.com>
parents:
11072
diff
changeset
|
78 } |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
79 |
11072
2017a2abcff4
Cache the C locale object.
Bruno Haible <bruno@clisp.org>
parents:
11070
diff
changeset
|
80 r = STRTOD_L (nptr, endptr, locale); |
5114 | 81 |
82 #else | |
83 | |
5223 | 84 char *saved_locale = setlocale (LC_NUMERIC, NULL); |
5114 | 85 |
86 if (saved_locale) | |
87 { | |
11075
ab7203b2967c
Change c_strtod, c_strtold to no longer call xalloc_die().
Bruno Haible <bruno@clisp.org>
parents:
11074
diff
changeset
|
88 saved_locale = strdup (saved_locale); |
ab7203b2967c
Change c_strtod, c_strtold to no longer call xalloc_die().
Bruno Haible <bruno@clisp.org>
parents:
11074
diff
changeset
|
89 if (saved_locale == NULL) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11082
diff
changeset
|
90 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11082
diff
changeset
|
91 if (endptr) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11082
diff
changeset
|
92 *endptr = (char *) nptr; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11082
diff
changeset
|
93 return 0; /* errno is set here */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11082
diff
changeset
|
94 } |
5223 | 95 setlocale (LC_NUMERIC, "C"); |
5114 | 96 } |
97 | |
98 r = STRTOD (nptr, endptr); | |
99 | |
100 if (saved_locale) | |
101 { | |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
102 int saved_errno = errno; |
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
103 |
5223 | 104 setlocale (LC_NUMERIC, saved_locale); |
5114 | 105 free (saved_locale); |
11070
42d47e0b9b24
Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
106 errno = saved_errno; |
5114 | 107 } |
108 | |
109 #endif | |
110 | |
111 return r; | |
112 } |