From f0e7ea6fd1793b6c4681834e7f6453fd264cc73e Mon Sep 17 00:00:00 2001 From: tresf Date: Thu, 8 Jan 2015 23:53:19 -0500 Subject: [PATCH] Expose major, minor, release and build values parsed from a project version --- include/ProjectVersion.h | 28 +++++++++++++++++-------- src/core/ProjectVersion.cpp | 41 ++++++++++--------------------------- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/include/ProjectVersion.h b/include/ProjectVersion.h index 7326ce3c3..adac28f7a 100644 --- a/include/ProjectVersion.h +++ b/include/ProjectVersion.h @@ -27,27 +27,39 @@ #define PROJECT_VERSION_H #include - +#include "export.h" class ProjectVersion : public QString { public: - ProjectVersion( const QString & _s ) : - QString( _s ) + ProjectVersion( const QString & s ) : + QString( s ), + major( section( '.', 0, 0 ).toInt() ) , + minor( section( '.', 1, 1 ).toInt() ) , + release( section( '.', 2 ).section( '-', 0, 0 ).toInt() ) , + build( section( '.', 2 ).section( '-', 1 ) ) { } - static int compare( const ProjectVersion & _v1, - const ProjectVersion & _v2 ); - + static int compare( const ProjectVersion & v1, + const ProjectVersion & v2 ); + const int majorVersion() const { return major; } + const int minorVersion() const { return minor; } + const int releaseVersion() const { return release; } + const QString buildVersion() const { return build; } +private: + const int major; + const int minor; + const int release; + const QString build; } ; -inline bool operator<( const ProjectVersion & _v1, const char * _str ) +inline bool operator<( const ProjectVersion & v1, const char * str ) { - return ProjectVersion::compare( _v1, ProjectVersion( _str ) ) < 0; + return ProjectVersion::compare( v1, ProjectVersion( str ) ) < 0; } diff --git a/src/core/ProjectVersion.cpp b/src/core/ProjectVersion.cpp index 58e5ac541..6d5b1bd53 100644 --- a/src/core/ProjectVersion.cpp +++ b/src/core/ProjectVersion.cpp @@ -29,54 +29,35 @@ #include "ProjectVersion.h" - - -int ProjectVersion::compare( const ProjectVersion & _v1, - const ProjectVersion & _v2 ) +int ProjectVersion::compare( const ProjectVersion & v1, + const ProjectVersion & v2 ) { - int n1, n2; - - // Major - n1 = _v1.section( '.', 0, 0 ).toInt(); - n2 = _v2.section( '.', 0, 0 ).toInt(); - if( n1 != n2 ) + if( v1.majorVersion() != v2.majorVersion() ) { - return n1 - n2; + return v1.majorVersion() - v2.majorVersion(); } - // Minor - n1 = _v1.section( '.', 1, 1 ).toInt(); - n2 = _v2.section( '.', 1, 1 ).toInt(); - if( n1 != n2 ) + if( v1.minorVersion() != v2.minorVersion() ) { - return n1 - n2; + return v1.minorVersion() - v2.minorVersion(); } - // Release - n1 = _v1.section( '.', 2 ).section( '-', 0, 0 ).toInt(); - n2 = _v2.section( '.', 2 ).section( '-', 0, 0 ).toInt(); - if( n1 != n2 ) + if( v1.releaseVersion() != v2.releaseVersion() ) { - return n1 - n2; + return v1.releaseVersion() - v2.releaseVersion(); } - // Build - const QString b1 = _v1.section( '.', 2 ).section( '-', 1 ); - const QString b2 = _v2.section( '.', 2 ).section( '-', 1 ); - // make sure 0.x.y > 0.x.y-patch - if( b1.isEmpty() ) + if( v1.buildVersion().isEmpty() ) { return 1; } - if( b2.isEmpty() ) + if( v2.buildVersion().isEmpty() ) { return -1; } - return QString::compare( b1, b2 ); + return QString::compare( v1.buildVersion(), v2.buildVersion() ); } - -