From 965cad146948a5bf8253d94aefbe29e7959711bc Mon Sep 17 00:00:00 2001 From: Sam T Date: Mon, 22 Apr 2013 17:24:38 +0400 Subject: + Add some utility functions. --- src/Data/Torrent.hs | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/Data/Torrent.hs') diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 41b78922..e6ed8813 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs @@ -11,6 +11,7 @@ module Data.Torrent ( module Data.Torrent.InfoHash , Torrent(..), ContentInfo(..), FileInfo(..) + , contenLength, pieceCount , fromFile ) where @@ -216,6 +217,18 @@ instance BEncodable FileInfo where fromBEncode _ = decodingError "FileInfo" +sizeInBase :: Integer -> Int -> Int +sizeInBase n b = fromIntegral (n `div` fromIntegral b) + align + where + align = if n `mod` fromIntegral b == 0 then 0 else 1 + +contentLength :: ContentInfo -> Integer +contentLength SingleFile { ciLength = len } = len +contentLength MultiFile { ciFiles = tfs } = sum (map fiLength tfs) + +pieceCount :: ContentInfo -> Int +pieceCount ti = contentLength ti `sizeInBase` ciPieceLength ti + -- | Read and decode a .torrent file. fromFile :: FilePath -> IO (Result Torrent) -- cgit v1.2.3