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

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

Stringなどで例外を投げるようにした。
#147の解決。
CType ASCII文字判定関数群の追加。

File size: 4.5 KB
RevLine 
[142]1' System/IO/Path.ab
2
3#require <Classes/System/Environment.ab>
4
[271]5
6Namespace System
7Namespace IO
8
9
[61]10Class Path
11Public
[388]12 Static AltDirectorySeparatorChar = &H2F As StrChar '/
13 Static DirectorySeparatorChar = &H5C As StrChar '\
14 Static PathSeparator = &H3B As StrChar ';
15 Static VolumeSeparatorChar = &H3A As StrChar ':
[61]16
17 Static Function GetFileName(path As String) As String
[136]18 Return path.Remove(0, getLastSeparatorPosision(path) + 1)
[61]19 End Function
20
21 Static Function GetFileNameWithoutExtension(path As String) As String
[197]22 Dim fileName = GetFileName(path) As String
23 Dim extPos = getExtensionPosition(fileName) As Long
[61]24 If extPos = -1 Then Return ""
25
[136]26 Return fileName.Remove(extPos)
[61]27 End Function
28
29 '手抜き
30 Static Function GetRandomFileName() As String
31 Randomize
32 Dim temp As Long
[296]33 temp = ((Rnd() * 900000000) As Long) + 10000000
[61]34 Return Str$(temp)
35 End Function
36
37 Static Function GetExtension(path As String) As String
[197]38 Dim extPos = getExtensionPosition(path) As Long
[61]39 If extPos = -1 Then Return ""
40
[136]41 Return path.Remove(0, extPos)
[61]42 End Function
[142]43
[61]44 Static Function ChangeExtension(path As String, extension As String) As String
[197]45 Dim extPos = getExtensionPosition(path) As Long
[61]46 If extPos => 0 Then
[136]47 path = path.Remove(extPos)
[61]48 End If
49
50 Return path + extension
51 End Function
52
[125]53 Static Function HasExtension(ByRef path As String) As Boolean
[61]54 If GetExtension(path) <> "" Then
[142]55 Return True
[61]56 Else
[142]57 Return False
[61]58 End If
59 End Function
60
61 Static Function GetTempFileName() As String
[125]62 Dim tempPathSize = __GetTempPath(0, 0)
63 Dim tempPath = _System_malloc(SizeOf (TCHAR) * tempPathSize) As PTSTR
64 If tempPath = 0 Then
65 ' Throw OutOfMemoryException
66 Debug
67 End If
68 If __GetTempPath(tempPathSize, tempPath) > tempPathSize Then
69 ' Throw IOException?
70 Debug
71 End If
72
73 Dim tempFileName[ELM(MAX_PATH)] As TCHAR
[285]74 Dim len = __GetTempFileName(tempPath, "ABT", 0, tempFileName)
75 _System_free(tempPath)
76 Return New String(tempFileName, len As Long)
[61]77 End Function
78
79 Static Function GetTempPath() As String
[292]80 Dim size = __GetTempPath(0, 0)
81 Dim tempPath = _System_malloc(size) As PTSTR
[142]82 __GetTempPath(size, tempPath)
[281]83 GetTempPath = New String(tempPath)
[142]84 _System_free(tempPath)
[61]85 End Function
86
87 Static Function GetFullPath(path As String) As String
[142]88 If IsPathRooted(path) Then
89 Return path
90 Else
91 Return Environment.CurrentDirectory + Chr$(DirectorySeparatorChar) + path
92 End If
[61]93 End Function
94
95 Static Function GetDirectoryName(path As String) As String
[197]96 Dim lastSepPos = getLastSeparatorPosision(path) As Long
[61]97 If lastSepPos = -1 Then Return ""
98
[136]99 path = path.Remove(lastSepPos)
[61]100 If path.Length <= 3 Then
101 If IsPathRooted(path) Then Return ""
102 End If
103 Return path
104 End Function
105
106 Static Function GetPathRoot(path As String) As String
107 If IsPathRooted(path) Then
[136]108 Return path.Remove(3)
[61]109 Else
110 Return ""
111 End If
112 End Function
113
[142]114 Static Function IsPathRooted(path As String) As Boolean
[197]115 If path.IndexOf(Chr$(VolumeSeparatorChar), 1, 1) = 1 Then
[142]116 Return True
[61]117 Else
[142]118 Return False
[61]119 End If
120 End Function
121
122 Static Function Combine(path1 As String, path2 As String) As String
[388]123 If path1.LastIndexOf(VolumeSeparatorChar) And path1.Length = 2 Then
[61]124 Return path1 + path2
125 End If
126
[388]127 If path1.LastIndexOf(DirectorySeparatorChar, ELM(path1.Length), 1) = -1 Then
[197]128 Return path1 + Chr$(DirectorySeparatorChar) + path2
[61]129 Else
130 Return path1 + path2
131 End If
132 End Function
133
134Private
[388]135 Static Function getExtensionPosition(path As String) As Long
[197]136 Dim lastSepPos = getLastSeparatorPosision(path) As Long
[388]137 If lastSepPos = -1 Then
138 lastSepPos = 0
139 End If
140 getExtensionPosition = path.LastIndexOf(Asc("."), ELM(path.Length), path.Length - lastSepPos)
[61]141 End Function
142
[388]143 Static Function getLastSeparatorPosision(path As String) As Long
144 Dim lastSepPos = path.LastIndexOf(DirectorySeparatorChar) As Long
[62]145 If lastSepPos <> -1 Then Return lastSepPos
[61]146
[388]147 lastSepPos = path.LastIndexOf(VolumeSeparatorChar)
[61]148 Return lastSepPos
149 End Function
150End Class
151
152'今はメソッド内で使えないので、実装されるまで回避
153Function __GetTempPath(nBufferLength As DWord, lpBuffer As LPSTR) As DWord
154 Return GetTempPath(nBufferLength, lpBuffer)
[125]155End Function
156
157Function __GetTempFileName(pPathName As PCSTR, pPrefixString As PCSTR, uUnique As DWord, pTempFileName As PSTR) As DWord
158 Return GetTempFileName(pPathName, pPrefixString, uUnique, pTempFileName)
[271]159End Function
160
161
162End Namespace
163End Namespace
Note: See TracBrowser for help on using the repository browser.