"Fossies" - the Fresh Open Source Software Archive

Member "poppler-0.82.0/goo/gstrtod.cc" (25 Oct 2019, 3735 Bytes) of package /linux/misc/poppler-0.82.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gstrtod.cc" see the Fossies "Dox" file reference documentation.

    1 /* This file is part of Libspectre.
    2  * 
    3  * Copyright (C) 2007, 2012 Albert Astals Cid <aacid@kde.org>
    4  * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
    5  *
    6  * Libspectre 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)
    9  * any later version.
   10  *
   11  * Libspectre 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
   17  * along with this program; if not, write to the Free Software
   18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   19  */
   20 
   21 /* This function comes from spectre-utils from libspectre */
   22 
   23 #include "gstrtod.h"
   24 
   25 #include <locale.h>
   26 #include <errno.h>
   27 #include <stdlib.h>
   28 #include <string.h>
   29 
   30 #define ascii_isspace(c) \
   31   (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
   32 #define ascii_isdigit(c) \
   33   (c >= '0' && c <= '9')
   34   
   35 double gatof(const char *nptr)
   36 {
   37   return gstrtod(nptr, nullptr);
   38 }
   39 
   40 double gstrtod(const char *nptr, char **endptr)
   41 {
   42   char *fail_pos;
   43   double val;
   44   struct lconv *locale_data;
   45   const char *decimal_point;
   46   int decimal_point_len;
   47   const char *p, *decimal_point_pos;
   48   const char *end = nullptr; /* Silence gcc */
   49   int strtod_errno;
   50 
   51   fail_pos = nullptr;
   52 
   53   locale_data = localeconv ();
   54   decimal_point = locale_data->decimal_point;
   55   decimal_point_len = strlen (decimal_point);
   56 
   57   decimal_point_pos = nullptr;
   58   end = nullptr;
   59 
   60   if (decimal_point[0] != '.' || decimal_point[1] != 0) {
   61     p = nptr;
   62     /* Skip leading space */
   63     while (ascii_isspace (*p))
   64       p++;
   65     
   66     /* Skip leading optional sign */
   67     if (*p == '+' || *p == '-')
   68       p++;
   69     
   70     if (ascii_isdigit (*p) || *p == '.') {
   71       while (ascii_isdigit (*p))
   72         p++;
   73       
   74       if (*p == '.')
   75         decimal_point_pos = p++;
   76 
   77       while (ascii_isdigit (*p))
   78         p++;
   79 
   80       if (*p == 'e' || *p == 'E')
   81         p++;
   82       if (*p == '+' || *p == '-')
   83         p++;
   84       while (ascii_isdigit (*p))
   85         p++;
   86 
   87       end = p;
   88     }
   89     /* For the other cases, we need not convert the decimal point */
   90   }
   91 
   92   if (decimal_point_pos) {
   93     char *copy, *c;
   94     
   95     /* We need to convert the '.' to the locale specific decimal point */
   96     copy = (char *) malloc (end - nptr + 1 + decimal_point_len);
   97     
   98     c = copy;
   99     memcpy (c, nptr, decimal_point_pos - nptr);
  100     c += decimal_point_pos - nptr;
  101     memcpy (c, decimal_point, decimal_point_len);
  102     c += decimal_point_len;
  103     memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
  104     c += end - (decimal_point_pos + 1);
  105     *c = 0;
  106 
  107     errno = 0;
  108     val = strtod (copy, &fail_pos);
  109     strtod_errno = errno;
  110 
  111     if (fail_pos) {
  112       if (fail_pos - copy > decimal_point_pos - nptr)
  113         fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
  114       else
  115         fail_pos = (char *)nptr + (fail_pos - copy);
  116     }
  117 
  118     free (copy);
  119   } else if (end) {
  120     char *copy;
  121     
  122     copy = (char *) malloc (end - (char *)nptr + 1);
  123     memcpy (copy, nptr, end - nptr);
  124     *(copy + (end - (char *)nptr)) = 0;
  125     
  126     errno = 0;
  127     val = strtod (copy, &fail_pos);
  128     strtod_errno = errno;
  129 
  130     if (fail_pos) {
  131       fail_pos = (char *)nptr + (fail_pos - copy);
  132     }
  133 
  134     free (copy);
  135   } else {
  136     errno = 0;
  137     val = strtod (nptr, &fail_pos);
  138     strtod_errno = errno;
  139   }
  140 
  141   if (endptr)
  142     *endptr = fail_pos;
  143 
  144   errno = strtod_errno;
  145 
  146   return val;
  147 }