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