liblawless-0.26.0: Prelude based on protolude for GHC 8 and beyond.

Copyright© 2016 All rights reserved.
LicenseGPL-3
MaintainerEvan Cofsky <>
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Path

Contents

Description

 

Synopsis

Documentation

addExtension :: FilePath os ar -> String -> FilePath os ar #

Add an extension, even if there is already one there. E.g. addExtension "foo.txt" "bat" -> "foo.txt.bat".

> Path.addExtension (relFile "file.txt") "bib" == Posix.relFile "file.txt.bib"
> Path.addExtension (relFile "file.") ".bib" == Posix.relFile "file..bib"
> Path.addExtension (relFile "file") ".bib" == Posix.relFile "file.bib"
> Path.addExtension Path.emptyFile "bib" == Posix.relFile ".bib"
> Path.addExtension Path.emptyFile ".bib" == Posix.relFile ".bib"
> Path.takeFileName (Path.addExtension Path.emptyFile "ext") == Posix.relFile ".ext"

combine :: DirPath os ar -> RelPath os fd -> Path os ar fd #

Join an (absolute or relative) directory path with a relative (file or directory) path to form a new path.

dropExtension :: FilePath os ar -> FilePath os ar #

Remove last extension, and the "." preceding it.

> Path.dropExtension x == fst (Path.splitExtension x)

dropExtensions :: FilePath os ar -> FilePath os ar #

Drop all extensions

> not $ Path.hasAnExtension (Path.dropExtensions x)

dropFileName :: FilePath os ar -> DirPath os ar #

Synonym for takeDirectory

mapFileName :: (String -> String) -> FilePath os ar -> FilePath os ar #

mapFileNameF :: Functor f => (String -> f String) -> FilePath os ar -> f (FilePath os ar) #

replaceDirectory :: FilePath os ar1 -> DirPath os ar2 -> FilePath os ar2 #

replaceExtension :: FilePath os ar -> String -> FilePath os ar #

Set the extension of a file, overwriting one if already present.

> Path.replaceExtension (relFile "file.txt") ".bob" == Posix.relFile "file.bob"
> Path.replaceExtension (relFile "file.txt") "bob" == Posix.relFile "file.bob"
> Path.replaceExtension (relFile "file") ".bob" == Posix.relFile "file.bob"
> Path.replaceExtension (relFile "file.txt") "" == Posix.relFile "file"
> Path.replaceExtension (relFile "file.fred.bob") "txt" == Posix.relFile "file.fred.txt"

splitDirName :: DirPath os ar -> Maybe (DirPath os ar, RelDir os) #

splitExtension :: FilePath os ar -> (FilePath os ar, String) #

Split on the extension. addExtension is the inverse.

> uncurry (<.>) (Path.splitExtension x) == x
> uncurry Path.addExtension (Path.splitExtension x) == x
> Path.splitExtension (relFile "file.txt") == (Posix.relFile "file",".txt")
> Path.splitExtension (relFile ".bashrc") == (Posix.emptyFile, ".bashrc")
> Path.splitExtension (relFile "file") == (Posix.relFile "file","")
> Path.splitExtension (relFile "file/file.txt") == (Posix.relFile "file/file",".txt")
> Path.splitExtension (relFile "file.txt/boris") == (Posix.relFile "file.txt/boris","")
> Path.splitExtension (relFile "file.txt/boris.ext") == (Posix.relFile "file.txt/boris",".ext")
> Path.splitExtension (relFile "file/path.txt.bob.fred") == (Posix.relFile "file/path.txt.bob",".fred")

splitExtensions :: FilePath os ar -> (FilePath os ar, String) #

Split on all extensions

> Path.splitExtensions (relFile "file.tar.gz") == (Posix.relFile "file",".tar.gz")

splitFileName :: FilePath os ar -> (DirPath os ar, RelFile os) #

splitPath :: (AbsRel ar, FileOrDir fd) => Path os ar fd -> (Bool, [RelDir os], Maybe (RelFile os)) #

