| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#if !defined( INCLUDED_VERSIONLIB_H ) |
| 23 |
#define INCLUDED_VERSIONLIB_H |
| 24 |
|
| 25 |
#include <cstddef> |
| 26 |
#include <string.h> |
| 27 |
#include <algorithm> |
| 28 |
|
| 29 |
class Version |
| 30 |
{ |
| 31 |
public: |
| 32 |
int major; |
| 33 |
int minor; |
| 34 |
}; |
| 35 |
|
| 36 |
inline bool operator<( const Version& version, const Version& other ){ |
| 37 |
return version.major < other.major || ( !( other.major < version.major ) && version.minor < other.minor ); |
| 38 |
} |
| 39 |
|
| 40 |
template<typename TextOutputStreamType> |
| 41 |
TextOutputStreamType& ostream_write( TextOutputStreamType& outputStream, const Version& version ){ |
| 42 |
return outputStream << version.major << '.' << version.minor; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
inline bool version_compatible( const Version& version, const Version& other ){ |
| 47 |
return version.major == other.major |
| 48 |
&& !( version.minor < other.minor ); |
| 49 |
} |
| 50 |
|
| 51 |
inline int string_range_parse_unsigned_decimal_integer( const char* first, const char* last ){ |
| 52 |
int result = 0; |
| 53 |
for (; first != last; ++first ) |
| 54 |
{ |
| 55 |
result *= 10; |
| 56 |
result += *first - '0'; |
| 57 |
} |
| 58 |
return result; |
| 59 |
} |
| 60 |
|
| 61 |
inline Version version_parse( const char* versionString ){ |
| 62 |
Version version; |
| 63 |
const char* endVersion = versionString + strlen( versionString ); |
| 64 |
|
| 65 |
const char* endMajor = strchr( versionString, '.' ); |
| 66 |
if ( endMajor == 0 ) { |
| 67 |
endMajor = endVersion; |
| 68 |
|
| 69 |
version.minor = 0; |
| 70 |
} |
| 71 |
else |
| 72 |
{ |
| 73 |
const char* endMinor = strchr( endMajor + 1, '.' ); |
| 74 |
if ( endMinor == 0 ) { |
| 75 |
endMinor = endVersion; |
| 76 |
} |
| 77 |
version.minor = string_range_parse_unsigned_decimal_integer( endMajor + 1, endMinor ); |
| 78 |
} |
| 79 |
version.major = string_range_parse_unsigned_decimal_integer( versionString, endMajor ); |
| 80 |
|
| 81 |
return version; |
| 82 |
} |
| 83 |
|
| 84 |
#endif |