2014-06-30 19:39:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-07-23 18:05:25 +04:00
|
|
|
|
2020-07-09 01:57:26 +03:00
|
|
|
// NB: This code may be used from non-XPCOM code, in particular, the
|
|
|
|
// standalone updater executable.
|
|
|
|
|
2005-07-23 18:05:25 +04:00
|
|
|
#ifndef nsVersionComparator_h__
|
|
|
|
#define nsVersionComparator_h__
|
|
|
|
|
2017-12-22 18:53:12 +03:00
|
|
|
#include "mozilla/Char16.h"
|
2012-04-19 07:22:29 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL)
|
|
|
|
# include <wchar.h>
|
2017-12-07 03:52:51 +03:00
|
|
|
# include "nsString.h"
|
2012-04-19 07:22:29 +04:00
|
|
|
#endif
|
|
|
|
|
2012-06-08 01:55:46 +04:00
|
|
|
/**
|
|
|
|
* In order to compare version numbers in Mozilla, you need to use the
|
|
|
|
* mozilla::Version class. You can construct an object of this type by passing
|
|
|
|
* in a string version number to the constructor. Objects of this type can be
|
|
|
|
* compared using the standard comparison operators.
|
|
|
|
*
|
|
|
|
* For example, let's say that you want to make sure that a given version
|
|
|
|
* number is not older than 15.a2. Here's how you would write a function to
|
|
|
|
* do that.
|
|
|
|
*
|
|
|
|
* bool IsVersionValid(const char* version) {
|
|
|
|
* return mozilla::Version("15.a2") <= mozilla::Version(version);
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* Or, since Version's constructor is implicit, you can simplify this code:
|
|
|
|
*
|
|
|
|
* bool IsVersionValid(const char* version) {
|
|
|
|
* return mozilla::Version("15.a2") <= version;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
|
2012-04-19 07:22:29 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2019-06-04 20:44:16 +03:00
|
|
|
/**
|
|
|
|
* Compares the version strings provided.
|
|
|
|
*
|
|
|
|
* Returns 0 if the versions match, < 0 if aStrB > aStrA and > 0 if
|
|
|
|
* aStrA > aStrB.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
int32_t CompareVersions(const char* aStrA, const char* aStrB);
|
2005-07-23 18:05:25 +04:00
|
|
|
|
2010-06-21 19:41:42 +04:00
|
|
|
#ifdef XP_WIN
|
2019-06-04 20:44:16 +03:00
|
|
|
/**
|
|
|
|
* As above but for wide character strings.
|
|
|
|
*/
|
2014-08-28 02:47:27 +04:00
|
|
|
int32_t CompareVersions(const char16_t* aStrA, const char16_t* aStrB);
|
2012-04-19 00:31:59 +04:00
|
|
|
#endif
|
|
|
|
|
2014-08-28 02:47:27 +04:00
|
|
|
struct Version {
|
2014-07-30 04:43:56 +04:00
|
|
|
explicit Version(const char* aVersionString) {
|
2014-06-27 05:35:39 +04:00
|
|
|
versionContent = strdup(aVersionString);
|
2012-04-19 07:22:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* ReadContent() const { return versionContent; }
|
|
|
|
|
|
|
|
~Version() { free(versionContent); }
|
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
bool operator<(const Version& aRhs) const {
|
2019-01-30 00:03:33 +03:00
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) < 0;
|
2012-04-19 07:22:29 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
bool operator<=(const Version& aRhs) const {
|
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) < 1;
|
2012-04-19 07:22:29 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
bool operator>(const Version& aRhs) const {
|
2019-01-30 00:03:33 +03:00
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) > 0;
|
2012-04-19 07:22:29 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
bool operator>=(const Version& aRhs) const {
|
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) > -1;
|
2012-04-19 07:22:29 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
bool operator==(const Version& aRhs) const {
|
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) == 0;
|
2012-04-19 07:22:29 +04:00
|
|
|
}
|
2014-06-27 05:35:39 +04:00
|
|
|
bool operator!=(const Version& aRhs) const {
|
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) != 0;
|
2012-04-19 07:22:29 +04:00
|
|
|
}
|
2014-07-30 04:43:56 +04:00
|
|
|
bool operator<(const char* aRhs) const {
|
2019-01-30 00:03:33 +03:00
|
|
|
return CompareVersions(versionContent, aRhs) < 0;
|
2014-07-30 04:43:56 +04:00
|
|
|
}
|
|
|
|
bool operator<=(const char* aRhs) const {
|
|
|
|
return CompareVersions(versionContent, aRhs) < 1;
|
|
|
|
}
|
|
|
|
bool operator>(const char* aRhs) const {
|
2019-01-30 00:03:33 +03:00
|
|
|
return CompareVersions(versionContent, aRhs) > 0;
|
2014-07-30 04:43:56 +04:00
|
|
|
}
|
|
|
|
bool operator>=(const char* aRhs) const {
|
|
|
|
return CompareVersions(versionContent, aRhs) > -1;
|
|
|
|
}
|
|
|
|
bool operator==(const char* aRhs) const {
|
|
|
|
return CompareVersions(versionContent, aRhs) == 0;
|
|
|
|
}
|
|
|
|
bool operator!=(const char* aRhs) const {
|
|
|
|
return CompareVersions(versionContent, aRhs) != 0;
|
|
|
|
}
|
2012-04-19 07:22:29 +04:00
|
|
|
|
2014-06-27 05:35:39 +04:00
|
|
|
private:
|
2012-04-19 07:22:29 +04:00
|
|
|
char* versionContent;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2005-07-23 18:05:25 +04:00
|
|
|
#endif // nsVersionComparator_h__
|