From: Ramakrishnan Muthukrishnan Date: Mon, 16 Feb 2015 05:27:52 +0000 (+0530) Subject: more refactoring X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/biz//%22%22?a=commitdiff_plain;h=09b3231f573b31bde81dd7484b00d581263f2b99;p=functorrent.git more refactoring --- diff --git a/src/Bencode.hs b/src/Bencode.hs index 16e4a2b..e07799e 100644 --- a/src/Bencode.hs +++ b/src/Bencode.hs @@ -133,6 +133,6 @@ encode (Bstr bs) = let s = BC.unpack bs encode (Bint i) = "i" ++ show i ++ "e" encode (Blist xs) = "l" ++ encodeList xs ++ "e" where encodeList [] = "" - encodeList (x:xs) = encode x ++ encodeList xs + encodeList (x:xs') = encode x ++ encodeList xs' encode (Bdict d) = "d" ++ encodeDict d ++ "e" where encodeDict m = concat [encode k ++ encode (m M.! k) | k <- M.keys m] diff --git a/src/Main.hs b/src/Main.hs index f5a0a50..10aa287 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -1,6 +1,7 @@ module Main where import System.Environment (getArgs) +import System.Exit import qualified Data.ByteString.Char8 as BC import qualified Bencode as Benc import qualified Metainfo as MInfo @@ -14,10 +15,21 @@ printError e = putStrLn $ "parse error: " ++ show e genPeerId :: String genPeerId = "-HS0001-20150215" +exit :: IO BC.ByteString +exit = exitWith ExitSuccess + +usage :: IO () +usage = putStrLn "usage: deluge torrent-file" + +parse :: [String] -> IO (BC.ByteString) +parse [] = usage >> exit +parse [a] = BC.readFile a +parse _ = exit + main :: IO () main = do args <- getArgs - torrentStr <- BC.readFile (head args) + torrentStr <- parse args case (Benc.decode torrentStr) of Right d -> case (MInfo.mkMetaInfo d) of Nothing -> putStrLn "parse error" diff --git a/src/Metainfo.hs b/src/Metainfo.hs index 47a134c..4c614d6 100644 --- a/src/Metainfo.hs +++ b/src/Metainfo.hs @@ -3,9 +3,6 @@ module Metainfo where import qualified Bencode as Benc import qualified Data.ByteString.Char8 as BC import qualified Data.Map as M -import qualified Crypto.Hash as H -import qualified Crypto.Hash.SHA1 as SHA1 -import Data.Time.Clock -- only single file mode supported for the time being. data Info = Info { pieceLength :: !Integer