From 8101fa8a4286e779c45e17ac2f2b86e91a9f3b0a Mon Sep 17 00:00:00 2001 From: Sam T Date: Mon, 15 Jul 2013 22:57:02 +0400 Subject: ~ Add read instance for infohash. --- src/Data/Torrent.hs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/Data') diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 3f555a5b..2ebe9374 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs @@ -62,6 +62,7 @@ import Prelude hiding (sum) import Control.Applicative import Control.Arrow import Control.Exception +import Control.Monad import qualified Crypto.Hash.SHA1 as C @@ -69,8 +70,6 @@ import Data.Aeson.TH import Data.BEncode as BE import Data.Char import Data.Foldable -import Data.Map (Map) -import qualified Data.Map as M import qualified Data.ByteString as B import Data.ByteString.Internal import qualified Data.ByteString.Char8 as BC (pack, unpack) @@ -78,10 +77,15 @@ import qualified Data.ByteString.Lazy as Lazy import qualified Data.ByteString.Lazy.Builder as B import qualified Data.ByteString.Lazy.Builder.ASCII as B import qualified Data.List as L +import Data.Maybe +import Data.Map (Map) +import qualified Data.Map as M import Data.Hashable as Hashable import Data.Text (Text) import Data.Serialize as S hiding (Result) import Text.PrettyPrint +import Text.ParserCombinators.ReadP as P +import Text.Read import Network.URI import System.FilePath @@ -98,6 +102,17 @@ newtype InfoHash = InfoHash { getInfoHash :: ByteString } instance Show InfoHash where show = render . ppInfoHash +instance Read InfoHash where + readsPrec _ = readP_to_S $ do + str <- replicateM 40 (satisfy isHexDigit) + return $ InfoHash $ decodeIH str + where + decodeIH = B.pack . map fromHex . pair + fromHex (a, b) = read $ '0' : 'x' : a : b : [] + + pair (a : b : xs) = (a, b) : pair xs + pair _ = [] + instance Hashable InfoHash where hash = Hashable.hash . getInfoHash -- cgit v1.2.3