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

Last change on this file since 290 was 290, checked in by rbri, 11 years ago

PDF plugin: Poppler library updated to version 0.12.4

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