    {-# LANGUAGE CPP #-}
    -- File created: 2008-10-10 13:40:35

    module System.FilePath.Glob.Utils
       ( isLeft, fromLeft
       , increasingSeq
       , addToRange, inRange, overlap
       , dropLeadingZeroes
       , pathParts
       , nubOrd
       , partitionDL, tailDL
       , getRecursiveContents
       , catchIO
       ) where

    import Control.Monad    (foldM)
    import qualified Control.Exception as E
    import Data.List        ((\\))
    import qualified Data.DList as DL
    import Data.DList       (DList)
    import qualified Data.Set as Set
    import System.Directory (getDirectoryContents)
    import System.FilePath  ((</>), isPathSeparator, dropDrive)
    import System.IO.Unsafe (unsafeInterleaveIO)

    #if mingw32_HOST_OS
    import Data.Bits          ((.&.))
    import System.Win32.Types (withTString)
    import System.Win32.File  (c_GetFileAttributes, fILE_ATTRIBUTE_DIRECTORY)
    #else
    import Foreign.C.String      (withCString)
    import Foreign.Marshal.Alloc (allocaBytes)
    import System.FilePath
       (isDrive, dropTrailingPathSeparator, addTrailingPathSeparator)
    import System.Posix.Internals (sizeof_stat, lstat, s_isdir, st_mode)
    #endif

    inRange :: Ord a => (a,a) -> a -> Bool
    inRange :: (a, a) -> a -> Bool
    inRange (a :: a
    a,b :: a
    b) c :: a
    c = a
    c a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    >= a
    a Bool -> Bool -> Bool
    && a
    c a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= a
    b

    -- returns Just (a range which covers both given ranges) or Nothing if they are
    -- disjoint.
    --
    -- Assumes that the ranges are in the correct order, i.e. (fst x < snd x).
    overlap :: Ord a => (a,a) -> (a,a) -> Maybe (a,a)
    overlap :: (a, a) -> (a, a) -> Maybe (a, a)
    overlap (a :: a
    a,b :: a
    b) (c :: a
    c,d :: a
    d) =
       if a
    b a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    >= a
    c
          then if a
    b a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    >= a
    d
                  then if a
    a a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= a
    c
                          then (a, a) -> Maybe (a, a)
    forall a. a -> Maybe a
    Just (a
    a,a
    b)
                          else (a, a) -> Maybe (a, a)
    forall a. a -> Maybe a
    Just (a
    c,a
    b)
                  else if a
    a a -> a -> Bool
    forall a. Ord a => a -> a -> Bool
    <= a
    c
                          then (a, a) -> Maybe (a, a)
    forall a. a -> Maybe a
    Just (a
    a,a
    d)
                          else (a, a) -> Maybe (a, a)
    forall a. a -> Maybe a
    Just (a
    c,a
    d)
          else Maybe (a, a)
    forall a. Maybe a
    Nothing

    addToRange :: (Ord a, Enum a) => (a,a) -> a -> Maybe (a,a)
    addToRange :: (a, a) -> a -> Maybe (a, a)
    addToRange (a :: a
    a,b :: a
    b) c :: a
    c
       | (a, a) -> a -> Bool
    forall a. Ord a => (a, a) -> a -> Bool
    inRange (a
    a,a
    b) a
    c = (a, a) -> Maybe (a, a)
    forall a. a -> Maybe a
    Just (a
    a,a
    b)
       | a
    c a -> a -> Bool
    forall a. Eq a => a -> a -> Bool
    == a -> a
    forall a. Enum a => a -> a
    pred a
    a     = (a, a) -> Maybe (a, a)
    forall a. a -> Maybe a
    Just (a
    c,a
    b)
       | a
    c a -> a -> Bool
    forall a. Eq a => a -> a -> Bool
    == a -> a
    forall a. Enum a => a -> a
    succ a
    b     = (a, a) -> Maybe (a, a)
    forall a. a -> Maybe a
    Just (a
    a,a
    c)
       | Bool
    otherwise       = Maybe (a, a)
    forall a. Maybe a
    Nothing

    -- fst of result is in reverse order so that:
    --
    -- If x = fst (increasingSeq (a:xs)), then
    -- x == reverse [a .. head x]
    increasingSeq :: (Eq a, Enum a) => [a] -> ([a],[a])
    increasingSeq :: [a] -> ([a], [a])
    increasingSeq []     = ([],[])
    increasingSeq (x :: a
    x:xs :: [a]
    xs) = [a] -> [a] -> ([a], [a])
    forall a. (Eq a, Enum a) => [a] -> [a] -> ([a], [a])
    go [a
    x] [a]
    xs
     where
       go :: [a] -> [a] -> ([a], [a])
    go is :: [a]
    is       []     = ([a]
    is,[])
       go is :: [a]
    is@(i :: a
    i:_) (y :: a
    y:ys :: [a]
    ys) =
          if a
    y a -> a -> Bool
    forall a. Eq a => a -> a -> Bool
    == a -> a
    forall a. Enum a => a -> a
    succ a
    i
             then [a] -> [a] -> ([a], [a])
    go (a
    ya -> [a] -> [a]
    forall a. a -> [a] -> [a]
    :[a]
    is) [a]
    ys
             else ([a]
    is, a
    ya -> [a] -> [a]
    forall a. a -> [a] -> [a]
    :[a]
    ys)
       go _ _ = [Char] -> ([a], [a])
    forall a. HasCallStack => [Char] -> a
    error "Glob.increasingSeq :: internal error"

    isLeft :: Either a b -> Bool
    isLeft :: Either a b -> Bool
    isLeft (Left _) = Bool
    True
    isLeft _        = Bool
    False

    fromLeft :: Either a b -> a
    fromLeft :: Either a b -> a
    fromLeft (Left x :: a
    x) = a
    x
    fromLeft _        = [Char] -> a
    forall a. HasCallStack => [Char] -> a
    error "fromLeft :: Right"

    dropLeadingZeroes :: String -> String
    dropLeadingZeroes :: [Char] -> [Char]
    dropLeadingZeroes s :: [Char]
    s =
       let x :: [Char]
    x = (Char -> Bool) -> [Char] -> [Char]
    forall a. (a -> Bool) -> [a] -> [a]
    dropWhile (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    =='0') [Char]
    s
        in if [Char] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Char]
    x then "0" else [Char]
    x

    -- foo/bar/baz -> [foo/bar/baz,bar/baz,baz]
    pathParts :: FilePath -> [FilePath]
    pathParts :: [Char] -> [[Char]]
    pathParts p :: [Char]
    p = [Char]
    p [Char] -> [[Char]] -> [[Char]]
    forall a. a -> [a] -> [a]
    : let d :: [Char]
    d = [Char] -> [Char]
    dropDrive [Char]
    p
                       in if [Char] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Char]
    d Bool -> Bool -> Bool
    || [Char]
    d [Char] -> [Char] -> Bool
    forall a. Eq a => a -> a -> Bool
    == [Char]
    p
                             then     [Char] -> [[Char]]
    f [Char]
    d
                             else [Char]
    d [Char] -> [[Char]] -> [[Char]]
    forall a. a -> [a] -> [a]
    : [Char] -> [[Char]]
    f [Char]
    d
     where
       f :: [Char] -> [[Char]]
    f []  = []
       f (x :: Char
    x:xs :: [Char]
    xs@(y :: Char
    y:_)) | Char -> Bool
    isPathSeparator Char
    x Bool -> Bool -> Bool
    && Char -> Bool
    isPathSeparator Char
    y = [Char] -> [[Char]]
    f [Char]
    xs
       f (x :: Char
    x:xs :: [Char]
    xs) =
          if Char -> Bool
    isPathSeparator Char
    x
             then [Char]
    xs [Char] -> [[Char]] -> [[Char]]
    forall a. a -> [a] -> [a]
    : [Char] -> [[Char]]
    f [Char]
    xs
             else      [Char] -> [[Char]]
    f [Char]
    xs

    -- Significantly speedier than System.Directory.doesDirectoryExist.
    doesDirectoryExist :: FilePath -> IO Bool
    #if mingw32_HOST_OS
    -- This one allocates more memory since it has to do a UTF-16 conversion, but
    -- that can't really be helped: the below version is locale-dependent.
    doesDirectoryExist = flip withTString $ \s -> do
       a <- c_GetFileAttributes s
       return (a /= 0xffffffff && a.&.fILE_ATTRIBUTE_DIRECTORY /= 0)
    #else
    doesDirectoryExist :: [Char] -> IO Bool
    doesDirectoryExist s :: [Char]
    s =
       Int -> (Ptr CStat -> IO Bool) -> IO Bool
    forall a b. Int -> (Ptr a -> IO b) -> IO b
    allocaBytes Int
    sizeof_stat ((Ptr CStat -> IO Bool) -> IO Bool)
    -> (Ptr CStat -> IO Bool) -> IO Bool
    forall a b. (a -> b) -> a -> b
    $ \p :: Ptr CStat
    p ->
          [Char] -> (CString -> IO Bool) -> IO Bool
    forall a. [Char] -> (CString -> IO a) -> IO a
    withCString
             (if [Char] -> Bool
    isDrive [Char]
    s
                 then [Char] -> [Char]
    addTrailingPathSeparator [Char]
    s
                 else [Char] -> [Char]
    dropTrailingPathSeparator [Char]
    s)
             ((CString -> IO Bool) -> IO Bool)
    -> (CString -> IO Bool) -> IO Bool
    forall a b. (a -> b) -> a -> b
    $ \c :: CString
    c -> do
                CInt
    st <- CString -> Ptr CStat -> IO CInt
    lstat CString
    c Ptr CStat
    p
                if CInt
    st CInt -> CInt -> Bool
    forall a. Eq a => a -> a -> Bool
    == 0
                   then (CMode -> Bool) -> IO CMode -> IO Bool
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap CMode -> Bool
    s_isdir (Ptr CStat -> IO CMode
    st_mode Ptr CStat
    p)
                   else Bool -> IO Bool
    forall (m :: * -> *) a. Monad m => a -> m a
    return Bool
    False
    #endif

    getRecursiveContents :: FilePath -> IO (DList FilePath)
    getRecursiveContents :: [Char] -> IO (DList [Char])
    getRecursiveContents dir :: [Char]
    dir =
       (IO (DList [Char])
     -> (IOException -> IO (DList [Char])) -> IO (DList [Char]))
    -> (IOException -> IO (DList [Char]))
    -> IO (DList [Char])
    -> IO (DList [Char])
    forall a b c. (a -> b -> c) -> b -> a -> c
    flip IO (DList [Char])
    -> (IOException -> IO (DList [Char])) -> IO (DList [Char])
    forall a. IO a -> (IOException -> IO a) -> IO a
    catchIO (\_ -> DList [Char] -> IO (DList [Char])
    forall (m :: * -> *) a. Monad m => a -> m a
    return (DList [Char] -> IO (DList [Char]))
    -> DList [Char] -> IO (DList [Char])
    forall a b. (a -> b) -> a -> b
    $ [Char] -> DList [Char]
    forall a. a -> DList a
    DL.singleton [Char]
    dir) (IO (DList [Char]) -> IO (DList [Char]))
    -> IO (DList [Char]) -> IO (DList [Char])
    forall a b. (a -> b) -> a -> b
    $ do

          [[Char]]
    raw <- [Char] -> IO [[Char]]
    getDirectoryContents [Char]
    dir

          let entries :: [[Char]]
    entries = ([Char] -> [Char]) -> [[Char]] -> [[Char]]
    forall a b. (a -> b) -> [a] -> [b]
    map ([Char]
    dir [Char] -> [Char] -> [Char]
    </>) ([[Char]]
    raw [[Char]] -> [[Char]] -> [[Char]]
    forall a. Eq a => [a] -> [a] -> [a]
    \\ [".",".."])
          (dirs :: [[Char]]
    dirs,files :: [[Char]]
    files) <- ([Char] -> IO Bool) -> [[Char]] -> IO ([[Char]], [[Char]])
    forall (m :: * -> *) a.
    Monad m =>
    (a -> m Bool) -> [a] -> m ([a], [a])
    partitionM [Char] -> IO Bool
    doesDirectoryExist [[Char]]
    entries

          [DList [Char]]
    subs <- IO [DList [Char]] -> IO [DList [Char]]
    forall a. IO a -> IO a
    unsafeInterleaveIO (IO [DList [Char]] -> IO [DList [Char]])
    -> ([[Char]] -> IO [DList [Char]]) -> [[Char]] -> IO [DList [Char]]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ([Char] -> IO (DList [Char])) -> [[Char]] -> IO [DList [Char]]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    mapM [Char] -> IO (DList [Char])
    getRecursiveContents ([[Char]] -> IO [DList [Char]]) -> [[Char]] -> IO [DList [Char]]
    forall a b. (a -> b) -> a -> b
    $ [[Char]]
    dirs

          DList [Char] -> IO (DList [Char])
    forall (m :: * -> *) a. Monad m => a -> m a
    return(DList [Char] -> IO (DList [Char]))
    -> DList [Char] -> IO (DList [Char])
    forall a b. (a -> b) -> a -> b
    $ [Char] -> DList [Char] -> DList [Char]
    forall a. a -> DList a -> DList a
    DL.cons [Char]
    dir ([[Char]] -> DList [Char]
    forall a. [a] -> DList a
    DL.fromList [[Char]]
    files DList [Char] -> DList [Char] -> DList [Char]
    forall a. DList a -> DList a -> DList a
    `DL.append` [DList [Char]] -> DList [Char]
    forall a. [DList a] -> DList a
    DL.concat [DList [Char]]
    subs)

    partitionM :: (Monad m) => (a -> m Bool) -> [a] -> m ([a], [a])
    partitionM :: (a -> m Bool) -> [a] -> m ([a], [a])
    partitionM p_ :: a -> m Bool
    p_ = (([a], [a]) -> a -> m ([a], [a]))
    -> ([a], [a]) -> [a] -> m ([a], [a])
    forall (t :: * -> *) (m :: * -> *) b a.
    (Foldable t, Monad m) =>
    (b -> a -> m b) -> b -> t a -> m b
    foldM ((a -> m Bool) -> ([a], [a]) -> a -> m ([a], [a])
    forall (m :: * -> *) a.
    Monad m =>
    (a -> m Bool) -> ([a], [a]) -> a -> m ([a], [a])
    f a -> m Bool
    p_) ([],[])
     where
       f :: (a -> m Bool) -> ([a], [a]) -> a -> m ([a], [a])
    f p :: a -> m Bool
    p (ts :: [a]
    ts,fs :: [a]
    fs) x :: a
    x = a -> m Bool
    p a
    x m Bool -> (Bool -> m ([a], [a])) -> m ([a], [a])
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= \b :: Bool
    b ->
          if Bool
    b
             then ([a], [a]) -> m ([a], [a])
    forall (m :: * -> *) a. Monad m => a -> m a
    return (a
    xa -> [a] -> [a]
    forall a. a -> [a] -> [a]
    :[a]
    ts, [a]
    fs)
             else ([a], [a]) -> m ([a], [a])
    forall (m :: * -> *) a. Monad m => a -> m a
    return ([a]
    ts, a
    xa -> [a] -> [a]
    forall a. a -> [a] -> [a]
    :[a]
    fs)

    partitionDL :: (a -> Bool) -> DList a -> (DList a, DList a)
    partitionDL :: (a -> Bool) -> DList a -> (DList a, DList a)
    partitionDL p_ :: a -> Bool
    p_ = (a -> (DList a, DList a) -> (DList a, DList a))
    -> (DList a, DList a) -> DList a -> (DList a, DList a)
    forall a b. (a -> b -> b) -> b -> DList a -> b
    DL.foldr ((a -> Bool) -> a -> (DList a, DList a) -> (DList a, DList a)
    forall a.
    (a -> Bool) -> a -> (DList a, DList a) -> (DList a, DList a)
    f a -> Bool
    p_) (DList a
    forall a. DList a
    DL.empty,DList a
    forall a. DList a
    DL.empty)
     where
       f :: (a -> Bool) -> a -> (DList a, DList a) -> (DList a, DList a)
    f p :: a -> Bool
    p x :: a
    x (ts :: DList a
    ts,fs :: DList a
    fs) =
          if a -> Bool
    p a
    x
             then (a -> DList a -> DList a
    forall a. a -> DList a -> DList a
    DL.cons a
    x DList a
    ts, DList a
    fs)
             else (DList a
    ts, a -> DList a -> DList a
    forall a. a -> DList a -> DList a
    DL.cons a
    x DList a
    fs)

    tailDL :: DList a -> DList a
    #if MIN_VERSION_dlist(1,0,0)
    tailDL = DL.fromList . DL.tail
    #else
    tailDL :: DList a -> DList a
    tailDL = DList a -> DList a
    forall a. DList a -> DList a
    DL.tail
    #endif

    nubOrd :: Ord a => [a] -> [a]
    nubOrd :: [a] -> [a]
    nubOrd = Set a -> [a] -> [a]
    forall a. Ord a => Set a -> [a] -> [a]
    go Set a
    forall a. Set a
    Set.empty
     where
       go :: Set a -> [a] -> [a]
    go _ [] = []
       go set :: Set a
    set (x :: a
    x:xs :: [a]
    xs) =
          if a -> Set a -> Bool
    forall a. Ord a => a -> Set a -> Bool
    Set.member a
    x Set a
    set
             then Set a -> [a] -> [a]
    go Set a
    set [a]
    xs
             else a
    x a -> [a] -> [a]
    forall a. a -> [a] -> [a]
    : Set a -> [a] -> [a]
    go (a -> Set a -> Set a
    forall a. Ord a => a -> Set a -> Set a
    Set.insert a
    x Set a
    set) [a]
    xs

    catchIO :: IO a -> (E.IOException -> IO a) -> IO a
    catchIO :: IO a -> (IOException -> IO a) -> IO a
    catchIO = IO a -> (IOException -> IO a) -> IO a
    forall e a. Exception e => IO a -> (e -> IO a) -> IO a
    E.catch