Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
version.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Versions
4 *
5 * Authors:
6 * MenTaLguY <mental@rydia.net>
7 * Jon A. Cruz <jon@joncruz.org>
8 * Kris De Gussem <Kris.DeGussem@gmail.com>
9 * RafaƂ Siejakowski <rs@rs-math.net>
10 *
11 * Copyright (C) 2003 MenTaLguY
12 * Copyright (C) 2012 Kris De Gussem
13 *
14 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15 */
16
17#include <sstream>
18#include "version.h"
19
20namespace Inkscape {
22 : _string_representation{"0.0"}
23{}
24
25Version::Version(unsigned major_version, unsigned minor_version, std::string_view suffix)
26 : _major{major_version}
27 , _minor{minor_version}
28 , _suffix{suffix}
29{}
30
31Version::Version(unsigned major_version, unsigned minor_version)
32 : Version(major_version, minor_version, "")
33{}
34
35std::optional<Version> Version::from_string(char const *version_string)
36{
37 if (!version_string) {
38 return {};
39 }
40
41 try {
42 std::istringstream ss{version_string};
43
44 // Throw an exception if an error occurs when parsing the major and minor numbers.
45 ss.exceptions(std::ios::failbit | std::ios::badbit);
46 unsigned major, minor;
47 std::string suffix;
48
49 ss >> major;
50 char tmp = 0;
51 ss >> tmp;
52 if (tmp != '.') {
53 return {};
54 }
55 ss >> minor;
56
57 // Don't throw exception if failbit gets set (empty string is OK).
58 ss.exceptions(std::ios::goodbit);
59 ss >> suffix;
60 return Version{major, minor, suffix};
61 } catch (...) {
62 return {};
63 }
64}
65
66std::string const &Version::str() const
67{
68 if (_string_representation.empty()) {
69 _string_representation = std::to_string(_major) + '.' + std::to_string(_minor) + _suffix;
70 }
72}
73} // namespace Inkscape
74
75/*
76 Local Variables:
77 mode:c++
78 c-file-style:"stroustrup"
79 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
80 indent-tabs-mode:nil
81 fill-column:99
82 End:
83*/
84// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
unsigned int _major
Definition version.h:49
static std::optional< Version > from_string(const char *version_string)
Build a Version form a string, returning an empty optional in case of error.
Definition version.cpp:35
unsigned int _minor
Definition version.h:50
std::string _string_representation
Definition version.h:52
std::string _suffix
For example, for development version.
Definition version.h:51
std::string const & str() const
Definition version.cpp:66
Helper class to stream background task notifications as a series of messages.
char const * version_string
full version string