Code

part of bug #339660; can not use Add/Rename dialog to create layer with no proper...
[inkscape.git] / src / helper-fns.h
index 29fd2ebec7aabf49e70148d8e4fa39283aa0f83c..43c90063b28252cb2a6ca1eeca2c47e2a640fcc9 100644 (file)
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
-static double
-helperfns_read_number(gchar const *value) {
+#include <string.h>
+#include <vector>
+#include <sstream>
+
+// calling helperfns_read_number(string, false), it's not obvious, what
+// that false stands for. helperfns_read_number(string, HELPERFNS_NO_WARNING)
+// can be more clear.
+#define HELPERFNS_NO_WARNING false
+
+/* Setting warning to false disables conversion error warnings from
+ * this function. This can be useful in places, where the input type
+ * is not known beforehand. For example, see sp_feColorMatrix_set in
+ * sp-fecolormatrix.cpp */
+inline double helperfns_read_number(gchar const *value, bool warning = true) {
     if (!value) return 0;
     char *end;
     double ret = g_ascii_strtod(value, &end);
     if (*end) {
-        g_warning("Unable to convert \"%s\" to number", value);
+        if (warning) {
+            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;
@@ -27,7 +41,7 @@ helperfns_read_number(gchar const *value) {
     return ret;
 }
 
-static bool helperfns_read_bool(gchar const *value, bool default_value){
+inline bool helperfns_read_bool(gchar const *value, bool default_value){
     if (!value) return default_value;
     switch(value[0]){
         case 't':
@@ -40,12 +54,20 @@ static bool helperfns_read_bool(gchar const *value, bool default_value){
     return default_value;
 }
 
-static std::vector<gdouble> helperfns_read_vector(const gchar* value, int size){
+inline std::vector<gdouble> helperfns_read_vector(const gchar* value, int size){
         std::vector<gdouble> v(size, (gdouble) 0);
-        int i;
-        gchar** values = g_strsplit(value , " ", size);
-        for (i=0;i<size && values[i];i++)
-            v[i] = g_ascii_strtod(values[i], NULL);
+        std::istringstream is(value);
+        for(int i = 0; i < size && (is >> v[i]); i++){};
+        return v;
+}
+
+inline std::vector<gdouble> helperfns_read_vector(const gchar* value){
+        std::vector<gdouble> v;
+        std::istringstream is(value);
+        gdouble d;
+        while (is >> d){
+            v.push_back(d);
+        }
         return v;
 }