{-# LANGUAGE CPP #-}
module System.Win32.Encoding
( getCurrentCodePage
, encodeMultiByte
, encodeMultiByteIO
, decodeMultiByte
, decodeMultiByteIO
, wideCharToMultiByte
, multiByteToWideChar
) where
import Foreign.C.Types (CInt(..))
import Foreign.C.String (peekCAStringLen, withCWStringLen)
import Foreign.Marshal.Array (allocaArray)
import Foreign.Marshal.Unsafe (unsafeLocalState)
import System.Win32.Console
import System.Win32.NLS
import System.Win32.Types
#include "windows_cconv.h"
getCurrentCodePage :: IO DWORD
getCurrentCodePage :: IO DWORD
getCurrentCodePage = do
DWORD
conCP <- IO DWORD
getConsoleCP
if DWORD
conCP forall a. Ord a => a -> a -> Bool
> DWORD
0
then forall (m :: * -> *) a. Monad m => a -> m a
return DWORD
conCP
else IO DWORD
getACP
encodeMultiByte :: CodePage -> String -> String
encodeMultiByte :: DWORD -> String -> String
encodeMultiByte DWORD
cp = forall a. IO a -> a
unsafeLocalState forall b c a. (b -> c) -> (a -> b) -> a -> c
. DWORD -> String -> IO String
encodeMultiByteIO DWORD
cp
encodeMultiByteIO :: CodePage -> String -> IO String
encodeMultiByteIO :: DWORD -> String -> IO String
encodeMultiByteIO DWORD
_ String
"" = forall (m :: * -> *) a. Monad m => a -> m a
return String
""
encodeMultiByteIO DWORD
cp String
wstr =
forall a. String -> (CWStringLen -> IO a) -> IO a
withCWStringLen String
wstr forall a b. (a -> b) -> a -> b
$ \(Ptr CWchar
cwstr,Int
len) -> do
CInt
mbchars' <- forall a. (Eq a, Num a) => String -> IO a -> IO a
failIfZero String
"WideCharToMultiByte" forall a b. (a -> b) -> a -> b
$ DWORD
-> DWORD
-> Ptr CWchar
-> CInt
-> LPSTR
-> CInt
-> LPSTR
-> LPBOOL
-> IO CInt
wideCharToMultiByte
DWORD
cp
DWORD
0
Ptr CWchar
cwstr
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
forall a. Ptr a
nullPtr CInt
0
forall a. Ptr a
nullPtr forall a. Ptr a
nullPtr
forall a b. Storable a => Int -> (Ptr a -> IO b) -> IO b
allocaArray (forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
mbchars') forall a b. (a -> b) -> a -> b
$ \LPSTR
mbstr -> do
CInt
mbchars <- forall a. (Eq a, Num a) => String -> IO a -> IO a
failIfZero String
"WideCharToMultiByte" forall a b. (a -> b) -> a -> b
$ DWORD
-> DWORD
-> Ptr CWchar
-> CInt
-> LPSTR
-> CInt
-> LPSTR
-> LPBOOL
-> IO CInt
wideCharToMultiByte
DWORD
cp
DWORD
0
Ptr CWchar
cwstr
(forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
LPSTR
mbstr CInt
mbchars'
forall a. Ptr a
nullPtr forall a. Ptr a
nullPtr
CStringLen -> IO String
peekCAStringLen (LPSTR
mbstr,forall a b. (Integral a, Num b) => a -> b
fromIntegral CInt
mbchars)
foreign import WINDOWS_CCONV "WideCharToMultiByte"
wideCharToMultiByte
:: CodePage
-> DWORD
-> LPCWSTR
-> CInt
-> LPSTR
-> CInt
-> LPCSTR
-> LPBOOL
-> IO CInt
decodeMultiByte :: CodePage -> String -> String
decodeMultiByte :: DWORD -> String -> String
decodeMultiByte DWORD
cp = forall a. IO a -> a
unsafeLocalState forall b c a. (b -> c) -> (a -> b) -> a -> c
. DWORD -> String -> IO String
decodeMultiByteIO DWORD
cp
decodeMultiByteIO :: CodePage -> String -> IO String
decodeMultiByteIO :: DWORD -> String -> IO String
decodeMultiByteIO = DWORD -> String -> IO String
stringToUnicode
{-# INLINE decodeMultiByteIO #-}