BlinkenArea - GitList
Repositories
Blog
Wiki
libetherpix
Code
Commits
Branches
Tags
Search
Tree:
4ef7955
Branches
Tags
master
libetherpix
src
strtod_noloc.c
update copyright year
Stefan Schuermans
commited
4ef7955
at 2017-05-20 16:55:59
strtod_noloc.c
Blame
History
Raw
/* * EtherPix library * * Copyright 2010-2017 Stefan Schuermans <stefan schuermans info> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3 of the License. * * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <math.h> #include <intern/strtod_noloc.h> /** * \brief own version of strtod ignoring locale * * This version of strtod always expects dots in floating point numbers. * The reason is to be able to use the same config files for all locales. * * \param[in] nptr string containing number to parse * \param[out] endptr filled with pointer to * \return value read from the string */ double etp_strtod_noloc(const char *nptr, char **endptr) { double sign = 1.0, value = 0.0, weight = 0.1; int exp_sign = 1, exp_value = 0; /* skip whitespace */ while (*nptr == ' ' || *nptr == '\t') ++nptr; /* read optional sign */ if (*nptr == '+') { sign = 1.0; ++nptr; } else if (*nptr == '-') { sign = -1.0; ++nptr; } /* read digits before dot */ while (*nptr >= '0' && *nptr <= '9') { value = value * 10.0 + (double)(*nptr - '0'); ++nptr; } /* read dot and digits after it */ if (*nptr == '.') { ++nptr; /* read digits after dot */ while (*nptr >= '0' && *nptr <= '9') { value += weight * (double)(*nptr - '0'); weight *= 0.1; ++nptr; } } /* if (*nptr == '.') */ /* merge sign into value */ value *= sign; /* read exponent */ if (*nptr == 'E' || *nptr == 'e') { ++nptr; /* read optional sign */ if (*nptr == '+') { exp_sign = 1; ++nptr; } else if (*nptr == '-') { exp_sign = -1; ++nptr; } /* read digits */ while (*nptr >= '0' && *nptr <= '9') { exp_value = exp_value * 10 + (int)(*nptr - '0'); ++nptr; } /* merge exponent into value */ value *= pow(10.0, exp_sign * exp_value); } /* if (*nptr == 'E' || *nptr == 'e') */ /* return pointer behind end */ if (endptr) *endptr = (char *)nptr; /* return value */ return value; }