source: trunk/Include/Classes/System/IO/Path.ab@ 497

Last change on this file since 497 was 497, checked in by イグトランス (egtra), 16 years ago

インクルードガードとその他不要な前処理定義などの削除

File size: 7.5 KB
Line 
1/*!
2@file Classes/System/IO/Path.ab
3@brief ファイルパス文字列を操作する
4@author OverTaker
5*/
6
7Namespace System
8Namespace IO
9
10Class Path
11Public
12 Static Const AltDirectorySeparatorChar = &H2F As Char '/
13 Static Const DirectorySeparatorChar = &H5C As Char '\
14 Static Const PathSeparator = &H3B As Char ';
15 Static Const VolumeSeparatorChar = &H3A As Char ':
16
17 '----------------------------------------------------------------
18 ' パブリック メソッド
19 '----------------------------------------------------------------
20
21 /*
22 @brief 拡張子を変更する
23 @param パス
24 @param 変更する拡張子(.を含む)
25 @return 拡張子変更後のパス
26 */
27 Static Function ChangeExtension(path As String, extension As String) As String
28 path = CheckPath(path)
29 If HasExtension(path) Then
30 Return path.Remove(GetExtensionIndex(path)) + extension
31 Else
32 Return path + extension
33 End If
34 End Function
35
36 /*
37 @brief 二つのパスを結合する
38 @param 結合されるパス
39 @param 結合するパス
40 @return パス
41 */
42 Static Function Combine(path1 As String, path2 As String) As String
43 path1 = CheckPath(path1)
44 path2 = CheckPath(path2)
45 If IsPathRooted(path2) Then
46 Return path2
47 Else
48 If Not path1.EndsWith(DirectorySeparatorChar) Then
49 path1 += Chr$(DirectorySeparatorChar)
50 End If
51 Return path1 + path2
52 End If
53 End Function
54
55 /*
56 @brief パス文字列からファイル名を取得する
57 @param パス
58 @return ファイル名
59 */
60 Static Function GetFileName(path As String) As String
61 path = CheckPath(path)
62 Dim lastSeparatorIndex = GetLastSeparatorIndex(path) As Long
63 If lastSeparatorIndex <> -1 Then
64 Return path.Substring(lastSeparatorIndex + 1)
65 Else
66 Return path
67 End If
68 End Function
69
70 /*
71 @brief パス文字列からファイル名を拡張子を除いて取得する
72 @param パス
73 @return 拡張子を除いたファイル名
74 */
75 Static Function GetFileNameWithoutExtension(path As String) As String
76 path = GetFileName(path)
77 If HasExtension(path) Then
78 Return path.Remove(GetExtensionIndex(path))
79 Else
80 Return path
81 End If
82 End Function
83
84 /*
85 @brief 絶対パスを取得する
86 @param 相対パス
87 @return 絶対パス
88 */
89 Static Function GetFullPath(path As String) As String
90 path = CheckPath(path)
91 If IsPathRooted(path) Then
92 Return path
93 Else
94 Return Combine(System.Environment.CurrentDirectory, path)
95 End If
96 End Function
97
98 /*
99 @brief パス文字列から拡張子を取得する
100 @param パス
101 @return .を含む拡張子
102 */
103 Static Function GetExtension(path As String) As String
104 path = CheckPath(path)
105 Dim extensionIndex = GetExtensionIndex(path)
106 If extensionIndex <> -1 Then
107 Return path.Substring(extensionIndex)
108 Else
109 Return New String()
110 End If
111 End Function
112
113 /*
114 @brief ひとつ上のディレクトリを取得する
115 @param パス
116 @return ディレクトリパス
117 */
118 Static Function GetDirectoryName(path As String) As String
119 path = CheckPath(path)
120 Dim lastSeparatorIndex = GetLastSeparatorIndex(path)
121 If lastSeparatorIndex = -1 Then
122 Return New String()
123 Else
124 Return path.Substring(0, lastSeparatorIndex)
125 End If
126 End Function
127
128 /*
129 @brief ルートディレクトリを取得する
130 @param パス
131 @return ルートディレクトリ
132 */
133 Static Function GetPathRoot(path As String) As String
134 path = CheckPath(path)
135 If IsPathRooted(path) Then
136 If path.Contains(UniformNamingConventionString) Then
137 Return path.Remove(path.IndexOf(DirectorySeparatorChar,
138 UniformNamingConventionString.Length) + 1)
139 Else
140 Return path.Remove(3)
141 End If
142 Else
143 Return New String()
144 End If
145 End Function
146
147 /*!
148 @brief ランダムなファイル名を取得する
149 @param ファイルパス
150 @return ファイル名
151 手抜き実装
152 */
153 Static Function GetRandomFileName() As String
154 Randomize
155 Return Str$(((Rnd() * 900000000) As Long) + 10000000)
156 End Function
157
158 /*!
159 @brief 一時ファイルを作成し、ファイルパスを取得する
160 @return パス
161 */
162 Static Function GetTempFileName() As String
163 Dim tempFileName[ELM(MAX_PATH)] As TCHAR
164 Dim len = WIN32API_GetTempFileName(GetTempPath(), "ABT", 0, tempFileName)
165 If len <> 0 Then
166 Return New String(tempFileName, len As Long)
167 Else
168 Throw New IOException("Path.GetTempFileName: Failed to GetTempFileName.")
169 End If
170 End Function
171
172
173 /*
174 @brief システムの一時フォルダのパス取得する
175 @return パス
176 */
177 Static Function GetTempPath() As String
178 Dim size = WIN32API_GetTempPath(0, 0)
179 Dim buf = New Text.StringBuilder(size)
180 buf.Length = size
181 Dim len = WIN32API_GetTempPath(size, StrPtr(buf))
182 If (len > size) or len = 0 Then
183 Throw New IOException("Path.GetTempPath: Failed to GetTempPath.")
184 Else
185 buf.Length = len
186 Return buf.ToString
187 End If
188 End Function
189
190
191 /*
192 @brief パスに拡張子が含まれるかどうか
193 @param パス
194 @retval True 含まれる
195 @retval False 含まれない
196 */
197 Static Function HasExtension(path As String) As Boolean
198 path = CheckPath(path)
199 Return GetExtensionIndex(path) <> -1
200 End Function
201
202 /*
203 @brief パスが絶対パスか取得する
204 @param パス
205 @retval True 絶対パス
206 @retval False 相対パス
207 */
208 Static Function IsPathRooted(path As String) As Boolean
209 path = CheckPath(path)
210 Return path.StartsWith(UniformNamingConventionString) _
211 or path.Contains(VolumeSeparatorChar) _
212 or path.StartsWith(DirectorySeparatorChar)
213 End Function
214
215Private
216 Static Const ExtensionSeparatorChar = &H2E As Char
217 Static Const InvalidPathChars = Ex"\q<>|\0\t" As String
218 Static Const UniformNamingConventionString = "\\" As String
219
220 '----------------------------------------------------------------
221 ' プライベート メソッド
222 '----------------------------------------------------------------
223
224 /*
225 @brief パスに不正な文字が含まれていないか調べる。
226 @param パス
227 @return 正しく直されたパス
228 パス末端にディレクトリ区切り記号があった場合除去する
229 ..\などが含まれるパスについては未実装
230 */
231 Static Function CheckPath(path As String) As String
232 path = path.Replace(AltDirectorySeparatorChar, DirectorySeparatorChar)
233
234 Dim i As Long
235 For i = 0 To ELM(InvalidPathChars.Length)
236 If path.Contains(InvalidPathChars[i]) Then
237 Throw New IOException("Path.CheckPath: The path contains invalidPathChars.")
238 End If
239 Next
240
241 Dim continuousIndex = path.IndexOf("\\", 1)
242 While continuousIndex <> -1
243 path = path.Remove(continuousIndex, 1)
244 continuousIndex = path.IndexOf("\\", continuousIndex)
245 Wend
246
247 If path.EndsWith(DirectorySeparatorChar) Then
248 Return path.Remove(path.Length - 1)
249 Else
250 Return path
251 End If
252 End Function
253
254 /*
255 @brief パスの最も後ろにあるディレクトリ区切り記号の位置を取得する
256 @param パス
257 @return インデックス
258 */
259 Static Function GetLastSeparatorIndex(path As String) As Long
260 Return System.Math.Max(path.LastIndexOf(DirectorySeparatorChar),
261 path.LastIndexOf(AltDirectorySeparatorChar)) As Long
262 End Function
263
264 /*
265 @brief 拡張子の位置を取得する。
266 @param パス
267 @return インデックス
268 */
269 Static Function GetExtensionIndex(path As String) As Long
270 Dim extensionIndex = path.LastIndexOf(ExtensionSeparatorChar) As Long
271 If extensionIndex > GetLastSeparatorIndex(path) Then
272 Return extensionIndex
273 Else
274 Return -1
275 End If
276 End Function
277End Class
278
279'メソッドと名前が被るAPI
280Function WIN32API_GetTempPath(nBufferLength As DWord, lpBuffer As PTSTR) As DWord
281 Return GetTempPath(nBufferLength, lpBuffer)
282End Function
283
284Function WIN32API_GetTempFileName (pPathName As PCTSTR, pPrefixString As PCTSTR, uUnique As DWord, pTempFileName As PTSTR) As DWord
285 Return GetTempFileName(pPathName, pPrefixString, uUnique, pTempFileName)
286End Function
287
288
289End Namespace
290End Namespace
Note: See TracBrowser for help on using the repository browser.