~valavanisalex/ubuntu/oneiric/inkscape/inkscape_0.48.1-2ubuntu4

« back to all changes in this revision

Viewing changes to src/helper-fns.h

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook, Ted Gould, Kees Cook
  • Date: 2009-06-24 14:00:43 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20090624140043-07stp20mry48hqup
Tags: 0.47~pre0-0ubuntu1
* New upstream release

[ Ted Gould ]
* debian/control: Adding libgsl0 and removing version specifics on boost

[ Kees Cook ]
* debian/watch: updated to run uupdate and mangle pre-release versions.
* Dropped patches that have been taken upstream:
  - 01_mips
  - 02-poppler-0.8.3
  - 03-chinese-inkscape
  - 05_fix_latex_patch
  - 06_gcc-4.4
  - 07_cdr2svg
  - 08_skip-bad-utf-on-pdf-import
  - 09_gtk-clist
  - 10_belarussian
  - 11_libpng
  - 12_desktop
  - 13_slider
  - 100_svg_import_improvements
  - 102_sp_pattern_painter_free
  - 103_bitmap_type_print

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#ifndef SEEN_HELPER_FNS_H
2
2
#define SEEN_HELPER_FNS_H
3
3
/** \file
4
 
 * 
 
4
 *
5
5
 * Some helper functions
6
 
 * 
 
6
 *
7
7
 * Authors:
8
8
 *   Felipe Corrêa da Silva Sanches <felipe.sanches@gmail.com>
9
 
 * 
 
9
 *
10
10
 *
11
11
 * Copyright (C) 2006 Hugo Rodrigues
12
12
 *
14
14
 */
15
15
 
16
16
#include <string.h>
17
 
 
 
17
#include <vector>
18
18
#include <sstream>
19
19
 
20
20
// calling helperfns_read_number(string, false), it's not obvious, what
22
22
// can be more clear.
23
23
#define HELPERFNS_NO_WARNING false
24
24
 
25
 
/* Setting warning to false disables conversion error warnings from
 
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
26
31
 * this function. This can be useful in places, where the input type
27
32
 * is not known beforehand. For example, see sp_feColorMatrix_set in
28
33
 * sp-fecolormatrix.cpp */
29
34
inline double helperfns_read_number(gchar const *value, bool warning = true) {
30
 
    if (!value) return 0;
 
35
    if (!value) {
 
36
        g_warning("Called helperfns_read_number with value==null_ptr, this can lead to unexpected behaviour.");
 
37
        return 0;
 
38
    }
31
39
    char *end;
32
 
    double ret = g_ascii_strtod(value, &end);
 
40
    double ret = g_strtod(value, &end);
33
41
    if (*end) {
34
42
        if (warning) {
35
 
            g_warning("Unable to convert \"%s\" to number", value);
 
43
            g_warning("helper-fns::helperfns_read_number() Unable to convert \"%s\" to number", value);
36
44
        }
37
45
        // We could leave this out, too. If strtod can't convert
38
46
        // anything, it will return zero.
54
62
    return default_value;
55
63
}
56
64
 
 
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
 */
57
72
inline std::vector<gdouble> helperfns_read_vector(const gchar* value, int size){
58
73
        std::vector<gdouble> v(size, (gdouble) 0);
59
74
        std::istringstream is(value);
60
 
        for(int i = 0; i < size && (is >> v[i]); i++);
 
75
        for(int i = 0; i < size; i++){
 
76
                std::string str;
 
77
            is >> str;
 
78
            char *end;
 
79
 
 
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
        };
61
89
        return v;
62
90
}
63
91
 
 
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
 */
64
98
inline std::vector<gdouble> helperfns_read_vector(const gchar* value){
65
99
        std::vector<gdouble> v;
66
 
        std::istringstream is(value);
67
 
        gdouble d;
68
 
        while (is >> d){
69
 
            v.push_back(d);
 
100
 
 
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);
 
114
 
 
115
            beg = end;
 
116
            while(isspace(*beg)) beg++;
70
117
        }
71
118
        return v;
72
119
}