nsVersionComparator.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsVersionComparator_h__
  6. #define nsVersionComparator_h__
  7. #include "nscore.h"
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL)
  12. #include <wchar.h>
  13. #include "nsStringGlue.h"
  14. #endif
  15. /**
  16. * In order to compare version numbers in Mozilla, you need to use the
  17. * mozilla::Version class. You can construct an object of this type by passing
  18. * in a string version number to the constructor. Objects of this type can be
  19. * compared using the standard comparison operators.
  20. *
  21. * For example, let's say that you want to make sure that a given version
  22. * number is not older than 15.a2. Here's how you would write a function to
  23. * do that.
  24. *
  25. * bool IsVersionValid(const char* version) {
  26. * return mozilla::Version("15.a2") <= mozilla::Version(version);
  27. * }
  28. *
  29. * Or, since Version's constructor is implicit, you can simplify this code:
  30. *
  31. * bool IsVersionValid(const char* version) {
  32. * return mozilla::Version("15.a2") <= version;
  33. * }
  34. *
  35. * On Windows, if your version strings are wide characters, you should use the
  36. * mozilla::VersionW variant instead. The semantics of that class is the same
  37. * as Version.
  38. */
  39. namespace mozilla {
  40. int32_t CompareVersions(const char* aStrA, const char* aStrB);
  41. #ifdef XP_WIN
  42. int32_t CompareVersions(const char16_t* aStrA, const char16_t* aStrB);
  43. #endif
  44. struct Version
  45. {
  46. explicit Version(const char* aVersionString)
  47. {
  48. versionContent = strdup(aVersionString);
  49. }
  50. const char* ReadContent() const
  51. {
  52. return versionContent;
  53. }
  54. ~Version()
  55. {
  56. free(versionContent);
  57. }
  58. bool operator<(const Version& aRhs) const
  59. {
  60. return CompareVersions(versionContent, aRhs.ReadContent()) == -1;
  61. }
  62. bool operator<=(const Version& aRhs) const
  63. {
  64. return CompareVersions(versionContent, aRhs.ReadContent()) < 1;
  65. }
  66. bool operator>(const Version& aRhs) const
  67. {
  68. return CompareVersions(versionContent, aRhs.ReadContent()) == 1;
  69. }
  70. bool operator>=(const Version& aRhs) const
  71. {
  72. return CompareVersions(versionContent, aRhs.ReadContent()) > -1;
  73. }
  74. bool operator==(const Version& aRhs) const
  75. {
  76. return CompareVersions(versionContent, aRhs.ReadContent()) == 0;
  77. }
  78. bool operator!=(const Version& aRhs) const
  79. {
  80. return CompareVersions(versionContent, aRhs.ReadContent()) != 0;
  81. }
  82. bool operator<(const char* aRhs) const
  83. {
  84. return CompareVersions(versionContent, aRhs) == -1;
  85. }
  86. bool operator<=(const char* aRhs) const
  87. {
  88. return CompareVersions(versionContent, aRhs) < 1;
  89. }
  90. bool operator>(const char* aRhs) const
  91. {
  92. return CompareVersions(versionContent, aRhs) == 1;
  93. }
  94. bool operator>=(const char* aRhs) const
  95. {
  96. return CompareVersions(versionContent, aRhs) > -1;
  97. }
  98. bool operator==(const char* aRhs) const
  99. {
  100. return CompareVersions(versionContent, aRhs) == 0;
  101. }
  102. bool operator!=(const char* aRhs) const
  103. {
  104. return CompareVersions(versionContent, aRhs) != 0;
  105. }
  106. private:
  107. char* versionContent;
  108. };
  109. #ifdef XP_WIN
  110. struct VersionW
  111. {
  112. VersionW(const char16_t* aVersionStringW)
  113. {
  114. versionContentW =
  115. reinterpret_cast<char16_t*>(wcsdup(char16ptr_t(aVersionStringW)));
  116. }
  117. const char16_t* ReadContentW() const
  118. {
  119. return versionContentW;
  120. }
  121. ~VersionW()
  122. {
  123. free(versionContentW);
  124. }
  125. bool operator<(const VersionW& aRhs) const
  126. {
  127. return CompareVersions(versionContentW, aRhs.ReadContentW()) == -1;
  128. }
  129. bool operator<=(const VersionW& aRhs) const
  130. {
  131. return CompareVersions(versionContentW, aRhs.ReadContentW()) < 1;
  132. }
  133. bool operator>(const VersionW& aRhs) const
  134. {
  135. return CompareVersions(versionContentW, aRhs.ReadContentW()) == 1;
  136. }
  137. bool operator>=(const VersionW& aRhs) const
  138. {
  139. return CompareVersions(versionContentW, aRhs.ReadContentW()) > -1;
  140. }
  141. bool operator==(const VersionW& aRhs) const
  142. {
  143. return CompareVersions(versionContentW, aRhs.ReadContentW()) == 0;
  144. }
  145. bool operator!=(const VersionW& aRhs) const
  146. {
  147. return CompareVersions(versionContentW, aRhs.ReadContentW()) != 0;
  148. }
  149. private:
  150. char16_t* versionContentW;
  151. };
  152. #endif
  153. } // namespace mozilla
  154. #endif // nsVersionComparator_h__