diff options
Diffstat (limited to 'src/Data/Torrent/Tree.hs')
-rw-r--r-- | src/Data/Torrent/Tree.hs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/Data/Torrent/Tree.hs b/src/Data/Torrent/Tree.hs new file mode 100644 index 00000000..e9a337a1 --- /dev/null +++ b/src/Data/Torrent/Tree.hs | |||
@@ -0,0 +1,71 @@ | |||
1 | -- | | ||
2 | -- Copyright : (c) Sam Truzjan 2013 | ||
3 | -- License : BSD3 | ||
4 | -- Maintainer : pxqr.sta@gmail.com | ||
5 | -- Stability : experimental | ||
6 | -- Portability : portable | ||
7 | -- | ||
8 | {-# LANGUAGE FlexibleInstances #-} | ||
9 | {-# LANGUAGE TemplateHaskell #-} | ||
10 | {-# LANGUAGE DeriveDataTypeable #-} | ||
11 | module Data.Torrent.Tree | ||
12 | ( DirTree (..) | ||
13 | , build | ||
14 | |||
15 | , Data.Torrent.Tree.lookup | ||
16 | , lookupDir | ||
17 | |||
18 | , fileCount | ||
19 | , dirCount | ||
20 | ) where | ||
21 | |||
22 | import Control.Arrow | ||
23 | import Data.ByteString as BS | ||
24 | import Data.ByteString.Char8 as BC | ||
25 | import Data.Foldable | ||
26 | import Data.List as L | ||
27 | import Data.Map as M | ||
28 | import Data.Monoid | ||
29 | |||
30 | import Data.Torrent.Layout | ||
31 | |||
32 | |||
33 | data DirTree a = Dir { children :: Map ByteString (DirTree a) } | ||
34 | | File { node :: FileInfo a } | ||
35 | deriving Show | ||
36 | |||
37 | build :: LayoutInfo -> DirTree () | ||
38 | build SingleFile {liFile = FileInfo {..}} = Dir | ||
39 | { children = M.singleton fiName (File fi) } | ||
40 | where | ||
41 | fi = FileInfo fiLength fiMD5Sum () | ||
42 | build MultiFile {..} = Dir $ M.singleton liDirName files | ||
43 | where | ||
44 | files = Dir $ M.fromList $ L.map mkFileEntry liFiles | ||
45 | mkFileEntry FileInfo {..} = (L.head fiName, ent) -- TODO FIXME | ||
46 | where | ||
47 | ent = File $ FileInfo fiLength fiMD5Sum () | ||
48 | |||
49 | decompress :: DirTree () -> [FileInfo ()] | ||
50 | decompress = undefined | ||
51 | |||
52 | lookup :: [FilePath] -> DirTree a -> Maybe (DirTree a) | ||
53 | lookup [] t = Just t | ||
54 | lookup (p : ps) (Dir m) | Just subTree <- M.lookup (BC.pack p) m | ||
55 | = Data.Torrent.Tree.lookup ps subTree | ||
56 | lookup _ _ = Nothing | ||
57 | |||
58 | lookupDir :: [FilePath] -> DirTree a -> Maybe [(ByteString, DirTree a)] | ||
59 | lookupDir ps d | ||
60 | | Just subTree <- Data.Torrent.Tree.lookup ps d = | ||
61 | case subTree of | ||
62 | File _ -> Nothing | ||
63 | Dir es -> Just $ M.toList es | ||
64 | |||
65 | fileCount :: DirTree a -> Sum Int | ||
66 | fileCount File {..} = Sum 1 | ||
67 | fileCount Dir {..} = foldMap fileCount children | ||
68 | |||
69 | dirCount :: DirTree a -> Sum Int | ||
70 | dirCount File {..} = Sum 0 | ||
71 | dirCount Dir {..} = Sum 1 <> foldMap dirCount children | ||