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

Last change on this file since 407 was 407, checked in by OverTaker, 15 years ago

System/IO/Directory.ab実装。例外処理を徐々に実装。

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