Copied remotely
[mono.git] / mcs / class / Microsoft.VisualBasic / Microsoft.VisualBasic / Microsoft.VisualBasic.CompilerServices / ByteType.cs
1     //
2     // ByteType.cs
3     //
4     //  Author:
5     //  Chris J Breisch (cjbreisch@altavista.net) 
6     //  Francesco Delfino (pluto@tipic.com)
7     //  Dennis Hayes (dennish@raytek.com)
8     //
9     //  (C) copyright 2002 Chris J Breisch
10     //  2002 Tipic, Inc (http://www.tipic.com)
11     //
12      /*
13       * Copyright (c) 2002-2003 Mainsoft Corporation.
14       * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15       *
16       * Permission is hereby granted, free of charge, to any person obtaining a
17       * copy of this software and associated documentation files (the "Software"),
18       * to deal in the Software without restriction, including without limitation
19       * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20       * and/or sell copies of the Software, and to permit persons to whom the
21       * Software is furnished to do so, subject to the following conditions:
22       * 
23       * The above copyright notice and this permission notice shall be included in
24       * all copies or substantial portions of the Software.
25       * 
26       * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27       * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28       * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29       * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30       * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31       * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32       * DEALINGS IN THE SOFTWARE.
33       */
34     /**
35      * Class that converts objects to Byte value.
36      */
37 using System;
38 using System.ComponentModel;
39
40 namespace Microsoft.VisualBasic.CompilerServices 
41 {
42         [StandardModule, EditorBrowsableAttribute(EditorBrowsableState.Never)] 
43         sealed public class ByteType {
44                 private ByteType () {}
45     
46                 /**
47                  * The method converts given object to byte by the following logic:
48                  * 1. If input object is null - return 0
49                  * 2. If input object is String - run FromString method
50                  * 3. Otherwise run .NET default conversion - Convert.ToByte
51                  * @param value - The object that going to be converted
52                  * @return byte The byte value that converted from the source object
53                  * @see system.Convert#ToByte
54                  */
55                 public static System.Byte FromObject (object Value) { 
56                         if ((object)Value==null)
57                                 return 0;
58
59                         if      (Value is string)
60                                 return FromString((string) Value);
61
62                         return Convert.ToByte(Value);//Throws correct execption. Execption not converted from .java code.
63                 }
64     
65                 // Methods
66                 /**
67                          * The method try to convert given string to byte in a following way:
68                          * 1. If input string is null return 0.
69                          * 2. If input string represents number: return value of this number, 
70                          * @exception InvalidCastException - in case if number translation failed 
71                          * @param str - The string that converted to int
72                          * @return int The value that extracted from the input string.
73                          * @see Microsoft.VisualBasic.VBUtils#isNumber
74                          */ 
75                 public static System.Byte FromString (string Value) {
76                         if(Value == null)return 0;
77     
78                         int Base = 10;
79                         int start = 0;
80                         if(Value.Substring(0,1) == "&"){
81                                 //is diff base
82                                 if(Value.Substring(1,1).ToUpper() == "H")
83                                         Base = 16;
84                                 else if(Value.Substring(1,1).ToUpper() == "O")
85                                         Base = 8;
86                                 else {
87                                         // I think we should just let convert take care of the execption.
88                                         // Should we throw a special execption instead?
89                                         // I think the Mainsoft java code below just converts execptions from java to C#
90                                 }
91                                 start = 2;
92                         }
93                         return Convert.ToByte(Value.Substring(start,Value.Length - start), Base);
94   
95                         // Mainsoft java implmentation.
96                         // isNumber checks for leading &H or &O
97                         // leave for documentation.
98                         //
99                         //            if (VBUtils.isNumber(str, lRes))
100                         //            {
101                         //                long val = (long)java.lang.Math.rint(lRes[0]);
102                         //                if (val > ClrByte.MaxValue || val < ClrByte.MinValue)
103                         //                    throw new OverflowException(
104                         //                        Environment.GetResourceString("Overflow_Byte"));
105                         //                return (int) val;
106                         //            }
107                         //        }
108                         //        catch (java.lang.Exception e)
109                         //        {
110                         //            throw new InvalidCastException(
111                         //                Utils.GetResourceString("InvalidCast_FromStringTo", 
112                         //                    str, "Byte"), e);
113                         //        }
114                 }
115         };
116 }