1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
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 C string (ie, character array)
172 -- in temporary storage, with explicit length information.
174 -- * the memory is freed when the subcomputation terminates (either
175 -- normally or via an exception), so the pointer to the temporary
176 -- storage must /not/ be used after this.
178 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
179 withCStringLen = withCAStringLen
181 -- | Determines whether a character can be accurately encoded in a 'CString'.
182 -- Unrepresentable characters are converted to @\'?\'@.
184 -- Currently only Latin-1 characters are representable.
185 charIsRepresentable :: Char -> IO Bool
186 charIsRepresentable c = return (ord c < 256)
188 -- single byte characters
189 -- ----------------------
191 -- ** NOTE: These routines don't handle conversions! **
193 -- | Convert a C byte, representing a Latin-1 character, to the corresponding
194 -- Haskell character.
195 castCCharToChar :: CChar -> Char
196 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
198 -- | Convert a Haskell character to a C character.
199 -- This function is only safe on the first 256 characters.
200 castCharToCChar :: Char -> CChar
201 castCharToCChar ch = fromIntegral (ord ch)
203 -- | Marshal a NUL terminated C string into a Haskell string.
205 peekCAString :: CString -> IO String
206 #ifndef __GLASGOW_HASKELL__
208 cs <- peekArray0 nUL cp
209 return (cCharsToChars cs)
212 l <- lengthArray0 nUL cp
213 if l <= 0 then return "" else loop "" (l-1)
216 xval <- peekElemOff cp i
217 let val = castCCharToChar xval
218 val `seq` if i <= 0 then return (val:s) else loop (val:s) (i-1)
221 -- | Marshal a C string with explicit length into a Haskell string.
223 peekCAStringLen :: CStringLen -> IO String
224 #ifndef __GLASGOW_HASKELL__
225 peekCAStringLen (cp, len) = do
226 cs <- peekArray len cp
227 return (cCharsToChars cs)
229 peekCAStringLen (cp, len)
230 | len <= 0 = return "" -- being (too?) nice.
231 | otherwise = loop [] (len-1)
234 xval <- peekElemOff cp i
235 let val = castCCharToChar xval
236 -- blow away the coercion ASAP.
237 if (val `seq` (i == 0))
238 then return (val:acc)
239 else loop (val:acc) (i-1)
242 -- | Marshal a Haskell string into a NUL terminated C string.
244 -- * the Haskell string may /not/ contain any NUL characters
246 -- * new storage is allocated for the C string and must be
247 -- explicitly freed using 'Foreign.Marshal.Alloc.free' or
248 -- 'Foreign.Marshal.Alloc.finalizerFree'.
250 newCAString :: String -> IO CString
251 #ifndef __GLASGOW_HASKELL__
252 newCAString = newArray0 nUL . charsToCChars
255 ptr <- mallocArray0 (length str)
257 go [] n = pokeElemOff ptr n nUL
258 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
263 -- | Marshal a Haskell string into a C string (ie, character array) with
264 -- explicit length information.
266 -- * new storage is allocated for the C string and must be
267 -- explicitly freed using 'Foreign.Marshal.Alloc.free' or
268 -- 'Foreign.Marshal.Alloc.finalizerFree'.
270 newCAStringLen :: String -> IO CStringLen
271 #ifndef __GLASGOW_HASKELL__
272 newCAStringLen str = newArrayLen (charsToCChars str)
274 newCAStringLen str = do
275 ptr <- mallocArray0 len
277 go [] n = n `seq` return () -- make it strict in n
278 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
285 -- | Marshal a Haskell string into a NUL terminated C string using temporary
288 -- * the Haskell string may /not/ contain any NUL characters
290 -- * the memory is freed when the subcomputation terminates (either
291 -- normally or via an exception), so the pointer to the temporary
292 -- storage must /not/ be used after this.
294 withCAString :: String -> (CString -> IO a) -> IO a
295 #ifndef __GLASGOW_HASKELL__
296 withCAString = withArray0 nUL . charsToCChars
299 allocaArray0 (length str) $ \ptr ->
301 go [] n = pokeElemOff ptr n nUL
302 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
308 -- | Marshal a Haskell string into a C string (ie, character array)
309 -- in temporary storage, with explicit length information.
311 -- * the memory is freed when the subcomputation terminates (either
312 -- normally or via an exception), so the pointer to the temporary
313 -- storage must /not/ be used after this.
315 withCAStringLen :: String -> (CStringLen -> IO a) -> IO a
316 withCAStringLen str f =
317 #ifndef __GLASGOW_HASKELL__
318 withArrayLen (charsToCChars str) $ \ len ptr -> f (ptr, len)
320 allocaArray len $ \ptr ->
322 go [] n = n `seq` return () -- make it strict in n
323 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
331 -- auxiliary definitions
332 -- ----------------------
334 -- C's end of string character
339 -- allocate an array to hold the list and pair it with the number of elements
340 newArrayLen :: Storable a => [a] -> IO (Ptr a, Int)
343 return (a, length xs)
345 #ifndef __GLASGOW_HASKELL__
346 -- cast [CChar] to [Char]
348 cCharsToChars :: [CChar] -> [Char]
349 cCharsToChars xs = map castCCharToChar xs
351 -- cast [Char] to [CChar]
353 charsToCChars :: [Char] -> [CChar]
354 charsToCChars xs = map castCharToCChar xs
357 -----------------------------------------------------------------------------
360 -- representation of wide strings in C
361 -- -----------------------------------
363 -- | A C wide string is a reference to an array of C wide characters
364 -- terminated by NUL.
365 type CWString = Ptr CWchar
367 -- | A wide character string with explicit length information in 'CWchar's
368 -- instead of a terminating NUL (allowing NUL characters in the middle
370 type CWStringLen = (Ptr CWchar, Int)
372 -- | Marshal a NUL terminated C wide string into a Haskell string.
374 peekCWString :: CWString -> IO String
376 cs <- peekArray0 wNUL cp
377 return (cWcharsToChars cs)
379 -- | Marshal a C wide string with explicit length into a Haskell string.
381 peekCWStringLen :: CWStringLen -> IO String
382 peekCWStringLen (cp, len) = do
383 cs <- peekArray len cp
384 return (cWcharsToChars cs)
386 -- | Marshal a Haskell string into a NUL terminated C wide string.
388 -- * the Haskell string may /not/ contain any NUL characters
390 -- * new storage is allocated for the C wide string and must
391 -- be explicitly freed using 'Foreign.Marshal.Alloc.free' or
392 -- 'Foreign.Marshal.Alloc.finalizerFree'.
394 newCWString :: String -> IO CWString
395 newCWString = newArray0 wNUL . charsToCWchars
397 -- | Marshal a Haskell string into a C wide string (ie, wide character array)
398 -- with explicit length information.
400 -- * new storage is allocated for the C wide string and must
401 -- be explicitly freed using 'Foreign.Marshal.Alloc.free' or
402 -- 'Foreign.Marshal.Alloc.finalizerFree'.
404 newCWStringLen :: String -> IO CWStringLen
405 newCWStringLen str = newArrayLen (charsToCWchars str)
407 -- | Marshal a Haskell string into a NUL terminated C wide string using
408 -- temporary storage.
410 -- * the Haskell string may /not/ contain any NUL characters
412 -- * the memory is freed when the subcomputation terminates (either
413 -- normally or via an exception), so the pointer to the temporary
414 -- storage must /not/ be used after this.
416 withCWString :: String -> (CWString -> IO a) -> IO a
417 withCWString = withArray0 wNUL . charsToCWchars
419 -- | Marshal a Haskell string into a NUL terminated C wide string using
420 -- temporary storage.
422 -- * the Haskell string may /not/ contain any NUL characters
424 -- * the memory is freed when the subcomputation terminates (either
425 -- normally or via an exception), so the pointer to the temporary
426 -- storage must /not/ be used after this.
428 withCWStringLen :: String -> (CWStringLen -> IO a) -> IO a
429 withCWStringLen str f =
430 withArrayLen (charsToCWchars str) $ \ len ptr -> f (ptr, len)
432 -- auxiliary definitions
433 -- ----------------------
438 cWcharsToChars :: [CWchar] -> [Char]
439 charsToCWchars :: [Char] -> [CWchar]
441 #ifdef mingw32_HOST_OS
443 -- On Windows, wchar_t is 16 bits wide and CWString uses the UTF-16 encoding.
445 -- coding errors generate Chars in the surrogate range
446 cWcharsToChars = map chr . fromUTF16 . map fromIntegral
448 fromUTF16 (c1:c2:wcs)
449 | 0xd800 <= c1 && c1 <= 0xdbff && 0xdc00 <= c2 && c2 <= 0xdfff =
450 ((c1 - 0xd800)*0x400 + (c2 - 0xdc00) + 0x10000) : fromUTF16 wcs
451 fromUTF16 (c:wcs) = c : fromUTF16 wcs
454 charsToCWchars = foldr utf16Char [] . map ord
457 | c < 0x10000 = fromIntegral c : wcs
458 | otherwise = let c' = c - 0x10000 in
459 fromIntegral (c' `div` 0x400 + 0xd800) :
460 fromIntegral (c' `mod` 0x400 + 0xdc00) : wcs
462 #else /* !mingw32_HOST_OS */
464 cWcharsToChars xs = map castCWcharToChar xs
465 charsToCWchars xs = map castCharToCWchar xs
467 -- These conversions only make sense if __STDC_ISO_10646__ is defined
468 -- (meaning that wchar_t is ISO 10646, aka Unicode)
470 castCWcharToChar :: CWchar -> Char
471 castCWcharToChar ch = chr (fromIntegral ch )
473 castCharToCWchar :: Char -> CWchar
474 castCharToCWchar ch = fromIntegral (ord ch)
476 #endif /* !mingw32_HOST_OS */