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