+{-# OPTIONS_GHC -fno-implicit-prelude #-}
+
-----------------------------------------------------------------------------
-- |
-- Module : System.FilePath
, changeFileExt
, isRootedPath
, isAbsolutePath
+ , dropAbsolutePrefix
, pathParents
, commonParent
, dllExtension
) where
+#ifdef __GLASGOW_HASKELL__
+import GHC.Base
+import GHC.IOBase(FilePath)
+import GHC.Num
+#else
import Prelude -- necessary to get dependencies right
-
-import Data.List(intersperse)
+#endif
+import Data.Maybe
+import Data.List
--------------------------------------------------------------
-- * FilePath
(_:dir) -> dir++drive
-- | The 'joinFileName' function is the opposite of 'splitFileName'.
--- It joins directory and file names to form complete file path.
+-- It joins directory and file names to form a complete file path.
--
-- The general rule is:
--
-- > where
-- > (dir,basename) = splitFileName path
--
--- There might be an exeptions to the rule but in any case the
+-- There might be an exceptions to the rule but in any case the
-- reconstructed path will refer to the same object (file or directory).
-- An example exception is that on Windows some slashes might be converted
-- to backslashes.
| otherwise = dir++pathSeparator:fname
-- | The 'joinFileExt' function is the opposite of 'splitFileExt'.
--- It joins file name and extension to form complete file path.
+-- It joins a file name and an extension to form a complete file path.
--
-- The general rule is:
--
#endif
isRootedPath _ = False
--- | Returns True if this path\'s meaning is independent of any OS
--- "working directory", False if it isn\'t.
+-- | Returns 'True' if this path\'s meaning is independent of any OS
+-- \"working directory\", or 'False' if it isn\'t.
isAbsolutePath :: FilePath -> Bool
#ifdef mingw32_TARGET_OS
isAbsolutePath (_:':':c:_) | isPathSeparator c = True
#endif
isAbsolutePath _ = False
+-- | If the function is applied to an absolute path then it returns a
+-- local path obtained by dropping the absolute prefix from the path.
+-- Under Windows the prefix is @\"\\\"@, @\"c:\"@ or @\"c:\\\"@.
+-- Under Unix the prefix is always @\"\/\"@.
+dropAbsolutePrefix :: FilePath -> FilePath
+dropAbsolutePrefix (c:cs) | isPathSeparator c = cs
+#ifdef mingw32_TARGET_OS
+dropAbsolutePrefix (_:':':c:cs) | isPathSeparator c = cs -- path with drive letter
+dropAbsolutePrefix (_:':':cs) = cs
+#endif
+dropAbsolutePrefix cs = cs
+
-- | Gets this path and all its parents.
-- The function is useful in case if you want to create
-- some file but you aren\'t sure whether all directories
--- in the path exists or if you want to search upward for some file.
+-- in the path exist or if you want to search upward for some file.
--
-- Some examples:
--
-- > pathParents "dir1" == [".", "dir1"]
-- > pathParents "dir1/dir2" == [".", "dir1", "dir1/dir2"]
--
--- In the above examples \"\/\" isn\'t included in the list
--- because you can\'t create root directory.
---
-- \[Windows\]
--
-- > pathParents "c:" == ["c:."]
-- > pathParents "c:dir1" == ["c:.","c:dir1"]
-- > pathParents "dir1\\dir2" == [".", "dir1", "dir1\\dir2"]
--
--- Note that if the file is relative then the the current directory (\".\")
+-- Note that if the file is relative then the current directory (\".\")
-- will be explicitly listed.
pathParents :: FilePath -> [FilePath]
pathParents p =
-- * Separators
--------------------------------------------------------------
--- | Checks whether the character is a valid path separator for the host platform.
--- The valid character is a 'pathSeparator' but since the Windows operating system
--- also accepts a backslash (\"\\\") the function also checks for \"\/\" on this platform.
+-- | Checks whether the character is a valid path separator for the host
+-- platform. The valid character is a 'pathSeparator' but since the Windows
+-- operating system also accepts a slash (\"\/\") since DOS 2, the function
+-- checks for it on this platform, too.
isPathSeparator :: Char -> Bool
-isPathSeparator ch =
#ifdef mingw32_TARGET_OS
- ch == '/' || ch == '\\'
+isPathSeparator ch = ch == '/' || ch == '\\'
#else
- ch == '/'
+isPathSeparator ch = ch == '/'
#endif
--- | Provides a platform-specific character used to separate directory levels in a
--- path string that reflects a hierarchical file system organization.
--- The separator is a slash (\"\/\") on Unix and Macintosh, and a backslash (\"\\\") on the
--- Windows operating system.
+-- | Provides a platform-specific character used to separate directory levels in
+-- a path string that reflects a hierarchical file system organization. The
+-- separator is a slash (@\"\/\"@) on Unix and Macintosh, and a backslash
+-- (@\"\\\"@) on the Windows operating system.
pathSeparator :: Char
#ifdef mingw32_TARGET_OS
pathSeparator = '\\'
pathSeparator = '/'
#endif
--- | A platform-specific character used to separate search path strings in
--- environment variables. The separator is a colon (\":\") on Unix and Macintosh,
--- and a semicolon (\";\") on the Windows operating system.
+-- | A platform-specific character used to separate search path strings in
+-- environment variables. The separator is a colon (@\":\"@) on Unix and
+-- Macintosh, and a semicolon (@\";\"@) on the Windows operating system.
searchPathSeparator :: Char
#ifdef mingw32_TARGET_OS
searchPathSeparator = ';'
-- ToDo: This should be determined via autoconf (AC_EXEEXT)
-- | Extension for executable files
--- (typically @""@ on Unix and @".exe"@ on Windows or OS/2)
+-- (typically @\"\"@ on Unix and @\"exe\"@ on Windows or OS\/2)
exeExtension :: String
+#ifdef mingw32_TARGET_OS
+exeExtension = "exe"
+#else
+exeExtension = ""
+#endif
-- ToDo: This should be determined via autoconf (AC_OBJEXT)
--- | Extension for object files
--- (typically @".o"@ on Unix and @".obj"@ on Windows)
+-- | Extension for object files. For GHC and NHC the extension is @\"o\"@.
+-- Hugs uses either @\"o\"@ or @\"obj\"@ depending on the used C compiler.
objExtension :: String
+objExtension = "o"
-- | Extension for dynamically linked (or shared) libraries
--- (typically @".so"@ on Unix and @".dll"@ on Windows)
+-- (typically @\"so\"@ on Unix and @\"dll\"@ on Windows)
dllExtension :: String
-
#ifdef mingw32_TARGET_OS
-exeExtension = ".exe"
-objExtension = ".obj"
-dllExtension = ".dll"
+dllExtension = "dll"
#else
-exeExtension = ""
-objExtension = ".o"
-dllExtension = ".so"
+dllExtension = "so"
#endif