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

Last change on this file since 234 was 197, checked in by OverTaker, 18 years ago

タイプミスと若干のコード修整

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