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;
36 public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version> {
37 int _Major, _Minor, _Build, _Revision;
39 private const int UNDEFINED = -1;
41 private void CheckedSet (int defined, int major, int minor, int build, int revision)
43 // defined should be 2, 3 or 4
46 throw new ArgumentOutOfRangeException ("major");
51 throw new ArgumentOutOfRangeException ("minor");
56 this._Build = UNDEFINED;
57 this._Revision = UNDEFINED;
62 throw new ArgumentOutOfRangeException ("build");
67 this._Revision = UNDEFINED;
72 throw new ArgumentOutOfRangeException ("revision");
74 this._Revision = revision;
79 CheckedSet (2, 0, 0, -1, -1);
82 public Version (string version)
86 int major = -1, minor = -1, build = -1, revision = -1;
88 if (version == null) {
89 throw new ArgumentNullException ("version");
92 vals = version.Split (new Char [] {'.'});
96 throw new ArgumentException (Locale.GetText
97 ("There must be 2, 3 or 4 components in the version string."));
101 major = int.Parse (vals [0]);
103 minor = int.Parse (vals [1]);
105 build = int.Parse (vals [2]);
107 revision = int.Parse (vals [3]);
109 CheckedSet (n, major, minor, build, revision);
112 public Version (int major, int minor)
114 CheckedSet (2, major, minor, 0, 0);
117 public Version (int major, int minor, int build)
119 CheckedSet (3, major, minor, build, 0);
122 public Version (int major, int minor, int build, int revision)
124 CheckedSet (4, major, minor, build, revision);
145 public int Revision {
150 public short MajorRevision {
152 return (short)(_Revision >> 16);
156 public short MinorRevision {
158 return (short)_Revision;
162 public object Clone ()
165 return new Version (_Major, _Minor);
166 else if (_Revision == -1)
167 return new Version (_Major, _Minor, _Build);
169 return new Version (_Major, _Minor, _Build, _Revision);
172 public int CompareTo (object version)
177 if (! (version is Version))
178 throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version."));
180 return this.CompareTo ((Version) version);
183 public override bool Equals (object obj)
185 return this.Equals (obj as Version);
188 public int CompareTo (Version value)
193 if (this._Major > value._Major)
195 else if (this._Major < value._Major)
198 if (this._Minor > value._Minor)
200 else if (this._Minor < value._Minor)
203 if (this._Build > value._Build)
205 else if (this._Build < value._Build)
208 if (this._Revision > value._Revision)
210 else if (this._Revision < value._Revision)
216 public bool Equals (Version obj)
218 return ((obj != null) &&
219 (obj._Major == _Major) &&
220 (obj._Minor == _Minor) &&
221 (obj._Build == _Build) &&
222 (obj._Revision == _Revision));
225 public override int GetHashCode ()
227 return (_Revision << 24) | (_Build << 16) | (_Minor << 8) | _Major;
231 // Returns a stringified representation of the version, format:
232 // major.minor[.build[.revision]]
234 public override string ToString ()
236 string mm = _Major.ToString () + "." + _Minor.ToString ();
238 if (_Build != UNDEFINED)
239 mm = mm + "." + _Build.ToString ();
240 if (_Revision != UNDEFINED)
241 mm = mm + "." + _Revision.ToString ();
247 // LAME: This API is lame, since there is no way of knowing
248 // how many fields a Version object has, it is unfair to throw
249 // an ArgumentException, but this is what the spec claims.
251 // ie, Version a = new Version (1, 2); a.ToString (3) should
252 // throw the expcetion.
254 public string ToString (int fieldCount)
259 return _Major.ToString ();
261 return _Major.ToString () + "." + _Minor.ToString ();
262 if (fieldCount == 3){
263 if (_Build == UNDEFINED)
264 throw new ArgumentException (Locale.GetText
265 ("fieldCount is larger than the number of components defined in this instance."));
266 return _Major.ToString () + "." + _Minor.ToString () + "." +
269 if (fieldCount == 4){
270 if (_Build == UNDEFINED || _Revision == UNDEFINED)
271 throw new ArgumentException (Locale.GetText
272 ("fieldCount is larger than the number of components defined in this instance."));
273 return _Major.ToString () + "." + _Minor.ToString () + "." +
274 _Build.ToString () + "." + _Revision.ToString ();
276 throw new ArgumentException (Locale.GetText ("Invalid fieldCount parameter: ") + fieldCount.ToString());
279 public static bool operator== (Version v1, Version v2)
281 return Equals (v1, v2);
284 public static bool operator!= (Version v1, Version v2)
286 return !Equals (v1, v2);
289 public static bool operator> (Version v1, Version v2)
291 return v1.CompareTo (v2) > 0;
294 public static bool operator>= (Version v1, Version v2)
296 return v1.CompareTo (v2) >= 0;
299 public static bool operator< (Version v1, Version v2)
301 return v1.CompareTo (v2) < 0;
304 public static bool operator<= (Version v1, Version v2)
306 return v1.CompareTo (v2) <= 0;
309 #if BOOSTRAP_NET_4_0 || NET_4_0
310 public static Version Parse (string input)
312 // Exactly the same as calling Version(string) .ctor
313 return new Version (input);
316 // Implemented the TryParse separated from the Parse impl due to its
317 // small size, and because the Parse one depends on the exceptions thrown by
318 // Int32.Parse to match .Net, and detecting such exceptions here is not worth it.
319 public static bool TryParse (string input, out Version result)
323 int [] values; // actual parsed values
330 vals = input.Split (new Char [] {'.'});
336 values = new int [n];
337 for (int i = 0; i < n; i++) {
339 if (!Int32.TryParse (vals [i], out part) || part < 0)
345 result = new Version ();
347 result._Major = values [0];
349 result._Minor = values [1];
351 result._Build = values [2];
353 result._Revision = values [3];
359 // a very gentle way to construct a Version object which takes
360 // the first four numbers in a string as the version
361 internal static Version CreateFromString (string info)
368 int number = UNDEFINED; // string may not begin with a digit
371 return new Version (0, 0, 0, 0);
373 for (int i=0; i < info.Length; i++) {
375 if (Char.IsDigit (c)) {
380 number = (number * 10) + (c - '0');
383 else if (number >= 0) {
402 // ignore end of string
424 return new Version (major, minor, build, revision);