michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsVersionComparator_h__ michael@0: #define nsVersionComparator_h__ michael@0: michael@0: #include "nscore.h" michael@0: #include michael@0: #include michael@0: #include michael@0: #if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL) michael@0: #include michael@0: #include "nsStringGlue.h" michael@0: #endif michael@0: michael@0: /** michael@0: * In order to compare version numbers in Mozilla, you need to use the michael@0: * mozilla::Version class. You can construct an object of this type by passing michael@0: * in a string version number to the constructor. Objects of this type can be michael@0: * compared using the standard comparison operators. michael@0: * michael@0: * For example, let's say that you want to make sure that a given version michael@0: * number is not older than 15.a2. Here's how you would write a function to michael@0: * do that. michael@0: * michael@0: * bool IsVersionValid(const char* version) { michael@0: * return mozilla::Version("15.a2") <= mozilla::Version(version); michael@0: * } michael@0: * michael@0: * Or, since Version's constructor is implicit, you can simplify this code: michael@0: * michael@0: * bool IsVersionValid(const char* version) { michael@0: * return mozilla::Version("15.a2") <= version; michael@0: * } michael@0: * michael@0: * On Windows, if your version strings are wide characters, you should use the michael@0: * mozilla::VersionW variant instead. The semantics of that class is the same michael@0: * as Version. michael@0: */ michael@0: michael@0: namespace mozilla { michael@0: michael@0: int32_t NS_COM_GLUE michael@0: CompareVersions(const char *A, const char *B); michael@0: michael@0: #ifdef XP_WIN michael@0: int32_t NS_COM_GLUE michael@0: CompareVersions(const char16_t *A, const char16_t *B); michael@0: #endif michael@0: michael@0: struct NS_COM_GLUE Version michael@0: { michael@0: Version(const char* versionString) michael@0: { michael@0: versionContent = strdup(versionString); michael@0: } michael@0: michael@0: const char* ReadContent() const michael@0: { michael@0: return versionContent; michael@0: } michael@0: michael@0: ~Version() michael@0: { michael@0: free(versionContent); michael@0: } michael@0: michael@0: bool operator< (const Version& rhs) const michael@0: { michael@0: return CompareVersions(versionContent, rhs.ReadContent()) == -1; michael@0: } michael@0: bool operator<= (const Version& rhs) const michael@0: { michael@0: return CompareVersions(versionContent, rhs.ReadContent()) < 1; michael@0: } michael@0: bool operator> (const Version& rhs) const michael@0: { michael@0: return CompareVersions(versionContent, rhs.ReadContent()) == 1; michael@0: } michael@0: bool operator>= (const Version& rhs) const michael@0: { michael@0: return CompareVersions(versionContent, rhs.ReadContent()) > -1; michael@0: } michael@0: bool operator== (const Version& rhs) const michael@0: { michael@0: return CompareVersions(versionContent, rhs.ReadContent()) == 0; michael@0: } michael@0: bool operator!= (const Version& rhs) const michael@0: { michael@0: return CompareVersions(versionContent, rhs.ReadContent()) != 0; michael@0: } michael@0: michael@0: private: michael@0: char* versionContent; michael@0: }; michael@0: michael@0: #ifdef XP_WIN michael@0: struct NS_COM_GLUE VersionW michael@0: { michael@0: VersionW(const char16_t *versionStringW) michael@0: { michael@0: versionContentW = reinterpret_cast(wcsdup(char16ptr_t(versionStringW))); michael@0: } michael@0: michael@0: const char16_t* ReadContentW() const michael@0: { michael@0: return versionContentW; michael@0: } michael@0: michael@0: ~VersionW() michael@0: { michael@0: free(versionContentW); michael@0: } michael@0: michael@0: bool operator< (const VersionW& rhs) const michael@0: { michael@0: return CompareVersions(versionContentW, rhs.ReadContentW()) == -1; michael@0: } michael@0: bool operator<= (const VersionW& rhs) const michael@0: { michael@0: return CompareVersions(versionContentW, rhs.ReadContentW()) < 1; michael@0: } michael@0: bool operator> (const VersionW& rhs) const michael@0: { michael@0: return CompareVersions(versionContentW, rhs.ReadContentW()) == 1; michael@0: } michael@0: bool operator>= (const VersionW& rhs) const michael@0: { michael@0: return CompareVersions(versionContentW, rhs.ReadContentW()) > -1; michael@0: } michael@0: bool operator== (const VersionW& rhs) const michael@0: { michael@0: return CompareVersions(versionContentW, rhs.ReadContentW()) == 0; michael@0: } michael@0: bool operator!= (const VersionW& rhs) const michael@0: { michael@0: return CompareVersions(versionContentW, rhs.ReadContentW()) != 0; michael@0: } michael@0: michael@0: private: michael@0: char16_t* versionContentW; michael@0: }; michael@0: #endif michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif // nsVersionComparator_h__ michael@0: