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

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

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

File size: 4.3 KB
RevLine 
[142]1' System/IO/Path.ab
2
3#require <Classes/System/Environment.ab>
4
[61]5Class Path
6Public
[125]7 Static AltDirectorySeparatorChar = &H2F As Char '/
8 Static DirectorySeparatorChar = &H5C As Char '\
9 Static PathSeparator = &H3B As Char ';
10 Static VolumeSeparatorChar = &H3A As Char ':
[61]11
12 Static Function GetFileName(path As String) As String
[136]13 Return path.Remove(0, getLastSeparatorPosision(path) + 1)
[61]14 End Function
15
16 Static Function GetFileNameWithoutExtension(path As String) As String
[197]17 Dim fileName = GetFileName(path) As String
18 Dim extPos = getExtensionPosition(fileName) As Long
[61]19 If extPos = -1 Then Return ""
20
[136]21 Return fileName.Remove(extPos)
[61]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
[197]33 Dim extPos = getExtensionPosition(path) As Long
[61]34 If extPos = -1 Then Return ""
35
[136]36 Return path.Remove(0, extPos)
[61]37 End Function
[142]38
[61]39 Static Function ChangeExtension(path As String, extension As String) As String
[197]40 Dim extPos = getExtensionPosition(path) As Long
[61]41 If extPos => 0 Then
[136]42 path = path.Remove(extPos)
[61]43 End If
44
45 Return path + extension
46 End Function
47
[125]48 Static Function HasExtension(ByRef path As String) As Boolean
[61]49 If GetExtension(path) <> "" Then
[142]50 Return True
[61]51 Else
[142]52 Return False
[61]53 End If
54 End Function
55
56 Static Function GetTempFileName() As String
[125]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
[61]72 End Function
73
74 Static Function GetTempPath() As String
[197]75 Dim size = GetTempPath(0, 0)
76 Dim tempPath = _System_malloc(size)
[142]77 __GetTempPath(size, tempPath)
78 GetTempPath = tempPath
79 _System_free(tempPath)
[61]80 End Function
81
82 Static Function GetFullPath(path As String) As String
[142]83 If IsPathRooted(path) Then
84 Return path
85 Else
86 Return Environment.CurrentDirectory + Chr$(DirectorySeparatorChar) + path
87 End If
[61]88 End Function
89
90 Static Function GetDirectoryName(path As String) As String
[197]91 Dim lastSepPos = getLastSeparatorPosision(path) As Long
[61]92 If lastSepPos = -1 Then Return ""
93
[136]94 path = path.Remove(lastSepPos)
[61]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
[136]103 Return path.Remove(3)
[61]104 Else
105 Return ""
106 End If
107 End Function
108
[142]109 Static Function IsPathRooted(path As String) As Boolean
[197]110 If path.IndexOf(Chr$(VolumeSeparatorChar), 1, 1) = 1 Then
[142]111 Return True
[61]112 Else
[142]113 Return False
[61]114 End If
115 End Function
116
117 Static Function Combine(path1 As String, path2 As String) As String
[197]118 If path1.LastIndexOf(Chr$(VolumeSeparatorChar)) And path1.Length = 2 Then
[61]119 Return path1 + path2
120 End If
121
[197]122 If path1.LastIndexOf(Chr$(DirectorySeparatorChar), ELM(path1.Length), 1) = -1 Then
123 Return path1 + Chr$(DirectorySeparatorChar) + path2
[61]124 Else
125 Return path1 + path2
126 End If
127 End Function
128
129Private
130 Static Function getExtensionPosition(ByRef path As String) As Long
[197]131 Dim lastSepPos = getLastSeparatorPosision(path) As Long
[61]132 getExtensionPosition = path.LastIndexOf(".", ELM(path.Length), path.Length - lastSepPos)
133 End Function
134
135 Static Function getLastSeparatorPosision(ByRef path As String) As Long
[197]136 Dim lastSepPos = path.LastIndexOf(Chr$(DirectorySeparatorChar)) As Long
[62]137 If lastSepPos <> -1 Then Return lastSepPos
[61]138
[197]139 lastSepPos = path.LastIndexOf(Chr$(VolumeSeparatorChar))
[61]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)
[125]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)
[61]151End Function
Note: See TracBrowser for help on using the repository browser.