/
version.cpp
192 lines (166 loc) · 4.42 KB
/
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
/**
* @file version.cpp
* Version numbering and labeling for libcore.
*
* @authors Copyright © 2011-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2011-2013 Daniel Swanson <danij@dengine.net>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "de/Version"
#include <QStringList>
#ifdef __GNUC__
# undef major
# undef minor
#endif
namespace de {
Version::Version()
: major(0)
, minor(0)
, patch(0)
, build(0)
{}
Version::Version(int major, int minor, int patch, int buildNumber)
: major(major)
, minor(minor)
, patch(patch)
, build(buildNumber)
{}
Version Version::currentBuild()
{
Version v;
v.major = LIBDENG2_MAJOR_VERSION;
v.minor = LIBDENG2_MINOR_VERSION;
v.patch = LIBDENG2_PATCHLEVEL;
#ifdef LIBDENG2_BUILD_TEXT
v.build = String(LIBDENG2_BUILD_TEXT).toInt();
#else
v.build = Time().asBuildNumber(); // only used in development builds
#endif
v.label = LIBDENG2_RELEASE_LABEL;
#ifdef LIBDENG2_GIT_DESCRIPTION
v.gitDescription = LIBDENG2_GIT_DESCRIPTION;
#endif
return v;
}
Version::Version(String const &version, int buildNumber)
{
parseVersionString(version);
if (buildNumber)
{
build = buildNumber;
}
}
bool Version::isValid() const
{
return major || minor || patch || build ||
!label.isEmpty() || !gitDescription.isEmpty();
}
String Version::base() const
{
String v = compactNumber();
if (!label.isEmpty()) v += String("-%1").arg(label);
return v;
}
String Version::compactNumber() const
{
if (patch != 0)
{
return String("%1.%2.%3").arg(major).arg(minor).arg(patch);
}
return String("%1.%2").arg(major).arg(minor);
}
String Version::fullNumber() const
{
if (build != 0)
{
return String("%1.%2.%3.%4").arg(major).arg(minor).arg(patch).arg(build);
}
return String("%1.%2.%3").arg(major).arg(minor).arg(patch);
}
String Version::asHumanReadableText() const
{
if (!build) return base();
return base() + String(" [#%1]").arg(build);
}
void Version::parseVersionString(String const &version)
{
major = minor = patch = build = 0;
label.clear();
gitDescription.clear();
int dashPos = version.indexOf('-');
QStringList parts = version.left(dashPos).split('.');
if (parts.size() >= 1) major = String(parts[0]).toInt();
if (parts.size() >= 2) minor = String(parts[1]).toInt(nullptr, 10, String::AllowSuffix);
if (parts.size() >= 3) patch = String(parts[2]).toInt(nullptr, 10, String::AllowSuffix);
if (parts.size() >= 4) build = String(parts[3]).toInt(nullptr, 10, String::AllowSuffix);
if (dashPos >= 0 && dashPos < version.size() - 1)
{
label = version.substr(dashPos + 1);
}
}
bool Version::operator < (Version const &other) const
{
if (major == other.major)
{
if (minor == other.minor)
{
if (patch == other.patch)
{
return build < other.build;
}
return patch < other.patch;
}
return minor < other.minor;
}
return major < other.major;
}
bool Version::operator == (Version const &other) const
{
return major == other.major && minor == other.minor &&
patch == other.patch && build == other.build;
}
bool Version::operator > (Version const &other) const
{
return !(*this < other || *this == other);
}
String Version::operatingSystem()
{
#if defined (WIN32)
return "windows";
#elif defined (MACOSX)
return "macx";
#elif defined (DENG_IOS)
return "ios";
#else
return "unix";
#endif
}
duint Version::cpuBits()
{
#ifdef DENG2_64BIT
return 64;
#else
return 32;
#endif
}
bool Version::isDebugBuild()
{
#ifdef DENG2_DEBUG
return true;
#else
return false;
#endif
}
} // namespace de