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
49 #ifdef __GLASGOW_HASKELL__
56 import Char ( chr, ord )
60 -----------------------------------------------------------------------------
63 -- representation of strings in C
64 -- ------------------------------
66 type CString = Ptr CChar -- conventional NUL terminates strings
67 type CStringLen = (CString, Int) -- strings with explicit length
73 -- * the following routines apply the default conversion when converting the
74 -- C-land character encoding into the Haskell-land character encoding
76 -- ** NOTE: The current implementation doesn't handle conversions yet! **
78 -- * the routines using an explicit length tolerate NUL characters in the
82 -- marshal a NUL terminated C string into a Haskell string
84 peekCString :: CString -> IO String
85 peekCString cp = do cs <- peekArray0 nUL cp; return (cCharsToChars cs)
87 -- marshal a C string with explicit length into a Haskell string
89 peekCStringLen :: CStringLen -> IO String
90 peekCStringLen (cp, len) = do cs <- peekArray len cp; return (cCharsToChars cs)
92 -- marshal a Haskell string into a NUL terminated C strings
94 -- * the Haskell string may *not* contain any NUL characters
96 -- * new storage is allocated for the C string and must be explicitly freed
98 newCString :: String -> IO CString
99 newCString = newArray0 nUL . charsToCChars
101 -- marshal a Haskell string into a C string (ie, character array) with
102 -- explicit length information
104 -- * new storage is allocated for the C string and must be explicitly freed
106 newCStringLen :: String -> IO CStringLen
107 newCStringLen str = do a <- newArray (charsToCChars str)
108 return (pairLength str a)
110 -- marshal a Haskell string into a NUL terminated C strings using temporary
113 -- * the Haskell string may *not* contain any NUL characters
115 -- * see the lifetime constraints of `MarshalAlloc.alloca'
117 withCString :: String -> (CString -> IO a) -> IO a
118 withCString = withArray0 nUL . charsToCChars
120 -- marshal a Haskell string into a NUL terminated C strings using temporary
123 -- * the Haskell string may *not* contain any NUL characters
125 -- * see the lifetime constraints of `MarshalAlloc.alloca'
127 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
128 withCStringLen str act = withArray (charsToCChars str) $ act . pairLength str
130 -- auxilliary definitions
131 -- ----------------------
133 -- C's end of string character
138 -- pair a C string with the length of the given Haskell string
140 pairLength :: String -> CString -> CStringLen
141 pairLength = flip (,) . length
143 -- cast [CChar] to [Char]
145 cCharsToChars :: [CChar] -> [Char]
146 cCharsToChars = map castCCharToChar
148 -- cast [Char] to [CChar]
150 charsToCChars :: [Char] -> [CChar]
151 charsToCChars = map castCharToCChar
153 castCCharToChar :: CChar -> Char
154 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
156 castCharToCChar :: Char -> CChar
157 castCharToCChar ch = fromIntegral (ord ch)