5 // Chris J Breisch (cjbreisch@altavista.net)
6 // Rafael Teixeira (rafaelteixeirabr@hotmail.com)
7 // Dennis Hayes (dennish@raytek.com)
9 // Copyright 2002 Chris J Breisch
10 // (C) 2004 Rafael Teixeira
13 * Copyright (c) 2002-2003 Mainsoft Corporation.
14 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
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.
38 * 1. Method MethodToString(MethodBase Method) is not implemented
39 * 2. Method SetTime is not supported (throw exception)
40 * 3. Method SetDate is not supported (throw exception)
44 using System.Globalization;
46 using System.Reflection;
47 using System.Resources;
49 namespace Microsoft.VisualBasic.CompilerServices {
50 [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
51 [Microsoft.VisualBasic.CompilerServices.StandardModule]
52 public sealed class Utils {
55 internal const int SEVERITY_ERROR = Int32.MinValue;
56 internal const int FACILITY_CONTROL = 655360;
57 internal const int FACILITY_RPC = 65536;
58 internal const int FACILITY_ITF = 262144;
59 internal const int SCODE_FACILITY = 536805376;
60 internal const char chPeriod = '.';
61 internal const char chSpace = ' ';
62 internal const char chIntlSpace = '\u3000';
63 internal const char chZero = '0';
64 internal const char chHyphen = '-';
65 internal const char chPlus = '+';
66 internal const char chNull = '\0';
67 internal const char chLetterA = 'A';
68 internal const char chLetterZ = 'Z';
69 internal const char chColon = ':';
70 internal const char chSlash = '/';
71 internal const char chBackslash = '\\';
72 internal const char chTab = '\t';
73 internal const char chCharH0A = '\n';
74 internal const char chCharH0B = '\t';
75 internal const char chCharH0C = '\f';
76 internal const char chCharH0D = '\r';
77 internal const char chLineFeed = '\n';
78 internal const char chDblQuote = '\"';
79 //TODO: why is this static, should it be const?
80 internal static char[] m_achIntlSpace = new char[] { ' ', '\u3000' };
82 private const String FILE_NAME = "Microsoft.VisualBasic.VBUtils";
83 private static ResourceManager RESOURCE_BUNDLE = new ResourceManager(FILE_NAME, Assembly.GetExecutingAssembly()) ;
85 // public static ResourceManager get_VBAResourceManager() throws NotImplementedException
87 // throw new NotImplementedException("The method get_VBAResourceManager in class VisualBasic.CompilerServices.Utils is not supported");
90 internal static string GetResourceString(string key)
94 str = RESOURCE_BUNDLE.GetString(key);
97 str = RESOURCE_BUNDLE.GetString("ID95");
103 internal static string GetResourceString(string key, bool notUsed)
105 return GetResourceString(key);
108 internal static string GetResourceString (string key,string paramValue)
110 StringBuilder sb = new StringBuilder(GetResourceString(key));
111 sb.Replace("|1", paramValue);
112 return sb.ToString();
115 internal static string GetResourceString (string key, string paramValue1, string paramValue2)
117 StringBuilder sb = new StringBuilder(GetResourceString(key));
118 sb.Replace("|1", paramValue1);
119 sb.Replace("|2", paramValue2);
120 return sb.ToString();
123 internal static string GetResourceString (string key, string param1, string param2, string param3)
125 StringBuilder sb = new StringBuilder(GetResourceString(key));
126 sb.Replace("|1", param1);
127 sb.Replace("|2", param2);
128 sb.Replace("|3", param3);
129 return sb.ToString();
133 internal static string GetResourceString (string key, string param1, string param2, string param3,
136 StringBuilder sb = new StringBuilder(GetResourceString(key));
137 sb.Replace("|1", param1);
138 sb.Replace("|2", param2);
139 sb.Replace("|3", param3);
140 sb.Replace("|4", param4);
141 return sb.ToString();
144 internal static string GetResourceString (int ResourceId)
146 string str = "ID" + ResourceId.ToString();
147 return GetResourceString(str);
150 public static void ThrowException (int hr)
152 throw ExceptionUtils.VbMakeException(hr);
155 internal static bool IsNumericType (Type tc)
157 TypeCode typeCode = Type.GetTypeCode(tc);
158 return IsNumericTypeCode((int)typeCode);
161 internal static bool IsNumericTypeCode (int i)
164 case (int)TypeCode.Boolean:
165 case (int)TypeCode.Byte:
166 case (int)TypeCode.Int16:
167 case (int)TypeCode.Int32:
168 case (int)TypeCode.Int64:
169 case (int)TypeCode.Double:
170 case (int)TypeCode.Single:
171 case (int)TypeCode.Decimal:
179 * This method change the given string which contains the suffix of array
180 * representation so that the the signs '(', ')' and ',' will appear in the
182 * @param sRank the given suffix
183 * @return string the suffix after the change
185 private static string changeArraySuffix(string sRank)
187 StringBuilder sb = new StringBuilder(sRank);
190 for (int i = sRank.Length-1; i >= 0; i--) {
192 if (currentChar == '(') {
197 else if (currentChar == ')') {
202 else if (currentChar == ',') {
208 return sb.ToString();
211 internal static string VBFriendlyName (object obj)
215 Type type = obj.GetType();
216 return VBFriendlyName(type,obj);
219 internal static string VBFriendlyName (Type type)
221 return VBFriendlyNameOfTypeName(type.Name);
224 internal static string VBFriendlyName (Type type, object obj)
227 //if (StringStaticWrapper.CompareOrdinal(type.FullName, "System.__ComObject") //java code
228 if (string.CompareOrdinal(type.FullName, "System.__ComObject") == 0) {
229 //this shape of if was writen since get_IsCOMObject is not implemented
230 //yet. in this flow only when the type name is "System.__ComObject" the
231 //not implementedException is thrown.
234 //if (type.IsCOMObject)
235 // Information.TypeNameOfCOMObject(obj, false);
237 return VBFriendlyNameOfTypeName(type.Name);
241 * This method return a Vb representation of the given type name.
242 * If this is an array it is changed to vb presentation with () instead of
244 * @param typename the given type name.
245 * @return string the vb presentation of the type name
247 internal static string VBFriendlyNameOfTypeName (string typename)
249 string tmpStr = null;
250 int bracketIndex = 0;
251 int length = typename.Length - 1;
252 //if this is array type
253 if (typename[length] == ']') {
254 bracketIndex = typename.IndexOf("[");
255 if (bracketIndex + 1 == length)
257 //when there is more than one dimention
261 .Substring(bracketIndex, length - bracketIndex + 1)
265 typename = typename.Substring(0, bracketIndex);
268 tmpStr = (tmpStr==null) ? "" :changeArraySuffix(tmpStr);
270 //this method returns the C# equivalent name if this type name is vb unique
271 //it returns null if the type name is like in C#
272 string str = Information.VbTypeName(typename);
279 * This method change the presentation of a number.
280 * if the decimal separator isn't '.' then it's changed to '.' and the updated
281 * string return, otherwise if the string start in one of the four way :
282 * "0." , "-0." , "+0." or " 0." ,then the '0' character is removed.
283 * if the input string doesn't contain the decimal separator the input value
285 * @param s the string that need to be changed
286 * @return string the updated string.
288 internal static string StdFormat(string s) {
289 string separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
291 int index = s.IndexOf(separator);
296 //StringBuffer sb = new StringBuffer(s);
297 StringBuilder sb = new StringBuilder(s);
298 if (s[index] != '.') {
302 if (sb.Length > 1 && sb[0] == '0' && sb[1] == '.')
303 return sb.ToString().Substring(0,1);// sb.substring(1);//java version
306 (sb[0] == '-' || sb[0] == '+'|| sb[0] == ' ') &&
307 sb[1] == '0' && sb[2] == '.') {
308 //sb = sb.deleteCharAt(1);//java version
311 return sb.ToString();
315 * This method returns the octal string representation of the given long
316 * @param Val the given long value
317 * @return string the Octal string representation
319 internal static string OctFromLong(long Val) {
320 return Convert.ToString(Val, 8);
323 public static void SetTime(DateTime dtTime) {
324 throw new NotImplementedException("Method SetTime in VisualBasic.CompilerServices.Utils is not supported");
327 public static void SetDate(DateTime vDate) {
328 throw new NotImplementedException("Method SetDate in VisualBasic.CompilerServices.Utils is not supported");
332 internal static DateTimeFormatInfo GetDateTimeFormatInfo() {
333 return CultureInfo.CurrentCulture.DateTimeFormat;
337 * This method maps exception id to message id in
338 * @param lNumber the exception constant
339 * @return int the exception message id in the resource file
341 internal static int MapHRESULT(int lNumber) {
342 return ExceptionUtils.fromDotNetToVB(lNumber);
345 internal static CultureInfo GetCultureInfo() {
346 return CultureInfo.CurrentCulture;
349 public static object SetCultureInfo(CultureInfo culture) {
350 CultureInfo currentCulture = CultureInfo.CurrentCulture;
351 //CultureInfo.set_CurrentCulture(culture);//java code
352 //TODO: CultureInfo.CurrentCulture is read only
353 //CultureInfo.CurrentCulture = culture;
354 return currentCulture;
357 internal static CultureInfo GetInvariantCultureInfo() {
358 return CultureInfo.InvariantCulture;
361 internal static Encoding GetFileIOEncoding() {
362 return Encoding.Default;
365 internal static int GetLocaleCodePage() {
366 return CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
370 // public static System.Array CopyArray (System.Array SourceArray, System.Array DestinationArray)
373 // long SourceLength = SourceArray.LongLength;
374 // long DestinationLength = DestinationArray.LongLength;
375 // long LengthToCopy = (SourceLength < DestinationLength) ? SourceLength : DestinationLength;
376 // Array.Copy(SourceArray, DestinationArray, LengthToCopy);
378 // int SourceLength = SourceArray.Length;
379 // int DestinationLength = DestinationArray.Length;
380 // int LengthToCopy = (SourceLength < DestinationLength) ? SourceLength : DestinationLength;
381 // Array.Copy(SourceArray, DestinationArray, LengthToCopy);
383 // return DestinationArray;
388 * This method copy the information from one array to another.
390 public static Array CopyArray(Array source, Array destination) {
391 if (source == null || destination == null)
395 long lengthToCopy = source.LongLength;
396 if (lengthToCopy == 0)
399 int totalRanks = source.Rank;
400 if (totalRanks != destination.Rank)
401 throw (InvalidCastException)ExceptionUtils.VbMakeException(new InvalidCastException(GetResourceString("Array_RankMismatch")), 9);
403 int ranksThatMustBeEqual = (totalRanks - 1);
404 for (int rank = 0; rank < ranksThatMustBeEqual; rank++) {
405 if (destination.GetUpperBound(rank) != source.GetUpperBound(rank))
406 throw (ArrayTypeMismatchException)ExceptionUtils.VbMakeException(new ArrayTypeMismatchException(GetResourceString("Array_TypeMismatch")), 9);
409 if (totalRanks == 1) {
410 if (lengthToCopy > destination.LongLength)
411 lengthToCopy = destination.LongLength;
412 Array.Copy(source, destination, lengthToCopy);
414 long sourceLengthInLastRank = source.GetLongLength(totalRanks - 1);
415 long destinationLengthInLastRank = destination.GetLongLength(totalRanks - 1);
416 if (destinationLengthInLastRank > 0) {
417 long lengthToCopyInLastRank = Math.Min(destinationLengthInLastRank, sourceLengthInLastRank);
418 long lowerRankBlocksToCopy = source.LongLength / sourceLengthInLastRank;
419 for (long block = 0; block < lowerRankBlocksToCopy; block++)
420 Array.Copy(source, (block * sourceLengthInLastRank), destination, (block * destinationLengthInLastRank), lengthToCopyInLastRank);
424 int lengthToCopy = source.Length;
425 if (lengthToCopy == 0)
428 int totalRanks = source.Rank;
429 if (totalRanks != destination.Rank)
430 throw (InvalidCastException)ExceptionUtils.VbMakeException(new InvalidCastException(GetResourceString("Array_RankMismatch")), 9);
432 int ranksThatMustBeEqual = (totalRanks - 1);
433 for (int rank = 0; rank < ranksThatMustBeEqual; rank++) {
434 if (destination.GetUpperBound(rank) != source.GetUpperBound(rank))
435 throw (ArrayTypeMismatchException)ExceptionUtils.VbMakeException(new ArrayTypeMismatchException(GetResourceString("Array_TypeMismatch")), 9);
438 if (totalRanks == 1) {
439 if (lengthToCopy > destination.Length)
440 lengthToCopy = destination.Length;
441 Array.Copy(source, destination, lengthToCopy);
443 int sourceLengthInLastRank = source.GetLength(totalRanks - 1);
444 int destinationLengthInLastRank = destination.GetLength(totalRanks - 1);
445 if (destinationLengthInLastRank > 0) {
446 int lengthToCopyInLastRank = Math.Min(destinationLengthInLastRank, sourceLengthInLastRank);
447 int lowerRankBlocksToCopy = source.Length / sourceLengthInLastRank;
448 for (int block = 0; block < lowerRankBlocksToCopy; block++)
449 Array.Copy(source, (block * sourceLengthInLastRank), destination, (block * destinationLengthInLastRank), lengthToCopyInLastRank);
457 public static string MethodToString(MethodBase Method) {
458 throw new NotImplementedException("The method MethodToString in class VisualBasic.CompilerServices.Utils is not supported");
462 internal static string FieldToString(FieldInfo fieldinfo) {
463 throw new NotImplementedException("The method FieldToString in class VisualBasic.CompilerServices.Utils is not supported");
466 internal static string MemberToString(MemberInfo memberinfo) {
467 switch (memberinfo.MemberType) {
468 case MemberTypes.Method :
469 return MethodToString((MethodInfo)memberinfo);
471 case MemberTypes.Field :
472 return FieldToString((FieldInfo)memberinfo);
474 return memberinfo.Name;