Code

Packaging. New Brazilian Portuguese NSH file. Set trunk package date to 2011.
[inkscape.git] / src / helper-fns.h
index f588b9905970fd355fc6485b644e0011a9f3fad9..f407364a595e7366be01be82bb718667550bb585 100644 (file)
@@ -1,25 +1,47 @@
 #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>
- * 
+ *   Felipe CorrĂȘa da Silva Sanches <juca@members.fsf.org>
+ *
  *
  * 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;
+#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
+
+/* convert ascii representation to double
+ * the function can only be used to convert numbers as given by gui elements that use localized representation
+ * @param value ascii representation of the number
+ * @return the converted number
+ *
+ * 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) {
+        g_warning("Called helperfns_read_number with value==null_ptr, this can lead to unexpected behaviour.");
+        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("helper-fns::helperfns_read_number() 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 +49,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 +62,59 @@ 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){
+/* convert ascii representation to double
+ * the function can only be used to convert numbers as given by gui elements that use localized representation
+ * numbers are delimeted by space
+ * @param value ascii representation of the number
+ * @param size number of elements in string
+ * @return the vector of the converted numbers
+ */
+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;i++)
-                v[i] = g_ascii_strtod(values[i], NULL);
+        std::istringstream is(value);
+        for(int i = 0; i < size; i++){
+               std::string str;
+            is >> str;
+            char *end;
+
+            double ret = g_ascii_strtod(str.c_str(), &end);
+            if (*end) {
+                g_warning("helper-fns::helperfns_read_vector() Unable to convert \"%s\" to number", str.c_str());
+                // We could leave this out, too. If strtod can't convert
+                // anything, it will return zero.
+                ret = 0;
+            }
+            v[i] = ret;
+        };
+        return v;
+}
+
+/* convert ascii representation to double
+ * the function can only be used to convert numbers as given by gui elements that use localized representation
+ * numbers are delimeted by space
+ * @param value ascii representation of the number
+ * @return the vector of the converted numbers
+ */
+inline std::vector<gdouble> helperfns_read_vector(const gchar* value){
+        std::vector<gdouble> v;
+
+        gchar const* beg = value;
+        while(isspace(*beg)) beg++;
+        while(*beg)
+        {
+            char *end;
+            double ret = g_ascii_strtod(beg, &end);
+            if (end==beg){
+                g_warning("helper-fns::helperfns_read_vector() Unable to convert \"%s\" to number", beg);
+                // We could leave this out, too. If strtod can't convert
+                // anything, it will return zero.
+                ret = 0;
+            }
+            v.push_back(ret);
+
+            beg = end;
+            while(isspace(*beg)) beg++;
+        }
         return v;
 }
 
@@ -60,4 +129,4 @@ static std::vector<gdouble> helperfns_read_vector(const gchar* value, int size){
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :