Author: lewing
Date: 2007-10-08 00:22:04 -0400 (Mon, 08 Oct 2007)
New Revision: 87071

Added:
   trunk/moon/test/xaml/test-path-group-torture.xaml
Modified:
   trunk/moon/src/ChangeLog
   trunk/moon/src/array.cpp
   trunk/moon/src/array.h
   trunk/moon/src/point.cpp
   trunk/moon/src/rect.cpp
   trunk/moon/src/xaml.cpp
   trunk/moon/test/harness/Makefile.am
Log:
2007-10-07  Larry Ewing  <[EMAIL PROTECTED]>

        * xaml.cpp:
        * rect.cpp:
        * point.cpp:
        * array.cpp: use g_utf8_next_char in some places to start making
        the parsers more utf8 safe.  Make rect/point_from_str more robust
        against separator characters


Modified: trunk/moon/src/ChangeLog
===================================================================
--- trunk/moon/src/ChangeLog    2007-10-07 23:32:57 UTC (rev 87070)
+++ trunk/moon/src/ChangeLog    2007-10-08 04:22:04 UTC (rev 87071)
@@ -1,3 +1,10 @@
+2007-10-07  Larry Ewing  <[EMAIL PROTECTED]>
+
+       * xaml.cpp:
+       * rect.cpp:
+       * point.cpp:
+       * array.cpp: use g_utf8_next_char in some places.
+
 2007-10-07  Chris Toshok  <[EMAIL PROTECTED]>
 
        * clock.cpp (Clock::Tick): make sure we're reversed before

Modified: trunk/moon/src/array.cpp
===================================================================
--- trunk/moon/src/array.cpp    2007-10-07 23:32:57 UTC (rev 87070)
+++ trunk/moon/src/array.cpp    2007-10-08 04:22:04 UTC (rev 87071)
@@ -27,67 +27,53 @@
        return p;
 }
 