Deconstructs a path into its components.

> Path.splitPath (Posix.absDir "/tmp/someDir/mydir.dir") == (True, map relDir ["tmp","someDir","mydir.dir"], Nothing)
> Path.splitPath (Posix.absFile "/tmp/someDir/myfile.txt") == (True, map relDir ["tmp","someDir"], Just $ relFile "myfile.txt")

takeBaseName :: FilePath os ar -> RelFile os #

Get the basename of a file

> Path.takeBaseName (absFile "/tmp/somedir/myfile.txt") == Posix.relFile "myfile"
> Path.takeBaseName (relFile "./myfile.txt") == Posix.relFile "myfile"
> Path.takeBaseName (relFile "myfile.txt") == Posix.relFile "myfile"

takeDirName :: DirPath os ar -> Maybe (RelDir os) #

takeDirectory :: FilePath os ar -> DirPath os ar #

takeExtension :: FilePath os ar -> String #

Get the extension of a file, returns "" for no extension, .ext otherwise.

> Path.takeExtension x == snd (Path.splitExtension x)
> Path.takeExtension (Path.addExtension x "ext") == ".ext"
> Path.takeExtension (Path.replaceExtension x "ext") == ".ext"

takeExtensions :: FilePath os ar -> String #

Get all extensions

> Path.takeExtensions (Posix.relFile "file.tar.gz") == ".tar.gz"

takeFileName :: FilePath os ar -> RelFile os #

Get the filename component of a file path (ie stripping all parent dirs)

> Path.takeFileName (absFile "/tmp/somedir/myfile.txt") == Posix.relFile "myfile.txt"
> Path.takeFileName (relFile "./myfile.txt") == Posix.relFile "myfile.txt"
> Path.takeFileName (relFile "myfile.txt") == Posix.relFile "myfile.txt"

toString :: (AbsRel ar, FileDir fd) => Path ar fd -> String #

Auxillary Manipulation Functions

dirFromFile :: FilePath os ar -> DirPath os ar #

Convert a file to a directory path. Obviously, the corresponding disk object won't change accordingly. The purpose of this function is to be an intermediate step when deriving a directory name from a file name.

dynamicMakeAbsolute :: System os => AbsDir os -> AbsRelPath os fd -> AbsPath os fd #

fileFromDir :: DirPath os ar -> Maybe (FilePath os ar) #

Convert a directory to a file path. The function returns Nothing if the directory path is empty. The purpose of this function is to be an intermediate step when deriving a file name from a directory name.

fromFileDir :: FileDir fd => FileDirPath os ar -> Maybe (Path os ar fd) #

genericMakeAbsolute :: (System os, AbsRel ar) => AbsDir os -> Path os ar fd -> AbsPath os fd #

As for makeAbsolute, but for use when the path may already be absolute (in which case it is left unchanged). You should avoid the use of genericMakeAbsolute-type functions, because then you avoid to absolutize a path that was already absolutized.

> Path.genericMakeAbsolute (absDir "/tmp") (relFile "file.txt")       == Posix.absFile "/tmp/file.txt"
> Path.genericMakeAbsolute (absDir "/tmp") (relFile "adir/file.txt")  == Posix.absFile "/tmp/adir/file.txt"
> Path.genericMakeAbsolute (absDir "/tmp") (absFile "/adir/file.txt") == Posix.absFile "/adir/file.txt"

genericMakeAbsoluteFromCwd :: (System os, AbsRel ar) => Path os ar fd -> IO (AbsPath os fd) #

As for makeAbsoluteFromCwd, but for use when the path may already be absolute (in which case it is left unchanged).

joinPath :: FileDir fd => [String] -> RelPath os fd #

Constructs a RelPath from a list of components. It is an unchecked error if the path components contain path separators. It is an unchecked error if a RelFile path is empty.

