summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-07-15 22:57:02 +0400
committerSam T <pxqr.sta@gmail.com>2013-07-15 22:57:02 +0400
commit8101fa8a4286e779c45e17ac2f2b86e91a9f3b0a (patch)
treecd4162c73b9a95a79730cae285bf666dbf317456
parent33da2f459c0819b694f2ef21dba6d09167a5af6a (diff)
~ Add read instance for infohash.
-rw-r--r--src/Data/Torrent.hs19
1 files changed, 17 insertions, 2 deletions
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)
62import Control.Applicative 62import Control.Applicative
63import Control.Arrow 63import Control.Arrow
64import Control.Exception 64import Control.Exception
65import Control.Monad
65 66
66import qualified Crypto.Hash.SHA1 as C 67import qualified Crypto.Hash.SHA1 as C
67 68
@@ -69,8 +70,6 @@ import Data.Aeson.TH
69import Data.BEncode as BE 70import Data.BEncode as BE
70import Data.Char 71import Data.Char
71import Data.Foldable 72import Data.Foldable
72import Data.Map (Map)
73import qualified Data.Map as M
74import qualified Data.ByteString as B 73import qualified Data.ByteString as B
75import Data.ByteString.Internal 74import Data.ByteString.Internal
76import qualified Data.ByteString.Char8 as BC (pack, unpack) 75import qualified Data.ByteString.Char8 as BC (pack, unpack)
@@ -78,10 +77,15 @@ import qualified Data.ByteString.Lazy as Lazy
78import qualified Data.ByteString.Lazy.Builder as B 77import qualified Data.ByteString.Lazy.Builder as B
79import qualified Data.ByteString.Lazy.Builder.ASCII as B 78import qualified Data.ByteString.Lazy.Builder.ASCII as B
80import qualified Data.List as L 79import qualified Data.List as L
80import Data.Maybe
81import Data.Map (Map)
82import qualified Data.Map as M
81import Data.Hashable as Hashable 83import Data.Hashable as Hashable
82import Data.Text (Text) 84import Data.Text (Text)
83import Data.Serialize as S hiding (Result) 85import Data.Serialize as S hiding (Result)
84import Text.PrettyPrint 86import Text.PrettyPrint
87import Text.ParserCombinators.ReadP as P
88import Text.Read
85 89
86import Network.URI 90import Network.URI
87import System.FilePath 91import System.FilePath
@@ -98,6 +102,17 @@ newtype InfoHash = InfoHash { getInfoHash :: ByteString }
98instance Show InfoHash where 102instance Show InfoHash where
99 show = render . ppInfoHash 103 show = render . ppInfoHash
100 104
105instance Read InfoHash where
106 readsPrec _ = readP_to_S $ do
107 str <- replicateM 40 (satisfy isHexDigit)
108 return $ InfoHash $ decodeIH str
109 where
110 decodeIH = B.pack . map fromHex . pair
111 fromHex (a, b) = read $ '0' : 'x' : a : b : []
112
113 pair (a : b : xs) = (a, b) : pair xs
114 pair _ = []
115
101instance Hashable InfoHash where 116instance Hashable InfoHash where
102 hash = Hashable.hash . getInfoHash 117 hash = Hashable.hash . getInfoHash
103 118