Created
December 17, 2019 19:57
-
-
Save yasar11732/1ad19f9f93fa6534252019613e4b00f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main (main) where | |
import RecursiveContents | |
import System.FilePath (takeExtension) | |
import System.IO (hPutStrLn,stderr) | |
import GHC.IO.Encoding (setLocaleEncoding, utf8) | |
import System.Environment (getArgs) | |
hasExtension :: String -> -- extension | |
String -> -- filepath | |
Bool -- wheter path has extension | |
hasExtension ext path = takeExtension path == ("." ++ ext) | |
searchFiltered ext = do | |
contents <- getRecursiveContents "." | |
let filtered = filter (hasExtension ext) contents | |
mapM_ putStrLn filtered | |
main :: IO () | |
main = do | |
setLocaleEncoding utf8 | |
args <- getArgs | |
case args of | |
[ext] -> searchFiltered ext | |
_ -> hPutStrLn stderr "Dosya uzantisi belirtilmedi." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module RecursiveContents (getRecursiveContents) | |
where | |
import Control.Monad (forM) | |
import Control.Applicative (liftA2) | |
import System.Directory (doesDirectoryExist, listDirectory) | |
import System.FilePath ((</>)) | |
import Control.Exception (catch,IOException) | |
import System.IO (hPrint,stderr) | |
myFilterM :: (Applicative m) => (a -> m Bool) -> [a] -> m ([a],[a]) | |
myFilterM pred = foldr myFilterM' (pure ([],[])) | |
where myFilterM' x = liftA2 ( | |
\flg -> if flg then | |
\(taken,dropped) -> (x:taken,dropped) | |
else | |
\(taken,dropped) -> (taken,x:dropped) | |
) (pred x) | |
getRecursiveContents :: FilePath -> -- path to root directory | |
IO [FilePath] -- list of found paths | |
getRecursiveContents root = do | |
isDir <- doesDirectoryExist root | |
if isDir then do | |
contents <- catch (listDirectory root) (\e -> hPrint stderr (e :: IOException) >> return []) | |
(dirs, others) <- myFilterM doesDirectoryExist [root </> x | x <- contents] | |
paths <- mapM getRecursiveContents dirs | |
return $ others ++ concat paths | |
else | |
return [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment