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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
1 /* Convert string to double, using the C locale.
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13 GNU General Public License for more details.
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
17
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18 /* Written by Paul Eggert. */
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
22 #include "c-strtod.h"
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
23
11070
42d47e0b9b24 Improve error handling of c_strtod.
Bruno Haible <bruno@clisp.org>
parents: 9309
diff changeset
24 #include <errno.h>
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
25 #include <locale.h>
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
28
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
29 #if LONG
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
30 # define C_STRTOD c_strtold
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
31 # define DOUBLE long double
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
32 # define STRTOD_L strtold_l
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
33 #else
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
34 # define C_STRTOD c_strtod
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
35 # define DOUBLE double
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
36 # define STRTOD_L strtod_l
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
37 #endif
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
41 # define STRTOD strtold
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
42 #else
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
43 # define STRTOD strtod
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
44 #endif
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
65 DOUBLE
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
66 C_STRTOD (char const *nptr, char **endptr)
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
67 {
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
68 DOUBLE r;
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
81
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
82 #else
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
83
5223
eceaa40c3485 Undo previous change.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5221
diff changeset
84 char *saved_locale = setlocale (LC_NUMERIC, NULL);
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
85
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
86 if (saved_locale)
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
eceaa40c3485 Undo previous change.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5221
diff changeset
95 setlocale (LC_NUMERIC, "C");
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
96 }
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
97
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
98 r = STRTOD (nptr, endptr);
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
99
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
100 if (saved_locale)
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
eceaa40c3485 Undo previous change.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5221
diff changeset
104 setlocale (LC_NUMERIC, saved_locale);
5114
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
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
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
107 }
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
108
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
109 #endif
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
110
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
111 return r;
e0eddd7ceb40 Imported from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
112 }