summaryrefslogtreecommitdiff
path: root/src/compiler/GF
diff options
context:
space:
mode:
authorhallgren <hallgren@chalmers.se>2012-10-05 12:54:49 +0000
committerhallgren <hallgren@chalmers.se>2012-10-05 12:54:49 +0000
commitb5bf276e9c82505038f4269a77ba3c6e201438bb (patch)
tree49631e5553bd676b8ab4fd07fa8f4cebd3de0dee /src/compiler/GF
parent2d371b768122695ce0bd37f10e3d6b0381c31a57 (diff)
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.
Diffstat (limited to 'src/compiler/GF')
-rw-r--r--src/compiler/GF/System/Console.hs42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/compiler/GF/System/Console.hs b/src/compiler/GF/System/Console.hs
new file mode 100644
index 000000000..ea901d55d
--- /dev/null
+++ b/src/compiler/GF/System/Console.hs
@@ -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