Code

Filters. Custom predefined filters update and new ABC filters.
[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 #if 0
32 /*
33  * until 2004-04-22, g_ascii_strtod could not handle having a locale-based
34  * decimal separator immediately following the number ("5,4" would
35  * parse to "5,4" instead of "5.0" in fr_FR)
36  *
37  * This is the corrected function, lifted from 1.107 gstrfuncs.c in glib
38  */
39 extern "C" {
40 #include <glib.h>
41 #include <locale.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <stdlib.h>
46 gdouble
47 fixed_g_ascii_strtod (const gchar *nptr,
48                 gchar      **endptr)
49 {
50   gchar *fail_pos;
51   gdouble val;
52   struct lconv *locale_data;
53   const char *decimal_point;
54   int decimal_point_len;
55   const char *p, *decimal_point_pos;
56   const char *end = NULL; /* Silence gcc */
58   g_return_val_if_fail (nptr != NULL, 0);
60   fail_pos = NULL;
62   locale_data = localeconv ();
63   decimal_point = locale_data->decimal_point;
64   decimal_point_len = strlen (decimal_point);
66   g_assert (decimal_point_len != 0);
68   decimal_point_pos = NULL;
69   if (decimal_point[0] != '.' ||
70       decimal_point[1] != 0)
71     {
72       p = nptr;
73       /* Skip leading space */
74       while (g_ascii_isspace (*p))
75         p++;
77       /* Skip leading optional sign */
78       if (*p == '+' || *p == '-')
79         p++;
81       if (p[0] == '0' &&
82           (p[1] == 'x' || p[1] == 'X'))
83         {
84           p += 2;
85           /* HEX - find the (optional) decimal point */
86         
87           while (g_ascii_isxdigit (*p))
88             p++;
89         
90           if (*p == '.')
91             {
92               decimal_point_pos = p++;
93         
94               while (g_ascii_isxdigit (*p))
95                 p++;
96         
97               if (*p == 'p' || *p == 'P')
98                 p++;
99               if (*p == '+' || *p == '-')
100                 p++;
101               while (g_ascii_isdigit (*p))
102                 p++;
103             }
104         }
105       else
106         {
107           while (g_ascii_isdigit (*p))
108             p++;
109         
110           if (*p == '.')
111             {
112               decimal_point_pos = p++;
113         
114               while (g_ascii_isdigit (*p))
115                 p++;
116         
117               if (*p == 'e' || *p == 'E')
118                 p++;
119               if (*p == '+' || *p == '-')
120                 p++;
121               while (g_ascii_isdigit (*p))
122                 p++;
123             }
124         }
125       /* For the other cases, we need not convert the decimal point */
126       end = p;
127     }
129   /* Set errno to zero, so that we can distinguish zero results
130      and underflows */
131   errno = 0;
133   if (decimal_point_pos)
134     {
135       char *copy, *c;
137       /* We need to convert the '.' to the locale specific decimal point */
138       copy = (char*)g_malloc (end - nptr + 1 + decimal_point_len);
140       c = copy;
141       memcpy (c, nptr, decimal_point_pos - nptr);
142       c += decimal_point_pos - nptr;
143       memcpy (c, decimal_point, decimal_point_len);
144       c += decimal_point_len;
145       memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
146       c += end - (decimal_point_pos + 1);
147       *c = 0;
149       val = strtod (copy, &fail_pos);
151       if (fail_pos)
152         {
153           if (fail_pos - copy > decimal_point_pos - nptr)
154             fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
155           else
156             fail_pos = (char *)nptr + (fail_pos - copy);
157         }
159       g_free (copy);
160         
161     }
162   else if (decimal_point[0] != '.' ||
163            decimal_point[1] != 0)
164     {
165       char *copy;
167       copy = (char*)g_malloc (end - (char *)nptr + 1);
168       memcpy (copy, nptr, end - nptr);
169       *(copy + (end - (char *)nptr)) = 0;
171       val = strtod (copy, &fail_pos);
173       if (fail_pos)
174         {
175           fail_pos = (char *)nptr + (fail_pos - copy);
176         }
178       g_free (copy);
179     }
180   else
181     {
182       val = strtod (nptr, &fail_pos);
183     }
185   if (endptr)
186     *endptr = fail_pos;
188   return val;
192 #endif /* BROKEN_G_ASCII_STRTOD */