{-# LANGUAGE DeriveDataTypeable #-}-- |-- Module: Filesystem.Path.Internal-- Copyright: 2010 John Millikin-- License: MIT---- Maintainer: jmillikin@gmail.com-- Portability: portable--moduleFilesystem.Path.InternalwhereimportPreludehiding(FilePath)importqualifiedData.ByteString.Char8asB8importData.Data(Data)importData.List(intersperse)importqualifiedData.TextasTimportqualifiedData.Text.EncodingasTEimportData.Typeable(Typeable)--------------------------------------------------------------------------------- File Paths-------------------------------------------------------------------------------dataChunk=Chunk{chunkText::T.Text,chunkGood::Bool}deriving(Ord,Data,Typeable)instanceEqChunkwhere(Chunkx_)==(Chunky_)=x==ytypeDirectory=ChunktypeBasename=ChunktypeExtension=ChunkdataRoot=RootPosix|RootWindowsVolumeChar|RootWindowsCurrentVolumederiving(Eq,Ord,Data,Typeable)dataFilePath=FilePath{pathRoot::MaybeRoot,pathDirectories::[Directory],pathBasename::MaybeBasename,pathExtensions::[Extension]}deriving(Eq,Ord,Data,Typeable)-- | A file path with no root, directory, or filenameempty::FilePathempty=FilePathNothing[]Nothing[]dot::Chunkdot=Chunk(T.pack".")Truedots::Chunkdots=Chunk(T.pack"..")TruefilenameChunk::Bool->FilePath->ChunkfilenameChunkstrictp=Chunk(T.concattexts)allGoodwherename=maybe(ChunkT.emptyTrue)id(pathBasenamep)exts=casepathExtensionspof[]->[]exts'->interspersedot((ChunkT.emptyTrue):exts')chunks=name:extstexts=mapchunkText'chunksallGood=and(mapchunkGoodchunks)chunkText'c=ifchunkGoodcthenifallGood||notstrictthenchunkTextcelseT.pack(B8.unpack(TE.encodeUtf8(chunkTextc)))elsechunkTextc--------------------------------------------------------------------------------- Rules-------------------------------------------------------------------------------dataRulesplatformFormat=Rules{rulesName::T.Text-- | Check if a 'FilePath' is valid; it must not contain any illegal-- characters, and must have a root appropriate to the current-- 'Rules'.,valid::FilePath->Bool-- | Split a search path, such as @$PATH@ or @$PYTHONPATH@, into-- a list of 'FilePath's.,splitSearchPath::platformFormat->[FilePath]-- | Attempt to convert a 'FilePath' to human&#x2010;readable text.---- If the path is decoded successfully, the result is a 'Right'-- containing the decoded text. Successfully decoded text can be-- converted back to the original path using 'fromText'.---- If the path cannot be decoded, the result is a 'Left' containing an-- approximation of the original path. If displayed to the user, this-- value should be accompanied by some warning that the path has an-- invalid encoding. Approximated text cannot be converted back to the-- original path.---- This function ignores the user&#x2019;s locale, and assumes all-- file paths are encoded in UTF8. If you need to display file paths-- with an unusual or obscure encoding, use 'encode' and then decode-- them manually.---- Since: 0.2,toText::FilePath->EitherT.TextT.Text-- | Convert human&#x2010;readable text into a 'FilePath'.---- This function ignores the user&#x2019;s locale, and assumes all-- file paths are encoded in UTF8. If you need to create file paths-- with an unusual or obscure encoding, encode them manually and then-- use 'decode'.---- Since: 0.2,fromText::T.Text->FilePath-- | Convert a 'FilePath' to a platform&#x2010;specific format,-- suitable for use with external OS functions.---- Since: 0.3,encode::FilePath->platformFormat-- | Convert a 'FilePath' from a platform&#x2010;specific format,-- suitable for use with external OS functions.---- Since: 0.3,decode::platformFormat->FilePath-- | Attempt to convert a 'FilePath' to a string suitable for use with-- functions in @System.IO@. The contents of this string are-- platform&#x2010;dependent, and are not guaranteed to be-- human&#x2010;readable. For converting 'FilePath's to a-- human&#x2010;readable format, use 'toText'.---- Since: 0.3.1,encodeString::FilePath->String-- | Attempt to parse a 'FilePath' from a string suitable for use-- with functions in @System.IO@. Do not use this function for parsing-- human&#x2010;readable paths, as the character set decoding is-- platform&#x2010;dependent. For converting human&#x2010;readable-- text to a 'FilePath', use 'fromText'.---- Since: 0.3.1,decodeString::String->FilePath}instanceShow(Rulesa)whereshowsPrecdr=showParen(d>10)(showString"Rules ".shows(rulesNamer))