OpenCPN Partial API docs
Loading...
Searching...
No Matches
semantic_vers.cpp
1/******************************************************************************
2 *
3 * Project: OpenCPN
4 *
5 ***************************************************************************
6 * Copyright (C) 2019 Alec Leamas *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22 ***************************************************************************
23 */
24
25#include "config.h"
26
27#include <algorithm>
28#include <cstdio>
29#include <string>
30#include <sstream>
31
32#include "semantic_vers.h"
33
34#undef major // walk around gnu's major() and minor() macros.
35#undef minor
36
38 using namespace std;
39
40 SemanticVersion vers;
41 size_t pos = s.find('+');
42 if (pos != string::npos) {
43 vers.build = s.substr(pos + 1);
44 s = s.substr(0, pos);
45 }
46 pos = s.find('-');
47 if (pos != string::npos) {
48 vers.pre = s.substr(pos + 1);
49 s = s.substr(0, pos);
50 }
51 int r = sscanf(s.c_str(), "%d.%d.%d.%d", &vers.major, &vers.minor,
52 &vers.patch, &vers.post);
53 if (r < 2) {
54 vers.major = -1;
55 }
56 return vers;
57}
58
60 : major(0), minor(0), patch(0), post(0), pre(""), build("") {}
61
62SemanticVersion::SemanticVersion(int major, int minor, int patch, int post,
63 std::string pre, std::string build) {
64 this->major = major;
65 this->minor = minor;
66 this->patch = patch;
67 this->post = post;
68 this->pre = pre;
69 this->build = build;
70}
71
72bool SemanticVersion::operator<(const SemanticVersion& other) {
73 if (major != other.major) return major < other.major;
74 if (minor != other.minor) return minor < other.minor;
75 if (patch != other.patch) return patch < other.patch;
76 if (post != other.post) return post < other.post;
77 return pre < other.pre;
78}
79
80bool SemanticVersion::operator==(const SemanticVersion& other) {
81 return major == other.major && minor == other.minor && patch == other.patch &&
82 post == other.post && pre == other.pre;
83}
84
85bool SemanticVersion::operator>(const SemanticVersion& other) {
86 return !(*this == other) && !(*this < other);
87}
88
89bool SemanticVersion::operator<=(const SemanticVersion& other) {
90 return (*this == other) || (*this < other);
91}
92
93bool SemanticVersion::operator>=(const SemanticVersion& other) {
94 return (*this == other) || (*this > other);
95}
96
97bool SemanticVersion::operator!=(const SemanticVersion& other) {
98 return !(*this == other);
99}
100
101std::ostream& operator<<(std::ostream& s, const SemanticVersion& v) {
102 if ((v.major == 0) && (v.minor == 0) && (v.patch == 0))
103 return s;
104
105 s << v.major << '.' << v.minor;
106 if (v.patch != -1) {
107 s << '.' << v.patch;
108 }
109 if (v.post != 0) {
110 s << '.' << v.post;
111 }
112 if (v.pre != "") {
113 s << '-' << v.pre;
114 }
115 if (v.build != "") {
116 s << '+' << v.build;
117 }
118 return s;
119}
120
122 std::ostringstream os;
123 os << *this;
124 return os.str();
125}
Versions uses a modified semantic versioning scheme: major.minor.revision.post-tag+build.
Definition: semantic_vers.h:51
SemanticVersion()
Construct a "0.0.0.0" version.
static SemanticVersion parse(std::string s)
Parse a version string, sets major == -1 on errors.
std::string to_string()
Return printable representation.