1 /* error-checking interface to strtod-like functions
3 Copyright (C) 1996, 1999, 2000, 2003, 2004, 2005 Free Software
6 This program 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 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.
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 Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* Written by Jim Meyering. */
32 /* An interface to strtod that encapsulates all the error checking
33 one should usually perform. Like strtod, but upon successful
34 conversion put the result in *RESULT and return true. Return
35 false and don't modify *RESULT upon any failure. CONVERT
36 specifies the conversion function, e.g., strtod itself. */
39 xstrtod (char const *str, char const **ptr, double *result,
40 double (*convert) (char const *, char **))
47 val = convert (str, &terminator);
49 /* Having a non-zero terminator is an error only when PTR is NULL. */
50 if (terminator == str || (ptr == NULL && *terminator != '\0'))
54 /* Allow underflow (in which case strtod returns zero),
55 but flag overflow as an error. */
56 if (val != 0.0 && errno == ERANGE)