summaryrefslogtreecommitdiff
path: root/src/compiler/GF/System/Console.hs
blob: 975b229f1f142319baafc8eace6d6d0a3d4d1ddd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# LANGUAGE CPP #-}
module GF.System.Console(
    -- ** Changing which character encoding to use for console IO
    setConsoleEncoding,changeConsoleEncoding) where
import System.IO
#ifdef mingw32_HOST_OS
import System.Win32.Console
import System.Win32.NLS
#endif

-- | Set the console encoding (for Windows, has no effect on Unix-like systems)
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