' System/IO/Path.ab

#require <Classes/System/Environment.ab>


Namespace System
Namespace IO


Class Path
Public
	Static AltDirectorySeparatorChar = &H2F As Char '/
	Static DirectorySeparatorChar = &H5C As Char    '\
	Static PathSeparator = &H3B As Char             ';
	Static VolumeSeparatorChar = &H3A As Char       ':

	Static Function GetFileName(path As String) As String
		Return path.Remove(0, getLastSeparatorPosision(path) + 1)
	End Function

	Static Function GetFileNameWithoutExtension(path As String) As String
		Dim fileName = GetFileName(path) As String
		Dim extPos = getExtensionPosition(fileName) As Long
		If extPos = -1 Then Return ""

		Return fileName.Remove(extPos)
	End Function

	'手抜き
	Static Function GetRandomFileName() As String
		Randomize
		Dim temp As Long
		temp = ((Rnd() * 900000000) As Long) + 10000000
		Return Str$(temp)
	End Function

	Static Function GetExtension(path As String) As String
		Dim extPos = getExtensionPosition(path) As Long
		If extPos = -1 Then Return ""

		Return path.Remove(0, extPos)
	End Function

	Static Function ChangeExtension(path As String, extension As String) As String
		Dim extPos = getExtensionPosition(path) As Long
		If extPos => 0 Then
			path = path.Remove(extPos)
		End If

		Return path + extension
	End Function

	Static Function HasExtension(ByRef path As String) As Boolean
		If GetExtension(path) <> "" Then
			Return True
		Else
			Return False
		End If
	End Function

	Static Function GetTempFileName() As String
		Dim tempPathSize = __GetTempPath(0, 0)
		Dim tempPath = _System_malloc(SizeOf (TCHAR) * tempPathSize) As PTSTR
		If tempPath = 0 Then
			' Throw OutOfMemoryException
			Debug
		End If
		If __GetTempPath(tempPathSize, tempPath) > tempPathSize Then
			' Throw IOException?
			Debug
		End If

		Dim tempFileName[ELM(MAX_PATH)] As TCHAR
		Dim len = __GetTempFileName(tempPath, "ABT", 0, tempFileName)
		_System_free(tempPath)
		Return New String(tempFileName, len As Long)
	End Function

	Static Function GetTempPath() As String
		Dim size = __GetTempPath(0, 0)
		Dim tempPath = _System_malloc(size) As PTSTR
		__GetTempPath(size, tempPath)
		GetTempPath = New String(tempPath)
		_System_free(tempPath)
	End Function

	Static Function GetFullPath(path As String) As String
		If IsPathRooted(path) Then
			Return path
		Else
			Return Environment.CurrentDirectory + Chr$(DirectorySeparatorChar) + path
		End If
	End Function

	Static Function GetDirectoryName(path As String) As String
		Dim lastSepPos = getLastSeparatorPosision(path) As Long
		If lastSepPos = -1 Then Return ""

		path = path.Remove(lastSepPos)
		If path.Length <= 3 Then
			If IsPathRooted(path) Then Return ""
		End If
		Return path
	End Function

	Static Function GetPathRoot(path As String) As String
		If IsPathRooted(path) Then
			Return path.Remove(3)
		Else
			Return ""
		End If
	End Function

	Static Function IsPathRooted(path As String) As Boolean
		If path.IndexOf(Chr$(VolumeSeparatorChar), 1, 1) = 1 Then
			Return True
		Else
			Return False
		End If
	End Function

	Static Function Combine(path1 As String, path2 As String) As String
		If path1.LastIndexOf(Chr$(VolumeSeparatorChar)) And path1.Length = 2 Then
			Return path1 + path2
		End If

		If path1.LastIndexOf(Chr$(DirectorySeparatorChar), ELM(path1.Length), 1) = -1 Then
			Return path1 + Chr$(DirectorySeparatorChar) + path2
		Else
			Return path1 + path2
		End If
	End Function

Private
	Static Function getExtensionPosition(ByRef path As String) As Long
		Dim lastSepPos = getLastSeparatorPosision(path) As Long
		getExtensionPosition = path.LastIndexOf(".", ELM(path.Length), path.Length - lastSepPos)
	End Function

	Static Function getLastSeparatorPosision(ByRef path As String) As Long
		Dim lastSepPos = path.LastIndexOf(Chr$(DirectorySeparatorChar)) As Long
		If lastSepPos <> -1 Then Return lastSepPos

		lastSepPos = path.LastIndexOf(Chr$(VolumeSeparatorChar))
		Return lastSepPos
	End Function
End Class

'今はメソッド内で使えないので、実装されるまで回避
Function __GetTempPath(nBufferLength As DWord, lpBuffer As LPSTR) As DWord
	Return GetTempPath(nBufferLength, lpBuffer)
End Function

Function __GetTempFileName(pPathName As PCSTR, pPrefixString As PCSTR, uUnique As DWord, pTempFileName As PSTR) As DWord
	Return GetTempFileName(pPathName, pPrefixString, uUnique, pTempFileName)
End Function


End Namespace
End Namespace
