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