From: Ramakrishnan Muthukrishnan Date: Thu, 5 Feb 2015 09:21:44 +0000 (+0530) Subject: parse and print Metainfo X-Git-Url: https://git.rkrishnan.org/?a=commitdiff_plain;h=24ab89755db53838119d502c02daf7742389010d;p=functorrent.git parse and print Metainfo --- diff --git a/src/Main.hs b/src/Main.hs index f5ee389..16b2ca5 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -14,7 +14,8 @@ main = do args <- getArgs torrentStr <- BC.readFile (head args) case (Benc.decode torrentStr) of - Right d -> let minfo = MInfo.mkMetaInfo d - in MInfo.printInfo minfo + Right d -> case (MInfo.mkMetaInfo d) of + Nothing -> putStrLn "parse error" + Just m -> putStrLn (show m) Left e -> printError e putStrLn "done" diff --git a/src/Metainfo.hs b/src/Metainfo.hs index b07b3f7..6ba7668 100644 --- a/src/Metainfo.hs +++ b/src/Metainfo.hs @@ -12,30 +12,53 @@ data Info = Info { pieceLength :: Integer , name :: String , lengthInBytes :: Integer , md5sum :: Maybe String } - deriving (Eq) + deriving (Eq, Show) data Metainfo = Metainfo { info :: Info , announce :: String , announceList :: Maybe [[String]] - , creationDate :: Maybe UTCTime + , creationDate :: Maybe String , comment :: Maybe String , createdBy :: Maybe String , encoding :: Maybe String } - deriving (Eq) + deriving (Eq, Show) + +mkInfo :: Benc.BVal -> Maybe Info +mkInfo (Benc.Bdict m) = let (Benc.Bint pieceLength') = m M.! (Benc.Bstr (BC.pack "piece length")) + (Benc.Bstr pieces') = m M.! (Benc.Bstr (BC.pack "pieces")) + private' = Nothing + (Benc.Bstr name') = m M.! (Benc.Bstr (BC.pack "name")) + (Benc.Bint length') = m M.! (Benc.Bstr (BC.pack "length")) + md5sum' = Nothing + in Just Info { pieceLength = pieceLength' + , pieces = pieces' + , private = private' + , name = BC.unpack name' + , lengthInBytes = length' + , md5sum = md5sum' } +mkInfo _ = Nothing + +maybeBstrToString :: Maybe Benc.BVal -> Maybe String +maybeBstrToString Nothing = Nothing +maybeBstrToString (Just s) = let (Benc.Bstr bs) = s + in Just (BC.unpack bs) mkMetaInfo :: Benc.BVal -> Maybe Metainfo -mkMetaInfo (Benc.Bdict m) = let info = mkInfo (m M.! (Benc.Bstr (BC.pack "info"))) - announce = m M.! (Benc.Bstr (BC.pack "announce")) - announceList = M.lookup (Benc.Bstr (BC.pack "announce list")) m - creationDate = M.lookup (Benc.Bstr (BC.pack "creation date")) m - comment = M.lookup (Benc.Bstr (BC.pack "comment")) m - createdBy = M.lookup (Benc.Bstr (BC.pack "created by")) m - encoding = M.lookup (Benc.Bstr (BC.pack "encoding")) m - in Just Metainfo { info = info - , announce = announce - , announceList = announceList - , creationDate = creationDate - , comment = comment - , createdBy = createdBy - , encoding = encoding } +mkMetaInfo (Benc.Bdict m) = let (Just info') = mkInfo (m M.! (Benc.Bstr (BC.pack "info"))) + (Benc.Bstr announce') = m M.! (Benc.Bstr (BC.pack "announce")) +-- announceList = M.lookup (Benc.Bstr (BC.pack "announce list")) + announceList' = Nothing + -- creationDate = M.lookup (Benc.Bstr (BC.pack "creation date")) m + creationDate' = Nothing + comment' = M.lookup (Benc.Bstr (BC.pack "comment")) m + createdBy' = M.lookup (Benc.Bstr (BC.pack "created by")) m + encoding' = M.lookup (Benc.Bstr (BC.pack "encoding")) m + in Just Metainfo { info = info' + , announce = BC.unpack announce' + , announceList = announceList' + , creationDate = creationDate' + , comment = maybeBstrToString comment' + , createdBy = maybeBstrToString createdBy' + , encoding = maybeBstrToString encoding' } mkMetaInfo _ = Nothing +