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 "model/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
52 // Ignore prefixes like 'v', 'rc'. etc.
53 while (s.size() && (s[0] < '0' || s[0] > '9')) s = s.substr(1);
54
55 int r = sscanf(s.c_str(), "%d.%d.%d.%d", &vers.major, &vers.minor,
56 &vers.patch, &vers.post);
57 if (r < 2) {
58 vers.major = -1;
59 }
60 return vers;
61}
62
64 : major(0), minor(0), patch(0), post(0), pre(""), build("") {}
65
66SemanticVersion::SemanticVersion(int major, int minor, int patch, int post,
67 std::string pre, std::string build) {
68 this->major = major;
69 this->minor = minor;
70 this->patch = patch;
71 this->post = post;
72 this->pre = pre;
73 this->build = build;
74}
75
76bool SemanticVersion::operator<(const SemanticVersion& other) {
77 if (major != other.major) return major < other.major;
78 if (minor != other.minor) return minor < other.minor;
79 if (patch != other.patch) return patch < other.patch;
80 if (post != other.post) return post < other.post;
81 return pre < other.pre;
82}
83
84bool SemanticVersion::operator==(const SemanticVersion& other) {
85 return major == other.major && minor == other.minor && patch == other.patch &&
86 post == other.post && pre == other.pre;
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) || (*this > other);
99}
100
101bool SemanticVersion::operator!=(const SemanticVersion& other) {
102 return !(*this == other);
103}
104
105std::ostream& operator<<(std::ostream& s, const SemanticVersion& v) {
106 if ((v.major == 0) && (v.minor == 0) && (v.patch == 0)) return s;
107
108 s << v.major << '.' << v.minor;
109 if (v.patch != -1) {
110 s << '.' << v.patch;
111 }
112 if (v.post != 0) {
113 s << '.' << v.post;
114 }
115 if (v.pre != "") {
116 s << '-' << v.pre;
117 }
118 if (v.build != "") {
119 s << '+' << v.build;
120 }
121 return s;
122}
123
125 std::ostringstream os;
126 os << *this;
127 return os.str();
128}
Versions uses a modified semantic versioning scheme: major.minor.revision.post-tag+build.
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.