X-Git-Url: http://git.euphorik.ch/?p=pompage.git;a=blobdiff_plain;f=src%2FPompage.hs;h=6cf4349929e3d19a99755a322fd9e24e6db38cf1;hp=b986e99953060c9735f54ce4660e121c6c01dd5e;hb=574c2c85c2925c3f80e40a75ca0d89d033c60993;hpb=96bf4ef892a3bf6d493a139c4ea49992f51a1ddf diff --git a/src/Pompage.hs b/src/Pompage.hs index b986e99..6cf4349 100644 --- a/src/Pompage.hs +++ b/src/Pompage.hs @@ -1,7 +1,14 @@ import System.IO (readFile, FilePath(..)) -import System.Environment (getArgs) +import System.Directory ( + getDirectoryContents + , doesDirectoryExist + ) +import System.FilePath (()) +import System.Environment (getArgs, getProgName) import Data.List +import Text.Printf (printf) import Text.XML.Light +import Control.Monad (foldM) type Movies = [Movie] @@ -23,35 +30,79 @@ data Movie = Movie { , url :: String } deriving (Show) -data Args = Args { - xml :: String - , moviesDir :: FilePath -} deriving (Show) +data Arg = XML | MovieDir deriving (Show, Eq) +type Args = [(Arg, String)] -test = do +main = do args <- getArgs - case readArgs args of - Left mess -> print mess - Right args -> do - paths <- moviePaths $ moviesDir args - file <- readFile "../xml/test.xml" - print $ parseXMLDoc file + progName <- getProgName + case checkArgs $ readArgs args of + Nothing -> printf usage progName + Just args -> do + let Just dir = lookup MovieDir args + let Just xmlFile = lookup XML args + paths <- moviePaths dir + movies <- readXMLFile xmlFile + print movies + print paths movieExtenstions = ["avi", "mkv", "rmvb", "ogm", "divx"] - -readArgs :: [String] -> Either String Args -readArgs plop = undefined -{--readArgs (name:value:rest) = case name of - "-x" -> { xml = value } - "-d" -> { moviesDir = value }--} +usage = "Usage : %s -d -x \n" + +checkArgs :: Maybe Args -> Maybe Args +checkArgs Nothing = Nothing +checkArgs (Just args) = + case (lookup XML args, lookup MovieDir args) of + (Just _, Just _) -> Just args + otherwise -> Nothing + +readArgs :: [String] -> Maybe Args +readArgs (name:value:rest) + | name == "-x" = (XML, value) <:> readArgs rest + | name == "-d" = (MovieDir, value) <:> readArgs rest + | otherwise = Nothing + where + _ <:> Nothing = Nothing + arg <:> Just args = Just $ arg : args +readArgs (_:[]) = Nothing +readArgs (_) = Just [] moviePaths :: FilePath -> IO [FilePath] -moviePaths dir = undefined +moviePaths dir = do + paths <- filePaths (\filename -> + any (`isSuffixOf` filename) movieExtenstions) dir + -- Keep only the relative path. + return $ map (drop $ Data.List.length dir + 1) paths + +filePaths :: (String -> Bool) -> FilePath -> IO [FilePath] +filePaths predicat baseDir = do + processDir baseDir + where + processDir dir = do + contents <- getDirectoryContents dir + foldM (\acc entry -> do + let absDir = dir entry + doesDirectoryExist absDir >>= \exists -> + if exists + then do + paths <- processDir absDir + return (paths ++ acc) + else + if predicat entry + then + return (absDir : acc) + else + return acc) + [] + (contents \\ ["..", "."]) + readXMLFile :: FilePath -> IO Movies readXMLFile file = undefined - - +{- + file <- readFile "../xml/test.xml" + --print $ parseXMLDoc file +-} writeXMLFile :: Movies -> FilePath -> IO () writeXMLFile movies file = undefined