{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, NoImplicitPrelude #-}
module Foreign.C.String (
CString,
CStringLen,
peekCString,
peekCStringLen,
newCString,
newCStringLen,
withCString,
withCStringLen,
charIsRepresentable,
castCharToCChar,
castCCharToChar,
castCharToCUChar,
castCUCharToChar,
castCharToCSChar,
castCSCharToChar,
peekCAString,
peekCAStringLen,
newCAString,
newCAStringLen,
withCAString,
withCAStringLen,
CWString,
CWStringLen,
peekCWString,
peekCWStringLen,
newCWString,
newCWStringLen,
withCWString,
withCWStringLen,
) where
import Foreign.Marshal.Array
import Foreign.C.Types
import Foreign.Ptr
import Foreign.Storable
import Data.Word
import Control.Monad
import GHC.Char
import GHC.List
import GHC.Real
import GHC.Num
import GHC.Base
import {-# SOURCE #-} GHC.IO.Encoding
import qualified GHC.Foreign as GHC
type CString = Ptr CChar
type CStringLen = (Ptr CChar, Int)
peekCString :: CString -> IO String
peekCString s = getForeignEncoding >>= flip GHC.peekCString s
peekCStringLen :: CStringLen -> IO String
peekCStringLen s = getForeignEncoding >>= flip GHC.peekCStringLen s
newCString :: String -> IO CString
newCString s = getForeignEncoding >>= flip GHC.newCString s
newCStringLen :: String -> IO CStringLen
newCStringLen s = getForeignEncoding >>= flip GHC.newCStringLen s
withCString :: String -> (CString -> IO a) -> IO a
withCString s f = getForeignEncoding >>= \enc -> GHC.withCString enc s f
withCStringLen :: String -> (CStringLen -> IO a) -> IO a
withCStringLen s f = getForeignEncoding >>= \enc -> GHC.withCStringLen enc s f
charIsRepresentable :: Char -> IO Bool
charIsRepresentable c = getForeignEncoding >>= flip GHC.charIsRepresentable c
castCCharToChar :: CChar -> Char
castCCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
castCharToCChar :: Char -> CChar
castCharToCChar ch = fromIntegral (ord ch)
castCUCharToChar :: CUChar -> Char
castCUCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
castCharToCUChar :: Char -> CUChar
castCharToCUChar ch = fromIntegral (ord ch)
castCSCharToChar :: CSChar -> Char
castCSCharToChar ch = unsafeChr (fromIntegral (fromIntegral ch :: Word8))
castCharToCSChar :: Char -> CSChar
castCharToCSChar ch = fromIntegral (ord ch)
peekCAString :: CString -> IO String
peekCAString cp = do
l <- lengthArray0 nUL cp
if l <= 0 then return "" else loop "" (l-1)
where
loop s i = do
xval <- peekElemOff cp i
let val = castCCharToChar xval
val `seq` if i <= 0 then return (val:s) else loop (val:s) (i-1)
peekCAStringLen :: CStringLen -> IO String
peekCAStringLen (cp, len)
| len <= 0 = return ""
| otherwise = loop [] (len-1)
where
loop acc i = do
xval <- peekElemOff cp i
let val = castCCharToChar xval
if (val `seq` (i == 0))
then return (val:acc)
else loop (val:acc) (i-1)
newCAString :: String -> IO CString
newCAString str = do
ptr <- mallocArray0 (length str)
let
go [] n = pokeElemOff ptr n nUL
go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
go str 0
return ptr
newCAStringLen :: String -> IO CStringLen
newCAStringLen str = do
ptr <- mallocArray0 len
let
go [] n = n `seq` return ()
go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
go str 0
return (ptr, len)
where
len = length str
withCAString :: String -> (CString -> IO a) -> IO a
withCAString str f =
allocaArray0 (length str) $ \ptr ->
let
go [] n = pokeElemOff ptr n nUL
go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
in do
go str 0
f ptr
withCAStringLen :: String -> (CStringLen -> IO a) -> IO a
withCAStringLen str f =
allocaArray len $ \ptr ->
let
go [] n = n `seq` return ()
go (c:cs) n = do pokeElemOff ptr n (castCharToCChar c); go cs (n+1)
in do
go str 0
f (ptr,len)
where
len = length str
nUL :: CChar
nUL = 0
newArrayLen :: Storable a => [a] -> IO (Ptr a, Int)
newArrayLen xs = do
a <- newArray xs
return (a, length xs)
type CWString = Ptr CWchar
type CWStringLen = (Ptr CWchar, Int)
peekCWString :: CWString -> IO String
peekCWString cp = do
cs <- peekArray0 wNUL cp
return (cWcharsToChars cs)
peekCWStringLen :: CWStringLen -> IO String
peekCWStringLen (cp, len) = do
cs <- peekArray len cp
return (cWcharsToChars cs)
newCWString :: String -> IO CWString
newCWString = newArray0 wNUL . charsToCWchars
newCWStringLen :: String -> IO CWStringLen
newCWStringLen str = newArrayLen (charsToCWchars str)
withCWString :: String -> (CWString -> IO a) -> IO a
withCWString = withArray0 wNUL . charsToCWchars
withCWStringLen :: String -> (CWStringLen -> IO a) -> IO a
withCWStringLen str f =
withArrayLen (charsToCWchars str) $ \ len ptr -> f (ptr, len)
wNUL :: CWchar
wNUL = 0
cWcharsToChars :: [CWchar] -> [Char]
charsToCWchars :: [Char] -> [CWchar]
#ifdef mingw32_HOST_OS
cWcharsToChars = map chr . fromUTF16 . map fromIntegral
where
fromUTF16 (c1:c2:wcs)
| 0xd800 <= c1 && c1 <= 0xdbff && 0xdc00 <= c2 && c2 <= 0xdfff =
((c1 - 0xd800)*0x400 + (c2 - 0xdc00) + 0x10000) : fromUTF16 wcs
fromUTF16 (c:wcs) = c : fromUTF16 wcs
fromUTF16 [] = []
charsToCWchars = foldr utf16Char [] . map ord
where
utf16Char c wcs
| c < 0x10000 = fromIntegral c : wcs
| otherwise = let c' = c - 0x10000 in
fromIntegral (c' `div` 0x400 + 0xd800) :
fromIntegral (c' `mod` 0x400 + 0xdc00) : wcs
#else /* !mingw32_HOST_OS */
cWcharsToChars xs = map castCWcharToChar xs
charsToCWchars xs = map castCharToCWchar xs
castCWcharToChar :: CWchar -> Char
castCWcharToChar ch = chr (fromIntegral ch )
castCharToCWchar :: Char -> CWchar
castCharToCWchar ch = fromIntegral (ord ch)
#endif /* !mingw32_HOST_OS */