[monet/monet-xml] Require "width" and "height" keywords in expressions



commit b430893b14704275ed8ed7b445d5507bbbdc935c
Author: Thomas Wood <thos gnome org>
Date:   Thu Jul 8 00:19:22 2010 +0100

    Require "width" and "height" keywords in expressions
    
    Use "width" and "height" rather than simply "w" and "h" to improve
    legibility. Also check strtod successfully parsed a number and bail out if
    no number was found.

 monet-gtk/monet.xml |   15 +++++++++------
 monet/mn-style.c    |   23 +++++++++++++++++++----
 2 files changed, 28 insertions(+), 10 deletions(-)
---
diff --git a/monet-gtk/monet.xml b/monet-gtk/monet.xml
index e79b060..d43c535 100644
--- a/monet-gtk/monet.xml
+++ b/monet-gtk/monet.xml
@@ -1,22 +1,24 @@
 <monet>
 <widget type="button">
-  <rect x="0" y="0" width="w" height="h" stroke-width="1" stroke="#988c7c" corner-radius="4">
+  <rect x="0" y="0" width="width" height="height" stroke-width="1"
+  stroke="#988c7c" corner-radius="4">
     <gradient x1="0" y1="0" x2="0" y1="50">
       <stop color="#fcfbfa" position="1"/>
       <stop color="#e7e2da" position="0"/>
     </gradient>
   </rect>
-  <rect x="1" y="1" width="w-2" height="h-2" stroke-width="1" stroke="#fff" corner-radius="3"></rect>
+  <rect x="1" y="1" width="width-2" height="height-2" stroke-width="1"
+  stroke="#fff" corner-radius="3"></rect>
 </widget>
 
 <widget type="button" state="active">
-  <rect x="0" y="0" width="w" height="h" stroke-width="1" stroke="#988c7c" corner-radius="4">
+  <rect x="0" y="0" width="width" height="height" stroke-width="1" stroke="#988c7c" corner-radius="4">
     <gradient x1="0" y1="0" x2="0" y1="50">
       <stop color="#fcfbfa" position="0"/>
       <stop color="#e7e2da" position="1"/>
     </gradient>
   </rect>
-  <rect x="1" y="1" width="w-2" height="h-2" stroke-width="1" stroke="#fff" corner-radius="3"></rect>
+  <rect x="1" y="1" width="width-2" height="height-2" stroke-width="1" stroke="#fff" corner-radius="3"></rect>
 </widget>
 
 <widget type="entry">
@@ -26,7 +28,8 @@
   <line x1="-1" y1="0" y2="-1" x1="-1" stroke="#fff" stroke-width="1"/>
   <line x1="-1" y1="-1" y2="-1" x1="0" stroke="#fff" stroke-width="1"/>
 
-  <rect x="1" y="1" width="w-2" height="w-2" stroke="#ccc" stroke-width="1"/>
+  <rect x="1" y="1" width="width-2" height="height-2" stroke="#ccc"
+  stroke-width="1"/>
 </widget>
 
 <widget type="radio">
@@ -34,7 +37,7 @@
 </widget>
 
 <widget type="check">
-  <rect x="0" y="0" width="w-2" height="h-2" stroke="#000" stroke-width="1">
+  <rect x="0" y="0" width="width-2" height="height-2" stroke="#000" stroke-width="1">
     <stroke-gradient x1="0" y1="0" x2="0" y2="20">
       <stop color="#fff" position="0"/>
       <stop color="#000" position="1"/>
diff --git a/monet/mn-style.c b/monet/mn-style.c
index c344bf3..60b5587 100644
--- a/monet/mn-style.c
+++ b/monet/mn-style.c
@@ -137,21 +137,27 @@ parse_expression (gchar  *expression,
                   double  w1,
                   double  h1)
 {
-  gchar *e;
+  gchar *e, *next;
   double result = 0;
   double num;
   gchar op = '+'; /* always add the first number to the result */
 
-  for (e = expression; *e; e = e + 1)
+  for (e = expression; e < expression + strlen (expression);)
     {
       switch (e[0])
         {
         case 'w':
+          /* width */
           num = w1;
+          /* skip to the end */
+          e += 5;
           break;
 
         case 'h':
+          /* height */
           num = h1;
+          /* skip to the end */
+          e += 6;
           break;
 
         case '+':
@@ -159,17 +165,26 @@ parse_expression (gchar  *expression,
         case '/':
         case '*':
           op = e[0];
+          e++;
           continue;
 
         case ' ':
         case '\t':
           /* ignore whitespace */
+          e++;
           continue;
 
         default:
           /* try and parse a number */
-          num = g_ascii_strtod (e, &e);
-          e--; /* move back one char to allow for loop to increment */
+          num = g_ascii_strtod (e, &next);
+          if (next == e)
+            {
+              g_warning ("Parse failed, expected number but found \"%s\"",
+                         e);
+              return 0;
+            }
+          else
+            e = next;
         }
 
       switch (op)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]