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 explicitly freed
144 newCString :: String -> IO CString
145 newCString = newCAString
147 -- | Marshal a Haskell string into a C string (ie, character array) with
148 -- explicit length information.
150 -- * new storage is allocated for the C string and must be explicitly freed
152 newCStringLen :: String -> IO CStringLen
153 newCStringLen = newCAStringLen
155 -- | Marshal a Haskell string into a NUL terminated C string using temporary
158 -- * the Haskell string may /not/ contain any NUL characters
160 -- * see the lifetime constraints of 'Foreign.Marshal.Alloc.alloca'
162 withCString :: String -> (CString -> IO a) -> IO a
163 withCString = withCAString
165 -- | Marshal a Haskell string into a NUL terminated C string using temporary
168 -- * the Haskell string may /not/ contain any NUL characters
170 -- * see the lifetime constraints of 'Foreign.Marshal.Alloc.alloca'
172 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
173 withCStringLen = withCAStringLen
175 -- | Determines whether a character can be accurately encoded in a 'CString'.
176 -- Unrepresentable characters are converted to @\'?\'@.
178 -- Currently only Latin-1 characters are representable.
179 charIsRepresentable :: Char -> IO Bool
180 charIsRepresentable c = return (ord c < 256)
182 -- single byte characters
183 -- ----------------------
185 -- ** NOTE: These routines don't handle conversions! **
187 -- | Convert a C byte, representing a Latin-1 character, to the corresponding
188 -- Haskell character.
189 castCCharToChar :: CChar -> Char
190 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
192 -- | Convert a Haskell character to a C character.
193 -- This function is only safe on the first 256 characters.
194 castCharToCChar :: Char -> CChar
195 castCharToCChar ch = fromIntegral (ord ch)
197 -- | Marshal a NUL terminated C string into a Haskell string.
199 peekCAString :: CString -> IO String
200 #ifndef __GLASGOW_HASKELL__
202 cs <- peekArray0 nUL cp
203 return (cCharsToChars cs)
206 l <- lengthArray0 nUL cp
207 if l <= 0 then return "" else loop "" (l-1)
210 xval <- peekElemOff cp i
211 let val = castCCharToChar xval
212 val `seq` if i <= 0 then return (val:s) else loop (val:s) (i-1)
215 -- | Marshal a C string with explicit length into a Haskell string.
217 peekCAStringLen :: CStringLen -> IO String
218 #ifndef __GLASGOW_HASKELL__
219 peekCAStringLen (cp, len) = do
220 cs <- peekArray len cp
221 return (cCharsToChars cs)
223 peekCAStringLen (cp, len)
224 | len <= 0 = return "" -- being (too?) nice.
225 | otherwise = loop [] (len-1)
228 xval <- peekElemOff cp i
229 let val = castCCharToChar xval
230 -- blow away the coercion ASAP.
231 if (val `seq` (i == 0))
232 then return (val:acc)
233 else loop (val:acc) (i-1)
236 -- | Marshal a Haskell string into a NUL terminated C string.
238 -- * the Haskell string may /not/ contain any NUL characters
240 -- * new storage is allocated for the C string and must be explicitly freed
242 newCAString :: String -> IO CString
243 #ifndef __GLASGOW_HASKELL__
244 newCAString = newArray0 nUL . charsToCChars
247 ptr <- mallocArray0 (length str)
249 go [] n = pokeElemOff ptr n nUL
250 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
255 -- | Marshal a Haskell string into a C string (ie, character array) with
256 -- explicit length information.
258 -- * new storage is allocated for the C string and must be explicitly freed
260 newCAStringLen :: String -> IO CStringLen
261 #ifndef __GLASGOW_HASKELL__
262 newCAStringLen str = do
263 a <- newArray (charsToCChars str)
264 return (pairLength str a)
266 newCAStringLen str = do
267 ptr <- mallocArray0 len
269 go [] n = n `seq` return () -- make it strict in n
270 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
277 -- | Marshal a Haskell string into a NUL terminated C string using temporary
280 -- * the Haskell string may /not/ contain any NUL characters
282 -- * see the lifetime constraints of 'Foreign.Marshal.Alloc.alloca'
284 withCAString :: String -> (CString -> IO a) -> IO a
285 #ifndef __GLASGOW_HASKELL__
286 withCAString = withArray0 nUL . charsToCChars
289 allocaArray0 (length str) $ \ptr ->
291 go [] n = pokeElemOff ptr n nUL
292 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
298 -- | Marshal a Haskell string into a NUL terminated C string using temporary
301 -- * the Haskell string may /not/ contain any NUL characters
303 -- * see the lifetime constraints of 'Foreign.Marshal.Alloc.alloca'
305 withCAStringLen :: String -> (CStringLen -> IO a) -> IO a
306 #ifndef __GLASGOW_HASKELL__
307 withCAStringLen str act = withArray (charsToCChars str) $ act . pairLength str
309 withCAStringLen str f =
310 allocaArray len $ \ptr ->
312 go [] n = n `seq` return () -- make it strict in n
313 go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
321 -- auxiliary definitions
322 -- ----------------------
324 -- C's end of string character
329 -- pair a C string with the length of the given Haskell string
331 pairLength :: String -> a -> (a, Int)
332 pairLength = flip (,) . length
334 #ifndef __GLASGOW_HASKELL__
335 -- cast [CChar] to [Char]
337 cCharsToChars :: [CChar] -> [Char]
338 cCharsToChars xs = map castCCharToChar xs
340 -- cast [Char] to [CChar]
342 charsToCChars :: [Char] -> [CChar]
343 charsToCChars xs = map castCharToCChar xs
346 -----------------------------------------------------------------------------
349 -- representation of wide strings in C
350 -- -----------------------------------
352 -- | A C wide string is a reference to an array of C wide characters
353 -- terminated by NUL.
354 type CWString = Ptr CWchar
356 -- | A wide character string with explicit length information in bytes
357 -- instead of a terminating NUL (allowing NUL characters in the middle
359 type CWStringLen = (Ptr CWchar, Int)
361 -- | Marshal a NUL terminated C wide string into a Haskell string.
363 peekCWString :: CWString -> IO String
365 cs <- peekArray0 wNUL cp
366 return (cWcharsToChars cs)
368 -- | Marshal a C wide string with explicit length into a Haskell string.
370 peekCWStringLen :: CWStringLen -> IO String
371 peekCWStringLen (cp, len) = do
372 cs <- peekArray len cp
373 return (cWcharsToChars cs)
375 -- | Marshal a Haskell string into a NUL terminated C wide string.
377 -- * the Haskell string may /not/ contain any NUL characters
379 -- * new storage is allocated for the C string and must be explicitly freed
381 newCWString :: String -> IO CWString
382 newCWString = newArray0 wNUL . charsToCWchars
384 -- | Marshal a Haskell string into a C wide string (ie, wide character array)
385 -- with explicit length information.
387 -- * new storage is allocated for the C string and must be explicitly freed
389 newCWStringLen :: String -> IO CWStringLen
390 newCWStringLen str = do
391 a <- newArray (charsToCWchars str)
392 return (pairLength str a)
394 -- | Marshal a Haskell string into a NUL terminated C wide string using
395 -- temporary storage.
397 -- * the Haskell string may /not/ contain any NUL characters
399 -- * see the lifetime constraints of 'Foreign.Marshal.Alloc.alloca'
401 withCWString :: String -> (CWString -> IO a) -> IO a
402 withCWString = withArray0 wNUL . charsToCWchars
404 -- | Marshal a Haskell string into a NUL terminated C wide string using
405 -- temporary storage.
407 -- * the Haskell string may /not/ contain any NUL characters
409 -- * see the lifetime constraints of 'Foreign.Marshal.Alloc.alloca'
411 withCWStringLen :: String -> (CWStringLen -> IO a) -> IO a
412 withCWStringLen str act = withArray (charsToCWchars str) $ act . pairLength str
414 -- auxiliary definitions
415 -- ----------------------
420 cWcharsToChars :: [CWchar] -> [Char]
421 charsToCWchars :: [Char] -> [CWchar]
423 #ifdef mingw32_TARGET_OS
425 -- On Windows, wchar_t is 16 bits wide and CWString uses the UTF-16 encoding.
427 -- coding errors generate Chars in the surrogate range
428 cWcharsToChars = map chr . fromUTF16 . map fromIntegral
430 fromUTF16 (c1:c2:wcs)
431 | 0xd800 <= c1 && c1 <= 0xdbff && 0xdc00 <= c2 && c2 <= 0xdfff =
432 ((c1 - 0xd800)*0x400 + (c2 - 0xdc00) + 0x10000) : fromUTF16 wcs
433 fromUTF16 (c:wcs) = c : fromUTF16 wcs
436 charsToCWchars = foldr utf16Char [] . map ord
439 | c < 0x10000 = fromIntegral c : wcs
440 | otherwise = let c' = c - 0x10000 in
441 fromIntegral (c' `div` 0x400 + 0xd800) :
442 fromIntegral (c' `mod` 0x400 + 0xdc00) : wcs
444 #else /* !mingw32_TARGET_OS */
446 cWcharsToChars xs = map castCWcharToChar xs
447 charsToCWchars xs = map castCharToCWchar xs
449 -- These conversions only make sense if __STDC_ISO_10646__ is defined
450 -- (meaning that wchar_t is ISO 10646, aka Unicode)
452 castCWcharToChar :: CWchar -> Char
453 castCWcharToChar ch = chr (fromIntegral ch )
455 castCharToCWchar :: Char -> CWchar
456 castCharToCWchar ch = fromIntegral (ord ch)
458 #endif /* !mingw32_TARGET_OS */