RemotePath.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using System;
  2. using System.Globalization;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. namespace WinSCP
  6. {
  7. [Guid("70253534-C5DC-4EF3-9C98-65C57D79C324")]
  8. [ClassInterface(Constants.ClassInterface)]
  9. [ComVisible(true)]
  10. public sealed class RemotePath : IReflect
  11. {
  12. public static string EscapeFileMask(string fileMask)
  13. {
  14. if (fileMask == null)
  15. {
  16. throw new ArgumentNullException("fileMask");
  17. }
  18. int lastSlash = fileMask.LastIndexOf('/');
  19. string path = lastSlash > 0 ? fileMask.Substring(0, lastSlash + 1) : string.Empty;
  20. string mask = lastSlash > 0 ? fileMask.Substring(lastSlash + 1) : fileMask;
  21. // Keep in sync with EscapeFileMask in GenerateUrl.cpp
  22. mask = mask.Replace("[", "[[]").Replace("*", "[*]").Replace("?", "[?]");
  23. return path + mask;
  24. }
  25. public static string CombinePaths(string path1, string path2)
  26. {
  27. if (path1 == null)
  28. {
  29. throw new ArgumentNullException("path1");
  30. }
  31. if (path2 == null)
  32. {
  33. throw new ArgumentNullException("path2");
  34. }
  35. string result;
  36. if (path2.StartsWith("/", StringComparison.Ordinal))
  37. {
  38. result = path2;
  39. }
  40. else
  41. {
  42. result =
  43. path1 +
  44. ((path1.Length == 0) || (path2.Length == 0) || path1.EndsWith("/", StringComparison.Ordinal) ? string.Empty : "/") +
  45. path2;
  46. }
  47. return result;
  48. }
  49. public static string TranslateRemotePathToLocal(string remotePath, string remoteRoot, string localRoot)
  50. {
  51. if (remotePath == null)
  52. {
  53. throw new ArgumentNullException("remotePath");
  54. }
  55. if (remoteRoot == null)
  56. {
  57. throw new ArgumentNullException("remoteRoot");
  58. }
  59. if (localRoot == null)
  60. {
  61. throw new ArgumentNullException("localRoot");
  62. }
  63. if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
  64. {
  65. localRoot += "\\";
  66. }
  67. // not adding to empty root paths, because the path may not even start with slash
  68. if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
  69. {
  70. remoteRoot += "/";
  71. }
  72. string localPath;
  73. // special case
  74. if (remotePath == remoteRoot)
  75. {
  76. localPath = localRoot;
  77. }
  78. else
  79. {
  80. if (!remotePath.StartsWith(remoteRoot, StringComparison.Ordinal))
  81. {
  82. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", remotePath, remoteRoot));
  83. }
  84. string subPath = remotePath.Substring(remoteRoot.Length);
  85. // can happen only when remoteRoot is empty
  86. if (subPath.StartsWith("/", StringComparison.Ordinal))
  87. {
  88. subPath = subPath.Substring(1);
  89. }
  90. subPath = subPath.Replace('/', '\\');
  91. localPath = localRoot + subPath;
  92. }
  93. return localPath;
  94. }
  95. public static string TranslateLocalPathToRemote(string localPath, string localRoot, string remoteRoot)
  96. {
  97. if (localPath == null)
  98. {
  99. throw new ArgumentNullException("localPath");
  100. }
  101. if (localRoot == null)
  102. {
  103. throw new ArgumentNullException("localRoot");
  104. }
  105. if (remoteRoot == null)
  106. {
  107. throw new ArgumentNullException("remoteRoot");
  108. }
  109. if ((localRoot.Length > 0) && !localRoot.EndsWith("\\", StringComparison.Ordinal))
  110. {
  111. localRoot += "\\";
  112. }
  113. // not adding to empty root paths, because the path may not even start with slash
  114. if ((remoteRoot.Length > 0) && !remoteRoot.EndsWith("/", StringComparison.Ordinal))
  115. {
  116. remoteRoot += "/";
  117. }
  118. string remotePath;
  119. // special case
  120. if (localPath == localRoot)
  121. {
  122. remotePath = remoteRoot;
  123. }
  124. else
  125. {
  126. if (!localPath.StartsWith(localRoot, StringComparison.Ordinal))
  127. {
  128. throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "{0} does not start with {1}", localPath, localRoot));
  129. }
  130. string subPath = localPath.Substring(localRoot.Length);
  131. // can happen only when localRoot is empty
  132. if (subPath.StartsWith("\\", StringComparison.Ordinal))
  133. {
  134. subPath = subPath.Substring(1);
  135. }
  136. subPath = subPath.Replace('\\', '/');
  137. remotePath = remoteRoot + subPath;
  138. }
  139. return remotePath;
  140. }
  141. public static string GetDirectoryName(string path)
  142. {
  143. string result;
  144. if (path == null)
  145. {
  146. result = null;
  147. }
  148. else if (path.Length == 0)
  149. {
  150. throw new ArgumentException("Path cannot be empty", "path");
  151. }
  152. else
  153. {
  154. int i = path.LastIndexOf('/');
  155. if (i < 0)
  156. {
  157. result = string.Empty;
  158. }
  159. else if (i == 0)
  160. {
  161. if (path.Length == 1)
  162. {
  163. result = null;
  164. }
  165. else
  166. {
  167. result = "/";
  168. }
  169. }
  170. else
  171. {
  172. result = path.Substring(0, i);
  173. }
  174. }
  175. return result;
  176. }
  177. public static string AddDirectorySeparator(string path)
  178. {
  179. if (string.IsNullOrEmpty(path))
  180. {
  181. throw new ArgumentException("Path cannot be empty", "path");
  182. }
  183. if (!path.EndsWith("/", StringComparison.Ordinal))
  184. {
  185. path += "/";
  186. }
  187. return path;
  188. }
  189. public static string GetFileName(string path)
  190. {
  191. string result;
  192. if (string.IsNullOrEmpty(path))
  193. {
  194. result = null;
  195. }
  196. else
  197. {
  198. int i = path.LastIndexOf('/');
  199. if (i >= 0)
  200. {
  201. result = path.Substring(i + 1);
  202. }
  203. else
  204. {
  205. result = path;
  206. }
  207. }
  208. return result;
  209. }
  210. FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
  211. {
  212. return GetType().GetField(name, bindingAttr);
  213. }
  214. FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
  215. {
  216. return GetType().GetFields(bindingAttr);
  217. }
  218. MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
  219. {
  220. return GetType().GetMember(name, bindingAttr);
  221. }
  222. MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
  223. {
  224. return GetType().GetMembers(bindingAttr);
  225. }
  226. MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
  227. {
  228. return GetType().GetMethod(name, bindingAttr);
  229. }
  230. MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
  231. {
  232. return GetType().GetMethod(name, bindingAttr, binder, types, modifiers);
  233. }
  234. MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
  235. {
  236. return GetType().GetMethods(bindingAttr);
  237. }
  238. PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
  239. {
  240. return GetType().GetProperties(bindingAttr);
  241. }
  242. PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
  243. {
  244. return GetType().GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
  245. }
  246. PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
  247. {
  248. return GetType().GetProperty(name, bindingAttr);
  249. }
  250. object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
  251. {
  252. if (target == null)
  253. {
  254. throw new ArgumentNullException("target");
  255. }
  256. Type type = target.GetType();
  257. // This trivial implementation allows calling static methods over COM
  258. return type.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters);
  259. }
  260. Type IReflect.UnderlyingSystemType
  261. {
  262. get { return GetType(); }
  263. }
  264. }
  265. }