OpenCPN Partial API docs
Loading...
Searching...
No Matches
semantic_vers.cpp
Go to the documentation of this file.
1/***************************************************************************
2 * Copyright (C) 2019 Alec Leamas *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
16 **************************************************************************/
17
24#include "config.h"
25
26#include <algorithm>
27#include <cstdio>
28#include <string>
29#include <sstream>
30
31#include "model/semantic_vers.h"
32
33#undef major // work around gnu's major() and minor() macros.
34#undef minor
35
37 using namespace std;
38
39 SemanticVersion vers;
40 size_t pos = s.find('+');
41 if (pos != string::npos) {
42 vers.build = s.substr(pos + 1);
43 s = s.substr(0, pos);
44 }
45 pos = s.find('-');
46 if (pos != string::npos) {
47 vers.pre = s.substr(pos + 1);
48 s = s.substr(0, pos);
49 }
50
51 // Ignore prefixes like 'v', 'rc'. etc.
52 while (s.size() && (s[0] < '0' || s[0] > '9')) s = s.substr(1);
53
54 int r = sscanf(s.c_str(), "%d.%d.%d.%d", &vers.major, &vers.minor,
55 &vers.patch, &vers.post);
56 if (r < 2) {
57 vers.major = -1;
58 }
59 return vers;
60}
61
63 : major(0), minor(0), patch(0), post(0), pre(""), build("") {}
64
65SemanticVersion::SemanticVersion(int major, int minor, int patch, int post,
66 std::string pre, std::string build) {
67 this->major = major;
68 this->minor = minor;
69 this->patch = patch;
70 this->post = post;
71 this->pre = pre;
72 this->build = build;
73}
74
75bool SemanticVersion::operator<(const SemanticVersion& other) {
76 if (major != other.major) return major < other.major;
77 if (minor != other.minor) return minor < other.minor;
78 if (patch != other.patch) return patch < other.patch;
79 if (post != other.post) return post < other.post;
80 return pre < other.pre;
81}
82
83bool SemanticVersion::operator==(const SemanticVersion& other) {
84 return major == other.major && minor == other.minor && patch == other.patch &&
85 post == other.post && pre == other.pre;
86}
87
88bool SemanticVersion::operator>(const SemanticVersion& other) {
89 return !(*this == other) && !(*this < other);
90}
91
92bool SemanticVersion::operator<=(const SemanticVersion& other) {
93 return (*this == other) || (*this < other);
94}
95
96bool SemanticVersion::operator>=(const SemanticVersion& other) {
97 return (*this == other) || (*this > other);
98}
99
100bool SemanticVersion::operator!=(const SemanticVersion& other) {
101 return !(*this == other);
102}
103
104std::ostream& operator<<(std::ostream& s, const SemanticVersion& v) {
105 if ((v.major == 0) && (v.minor == 0) && (v.patch == 0)) return s;
106
107 s << v.major << '.' << v.minor;
108 if (v.patch != -1) {
109 s << '.' << v.patch;
110 }
111 if (v.post != 0) {
112 s << '.' << v.post;
113 }
114 if (v.pre != "") {
115 s << '-' << v.pre;
116 }
117 if (v.build != "") {
118 s << '+' << v.build;
119 }
120 return s;
121}
122
124 std::ostringstream os;
125 os << *this;
126 return os.str();
127}
std::ostream & operator<<(std::ostream &s, const SemanticVersion &v)
Dump version string.
Semantic version encode/decode object.
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.