summaryrefslogtreecommitdiff
path: root/Build/source/libs/poppler/poppler-0.20.2/goo/gstrtod.cc
blob: e6c3a00741f69d643f30ffbddbb84059d9292b41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* This file is part of Libspectre.
 * 
 * Copyright (C) 2007 Albert Astals Cid <aacid@kde.org>
 * Copyright (C) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
 *
 * Libspectre 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; either version 2, or (at your option)
 * any later version.
 *
 * Libspectre 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/* This function comes from spectre-utils from libspectre */

#include "gstrtod.h"

#include <clocale>
#include <cerrno>
#include <cstdlib>
#include <cstring>

#define ascii_isspace(c) \
  (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
#define ascii_isdigit(c) \
  (c >= '0' && c <= '9')
  
double gatof(const char *nptr)
{
  return gstrtod(nptr, NULL);
}

double gstrtod(const char *nptr, char **endptr)
{
  char *fail_pos;
  double val;
  struct lconv *locale_data;
  const char *decimal_point;
  int decimal_point_len;
  const char *p, *decimal_point_pos;
  const char *end = NULL; /* Silence gcc */
  int strtod_errno;

  fail_pos = NULL;

  locale_data = localeconv ();
  decimal_point = locale_data->decimal_point;
  decimal_point_len = strlen (decimal_point);

  decimal_point_pos = NULL;
  end = NULL;

  if (decimal_point[0] != '.' || decimal_point[1] != 0) {
    p = nptr;
    /* Skip leading space */
    while (ascii_isspace (*p))
      p++;
    
    /* Skip leading optional sign */
    if (*p == '+' || *p == '-')
      p++;
    
    if (ascii_isdigit (*p) || *p == '.') {
      while (ascii_isdigit (*p))
        p++;
      
      if (*p == '.')
        decimal_point_pos = p++;

      while (ascii_isdigit (*p))
        p++;

      if (*p == 'e' || *p == 'E')
        p++;
      if (*p == '+' || *p == '-')
        p++;
      while (ascii_isdigit (*p))
        p++;

      end = p;
    }
    /* For the other cases, we need not convert the decimal point */
  }

  if (decimal_point_pos) {
    char *copy, *c;
    
    /* We need to convert the '.' to the locale specific decimal point */
    copy = (char *) malloc (end - nptr + 1 + decimal_point_len);
    
    c = copy;
    memcpy (c, nptr, decimal_point_pos - nptr);
    c += decimal_point_pos - nptr;
    memcpy (c, decimal_point, decimal_point_len);
    c += decimal_point_len;
    memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
    c += end - (decimal_point_pos + 1);
    *c = 0;

    errno = 0;
    val = strtod (copy, &fail_pos);
    strtod_errno = errno;

    if (fail_pos) {
      if (fail_pos - copy > decimal_point_pos - nptr)
        fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
      else
        fail_pos = (char *)nptr + (fail_pos - copy);
    }

    free (copy);
  } else if (end) {
    char *copy;
    
    copy = (char *) malloc (end - (char *)nptr + 1);
    memcpy (copy, nptr, end - nptr);
    *(copy + (end - (char *)nptr)) = 0;
    
    errno = 0;
    val = strtod (copy, &fail_pos);
    strtod_errno = errno;

    if (fail_pos) {
      fail_pos = (char *)nptr + (fail_pos - copy);
    }

    free (copy);
  } else {
    errno = 0;
    val = strtod (nptr, &fail_pos);
    strtod_errno = errno;
  }

  if (endptr)
    *endptr = fail_pos;

  errno = strtod_errno;

  return val;
}