module Language.Fortran.Util.Files
  ( flexReadFile
  , getDirContents
  , rGetDirContents
  ) where

import qualified Data.Text.Encoding         as T
import qualified Data.Text.Encoding.Error   as T
import qualified Data.ByteString.Char8      as B
import           System.Directory (listDirectory, canonicalizePath,
                                   doesDirectoryExist, getDirectoryContents)
import           System.FilePath  ((</>))
import           Data.List        ((\\))

-- | Obtain a UTF-8 safe 'B.ByteString' representation of a file's contents.
--
-- Invalid UTF-8 is replaced with the space character.
flexReadFile :: FilePath -> IO B.ByteString
flexReadFile :: FilePath -> IO ByteString
flexReadFile = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> ByteString
T.encodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. OnDecodeError -> ByteString -> Text
T.decodeUtf8With (forall b a. b -> OnError a b
T.replace Char
' ')) forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO ByteString
B.readFile

-- | List files in directory, with the directory prepended to each entry.
getDirContents :: FilePath -> IO [FilePath]
getDirContents :: FilePath -> IO [FilePath]
getDirContents FilePath
d = do
  FilePath
d' <- FilePath -> IO FilePath
canonicalizePath FilePath
d
  forall a b. (a -> b) -> [a] -> [b]
map (FilePath
d' FilePath -> FilePath -> FilePath
</>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` FilePath -> IO [FilePath]
listDirectory FilePath
d'

-- | List files in directory recursively.
rGetDirContents :: FilePath -> IO [FilePath]
rGetDirContents :: FilePath -> IO [FilePath]
rGetDirContents FilePath
d = FilePath -> IO FilePath
canonicalizePath FilePath
d forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \FilePath
d' -> [FilePath] -> FilePath -> IO [FilePath]
go [FilePath
d'] FilePath
d'
  where
    go :: [FilePath] -> FilePath -> IO [FilePath]
go [FilePath]
seen FilePath
d'' = do
      [FilePath]
ds <- FilePath -> IO [FilePath]
getDirectoryContents FilePath
d''
      forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM FilePath -> IO [FilePath]
f forall a b. (a -> b) -> a -> b
$ [FilePath]
ds forall a. Eq a => [a] -> [a] -> [a]
\\ [FilePath
".", FilePath
".."] -- remove '.' and '..' entries
        where
          f :: FilePath -> IO [FilePath]
f FilePath
x = do
            FilePath
path <- FilePath -> IO FilePath
canonicalizePath forall a b. (a -> b) -> a -> b
$ FilePath
d forall a. [a] -> [a] -> [a]
++ FilePath
"/" forall a. [a] -> [a] -> [a]
++ FilePath
x
            Bool
g <- FilePath -> IO Bool
doesDirectoryExist FilePath
path
            if Bool
g Bool -> Bool -> Bool
&& forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
notElem FilePath
path [FilePath]
seen then do
              [FilePath]
x' <- [FilePath] -> FilePath -> IO [FilePath]
go (FilePath
path forall a. a -> [a] -> [a]
: [FilePath]
seen) FilePath
path
              forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. (a -> b) -> [a] -> [b]
map (\ FilePath
y -> FilePath
x forall a. [a] -> [a] -> [a]
++ FilePath
"/" forall a. [a] -> [a] -> [a]
++ FilePath
y) [FilePath]
x'
            else forall (m :: * -> *) a. Monad m => a -> m a
return [FilePath
x]