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

Last change on this file since 136 was 136, checked in by OverTaker, 17 years ago

String変更による修整。

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