Factor out code for setting the console encoding

Moved similar low-level code blocks in Main and GFI for setting the console
encoding to the new module GF.System.Console.
This commit is contained in:
hallgren
2012-10-05 12:54:49 +00:00
parent 2d371b7681
commit b5bf276e9c
4 changed files with 48 additions and 35 deletions

View File

@@ -0,0 +1,42 @@
{-# LANGUAGE CPP #-}
module GF.System.Console(setConsoleEncoding,changeConsoleEncoding) where
import System.IO
#ifdef mingw32_HOST_OS
import System.Win32.Console
import System.Win32.NLS
#endif
setConsoleEncoding =
#ifdef mingw32_HOST_OS
do codepage <- getACP
setCP codepage
setEncoding ("CP"++show codepage)
#endif
return () :: IO ()
changeConsoleEncoding code =
do
#ifdef mingw32_HOST_OS
maybe (return ()) setCP (readCP code)
#endif
setEncoding code
setEncoding code =
do enc <- mkTextEncoding code
hSetEncoding stdin enc
hSetEncoding stdout enc
hSetEncoding stderr enc
#ifdef mingw32_HOST_OS
setCP codepage =
do setConsoleCP codepage
setConsoleOutputCP codepage
readCP code =
case code of
'C':'P':c -> case reads c of
[(cp,"")] -> Just cp
_ -> Nothing
"UTF-8" -> Just 65001
_ -> Nothing
#endif