-
-
Notifications
You must be signed in to change notification settings - Fork 990
/
game_version.cpp
244 lines (201 loc) · 5.45 KB
/
game_version.cpp
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
Copyright (C) 2008 - 2018 by Iris Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "game_version.hpp"
#include "lexical_cast.hpp"
#include "serialization/string_utils.hpp"
#include <cassert>
#include <functional>
#include <locale>
#include <boost/algorithm/string.hpp>
version_info::version_info()
: nums_(3,0), special_(""), special_separator_('\0')
{
}
version_info::version_info(const char* str)
: version_info(std::string(str))
{
}
version_info::version_info(unsigned int major, unsigned int minor, unsigned int revision_level,
char special_separator, const std::string& special)
: nums_(3,0), special_(special), special_separator_(special_separator)
{
nums_[0] = major;
nums_[1] = minor;
nums_[2] = revision_level;
}
version_info::version_info(const std::string& str)
: nums_(3,0)
, special_("")
, special_separator_('\0')
{
std::string v = str;
boost::trim(v);
if(v.empty())
return;
//
// The breakpoint is where the "special" version component begins.
// For 1.1.2a it would at the index of the char 'a'. For 1.1.4+dev it is at '+'.
//
// For 1.5.2 it is at npos.
//
const std::string::size_type breakpoint_pos = v.find_first_not_of(".0123456789");
std::string left_side;
if(breakpoint_pos != std::string::npos) {
const std::string right_side = v.substr(breakpoint_pos);
assert(right_side.empty() == false);
if(std::isalpha(right_side[0], std::locale::classic())) {
special_separator_ = '\0';
special_ = right_side;
}
else {
special_separator_ = right_side[0];
if(right_side.size() > 1) {
special_ = right_side.substr(1);
}
}
left_side = v.substr(0, breakpoint_pos);
}
else {
left_side = v;
}
const std::vector<std::string> components = utils::split(left_side, '.');
const size_t s = components.size();
if(s == 0) {
return;
}
else if(s > 3) {
nums_.resize(s, 0);
}
for(size_t i = 0; (i < s); ++i) {
nums_[i] = lexical_cast_default<unsigned int>(components[i]);
}
}
std::string version_info::str() const
{
const size_t s = nums_.size();
std::ostringstream o;
for(size_t k = 0; k < s; ++k) {
o << nums_[k];
if(s != 1+k) {
o << '.';
}
}
if(! special_.empty()) {
if(special_separator_ != '\0') {
o << special_separator_;
}
o << special_;
}
return o.str();
}
void version_info::set_major_version(unsigned int v) {
nums_[0] = v;
}
void version_info::set_minor_version(unsigned int v) {
nums_[1] = v;
}
void version_info::set_revision_level(unsigned int v) {
nums_[2] = v;
}
unsigned int version_info::major_version() const {
return nums_[0];
}
unsigned int version_info::minor_version() const {
return nums_[1];
}
unsigned int version_info::revision_level() const {
return nums_[2];
}
bool version_info::is_canonical() const {
return nums_.size() <= 3;
}
namespace {
template<template<typename> class Fcn>
bool version_comparison_internal(const version_info& l, const version_info& r)
{
std::vector<unsigned int> lc = l.components();
std::vector<unsigned int> rc = r.components();
const size_t lsize = lc.size();
const size_t rsize = rc.size();
const size_t csize = std::max(lsize, rsize);
// make compatible, missing items default to zero
if(lsize < csize) lc.resize(csize, 0);
if(rsize < csize) rc.resize(csize, 0);
using comp_list = std::vector<unsigned int>;
using comp_pair = std::tuple<const comp_list&, const std::string&>;
Fcn<comp_pair> comp;
const comp_pair& lp = std::tie(lc, l.special_version());
const comp_pair& rp = std::tie(rc, r.special_version());
return comp(lp, rp);
}
} // end unnamed namespace
bool operator==(const version_info& l, const version_info& r)
{
return version_comparison_internal<std::equal_to>(l, r);
}
bool operator!=(const version_info& l, const version_info& r)
{
return version_comparison_internal<std::not_equal_to>(l, r);
}
bool operator<(const version_info& l, const version_info& r)
{
return version_comparison_internal<std::less>(l, r);
}
bool operator>(const version_info& l, const version_info& r)
{
return version_comparison_internal<std::greater>(l, r);
}
bool operator<=(const version_info& l, const version_info& r)
{
return version_comparison_internal<std::less_equal>(l, r);
}
bool operator>=(const version_info& l, const version_info& r)
{
return version_comparison_internal<std::greater_equal>(l, r);
}
VERSION_COMP_OP parse_version_op(const std::string& op_str)
{
if(op_str == "==") {
return OP_EQUAL;
} else if(op_str == "!=") {
return OP_NOT_EQUAL;
} else if(op_str == "<") {
return OP_LESS;
} else if(op_str == "<=") {
return OP_LESS_OR_EQUAL;
} else if(op_str == ">") {
return OP_GREATER;
} else if(op_str == ">=") {
return OP_GREATER_OR_EQUAL;
}
return OP_INVALID;
}
bool do_version_check(const version_info& a, VERSION_COMP_OP op, const version_info& b)
{
switch(op) {
case OP_EQUAL:
return a == b;
case OP_NOT_EQUAL:
return a != b;
case OP_LESS:
return a < b;
case OP_LESS_OR_EQUAL:
return a <= b;
case OP_GREATER:
return a > b;
case OP_GREATER_OR_EQUAL:
return a >= b;
default:
;
}
return false;
}