Datei: NDOPackage/ExtendedPath.cs
Last Commit (4a7e8ab)
1 | // |
2 | // Copyright (c) 2002-2019 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.IO; |
25 | |
26 | namespace NETDataObjects. NDOVSPackage |
27 | { |
28 | ····/// <summary> |
29 | ····/// Zusammenfassung für ExtendedPath. |
30 | ····/// </summary> |
31 | ····public class ExtendedPath |
32 | ····{ |
33 | ········public static string GetRelativePath(string referencePath, string targetPath) |
34 | ········{ |
35 | ············string pathRoot1; |
36 | ············string pathRoot2; |
37 | |
38 | ············pathRoot1 = Path.GetPathRoot(referencePath); |
39 | ············pathRoot2 = Path.GetPathRoot(targetPath); |
40 | ············if (pathRoot1 == string.Empty && pathRoot2 == string.Empty) |
41 | ················throw new ArgumentException("Can't compute the relative path of two relative paths", "targetPath"); |
42 | |
43 | ············if (String.Compare(pathRoot1, pathRoot2, true) != 0) // includes pathRoot1 == empty |
44 | ················return targetPath; |
45 | |
46 | ············string rumpf1 = referencePath.Substring(pathRoot1.Length); |
47 | ············string rumpf2 = targetPath.Substring(pathRoot2.Length); |
48 | if ( rumpf1. EndsWith( "\\") || rumpf1. EndsWith( "/") ) |
49 | rumpf1 = rumpf1. Substring( 0, rumpf1. Length - 1) ; |
50 | |
51 | ············char[] sepChars = new char[]{Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}; |
52 | ············string[] arr1 = rumpf1.Split(sepChars); |
53 | ············string[] arr2 = rumpf2.Split(sepChars); |
54 | |
55 | ············int i; |
56 | ············for (i = 0; i < Math.Min(arr1.Length, arr2.Length); i++) |
57 | ············{ |
58 | ················if (string.Compare(arr1[i], arr2[i], true) != 0) |
59 | ····················break; |
60 | ············} |
61 | |
62 | ············string result = string.Empty; |
63 | ············string backpath = ".." + Path.DirectorySeparatorChar; |
64 | ············for (int j = arr1.Length - 1; j >= i; j--) |
65 | ············{ |
66 | ················result += backpath; |
67 | ············} |
68 | ············for (int j = i; j < arr2.Length; j++) |
69 | ············{ |
70 | ················result += arr2[j]; |
71 | ················if (j < arr2.Length - 1) |
72 | ····················result += Path.DirectorySeparatorChar; |
73 | ············} |
74 | |
75 | ············return result; |
76 | ········} |
77 | ····} |
78 | } |
79 |
New Commit (3030986)
1 | // |
2 | // Copyright (c) 2002-2019 Mirko Matytschak |
3 | // (www.netdataobjects.de) |
4 | // |
5 | // Author: Mirko Matytschak |
6 | // |
7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
11 | // conditions: |
12 | |
13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
14 | // of the Software. |
15 | // |
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. |
21 | |
22 | |
23 | using System; |
24 | using System.IO; |
25 | |
26 | namespace NDOVsPackage |
27 | { |
28 | ····/// <summary> |
29 | ····/// Zusammenfassung für ExtendedPath. |
30 | ····/// </summary> |
31 | ····public class ExtendedPath |
32 | ····{ |
33 | ········public static string GetRelativePath(string referencePath, string targetPath) |
34 | ········{ |
35 | ············string pathRoot1; |
36 | ············string pathRoot2; |
37 | |
38 | ············pathRoot1 = Path.GetPathRoot(referencePath); |
39 | ············pathRoot2 = Path.GetPathRoot(targetPath); |
40 | ············if (pathRoot1 == string.Empty && pathRoot2 == string.Empty) |
41 | ················throw new ArgumentException("Can't compute the relative path of two relative paths", "targetPath"); |
42 | |
43 | ············if (String.Compare(pathRoot1, pathRoot2, true) != 0) // includes pathRoot1 == empty |
44 | ················return targetPath; |
45 | |
46 | ············string rumpf1 = referencePath.Substring(pathRoot1.Length); |
47 | ············string rumpf2 = targetPath.Substring(pathRoot2.Length); |
48 | var dsc = Path. DirectorySeparatorChar; |
49 | var adsc = Path. AltDirectorySeparatorChar; |
50 | |
51 | ············rumpf1 = rumpf1.TrimEnd(dsc, adsc); |
52 | |
53 | ············char[] sepChars = new char[]{Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}; |
54 | ············string[] arr1 = rumpf1.Split(sepChars); |
55 | ············string[] arr2 = rumpf2.Split(sepChars); |
56 | |
57 | ············int i; |
58 | ············for (i = 0; i < Math.Min(arr1.Length, arr2.Length); i++) |
59 | ············{ |
60 | ················if (string.Compare(arr1[i], arr2[i], true) != 0) |
61 | ····················break; |
62 | ············} |
63 | |
64 | ············string result = string.Empty; |
65 | ············string backpath = ".." + Path.DirectorySeparatorChar; |
66 | ············for (int j = arr1.Length - 1; j >= i; j--) |
67 | ············{ |
68 | ················result += backpath; |
69 | ············} |
70 | ············for (int j = i; j < arr2.Length; j++) |
71 | ············{ |
72 | ················result += arr2[j]; |
73 | ················if (j < arr2.Length - 1) |
74 | ····················result += Path.DirectorySeparatorChar; |
75 | ············} |
76 | |
77 | ············return result; |
78 | ········} |
79 | ····} |
80 | } |
81 |