5 // Miguel de Icaza (miguel@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
8 // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
37 public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version> {
39 public sealed class Version : ICloneable, IComparable {
41 int _Major, _Minor, _Build, _Revision;
43 private const int UNDEFINED = -1;
45 private void CheckedSet (int defined, int major, int minor, int build, int revision)
47 // defined should be 2, 3 or 4
50 throw new ArgumentOutOfRangeException ("major");
55 throw new ArgumentOutOfRangeException ("minor");
60 this._Build = UNDEFINED;
61 this._Revision = UNDEFINED;
66 throw new ArgumentOutOfRangeException ("build");
71 this._Revision = UNDEFINED;
76 throw new ArgumentOutOfRangeException ("revision");
78 this._Revision = revision;
83 CheckedSet (2, 0, 0, -1, -1);
86 public Version (string version)
90 int major = -1, minor = -1, build = -1, revision = -1;
92 if (version == null) {
93 throw new ArgumentNullException ("version");
96 vals = version.Split (new Char [] {'.'});
100 throw new ArgumentException (Locale.GetText
101 ("There must be 2, 3 or 4 components in the version string."));
105 major = int.Parse (vals [0]);
107 minor = int.Parse (vals [1]);
109 build = int.Parse (vals [2]);
111 revision = int.Parse (vals [3]);
113 CheckedSet (n, major, minor, build, revision);
116 public Version (int major, int minor)
118 CheckedSet (2, major, minor, 0, 0);
121 public Version (int major, int minor, int build)
123 CheckedSet (3, major, minor, build, 0);
126 public Version (int major, int minor, int build, int revision)
128 CheckedSet (4, major, minor, build, revision);
149 public int Revision {
155 public short MajorRevision {
157 return (short)(_Revision >> 16);
161 public short MinorRevision {
163 return (short)_Revision;
167 public object Clone ()
170 return new Version (_Major, _Minor);
171 else if (_Revision == -1)
172 return new Version (_Major, _Minor, _Build);
174 return new Version (_Major, _Minor, _Build, _Revision);
177 public int CompareTo (object version)
182 if (! (version is Version))
183 throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version."));
185 return this.CompareTo ((Version) version);
188 public override bool Equals (object obj)
190 return this.Equals (obj as Version);
196 int CompareTo (Version value)
201 if (this._Major > value._Major)
203 else if (this._Major < value._Major)
206 if (this._Minor > value._Minor)
208 else if (this._Minor < value._Minor)
211 if (this._Build > value._Build)
213 else if (this._Build < value._Build)
216 if (this._Revision > value._Revision)
218 else if (this._Revision < value._Revision)
227 bool Equals (Version obj)
229 return ((obj != null) &&
230 (obj._Major == _Major) &&
231 (obj._Minor == _Minor) &&
232 (obj._Build == _Build) &&
233 (obj._Revision == _Revision));
236 public override int GetHashCode ()
238 return (_Revision << 24) | (_Build << 16) | (_Minor << 8) | _Major;
242 // Returns a stringified representation of the version, format:
243 // major.minor[.build[.revision]]
245 public override string ToString ()
247 string mm = _Major.ToString () + "." + _Minor.ToString ();
249 if (_Build != UNDEFINED)
250 mm = mm + "." + _Build.ToString ();
251 if (_Revision != UNDEFINED)
252 mm = mm + "." + _Revision.ToString ();
258 // LAME: This API is lame, since there is no way of knowing
259 // how many fields a Version object has, it is unfair to throw
260 // an ArgumentException, but this is what the spec claims.
262 // ie, Version a = new Version (1, 2); a.ToString (3) should
263 // throw the expcetion.
265 public string ToString (int fieldCount)
270 return _Major.ToString ();
272 return _Major.ToString () + "." + _Minor.ToString ();
273 if (fieldCount == 3){
274 if (_Build == UNDEFINED)
275 throw new ArgumentException (Locale.GetText
276 ("fieldCount is larger than the number of components defined in this instance."));
277 return _Major.ToString () + "." + _Minor.ToString () + "." +
280 if (fieldCount == 4){
281 if (_Build == UNDEFINED || _Revision == UNDEFINED)
282 throw new ArgumentException (Locale.GetText
283 ("fieldCount is larger than the number of components defined in this instance."));
284 return _Major.ToString () + "." + _Minor.ToString () + "." +
285 _Build.ToString () + "." + _Revision.ToString ();
287 throw new ArgumentException (Locale.GetText ("Invalid fieldCount parameter: ") + fieldCount.ToString());
290 public static bool operator== (Version v1, Version v2)
292 return Equals (v1, v2);
295 public static bool operator!= (Version v1, Version v2)
297 return !Equals (v1, v2);
300 public static bool operator> (Version v1, Version v2)
302 return v1.CompareTo (v2) > 0;
305 public static bool operator>= (Version v1, Version v2)
307 return v1.CompareTo (v2) >= 0;
310 public static bool operator< (Version v1, Version v2)
312 return v1.CompareTo (v2) < 0;
315 public static bool operator<= (Version v1, Version v2)
317 return v1.CompareTo (v2) <= 0;
320 // a very gentle way to construct a Version object which takes
321 // the first four numbers in a string as the version
322 internal static Version CreateFromString (string info)
329 int number = UNDEFINED; // string may not begin with a digit
332 return new Version (0, 0, 0, 0);
334 for (int i=0; i < info.Length; i++) {
336 if (Char.IsDigit (c)) {
341 number = (number * 10) + (c - '0');
344 else if (number >= 0) {
363 // ignore end of string
385 return new Version (major, minor, build, revision);