> Path.joinPath ["tmp","someDir","dir"] == Posix.relDir "tmp/someDir/dir"
> Path.joinPath ["tmp","someDir","file.txt"] == Posix.relFile "tmp/someDir/file.txt"

makeAbsolute :: System os => AbsDir os -> RelPath os fd -> AbsPath os fd #

Joins an absolute directory with a relative path to construct a new absolute path.

> Path.makeAbsolute (absDir "/tmp") (relFile "file.txt")      == Posix.absFile "/tmp/file.txt"
> Path.makeAbsolute (absDir "/tmp") (relFile "adir/file.txt") == Posix.absFile "/tmp/adir/file.txt"
> Path.makeAbsolute (absDir "/tmp") (relDir  "adir/dir")      == Posix.absDir "/tmp/adir/dir"

makeAbsoluteFromCwd :: System os => RelPath os fd -> IO (AbsPath os fd) #

Converts a relative path into an absolute one by prepending the current working directory.

makeRelative :: (System os, FileDir fd) => AbsDir os -> AbsPath os fd -> RelPath os fd #

This function can be used to construct a relative path by removing the supplied AbsDir from the front. It is a runtime error if the supplied AbsPath doesn't start with the AbsDir.

> Path.makeRelative (absDir "/tmp/somedir") (absFile "/tmp/somedir/anotherdir/file.txt") == Posix.relFile "anotherdir/file.txt"
> Path.makeRelative (absDir "/tmp/somedir") (absDir "/tmp/somedir/anotherdir/dir") == Posix.relDir "anotherdir/dir"
> Path.makeRelative (absDir "c:\\tmp\\somedir") (absFile "C:\\Tmp\\SomeDir\\AnotherDir\\File.txt") == Windows.relFile "AnotherDir\\File.txt"
> Path.makeRelative (absDir "c:\\tmp\\somedir") (absDir "c:\\tmp\\somedir\\anotherdir\\dir") == Windows.relDir "anotherdir\\dir"
> Path.makeRelative (absDir "c:tmp\\somedir") (absDir "c:tmp\\somedir\\anotherdir\\dir") == Windows.relDir "anotherdir\\dir"

makeRelativeMaybe :: (System os, FileDir fd) => AbsDir os -> AbsPath os fd -> Maybe (RelPath os fd) #

normalise :: System os => Path os ar fd -> Path os ar fd #

Currently just transforms:

> Path.normalise (absFile "/tmp/fred/./jim/./file") == Posix.absFile "/tmp/fred/jim/file"

pathMap :: FileDir fd => (String -> String) -> Path os ar fd -> Path os ar fd #

Map over the components of the path.

> Path.pathMap (map toLower) (absDir "/tmp/Reports/SpreadSheets") == Posix.absDir "/tmp/reports/spreadsheets"

toFileDir :: FileDir fd => Path os ar fd -> FileDirPath os ar #

Path Predicates

isAbsolute :: AbsRel ar => Path os ar fd -> Bool #

Test whether a Path ar fd is absolute.

> Path.isAbsolute (Posix.absFile "/fred")
> Path.isAbsolute (Windows.absFile "\\fred")
> Path.isAbsolute (Windows.absFile "c:\\fred")
> Path.isAbsolute (Windows.absFile "c:fred")

isRelative :: AbsRel ar => Path os ar fd -> Bool #

Invariant - this should return True iff arg is of type Path Part.Rel _

 isRelative = not . isAbsolute
> Path.isRelative (Posix.relFile "fred")
> Path.isRelative (Windows.relFile "fred")

hasAnExtension :: FilePath os ar -> Bool #

Does the given filename have an extension?

> null (Path.takeExtension x) == not (Path.hasAnExtension x)

hasExtension :: String -> FilePath os ar -> Bool #

Does the given filename have the given extension?

> Path.hasExtension ".hs" (Posix.relFile "MyCode.hs")
> Path.hasExtension ".hs" (Posix.relFile "MyCode.bak.hs")
> not $ Path.hasExtension ".hs" (Posix.relFile "MyCode.hs.bak")

