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

Last change on this file since 142 was 142, checked in by イグトランス (egtra), 16 years ago

Environment, OperatingSystem?, Versionの追加、Unicode対応修正ほか

File size: 4.6 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 As String
18        Dim extPos As Long
19
20        fileName = GetFileName(path)
21        extPos = getExtensionPosition(fileName)
22        If extPos = -1 Then Return ""
23
24        Return fileName.Remove(extPos)
25    End Function
26
27    '手抜き
28    Static Function GetRandomFileName() As String
29        Randomize
30        Dim temp As Long
31        temp = Rnd() * 900000000 + 10000000
32        Return Str$(temp)
33    End Function
34
35    Static Function GetExtension(path As String) As String
36        Dim extPos As Long
37        extPos = getExtensionPosition(path)
38        If extPos = -1 Then Return ""
39
40        Return path.Remove(0, extPos)
41    End Function
42
43    Static Function ChangeExtension(path As String, extension As String) As String
44        Dim extPos As Long
45        extPos = getExtensionPosition(path)
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        __GetTempFileName(tempPath, "ABT", 0, tempFileName)
75        free(tempPath)
76        Return tempFileName
77    End Function
78
79    Static Function GetTempPath() As String
80        Dim size = GetTempPath(0, 0
81        Dim tempPath = _System_malloc(size))
82        __GetTempPath(size, tempPath)
83        GetTempPath = 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 As Long
97        lastSepPos = getLastSeparatorPosision(path)
98        If lastSepPos = -1 Then Return ""
99
100        path = path.Remove(lastSepPos)
101        If path.Length <= 3 Then
102            If IsPathRooted(path) Then Return ""
103        End If
104        Return path
105    End Function
106
107    Static Function GetPathRoot(path As String) As String
108        If IsPathRooted(path) Then
109            Return path.Remove(3)
110        Else
111            Return ""
112        End If
113    End Function
114
115    Static Function IsPathRooted(path As String) As Boolean
116        Dim volSepChar As String(Chr$(VolumeSeparatorChar))
117        If path.IndexOf(volSepChar, 1, 1) = 1 Then
118            Return True
119        Else
120            Return False
121        End If
122    End Function
123
124    Static Function Combine(path1 As String, path2 As String) As String
125        Dim volSepChar As String(Chr$(VolumeSeparatorChar))
126        Dim dirSepChar As String(Chr$(DirectorySeparatorChar))
127        If path1.LastIndexOf(volSepChar) And path1.Length = 2 Then
128            Return path1 + path2
129        End If
130
131        If path1.LastIndexOf(dirSepChar, ELM(path1.Length), 1) = -1 Then
132            Return path1 + dirSepChar + path2
133        Else
134            Return path1 + path2
135        End If
136    End Function
137
138Private
139    Static Function getExtensionPosition(ByRef path As String) As Long
140        Dim lastSepPos As Long
141        lastSepPos = getLastSeparatorPosision(path)
142        getExtensionPosition = path.LastIndexOf(".", ELM(path.Length), path.Length - lastSepPos)
143    End Function
144
145    Static Function getLastSeparatorPosision(ByRef path As String) As Long
146        Dim lastSepPos As Long
147        Dim dirSepChar As String(Chr$(DirectorySeparatorChar))
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
158End Class
159
160'今はメソッド内で使えないので、実装されるまで回避
161Function __GetTempPath(nBufferLength As DWord, lpBuffer As LPSTR) As DWord
162    Return GetTempPath(nBufferLength, lpBuffer)
163End Function
164
165Function __GetTempFileName(pPathName As PCSTR, pPrefixString As PCSTR, uUnique As DWord, pTempFileName As PSTR) As DWord
166    Return GetTempFileName(pPathName, pPrefixString, uUnique, pTempFileName)
167End Function
Note: See TracBrowser for help on using the repository browser.