source: Include/Classes/System/IO/Path.ab@ 271

Last change on this file since 271 was 271, checked in by dai, 17 years ago

SHFILEOPSTRUCT.hWnd → SHFILEOPSTRUCT.hwnd
System.IOに属するクラスに名前空間を適用した。

File size: 4.4 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
[125]12 Static AltDirectorySeparatorChar = &H2F As Char '/
13 Static DirectorySeparatorChar = &H5C As Char '\
14 Static PathSeparator = &H3B As Char ';
15 Static VolumeSeparatorChar = &H3A As Char ':
[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
33 temp = Rnd() * 900000000 + 10000000
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
74 __GetTempFileName(tempPath, "ABT", 0, tempFileName)
75 free(tempPath)
76 Return tempFileName
[61]77 End Function
78
79 Static Function GetTempPath() As String
[197]80 Dim size = GetTempPath(0, 0)
81 Dim tempPath = _System_malloc(size)
[142]82 __GetTempPath(size, tempPath)
83 GetTempPath = tempPath
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
[197]123 If path1.LastIndexOf(Chr$(VolumeSeparatorChar)) And path1.Length = 2 Then
[61]124 Return path1 + path2
125 End If
126
[197]127 If path1.LastIndexOf(Chr$(DirectorySeparatorChar), ELM(path1.Length), 1) = -1 Then
128 Return path1 + Chr$(DirectorySeparatorChar) + path2
[61]129 Else
130 Return path1 + path2
131 End If
132 End Function
133
134Private
135 Static Function getExtensionPosition(ByRef path As String) As Long
[197]136 Dim lastSepPos = getLastSeparatorPosision(path) As Long
[61]137 getExtensionPosition = path.LastIndexOf(".", ELM(path.Length), path.Length - lastSepPos)
138 End Function
139
140 Static Function getLastSeparatorPosision(ByRef path As String) As Long
[197]141 Dim lastSepPos = path.LastIndexOf(Chr$(DirectorySeparatorChar)) As Long
[62]142 If lastSepPos <> -1 Then Return lastSepPos
[61]143
[197]144 lastSepPos = path.LastIndexOf(Chr$(VolumeSeparatorChar))
[61]145 Return lastSepPos
146 End Function
147End Class
148
149'今はメソッド内で使えないので、実装されるまで回避
150Function __GetTempPath(nBufferLength As DWord, lpBuffer As LPSTR) As DWord
151 Return GetTempPath(nBufferLength, lpBuffer)
[125]152End Function
153
154Function __GetTempFileName(pPathName As PCSTR, pPrefixString As PCSTR, uUnique As DWord, pTempFileName As PSTR) As DWord
155 Return GetTempFileName(pPathName, pPrefixString, uUnique, pTempFileName)
[271]156End Function
157
158
159End Namespace
160End Namespace
Note: See TracBrowser for help on using the repository browser.