[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36

John Goerzen jgoerzen at complete.org
Fri Apr 23 14:42:32 UTC 2010


The following commit has been merged in the master branch:
commit 8b977b1aff405f3010194b1c32fda196394c67c1
Author: John Goerzen <jgoerzen at complete.org>
Date:   Tue Oct 5 22:23:24 2004 +0100

    Added binary I/O utilities
    
    Keywords:
    
    
    (jgoerzen at complete.org--projects/missingh--head--1.0--patch-16)

diff --git a/MissingH/IOutil.hs b/MissingH/IOutil.hs
index 04bb247..eda0530 100644
--- a/MissingH/IOutil.hs
+++ b/MissingH/IOutil.hs
@@ -24,11 +24,16 @@ Written by John Goerzen, jgoerzen\@complete.org
 module MissingH.IOutil(-- * Line Processing Utilities
                        hPutStrLns, hGetLines,
                        -- * Lazy Interaction
-                       hInteract, lineInteract, hLineInteract
+                       hInteract, lineInteract, hLineInteract,
+                       -- * Binary Files
+                       hPutBufStr, hGetBufStr, hFullGetBufStr
                         ) where
-import IO
-import System.IO.Unsafe
 
+import Foreign.Ptr
+import Foreign.ForeignPtr
+import Foreign.C.String
+import System.IO.Unsafe
+import System.IO
 
 {- | Given a list of strings, output a line containing each item, adding
 newlines as appropriate.  The list is not expected to have newlines already.
@@ -113,3 +118,43 @@ hLineInteract finput foutput func =
     do
     lines <- hGetLines finput
     hPutStrLns foutput (func lines)
+
+-- . **************************************************
+-- . Binary Files
+-- . **************************************************
+
+
+{- | As a wrapper around the standard function 'System.IO.hPutBuf',
+this function takes a standard Haskell 'String' instead of the far less
+convenient 'Ptr a'.  The entire contents of the string will be written
+as a binary buffer using 'hPutBuf'.  The length of the output will be
+the length of the string. -}
+hPutBufStr :: Handle -> String -> IO ()
+hPutBufStr f s = withCString s (\cs -> hPutBuf f cs (length s))
+
+{- | As a wrapper around the standard function 'System.IO.hGetBuf',
+this function returns a standard Haskell string instead of modifying
+a 'Ptr a' buffer.  The length is the maximum length to read and the
+semantice are the same as with 'hGetBuf'; namely, the empty string
+is returned with EOF is reached, and any given read may read fewer
+bytes than the given length. -}
+hGetBufStr :: Handle -> Int -> IO String
+hGetBufStr f count = do
+   fbuf <- mallocForeignPtrArray (count + 1)
+   withForeignPtr fbuf (\buf -> do
+                        bytesread <- hGetBuf f buf count
+                        haskstring <- peekCStringLen (buf, bytesread)
+                        return haskstring)
+
+{- | Like 'hGetBufStr', but guarantees that it will only return fewer than
+the requested number of bytes when EOF is encountered. -}
+hFullGetBufStr :: Handle -> Int -> IO String
+hFullGetBufStr f 0 = return ""
+hFullGetBufStr f count = do
+                         thisstr <- hGetBufStr f count
+                         if thisstr == "" -- EOF
+                            then return ""
+                            else do
+                                 remainder <- hFullGetBufStr f (count - (length thisstr))
+                                 return (thisstr ++ remainder)
+

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list