1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // NB: This code may be used from non-XPCOM code, in particular, the
8 // standalone updater executable.
10 #ifndef nsVersionComparator_h__
11 #define nsVersionComparator_h__
13 #include "mozilla/Char16.h"
17 #if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL)
19 # include "nsString.h"
23 * In order to compare version numbers in Mozilla, you need to use the
24 * mozilla::Version class. You can construct an object of this type by passing
25 * in a string version number to the constructor. Objects of this type can be
26 * compared using the standard comparison operators.
28 * For example, let's say that you want to make sure that a given version
29 * number is not older than 15.a2. Here's how you would write a function to
32 * bool IsVersionValid(const char* version) {
33 * return mozilla::Version("15.a2") <= mozilla::Version(version);
36 * Or, since Version's constructor is implicit, you can simplify this code:
38 * bool IsVersionValid(const char* version) {
39 * return mozilla::Version("15.a2") <= version;
46 * Compares the version strings provided.
48 * Returns 0 if the versions match, < 0 if aStrB > aStrA and > 0 if
51 int32_t CompareVersions(const char* aStrA
, const char* aStrB
);
55 * As above but for wide character strings.
57 int32_t CompareVersions(const char16_t
* aStrA
, const char16_t
* aStrB
);
61 explicit Version(const char* aVersionString
) {
62 versionContent
= strdup(aVersionString
);
65 const char* ReadContent() const { return versionContent
; }
67 ~Version() { free(versionContent
); }
69 bool operator<(const Version
& aRhs
) const {
70 return CompareVersions(versionContent
, aRhs
.ReadContent()) < 0;
72 bool operator<=(const Version
& aRhs
) const {
73 return CompareVersions(versionContent
, aRhs
.ReadContent()) < 1;
75 bool operator>(const Version
& aRhs
) const {
76 return CompareVersions(versionContent
, aRhs
.ReadContent()) > 0;
78 bool operator>=(const Version
& aRhs
) const {
79 return CompareVersions(versionContent
, aRhs
.ReadContent()) > -1;
81 bool operator==(const Version
& aRhs
) const {
82 return CompareVersions(versionContent
, aRhs
.ReadContent()) == 0;
84 bool operator!=(const Version
& aRhs
) const {
85 return CompareVersions(versionContent
, aRhs
.ReadContent()) != 0;
87 bool operator<(const char* aRhs
) const {
88 return CompareVersions(versionContent
, aRhs
) < 0;
90 bool operator<=(const char* aRhs
) const {
91 return CompareVersions(versionContent
, aRhs
) < 1;
93 bool operator>(const char* aRhs
) const {
94 return CompareVersions(versionContent
, aRhs
) > 0;
96 bool operator>=(const char* aRhs
) const {
97 return CompareVersions(versionContent
, aRhs
) > -1;
99 bool operator==(const char* aRhs
) const {
100 return CompareVersions(versionContent
, aRhs
) == 0;
102 bool operator!=(const char* aRhs
) const {
103 return CompareVersions(versionContent
, aRhs
) != 0;
107 char* versionContent
;
110 } // namespace mozilla
112 #endif // nsVersionComparator_h__