1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2025-03-20 04:46:22 +09:00

Compressed inventory parser and DarcsRev TH utils

This commit is contained in:
fr33domlover 2016-05-17 20:34:22 +00:00
parent 9ba6761459
commit e76c1f7206
13 changed files with 827 additions and 209 deletions
src/Data/Attoparsec/ByteString

View file

@ -15,15 +15,17 @@
module Data.Attoparsec.ByteString.Local
( parseFileIncremental
, parseCompressedFileIncremental
)
where
import Prelude
import Codec.Compression.Zlib.Internal
import Data.Attoparsec.ByteString
import System.IO
import qualified Data.ByteString as B (hGet)
import qualified Data.ByteString as B (null, hGet)
import qualified Data.ByteString.Lazy.Internal as BLI (defaultChunkSize)
parseFileIncremental :: FilePath -> Parser a -> IO (Either String a)
@ -36,3 +38,36 @@ parseFileIncremental file parser =
firstChunk <- getChunk
let firstResult = parse parser firstChunk
go firstResult
parseCompressedFileIncremental
:: Format
-> DecompressParams
-> FilePath
-> Parser a
-> IO (Either String a)
parseCompressedFileIncremental format params file parser =
withBinaryFile file ReadMode $ \ h -> do
let getChunk = B.hGet h BLI.defaultChunkSize
pGo _ (Fail _remainder _contexts msg) = return $ Left msg
pGo f (Partial cont) = f cont
pGo _ (Done _remainder value) = return $ Right value
dGo pCont (DecompressInputRequired dCont) =
getChunk >>= dCont >>= dGo pCont
dGo pCont (DecompressOutputAvailable output next) =
pGo (\ c -> next >>= dGo c) (pCont output)
dGo pCont (DecompressStreamEnd remainder) =
if B.null remainder
then
pGo
( const $
return $
Left "Parser wants input but there's none"
)
(pCont remainder)
else return $ Left "Decompression ended with remainder"
dGo pCont (DecompressStreamError err) =
return $ Left $ show err
dGo (parse parser) (decompressIO format params)