Mercurial > hg > octave-shane > gnulib-hg
annotate lib/xstrtod.c @ 10375:f96e845fc36d
Avoid some "gcc -pedantic" warnings.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Sat, 30 Aug 2008 03:09:15 +0200 |
parents | bbbbbf4cd1c5 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
4910 | 1 /* error-checking interface to strtod-like functions |
5159 | 2 |
6912 | 3 Copyright (C) 1996, 1999, 2000, 2003, 2004, 2005, 2006 Free |
4 Software Foundation, Inc. | |
569 | 5 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
6 This program is free software: you can redistribute it and/or modify |
569 | 7 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
|
8 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
|
9 (at your option) any later version. |
569 | 10 |
11 This program 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 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
569 | 18 |
19 /* Written by Jim Meyering. */ | |
20 | |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6912
diff
changeset
|
21 #include <config.h> |
513 | 22 |
4691 | 23 #include "xstrtod.h" |
513 | 24 |
25 #include <errno.h> | |
4691 | 26 #include <limits.h> |
513 | 27 #include <stdio.h> |
28 | |
6912 | 29 #if LONG |
30 # define XSTRTOD xstrtold | |
31 # define DOUBLE long double | |
32 #else | |
33 # define XSTRTOD xstrtod | |
34 # define DOUBLE double | |
35 #endif | |
36 | |
37 /* An interface to a string-to-floating-point conversion function that | |
38 encapsulates all the error checking one should usually perform. | |
39 Like strtod/strtold, but upon successful | |
5159 | 40 conversion put the result in *RESULT and return true. Return |
41 false and don't modify *RESULT upon any failure. CONVERT | |
4910 | 42 specifies the conversion function, e.g., strtod itself. */ |
568 | 43 |
5159 | 44 bool |
6912 | 45 XSTRTOD (char const *str, char const **ptr, DOUBLE *result, |
46 DOUBLE (*convert) (char const *, char **)) | |
513 | 47 { |
6912 | 48 DOUBLE val; |
513 | 49 char *terminator; |
5159 | 50 bool ok = true; |
513 | 51 |
52 errno = 0; | |
4910 | 53 val = convert (str, &terminator); |
513 | 54 |
55 /* Having a non-zero terminator is an error only when PTR is NULL. */ | |
56 if (terminator == str || (ptr == NULL && *terminator != '\0')) | |
5159 | 57 ok = false; |
513 | 58 else |
59 { | |
6912 | 60 /* Allow underflow (in which case CONVERT returns zero), |
513 | 61 but flag overflow as an error. */ |
6912 | 62 if (val != 0 && errno == ERANGE) |
5159 | 63 ok = false; |
513 | 64 } |
65 | |
66 if (ptr != NULL) | |
67 *ptr = terminator; | |
68 | |
69 *result = val; | |
5159 | 70 return ok; |
513 | 71 } |