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