-
Notifications
You must be signed in to change notification settings - Fork 0
/
Finders.hs
31 lines (26 loc) · 986 Bytes
/
Finders.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Finders where
import FileSystem(FileSystem(..))
import Predicates (isNameOfFolder, isNameOfFile)
import Parser (getNextDir)
findFile :: String -> [FileSystem] -> Maybe FileSystem
findFile name xs =
case filter (isNameOfFile name) xs of
[] -> Nothing
(x : _) -> Just x
findFolder :: String -> FileSystem -> Maybe FileSystem
findFolder name (Root _ xs) =
case filter (isNameOfFolder name) xs of
[] -> Nothing
(x : _) -> Just x
findFolder _ _ = Nothing
findFileInRoot :: String -> FileSystem -> Maybe FileSystem
findFileInRoot name (Root _ xs) = findFile name xs
findFileInRoot _ _ = Nothing
findFileByDir :: String -> FileSystem -> Maybe FileSystem
findFileByDir input x@(Root _ xs) =
case getNextDir input of
Just ("", file) -> findFile file xs
Just (rest, curr) -> case findFolder curr x of
Nothing -> Nothing
Just cFolder -> findFileByDir rest cFolder
findFileByDir _ _ = Nothing