1 #ifndef SEEN_HELPER_FNS_H
2 #define SEEN_HELPER_FNS_H
3 /** \file
4 *
5 * Some helper functions
6 *
7 * Authors:
8 * Felipe CorrĂȘa da Silva Sanches <felipe.sanches@gmail.com>
9 *
10 *
11 * Copyright (C) 2006 Hugo Rodrigues
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include <string.h>
17 #include <vector>
18 #include <sstream>
20 // calling helperfns_read_number(string, false), it's not obvious, what
21 // that false stands for. helperfns_read_number(string, HELPERFNS_NO_WARNING)
22 // can be more clear.
23 #define HELPERFNS_NO_WARNING false
25 /* convert localized ascii representation to double
26 * the function can only be used to convert numbers as given by gui elements that use localized representation
27 * @param value ascii representation of the number
28 * @return the converted number
29 *
30 * Setting warning to false disables conversion error warnings from
31 * this function. This can be useful in places, where the input type
32 * is not known beforehand. For example, see sp_feColorMatrix_set in
33 * sp-fecolormatrix.cpp */
34 inline double helperfns_read_number(gchar const *value, bool warning = true) {
35 if (!value) return 0;
36 char *end;
37 double ret = g_strtod(value, &end);
38 if (*end) {
39 if (warning) {
40 g_warning("helper-fns::helperfns_read_number() Unable to convert \"%s\" to number", value);
41 }
42 // We could leave this out, too. If strtod can't convert
43 // anything, it will return zero.
44 ret = 0;
45 }
46 return ret;
47 }
49 inline bool helperfns_read_bool(gchar const *value, bool default_value){
50 if (!value) return default_value;
51 switch(value[0]){
52 case 't':
53 if (strncmp(value, "true", 4) == 0) return true;
54 break;
55 case 'f':
56 if (strncmp(value, "false", 5) == 0) return false;
57 break;
58 }
59 return default_value;
60 }
62 /* convert localized ascii representation to double
63 * the function can only be used to convert numbers as given by gui elements that use localized representation
64 * numbers are delimeted by space
65 * @param value ascii representation of the number
66 * @param size number of elements in string
67 * @return the vector of the converted numbers
68 */
69 inline std::vector<gdouble> helperfns_read_vector(const gchar* value, int size){
70 std::vector<gdouble> v(size, (gdouble) 0);
71 std::istringstream is(value);
72 for(int i = 0; i < size; i++){
73 std::string str;
74 is >> str;
75 char *end;
77 double ret = g_strtod(str.c_str(), &end);
78 if (*end) {
79 g_warning("helper-fns::helperfns_read_vector() Unable to convert \"%s\" to number", str.c_str());
80 // We could leave this out, too. If strtod can't convert
81 // anything, it will return zero.
82 ret = 0;
83 }
84 v[i] = ret;
85 };
86 return v;
87 }
89 /* convert localized ascii representation to double
90 * the function can only be used to convert numbers as given by gui elements that use localized representation
91 * numbers are delimeted by space
92 * @param value ascii representation of the number
93 * @return the vector of the converted numbers
94 */
95 inline std::vector<gdouble> helperfns_read_vector(const gchar* value){
96 std::vector<gdouble> v;
98 gchar const* beg = value;
99 while(isspace(*beg)) beg++;
100 while(*beg)
101 {
102 char *end;
103 double ret = g_strtod(beg, &end);
104 if (end==beg){
105 g_warning("helper-fns::helperfns_read_vector() Unable to convert \"%s\" to number", beg);
106 // We could leave this out, too. If strtod can't convert
107 // anything, it will return zero.
108 ret = 0;
109 }
110 v.push_back(ret);
112 beg = end;
113 while(isspace(*beg)) beg++;
114 }
115 return v;
116 }
118 #endif /* !SEEN_HELPER_FNS_H */
120 /*
121 Local Variables:
122 mode:c++
123 c-file-style:"stroustrup"
124 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
125 indent-tabs-mode:nil
126 fill-column:99
127 End:
128 */
129 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :