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 <juca@members.fsf.org>
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) {
36 g_warning("Called helperfns_read_number with value==null_ptr, this can lead to unexpected behaviour.");
37 return 0;
38 }
39 char *end;
40 double ret = g_strtod(value, &end);
41 if (*end) {
42 if (warning) {
43 g_warning("helper-fns::helperfns_read_number() Unable to convert \"%s\" to number", value);
44 }
45 // We could leave this out, too. If strtod can't convert
46 // anything, it will return zero.
47 ret = 0;
48 }
49 return ret;
50 }
52 inline bool helperfns_read_bool(gchar const *value, bool default_value){
53 if (!value) return default_value;
54 switch(value[0]){
55 case 't':
56 if (strncmp(value, "true", 4) == 0) return true;
57 break;
58 case 'f':
59 if (strncmp(value, "false", 5) == 0) return false;
60 break;
61 }
62 return default_value;
63 }
65 /* convert localized ascii representation to double
66 * the function can only be used to convert numbers as given by gui elements that use localized representation
67 * numbers are delimeted by space
68 * @param value ascii representation of the number
69 * @param size number of elements in string
70 * @return the vector of the converted numbers
71 */
72 inline std::vector<gdouble> helperfns_read_vector(const gchar* value, int size){
73 std::vector<gdouble> v(size, (gdouble) 0);
74 std::istringstream is(value);
75 for(int i = 0; i < size; i++){
76 std::string str;
77 is >> str;
78 char *end;
80 double ret = g_strtod(str.c_str(), &end);
81 if (*end) {
82 g_warning("helper-fns::helperfns_read_vector() Unable to convert \"%s\" to number", str.c_str());
83 // We could leave this out, too. If strtod can't convert
84 // anything, it will return zero.
85 ret = 0;
86 }
87 v[i] = ret;
88 };
89 return v;
90 }
92 /* convert localized ascii representation to double
93 * the function can only be used to convert numbers as given by gui elements that use localized representation
94 * numbers are delimeted by space
95 * @param value ascii representation of the number
96 * @return the vector of the converted numbers
97 */
98 inline std::vector<gdouble> helperfns_read_vector(const gchar* value){
99 std::vector<gdouble> v;
101 gchar const* beg = value;
102 while(isspace(*beg)) beg++;
103 while(*beg)
104 {
105 char *end;
106 double ret = g_strtod(beg, &end);
107 if (end==beg){
108 g_warning("helper-fns::helperfns_read_vector() Unable to convert \"%s\" to number", beg);
109 // We could leave this out, too. If strtod can't convert
110 // anything, it will return zero.
111 ret = 0;
112 }
113 v.push_back(ret);
115 beg = end;
116 while(isspace(*beg)) beg++;
117 }
118 return v;
119 }
121 #endif /* !SEEN_HELPER_FNS_H */
123 /*
124 Local Variables:
125 mode:c++
126 c-file-style:"stroustrup"
127 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
128 indent-tabs-mode:nil
129 fill-column:99
130 End:
131 */
132 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :