Code

refactoring: gathering some commonly copy'n'pasted functions on a common
authorjucablues <jucablues@users.sourceforge.net>
Fri, 27 Jul 2007 04:23:10 +0000 (04:23 +0000)
committerjucablues <jucablues@users.sourceforge.net>
Fri, 27 Jul 2007 04:23:10 +0000 (04:23 +0000)
header

src/helper-fns.h [new file with mode: 0644]
src/sp-fecomposite.cpp
src/sp-feconvolvematrix.cpp
src/sp-feoffset.cpp
src/sp-feturbulence.cpp

diff --git a/src/helper-fns.h b/src/helper-fns.h
new file mode 100644 (file)
index 0000000..10c311e
--- /dev/null
@@ -0,0 +1,54 @@
+#ifndef SEEN_HELPER_FNS_H
+#define SEEN_HELPER_FNS_H
+/** \file
+ * 
+ * Some helper functions
+ * 
+ * Authors:
+ *   Felipe CorrĂȘa da Silva Sanches <felipe.sanches@gmail.com>
+ * 
+ *
+ * Copyright (C) 2006 Hugo Rodrigues
+ *
+ * Released under GNU GPL, read the file 'COPYING' for more information
+ */
+
+static double
+helperfns_read_number(gchar const *value) {
+    if (!value) return 0;
+    char *end;
+    double ret = g_ascii_strtod(value, &end);
+    if (*end) {
+        g_warning("Unable to convert \"%s\" to number", value);
+        // We could leave this out, too. If strtod can't convert
+        // anything, it will return zero.
+        ret = 0;
+    }
+    return ret;
+}
+
+static bool helperfns_read_bool(gchar const *value, bool default_value){
+    if (!value) return default_value;
+    switch(value[0]){
+        case 't':
+            if (strncmp(value, "true", 4) == 0) return true;
+            break;
+        case 'f':
+            if (strncmp(value, "false", 5) == 0) return false;
+            break;
+    }
+    return default_value;
+}
+#endif /* !SEEN_HELPER_FNS_H */
+
+/*
+  Local Variables:
+  mode:c++
+  c-file-style:"stroustrup"
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
+  indent-tabs-mode:nil
+  fill-column:99
+  End:
+*/
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
index b191f982657f0bfac3d39b71bab5dfd472c9da8d..cb1726be79b3be2744a7f28da5546fad2716ce8e 100644 (file)
@@ -20,6 +20,7 @@
 #include "attributes.h"
 #include "svg/svg.h"
 #include "sp-fecomposite.h"
+#include "helper-fns.h"
 #include "xml/repr.h"
 #include "display/nr-filter-composite.h"
 
@@ -120,21 +121,6 @@ sp_feComposite_release(SPObject *object)
         ((SPObjectClass *) feComposite_parent_class)->release(object);
 }
 
-static double
-sp_feComposite_read_number(gchar const *value) {
-    if (!value) return 0;
-
-    char *end;
-    double ret = g_ascii_strtod(value, &end);
-    if (*end) {
-        g_warning("Unable to convert \"%s\" to number", value);
-        // We could leave this out, too. If strtod can't convert
-        // anything, it will return zero.
-        ret = 0;
-    }
-    return ret;
-}
-
 static FeCompositeOperator
 sp_feComposite_read_operator(gchar const *value) {
     if (!value) return COMPOSITE_DEFAULT;
@@ -171,7 +157,7 @@ sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value)
             break;
 
         case SP_ATTR_K1:
-            k_n = sp_feComposite_read_number(value);
+            k_n = helperfns_read_number(value);
             if (k_n != feComposite->k1) {
                 feComposite->k1 = k_n;
                 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
@@ -180,7 +166,7 @@ sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value)
             break;
 
         case SP_ATTR_K2:
-            k_n = sp_feComposite_read_number(value);
+            k_n = helperfns_read_number(value);
             if (k_n != feComposite->k2) {
                 feComposite->k2 = k_n;
                 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
@@ -189,7 +175,7 @@ sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value)
             break;
 
         case SP_ATTR_K3:
