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__
57 -----------------------------------------------------------------------------
60 -- representation of strings in C
61 -- ------------------------------
63 type CString = Ptr CChar -- conventional NUL terminates strings
64 type CStringLen = (CString, Int) -- strings with explicit length
70 -- * the following routines apply the default conversion when converting the
71 -- C-land character encoding into the Haskell-land character encoding
73 -- ** NOTE: The current implementation doesn't handle conversions yet! **
75 -- * the routines using an explicit length tolerate NUL characters in the
79 -- marshal a NUL terminated C string into a Haskell string
81 peekCString :: CString -> IO String
82 peekCString cp = do cs <- peekArray0 nUL cp; return (cCharsToChars cs)
84 -- marshal a C string with explicit length into a Haskell string
86 peekCStringLen :: CStringLen -> IO String
87 peekCStringLen (cp, len) = do cs <- peekArray len cp; return (cCharsToChars cs)
89 -- marshal a Haskell string into a NUL terminated C strings
91 -- * the Haskell string may *not* contain any NUL characters
93 -- * new storage is allocated for the C string and must be explicitly freed
95 newCString :: String -> IO CString
96 newCString = newArray0 nUL . charsToCChars
98 -- marshal a Haskell string into a C string (ie, character array) with
99 -- explicit length information
101 -- * new storage is allocated for the C string and must be explicitly freed
103 newCStringLen :: String -> IO CStringLen
104 newCStringLen str = do a <- newArray (charsToCChars str)
105 return (pairLength str a)
107 -- marshal a Haskell string into a NUL terminated C strings using temporary
110 -- * the Haskell string may *not* contain any NUL characters
112 -- * see the lifetime constraints of `MarshalAlloc.alloca'
114 withCString :: String -> (CString -> IO a) -> IO a
115 withCString = withArray0 nUL . charsToCChars
117 -- marshal a Haskell string into a NUL terminated C strings using temporary
120 -- * the Haskell string may *not* contain any NUL characters
122 -- * see the lifetime constraints of `MarshalAlloc.alloca'
124 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
125 withCStringLen str act = withArray (charsToCChars str) $ act . pairLength str
127 -- auxilliary definitions
128 -- ----------------------
130 -- C's end of string character
135 -- pair a C string with the length of the given Haskell string
137 pairLength :: String -> CString -> CStringLen
138 pairLength = flip (,) . length
140 -- cast [CChar] to [Char]
142 cCharsToChars :: [CChar] -> [Char]
143 cCharsToChars = map castCCharToChar
145 -- cast [Char] to [CChar]
147 charsToCChars :: [Char] -> [CChar]
148 charsToCChars = map castCharToCChar
150 castCCharToChar :: CChar -> Char
151 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
153 castCharToCChar :: Char -> CChar
154 castCharToCChar ch = fromIntegral (ord ch)