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/base/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- Utilities for primitive marshaling
14 -----------------------------------------------------------------------------
16 module Foreign.C.String ( -- representation of strings in C
18 CString, -- = Ptr CChar
19 CStringLen, -- = (CString, Int)
21 -- conversion of C strings into Haskell strings
23 peekCString, -- :: CString -> IO String
24 peekCStringLen, -- :: CStringLen -> IO String
26 -- conversion of Haskell strings into C strings
28 newCString, -- :: String -> IO CString
29 newCStringLen, -- :: String -> IO CStringLen
31 -- conversion of Haskell strings into C strings using temporary storage
33 withCString, -- :: String -> (CString -> IO a) -> IO a
34 withCStringLen, -- :: String -> (CStringLen -> IO a) -> IO a
36 -- conversion between Haskell and C characters *ignoring* the encoding
38 castCharToCChar, -- :: Char -> CChar
39 castCCharToChar, -- :: CChar -> Char
43 import Foreign.Marshal.Array
44 import Foreign.C.Types
46 import Foreign.Storable
50 #ifdef __GLASGOW_HASKELL__
57 import Data.Char ( chr, ord )
61 -----------------------------------------------------------------------------
64 -- representation of strings in C
65 -- ------------------------------
67 type CString = Ptr CChar -- conventional NUL terminates strings
68 type CStringLen = (CString, Int) -- strings with explicit length
74 -- * the following routines apply the default conversion when converting the
75 -- C-land character encoding into the Haskell-land character encoding
77 -- ** NOTE: The current implementation doesn't handle conversions yet! **
79 -- * the routines using an explicit length tolerate NUL characters in the
83 -- marshal a NUL terminated C string into a Haskell string
85 peekCString :: CString -> IO String
86 #ifndef __GLASGOW_HASKELL__
87 peekCString cp = do cs <- peekArray0 nUL cp; return (cCharsToChars cs)
90 l <- lengthArray0 nUL cp
91 if l <= 0 then return "" else loop "" (l-1)
94 xval <- peekElemOff cp i
95 let val = castCCharToChar xval
96 val `seq` if i <= 0 then return (val:s) else loop (val:s) (i-1)
99 -- marshal a C string with explicit length into a Haskell string
101 peekCStringLen :: CStringLen -> IO String
102 #ifndef __GLASGOW_HASKELL__
103 peekCStringLen (cp, len) = do cs <- peekArray len cp; return (cCharsToChars cs)
105 peekCStringLen (cp, len)
106 | len <= 0 = return "" -- being (too?) nice.
107 | otherwise = loop [] (len-1)
110 xval <- peekElemOff cp i
111 let val = castCCharToChar xval
112 -- blow away the coercion ASAP.
113 if (val `seq` (i == 0))
114 then return (val:acc)
115 else loop (val:acc) (i-1)
118 -- marshal a Haskell string into a NUL terminated C strings
120 -- * the Haskell string may *not* contain any NUL characters
122 -- * new storage is allocated for the C string and must be explicitly freed
124 newCString :: String -> IO CString
125 #ifndef __GLASGOW_HASKELL__
126 newCString = newArray0 nUL . charsToCChars
129 ptr <- mallocArray0 (length str)
131 go [] n# = pokeElemOff ptr (I# n#) nUL
132 go (c:cs) n# = do pokeElemOff ptr (I# n#) (castCharToCChar c); go cs (n# +# 1#)
137 -- marshal a Haskell string into a C string (ie, character array) with
138 -- explicit length information
140 -- * new storage is allocated for the C string and must be explicitly freed
142 newCStringLen :: String -> IO CStringLen
143 #ifndef __GLASGOW_HASKELL__
144 newCStringLen str = do a <- newArray (charsToCChars str)
145 return (pairLength str a)
147 newCStringLen str = do
148 ptr <- mallocArray0 len
151 go (c:cs) n# = do pokeElemOff ptr (I# n#) (castCharToCChar c); go cs (n# +# 1#)
158 -- marshal a Haskell string into a NUL terminated C strings using temporary
161 -- * the Haskell string may *not* contain any NUL characters
163 -- * see the lifetime constraints of `MarshalAlloc.alloca'
165 withCString :: String -> (CString -> IO a) -> IO a
166 #ifndef __GLASGOW_HASKELL__
167 withCString = withArray0 nUL . charsToCChars
170 allocaArray0 (length str) $ \ptr ->
172 go [] n# = pokeElemOff ptr (I# n#) nUL
173 go (c:cs) n# = do pokeElemOff ptr (I# n#) (castCharToCChar c); go cs (n# +# 1#)
179 -- marshal a Haskell string into a NUL terminated C strings using temporary
182 -- * the Haskell string may *not* contain any NUL characters
184 -- * see the lifetime constraints of `MarshalAlloc.alloca'
186 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
187 #ifndef __GLASGOW_HASKELL__
188 withCStringLen str act = withArray (charsToCChars str) $ act . pairLength str
190 withCStringLen str f =
191 allocaArray len $ \ptr ->
194 go (c:cs) n# = do pokeElemOff ptr (I# n#) (castCharToCChar c); go cs (n# +# 1#)
202 -- auxilliary definitions
203 -- ----------------------
205 -- C's end of string character
210 -- pair a C string with the length of the given Haskell string
212 pairLength :: String -> CString -> CStringLen
213 pairLength = flip (,) . length
215 -- cast [CChar] to [Char]
217 cCharsToChars :: [CChar] -> [Char]
218 cCharsToChars xs = map castCCharToChar xs
220 -- cast [Char] to [CChar]
222 charsToCChars :: [Char] -> [CChar]
223 charsToCChars xs = map castCharToCChar xs
225 castCCharToChar :: CChar -> Char
226 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
228 castCharToCChar :: Char -> CChar
229 castCharToCChar ch = fromIntegral (ord ch)