1 {-# OPTIONS -fno-implicit-prelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Foreign.C.String
5 -- Copyright : (c) The FFI task force 2001
6 -- License : BSD-style (see the file libraries/core/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- $Id: String.hs,v 1.2 2001/07/03 11:37:50 simonmar Exp $
14 -- Utilities for primitive marshaling
16 -----------------------------------------------------------------------------
18 module Foreign.C.String ( -- representation of strings in C
20 CString, -- = Ptr CChar
21 CStringLen, -- = (CString, Int)
23 -- conversion of C strings into Haskell strings
25 peekCString, -- :: CString -> IO String
26 peekCStringLen, -- :: CStringLen -> IO String
28 -- conversion of Haskell strings into C strings
30 newCString, -- :: String -> IO CString
31 newCStringLen, -- :: String -> IO CStringLen
33 -- conversion of Haskell strings into C strings using temporary storage
35 withCString, -- :: String -> (CString -> IO a) -> IO a
36 withCStringLen, -- :: String -> (CStringLen -> IO a) -> IO a
38 -- conversion between Haskell and C characters *ignoring* the encoding
40 castCharToCChar, -- :: Char -> CChar
41 castCCharToChar, -- :: CChar -> Char
43 -- UnsafeCString: these might be more efficient than CStrings when
44 -- passing the string to an "unsafe" foreign import. NOTE: this
45 -- feature might be removed in favour of a more general approach in
48 UnsafeCString, -- abstract
49 withUnsafeCString, -- :: String -> (UnsafeCString -> IO a) -> IO a
53 import Foreign.Marshal.Array
54 import Foreign.C.Types
59 #ifdef __GLASGOW_HASKELL__
69 -----------------------------------------------------------------------------
72 -- representation of strings in C
73 -- ------------------------------
75 type CString = Ptr CChar -- conventional NUL terminates strings
76 type CStringLen = (CString, Int) -- strings with explicit length
82 -- * the following routines apply the default conversion when converting the
83 -- C-land character encoding into the Haskell-land character encoding
85 -- ** NOTE: The current implementation doesn't handle conversions yet! **
87 -- * the routines using an explicit length tolerate NUL characters in the
91 -- marshal a NUL terminated C string into a Haskell string
93 peekCString :: CString -> IO String
94 peekCString cp = do cs <- peekArray0 nUL cp; return (cCharsToChars cs)
96 -- marshal a C string with explicit length into a Haskell string
98 peekCStringLen :: CStringLen -> IO String
99 peekCStringLen (cp, len) = do cs <- peekArray len cp; return (cCharsToChars cs)
101 -- marshal a Haskell string into a NUL terminated C strings
103 -- * the Haskell string may *not* contain any NUL characters
105 -- * new storage is allocated for the C string and must be explicitly freed
107 newCString :: String -> IO CString
108 newCString = newArray0 nUL . charsToCChars
110 -- marshal a Haskell string into a C string (ie, character array) with
111 -- explicit length information
113 -- * new storage is allocated for the C string and must be explicitly freed
115 newCStringLen :: String -> IO CStringLen
116 newCStringLen str = do a <- newArray (charsToCChars str)
117 return (pairLength str a)
119 -- marshal a Haskell string into a NUL terminated C strings using temporary
122 -- * the Haskell string may *not* contain any NUL characters
124 -- * see the lifetime constraints of `MarshalAlloc.alloca'
126 withCString :: String -> (CString -> IO a) -> IO a
127 withCString = withArray0 nUL . charsToCChars
129 -- marshal a Haskell string into a NUL terminated C strings using temporary
132 -- * the Haskell string may *not* contain any NUL characters
134 -- * see the lifetime constraints of `MarshalAlloc.alloca'
136 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
137 withCStringLen str act = withArray (charsToCChars str) $ act . pairLength str
139 -- auxilliary definitions
140 -- ----------------------
142 -- C's end of string character
147 -- pair a C string with the length of the given Haskell string
149 pairLength :: String -> CString -> CStringLen
150 pairLength = flip (,) . length
152 -- cast [CChar] to [Char]
154 cCharsToChars :: [CChar] -> [Char]
155 cCharsToChars = map castCCharToChar
157 -- cast [Char] to [CChar]
159 charsToCChars :: [Char] -> [CChar]
160 charsToCChars = map castCharToCChar
162 castCCharToChar :: CChar -> Char
163 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
165 castCharToCChar :: Char -> CChar
166 castCharToCChar ch = fromIntegral (ord ch)
172 withUnsafeCString :: String -> (UnsafeCString -> IO a) -> IO a
173 #if __GLASGOW_HASKELL__
174 newtype UnsafeCString = UnsafeCString (ByteArray Int)
175 withUnsafeCString s f = f (UnsafeCString (packString s))
177 newtype UnsafeCString = UnsafeCString (Ptr CChar)
178 withUnsafeCString s f = withCString s (\p -> f (UnsafeCString p))