2008-08-27 Miguel de Icaza <miguel@novell.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 #if NET_2_0
36         [ComVisible (true)]
37         public sealed class Version : ICloneable, IComparable, IComparable<Version>, IEquatable<Version> {
38 #else
39         public sealed class Version : ICloneable, IComparable {
40 #endif
41                 int _Major, _Minor, _Build, _Revision;
42
43                 private const int UNDEFINED = -1;
44
45                 private void CheckedSet (int defined, int major, int minor, int build, int revision)
46                 {
47                         // defined should be 2, 3 or 4
48
49                         if (major < 0) {
50                                 throw new ArgumentOutOfRangeException ("major");
51                         }
52                         this._Major = major;
53
54                         if (minor < 0) {
55                                 throw new ArgumentOutOfRangeException ("minor");
56                         }
57                         this._Minor = minor;
58
59                         if (defined == 2) {
60                                 this._Build = UNDEFINED;
61                                 this._Revision = UNDEFINED;
62                                 return;
63                         }
64
65                         if (build < 0) {
66                                 throw new ArgumentOutOfRangeException ("build");
67                         }
68                         this._Build = build;
69
70                         if (defined == 3) {
71                                 this._Revision = UNDEFINED;
72                                 return;
73                         }
74
75                         if (revision < 0) {
76                                 throw new ArgumentOutOfRangeException ("revision");
77                         }
78                         this._Revision = revision;
79                 }
80
81                 public Version ()
82                 {
83                         CheckedSet (2, 0, 0, -1, -1);
84                 }
85
86                 public Version (string version)
87                 {
88                         int n;
89                         string [] vals;
90                         int major = -1, minor = -1, build = -1, revision = -1;
91
92                         if (version == null) {
93                                 throw new ArgumentNullException ("version");
94                         }
95
96                         vals = version.Split (new Char [] {'.'});
97                         n = vals.Length;
98
99                         if (n < 2 || n > 4) {
100                                 throw new ArgumentException (Locale.GetText
101                                         ("There must be 2, 3 or 4 components in the version string."));
102                         }
103
104                         if (n > 0)
105                                 major = int.Parse (vals [0]);
106                         if (n > 1)
107                                 minor = int.Parse (vals [1]);
108                         if (n > 2)
109                                 build = int.Parse (vals [2]);
110                         if (n > 3)
111                                 revision = int.Parse (vals [3]);
112
113                         CheckedSet (n, major, minor, build, revision);
114                 }
115
116                 public Version (int major, int minor)
117                 {
118                         CheckedSet (2, major, minor, 0, 0);
119                 }
120
121                 public Version (int major, int minor, int build)
122                 {
123                         CheckedSet (3, major, minor, build, 0);
124                 }
125
126                 public Version (int major, int minor, int build, int revision)
127                 {
128                         CheckedSet (4, major, minor, build, revision);
129                 }
130
131                 public int Build {
132                         get {
133                                 return _Build;
134                         }
135                 }
136
137                 public int Major {
138                         get {
139                                 return _Major;
140                         }
141                 }
142
143                 public int Minor {
144                         get {
145                                 return _Minor;
146                         }
147                 }
148
149                 public int Revision {
150                         get {
151                                 return _Revision;
152                         }
153                 }
154 #if NET_2_0
155                 public short MajorRevision {
156                         get {
157                                 return (short)(_Revision >> 16);
158                         }
159                 }
160
161                 public short MinorRevision {
162                         get {
163                                 return (short)_Revision;
164                         }
165                 }
166 #endif
167                 public object Clone ()
168                 {
169                         if (_Build == -1)
170                                 return new Version (_Major, _Minor);
171                         else if (_Revision == -1)
172                                 return new Version (_Major, _Minor, _Build);
173                         else
174                                 return new Version (_Major, _Minor, _Build, _Revision);
175                 }
176
177                 public int CompareTo (object version)
178                 {
179                         if (version == null)
180                                 return 1;
181
182                         if (! (version is Version))
183                                 throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version."));
184
185                         return this.CompareTo ((Version) version);
186                 }
187
188                 public override bool Equals (object obj)
189                 {
190                         return this.Equals (obj as Version);
191                 }
192
193 #if NET_2_0
194                 public
195 #endif
196                 int CompareTo (Version value)
197                 {
198                         if (value == null)
199                                 return 1;
200                         
201                         if (this._Major > value._Major)
202                                 return 1;
203                         else if (this._Major < value._Major)
204                                 return -1;
205
206                         if (this._Minor > value._Minor)
207                                 return 1;
208                         else if (this._Minor < value._Minor)
209                                 return -1;
210
211                         if (this._Build > value._Build)
212                                 return 1;
213                         else if (this._Build < value._Build)
214                                 return -1;
215
216                         if (this._Revision > value._Revision)
217                                 return 1;
218                         else if (this._Revision < value._Revision)
219                                 return -1;
220
221                         return 0;
222                 }
223
224 #if NET_2_0
225                 public
226 #endif
227                 bool Equals (Version obj)
228                 {
229                         return ((obj != null) &&
230                             (obj._Major == _Major) &&
231                             (obj._Minor == _Minor) &&
232                             (obj._Build == _Build) &&
233                             (obj._Revision == _Revision));
234                 }
235
236                 public override int GetHashCode ()
237                 {
238                         return (_Revision << 24) | (_Build << 16) | (_Minor << 8) | _Major;
239                 }
240
241                 // <summary>
242                 //   Returns a stringified representation of the version, format:
243                 //   major.minor[.build[.revision]]
244                 // </summary>
245                 public override string ToString ()
246                 {
247                         string mm = _Major.ToString () + "." + _Minor.ToString ();
248                         
249                         if (_Build != UNDEFINED)
250                                 mm = mm + "." + _Build.ToString ();
251                         if (_Revision != UNDEFINED)
252                                 mm = mm + "." + _Revision.ToString ();
253
254                         return mm;
255                 }
256
257                 // <summary>
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.
261                 //
262                 //    ie, Version a = new Version (1, 2);  a.ToString (3) should
263                 //    throw the expcetion.
264                 // </summary>
265                 public string ToString (int fieldCount)
266                 {
267                         if (fieldCount == 0)
268                                 return String.Empty;
269                         if (fieldCount == 1)
270                                 return _Major.ToString ();
271                         if (fieldCount == 2)
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 () + "." +
278                                         _Build.ToString ();
279                         }
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 ();
286                         }
287                         throw new ArgumentException (Locale.GetText ("Invalid fieldCount parameter: ") + fieldCount.ToString());        
288                 }
289
290                 public static bool operator== (Version v1, Version v2) 
291                 {
292                         return Equals (v1, v2);
293                 }
294
295                 public static bool operator!= (Version v1, Version v2)
296                 {
297                         return !Equals (v1, v2);
298                 }
299
300                 public static bool operator> (Version v1, Version v2)
301                 {
302                         return v1.CompareTo (v2) > 0;
303                 }
304
305                 public static bool operator>= (Version v1, Version v2)
306                 {
307                         return v1.CompareTo (v2) >= 0;
308                 }
309
310                 public static bool operator< (Version v1, Version v2)
311                 {
312                         return v1.CompareTo (v2) < 0;
313                 }
314
315                 public static bool operator<= (Version v1, Version v2)
316                 {
317                         return v1.CompareTo (v2) <= 0;
318                 }
319
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)
323                 {
324                         int major = 0;
325                         int minor = 0;
326                         int build = 0;
327                         int revision = 0;
328                         int state = 1;
329                         int number = UNDEFINED; // string may not begin with a digit
330
331                         if (info == null)
332                                 return new Version (0, 0, 0, 0);
333
334                         for (int i=0; i < info.Length; i++) {
335                                 char c = info [i];
336                                 if (Char.IsDigit (c)) {
337                                         if (number < 0) {
338                                                 number = (c - '0');
339                                         }
340                                         else {
341                                                 number = (number * 10) + (c - '0');
342                                         }
343                                 }
344                                 else if (number >= 0) {
345                                         // assign
346                                         switch (state) {
347                                         case 1:
348                                                 major = number;
349                                                 break;
350                                         case 2:
351                                                 minor = number;
352                                                 break;
353                                         case 3:
354                                                 build = number;
355                                                 break;
356                                         case 4:
357                                                 revision = number;
358                                                 break;
359                                         }
360                                         number = -1;
361                                         state ++;
362                                 }
363                                 // ignore end of string
364                                 if (state == 5)
365                                         break;
366                         }
367
368                         // Last number
369                         if (number >= 0) {
370                                 switch (state) {
371                                 case 1:
372                                         major = number;
373                                         break;
374                                 case 2:
375                                         minor = number;
376                                         break;
377                                 case 3:
378                                         build = number;
379                                         break;
380                                 case 4:
381                                         revision = number;
382                                         break;
383                                 }
384                         }
385                         return new Version (major, minor, build, revision);
386                 }
387         }
388 }