Following example will demonstrates comparison between following version format:
major.minor.revision.build
or any shorter version, like only major
while it allows you to extend it to fit your needs as of,
"some of the version numbers may be very long like 2.3.2.2.3.1.1.5.3.5.6.2"
Using example bellow, dots in the beginning and end of the version string are taken care of as far .0.4
is considered to be equal to 0.0.4
and .1.
is considered to be equal to 0.1.0
.
CompareVersion.h
#ifndef COMPAREVERSION_H_
#define COMPAREVERSION_H_
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
struct CompareVersion {
public:
int maj;
int min;
int rev;
int build;
CompareVersion(std::string);
bool operator < (const CompareVersion&);
bool operator <= (const CompareVersion&);
bool operator == (const CompareVersion&);
friend std::ostream& operator << (std::ostream& stream, const CompareVersion& ver) {
stream << ver.maj;
stream << '.';
stream << ver.min;
stream << '.';
stream << ver.rev;
stream << '.';
stream << ver.build;
return stream;
};
void reset();
};
#endif /* COMPAREVERSION_H_ */
CompareVersion.cpp
#include "CompareVersion.h"
CompareVersion::CompareVersion(std::string version)
{
reset();
if (version.compare(0,1,".") == 0)
version = "0"+version;
if (version.compare(version.size()-1,1,".") == 0)
version.append("0");
sscanf(version.c_str(), "%d.%d.%d.%d", &maj, &min, &rev, &build);
if (maj <= 0) maj = 0;
if (min <= 0) min = 0;
if (rev <= 0) rev = 0;
if (build <= 0) build = 0;
}
bool CompareVersion::operator < (const CompareVersion& other)
{
if (maj < other.maj) return true;
if (min < other.min) return true;
if (rev < other.rev) return true;
if (build < other.build) return true;
return false;
}
bool CompareVersion::operator <= (const CompareVersion& other)
{
if (maj >= other.maj) return true;
if (min >= other.min) return true;
if (rev >= other.rev) return true;
if (build >= other.build) return true;
return false;
}
bool CompareVersion::operator == (const CompareVersion& other)
{
return maj == other.maj
&& min == other.min
&& rev == other.rev
&& build == other.build;
}
void CompareVersion::reset()
{
maj = 0;
min = 0;
rev = 0;
build = 0;
}
main.cpp
#include <iostream>
#include "CompareVersion.h"
using namespace std;
int main()
{
if((CompareVersion("1.2.3.4") == CompareVersion("1.2.3.4")) == true)
cout << "Version 1.2.3.4 and version 1.2.3.4 are equal" << endl;
if((CompareVersion("1.2.3.3") < CompareVersion("1.2.3.4")) == true)
cout << "Version 1.2.3.3 is smaller than 1.2.3.4. " << endl;
if((CompareVersion("1.2.3.4") < CompareVersion("1.2.3.4")) == true)
cout << "You won't see that. " << endl;
if((CompareVersion("1.2.3.4") <= CompareVersion("1.2.3.4")) == true)
cout << "Version 1.2.3.4 is smaller or equal to 1.2.3.4" << endl;
if((CompareVersion("1") <= CompareVersion("1.0.0.1")) == true)
cout << "Version 1 is smaller or equal to 1.0.0.1" << endl;
/* THE DOTS */
if((CompareVersion(".0.4") == CompareVersion("0.0.4")) == true)
cout << "Version .0.4 and version 0.0.4 are equal" << endl;
if((CompareVersion(".1.") == CompareVersion("0.1.0")) == true)
cout << "Version .1. and version 0.1.0 are equal" << endl;
if((CompareVersion("1") == CompareVersion("1.0.0.0")) == true)
cout << "Version 1 and version 1.0.0.0 are equal" << endl;
return 0;
}
Output
Version 1.2.3.4 and version 1.2.3.4 are equal
Version 1.2.3.3 is smaller than 1.2.3.4.
Version 1.2.3.4 is smaller or equal to 1.2.3.4
Version 1 is smaller or equal to 1.0.0.1
Version .0.4 and version 0.0.4 are equal
Version .1. and version 0.1.0 are equal
Version 1 and version 1.0.0.0 are equal