ctkVersion.cpp

Go to the documentation of this file.
00001 /*=============================================================================
00002 
00003   Library: CTK
00004 
00005   Copyright (c) 2010 German Cancer Research Center,
00006     Division of Medical and Biological Informatics
00007 
00008   Licensed under the Apache License, Version 2.0 (the "License");
00009   you may not use this file except in compliance with the License.
00010   You may obtain a copy of the License at
00011 
00012     http://www.apache.org/licenses/LICENSE-2.0
00013 
00014   Unless required by applicable law or agreed to in writing, software
00015   distributed under the License is distributed on an "AS IS" BASIS,
00016   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00017   See the License for the specific language governing permissions and
00018   limitations under the License.
00019 
00020 =============================================================================*/
00021 
00022 #include "ctkVersion.h"
00023 
00024 #include <stdexcept>
00025 
00026 #include <QStringListIterator>
00027 #include <QDebug>
00028 
00029 
00030 const QString ctkVersion::SEPARATOR = ".";
00031 const QRegExp ctkVersion::RegExp = QRegExp("[a-zA-Z0-9_\\-]*");
00032 
00033 const ctkVersion& ctkVersion::emptyVersion()
00034 {
00035   static ctkVersion emptyV;
00036   return emptyV;
00037 }
00038 
00039 ctkVersion& ctkVersion::operator=(const ctkVersion& v)
00040 {
00041   majorVersion = v.majorVersion;
00042   minorVersion = v.minorVersion;
00043   microVersion = v.microVersion;
00044   qualifier = v.qualifier;
00045   return *this;
00046 }
00047 
00048 ctkVersion::ctkVersion()
00049   : majorVersion(0), minorVersion(0), microVersion(0), qualifier("")
00050 {
00051 
00052 }
00053 
00054 
00055 void ctkVersion::validate()
00056 {
00057   if (!RegExp.exactMatch(qualifier))
00058     throw std::invalid_argument(std::string("invalid qualifier: ") + qualifier.toStdString());
00059 }
00060 
00061 ctkVersion::ctkVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion)
00062   : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier("")
00063 {
00064 
00065 }
00066 
00067 ctkVersion::ctkVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion, const QString& qualifier)
00068    : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(qualifier)
00069 {
00070   this->validate();
00071 }
00072 
00073 ctkVersion::ctkVersion(const QString& version)
00074   : majorVersion(0), minorVersion(0), microVersion(0)
00075 {
00076   unsigned int maj = 0;
00077   unsigned int min = 0;
00078   unsigned int mic = 0;
00079   QString qual("");
00080 
00081   QStringList st = version.split(SEPARATOR);
00082 
00083   if (st.empty()) return;
00084 
00085   QStringListIterator i(st);
00086 
00087   bool ok = true;
00088   maj = i.next().toUInt(&ok);
00089 
00090   if (i.hasNext())
00091   {
00092     min = i.next().toUInt(&ok);
00093     if (i.hasNext())
00094     {
00095       mic = i.next().toUInt(&ok);
00096       if (i.hasNext())
00097       {
00098         qual = i.next();
00099         if (i.hasNext())
00100         {
00101            ok = false;
00102         }
00103       }
00104     }
00105   }
00106 
00107   if (!ok) throw std::invalid_argument("invalid format");
00108 
00109   majorVersion = maj;
00110   minorVersion = min;
00111   microVersion = mic;
00112   qualifier = qual;
00113   this->validate();
00114 }
00115 
00116 ctkVersion::ctkVersion(const ctkVersion& version)
00117 : majorVersion(version.majorVersion), minorVersion(version.minorVersion),
00118   microVersion(version.microVersion), qualifier(version.qualifier)
00119 {
00120 
00121 }
00122 
00123 ctkVersion ctkVersion::parseVersion(const QString& version)
00124 {
00125   if (version.isEmpty())
00126   {
00127     return emptyVersion();
00128   }
00129 
00130   QString version2 = version.trimmed();
00131   if (version2.isEmpty())
00132   {
00133     return emptyVersion();
00134   }
00135 
00136   return ctkVersion(version2);
00137 }
00138 
00139 unsigned int ctkVersion::getMajor() const
00140 {
00141   return majorVersion;
00142 }
00143 
00144 unsigned int ctkVersion::getMinor() const
00145 {
00146   return minorVersion;
00147 }
00148 
00149 unsigned int ctkVersion::getMicro() const
00150 {
00151   return microVersion;
00152 }
00153 
00154 QString ctkVersion::getQualifier() const
00155 {
00156   return qualifier;
00157 }
00158 
00159 QString ctkVersion::toString() const
00160 {
00161   QString result;
00162   result += QString::number(majorVersion) + SEPARATOR + QString::number(minorVersion) + SEPARATOR + QString::number(microVersion);
00163   if (!qualifier.isEmpty())
00164   {
00165     result += SEPARATOR + qualifier;
00166   }
00167   return result;
00168 }
00169 
00170 bool ctkVersion::operator==(const ctkVersion& other) const
00171 {
00172   if (&other == this)
00173   { // quicktest
00174     return true;
00175   }
00176 
00177   return (majorVersion == other.majorVersion) && (minorVersion == other.minorVersion) && (microVersion
00178       == other.microVersion) && qualifier == other.qualifier;
00179 }
00180 
00181 int ctkVersion::compare(const ctkVersion& other) const
00182 {
00183   if (&other == this)
00184   { // quicktest
00185     return 0;
00186   }
00187 
00188   if (majorVersion < other.majorVersion)
00189   {
00190     return -1;
00191   }
00192 
00193   if (majorVersion == other.majorVersion)
00194   {
00195 
00196     if (minorVersion < other.minorVersion)
00197     {
00198       return -1;
00199     }
00200 
00201     if (minorVersion == other.minorVersion)
00202     {
00203 
00204       if (microVersion < other.microVersion)
00205       {
00206         return -1;
00207       }
00208 
00209       if (microVersion == other.microVersion)
00210       {
00211         return qualifier.compare(other.qualifier);
00212       }
00213     }
00214   }
00215   return 1;
00216 }
00217 
00218 
00219 QDebug operator<<(QDebug dbg, const ctkVersion& v)
00220 {
00221   dbg << v.toString();
00222 
00223   return dbg.maybeSpace();
00224 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated on 21 May 2010 for CTK by  doxygen 1.6.1