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 marshalling of C strings.
14 -- The marshalling converts each Haskell character, representing a Unicode
15 -- code point, to one or more bytes in a manner that, by default, is
16 -- determined by the current locale. As a consequence, no guarantees
17 -- can be made about the relative length of a Haskell string and its
18 -- corresponding C string, and therefore all the marshalling routines
19 -- include memory allocation. The translation between Unicode and the
20 -- encoding of the current locale may be lossy.
22 -----------------------------------------------------------------------------
24 module Foreign.C.String ( -- representation of strings in C
28 CString, -- = Ptr CChar
29 CStringLen, -- = (Ptr CChar, Int)
31 -- ** Using a locale-dependent encoding
33 -- | Currently these functions are identical to their @CAString@ counterparts;
34 -- eventually they will use an encoding determined by the current locale.
36 -- conversion of C strings into Haskell strings
38 peekCString, -- :: CString -> IO String
39 peekCStringLen, -- :: CStringLen -> IO String
41 -- conversion of Haskell strings into C strings
43 newCString, -- :: String -> IO CString
44 newCStringLen, -- :: String -> IO CStringLen
46 -- conversion of Haskell strings into C strings using temporary storage
48 withCString, -- :: String -> (CString -> IO a) -> IO a
49 withCStringLen, -- :: String -> (CStringLen -> IO a) -> IO a
51 charIsRepresentable, -- :: Char -> IO Bool
53 -- ** Using 8-bit characters
55 -- | These variants of the above functions are for use with C libraries
56 -- that are ignorant of Unicode. These functions should be used with
57 -- care, as a loss of information can occur.
59 castCharToCChar, -- :: Char -> CChar
60 castCCharToChar, -- :: CChar -> Char
62 peekCAString, -- :: CString -> IO String
63 peekCAStringLen, -- :: CStringLen -> IO String
64 newCAString, -- :: String -> IO CString
65 newCAStringLen, -- :: String -> IO CStringLen
66 withCAString, -- :: String -> (CString -> IO a) -> IO a
67 withCAStringLen, -- :: String -> (CStringLen -> IO a) -> IO a
71 -- | These variants of the above functions are for use with C libraries
72 -- that encode Unicode using the C @wchar_t@ type in a system-dependent
73 -- way. The only encodings supported are
75 -- * UTF-32 (the C compiler defines @__STDC_ISO_10646__@), or
77 -- * UTF-16 (as used on Windows systems).
79 CWString, -- = Ptr CWchar
80 CWStringLen, -- = (Ptr CWchar, Int)
82 peekCWString, -- :: CWString -> IO String
83 peekCWStringLen, -- :: CWStringLen -> IO String
84 newCWString, -- :: String -> IO CWString
85 newCWStringLen, -- :: String -> IO CWStringLen
86 withCWString, -- :: String -> (CWString -> IO a) -> IO a
87 withCWStringLen, -- :: String -> (CWStringLen -> IO a) -> IO a
91 import Foreign.Marshal.Array
92 import Foreign.C.Types
94 import Foreign.Storable
98 #ifdef __GLASGOW_HASKELL__
105 import Data.Char ( chr, ord )
106 #define unsafeChr chr
109 -----------------------------------------------------------------------------
112 -- representation of strings in C
113 -- ------------------------------
115 -- | A C string is a reference to an array of C characters terminated by NUL.
116 type CString = Ptr CChar
118 -- | A string with explicit length information in bytes instead of a
119 -- terminating NUL (allowing NUL characters in the middle of the string).
120 type CStringLen = (Ptr CChar, Int)
122 -- exported functions
123 -- ------------------
125 -- * the following routines apply the default conversion when converting the
126 -- C-land character encoding into the Haskell-land character encoding
128 -- | Marshal a NUL terminated C string into a Haskell string.
130 peekCString :: CString -> IO String
131 peekCString = peekCAString
133 -- | Marshal a C string with explicit length into a Haskell string.
135 peekCStringLen :: CStringLen -> IO String
136 peekCStringLen = peekCAStringLen
138 -- | Marshal a Haskell string into a NUL terminated C string.
140 -- * the Haskell string may /not/ contain any NUL characters
142 -- * new storage is allocated for the C string and must be
143 -- explicitly freed using 'Foreign.Marshal.Alloc.free' or
144 -- 'Foreign.Marshal.Alloc.finalizerFree'.
146 newCString :: String -> IO CString
147 newCString = newCAString
149 -- | Marshal a Haskell string into a C string (ie, character array) with
150 -- explicit length information.
152 -- * new storage is allocated for the C string and must be
153 -- explicitly freed using 'Foreign.Marshal.Alloc.free' or
154 -- 'Foreign.Marshal.Alloc.finalizerFree'.
156 newCStringLen :: String -> IO CStringLen
157 newCStringLen = newCAStringLen
159 -- | Marshal a Haskell string into a NUL terminated C string using temporary
162 -- * the Haskell string may /not/ contain any NUL characters
164 -- * the memory is freed when the subcomputation terminates (either
165 -- normally or via an exception), so the pointer to the temporary
166 -- storage must /not/ be used after this.
168 withCString :: String -> (CString -> IO a) -> IO a
169 withCString = withCAString
171 -- | Marshal a Haskell string into a NUL terminated C string using temporary
174 -- * the Haskell string may /not/ contain any NUL characters
176 -- * the memory is freed when the subcomputation terminates (either
177 -- normally or via an exception), so the pointer to the temporary
178 -- storage must /not/ be used after this.
180 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
181 withCStringLen = withCAStringLen
183 -- | Determines whether a character can be accurately encoded in a 'CString'.
184 -- Unrepresentable characters are converted to @\'?\'@.
186 -- Currently only Latin-1 characters are representable.
187 charIsRepresentable :: Char -> IO Bool
188 charIsRepresentable c = return (ord c < 256)
190 -- single byte characters
191 -- ----------------------
193 -- ** NOTE: These routines don't handle conversions! **
195 -- | Convert a C byte, representing a Latin-1 character, to the corresponding
196 -- Haskell character.
197 castCCharToChar :: CChar -> Char
198 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
200 -- | Convert a Haskell character to a C character.
201 -- This function is only safe on the first 256 characters.
202 castCharToCChar :: Char -> CChar
203 castCharToCChar ch = fromIntegral (ord ch)
205 -- | Marshal a NUL terminated C string into a Haskell string.
207 peekCAString :: CString -> IO String
208 #ifndef __GLASGOW_HASKELL__
210 cs <- peekArray0 nUL cp
211 return (cCharsToChars cs)
214 l <- lengthArray0 nUL cp
215 if l <= 0 then return "" else loop "" (l-1)
218 xval <- peekElemOff cp i
219 let val = castCCharToChar xval
220 val `seq` if i <= 0 then return (val:s) else loop (val:s) (i-1)
223 -- | Marshal a C string with explicit length into a Haskell string.
225 peekCAStringLen :: CStringLen -> IO String
226 #ifndef __GLASGOW_HASKELL__
227 peekCAStringLen (cp, len) = do
228 cs <- peekArray len cp
229 return (cCharsToChars cs)
231 peekCAStringLen (cp, len)
232 | len <= 0 = return "" -- being (too?) nice.
233 | otherwise = loop [] (len-1)
236 xval <- peekElemOff cp i
237 let val = castCCharToChar xval
238 -- blow away the coercion ASAP.
239 if (val `seq` (i == 0))
240 then return (val:acc)
241 else loop (val:acc) (i-1)
244 -- | Marshal a Haskell string into a NUL terminated C string.
246 -- * the Haskell string may /not/ contain any NUL characters
248 -- * new storage is allocated for the C string and must be
249 -- explicitly freed using 'Foreign.Marshal.Alloc.free' or
250 -- 'Foreign.Marshal.Alloc.finalizerFree'.
252 newCAString :: String -> IO CString
253 #ifndef __GLASGOW_HASKELL__
254 newCAString = newArray0 nUL . charsToCChars
257 ptr <- mallocArray0 (length str)
259 go [] n = pokeElemOff ptr n nUL
260 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
265 -- | Marshal a Haskell string into a C string (ie, character array) with
266 -- explicit length information.
268 -- * new storage is allocated for the C string and must be
269 -- explicitly freed using 'Foreign.Marshal.Alloc.free' or
270 -- 'Foreign.Marshal.Alloc.finalizerFree'.
272 newCAStringLen :: String -> IO CStringLen
273 #ifndef __GLASGOW_HASKELL__
274 newCAStringLen str = do
275 a <- newArray (charsToCChars str)
276 return (pairLength str a)
278 newCAStringLen str = do
279 ptr <- mallocArray0 len
281 go [] n = n `seq` return n -- make it strict in n
282 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
287 -- | Marshal a Haskell string into a NUL terminated C string using temporary
290 -- * the Haskell string may /not/ contain any NUL characters
292 -- * the memory is freed when the subcomputation terminates (either
293 -- normally or via an exception), so the pointer to the temporary
294 -- storage must /not/ be used after this.
296 withCAString :: String -> (CString -> IO a) -> IO a
297 #ifndef __GLASGOW_HASKELL__
298 withCAString = withArray0 nUL . charsToCChars
301 allocaArray0 (length str) $ \ptr ->
303 go [] n = pokeElemOff ptr n nUL
304 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
310 -- | Marshal a Haskell string into a NUL terminated C string using temporary
313 -- * the Haskell string may /not/ contain any NUL characters
315 -- * the memory is freed when the subcomputation terminates (either
316 -- normally or via an exception), so the pointer to the temporary
317 -- storage must /not/ be used after this.
319 withCAStringLen :: String -> (CStringLen -> IO a) -> IO a
320 #ifndef __GLASGOW_HASKELL__
321 withCAStringLen str act = withArray (charsToCChars str) $ act . pairLength str
323 withCAStringLen str f =
324 allocaArray len $ \ptr ->
326 go [] n = n `seq` return n -- make it strict in n
327 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
333 -- auxiliary definitions
334 -- ----------------------
336 -- C's end of string character
341 -- pair a C string with the length of the given Haskell string
343 pairLength :: String -> a -> (a, Int)
344 pairLength = flip (,) . length
346 #ifndef __GLASGOW_HASKELL__
347 -- cast [CChar] to [Char]
349 cCharsToChars :: [CChar] -> [Char]
350 cCharsToChars xs = map castCCharToChar xs
352 -- cast [Char] to [CChar]
354 charsToCChars :: [Char] -> [CChar]
355 charsToCChars xs = map castCharToCChar xs
358 -----------------------------------------------------------------------------
361 -- representation of wide strings in C
362 -- -----------------------------------
364 -- | A C wide string is a reference to an array of C wide characters
365 -- terminated by NUL.
366 type CWString = Ptr CWchar
368 -- | A wide character string with explicit length information in bytes
369 -- instead of a terminating NUL (allowing NUL characters in the middle
371 type CWStringLen = (Ptr CWchar, Int)
373 -- | Marshal a NUL terminated C wide string into a Haskell string.
375 peekCWString :: CWString -> IO String
377 cs <- peekArray0 wNUL cp
378 return (cWcharsToChars cs)
380 -- | Marshal a C wide string with explicit length into a Haskell string.
382 peekCWStringLen :: CWStringLen -> IO String
383 peekCWStringLen (cp, len) = do
384 cs <- peekArray len cp
385 return (cWcharsToChars cs)
387 -- | Marshal a Haskell string into a NUL terminated C wide string.
389 -- * the Haskell string may /not/ contain any NUL characters
391 -- * new storage is allocated for the C wide string and must
392 -- be explicitly freed using 'Foreign.Marshal.Alloc.free' or
393 -- 'Foreign.Marshal.Alloc.finalizerFree'.
395 newCWString :: String -> IO CWString
396 newCWString = newArray0 wNUL . charsToCWchars
398 -- | Marshal a Haskell string into a C wide string (ie, wide character array)
399 -- with explicit length information.
401 -- * new storage is allocated for the C wide string and must
402 -- be explicitly freed using 'Foreign.Marshal.Alloc.free' or
403 -- 'Foreign.Marshal.Alloc.finalizerFree'.
405 newCWStringLen :: String -> IO CWStringLen
406 newCWStringLen str = do
407 a <- newArray (charsToCWchars str)
408 return (pairLength str a)
410 -- | Marshal a Haskell string into a NUL terminated C wide string using
411 -- temporary storage.
413 -- * the Haskell string may /not/ contain any NUL characters
415 -- * the memory is freed when the subcomputation terminates (either
416 -- normally or via an exception), so the pointer to the temporary
417 -- storage must /not/ be used after this.
419 withCWString :: String -> (CWString -> IO a) -> IO a
420 withCWString = withArray0 wNUL . charsToCWchars
422 -- | Marshal a Haskell string into a NUL terminated C wide string using
423 -- temporary storage.
425 -- * the Haskell string may /not/ contain any NUL characters
427 -- * the memory is freed when the subcomputation terminates (either
428 -- normally or via an exception), so the pointer to the temporary
429 -- storage must /not/ be used after this.
431 withCWStringLen :: String -> (CWStringLen -> IO a) -> IO a
432 withCWStringLen str act = withArray (charsToCWchars str) $ act . pairLength str
434 -- auxiliary definitions
435 -- ----------------------
440 cWcharsToChars :: [CWchar] -> [Char]
441 charsToCWchars :: [Char] -> [CWchar]
443 #ifdef mingw32_TARGET_OS
445 -- On Windows, wchar_t is 16 bits wide and CWString uses the UTF-16 encoding.
447 -- coding errors generate Chars in the surrogate range
448 cWcharsToChars = map chr . fromUTF16 . map fromIntegral
450 fromUTF16 (c1:c2:wcs)
451 | 0xd800 <= c1 && c1 <= 0xdbff && 0xdc00 <= c2 && c2 <= 0xdfff =
452 ((c1 - 0xd800)*0x400 + (c2 - 0xdc00) + 0x10000) : fromUTF16 wcs
453 fromUTF16 (c:wcs) = c : fromUTF16 wcs
456 charsToCWchars = foldr utf16Char [] . map ord
459 | c < 0x10000 = fromIntegral c : wcs
460 | otherwise = let c' = c - 0x10000 in
461 fromIntegral (c' `div` 0x400 + 0xd800) :
462 fromIntegral (c' `mod` 0x400 + 0xdc00) : wcs
464 #else /* !mingw32_TARGET_OS */
466 cWcharsToChars xs = map castCWcharToChar xs
467 charsToCWchars xs = map castCharToCWchar xs
469 -- These conversions only make sense if __STDC_ISO_10646__ is defined
470 -- (meaning that wchar_t is ISO 10646, aka Unicode)
472 castCWcharToChar :: CWchar -> Char
473 castCWcharToChar ch = chr (fromIntegral ch )
475 castCharToCWchar :: Char -> CWchar
476 castCharToCWchar ch = fromIntegral (ord ch)
478 #endif /* !mingw32_TARGET_OS */