mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-22 08:26:35 +01:00
remove Iteratee dependency
This commit is contained in:
parent
83f1735fbb
commit
7201fcbe0f
@ -1,12 +1,12 @@
|
||||
Name: msgpack
|
||||
Version: 0.5.0.0
|
||||
Version: 0.6.0.0
|
||||
Synopsis: A Haskell implementation of MessagePack
|
||||
Description:
|
||||
A Haskell implementation of MessagePack <http://msgpack.org/>
|
||||
|
||||
License: BSD3
|
||||
License-File: LICENSE
|
||||
Copyright: Copyright (c) 2009-2010, Hideyuki Tanaka
|
||||
Copyright: Copyright (c) 2009-2011, Hideyuki Tanaka
|
||||
Category: Data
|
||||
Author: Hideyuki Tanaka
|
||||
Maintainer: Hideyuki Tanaka <tanaka.hideyuki@gmail.com>
|
||||
@ -22,10 +22,8 @@ Extra-source-files:
|
||||
Library
|
||||
Build-depends: base >=4 && <5,
|
||||
transformers >= 0.2 && < 0.3,
|
||||
MonadCatchIO-transformers >= 0.2.2 && < 0.2.3,
|
||||
bytestring >= 0.9 && < 0.10,
|
||||
vector >= 0.7 && < 0.8,
|
||||
iteratee >= 0.8 && < 0.9,
|
||||
attoparsec >= 0.8 && < 0.9,
|
||||
binary >= 0.5.0 && < 0.5.1,
|
||||
data-binary-ieee754 >= 0.4 && < 0.5,
|
||||
@ -41,7 +39,6 @@ Library
|
||||
Data.MessagePack.Pack
|
||||
Data.MessagePack.Unpack
|
||||
Data.MessagePack.Object
|
||||
Data.MessagePack.Iteratee
|
||||
Data.MessagePack.Derive
|
||||
|
||||
Source-repository head
|
||||
|
@ -17,89 +17,11 @@ module Data.MessagePack(
|
||||
module Data.MessagePack.Pack,
|
||||
module Data.MessagePack.Unpack,
|
||||
module Data.MessagePack.Object,
|
||||
module Data.MessagePack.Iteratee,
|
||||
module Data.MessagePack.Derive,
|
||||
|
||||
-- * Pack functions
|
||||
packToString,
|
||||
packToHandle,
|
||||
packToHandle',
|
||||
packToFile,
|
||||
|
||||
-- * Unpack functions
|
||||
unpackFromString,
|
||||
unpackFromHandle,
|
||||
unpackFromFile,
|
||||
unpackFromStringI,
|
||||
unpackFromHandleI,
|
||||
unpackFromFileI,
|
||||
|
||||
) where
|
||||
|
||||
import qualified Control.Monad.CatchIO as CIO
|
||||
import Control.Monad.IO.Class
|
||||
import qualified Data.Attoparsec as A
|
||||
import Data.Binary.Put
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.ByteString.Lazy as L
|
||||
import qualified Data.Iteratee as I
|
||||
import System.IO
|
||||
|
||||
import Data.MessagePack.Assoc
|
||||
import Data.MessagePack.Pack
|
||||
import Data.MessagePack.Unpack
|
||||
import Data.MessagePack.Object
|
||||
import Data.MessagePack.Iteratee
|
||||
import Data.MessagePack.Derive
|
||||
|
||||
bufferSize :: Int
|
||||
bufferSize = 4 * 1024
|
||||
|
||||
-- | Pack to ByteString.
|
||||
packToString :: Put -> L.ByteString
|
||||
packToString = runPut
|
||||
|
||||
-- | Pack to Handle
|
||||
packToHandle :: Handle -> Put -> IO ()
|
||||
packToHandle h = L.hPutStr h . packToString
|
||||
|
||||
-- | Pack to Handle and Flush Handle
|
||||
packToHandle' :: Handle -> Put -> IO ()
|
||||
packToHandle' h p = packToHandle h p >> hFlush h
|
||||
|
||||
-- | Pack to File
|
||||
packToFile :: FilePath -> Put -> IO ()
|
||||
packToFile path = L.writeFile path . packToString
|
||||
|
||||
-- | Unpack from ByteString
|
||||
unpackFromString :: (Monad m, IsByteString s) => s -> A.Parser a -> m a
|
||||
unpackFromString bs =
|
||||
unpackFromStringI bs . parserToIteratee
|
||||
|
||||
-- | Unpack from Handle
|
||||
unpackFromHandle :: CIO.MonadCatchIO m => Handle -> A.Parser a -> m a
|
||||
unpackFromHandle h =
|
||||
unpackFromHandleI h .parserToIteratee
|
||||
|
||||
-- | Unpack from File
|
||||
unpackFromFile :: CIO.MonadCatchIO m => FilePath -> A.Parser a -> m a
|
||||
unpackFromFile path =
|
||||
unpackFromFileI path . parserToIteratee
|
||||
|
||||
-- | Iteratee interface to unpack from ByteString
|
||||
unpackFromStringI :: (Monad m, IsByteString s) => s -> I.Iteratee B.ByteString m a -> m a
|
||||
unpackFromStringI bs =
|
||||
I.run . I.joinIM . I.enumPure1Chunk (toBS bs)
|
||||
|
||||
-- | Iteratee interface to unpack from Handle
|
||||
unpackFromHandleI :: CIO.MonadCatchIO m => Handle -> I.Iteratee B.ByteString m a -> m a
|
||||
unpackFromHandleI h =
|
||||
I.run . I.joinIM . enumHandleNonBlocking bufferSize h
|
||||
|
||||
-- | Iteratee interface to unpack from File
|
||||
unpackFromFileI :: CIO.MonadCatchIO m => FilePath -> I.Iteratee B.ByteString m a -> m a
|
||||
unpackFromFileI path p =
|
||||
CIO.bracket
|
||||
(liftIO $ openBinaryFile path ReadMode)
|
||||
(liftIO . hClose)
|
||||
(flip unpackFromHandleI p)
|
||||
|
@ -1,82 +0,0 @@
|
||||
--------------------------------------------------------------------
|
||||
-- |
|
||||
-- Module : Data.MessagePack.Iteratee
|
||||
-- Copyright : (c) Hideyuki Tanaka, 2009-2010
|
||||
-- License : BSD3
|
||||
--
|
||||
-- Maintainer: tanaka.hideyuki@gmail.com
|
||||
-- Stability : experimental
|
||||
-- Portability: portable
|
||||
--
|
||||
-- MessagePack Deserializer interface to @Data.Iteratee@
|
||||
--
|
||||
--------------------------------------------------------------------
|
||||
|
||||
module Data.MessagePack.Iteratee(
|
||||
-- * Iteratee version of deserializer
|
||||
getI,
|
||||
-- * Non Blocking Enumerator
|
||||
enumHandleNonBlocking,
|
||||
-- * Convert Parser to Iteratee
|
||||
parserToIteratee,
|
||||
) where
|
||||
|
||||
import Control.Exception
|
||||
import Control.Monad.IO.Class
|
||||
import qualified Data.Attoparsec as A
|
||||
import qualified Data.ByteString as B
|
||||
import qualified Data.Iteratee as I
|
||||
import System.IO
|
||||
|
||||
import Data.MessagePack.Unpack
|
||||
|
||||
-- | Deserialize a value
|
||||
getI :: (Monad m, Unpackable a) => I.Iteratee B.ByteString m a
|
||||
getI = parserToIteratee get
|
||||
|
||||
-- | Enumerator
|
||||
enumHandleNonBlocking :: MonadIO m => Int -> Handle -> I.Enumerator B.ByteString m a
|
||||
enumHandleNonBlocking bufSize h =
|
||||
I.enumFromCallback (readSome bufSize h) ()
|
||||
|
||||
readSome :: MonadIO m => Int -> Handle -> st -> m (Either SomeException ((Bool, st), B.ByteString))
|
||||
readSome bufSize h st = liftIO $ do
|
||||
ebs <- try $ hGetSome bufSize h
|
||||
case ebs of
|
||||
Left exc ->
|
||||
return $ Left (exc :: SomeException)
|
||||
Right bs | B.null bs ->
|
||||
return $ Right ((False, st), B.empty)
|
||||
Right bs ->
|
||||
return $ Right ((True, st), bs)
|
||||
|
||||
hGetSome :: Int -> Handle -> IO B.ByteString
|
||||
hGetSome bufSize h = do
|
||||
bs <- B.hGetNonBlocking h bufSize
|
||||
if B.null bs
|
||||
then do
|
||||
hd <- B.hGet h 1
|
||||
if B.null hd
|
||||
then do
|
||||
return B.empty
|
||||
else do
|
||||
rest <- B.hGetNonBlocking h (bufSize - 1)
|
||||
return $ B.cons (B.head hd) rest
|
||||
else do
|
||||
return bs
|
||||
|
||||
-- | Convert Parser to Iteratee
|
||||
parserToIteratee :: Monad m => A.Parser a -> I.Iteratee B.ByteString m a
|
||||
parserToIteratee p = I.icont (itr (A.parse p)) Nothing
|
||||
where
|
||||
itr pcont s = case s of
|
||||
I.EOF _ ->
|
||||
I.throwErr (I.setEOF s)
|
||||
I.Chunk bs ->
|
||||
case pcont bs of
|
||||
A.Fail _ _ msg ->
|
||||
I.throwErr (I.iterStrExc msg)
|
||||
A.Partial cont ->
|
||||
I.icont (itr cont) Nothing
|
||||
A.Done remain ret ->
|
||||
I.idone ret (I.Chunk remain)
|
Loading…
x
Reference in New Issue
Block a user