Révision | 7e779dbc3b5aa498b2b0b9ad1bef6cfad306e822 (tree) |
---|---|
l'heure | 2022-08-06 01:29:29 |
Auteur | yoshy <yoshy.org.bitbucket@gz.j...> |
Commiter | yoshy |
[ADD] PathHelper クラスを追加
@@ -0,0 +1,121 @@ | ||
1 | +using NLog; | |
2 | +using Reactive.Bindings; | |
3 | +using System.IO; | |
4 | + | |
5 | +namespace CleanAuLait.Core.IO | |
6 | +{ | |
7 | + public static class PathHelper | |
8 | + { | |
9 | + private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); | |
10 | + | |
11 | + public static readonly string RELATIVE_PATH_CURRENT = "."; | |
12 | + public static readonly string RELATIVE_PATH_UPWARDS = ".."; | |
13 | + | |
14 | + public static readonly string DUMMY_ROOT_PATH = @"z:\"; | |
15 | + | |
16 | + public static string CreateCanonicalPath(string path, string name) | |
17 | + { | |
18 | + if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(name)) | |
19 | + { | |
20 | + return null; | |
21 | + } | |
22 | + | |
23 | + return CreateCanonicalPath(Path.Combine(path, name)); | |
24 | + } | |
25 | + | |
26 | + public static string CreateCanonicalPath(string path) | |
27 | + { | |
28 | + if (string.IsNullOrEmpty(path)) | |
29 | + { | |
30 | + return null; | |
31 | + } | |
32 | + | |
33 | + return Path.GetFullPath(path); | |
34 | + } | |
35 | + | |
36 | + public static string CreateCanonicalRelativePath(string path, string name) | |
37 | + { | |
38 | + if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(name)) | |
39 | + { | |
40 | + return null; | |
41 | + } | |
42 | + | |
43 | + return CreateCanonicalRelativePath(Path.Combine(path, name)); | |
44 | + } | |
45 | + | |
46 | + public static string CreateCanonicalRelativePath(string path) | |
47 | + { | |
48 | + string canonicalPath = CreateCanonicalPath(DUMMY_ROOT_PATH, path); | |
49 | + | |
50 | + return Path.GetRelativePath(DUMMY_ROOT_PATH, canonicalPath); | |
51 | + } | |
52 | + | |
53 | + public static bool IsChild(string basePath, string targetPath) | |
54 | + { | |
55 | + if (!Path.IsPathFullyQualified(basePath) | |
56 | + || !Path.IsPathFullyQualified(targetPath)) | |
57 | + { | |
58 | + return false; | |
59 | + } | |
60 | + | |
61 | + | |
62 | + (bool isSameDirectory, bool isParentDirectory, bool isAnotherDrive) = CheckPathRelation(basePath, targetPath); | |
63 | + | |
64 | + return !isSameDirectory && !isParentDirectory && !isAnotherDrive; | |
65 | + } | |
66 | + | |
67 | + public static bool IsSameOrChild(string basePath, string targetPath) | |
68 | + { | |
69 | + if (!Path.IsPathFullyQualified(basePath) | |
70 | + || !Path.IsPathFullyQualified(targetPath)) | |
71 | + { | |
72 | + return false; | |
73 | + } | |
74 | + | |
75 | + | |
76 | + (_, bool isParentDirectory, bool isAnotherDrive) = CheckPathRelation(basePath, targetPath); | |
77 | + | |
78 | + return !isParentDirectory && !isAnotherDrive; | |
79 | + } | |
80 | + | |
81 | + private static (bool isSameDirectory, bool isParentDirectory, bool isAnotherDrive) | |
82 | + CheckPathRelation(string basePath, string targetPath) | |
83 | + { | |
84 | + if (!Path.IsPathFullyQualified(basePath) | |
85 | + || !Path.IsPathFullyQualified(targetPath)) | |
86 | + { | |
87 | + throw new ArgumentException("path must be fully qualified."); | |
88 | + } | |
89 | + | |
90 | + string relativePath = Path.GetRelativePath(basePath, targetPath); | |
91 | + | |
92 | + // basePath == targetPath | |
93 | + bool isSameDirectory = relativePath == RELATIVE_PATH_CURRENT; | |
94 | + | |
95 | + // basePath ⊂ targetPath | |
96 | + bool isParentDirectory = relativePath.StartsWith(RELATIVE_PATH_UPWARDS); | |
97 | + | |
98 | + // basePath's Drive != targetPath's Drive | |
99 | + bool isAnotherDrive = Path.IsPathRooted(relativePath); | |
100 | + | |
101 | + return (isSameDirectory, isParentDirectory, isAnotherDrive); | |
102 | + } | |
103 | + | |
104 | + public static string EnsureLeadingDotPath(string relativePath) | |
105 | + { | |
106 | + return relativePath == RELATIVE_PATH_CURRENT | |
107 | + ? relativePath | |
108 | + : Path.Combine(RELATIVE_PATH_CURRENT, relativePath); | |
109 | + } | |
110 | + | |
111 | + public static string EnsureEndingDirectorySeparator(string path) | |
112 | + { | |
113 | + if (!Path.EndsInDirectorySeparator(path)) | |
114 | + { | |
115 | + return path + Path.DirectorySeparatorChar; | |
116 | + } | |
117 | + | |
118 | + return path; | |
119 | + } | |
120 | + } | |
121 | +} |