123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- /*=============================================================================
- Library: CTK
- Copyright (c) German Cancer Research Center,
- Division of Medical and Biological Informatics
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =============================================================================*/
- #include "ctkVersion.h"
- #include "ctkException.h"
- #include <QStringListIterator>
- #include <QDebug>
- const QString ctkVersion::SEPARATOR = ".";
- const QRegExp ctkVersion::RegExp = QRegExp("[a-zA-Z0-9_\\-]*");
- //----------------------------------------------------------------------------
- ctkVersion ctkVersion::emptyVersion()
- {
- static ctkVersion emptyV(false);
- return emptyV;
- }
- //----------------------------------------------------------------------------
- ctkVersion ctkVersion::undefinedVersion()
- {
- static ctkVersion undefinedV(true);
- return undefinedV;
- }
- //----------------------------------------------------------------------------
- ctkVersion& ctkVersion::operator=(const ctkVersion& v)
- {
- majorVersion = v.majorVersion;
- minorVersion = v.minorVersion;
- microVersion = v.microVersion;
- qualifier = v.qualifier;
- undefined = v.undefined;
- return *this;
- }
- //----------------------------------------------------------------------------
- ctkVersion::ctkVersion(bool undefined)
- : majorVersion(0), minorVersion(0), microVersion(0), qualifier(""), undefined(undefined)
- {
- }
- //----------------------------------------------------------------------------
- void ctkVersion::validate()
- {
- if (!RegExp.exactMatch(qualifier))
- throw ctkInvalidArgumentException(QString("invalid qualifier: ") + qualifier);
- undefined = false;
- }
- //----------------------------------------------------------------------------
- ctkVersion::ctkVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion)
- : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(""),
- undefined(false)
- {
- }
- //----------------------------------------------------------------------------
- ctkVersion::ctkVersion(unsigned int majorVersion, unsigned int minorVersion, unsigned int microVersion, const QString& qualifier)
- : majorVersion(majorVersion), minorVersion(minorVersion), microVersion(microVersion), qualifier(qualifier),
- undefined(true)
- {
- this->validate();
- }
- //----------------------------------------------------------------------------
- ctkVersion::ctkVersion(const QString& version)
- : majorVersion(0), minorVersion(0), microVersion(0), undefined(true)
- {
- unsigned int maj = 0;
- unsigned int min = 0;
- unsigned int mic = 0;
- QString qual("");
- QStringList st = version.split(SEPARATOR);
- if (st.empty()) return;
- QStringListIterator i(st);
- bool ok = true;
- maj = i.next().toUInt(&ok);
- if (i.hasNext())
- {
- min = i.next().toUInt(&ok);
- if (i.hasNext())
- {
- mic = i.next().toUInt(&ok);
- if (i.hasNext())
- {
- qual = i.next();
- if (i.hasNext())
- {
- ok = false;
- }
- }
- }
- }
- if (!ok) throw ctkInvalidArgumentException("invalid format");
- majorVersion = maj;
- minorVersion = min;
- microVersion = mic;
- qualifier = qual;
- this->validate();
- }
- //----------------------------------------------------------------------------
- ctkVersion::ctkVersion(const ctkVersion& version)
- : majorVersion(version.majorVersion), minorVersion(version.minorVersion),
- microVersion(version.microVersion), qualifier(version.qualifier),
- undefined(version.undefined)
- {
- }
- //----------------------------------------------------------------------------
- ctkVersion ctkVersion::parseVersion(const QString& version)
- {
- if (version.isEmpty())
- {
- return emptyVersion();
- }
- QString version2 = version.trimmed();
- if (version2.isEmpty())
- {
- return emptyVersion();
- }
- return ctkVersion(version2);
- }
- //----------------------------------------------------------------------------
- bool ctkVersion::isUndefined() const
- {
- return undefined;
- }
- //----------------------------------------------------------------------------
- unsigned int ctkVersion::getMajor() const
- {
- if (undefined) throw ctkIllegalStateException("Version undefined");
- return majorVersion;
- }
- //----------------------------------------------------------------------------
- unsigned int ctkVersion::getMinor() const
- {
- if (undefined) throw ctkIllegalStateException("Version undefined");
- return minorVersion;
- }
- //----------------------------------------------------------------------------
- unsigned int ctkVersion::getMicro() const
- {
- if (undefined) throw ctkIllegalStateException("Version undefined");
- return microVersion;
- }
- //----------------------------------------------------------------------------
- QString ctkVersion::getQualifier() const
- {
- if (undefined) throw ctkIllegalStateException("Version undefined");
- return qualifier;
- }
- //----------------------------------------------------------------------------
- QString ctkVersion::toString() const
- {
- if (undefined) return "undefined";
- QString result;
- result += QString::number(majorVersion) + SEPARATOR + QString::number(minorVersion) + SEPARATOR + QString::number(microVersion);
- if (!qualifier.isEmpty())
- {
- result += SEPARATOR + qualifier;
- }
- return result;
- }
- //----------------------------------------------------------------------------
- bool ctkVersion::operator==(const ctkVersion& other) const
- {
- if (&other == this)
- { // quicktest
- return true;
- }
- if (other.undefined && this->undefined) return true;
- if (this->undefined) throw ctkIllegalStateException("Version undefined");
- if (other.undefined) return false;
- return (majorVersion == other.majorVersion) && (minorVersion == other.minorVersion) && (microVersion
- == other.microVersion) && qualifier == other.qualifier;
- }
- //----------------------------------------------------------------------------
- int ctkVersion::compare(const ctkVersion& other) const
- {
- if (&other == this)
- { // quicktest
- return 0;
- }
- if (this->undefined || other.undefined)
- throw ctkIllegalStateException("Cannot compare undefined version");
- if (majorVersion < other.majorVersion)
- {
- return -1;
- }
- if (majorVersion == other.majorVersion)
- {
- if (minorVersion < other.minorVersion)
- {
- return -1;
- }
- if (minorVersion == other.minorVersion)
- {
- if (microVersion < other.microVersion)
- {
- return -1;
- }
- if (microVersion == other.microVersion)
- {
- return qualifier.compare(other.qualifier);
- }
- }
- }
- return 1;
- }
- //----------------------------------------------------------------------------
- bool ctkVersion::operator <(const ctkVersion &object) const
- {
- return this->compare(object) < 0;
- }
- //----------------------------------------------------------------------------
- QDebug operator<<(QDebug dbg, const ctkVersion& v)
- {
- dbg << v.toString();
- return dbg.maybeSpace();
- }
|