[project @ 2001-05-18 16:54:04 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / PrelCString.lhs
index 753416f..4fe13fd 100644 (file)
@@ -1,5 +1,5 @@
 % -----------------------------------------------------------------------------
-% $Id: PrelCString.lhs,v 1.1 2001/01/11 17:25:57 simonmar Exp $
+% $Id: PrelCString.lhs,v 1.4 2001/05/18 16:54:05 simonmar Exp $
 %
 % (c) The FFI task force, 2000
 %
@@ -7,24 +7,23 @@
 Utilities for primitive marshaling
 
 \begin{code}
-module PrelCString where
+{-# OPTIONS -fno-implicit-prelude #-}
 
-import Monad
+module PrelCString where
 
+#ifdef __GLASGOW_HASKELL__
 import PrelMarshalArray
-import PrelMarshalAlloc
-import PrelException
 import PrelPtr
 import PrelStorable
 import PrelCTypes
-import PrelCTypesISO
-import PrelInt
+import PrelWord
 import PrelByteArr
 import PrelPack
+import PrelList
+import PrelReal
+import PrelNum
+import PrelIOBase
 import PrelBase
-
-#ifdef __GLASGOW_HASKELL__
-import PrelIOBase hiding (malloc, _malloc)
 #endif
 
 -----------------------------------------------------------------------------
@@ -52,12 +51,12 @@ type CStringLen = (CString, Int)    -- strings with explicit length
 -- marshal a NUL terminated C string into a Haskell string 
 --
 peekCString    :: CString -> IO String
-peekCString cp  = liftM cCharsToChars $ peekArray0 nUL cp
+peekCString cp  = do cs <- peekArray0 nUL cp; return (cCharsToChars cs)
 
 -- marshal a C string with explicit length into a Haskell string 
 --
 peekCStringLen           :: CStringLen -> IO String
-peekCStringLen (cp, len)  = liftM cCharsToChars $ peekArray len cp
+peekCStringLen (cp, len)  = do cs <- peekArray len cp; return (cCharsToChars cs)
 
 -- marshal a Haskell string into a NUL terminated C strings
 --
@@ -74,7 +73,8 @@ newCString  = newArray0 nUL . charsToCChars
 -- * new storage is allocated for the C string and must be explicitly freed
 --
 newCStringLen     :: String -> IO CStringLen
-newCStringLen str  = liftM (pairLength str) $ newArray (charsToCChars str)
+newCStringLen str  = do a <- newArray (charsToCChars str)
+                       return (pairLength str a)
 
 -- marshal a Haskell string into a NUL terminated C strings using temporary
 -- storage
@@ -102,7 +102,7 @@ withCStringLen str act  = withArray (charsToCChars str) $ act . pairLength str
 -- C's end of string character
 --
 nUL :: CChar
-nUL  = castCharToCChar '\0'
+nUL  = 0
 
 -- pair a C string with the length of the given Haskell string
 --
@@ -120,11 +120,7 @@ charsToCChars :: [Char] -> [CChar]
 charsToCChars  = map castCharToCChar
 
 castCCharToChar :: CChar -> Char
--- castCCharToChar ch = chr (fromIntegral (fromIntegral ch :: Word8))
--- The above produces horrible code. Word and Int modules really
--- should be cleaned up... Here is an ugly but fast version:
-castCCharToChar ch = case fromIntegral (fromIntegral ch :: Int32) of
-    I# i# -> C# (chr# (word2Int# (int2Word# i# `and#` int2Word# 0xFF#)))
+castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
 
 castCharToCChar :: Char -> CChar
 castCharToCChar ch = fromIntegral (ord ch)
@@ -133,6 +129,7 @@ castCharToCChar ch = fromIntegral (ord ch)
 -- unsafe CStrings
 -- ---------------
 
+withUnsafeCString :: String -> (UnsafeCString -> IO a) -> IO a
 #if __GLASGOW_HASKELL__
 newtype UnsafeCString = UnsafeCString (ByteArray Int)
 withUnsafeCString s f = f (UnsafeCString (packString s))