1 % -----------------------------------------------------------------------------
2 % $Id: PrelCString.lhs,v 1.5 2001/08/10 13:48:06 simonmar Exp $
4 % (c) The FFI task force, 2000
7 Utilities for primitive marshaling
10 {-# OPTIONS -fno-implicit-prelude #-}
12 module PrelCString where
14 #ifdef __GLASGOW_HASKELL__
15 import PrelMarshalArray
29 -----------------------------------------------------------------------------
32 -- representation of strings in C
33 -- ------------------------------
35 type CString = Ptr CChar -- conventional NUL terminates strings
36 type CStringLen = (CString, Int) -- strings with explicit length
42 -- * the following routines apply the default conversion when converting the
43 -- C-land character encoding into the Haskell-land character encoding
45 -- ** NOTE: The current implementation doesn't handle conversions yet! **
47 -- * the routines using an explicit length tolerate NUL characters in the
51 -- marshal a NUL terminated C string into a Haskell string
53 peekCString :: CString -> IO String
54 peekCString cp = do cs <- peekArray0 nUL cp; return (cCharsToChars cs)
56 -- marshal a C string with explicit length into a Haskell string
58 peekCStringLen :: CStringLen -> IO String
59 peekCStringLen (cp, len) = do cs <- peekArray len cp; return (cCharsToChars cs)
61 -- marshal a Haskell string into a NUL terminated C strings
63 -- * the Haskell string may *not* contain any NUL characters
65 -- * new storage is allocated for the C string and must be explicitly freed
67 newCString :: String -> IO CString
68 newCString = newArray0 nUL . charsToCChars
70 -- marshal a Haskell string into a C string (ie, character array) with
71 -- explicit length information
73 -- * new storage is allocated for the C string and must be explicitly freed
75 newCStringLen :: String -> IO CStringLen
76 newCStringLen str = do a <- newArray (charsToCChars str)
77 return (pairLength str a)
79 -- marshal a Haskell string into a NUL terminated C strings using temporary
82 -- * the Haskell string may *not* contain any NUL characters
84 -- * see the lifetime constraints of `MarshalAlloc.alloca'
86 withCString :: String -> (CString -> IO a) -> IO a
87 withCString = withArray0 nUL . charsToCChars
89 -- marshal a Haskell string into a NUL terminated C strings using temporary
92 -- * the Haskell string may *not* contain any NUL characters
94 -- * see the lifetime constraints of `MarshalAlloc.alloca'
96 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
97 withCStringLen str act = withArray (charsToCChars str) $ act . pairLength str
99 -- auxilliary definitions
100 -- ----------------------
102 -- C's end of string character
107 -- pair a C string with the length of the given Haskell string
109 pairLength :: String -> CString -> CStringLen
110 pairLength = flip (,) . length
112 -- cast [CChar] to [Char]
114 cCharsToChars :: [CChar] -> [Char]
115 cCharsToChars = map castCCharToChar
117 -- cast [Char] to [CChar]
119 charsToCChars :: [Char] -> [CChar]
120 charsToCChars = map castCharToCChar
122 castCCharToChar :: CChar -> Char
123 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
125 castCharToCChar :: Char -> CChar
126 castCharToCChar ch = fromIntegral (ord ch)