-
Notifications
You must be signed in to change notification settings - Fork 21
/
Version.cpp
143 lines (125 loc) · 4.26 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
//#############################################################################
//
// FILENAME: Version.cpp
//
// CLASSIFICATION: Unclassified
//
// DESCRIPTION:
// This file provides implementation for methods declared in the
// Version class.
//
// SOFTWARE HISTORY:
// Date Author Comment
// ----------- ------ -------
// 19-Apr-2012 SCM Initial creation
// 30-Oct-2012 SCM Renamed to Version.cpp
// 17-Dec-2012 BAH Documentation updates.
// 10-Sep-2013 SCM Added std namespace to atoi() calls.
//
// NOTES:
// Refer to Version.h for more information.
//
//#############################################################################
#define CSM_LIBRARY
#include "Version.h"
#include "Error.h"
#include <ostream>
#include <cstdlib>
namespace csm
{
//*****************************************************************************
// Version::Version
//*****************************************************************************
Version::Version(const std::string& version)
:
theVersions()
{
static const char* const MODULE = "csm::Version::Version";
// split on periods
std::string::size_type pos = 0;
std::string::size_type dotPos = 0;
while ( (dotPos = version.find('.', pos)) != std::string::npos )
{
const std::string v = version.substr(pos, dotPos - pos);
if (v.find_first_not_of("1234567890") != std::string::npos)
{
throw Error(Error::INVALID_USE, "Invalid version component: " + v,
MODULE);
}
theVersions.push_back(std::atoi(v.c_str()));
pos = dotPos + 1;
}
// add the final version
const std::string v = version.substr(pos);
if (v.find_first_not_of("1234567890") != std::string::npos)
{
throw Error(Error::INVALID_USE, "Invalid version component: " + v,
MODULE);
}
theVersions.push_back(std::atoi(v.c_str()));
}
//*****************************************************************************
// Version::Version
//*****************************************************************************
Version::Version(int major, int minor, int revision)
:
theVersions()
{
static const char* const MODULE = "csm::Version::Version";
if (major < 0)
{
throw Error(Error::BOUNDS, "Invalid major version number", MODULE);
}
if (minor < 0)
{
throw Error(Error::BOUNDS, "Invalid minor version number", MODULE);
}
theVersions.push_back(major);
theVersions.push_back(minor);
if (revision >= 0) theVersions.push_back(revision);
}
//*****************************************************************************
// Version::~Version
//*****************************************************************************
Version::~Version()
{
}
//*****************************************************************************
// Version::print
//*****************************************************************************
std::ostream& Version::print(std::ostream& os) const
{
for(std::vector<int>::const_iterator it = theVersions.begin();
it != theVersions.end(); ++it)
{
if (it != theVersions.begin()) os << ".";
os << *it;
}
return os;
}
//*****************************************************************************
// Version::operator==
//*****************************************************************************
bool Version::operator==(const Version& rhs)
{
return (theVersions == rhs.theVersions);
}
//*****************************************************************************
// Version::operator<
//*****************************************************************************
bool Version::operator<(const Version& rhs)
{
for(size_t i = 0; i < theVersions.size() && i < rhs.theVersions.size(); ++i)
{
if (theVersions[i] < rhs.theVersions[i]) return true;
if (theVersions[i] > rhs.theVersions[i]) return false;
// must be equal, move to next value
}
//***
// Equal up until one version was less than the other, so if our vector has
// fewer elements than the RHS vector, then we are less. Otherwise, we are
// not less (note: that might mean we're equal, but that still means false)
//***
return (theVersions.size() < rhs.theVersions.size());
}
} // namespace csm