From e9af5a22f78a81b8a255a2766705a361ce4e215e Mon Sep 17 00:00:00 2001 From: Matthias Kilian Date: Mon, 29 Jun 2009 18:36:34 +0000 Subject: [PATCH] Add a wrapper for libiconv. --- GHC/IO/Encoding/Iconv.hs | 18 +++++++++--------- base.cabal | 1 + cbits/iconv.c | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 cbits/iconv.c diff --git a/GHC/IO/Encoding/Iconv.hs b/GHC/IO/Encoding/Iconv.hs index 25e22ab..a935a25 100644 --- a/GHC/IO/Encoding/Iconv.hs +++ b/GHC/IO/Encoding/Iconv.hs @@ -114,14 +114,14 @@ localeEncoding = unsafePerformIO $ do -- value -1, which is a possible return value from iconv_open. type IConv = CLong -- ToDo: (#type iconv_t) -foreign import ccall unsafe "iconv_open" - iconv_open :: CString -> CString -> IO IConv +foreign import ccall unsafe "hs_iconv_open" + hs_iconv_open :: CString -> CString -> IO IConv -foreign import ccall unsafe "iconv_close" - iconv_close :: IConv -> IO CInt +foreign import ccall unsafe "hs_iconv_close" + hs_iconv_close :: IConv -> IO CInt -foreign import ccall unsafe "iconv" - iconv :: IConv -> Ptr CString -> Ptr CSize -> Ptr CString -> Ptr CSize +foreign import ccall unsafe "hs_iconv" + hs_iconv :: IConv -> Ptr CString -> Ptr CSize -> Ptr CString -> Ptr CSize -> IO CSize foreign import ccall unsafe "localeEncoding" @@ -152,8 +152,8 @@ newIConv :: String -> String newIConv from to fn = withCString from $ \ from_str -> withCString to $ \ to_str -> do - iconvt <- throwErrnoIfMinus1 "mkTextEncoding" $ iconv_open to_str from_str - let iclose = do throwErrnoIfMinus1 "Iconv.close" $ iconv_close iconvt + iconvt <- throwErrnoIfMinus1 "mkTextEncoding" $ hs_iconv_open to_str from_str + let iclose = do throwErrnoIfMinus1 "Iconv.close" $ hs_iconv_close iconvt return () return BufferCodec{ encode = fn iconvt, @@ -185,7 +185,7 @@ iconvRecode iconv_t with (poraw `plusPtr` (ow `shiftL` oscale)) $ \ p_outbuf -> do with (fromIntegral ((iw-ir) `shiftL` iscale)) $ \ p_inleft -> do with (fromIntegral ((os-ow) `shiftL` oscale)) $ \ p_outleft -> do - res <- iconv iconv_t p_inbuf p_inleft p_outbuf p_outleft + res <- hs_iconv iconv_t p_inbuf p_inleft p_outbuf p_outleft new_inleft <- peek p_inleft new_outleft <- peek p_outleft let diff --git a/base.cabal b/base.cabal index 032010d..68b6911 100644 --- a/base.cabal +++ b/base.cabal @@ -187,6 +187,7 @@ Library { cbits/WCsubst.c cbits/Win32Utils.c cbits/consUtils.c + cbits/iconv.c cbits/inputReady.c cbits/selectUtils.c include-dirs: include diff --git a/cbits/iconv.c b/cbits/iconv.c new file mode 100644 index 0000000..c7e6ea1 --- /dev/null +++ b/cbits/iconv.c @@ -0,0 +1,23 @@ +#ifndef __MINGW32__ + +#include +#include + +iconv_t hs_iconv_open(const char* tocode, + const char* fromcode) +{ + return iconv_open(tocode, fromcode); +} + +size_t hs_iconv(iconv_t cd, + const char* * inbuf, size_t * inbytesleft, + char* * outbuf, size_t * outbytesleft) +{ + return iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft); +} + +int hs_iconv_close(iconv_t cd) { + return iconv_close(cd); +} + +#endif -- 1.7.10.4