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/core/LICENSE)
8 -- Maintainer : ffi@haskell.org
9 -- Stability : provisional
10 -- Portability : portable
12 -- $Id: String.hs,v 1.4 2001/09/13 11:37:43 simonmar Exp $
14 -- Utilities for primitive marshaling
16 -----------------------------------------------------------------------------
18 module Foreign.C.String ( -- representation of strings in C
20 CString, -- = Ptr CChar
21 CStringLen, -- = (CString, Int)
23 -- conversion of C strings into Haskell strings
25 peekCString, -- :: CString -> IO String
26 peekCStringLen, -- :: CStringLen -> IO String
28 -- conversion of Haskell strings into C strings
30 newCString, -- :: String -> IO CString
31 newCStringLen, -- :: String -> IO CStringLen
33 -- conversion of Haskell strings into C strings using temporary storage
35 withCString, -- :: String -> (CString -> IO a) -> IO a
36 withCStringLen, -- :: String -> (CStringLen -> IO a) -> IO a
38 -- conversion between Haskell and C characters *ignoring* the encoding
40 castCharToCChar, -- :: Char -> CChar
41 castCCharToChar, -- :: CChar -> Char
45 import Foreign.Marshal.Array
46 import Foreign.C.Types
51 #ifdef __GLASGOW_HASKELL__
59 -----------------------------------------------------------------------------
62 -- representation of strings in C
63 -- ------------------------------
65 type CString = Ptr CChar -- conventional NUL terminates strings
66 type CStringLen = (CString, Int) -- strings with explicit length
72 -- * the following routines apply the default conversion when converting the
73 -- C-land character encoding into the Haskell-land character encoding
75 -- ** NOTE: The current implementation doesn't handle conversions yet! **
77 -- * the routines using an explicit length tolerate NUL characters in the
81 -- marshal a NUL terminated C string into a Haskell string
83 peekCString :: CString -> IO String
84 peekCString cp = do cs <- peekArray0 nUL cp; return (cCharsToChars cs)
86 -- marshal a C string with explicit length into a Haskell string
88 peekCStringLen :: CStringLen -> IO String
89 peekCStringLen (cp, len) = do cs <- peekArray len cp; return (cCharsToChars cs)
91 -- marshal a Haskell string into a NUL terminated C strings
93 -- * the Haskell string may *not* contain any NUL characters
95 -- * new storage is allocated for the C string and must be explicitly freed
97 newCString :: String -> IO CString
98 newCString = newArray0 nUL . charsToCChars
100 -- marshal a Haskell string into a C string (ie, character array) with
101 -- explicit length information
103 -- * new storage is allocated for the C string and must be explicitly freed
105 newCStringLen :: String -> IO CStringLen
106 newCStringLen str = do a <- newArray (charsToCChars str)
107 return (pairLength str a)
109 -- marshal a Haskell string into a NUL terminated C strings using temporary
112 -- * the Haskell string may *not* contain any NUL characters
114 -- * see the lifetime constraints of `MarshalAlloc.alloca'
116 withCString :: String -> (CString -> IO a) -> IO a
117 withCString = withArray0 nUL . charsToCChars
119 -- marshal a Haskell string into a NUL terminated C strings using temporary
122 -- * the Haskell string may *not* contain any NUL characters
124 -- * see the lifetime constraints of `MarshalAlloc.alloca'
126 withCStringLen :: String -> (CStringLen -> IO a) -> IO a
127 withCStringLen str act = withArray (charsToCChars str) $ act . pairLength str
129 -- auxilliary definitions
130 -- ----------------------
132 -- C's end of string character
137 -- pair a C string with the length of the given Haskell string
139 pairLength :: String -> CString -> CStringLen
140 pairLength = flip (,) . length
142 -- cast [CChar] to [Char]
144 cCharsToChars :: [CChar] -> [Char]
145 cCharsToChars = map castCCharToChar
147 -- cast [Char] to [CChar]
149 charsToCChars :: [Char] -> [CChar]
150 charsToCChars = map castCharToCChar
152 castCCharToChar :: CChar -> Char
153 castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
155 castCharToCChar :: Char -> CChar
156 castCharToCChar ch = fromIntegral (ord ch)