/src/mozilla-central/xpcom/base/nsVersionComparator.h
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
7 | | #ifndef nsVersionComparator_h__ |
8 | | #define nsVersionComparator_h__ |
9 | | |
10 | | #include "mozilla/Char16.h" |
11 | | #include "nscore.h" |
12 | | #include <stdlib.h> |
13 | | #include <string.h> |
14 | | #include <assert.h> |
15 | | #if defined(XP_WIN) && !defined(UPDATER_NO_STRING_GLUE_STL) |
16 | | #include <wchar.h> |
17 | | #include "nsString.h" |
18 | | #endif |
19 | | |
20 | | /** |
21 | | * In order to compare version numbers in Mozilla, you need to use the |
22 | | * mozilla::Version class. You can construct an object of this type by passing |
23 | | * in a string version number to the constructor. Objects of this type can be |
24 | | * compared using the standard comparison operators. |
25 | | * |
26 | | * For example, let's say that you want to make sure that a given version |
27 | | * number is not older than 15.a2. Here's how you would write a function to |
28 | | * do that. |
29 | | * |
30 | | * bool IsVersionValid(const char* version) { |
31 | | * return mozilla::Version("15.a2") <= mozilla::Version(version); |
32 | | * } |
33 | | * |
34 | | * Or, since Version's constructor is implicit, you can simplify this code: |
35 | | * |
36 | | * bool IsVersionValid(const char* version) { |
37 | | * return mozilla::Version("15.a2") <= version; |
38 | | * } |
39 | | * |
40 | | * On Windows, if your version strings are wide characters, you should use the |
41 | | * mozilla::VersionW variant instead. The semantics of that class is the same |
42 | | * as Version. |
43 | | */ |
44 | | |
45 | | namespace mozilla { |
46 | | |
47 | | int32_t CompareVersions(const char* aStrA, const char* aStrB); |
48 | | |
49 | | #ifdef XP_WIN |
50 | | int32_t CompareVersions(const char16_t* aStrA, const char16_t* aStrB); |
51 | | #endif |
52 | | |
53 | | struct Version |
54 | | { |
55 | | explicit Version(const char* aVersionString) |
56 | 144 | { |
57 | 144 | versionContent = strdup(aVersionString); |
58 | 144 | } |
59 | | |
60 | | const char* ReadContent() const |
61 | | { |
62 | | return versionContent; |
63 | | } |
64 | | |
65 | | ~Version() |
66 | 144 | { |
67 | 144 | free(versionContent); |
68 | 144 | } |
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()) < 1; |
77 | | } |
78 | | bool operator>(const Version& aRhs) const |
79 | | { |
80 | | return CompareVersions(versionContent, aRhs.ReadContent()) == 1; |
81 | | } |
82 | | bool operator>=(const Version& aRhs) const |
83 | | { |
84 | | return CompareVersions(versionContent, aRhs.ReadContent()) > -1; |
85 | | } |
86 | | bool operator==(const Version& aRhs) const |
87 | | { |
88 | | return CompareVersions(versionContent, aRhs.ReadContent()) == 0; |
89 | | } |
90 | | bool operator!=(const Version& aRhs) const |
91 | 0 | { |
92 | 0 | return CompareVersions(versionContent, aRhs.ReadContent()) != 0; |
93 | 0 | } |
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) < 1; |
101 | | } |
102 | | bool operator>(const char* aRhs) const |
103 | 3 | { |
104 | 3 | return CompareVersions(versionContent, aRhs) == 1; |
105 | 3 | } |
106 | | bool operator>=(const char* aRhs) const |
107 | 0 | { |
108 | 0 | return CompareVersions(versionContent, aRhs) > -1; |
109 | 0 | } |
110 | | bool operator==(const char* aRhs) const |
111 | 0 | { |
112 | 0 | return CompareVersions(versionContent, aRhs) == 0; |
113 | 0 | } |
114 | | bool operator!=(const char* aRhs) const |
115 | 0 | { |
116 | 0 | return CompareVersions(versionContent, aRhs) != 0; |
117 | 0 | } |
118 | | |
119 | | private: |
120 | | char* versionContent; |
121 | | }; |
122 | | |
123 | | #ifdef XP_WIN |
124 | | struct VersionW |
125 | | { |
126 | | explicit VersionW(const char16_t* aVersionStringW) |
127 | | { |
128 | | versionContentW = |
129 | | reinterpret_cast<char16_t*>(wcsdup(char16ptr_t(aVersionStringW))); |
130 | | } |
131 | | |
132 | | const char16_t* ReadContentW() const |
133 | | { |
134 | | return versionContentW; |
135 | | } |
136 | | |
137 | | ~VersionW() |
138 | | { |
139 | | free(versionContentW); |
140 | | } |
141 | | |
142 | | bool operator<(const VersionW& aRhs) const |
143 | | { |
144 | | return CompareVersions(versionContentW, aRhs.ReadContentW()) == -1; |
145 | | } |
146 | | bool operator<=(const VersionW& aRhs) const |
147 | | { |
148 | | return CompareVersions(versionContentW, aRhs.ReadContentW()) < 1; |
149 | | } |
150 | | bool operator>(const VersionW& aRhs) const |
151 | | { |
152 | | return CompareVersions(versionContentW, aRhs.ReadContentW()) == 1; |
153 | | } |
154 | | bool operator>=(const VersionW& aRhs) const |
155 | | { |
156 | | return CompareVersions(versionContentW, aRhs.ReadContentW()) > -1; |
157 | | } |
158 | | bool operator==(const VersionW& aRhs) const |
159 | | { |
160 | | return CompareVersions(versionContentW, aRhs.ReadContentW()) == 0; |
161 | | } |
162 | | bool operator!=(const VersionW& aRhs) const |
163 | | { |
164 | | return CompareVersions(versionContentW, aRhs.ReadContentW()) != 0; |
165 | | } |
166 | | |
167 | | private: |
168 | | char16_t* versionContentW; |
169 | | }; |
170 | | #endif |
171 | | |
172 | | } // namespace mozilla |
173 | | |
174 | | #endif // nsVersionComparator_h__ |
175 | | |