// path.inc = dir/path/file shell sections

//
//	Return the directory portion of a path
//
//		$0 = Path
//
//		$R0 = Directory
//
[DirectoryFromPath]

	set Path = $($0)
	_Enter("DirectoryFromPath", _Show(Path))

	set Directory = EMPTY_STR

	Split-String $(Path) "\\" PathComponents
	QueryListSize Size $(PathComponents)

	ifint $(Size) == 1
		goto Exit
	endif

	ForListDo $(PathComponents)
		ifint $(#) < $(Size)
			set Directory = $(Directory)$($)
		endif
	EndForListDo

	_Exit("DirectoryFromPath", _Show(Directory))
	Return $(Directory)

//
//	Return the filename portion of a path
//
//		$0 = Path
//
//		$R0 = Filename
//
[FilenameFromPath]

	set Path = $($0)
	_Enter("FilenameFromPath", _Show(Path))

	Split-String $(Path) "\\" PathComponents
	QueryListSize Size $(PathComponents)

	set Filename = *($(PathComponents), $(Size))

	_Exit("FilenameFromPath", _Show(Filename))
	Return $(Filename)

//
//	Return the base portion (everything but extension) of a filename.
//
//		$0 = Filename
//
//		$R0 = Base
//
[BaseFromFilename]

	set Filename = $($0)
	_Enter("BaseFromFilename", _Show(Filename))

	Split-String $(Filename) "." FilenameComponents
	QueryListSize Size $(FilenameComponents)

	set Dot = $(Size)
	ifstr *($(FilenameComponents), $(Dot)) == "."
		// Filename has no extension.
		goto Make
	endif

	set-sub Dot = $(Dot), 1
	ifstr *($(FilenameComponents), $(Dot)) == "."
		// Filename has extension.
		goto Make
	endif

	// Filename has no extension or ".".
	set Base = $(Filename)
	goto Exit

Make = +

	// Remake rilename up to the "." found.
	set Base = EMPTY_STR
	ForListDo $(FilenameComponents)
		ifint $(#) < $(Dot)
			set Base = $(Base)$($)
		endif
	EndForListDo

Exit = +

	_Exit("BaseFromFilename", _Show(Base))
	Return $(Base)

//
//	Return the extension portion of a filename
//
//		$0 = Filename
//
//		$R0 = Extension
//
[ExtensionFromFilename]

	set Filename = $($0)
	_Enter("ExtensionFromFilename", _Show(Filename))

	set Extension = EMPTY_STR

	Split-String $(Filename) "." FilenameComponents
	QueryListSize Size $(FilenameComponents)

	set Dot = $(Size)
	ifstr *($(FilenameComponents), $(Dot)) == "."
		// Filename has no extension.
		goto Exit
	endif

	set-sub Dot = $(Dot), 1
	ifstr *($(FilenameComponents), $(Dot)) == "."
		// Filename has extension.
		goto Make
	endif

	// Filename has no extension or ".".
	goto Exit

Make = +

	set Extension = *($(FilenameComponents), Size)

Exit = +

	_Exit("ExtensionFromFilename", _Show(Extension))
	Return $(Extension)

//
//	Return the base potion of the filename contained in a path.
//
//		$0 = Path
//
//		$R0 = Base
//
[BaseFromPath]

	set Path = $($0)
	_Enter("BaseFromPath", _Show(Path))

	_LocalShell FilenameFromPath $(Path)
	set Filename = $($R0)

	_LocalShell BaseFromFilename $(Filename)
	set Base = $($R0)

	_Exit("BaseFromPath", _Show(Base))
	Return $(Base)
