2010-03-18 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / corlib / System / Version.cs
1 //
2 // System.Version.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 using System.Runtime.InteropServices;
31
32 namespace System {
33
34         [Serializable]
35         [ComVisible (true)]
36         public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version> {
37                 int _Major, _Minor, _Build, _Revision;
38
39                 private const int UNDEFINED = -1;
40
41                 private void CheckedSet (int defined, int major, int minor, int build, int revision)
42                 {
43                         // defined should be 2, 3 or 4
44
45                         if (major < 0) {
46                                 throw new ArgumentOutOfRangeException ("major");
47                         }
48                         this._Major = major;
49
50                         if (minor < 0) {
51                                 throw new ArgumentOutOfRangeException ("minor");
52                         }
53                         this._Minor = minor;
54
55                         if (defined == 2) {
56                                 this._Build = UNDEFINED;
57                                 this._Revision = UNDEFINED;
58                                 return;
59                         }
60
61                         if (build < 0) {
62                                 throw new ArgumentOutOfRangeException ("build");
63                         }
64                         this._Build = build;
65
66                         if (defined == 3) {
67                                 this._Revision = UNDEFINED;
68                                 return;
69                         }
70
71                         if (revision < 0) {
72                                 throw new ArgumentOutOfRangeException ("revision");
73                         }
74                         this._Revision = revision;
75                 }
76
77                 public Version ()
78                 {
79                         CheckedSet (2, 0, 0, -1, -1);
80                 }
81
82                 public Version (string version)
83                 {
84                         int n;
85                         string [] vals;
86                         int major = -1, minor = -1, build = -1, revision = -1;
87
88                         if (version == null) {
89                                 throw new ArgumentNullException ("version");
90                         }
91
92                         vals = version.Split (new Char [] {'.'});
93                         n = vals.Length;
94
95                         if (n < 2 || n > 4) {
96                                 throw new ArgumentException (Locale.GetText
97                                         ("There must be 2, 3 or 4 components in the version string."));
98                         }
99
100                         if (n > 0)
101                                 major = int.Parse (vals [0]);
102                         if (n > 1)
103                                 minor = int.Parse (vals [1]);
104                         if (n > 2)
105                                 build = int.Parse (vals [2]);
106                         if (n > 3)
107                                 revision = int.Parse (vals [3]);
108
109                         CheckedSet (n, major, minor, build, revision);
110                 }
111
112                 public Version (int major, int minor)
113                 {
114                         CheckedSet (2, major, minor, 0, 0);
115                 }
116
117                 public Version (int major, int minor, int build)
118                 {
119                         CheckedSet (3, major, minor, build, 0);
120                 }
121
122                 public Version (int major, int minor, int build, int revision)
123                 {
124                         CheckedSet (4, major, minor, build, revision);
125                 }
126
127                 public int Build {
128                         get {
129                                 return _Build;
130                         }
131                 }
132
133                 public int Major {
134                         get {
135                                 return _Major;
136                         }
137                 }
138
139                 public int Minor {
140                         get {
141                                 return _Minor;
142                         }
143                 }
144
145                 public int Revision {
146                         get {
147                                 return _Revision;
148                         }
149                 }
150                 public short MajorRevision {
151                         get {
152                                 return (short)(_Revision >> 16);
153                         }
154                 }
155
156                 public short MinorRevision {
157                         get {
158                                 return (short)_Revision;
159                         }
160                 }
161
162                 public object Clone ()
163                 {
164                         if (_Build == -1)
165                                 return new Version (_Major, _Minor);
166                         else if (_Revision == -1)
167                                 return new Version (_Major, _Minor, _Build);
168                         else
169                                 return new Version (_Major, _Minor, _Build, _Revision);
170                 }
171
172                 public int CompareTo (object version)
173                 {
174                         if (version == null)
175                                 return 1;
176
177                         if (! (version is Version))
178                                 throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version."));
179
180                         return this.CompareTo ((Version) version);
181                 }
182
183                 public override bool Equals (object obj)
184                 {
185                         return this.Equals (obj as Version);
186                 }
187
188                 public int CompareTo (Version value)
189                 {
190                         if (value == null)
191                                 return 1;
192                         
193                         if (this._Major > value._Major)
194                                 return 1;
195                         else if (this._Major < value._Major)
196                                 return -1;
197
198                         if (this._Minor > value._Minor)
199                                 return 1;
200                         else if (this._Minor < value._Minor)
201                                 return -1;
202
203                         if (this._Build > value._Build)
204                                 return 1;
205                         else if (this._Build < value._Build)
206                                 return -1;
207
208                         if (this._Revision > value._Revision)
209                                 return 1;
210                         else if (this._Revision < value._Revision)
211                                 return -1;
212
213                         return 0;
214                 }
215
216                 public bool Equals (Version obj)
217                 {
218                         return ((obj != null) &&
219                             (obj._Major == _Major) &&
220                             (obj._Minor == _Minor) &&
221                             (obj._Build == _Build) &&
222                             (obj._Revision == _Revision));
223                 }
224
225                 public override int GetHashCode ()
226                 {
227                         return (_Revision << 24) | (_Build << 16) | (_Minor << 8) | _Major;
228                 }
229
230                 // <summary>
231                 //   Returns a stringified representation of the version, format:
232                 //   major.minor[.build[.revision]]
233                 // </summary>
234                 public override string ToString ()
235                 {
236                         string mm = _Major.ToString () + "." + _Minor.ToString ();
237                         
238                         if (_Build != UNDEFINED)
239                                 mm = mm + "." + _Build.ToString ();
240                         if (_Revision != UNDEFINED)
241                                 mm = mm + "." + _Revision.ToString ();
242
243                         return mm;
244                 }
245
246                 // <summary>
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.
250                 //
251                 //    ie, Version a = new Version (1, 2);  a.ToString (3) should
252                 //    throw the expcetion.
253                 // </summary>
254                 public string ToString (int fieldCount)
255                 {
256                         if (fieldCount == 0)
257                                 return String.Empty;
258                         if (fieldCount == 1)
259                                 return _Major.ToString ();
260                         if (fieldCount == 2)
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 () + "." +
267                                         _Build.ToString ();
268                         }
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 ();
275                         }
276                         throw new ArgumentException (Locale.GetText ("Invalid fieldCount parameter: ") + fieldCount.ToString());        
277                 }
278
279                 public static bool operator== (Version v1, Version v2) 
280                 {
281                         return Equals (v1, v2);
282                 }
283
284                 public static bool operator!= (Version v1, Version v2)
285                 {
286                         return !Equals (v1, v2);
287                 }
288
289                 public static bool operator> (Version v1, Version v2)
290                 {
291                         return v1.CompareTo (v2) > 0;
292                 }
293
294                 public static bool operator>= (Version v1, Version v2)
295                 {
296                         return v1.CompareTo (v2) >= 0;
297                 }
298
299                 public static bool operator< (Version v1, Version v2)
300                 {
301                         return v1.CompareTo (v2) < 0;
302                 }
303
304                 public static bool operator<= (Version v1, Version v2)
305                 {
306                         return v1.CompareTo (v2) <= 0;
307                 }
308
309 #if BOOSTRAP_NET_4_0 || NET_4_0
310                 public static Version Parse (string input)
311                 {
312                         // Exactly the same as calling Version(string) .ctor
313                         return new Version (input);
314                 }
315
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)
320                 {
321                         int n;
322                         string [] vals;
323                         int [] values; // actual parsed values
324
325                         result = null;
326
327                         if (input == null)
328                                 return false;
329
330                         vals = input.Split (new Char [] {'.'});
331                         n = vals.Length;
332
333                         if (n < 2 || n > 4)
334                                 return false;
335
336                         values = new int [n];
337                         for (int i = 0; i < n; i++) {
338                                 int part;
339                                 if (!Int32.TryParse (vals [i], out part) || part < 0)
340                                         return false;
341
342                                 values [i] = part;
343                         }
344
345                         result = new Version ();
346                         if (n > 0)
347                                 result._Major = values [0];
348                         if (n > 1)
349                                 result._Minor = values [1];
350                         if (n > 2)
351                                 result._Build = values [2];
352                         if (n > 3)
353                                 result._Revision = values [3];
354
355                         return true;
356                 }
357 #endif
358
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)
362                 {
363                         int major = 0;
364                         int minor = 0;
365                         int build = 0;
366                         int revision = 0;
367                         int state = 1;
368                         int number = UNDEFINED; // string may not begin with a digit
369
370                         if (info == null)
371                                 return new Version (0, 0, 0, 0);
372
373                         for (int i=0; i < info.Length; i++) {
374                                 char c = info [i];
375                                 if (Char.IsDigit (c)) {
376                                         if (number < 0) {
377                                                 number = (c - '0');
378                                         }
379                                         else {
380                                                 number = (number * 10) + (c - '0');
381                                         }
382                                 }
383                                 else if (number >= 0) {
384                                         // assign
385                                         switch (state) {
386                                         case 1:
387                                                 major = number;
388                                                 break;
389                                         case 2:
390                                                 minor = number;
391                                                 break;
392                                         case 3:
393                                                 build = number;
394                                                 break;
395                                         case 4:
396                                                 revision = number;
397                                                 break;
398                                         }
399                                         number = -1;
400                                         state ++;
401                                 }
402                                 // ignore end of string
403                                 if (state == 5)
404                                         break;
405                         }
406
407                         // Last number
408                         if (number >= 0) {
409                                 switch (state) {
410                                 case 1:
411                                         major = number;
412                                         break;
413                                 case 2:
414                                         minor = number;
415                                         break;
416                                 case 3:
417                                         build = number;
418                                         break;
419                                 case 4:
420                                         revision = number;
421                                         break;
422                                 }
423                         }
424                         return new Version (major, minor, build, revision);
425                 }
426         }
427 }