2010-01-17 01:22:05 +00:00
|
|
|
{-
|
|
|
|
|
|
|
|
Copyright (C) 2010 Scott R Parish <srp@srparish.net>
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2010-01-10 02:45:45 +00:00
|
|
|
module Database.MongoDB.Util
|
|
|
|
(
|
2010-01-13 01:59:52 +00:00
|
|
|
putI8, putI32, putI64, putNothing, putNull, putS,
|
2010-02-05 13:31:01 +00:00
|
|
|
getI8, getI32, getI64, getC, getS, getNull, putStrSz,
|
2010-01-10 02:45:45 +00:00
|
|
|
)
|
|
|
|
where
|
2010-01-17 03:40:22 +00:00
|
|
|
import Control.Exception (assert)
|
2010-01-10 02:45:45 +00:00
|
|
|
import Control.Monad
|
|
|
|
import Data.Binary
|
|
|
|
import Data.Binary.Get
|
|
|
|
import Data.Binary.Put
|
2010-01-13 01:59:52 +00:00
|
|
|
import Data.ByteString.Char8
|
2010-01-10 02:45:45 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import qualified Data.ByteString.Lazy.UTF8 as L8
|
2010-01-17 03:40:22 +00:00
|
|
|
import Data.Char (chr)
|
2010-01-10 02:45:45 +00:00
|
|
|
import Data.Int
|
|
|
|
|
2010-01-17 03:40:22 +00:00
|
|
|
getC :: Get Char
|
2010-01-10 02:45:45 +00:00
|
|
|
getC = liftM chr getI8
|
2010-01-17 03:40:22 +00:00
|
|
|
|
|
|
|
getI8 :: (Integral a) => Get a
|
2010-01-10 02:45:45 +00:00
|
|
|
getI8 = liftM fromIntegral getWord8
|
|
|
|
|
|
|
|
getI32 :: Get Int32
|
|
|
|
getI32 = liftM fromIntegral getWord32le
|
|
|
|
|
|
|
|
getI64 :: Get Int64
|
|
|
|
getI64 = liftM fromIntegral getWord64le
|
|
|
|
|
|
|
|
getS :: Get (Integer, L8.ByteString)
|
|
|
|
getS = getLazyByteStringNul >>= \s -> return (fromIntegral $ L.length s + 1, s)
|
|
|
|
|
2010-01-17 03:40:22 +00:00
|
|
|
getNull :: Get ()
|
|
|
|
getNull = do {c <- getC; assert (c == '\0') $ return ()}
|
2010-01-10 02:45:45 +00:00
|
|
|
|
2010-03-13 05:03:37 +00:00
|
|
|
putI8 :: Int8 -> Put
|
2010-01-10 02:45:45 +00:00
|
|
|
putI8 = putWord8 . fromIntegral
|
|
|
|
|
|
|
|
putI32 :: Int32 -> Put
|
|
|
|
putI32 = putWord32le . fromIntegral
|
|
|
|
|
|
|
|
putI64 :: Int64 -> Put
|
|
|
|
putI64 = putWord64le . fromIntegral
|
|
|
|
|
2010-01-17 03:40:22 +00:00
|
|
|
putNothing :: Put
|
2010-01-13 01:59:52 +00:00
|
|
|
putNothing = putByteString $ pack ""
|
|
|
|
|
2010-01-17 03:40:22 +00:00
|
|
|
putNull :: Put
|
2010-03-13 05:03:37 +00:00
|
|
|
putNull = putI8 0
|
2010-01-10 02:45:45 +00:00
|
|
|
|
|
|
|
putS :: L8.ByteString -> Put
|
|
|
|
putS s = putLazyByteString s >> putNull
|
2010-02-05 13:31:01 +00:00
|
|
|
|
|
|
|
putStrSz :: L.ByteString -> Put
|
|
|
|
putStrSz s = putI32 (fromIntegral $ 1 + L8.length s) >> putS s
|