/src/csharp/Utilities.cs

https://github.com/Microsoft/vscode-mono-debug · C# · 191 lines · 142 code · 21 blank · 28 comment · 23 complexity · f7e9e253741dd13e09dfd29908b96ff8 MD5 · raw file

  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. using System;
  6. using System.Net.Sockets;
  7. using System.Net;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. using System.Reflection;
  12. using System.Diagnostics;
  13. using Mono.Debugging.Client;
  14. namespace VSCodeDebug
  15. {
  16. public class Utilities
  17. {
  18. private const string WHICH = "/usr/bin/which";
  19. private const string WHERE = "where";
  20. private static readonly Regex VARIABLE = new Regex(@"\{(\w+)\}");
  21. private static char[] ARGUMENT_SEPARATORS = new char[] { ' ', '\t' };
  22. /*
  23. * Enclose the given string in quotes if it contains space or tab characters.
  24. */
  25. public static string Quote(string arg)
  26. {
  27. if (arg.IndexOfAny(ARGUMENT_SEPARATORS) >= 0) {
  28. return '"' + arg + '"';
  29. }
  30. return arg;
  31. }
  32. /*
  33. * Is the given runtime executable on the PATH.
  34. */
  35. public static bool IsOnPath(string runtime)
  36. {
  37. var process = new Process();
  38. process.StartInfo.CreateNoWindow = true;
  39. process.StartInfo.UseShellExecute = false;
  40. process.StartInfo.RedirectStandardOutput = true;
  41. process.StartInfo.FileName = File.Exists(WHICH) ? WHICH : WHERE;
  42. process.StartInfo.Arguments = Quote(runtime);
  43. try {
  44. process.Start();
  45. process.WaitForExit();
  46. return process.ExitCode == 0;
  47. } catch (Exception) {
  48. // ignore
  49. }
  50. return false;
  51. }
  52. public static string ConcatArgs(string[] args)
  53. {
  54. var arg = "";
  55. if (args != null) {
  56. foreach (var r in args) {
  57. if (arg.Length > 0) {
  58. arg += " ";
  59. }
  60. arg += Utilities.Quote(r);
  61. }
  62. }
  63. return arg;
  64. }
  65. /*
  66. * Resolve hostname, dotted-quad notation for IPv4, or colon-hexadecimal notation for IPv6 to IPAddress.
  67. * Returns null on failure.
  68. */
  69. public static IPAddress ResolveIPAddress(string addressString)
  70. {
  71. try {
  72. IPAddress ipaddress = null;
  73. if (IPAddress.TryParse(addressString, out ipaddress)) {
  74. return ipaddress;
  75. }
  76. #if DNXCORE50
  77. IPHostEntry entry = Dns.GetHostEntryAsync(addressString).Result;
  78. #else
  79. IPHostEntry entry = Dns.GetHostEntry(addressString);
  80. #endif
  81. if (entry != null && entry.AddressList != null && entry.AddressList.Length > 0) {
  82. if (entry.AddressList.Length == 1) {
  83. return entry.AddressList[0];
  84. }
  85. foreach (IPAddress address in entry.AddressList) {
  86. if (address.AddressFamily == AddressFamily.InterNetwork) {
  87. return address;
  88. }
  89. }
  90. }
  91. }
  92. catch (Exception) {
  93. // fall through
  94. }
  95. return null;
  96. }
  97. /*
  98. * Find a free socket port.
  99. */
  100. public static int FindFreePort(int fallback)
  101. {
  102. TcpListener l = null;
  103. try {
  104. l = new TcpListener(IPAddress.Loopback, 0);
  105. l.Start();
  106. return ((IPEndPoint)l.LocalEndpoint).Port;
  107. }
  108. catch (Exception) {
  109. // ignore
  110. }
  111. finally {
  112. l.Stop();
  113. }
  114. return fallback;
  115. }
  116. public static string ExpandVariables(string format, dynamic variables, bool underscoredOnly = true)
  117. {
  118. if (variables == null) {
  119. variables = new { };
  120. }
  121. Type type = variables.GetType();
  122. return VARIABLE.Replace(format, match => {
  123. string name = match.Groups[1].Value;
  124. if (!underscoredOnly || name.StartsWith("_")) {
  125. PropertyInfo property = type.GetProperty(name);
  126. if (property != null) {
  127. object value = property.GetValue(variables, null);
  128. return value.ToString();
  129. }
  130. return '{' + name + ": not found}";
  131. }
  132. return match.Groups[0].Value;
  133. });
  134. }
  135. /**
  136. * converts the given absPath into a path that is relative to the given dirPath.
  137. */
  138. public static string MakeRelativePath(string dirPath, string absPath)
  139. {
  140. if (!dirPath.EndsWith("/")) {
  141. dirPath += "/";
  142. }
  143. if (absPath.StartsWith(dirPath)) {
  144. return absPath.Replace(dirPath, "");
  145. }
  146. return absPath;
  147. /*
  148. Uri uri1 = new Uri(path);
  149. Uri uri2 = new Uri(dir_path);
  150. return uri2.MakeRelativeUri(uri1).ToString();
  151. */
  152. }
  153. }
  154. class CustomLogger : ICustomLogger
  155. {
  156. public void LogError(string message, Exception ex)
  157. {
  158. }
  159. public void LogAndShowException(string message, Exception ex)
  160. {
  161. }
  162. public void LogMessage(string format, params object[] args)
  163. {
  164. }
  165. public string GetNewDebuggerLogFilename()
  166. {
  167. return null;
  168. }
  169. }
  170. }