Fixed API: added missing attribute System.Serializable
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / Information.cs
1 //
2 // Information.cs
3 //
4 // Author:
5 //   Chris J Breisch (cjbreisch@altavista.net) 
6 //   Francesco Delfino (pluto@tipic.com)
7 //   Rafael Teixeira (rafaelteixeirabr@hotmail.com)
8 //
9 // (C) 2002 Chris J Breisch
10 //     2003 Tipic, Inc. (http://www.tipic.com)
11 //     2004 Rafael Teixeira
12 //
13
14 //
15 // Copyright (c) 2002-2003 Mainsoft Corporation.
16 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 //
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
25 // 
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
28 // 
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 //
37
38 using System;
39 using System.Globalization;
40
41 namespace Microsoft.VisualBasic
42 {
43         using CompilerServices;
44
45         [Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute] 
46         sealed public class Information {
47
48                 private Information ()
49                 {
50                         //Nothing to do here...
51                 }
52
53                 private static int [] QBColorTable = { 0, 8388608, 32768, 8421376, 
54                                                        128, 8388736, 32896, 12632256, 
55                                                        8421504, 16711680, 65280, 16776960, 
56                                                        255, 16711935, 65535, 16777215 };
57
58
59
60                 public static System.Boolean IsArray (Object VarName)
61                 {
62                         if(VarName == null)
63                                 return false;
64                         else
65                                 return VarName is Array;
66                 }
67
68                 public static System.Boolean IsError (Object Expression)
69                 { 
70                         if(Expression == null )
71                                 return false;
72                         else
73                                 return Expression is Exception;
74                 }
75
76                 public static System.Boolean IsReference (Object Expression)
77                 {
78                         return (false == Expression is ValueType);
79                 }
80
81
82
83                 public static System.Boolean IsDate (System.Object Expression)
84                 {
85                         if(Expression == null)
86                                 return false;
87                         else if(Expression is DateTime)
88                                 return true;
89                         else if(Expression is String) {
90                                 try {
91                                         DateTime.Parse((String) Expression);
92                                 }
93                                 catch (Exception e) {
94                                         return false;
95                                 }
96
97                                 return true;
98                         }
99                         else
100                                 return false;
101                 }
102
103
104                 public static System.Boolean IsDBNull (System.Object Expression) 
105                 { 
106                         if(Expression == null)
107                                 return false;
108                         else
109                                 return Expression is DBNull;
110                 }
111
112                 public static System.Boolean IsNothing (System.Object Expression) 
113                 { 
114                         return (Expression == null);
115                 }
116
117                 public static System.Int32 RGB (int Red, int Green, int Blue) 
118                 { 
119                         if(Red < 0)
120                                 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Red"));
121                         if(Green < 0)
122                                 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Green"));
123                         if(Blue < 0)
124                                 throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValue1", "Blue"));
125
126                         if(Red > 255) 
127                                 Red = 255;
128                         if(Green > 255) 
129                                 Green = 255;
130                         if(Blue > 255) 
131                                 Blue = 255;
132
133                         return (((Red & 0xFF) << 16) | ((Green & 0xFF) << 8) | ((Blue & 0xFF) << 0));
134                 }
135
136
137
138
139                 public static System.Boolean IsNumeric (System.Object Expression) 
140                 { 
141
142                         if(Expression == null || Expression is DateTime)
143                                 return false;
144
145                         if(Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal ||
146                             Expression is Single || Expression is Double || Expression is Boolean)
147                                 return true;
148         
149
150
151                         try {
152                                 if(Expression is string)
153                                         Double.Parse(Expression as string);
154                                 else
155                                         Double.Parse(Expression.ToString());
156                                 return true;
157                         } catch {} // just dismiss errors but return false
158
159                         return false;
160                 }
161
162                 public static System.Int32 QBColor (System.Int32 Color) 
163                 { 
164                         if(Color < 0 || Color > 15)
165                                 throw new ArgumentException(
166                                                             Utils.GetResourceString("Argument_InvalidValue1", "Color"));
167                         return QBColorTable[Color];
168                 }
169
170                 public static String VbTypeName(String UrtName)
171                 {
172
173                         if(UrtName == null)
174                                 return null;
175
176                         String tmpName = UrtName.Trim().ToLower(CultureInfo.InvariantCulture);
177
178                         if(tmpName.StartsWith("system."))
179                                 tmpName = tmpName.Substring(7);
180
181                         if(tmpName.Equals("object"))
182                                 return "Object";
183                         if(tmpName.Equals("int16"))
184                                 return "Short";
185                         if(tmpName.Equals("clrint16"))
186                                 return "Short";
187                         if(tmpName.Equals("int32"))
188                                 return "Integer";
189                         if(tmpName.Equals("clrint32"))
190                                 return "Integer";
191                         if(tmpName.Equals("int64"))
192                                 return "Long";
193                         if(tmpName.Equals("clrint64"))
194                                 return "Long";
195                         if(tmpName.Equals("clrsingle"))
196                                 return "Single";
197                         if(tmpName.Equals("single"))
198                                 return "Single";
199                         if(tmpName.Equals("double"))
200                                 return "Double";
201                         if(tmpName.Equals("clrdouble"))
202                                 return "Double";
203                         if(tmpName.Equals("boolean"))
204                                 return "Boolean";
205                         if(tmpName.Equals("clrboolean"))
206                                 return "Boolean";
207                         if(tmpName.Equals("char"))
208                                 return "Char";
209                         if(tmpName.Equals("clrchar"))
210                                 return "char";
211                         if(tmpName.Equals("string"))
212                                 return "String";
213                         if(tmpName.Equals("java.lang.string"))
214                                 return "String";
215                         if(tmpName.Equals("byte"))
216                                 return "Byte";
217                         if(tmpName.Equals("clrbyte"))
218                                 return "Byte";
219                         if(tmpName.Equals("decimal"))
220                                 return "Decimal";
221                         if(tmpName.Equals("datetime"))
222                                 return "Date";
223
224                         return null;
225                 }
226
227                 internal static String TypeNameOfCOMObject (Object obj, Boolean flag) 
228                 {
229                         throw new NotImplementedException("Method Microsoft.VisualBasic.Information.TypeNameOfCOMObject() is not supported");
230                 }
231
232                 public static System.Int32 UBound (System.Array Array, 
233                                                    [System.Runtime.InteropServices.Optional] 
234                                                    [System.ComponentModel.DefaultValue(1)] 
235                                                    System.Int32 Rank) 
236                 { 
237                         Exception e;
238
239                         if(Array == null) {
240                                 e =  new ArgumentNullException(Utils.GetResourceString("Argument_InvalidNullValue1", "Array"));
241                                 throw (ArgumentNullException) ExceptionUtils.VbMakeException(e, 9);
242                         }
243
244                         e = ExceptionUtils.VbMakeException((Exception) new ArgumentNullException(VBUtils.GetResourceString(9)), 9);
245
246                         if(!IsArray(Array))
247                         {
248                                 throw new ArgumentException("Not array arrived to UBound method of Information class");
249                         }
250
251                         if(Rank < 1 || Rank > Array.Rank)
252                                 throw new RankException(Utils.GetResourceString("Argument_InvalidRank1", "Rank"));
253
254                         return Array.GetUpperBound(Rank-1);
255                 }
256
257                 public static System.Int32 LBound (System.Array Array, 
258                                                    [System.Runtime.InteropServices.Optional] 
259                                                    [System.ComponentModel.DefaultValue(1)] 
260                                                    System.Int32 Rank) 
261                 { 
262                         Exception e;
263
264                         if(Array == null) {
265                                 e = new ArgumentNullException(Utils.GetResourceString("Argument_InvalidNullValue1", "Array"));
266                                 throw (ArgumentNullException)ExceptionUtils.VbMakeException(e, 9);
267                         }
268
269                         if(!IsArray(Array)) {
270                                 throw new ArgumentException("Not array arrived to LBound method of Information class");
271                         }
272
273                         if(Rank < 1 || Rank > Array.Rank)
274                                 throw new RankException(Utils.GetResourceString("Argument_InvalidRank1", "Rank"));
275
276                         return Array.GetLowerBound(Rank-1);
277                 }
278
279                 public static Microsoft.VisualBasic.ErrObject Err() 
280                 { 
281                         return ProjectData.Err;
282                 }
283
284                 [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] 
285                 public static System.Int32 Erl () 
286                 { 
287                         return ProjectData.Err.Erl;
288                 }
289
290                 public static System.String TypeName (System.Object VarName) 
291                 { 
292                         String name;
293                         Boolean isArray = false;
294
295                         if(VarName == null)
296                                 return "Nothing";
297
298                         Type type = VarName.GetType();
299                         if(type.IsArray) {
300                                 isArray = true;
301                                 type = type.GetElementType();
302                         }
303                         if(type.IsEnum)
304                                 name = type.Name;
305                         else {
306                                 switch(Type.GetTypeCode(type)) {
307                                 case TypeCode.DBNull:
308                                         name = "DBNull";
309                                         break;
310                                 case TypeCode.Boolean:
311                                         name = "Boolean";
312                                         break;
313                                 case TypeCode.Char:
314                                         name = "Char";
315                                         break;
316                                 case TypeCode.Byte:
317                                         name = "Byte";
318                                         break;
319                                 case TypeCode.Int16:
320                                         name = "Short";
321                                         break;
322                                 case TypeCode.Int32:
323                                         name = "Integer";
324                                         break;
325                                 case TypeCode.Int64:
326                                         name = "Long";
327                                         break;
328                                 case TypeCode.Single:
329                                         name = "Single";
330                                         break;
331                                 case TypeCode.Double:
332                                         name = "Double";
333                                         break;
334                                 case TypeCode.Decimal:
335                                         name = "Decimal";
336                                         break;
337                                 case TypeCode.DateTime:
338                                         name = "Date";
339                                         break;
340                                 case TypeCode.String:
341                                         name = "String";
342                                         break;
343                                 default :
344                                         name = type.Name;
345                                         break;
346                                 }
347                                 
348                                 // the following is commented even with mainsoft
349
350                                 // if(type.get_IsCOMObject() && name.equals("__ComObject"))
351                                 // {
352                                 //      throw new NotImplementedException("COM Objects unsupported in TypeName of Information");
353                                 // }
354                         }
355                         int index = name.IndexOf('+');
356                         if(index >= 0)
357                                 name = name.Substring(index + 1);
358
359                         if(isArray == true) {
360                                 int rank = (VarName as Array).Rank;
361                                 if(rank == 1)
362                                         name = name + "[]";
363                                 else {
364                                         name = name + "[";
365                                         for (int i = 0; i < rank - 1; i++)
366                                                 name += ',';
367                                         name = name + "]";
368                                 }
369                                 name = Utils.VBFriendlyNameOfTypeName(name);
370                         }
371                         return name;
372                 }
373
374                 public static System.String SystemTypeName (System.String VbName) 
375                 { 
376                         String tmpStr = VbName.Trim().ToLower(CultureInfo.InvariantCulture);
377                         if(tmpStr.Equals("object"))
378                                 return "System.Object";
379                         if(tmpStr.Equals("short"))
380                                 return "System.Int16";
381                         if(tmpStr.Equals("integer"))
382                                 return "System.Int32";
383                         if(tmpStr.Equals("single"))
384                                 return "System.Single";
385                         if(tmpStr.Equals("double"))
386                                 return "System.Double";
387                         if(tmpStr.Equals("date"))
388                                 return "System.DateTime";
389                         if(tmpStr.Equals("string"))
390                                 return "System.String";
391                         if(tmpStr.Equals("boolean"))
392                                 return "System.Boolean";
393                         if(tmpStr.Equals("decimal"))
394                                 return "System.Decimal";
395                         if(tmpStr.Equals("byte"))
396                                 return "System.Byte";
397                         if(tmpStr.Equals("char"))
398                                 return "System.Char";
399                         if(tmpStr.Equals("long"))
400                                 return "System.Int64";
401
402                         return null;
403                 }
404
405                 [MonoTODO]
406                 public static VariantType VarType(Object VarName)
407                 {
408                         if(VarName == null)
409                                 return VariantType.Object;
410
411                         if(VarName is Exception)
412                                 return VariantType.Error;
413
414                         return varType(VarName.GetType());
415                 }
416
417                 private static VariantType varType(Type varType)
418                 {
419                         if(varType == null)
420                                 return VariantType.Object;
421
422                         if(varType.IsArray) {
423
424                                 Type type = varType.GetElementType();
425                                 if(type.IsArray)
426                                         return VariantType.ObjectArray;
427
428                                 int elemVal = (int) Information.varType(type);
429                                 return (VariantType)((int)VariantType.Array | elemVal);
430                         }
431         
432                         if(varType.IsEnum)
433                                 varType = Enum.GetUnderlyingType(varType);
434
435                         if(varType == null)
436                                 return VariantType.Empty;
437
438                         switch(Type.GetTypeCode(varType)) {
439                         case TypeCode.Empty:    
440                                 return VariantType.Empty;
441                         case TypeCode.DBNull:   
442                                 return VariantType.Null;
443                         case TypeCode.Boolean:  
444                                 return VariantType.Boolean;
445                         case TypeCode.Char:     
446                                 return VariantType.Char;
447                         case TypeCode.Byte:     
448                                 return VariantType.Byte;
449                         case TypeCode.Int16:    
450                                 return VariantType.Short;
451                         case TypeCode.Int32:    
452                                 return VariantType.Integer;
453                         case TypeCode.Int64:    
454                                 return VariantType.Long;
455                         case TypeCode.Single:   
456                                 return VariantType.Single;
457                         case TypeCode.Double:   
458                                 return VariantType.Double;
459                         case TypeCode.Decimal:  
460                                 return VariantType.Decimal;
461                         case TypeCode.DateTime: 
462                                 return VariantType.Date;
463                         case TypeCode.String:   
464                                 return VariantType.String;
465                         }
466
467                         if(varType.IsSubclassOf(typeof(System.Exception)))
468                                 return VariantType.Error;
469
470                         if(varType.IsValueType)
471                                 return VariantType.UserDefinedType;
472
473                         return VariantType.Object;
474                 }
475         }
476 }
477
478