Code

handle the case of no filter primitives
[inkscape.git] / src / fixes.cpp
1 /*
2  *
3  * This is the header file to include to fix any broken definitions or funcs
4  *
5  * $Id$
6  *
7  * 2004 Kees Cook
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #if defined(g_ascii_strtod)
31 /*
32  * until 2004-04-22, g_ascii_strtod could not handle having a locale-based
33  * decimal separator immediately following the number ("5,4" would
34  * parse to "5,4" instead of "5.0" in fr_FR)
35  *
36  * This is the corrected function, lifted from 1.107 gstrfuncs.c in glib
37  */
38 extern "C" {
39 #include <glib.h>
40 #include <locale.h>
41 #include <string.h>
42 #include <errno.h>
43 #include <stdlib.h>
45 gdouble
46 fixed_g_ascii_strtod (const gchar *nptr,
47                 gchar      **endptr)
48 {
49   gchar *fail_pos;
50   gdouble 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 */
57   g_return_val_if_fail (nptr != NULL, 0);
59   fail_pos = NULL;
61   locale_data = localeconv ();
62   decimal_point = locale_data->decimal_point;
63   decimal_point_len = strlen (decimal_point);
65   g_assert (decimal_point_len != 0);
67   decimal_point_pos = NULL;
68   if (decimal_point[0] != '.' ||
69       decimal_point[1] != 0)
70     {
71       p = nptr;
72       /* Skip leading space */
73       while (g_ascii_isspace (*p))
74         p++;
76       /* Skip leading optional sign */
77       if (*p == '+' || *p == '-')
78         p++;
80       if (p[0] == '0' &&
81           (p[1] == 'x' || p[1] == 'X'))
82         {
83           p += 2;
84           /* HEX - find the (optional) decimal point */
85         
86           while (g_ascii_isxdigit (*p))
87             p++;
88         
89           if (*p == '.')
90             {
91               decimal_point_pos = p++;
92         
93               while (g_ascii_isxdigit (*p))
94                 p++;
95         
96               if (*p == 'p' || *p == 'P')
97                 p++;
98               if (*p == '+' || *p == '-')
99                 p++;
100               while (g_ascii_isdigit (*p))
101                 p++;
102             }
103         }
104       else
105         {
106           while (g_ascii_isdigit (*p))
107             p++;
108         
109           if (*p == '.')
110             {
111               decimal_point_pos = p++;
112         
113               while (g_ascii_isdigit (*p))
114                 p++;
115         
116               if (*p == 'e' || *p == 'E')
117                 p++;
118               if (*p == '+' || *p == '-')
119                 p++;
120               while (g_ascii_isdigit (*p))
121                 p++;
122             }
123         }
124       /* For the other cases, we need not convert the decimal point */
125       end = p;
126     }
128   /* Set errno to zero, so that we can distinguish zero results
129      and underflows */
130   errno = 0;
132   if (decimal_point_pos)
133     {
134       char *copy, *c;
136       /* We need to convert the '.' to the locale specific decimal point */
137       copy = (char*)g_malloc (end - nptr + 1 + decimal_point_len);
139       c = copy;
140       memcpy (c, nptr, decimal_point_pos - nptr);
141       c += decimal_point_pos - nptr;
142       memcpy (c, decimal_point, decimal_point_len);
143       c += decimal_point_len;
144       memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
145       c += end - (decimal_point_pos + 1);
146       *c = 0;
148       val = strtod (copy, &fail_pos);
150       if (fail_pos)
151         {
152           if (fail_pos - copy > decimal_point_pos - nptr)
153             fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
154           else
155             fail_pos = (char *)nptr + (fail_pos - copy);
156         }
158       g_free (copy);
159         
160     }
161   else if (decimal_point[0] != '.' ||
162            decimal_point[1] != 0)
163     {
164       char *copy;
166       copy = (char*)g_malloc (end - (char *)nptr + 1);
167       memcpy (copy, nptr, end - nptr);
168       *(copy + (end - (char *)nptr)) = 0;
170       val = strtod (copy, &fail_pos);
172       if (fail_pos)
173         {
174           fail_pos = (char *)nptr + (fail_pos - copy);
175         }
177       g_free (copy);
178     }
179   else
180     {
181       val = strtod (nptr, &fail_pos);
182     }
184   if (endptr)
185     *endptr = fail_pos;
187   return val;
191 #endif /* BROKEN_G_ASCII_STRTOD */