diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-22 17:24:38 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-22 17:24:38 +0400 |
commit | 965cad146948a5bf8253d94aefbe29e7959711bc (patch) | |
tree | 7fa1cfdf16058f4d6d42f9a0e47e622682d2a30a /src/Data/Torrent.hs | |
parent | 6b50fcd1540053694827119b5207cdc33c9f3ef9 (diff) |
+ Add some utility functions.
Diffstat (limited to 'src/Data/Torrent.hs')
-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) |