-            k_n = sp_feComposite_read_number(value);
+            k_n = helperfns_read_number(value);
             if (k_n != feComposite->k3) {
                 feComposite->k3 = k_n;
                 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
@@ -198,7 +184,7 @@ sp_feComposite_set(SPObject *object, unsigned int key, gchar const *value)
             break;
 
         case SP_ATTR_K4:
-            k_n = sp_feComposite_read_number(value);
+            k_n = helperfns_read_number(value);
             if (k_n != feComposite->k4) {
                 feComposite->k4 = k_n;
                 if (feComposite->composite_operator == COMPOSITE_ARITHMETIC)
index edd093499883324052eaea98c5e79a14b1bf5c9b..adabc1bac06d1362551897906d7087f4c3ebab49 100644 (file)
@@ -22,6 +22,7 @@
 #include "attributes.h"
 #include "svg/svg.h"
 #include "sp-feconvolvematrix.h"
+#include "helper-fns.h"
 #include "xml/repr.h"
 #include "display/nr-filter-convolve-matrix.h"
 
@@ -116,7 +117,6 @@ sp_feConvolveMatrix_release(SPObject *object)
         ((SPObjectClass *) feConvolveMatrix_parent_class)->release(object);
 }
 
-
 static std::vector<gdouble> read_kernel_matrix(const gchar* value, int size){
         std::vector<gdouble> v(size, (gdouble) 0);
         int i;
@@ -126,20 +126,6 @@ static std::vector<gdouble> read_kernel_matrix(const gchar* value, int size){
         return v;
 }
 
-static double
-sp_feConvolveMatrix_read_number(gchar const *value) {
-    if (!value) return 0;
-    char *end;
-    double ret = g_ascii_strtod(value, &end);
-    if (*end) {
-        g_warning("Unable to convert \"%s\" to number", value);
-        // We could leave this out, too. If strtod can't convert
-        // anything, it will return zero.
-        ret = 0;
-    }
-    return ret;
-}
-
 static int sp_feConvolveMatrix_read_edgeMode(gchar const *value){
     if (!value) return 0; //duplicate is default
     switch(value[0]){
@@ -156,20 +142,6 @@ static int sp_feConvolveMatrix_read_edgeMode(gchar const *value){
     return 0; //duplicate is default
 }
 
-
-static bool sp_feConvolveMatrix_read_bool(gchar const *value){
-    if (!value) return false; //false is default
-    switch(value[0]){
-        case 't':
-            if (strncmp(value, "true", 4) == 0) return true;
-            break;
-        case 'f':
-            if (strncmp(value, "false", 5) == 0) return false;
-            break;
-    }
-    return false; //false is default
-}
-
 /**
  * Sets a specific value in the SPFeConvolveMatrix.
  */
@@ -196,28 +168,28 @@ sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
         case SP_ATTR_DIVISOR:
-            read_num = sp_feConvolveMatrix_read_number(value);
+            read_num = helperfns_read_number(value);
             if (read_num != feConvolveMatrix->divisor){
                 feConvolveMatrix->divisor = read_num;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
         case SP_ATTR_BIAS:
-            read_num = sp_feConvolveMatrix_read_number(value);
+            read_num = helperfns_read_number(value);
             if (read_num != feConvolveMatrix->bias){
                 feConvolveMatrix->bias = read_num;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
         case SP_ATTR_TARGETX:
-            read_int = (int) sp_feConvolveMatrix_read_number(value);
+            read_int = (int) helperfns_read_number(value);
             if (read_int != feConvolveMatrix->targetX){
                 feConvolveMatrix->targetX = read_int;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
         case SP_ATTR_TARGETY:
-            read_int = (int) sp_feConvolveMatrix_read_number(value);
+            read_int = (int) helperfns_read_number(value);
             if (read_int != feConvolveMatrix->targetY){
                 feConvolveMatrix->targetY = read_int;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
@@ -235,7 +207,7 @@ sp_feConvolveMatrix_set(SPObject *object, unsigned int key, gchar const *value)
             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
         case SP_ATTR_PRESERVEALPHA:
-            read_bool = sp_feConvolveMatrix_read_bool(value);
+            read_bool = helperfns_read_bool(value, false);
             if (read_bool != feConvolveMatrix->preserveAlpha){
                 feConvolveMatrix->preserveAlpha = read_bool;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
index 03d8b6227502b7a21831c7a46cd37ff7fdccc74c..67a8b99e2749b46cf90d56f115a4f6a4fe4f6914 100644 (file)
@@ -21,6 +21,7 @@
 #include "attributes.h"
 #include "svg/svg.h"
 #include "sp-feoffset.h"
+#include "helper-fns.h"
 #include "xml/repr.h"
 #include "display/nr-filter-offset.h"
 
@@ -109,20 +110,6 @@ sp_feOffset_release(SPObject *object)
         ((SPObjectClass *) feOffset_parent_class)->release(object);
 }
 
-static double
-sp_feOffset_read_number(gchar const *value) {
-    if (!value) return 0;
-    char *end;
-    double ret = g_ascii_strtod(value, &end);
-    if (*end) {
-        g_warning("Unable to convert \"%s\" to number", value);
-        // We could leave this out, too. If strtod can't convert
-        // anything, it will return zero.
-        ret = 0;
-    }
-    return ret;
-}
-
 /**
  * Sets a specific value in the SPFeOffset.
  */
@@ -134,14 +121,14 @@ sp_feOffset_set(SPObject *object, unsigned int key, gchar const *value)
     double read_num;
     switch(key) {
         case SP_ATTR_DX:
-            read_num = sp_feOffset_read_number(value);
+            read_num = helperfns_read_number(value);
             if (read_num != feOffset->dx) {
                 feOffset->dx = read_num;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
         case SP_ATTR_DY:
-            read_num = sp_feOffset_read_number(value);
+            read_num = helperfns_read_number(value);
             if (read_num != feOffset->dy) {
                 feOffset->dy = read_num;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
index f7b256898023d39fd9908774905c2a971299ca27..effdd777ee1ed657785901a7ca473381b33b598a 100644 (file)
@@ -22,6 +22,7 @@
 #include "attributes.h"
 #include "svg/svg.h"
 #include "sp-feturbulence.h"
+#include "helper-fns.h"
 #include "xml/repr.h"
 #include <string.h>
 
@@ -115,20 +116,6 @@ sp_feTurbulence_release(SPObject *object)
         ((SPObjectClass *) feTurbulence_parent_class)->release(object);
 }
 
-static double
-sp_feTurbulence_read_number(gchar const *value) {
-    if (!value) return 0;
-    char *end;
-    double ret = g_ascii_strtod(value, &end);
-    if (*end) {
-        g_warning("Unable to convert \"%s\" to number", value);
-        // We could leave this out, too. If strtod can't convert
-        // anything, it will return zero.
-        ret = 0;
-    }
-    return ret;
-}
-
 static bool sp_feTurbulence_read_stitchTiles(gchar const *value){
     if (!value) return false; // 'noStitch' is default
     switch(value[0]){
@@ -179,14 +166,14 @@ sp_feTurbulence_set(SPObject *object, unsigned int key, gchar const *value)
             object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             break;
         case SP_ATTR_NUMOCTAVES:
-            read_int = (int) sp_feTurbulence_read_number(value);
+            read_int = (int) helperfns_read_number(value);
             if (read_int != feTurbulence->numOctaves){
                 feTurbulence->numOctaves = read_int;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);
             }
             break;
         case SP_ATTR_SEED:
-            read_num = sp_feTurbulence_read_number(value);
+            read_num = helperfns_read_number(value);
             if (read_num != feTurbulence->seed){
                 feTurbulence->seed = read_num;
                 object->parent->requestModified(SP_OBJECT_MODIFIED_FLAG);