2014-06-30 08:39:45 -07: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 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef nsVersionComparator_h__
|
|
|
|
#define nsVersionComparator_h__
|
|
|
|
|
|
|
|
#include "nscore.h"
|
2012-04-18 20:22:29 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL)
|
|
|
|
#include <wchar.h>
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
#endif
|
|
|
|
|
2012-06-07 14:55:46 -07: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;
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* On Windows, if your version strings are wide characters, you should use the
|
|
|
|
* mozilla::VersionW variant instead. The semantics of that class is the same
|
|
|
|
* as Version.
|
|
|
|
*/
|
|
|
|
|
2012-04-18 20:22:29 -07:00
|
|
|
namespace mozilla {
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
int32_t NS_COM_GLUE CompareVersions(const char* aStrA, const char* aStrB);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-21 08:41:42 -07:00
|
|
|
#ifdef XP_WIN
|
2014-06-26 18:35:39 -07:00
|
|
|
int32_t NS_COM_GLUE CompareVersions(const char16_t* aStrA, const char16_t* aStrB);
|
2012-04-18 13:31:59 -07:00
|
|
|
#endif
|
|
|
|
|
2012-04-18 20:22:29 -07:00
|
|
|
struct NS_COM_GLUE Version
|
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
Version(const char* aVersionString)
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
versionContent = strdup(aVersionString);
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* ReadContent() const
|
|
|
|
{
|
|
|
|
return versionContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
~Version()
|
|
|
|
{
|
|
|
|
free(versionContent);
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator<(const Version& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) == -1;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator<=(const Version& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) < 1;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator>(const Version& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) == 1;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator>=(const Version& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) > -1;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator==(const Version& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) == 0;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator!=(const Version& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContent, aRhs.ReadContent()) != 0;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
private:
|
2012-04-18 20:22:29 -07:00
|
|
|
char* versionContent;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
struct NS_COM_GLUE VersionW
|
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
VersionW(const char16_t* aVersionStringW)
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
versionContentW =
|
|
|
|
reinterpret_cast<char16_t*>(wcsdup(char16ptr_t(aVersionStringW)));
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t* ReadContentW() const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
|
|
|
return versionContentW;
|
|
|
|
}
|
|
|
|
|
|
|
|
~VersionW()
|
|
|
|
{
|
|
|
|
free(versionContentW);
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator<(const VersionW& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContentW, aRhs.ReadContentW()) == -1;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator<=(const VersionW& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContentW, aRhs.ReadContentW()) < 1;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator>(const VersionW& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContentW, aRhs.ReadContentW()) == 1;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator>=(const VersionW& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContentW, aRhs.ReadContentW()) > -1;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator==(const VersionW& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContentW, aRhs.ReadContentW()) == 0;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
2014-06-26 18:35:39 -07:00
|
|
|
bool operator!=(const VersionW& aRhs) const
|
2012-04-18 20:22:29 -07:00
|
|
|
{
|
2014-06-26 18:35:39 -07:00
|
|
|
return CompareVersions(versionContentW, aRhs.ReadContentW()) != 0;
|
2012-04-18 20:22:29 -07:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:35:39 -07:00
|
|
|
private:
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t* versionContentW;
|
2012-04-18 20:22:29 -07:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace mozilla
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#endif // nsVersionComparator_h__
|
2012-04-18 20:22:29 -07:00
|
|
|
|