In .:
[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 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 namespace System
34 {
35         [Serializable]
36         public sealed class Version : ICloneable, IComparable
37 #if NET_2_0
38                 , IComparable<Version>, IEquatable <Version>
39 #endif
40         {
41
42                 int _Major, _Minor, _Build, _Revision;
43
44                 private const int UNDEFINED = -1;
45
46                 private void CheckedSet (int defined, int major, int minor, int build, int revision)
47                 {
48                         // defined should be 2, 3 or 4
49
50                         if (major < 0) {
51                                 throw new ArgumentOutOfRangeException ("major");
52                         }
53                         this._Major = major;
54
55                         if (minor < 0) {
56                                 throw new ArgumentOutOfRangeException ("minor");
57                         }
58                         this._Minor = minor;
59
60                         if (defined == 2) {
61                                 this._Build = UNDEFINED;
62                                 this._Revision = UNDEFINED;
63                                 return;
64                         }
65
66                         if (build < 0) {
67                                 throw new ArgumentOutOfRangeException ("build");
68                         }
69                         this._Build = build;
70
71                         if (defined == 3) {
72                                 this._Revision = UNDEFINED;
73                                 return;
74                         }
75
76                         if (revision < 0) {
77                                 throw new ArgumentOutOfRangeException ("revision");
78                         }
79                         this._Revision = revision;
80                 }
81
82                 public Version ()
83                 {
84                         CheckedSet (2, 0, 0, -1, -1);
85                 }
86
87                 public Version (string version)
88                 {
89                         int n;
90                         string [] vals;
91                         int major = -1, minor = -1, build = -1, revision = -1;
92
93                         if (version == null) {
94                                 throw new ArgumentNullException ("version");
95                         }
96
97                         vals = version.Split (new Char [] {'.'});
98                         n = vals.Length;
99
100                         if (n < 2 || n > 4) {
101                                 throw new ArgumentException (Locale.GetText
102                                         ("There must be 2, 3 or 4 components in the version string."));
103                         }
104
105                         if (n > 0)
106                                 major = int.Parse (vals [0]);
107                         if (n > 1)
108                                 minor = int.Parse (vals [1]);
109                         if (n > 2)
110                                 build = int.Parse (vals [2]);
111                         if (n > 3)
112                                 revision = int.Parse (vals [3]);
113
114                         CheckedSet (n, major, minor, build, revision);
115                 }
116
117                 public Version (int major, int minor)
118                 {
119                         CheckedSet (2, major, minor, 0, 0);
120                 }
121
122                 public Version (int major, int minor, int build)
123                 {
124                         CheckedSet (3, major, minor, build, 0);
125                 }
126
127                 public Version (int major, int minor, int build, int revision)
128                 {
129                         CheckedSet (4, major, minor, build, revision);
130                 }
131
132                 public int Build {
133                         get {
134                                 return _Build;
135                         }
136                 }
137
138                 public int Major {
139                         get {
140                                 return _Major;
141                         }
142                 }
143
144                 public int Minor {
145                         get {
146                                 return _Minor;
147                         }
148                 }
149
150                 public int Revision {
151                         get {
152                                 return _Revision;
153                         }
154                 }
155
156                 public object Clone ()
157                 {
158                         if (_Build == -1)
159                                 return new Version (_Major, _Minor);
160                         else if (_Revision == -1)
161                                 return new Version (_Major, _Minor, _Build);
162                         else
163                                 return new Version (_Major, _Minor, _Build, _Revision);
164                 }
165
166                 public int CompareTo (object version)
167                 {
168                         if (version == null)
169                                 return 1;
170
171                         if (! (version is Version))
172                                 throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version."));
173
174                         return this.CompareTo ((Version) version);
175                 }
176
177                 public override bool Equals (object obj)
178                 {
179                         return this.Equals (obj as Version);
180                 }
181
182 #if NET_2_0
183                 public
184 #endif
185                 int CompareTo (Version v)
186                 {
187                         if (v == null)
188                                 return 1;
189                         
190                         if (this._Major > v._Major)
191                                 return 1;
192                         else if (this._Major < v._Major)
193                                 return -1;
194
195                         if (this._Minor > v._Minor)
196                                 return 1;
197                         else if (this._Minor < v._Minor)
198                                 return -1;
199
200                         if (this._Build > v._Build)
201                                 return 1;
202                         else if (this._Build < v._Build)
203                                 return -1;
204
205                         if (this._Revision > v._Revision)
206                                 return 1;
207                         else if (this._Revision < v._Revision)
208                                 return -1;
209
210                         return 0;
211                 }
212
213 #if NET_2_0
214                 public
215 #endif
216                 bool Equals (Version x)
217                 {
218                         return ((x != null) &&
219                             (x._Major == _Major) &&
220                             (x._Minor == _Minor) &&
221                             (x._Build == _Build) &&
222                             (x._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 fields)
255                 {
256                         if (fields == 0)
257                                 return "";
258                         if (fields == 1)
259                                 return _Major.ToString ();
260                         if (fields == 2)
261                                 return _Major.ToString () + "." + _Minor.ToString ();
262                         if (fields == 3){
263                                 if (_Build == UNDEFINED)
264                                         throw new ArgumentException (Locale.GetText
265                                         ("fields is larger than the number of components defined in this instance."));
266                                 return _Major.ToString () + "." + _Minor.ToString () + "." +
267                                         _Build.ToString ();
268                         }
269                         if (fields == 4){
270                                 if (_Build == UNDEFINED || _Revision == UNDEFINED)
271                                         throw new ArgumentException (Locale.GetText
272                                         ("fields 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 fields parameter: ") + fields.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                 // a very gentle way to construct a Version object which takes 
310                 // the first four numbers in a string as the version
311                 internal static Version CreateFromString (string info)
312                 {
313                         int major = 0;
314                         int minor = 0;
315                         int build = 0;
316                         int revision = 0;
317                         int state = 1;
318                         int number = UNDEFINED; // string may not begin with a digit
319
320                         if (info == null)
321                                 return new Version (0, 0, 0, 0);
322
323                         for (int i=0; i < info.Length; i++) {
324                                 char c = info [i];
325                                 if (Char.IsDigit (c)) {
326                                         if (number < 0) {
327                                                 number = (c - '0');
328                                         }
329                                         else {
330                                                 number = (number * 10) + (c - '0');
331                                         }
332                                 }
333                                 else if (number >= 0) {
334                                         // assign
335                                         switch (state) {
336                                         case 1:
337                                                 major = number;
338                                                 break;
339                                         case 2:
340                                                 minor = number;
341                                                 break;
342                                         case 3:
343                                                 build = number;
344                                                 break;
345                                         case 4:
346                                                 revision = number;
347                                                 break;
348                                         }
349                                         number = -1;
350                                         state ++;
351                                 }
352                                 // ignore end of string
353                                 if (state == 5)
354                                         break;
355                         }
356
357                         // Last number
358                         if (number >= 0) {
359                                 switch (state) {
360                                 case 1:
361                                         major = number;
362                                         break;
363                                 case 2:
364                                         minor = number;
365                                         break;
366                                 case 3:
367                                         build = number;
368                                         break;
369                                 case 4:
370                                         revision = number;
371                                         break;
372                                 }
373                         }
374                         return new Version (major, minor, build, revision);
375                 }
376         }
377 }