Separators

extSeparator :: Char #

Part.File extension character

> Posix.extSeparator == '.'

searchPathSeparator :: Char #

The character that is used to separate the entries in the $PATH environment variable.

isExtSeparator :: Char -> Bool #

Is the character an extension character?

> Posix.isExtSeparator a == (a == Posix.extSeparator)

isSearchPathSeparator :: Char -> Bool #

Is the character a file separator?

> Posix.isSearchPathSeparator a == (a == Posix.searchPathSeparator)

Generic Manipulation Functions

genericAddExtension :: FileDir fd => Path os ar fd -> String -> Path os ar fd #

This is a more flexible variant of addExtension / <.> which can work with files or directories

> Path.genericAddExtension (absDir "/") "x" == Posix.absDir "/.x"
> Path.genericAddExtension (absDir "/a") "x" == Posix.absDir "/a.x"
> Path.genericAddExtension Path.emptyFile "x" == Posix.relFile ".x"
> Path.genericAddExtension Path.emptyFile "" == Posix.emptyFile

genericDropExtension :: FileDir fd => Path os ar fd -> Path os ar fd #

genericDropExtensions :: FileDir fd => Path os ar fd -> Path os ar fd #

genericSplitExtension :: FileDir fd => Path os ar fd -> (Path os ar fd, String) #

genericSplitExtensions :: FileDir fd => Path os ar fd -> (Path os ar fd, String) #

parse :: (AbsRel ar, FileDir fd) => String -> Either String (Path ar fd) #

toText :: (AbsRel ar, FileDir fd) => Path ar fd -> Text Source #

type AbsFile = AbsFile System #

type RelFile = RelFile System #

type AbsDir = AbsDir System #

type RelDir = RelDir System #

type AbsRelFile = AbsRelFile System #

type AbsRelDir = AbsRelDir System #

type File ar = File System ar #

absDir :: IsText t => t -> AbsDir Source #

relDir :: IsText t => t -> RelDir Source #

(</>) :: DirPath os ar -> RelPath os fd -> Path os ar fd infixr 5 #

Infix variant of combine.

> Posix.toString (Posix.absDir "/tmp" </> Posix.relFile "file.txt") == "/tmp/file.txt"
> Posix.toString (Posix.absDir "/tmp" </> Posix.relDir "dir" </> Posix.relFile "file.txt") == "/tmp/dir/file.txt"
> Posix.toString (Posix.relDir "dir" </> Posix.relFile "file.txt") == "dir/file.txt"
> Windows.toString (Windows.absDir "\\tmp" </> Windows.relFile "file.txt") == "\\tmp\\file.txt"
> Windows.toString (Windows.absDir "c:\\tmp" </> Windows.relFile "file.txt") == "c:\\tmp\\file.txt"
> Windows.toString (Windows.absDir "c:tmp" </> Windows.relFile "file.txt") == "c:tmp\\file.txt"
> Windows.toString (Windows.absDir "c:\\" </> Windows.relDir "tmp" </> Windows.relFile "file.txt") == "c:\\tmp\\file.txt"
> Windows.toString (Windows.absDir "c:" </> Windows.relDir "tmp" </> Windows.relFile "file.txt") == "c:tmp\\file.txt"
> Windows.toString (Windows.relDir "dir" </> Windows.relFile "file.txt") == "dir\\file.txt"

(<.>) :: FilePath os ar -> String -> FilePath os ar infixl 7 #

Infix variant of addExtension. We only allow files (and not directories) to have extensions added by this function. This is because it's the vastly common case and an attempt to add one to a directory will - more often than not - represent an error. We don't however want to prevent the corresponding operation on directories, and so we provide a function that is more flexible: genericAddExtension.

(<++>) :: FilePath os ar -> String -> FilePath os ar infixl 7 #

Orphan instances