aboutsummaryrefslogtreecommitdiff
path: root/Utility/Misc.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2012-09-20 16:01:31 -0400
committerGravatar Joey Hess <joey@kitenet.net>2012-09-20 16:01:31 -0400
commita6504e41925ed594c95895ddf0862f817dbf1968 (patch)
tree4e8d33f69a93e87ddfbbdc27221c20c25ba293d3 /Utility/Misc.hs
parent2ae38325d5ba1fde3de0c53197658a3febc227f4 (diff)
optimised rsync output reader to read whole blocks at a time
Diffstat (limited to 'Utility/Misc.hs')
-rw-r--r--Utility/Misc.hs28
1 files changed, 27 insertions, 1 deletions
diff --git a/Utility/Misc.hs b/Utility/Misc.hs
index 77ebb4f3d..349b20efe 100644
--- a/Utility/Misc.hs
+++ b/Utility/Misc.hs
@@ -9,6 +9,9 @@ module Utility.Misc where
import System.IO
import Control.Monad
+import Foreign
+import Data.Char
+import Control.Applicative
{- A version of hgetContents that is not lazy. Ensures file is
- all read before it gets closed. -}
@@ -47,8 +50,31 @@ segment p l = map reverse $ go [] [] l
| otherwise = go (i:c) r is
{- Given two orderings, returns the second if the first is EQ and returns
- - the first otherwise. -}
+ - the first otherwise.
+ -
+ - Example use:
+ -
+ - compare lname1 lname2 `thenOrd` compare fname1 fname2
+ -}
thenOrd :: Ordering -> Ordering -> Ordering
thenOrd EQ x = x
thenOrd x _ = x
{-# INLINE thenOrd #-}
+
+{- Wrapper around hGetBufSome that returns a String.
+ -
+ - The null string is returned on eof, otherwise returns whatever
+ - data is currently available to read from the handle, or waits for
+ - data to be written to it if none is currently available.
+ -
+ - Note on encodings: The normal encoding of the Handle is ignored;
+ - each byte is converted to a Char. Not unicode clean!
+ -}
+hGetSomeString :: Handle -> Int -> IO String
+hGetSomeString h sz = do
+ fp <- mallocForeignPtrBytes sz
+ len <- withForeignPtr fp $ \buf -> hGetBufSome h buf sz
+ map (chr . fromIntegral) <$> withForeignPtr fp (peekbytes len)
+ where
+ peekbytes :: Int -> Ptr Word8 -> IO [Word8]
+ peekbytes len buf = mapM (peekElemOff buf) [0..pred len]