a patch for ttc fonts with bad index
[luatex.git] / source / libs / poppler / poppler-0.36.0 / goo / gstrtod.cc
blobcd1d5b554f787a91c8233efdb1598424bec31657
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>
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.
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.
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.
21 /* This function comes from spectre-utils from libspectre */
23 #include "gstrtod.h"
25 #include <locale.h>
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
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')
35 double gatof(const char *nptr)
37 return gstrtod(nptr, NULL);
40 double gstrtod(const char *nptr, char **endptr)
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 = NULL; /* Silence gcc */
49 int strtod_errno;
51 fail_pos = NULL;
53 locale_data = localeconv ();
54 decimal_point = locale_data->decimal_point;
55 decimal_point_len = strlen (decimal_point);
57 decimal_point_pos = NULL;
58 end = NULL;
60 if (decimal_point[0] != '.' || decimal_point[1] != 0) {
61 p = nptr;
62 /* Skip leading space */
63 while (ascii_isspace (*p))
64 p++;
66 /* Skip leading optional sign */
67 if (*p == '+' || *p == '-')
68 p++;
70 if (ascii_isdigit (*p) || *p == '.') {
71 while (ascii_isdigit (*p))
72 p++;
74 if (*p == '.')
75 decimal_point_pos = p++;
77 while (ascii_isdigit (*p))
78 p++;
80 if (*p == 'e' || *p == 'E')
81 p++;
82 if (*p == '+' || *p == '-')
83 p++;
84 while (ascii_isdigit (*p))
85 p++;
87 end = p;
89 /* For the other cases, we need not convert the decimal point */
92 if (decimal_point_pos) {
93 char *copy, *c;
95 /* We need to convert the '.' to the locale specific decimal point */
96 copy = (char *) malloc (end - nptr + 1 + decimal_point_len);
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;
107 errno = 0;
108 val = strtod (copy, &fail_pos);
109 strtod_errno = errno;
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);
118 free (copy);
119 } else if (end) {
120 char *copy;
122 copy = (char *) malloc (end - (char *)nptr + 1);
123 memcpy (copy, nptr, end - nptr);
124 *(copy + (end - (char *)nptr)) = 0;
126 errno = 0;
127 val = strtod (copy, &fail_pos);
128 strtod_errno = errno;
130 if (fail_pos) {
131 fail_pos = (char *)nptr + (fail_pos - copy);
134 free (copy);
135 } else {
136 errno = 0;
137 val = strtod (nptr, &fail_pos);
138 strtod_errno = errno;
141 if (endptr)
142 *endptr = fail_pos;
144 errno = strtod_errno;
146 return val;