diff options
-rw-r--r-- | src/Data/Torrent.hs | 13 |
1 files changed, 13 insertions, 0 deletions
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 @@ | |||
11 | module Data.Torrent | 11 | module Data.Torrent |
12 | ( module Data.Torrent.InfoHash | 12 | ( module Data.Torrent.InfoHash |
13 | , Torrent(..), ContentInfo(..), FileInfo(..) | 13 | , Torrent(..), ContentInfo(..), FileInfo(..) |
14 | , contenLength, pieceCount | ||
14 | , fromFile | 15 | , fromFile |
15 | ) where | 16 | ) where |
16 | 17 | ||
@@ -216,6 +217,18 @@ instance BEncodable FileInfo where | |||
216 | 217 | ||
217 | fromBEncode _ = decodingError "FileInfo" | 218 | fromBEncode _ = decodingError "FileInfo" |
218 | 219 | ||
220 | sizeInBase :: Integer -> Int -> Int | ||
221 | sizeInBase n b = fromIntegral (n `div` fromIntegral b) + align | ||
222 | where | ||
223 | align = if n `mod` fromIntegral b == 0 then 0 else 1 | ||
224 | |||
225 | contentLength :: ContentInfo -> Integer | ||
226 | contentLength SingleFile { ciLength = len } = len | ||
227 | contentLength MultiFile { ciFiles = tfs } = sum (map fiLength tfs) | ||
228 | |||
229 | pieceCount :: ContentInfo -> Int | ||
230 | pieceCount ti = contentLength ti `sizeInBase` ciPieceLength ti | ||
231 | |||
219 | 232 | ||
220 | -- | Read and decode a .torrent file. | 233 | -- | Read and decode a .torrent file. |
221 | fromFile :: FilePath -> IO (Result Torrent) | 234 | fromFile :: FilePath -> IO (Result Torrent) |