-
Notifications
You must be signed in to change notification settings - Fork 29
/
versionstring.cpp
182 lines (154 loc) · 3.31 KB
/
versionstring.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
// Copyright (c) 2016 SMART Technologies. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#pragma warning(push)
#pragma warning(disable : 4996)
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#pragma warning(pop)
#include "versionstring.h"
#include <sstream>
#include <vector>
namespace peparser
{
VersionString::VersionString(const std::wstring& value)
{
Reset(value);
}
bool VersionString::Parse(const std::wstring& value)
{
m_valid = false;
std::vector<std::wstring> versions;
try
{
boost::split(versions, value, boost::is_any_of(L"."));
if (versions.size() == 4)
{
m_valid = true;
m_major = std::stoi(versions[0]);
m_minor = std::stoi(versions[1]);
m_build = std::stoi(versions[2]);
m_patch = std::stoi(versions[3]);
}
}
catch (std::exception&)
{
m_valid = false;
}
return m_valid;
}
std::wstring VersionString::Combine()
{
std::wstringstream res;
res << m_major << L"." << m_minor << L"." << m_build << L"." << m_patch;
return res.str();
}
VersionString& VersionString::operator=(const std::wstring& value)
{
Reset(value);
return *this;
}
void VersionString::Reset(const std::wstring& value)
{
Parse(value);
m_fullUnformatted = value;
m_fullFormatted = Combine();
}
bool VersionString::operator<(const VersionString& vs) const
{
if (!this->IsValid())
{
if (!vs.IsValid()) // both are invalid, comparing strings
{
return this->m_fullUnformatted < vs.m_fullUnformatted;
}
return true; // Invalid string is less than valid
}
else
{
if (!vs.IsValid())
{
return false; // Valid string is not less than invalid
}
}
if (m_major < vs.m_major)
{
return true;
}
if (m_major > vs.m_major)
{
return false;
}
if (m_minor < vs.m_minor) // majors are equal
{
return true;
}
if (m_minor > vs.m_minor)
{
return false;
}
if (m_build < vs.m_build) // minors are equal too
{
return true;
}
if (m_build > vs.m_build)
{
return false;
}
if (m_patch < vs.m_patch) // builds are equal as well
{
return true;
}
return false;
}
bool VersionString::operator==(const VersionString& vs)const
{
if (!this->IsValid())
{
if (!vs.IsValid())
{
return this->m_fullUnformatted == vs.m_fullUnformatted;
}
return false; // Invalid string is not equal to valid one
}
if (!vs.IsValid())
{
return false; // Valid string is not equal to invalid one
}
return ((!(vs < *this)) && (!(*this < vs)));
}
bool VersionString::operator>(const VersionString& vs)const
{
if (!this->IsValid())
{
if (!vs.IsValid())
{
return this->m_fullUnformatted > vs.m_fullUnformatted;
}
return false; // Invalid string is not greater than valid one
}
if (!vs.IsValid()) return true; // Valid string is greater than invalid one
return (vs < *this);
}
VersionString::operator std::wstring() const
{
if (IsValid())
{
return m_fullFormatted;
}
else
{
return m_fullUnformatted;
}
}
std::wstring VersionString::FullFormattedForResources() const
{
if (IsValid())
{
std::wstringstream res;
res << m_major << L"." << m_minor << L"." << m_build << L"." << m_patch;
return res.str();
}
else
return m_fullUnformatted;
}
}