versionlib.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #if !defined(INCLUDED_VERSIONLIB_H)
00023 #define INCLUDED_VERSIONLIB_H
00024
00025 #include <cstddef>
00026 #include <string.h>
00027 #include <algorithm>
00028
00029 class Version
00030 {
00031 public:
00032 int major;
00033 int minor;
00034 };
00035
00036 inline bool operator< (const Version& version, const Version& other)
00037 {
00038 return version.major < other.major || (!(other.major < version.major) && version.minor < other.minor);
00039 }
00040
00042 inline bool version_compatible (const Version& version, const Version& other)
00043 {
00044 return version.major == other.major
00045 && !(version.minor < other.minor);
00046 }
00047
00048 inline int string_range_parse_unsigned_decimal_integer (const char* first, const char* last)
00049 {
00050 int result = 0;
00051 for (; first != last; ++first) {
00052 result *= 10;
00053 result += *first - '0';
00054 }
00055 return result;
00056 }
00057
00058 inline Version version_parse (const std::string& versionString)
00059 {
00060 Version version;
00061 const char* endVersion = versionString.c_str() + versionString.length();
00062
00063 const char* endMajor = strchr(versionString.c_str(), '.');
00064 if (endMajor == 0) {
00065 endMajor = endVersion;
00066
00067 version.minor = 0;
00068 } else {
00069 const char* endMinor = strchr(endMajor + 1, '.');
00070 if (endMinor == 0) {
00071 endMinor = endVersion;
00072 }
00073 version.minor = string_range_parse_unsigned_decimal_integer(endMajor + 1, endMinor);
00074 }
00075 version.major = string_range_parse_unsigned_decimal_integer(versionString.c_str(), endMajor);
00076
00077 return version;
00078 }
00079
00080 #endif