summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-22 17:24:38 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-22 17:24:38 +0400
commit965cad146948a5bf8253d94aefbe29e7959711bc (patch)
tree7fa1cfdf16058f4d6d42f9a0e47e622682d2a30a
parent6b50fcd1540053694827119b5207cdc33c9f3ef9 (diff)
+ Add some utility functions.
-rw-r--r--src/Data/Torrent.hs13
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 @@
11module Data.Torrent 11module 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
220sizeInBase :: Integer -> Int -> Int
221sizeInBase n b = fromIntegral (n `div` fromIntegral b) + align
222 where
223 align = if n `mod` fromIntegral b == 0 then 0 else 1
224
225contentLength :: ContentInfo -> Integer
226contentLength SingleFile { ciLength = len } = len
227contentLength MultiFile { ciFiles = tfs } = sum (map fiLength tfs)
228
229pieceCount :: ContentInfo -> Int
230pieceCount ti = contentLength ti `sizeInBase` ciPieceLength ti
231
219 232
220-- | Read and decode a .torrent file. 233-- | Read and decode a .torrent file.
221fromFile :: FilePath -> IO (Result Torrent) 234fromFile :: FilePath -> IO (Result Torrent)