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

Last change on this file since 385 was 296, checked in by NoWest, 17 years ago
File size: 4.4 KB
Line 
1' System/IO/Path.ab
2
3#require <Classes/System/Environment.ab>
4
5
6Namespace System
7Namespace IO
8
9
10Class Path
11Public
12 Static AltDirectorySeparatorChar = &H2F As Char '/
13 Static DirectorySeparatorChar = &H5C As Char '\
14 Static PathSeparator = &H3B As Char ';
15 Static VolumeSeparatorChar = &H3A As Char ':
16
17 Static Function GetFileName(path As String) As String
18 Return path.Remove(0, getLastSeparatorPosision(path) + 1)
19 End Function
20
21 Static Function GetFileNameWithoutExtension(path As String) As String
22 Dim fileName = GetFileName(path) As String
23 Dim extPos = getExtensionPosition(fileName) As Long
24 If extPos = -1 Then Return ""
25
26 Return fileName.Remove(extPos)
27 End Function
28
29 '手抜き
30 Static Function GetRandomFileName() As String
31 Randomize
32 Dim temp As Long
33 temp = ((Rnd() * 900000000) As Long) + 10000000
34 Return Str$(temp)
35 End Function
36
37 Static Function GetExtension(path As String) As String
38 Dim extPos = getExtensionPosition(path) As Long
39 If extPos = -1 Then Return ""
40
41 Return path.Remove(0, extPos)
42 End Function
43
44 Static Function ChangeExtension(path As String, extension As String) As String
45 Dim extPos = getExtensionPosition(path) As Long
46 If extPos => 0 Then
47 path = path.Remove(extPos)
48 End If
49
50 Return path + extension
51 End Function
52
53 Static Function HasExtension(ByRef path As String) As Boolean
54 If GetExtension(path) <> "" Then
55 Return True
56 Else
57 Return False
58 End If
59 End Function
60
61 Static Function GetTempFileName() As String
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 Dim len = __GetTempFileName(tempPath, "ABT", 0, tempFileName)
75 _System_free(tempPath)
76 Return New String(tempFileName, len As Long)
77 End Function
78
79 Static Function GetTempPath() As String
80 Dim size = __GetTempPath(0, 0)
81 Dim tempPath = _System_malloc(size) As PTSTR
82 __GetTempPath(size, tempPath)
83 GetTempPath = New String(tempPath)
84 _System_free(tempPath)
85 End Function
86
87 Static Function GetFullPath(path As String) As String
88 If IsPathRooted(path) Then
89 Return path
90 Else
91 Return Environment.CurrentDirectory + Chr$(DirectorySeparatorChar) + path
92 End If
93 End Function
94
95 Static Function GetDirectoryName(path As String) As String
96 Dim lastSepPos = getLastSeparatorPosision(path) As Long
97 If lastSepPos = -1 Then Return ""
98
99 path = path.Remove(lastSepPos)
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
108 Return path.Remove(3)
109 Else
110 Return ""
111 End If
112 End Function
113
114 Static Function IsPathRooted(path As String) As Boolean
115 If path.IndexOf(Chr$(VolumeSeparatorChar), 1, 1) = 1 Then
116 Return True
117 Else
118 Return False
119 End If
120 End Function
121
122 Static Function Combine(path1 As String, path2 As String) As String
123 If path1.LastIndexOf(Chr$(VolumeSeparatorChar)) And path1.Length = 2 Then
124 Return path1 + path2
125 End If
126
127 If path1.LastIndexOf(Chr$(DirectorySeparatorChar), ELM(path1.Length), 1) = -1 Then
128 Return path1 + Chr$(DirectorySeparatorChar) + path2
129 Else
130 Return path1 + path2
131 End If
132 End Function
133
134Private
135 Static Function getExtensionPosition(ByRef path As String) As Long
136 Dim lastSepPos = getLastSeparatorPosision(path) As Long
137 getExtensionPosition = path.LastIndexOf(".", ELM(path.Length), path.Length - lastSepPos)
138 End Function
139
140 Static Function getLastSeparatorPosision(ByRef path As String) As Long
141 Dim lastSepPos = path.LastIndexOf(Chr$(DirectorySeparatorChar)) As Long
142 If lastSepPos <> -1 Then Return lastSepPos
143
144 lastSepPos = path.LastIndexOf(Chr$(VolumeSeparatorChar))
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)
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)
156End Function
157
158
159End Namespace
160End Namespace
Note: See TracBrowser for help on using the repository browser.