source: trunk/poppler/mypoppler/goo/gstrtod.cc @ 461

Last change on this file since 461 was 461, checked in by Silvan Scherrer, 11 years ago

poppler update to 0.14.2

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1/* This file is part of Libspectre.
2 *
3 * Copyright (C) 2007 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#if !defined(OS2)
26#include <clocale>
27#include <cerrno>
28#include <cstdlib>
29#include <cstring>
30#else
31#include <locale.h>
32#include <errno.h>
33#include <stdlib.h>
34#include <string.h>
35#endif
36
37#define ascii_isspace(c) \
38  (c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v')
39#define ascii_isdigit(c) \
40  (c >= '0' && c <= '9')
41 
42double gatof(const char *nptr)
43{
44  return gstrtod(nptr, NULL);
45}
46
47double gstrtod(const char *nptr, char **endptr)
48{
49  char *fail_pos;
50  double val;
51  struct lconv *locale_data;
52  const char *decimal_point;
53  int decimal_point_len;
54  const char *p, *decimal_point_pos;
55  const char *end = NULL; /* Silence gcc */
56  int strtod_errno;
57
58  fail_pos = NULL;
59
60  locale_data = localeconv ();
61  decimal_point = locale_data->decimal_point;
62  decimal_point_len = strlen (decimal_point);
63
64  decimal_point_pos = NULL;
65  end = NULL;
66
67  if (decimal_point[0] != '.' || decimal_point[1] != 0) {
68    p = nptr;
69    /* Skip leading space */
70    while (ascii_isspace (*p))
71      p++;
72   
73    /* Skip leading optional sign */
74    if (*p == '+' || *p == '-')
75      p++;
76   
77    if (ascii_isdigit (*p) || *p == '.') {
78      while (ascii_isdigit (*p))
79        p++;
80     
81      if (*p == '.')
82        decimal_point_pos = p++;
83
84      while (ascii_isdigit (*p))
85        p++;
86
87      if (*p == 'e' || *p == 'E')
88        p++;
89      if (*p == '+' || *p == '-')
90        p++;
91      while (ascii_isdigit (*p))
92        p++;
93
94      end = p;
95    }
96    /* For the other cases, we need not convert the decimal point */
97  }
98
99  if (decimal_point_pos) {
100    char *copy, *c;
101   
102    /* We need to convert the '.' to the locale specific decimal point */
103    copy = (char *) malloc (end - nptr + 1 + decimal_point_len);
104   
105    c = copy;
106    memcpy (c, nptr, decimal_point_pos - nptr);
107    c += decimal_point_pos - nptr;
108    memcpy (c, decimal_point, decimal_point_len);
109    c += decimal_point_len;
110    memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
111    c += end - (decimal_point_pos + 1);
112    *c = 0;
113
114    errno = 0;
115    val = strtod (copy, &fail_pos);
116    strtod_errno = errno;
117
118    if (fail_pos) {
119      if (fail_pos - copy > decimal_point_pos - nptr)
120        fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
121      else
122        fail_pos = (char *)nptr + (fail_pos - copy);
123    }
124
125    free (copy);
126  } else if (end) {
127    char *copy;
128   
129    copy = (char *) malloc (end - (char *)nptr + 1);
130    memcpy (copy, nptr, end - nptr);
131    *(copy + (end - (char *)nptr)) = 0;
132   
133    errno = 0;
134    val = strtod (copy, &fail_pos);
135    strtod_errno = errno;
136
137    if (fail_pos) {
138      fail_pos = (char *)nptr + (fail_pos - copy);
139    }
140
141    free (copy);
142  } else {
143    errno = 0;
144    val = strtod (nptr, &fail_pos);
145    strtod_errno = errno;
146  }
147
148  if (endptr)
149    *endptr = fail_pos;
150
151  errno = strtod_errno;
152
153  return val;
154}
Note: See TracBrowser for help on using the repository browser.