DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
Version.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "System/Version.h"
5#include <sstream>
6#include <vector>
7#include <string>
8
9namespace DotNetDupe {
10 namespace System {
11
12 static bool ParseComponent(const std::string& sPart, int& iVal) {
14 if (sPart.empty()) return false;
15
17 for (char c : sPart) {
18 if (c < '0' || c > '9') return false;
19 }
20
22 try {
23 long long llVal = std::stoll(sPart);
24 if (llVal < 0 || llVal > 2147483647LL) return false;
25 iVal = static_cast<int>(llVal);
26 return true;
27 } catch (...) {
28 return false;
29 }
30 }
31
32 static bool SplitVersionParts(const String& sInput, std::vector<std::string>& vecParts) {
34 if (sInput.IsEmpty()) return false;
35 std::string s = sInput.GetRawString();
36 if (s.empty() || s.front() == '.' || s.back() == '.') return false;
37
39 std::stringstream ss(s);
40 std::string item;
41 while (std::getline(ss, item, '.')) {
42 if (item.empty()) return false;
43 vecParts.push_back(item);
44 }
45
47 return (vecParts.size() >= 2 && vecParts.size() <= 4);
48 }
49
50 static bool TryParseInternal(const String& sInput, int& iMajor, int& iMinor, int& iBuild, int& iRevision, int& iCount) {
52 std::vector<std::string> vecParts;
53 if (!SplitVersionParts(sInput, vecParts)) return false;
54
56 iCount = static_cast<int>(vecParts.size());
57 int arrVals[4] = { 0, 0, 0, 0 };
58 for (size_t i = 0; i < vecParts.size(); ++i) {
59 if (!ParseComponent(vecParts[i], arrVals[i])) return false;
60 }
61 iMajor = arrVals[0];
62 iMinor = arrVals[1];
63 iBuild = (iCount >= 3) ? arrVals[2] : 0;
64 iRevision = (iCount == 4) ? arrVals[3] : 0;
65 return true;
66 }
67
68 Version::Version(int iMajor, int iMinor, int iBuild, int iRevision)
69 : m_iMajor(iMajor), m_iMinor(iMinor), m_iBuild(iBuild), m_iRevision(iRevision) { }
70
71 Version::Version(int iMajor, int iMinor, int iBuild)
72 : m_iMajor(iMajor), m_iMinor(iMinor), m_iBuild(iBuild), m_iRevision(0) { }
73
74 Version::Version(int iMajor, int iMinor)
75 : m_iMajor(iMajor), m_iMinor(iMinor), m_iBuild(0), m_iRevision(0) { }
76
78 : m_iMajor(0), m_iMinor(0), m_iBuild(0), m_iRevision(0) { }
79
80 int Version::GetMajor() const {
81 return m_iMajor;
82 }
83
84 int Version::GetMinor() const {
85 return m_iMinor;
86 }
87
88 int Version::GetBuild() const {
89 return m_iBuild;
90 }
91
93 return m_iRevision;
94 }
95
97 std::stringstream ss;
98 ss << m_iMajor << "." << m_iMinor << "." << m_iBuild << "." << m_iRevision;
99 return String(ss.str().c_str());
100 }
101
103 if (sInput.IsEmpty()) {
104 throw ArgumentException("Input string cannot be empty.");
105 }
106 Version vResult;
107 if (!TryParse(sInput, vResult)) {
108 throw FormatException("Input string was not in a correct format.");
109 }
110 return vResult;
111 }
112
113 bool Version::TryParse(const String& sInput, Version& vResult) {
114 int iMajor = 0, iMinor = 0, iBuild = 0, iRevision = 0, iCount = 0;
115 if (!TryParseInternal(sInput, iMajor, iMinor, iBuild, iRevision, iCount)) {
116 return false;
117 }
118 if (iCount == 2) vResult = Version(iMajor, iMinor);
119 else if (iCount == 3) vResult = Version(iMajor, iMinor, iBuild);
120 else vResult = Version(iMajor, iMinor, iBuild, iRevision);
121 return true;
122 }
123
124 bool Version::operator==(const Version& vOther) const {
125 return m_iMajor == vOther.m_iMajor &&
126 m_iMinor == vOther.m_iMinor &&
127 m_iBuild == vOther.m_iBuild &&
128 m_iRevision == vOther.m_iRevision;
129 }
130
131 bool Version::operator!=(const Version& other) const {
132 return !(*this == other);
133 }
134
135 }
136}
Defines the exception thrown when an invalid argument is provided to a method.
Defines the exception thrown when the format of an argument is invalid or not compliant with specific...
Represents the version number of an assembly, operating system, or application component.
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
FormatException(const String &sMessage)
Initializes a new instance of the FormatException class with a specified error message.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
const char * GetRawString() const
Definition String.cpp:230
int GetMajor() const
Gets the value of the major component of the version number.
Definition Version.cpp:80
bool operator==(const Version &vOther) const
Determines whether two Version instances are equal.
Definition Version.cpp:124
Version(int iMajor, int iMinor, int iBuild, int iRevision)
Initializes a new instance of the Version class using specified major, minor, build,...
Definition Version.cpp:68
int GetMinor() const
Gets the value of the minor component of the version number.
Definition Version.cpp:84
static bool TryParse(const String &sInput, Version &vResult)
Tries to convert the string representation of a version number to an equivalent Version object.
Definition Version.cpp:113
Version()
Initializes a new instance of the Version class to 0.0.0.0.
Definition Version.cpp:77
int GetRevision() const
Gets the value of the revision component of the version number.
Definition Version.cpp:92
static Version Parse(const String &sInput)
Converts the string representation of a version number to an equivalent Version object.
Definition Version.cpp:102
int GetBuild() const
Gets the value of the build component of the version number.
Definition Version.cpp:88
bool operator!=(const Version &vOther) const
Determines whether two Version instances are not equal.
Definition Version.cpp:131
String ToString() const
Converts the value of the current Version object to its equivalent String representation.
Definition Version.cpp:96
static bool TryParseInternal(const String &sInput, int &iMajor, int &iMinor, int &iBuild, int &iRevision, int &iCount)
Definition Version.cpp:50
static bool SplitVersionParts(const String &sInput, std::vector< std::string > &vecParts)
Definition Version.cpp:32
static bool ParseComponent(const std::string &sPart, int &iVal)
Definition Version.cpp:12