From: jucablues Date: Fri, 27 Jul 2007 04:23:10 +0000 (+0000) Subject: refactoring: gathering some commonly copy'n'pasted functions on a common X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=12f8d6edf83762026a782661a1e458bf4d026248;p=inkscape.git refactoring: gathering some commonly copy'n'pasted functions on a common header --- diff --git a/src/helper-fns.h b/src/helper-fns.h new file mode 100644 index 000000000..10c311eda --- /dev/null +++ b/src/helper-fns.h @@ -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 + * + * + * 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 : diff --git a/src/sp-fecomposite.cpp b/src/sp-fecomposite.cpp index b191f9826..cb1726be7 100644 --- a/src/sp-fecomposite.cpp +++ b/src/sp-fecomposite.cpp @@ -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) diff --git a/src/sp-feconvolvematrix.cpp b/src/sp-feconvolvematrix.cpp index edd093499..adabc1bac 100644 --- a/src/sp-feconvolvematrix.cpp +++ b/src/sp-feconvolvematrix.cpp @@ -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 read_kernel_matrix(const gchar* value, int size){ std::vector v(size, (gdouble) 0); int i; @@ -126,20 +126,6 @@ static std::vector 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); diff --git a/src/sp-feoffset.cpp b/src/sp-feoffset.cpp index 03d8b6227..67a8b99e2 100644 --- a/src/sp-feoffset.cpp +++ b/src/sp-feoffset.cpp @@ -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); diff --git a/src/sp-feturbulence.cpp b/src/sp-feturbulence.cpp index f7b256898..effdd777e 100644 --- a/src/sp-feturbulence.cpp +++ b/src/sp-feturbulence.cpp @@ -22,6 +22,7 @@ #include "attributes.h" #include "svg/svg.h" #include "sp-feturbulence.h" +#include "helper-fns.h" #include "xml/repr.h" #include @@ -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);