-
-static bool
-is_separator (char c)
+GArray *double_garray_from_str (const char *s, gint max)
 {
-       switch (c) {
-       case ',':
-       case ' ':
-       case '\r':
-       case '\n':
-       case '\t':
-               return true;
-       default:
-               return false;
+       char *next = (char *)s;
+       GArray *values = g_array_sized_new (false, true, sizeof (double), max > 
0 ? max : 16);
+       double coord = 0.0;
+       guint end = max > 0 ? max : G_MAXINT;
+
+       while (next && values->len < end) {
+               while (g_ascii_isspace (*next) || *next == ',')
+                       next = g_utf8_next_char (next);
+
+               if (next) {
+                       errno = 0;
+                       char *prev = next;
+                       coord = g_ascii_strtod (prev, &next);
+                       if (errno != 0 || next == prev)
+                               goto error;
+
+                       g_array_append_val (values, coord);
+               }
        }
-}
 
+error:
+       while (values->len < (guint) max) {
+               coord = 0.0;
+               g_array_append_val (values, coord);
+       }
 
+       return values;
+}
+
 double *
 double_array_from_str (const char *s, int *count)
 {
-       int n = 16;
-       int pos = 0;
-       double *doubles = new double [n];
-       char *start = (char*)s;
-       char *end = NULL;
+       GArray *values = double_garray_from_str (s, 0);
 
-       errno = 0;
-       // note: we use g_ascii_strtod because in some locale, like french, 
-       // comma is used as the decimal point
-       double dv = g_ascii_strtod (start, &end);
-       if (errno != 0 || (start == end))
-               goto error;
-
-       doubles[pos++] = dv;
-       while (*end) {
-               if (is_separator (*end)) {
-                       end++;
-               } else {
-                       start = end;
-                       errno = 0;
-                       dv = g_ascii_strtod (start, &end);
-                       if (errno != 0 || (start == end))
-                               goto error;
-                       if (pos == n) {
-                               int new_size = n << 1; // double array size
-                               double *newdoubles = new double [new_size];
-                               memcpy (newdoubles, doubles, n * sizeof 
(double));
-                               delete[] doubles;
-                               doubles = newdoubles;
-                               n = new_size;
-                       }
-                       doubles[pos++] = dv;
-               }
+       if (values->len == 0) {
+               *count = 0;
+               return NULL;
        }
 
-       *count = pos;
+       double *doubles = new double [values->len];
+       memcpy (doubles, values->data, values->len * sizeof (double));
+       *count = values->len;
+       g_array_free (values, true);
+
        return doubles;
-error:
-       delete [] doubles;
-       *count = 0;
-       return NULL;
 }
 
 
@@ -106,25 +92,22 @@
 point_array_from_str (const char *s, int* count)
 {
        int i, j, n = 0;
-       double *doubles = double_array_from_str (s, &n);
-       if (!doubles) {
+       GArray *values = double_garray_from_str (s, 0);
+
+       n = values->len / 2;
+       if (n == 0 || n % 1 == 1) {
+               g_array_free (values, true);
                *count = 0;
                return NULL;
        }
-       // invalid if doubles are not in pair
-       if ((n & 1) == 1) {
-               delete [] doubles;
-               *count = 0;
-               return NULL;
-       }
 
-       n >>= 1; // 2 doubles for each point
        Point *points = new Point [n];
        for (i = 0, j = 0; j < n; j++) {
-               points[j].x = doubles [i++];
-               points[j].y = doubles [i++];
+               points[j].x = g_array_index (values, double, i++);
+               points[j].y = g_array_index (values, double, i++);
        }
-       delete [] doubles;
+
+       g_array_free (values, true);
        *count = n;
        return points;
 }

Modified: trunk/moon/src/array.h
===================================================================
--- trunk/moon/src/array.h      2007-10-07 23:32:57 UTC (rev 87070)
+++ trunk/moon/src/array.h      2007-10-08 04:22:04 UTC (rev 87071)
@@ -43,6 +43,7 @@
 Point* point_array_from_str (const char *s, int* count);
 
 double* double_array_from_str   (const char *s, int* count);
+GArray* double_garray_from_str   (const char *s, gint max);
 DoubleArray *double_array_new   (int count, double *values);
 
 G_END_DECLS

Modified: trunk/moon/src/point.cpp
===================================================================
--- trunk/moon/src/point.cpp    2007-10-07 23:32:57 UTC (rev 87070)
+++ trunk/moon/src/point.cpp    2007-10-08 04:22:04 UTC (rev 87071)
@@ -12,17 +12,17 @@
 
 #include <gtk/gtk.h>
 
+#include "array.h"
 #include "point.h"
 
 Point
 point_from_str (const char *s)
 {
-       // FIXME - not robust enough for production
-       char *next = NULL;
-       double x = g_ascii_strtod (s, &next);
-       double y = 0.0;
-       if (next)
-               y = g_ascii_strtod (++next, NULL);
-       return Point (x, y);
+       GArray *values = double_garray_from_str (s, 2);
+       Point p = Point (g_array_index (values, double, 0), g_array_index 
(values, double, 1));
+
+       g_array_free (values, true);
+
+       return p;
 }
 

Modified: trunk/moon/src/rect.cpp
===================================================================
--- trunk/moon/src/rect.cpp     2007-10-07 23:32:57 UTC (rev 87070)
+++ trunk/moon/src/rect.cpp     2007-10-08 04:22:04 UTC (rev 87071)
@@ -11,31 +11,21 @@
 #include <stdlib.h>
 
 #include <gtk/gtk.h>
-
+#include "array.h"
 #include "rect.h"
 
 Rect
 rect_from_str (const char *s)
 {
-       // FIXME - not robust enough for production
-       char *next = NULL;
-       double x = g_ascii_strtod (s, &next);
-       double y = 0.0;
-       if (next) {
-               ++next;
-               y = g_ascii_strtod (next, &next);
-       }
-       double w = 0.0;
-       if (next) {
-               ++next;
-               w = g_ascii_strtod (next, &next);
-       }
-       double h = 0.0;
-       if (next) {
-               ++next;
-               h = g_ascii_strtod (next, &next);
-       }
-       return Rect (x, y, w, h);
+       GArray *values = double_garray_from_str (s, 4);
+       Rect r = Rect (g_array_index (values, double, 0), 
+                      g_array_index (values, double, 1),
+                      g_array_index (values, double, 2),
+                      g_array_index (values, double, 3));
+       
+       g_array_free (values, true);
+
+       return r;
 }
 
 Rect

Modified: trunk/moon/src/xaml.cpp
===================================================================
--- trunk/moon/src/xaml.cpp     2007-10-07 23:32:57 UTC (rev 87070)
+++ trunk/moon/src/xaml.cpp     2007-10-08 04:22:04 UTC (rev 87071)
@@ -1398,7 +1398,7 @@
        char *inptr = *in;
        
        while (*inptr && !g_ascii_isalnum (*inptr) && *inptr != '.' && *inptr 
!= '-' && *inptr != '+')
-               inptr++;
+               inptr = g_utf8_next_char (inptr);
        
        *in = inptr;
 }
@@ -1522,9 +1522,10 @@
                
                bool relative = false;
                
-               inptr++;
+               char c = *inptr;
+               inptr = g_utf8_next_char (inptr);
                
-               switch (inptr[-1]) {
+               switch (c) {
                case 'f':
                case 'F':
                        if (*inptr == '0')
@@ -1532,7 +1533,7 @@
                        else if (*inptr == '1')
                                geometry_set_fill_rule (pg, FillRuleNonzero);
                        // FIXME: else it's a bad value and nothing should be 
rendered
-                       inptr++;
+                       inptr = g_utf8_next_char (inptr);
                        break;
 
                case 'm':

Modified: trunk/moon/test/harness/Makefile.am
===================================================================
--- trunk/moon/test/harness/Makefile.am 2007-10-07 23:32:57 UTC (rev 87070)
+++ trunk/moon/test/harness/Makefile.am 2007-10-08 04:22:04 UTC (rev 87071)
@@ -27,6 +27,7 @@
        ../xaml/test-path-group-empty.xaml \
        ../xaml/test-path-group-rectangle.xaml \
        ../xaml/test-path-group-stretch.xaml \
+       ../xaml/test-path-group-torture.xaml \
        ../xaml/test-path-group.xaml \
        ../xaml/test-path-line-stretch.xaml \
        ../xaml/test-path-line-stroke.xaml \

Added: trunk/moon/test/xaml/test-path-group-torture.xaml
===================================================================
--- trunk/moon/test/xaml/test-path-group-torture.xaml   2007-10-07 23:32:57 UTC 
(rev 87070)
+++ trunk/moon/test/xaml/test-path-group-torture.xaml   2007-10-08 04:22:04 UTC 
(rev 87071)
@@ -0,0 +1,37 @@
+<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation";>
+       <Path Fill="#FF0000" Stroke="Green" StrokeThickness="2" Canvas.Top="80">
+               <Path.Data>
+                       <GeometryGroup FillRule="EvenOdd">
+                               <RectangleGeometry Rect="20-40 200 200" />
+                               <LineGeometry StartPoint="10-20" EndPoint="110,
+-20" />
+                               <EllipseGeometry Center="50,
+
+-30" RadiusX="10" RadiusY="10" />
+                       </GeometryGroup>
+               </Path.Data>
+       </Path>
+       <Path Fill="#00FF00" Stroke="Blue" StrokeThickness="2" Canvas.Left="50">
+               <Path.Data>
+                       <GeometryGroup FillRule="EvenOdd">
+                               <RectangleGeometry Rect="
+   -30,                
+   40                  200
+ 200" />
+                               <LineGeometry StartPoint="-40 , 60" 
EndPoint="110,60" />
+                               <EllipseGeometry Center="0,     50" 
RadiusX="10" RadiusY="10" />
+                       </GeometryGroup>
+               </Path.Data>
+       </Path>
+       <Path Fill="#0000FF" Stroke="Red" StrokeThickness="2">
+               <Path.Data>
+                       <GeometryGroup FillRule="EvenOdd">
+                               <RectangleGeometry Rect="   20+40 200 200" />
+                               <LineGeometry StartPoint="
+10,
+60" EndPoint="110,             60" />
+                               <EllipseGeometry Center="50,    50" 
RadiusX="10" RadiusY="10" />
+                       </GeometryGroup>
+               </Path.Data>
+       </Path>
+</Canvas>

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to