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 /* Setting warning to false disables conversion error warnings from
26 * this function. This can be useful in places, where the input type
27 * is not known beforehand. For example, see sp_feColorMatrix_set in
28 * sp-fecolormatrix.cpp */
29 inline double helperfns_read_number(gchar const *value, bool warning = true) {
30 if (!value) return 0;
31 char *end;
32 double ret = g_ascii_strtod(value, &end);
33 if (*end) {
34 if (warning) {
35 g_warning("Unable to convert \"%s\" to number", value);
36 }
37 // We could leave this out, too. If strtod can't convert
38 // anything, it will return zero.
39 ret = 0;
40 }
41 return ret;
42 }
44 inline bool helperfns_read_bool(gchar const *value, bool default_value){
45 if (!value) return default_value;
46 switch(value[0]){
47 case 't':
48 if (strncmp(value, "true", 4) == 0) return true;
49 break;
50 case 'f':
51 if (strncmp(value, "false", 5) == 0) return false;
52 break;
53 }
54 return default_value;
55 }
57 inline std::vector<gdouble> helperfns_read_vector(const gchar* value, int size){
58 std::vector<gdouble> v(size, (gdouble) 0);
59 std::istringstream is(value);
60 for(int i = 0; i < size && (is >> v[i]); i++){};
61 return v;
62 }
64 inline std::vector<gdouble> helperfns_read_vector(const gchar* value){
65 std::vector<gdouble> v;
66 std::istringstream is(value);
67 gdouble d;
68 while (is >> d){
69 v.push_back(d);
70 }
71 return v;
72 }
74 #endif /* !SEEN_HELPER_FNS_H */
76 /*
77 Local Variables:
78 mode:c++
79 c-file-style:"stroustrup"
80 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
81 indent-tabs-mode:nil
82 fill-column:99
83 End:
84 */
85 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :