summaryrefslogtreecommitdiffstats
path: root/src/number-opt-number.h
blob: e39b87ed7fc4f760d1279e9046c81bdc089a4723 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// SPDX-License-Identifier: GPL-2.0-or-later
#ifndef SEEN_NUMBER_OPT_NUMBER_H
#define SEEN_NUMBER_OPT_NUMBER_H

/** \file
 * <number-opt-number> implementation.
 */
/*
 * Authors:
 *   Hugo Rodrigues <haa.rodrigues@gmail.com>
 *
 * Copyright (C) 2006 Hugo Rodrigues
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <glib.h>
#include <glib/gprintf.h>
#include <cstdlib>

#include "svg/stringstream.h"

class NumberOptNumber
{
public:
    NumberOptNumber()
    {
        _num = 0.0;
        _set = false;
        _optnum = 0.0;
        _optset = false;
    }

    NumberOptNumber(float num)
    {
        _num = num;
        _set = true;
        _optnum = 0.0;
        _optset = false;
    }

    NumberOptNumber(float num, float optnum)
    {
        _num = num;
        _set = true;
        _optnum = optnum;
        _optset = true;
    }

    float getNumber() const
    {
        return _set ? _num : -1;
    }

    float getOptNumber(bool or_num = false) const
    {
        return _optset ? _optnum : (or_num ? _num : -1);
    }

    void setNumber(float num)
    {
        _set = true;
        _num = num;
    }

    void setOptNumber(float optnum)
    {
        _optset = optnum != -1;
        _optnum = optnum;
    }

    bool numIsSet() const
    {
        return _set;
    }

    bool optNumIsSet() const
    {
        return _optset;
    }
    
    std::string getValueString() const
    {
        Inkscape::SVGOStringStream os;

        if (_set) {
            os << _num;
            if (_optset) {
                os << " " << _optnum;
            }
        }

        return os.str();
    }

    void set(char const *str)
    {
        if (!str) {
            return;
        }

        _set = false;
        _optset = false;

        char **values = g_strsplit(str, " ", 2);

        if (values[0]) {
            _num = g_ascii_strtod(values[0], nullptr);
            _set = true;

            if (values[1]) {
                _optnum = g_ascii_strtod(values[1], nullptr);
                _optset = true;
            }
        }

        g_strfreev(values);
    }

private:
    float _num;
    float _optnum;
    bool _set : 1;
    bool _optset : 1;
};

#endif // SEEN_NUMBER_OPT_NUMBER_H

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :