2004-08-02 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                         return new Version (_Major, _Minor, _Build, _Revision);
161                 }
162
163                 public int CompareTo (object version)
164                 {
165                         Version v;
166
167                         if (version == null)
168                                 return 1;
169
170                         if (! (version is Version))
171                                 throw new ArgumentException (Locale.GetText ("Argument to Version.CompareTo must be a Version."));
172
173                         v = version as Version;
174
175                         if (this._Major > v._Major)
176                                 return 1;
177                         else if (this._Major < v._Major)
178                                 return -1;
179
180                         if (this._Minor > v._Minor)
181                                 return 1;
182                         else if (this._Minor < v._Minor)
183                                 return -1;
184
185                         if (this._Build > v._Build)
186                                 return 1;
187                         else if (this._Build < v._Build)
188                                 return -1;
189
190                         if (this._Revision > v._Revision)
191                                 return 1;
192                         else if (this._Revision < v._Revision)
193                                 return -1;
194
195                         return 0;
196                 }
197
198                 public override bool Equals (object obj)
199                 {
200                         Version x;
201                         
202                         if (obj == null || !(obj is Version))
203                                 return false;
204
205                         x = (Version) obj;
206                         
207                         if ((x._Major == _Major) &&
208                             (x._Minor == _Minor) &&
209                             (x._Build == _Build) &&
210                             (x._Revision == _Revision))
211                                 return true;
212                         return false;
213                 }
214
215 #if NET_2_0
216                 public int CompareTo (Version v)
217                 {
218                         if (this._Major > v._Major)
219                                 return 1;
220                         else if (this._Major < v._Major)
221                                 return -1;
222
223                         if (this._Minor > v._Minor)
224                                 return 1;
225                         else if (this._Minor < v._Minor)
226                                 return -1;
227
228                         if (this._Build > v._Build)
229                                 return 1;
230                         else if (this._Build < v._Build)
231                                 return -1;
232
233                         if (this._Revision > v._Revision)
234                                 return 1;
235                         else if (this._Revision < v._Revision)
236                                 return -1;
237
238                         return 0;
239                 }
240
241                 public bool Equals (Version x)
242                 {
243                         if ((x._Major == _Major) &&
244                             (x._Minor == _Minor) &&
245                             (x._Build == _Build) &&
246                             (x._Revision == _Revision))
247                                 return true;
248                         return false;
249                 }
250 #endif
251
252                 public override int GetHashCode ()
253                 {
254                         return (_Revision << 24) | (_Build << 16) | (_Minor << 8) | _Major;
255                 }
256
257                 // <summary>
258                 //   Returns a stringified representation of the version, format:
259                 //   major.minor[.build[.revision]]
260                 // </summary>
261                 public override string ToString ()
262                 {
263                         string mm = _Major.ToString () + "." + _Minor.ToString ();
264                         
265                         if (_Build != UNDEFINED)
266                                 mm = mm + "." + _Build.ToString ();
267                         if (_Revision != UNDEFINED)
268                                 mm = mm + "." + _Revision.ToString ();
269
270                         return mm;
271                 }
272
273                 // <summary>
274                 //    LAME: This API is lame, since there is no way of knowing
275                 //    how many fields a Version object has, it is unfair to throw
276                 //    an ArgumentException, but this is what the spec claims.
277                 //
278                 //    ie, Version a = new Version (1, 2);  a.ToString (3) should
279                 //    throw the expcetion.
280                 // </summary>
281                 public string ToString (int fields)
282                 {
283                         if (fields == 0)
284                                 return "";
285                         if (fields == 1)
286                                 return _Major.ToString ();
287                         if (fields == 2)
288                                 return _Major.ToString () + "." + _Minor.ToString ();
289                         if (fields == 3){
290                                 if (_Build == UNDEFINED)
291                                         throw new ArgumentException (Locale.GetText
292                                         ("fields is larger than the number of components defined in this instance."));
293                                 return _Major.ToString () + "." + _Minor.ToString () + "." +
294                                         _Build.ToString ();
295                         }
296                         if (fields == 4){
297                                 if (_Build == UNDEFINED || _Revision == UNDEFINED)
298                                         throw new ArgumentException (Locale.GetText
299                                         ("fields is larger than the number of components defined in this instance."));
300                                 return _Major.ToString () + "." + _Minor.ToString () + "." +
301                                         _Build.ToString () + "." + _Revision.ToString ();
302                         }
303                         throw new ArgumentException (Locale.GetText ("Invalid fields parameter: ") + fields.ToString());        
304                 }
305
306                 public static bool operator== (Version v1, Version v2) 
307                 {
308                         return Equals (v1, v2);
309                 }
310
311                 public static bool operator!= (Version v1, Version v2)
312                 {
313                         return !Equals (v1, v2);
314                 }
315
316                 public static bool operator> (Version v1, Version v2)
317                 {
318                         return v1.CompareTo (v2) > 0;
319                 }
320
321                 public static bool operator>= (Version v1, Version v2)
322                 {
323                         return v1.CompareTo (v2) >= 0;
324                 }
325
326                 public static bool operator< (Version v1, Version v2)
327                 {
328                         return v1.CompareTo (v2) < 0;
329                 }
330
331                 public static bool operator<= (Version v1, Version v2)
332                 {
333                         return v1.CompareTo (v2) <= 0;
334                 }
335
336                 // a very gentle way to construct a Version object which takes 
337                 // the first four numbers in a string as the version
338                 internal static Version CreateFromString (string info)
339                 {
340                         int major = 0;
341                         int minor = 0;
342                         int build = 0;
343                         int revision = 0;
344                         int state = 1;
345                         int number = UNDEFINED; // string may not begin with a digit
346
347                         for (int i=0; i < info.Length; i++) {
348                                 char c = info [i];
349                                 if (Char.IsDigit (c)) {
350                                         if (number < 0) {
351                                                 number = (c - '0');
352                                         }
353                                         else {
354                                                 number = (number * 10) + (c - '0');
355                                         }
356                                 }
357                                 else if (number >= 0) {
358                                         // assign
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                                         number = -1;
374                                         state ++;
375                                 }
376                                 // ignore end of string
377                                 if (state == 5)
378                                         break;
379                         }
380                         return new Version (major, minor, build, revision);
381                 }
382         }
383 }