Export Unicode and newline functionality from System.IO; update Haddock docs
[ghc-base.git] / GHC / IO / Encoding.hs
1 {-# OPTIONS_GHC -fno-implicit-prelude -funbox-strict-fields #-}
2 -----------------------------------------------------------------------------
3 -- |
4 -- Module      :  GHC.IO.Encoding
5 -- Copyright   :  (c) The University of Glasgow, 2008-2009
6 -- License     :  see libraries/base/LICENSE
7 -- 
8 -- Maintainer  :  libraries@haskell.org
9 -- Stability   :  internal
10 -- Portability :  non-portable
11 --
12 -- Text codecs for I/O
13 --
14 -----------------------------------------------------------------------------
15
16 module GHC.IO.Encoding (
17   BufferCodec(..), TextEncoding(..), TextEncoder, TextDecoder,
18   latin1, latin1_encode, latin1_decode,
19   utf8, 
20   utf16, utf16le, utf16be,
21   utf32, utf32le, utf32be, 
22   localeEncoding,
23   mkTextEncoding,
24   ) where
25
26 import GHC.Base
27 --import GHC.IO
28 import GHC.IO.Buffer
29 import GHC.IO.Encoding.Types
30 import GHC.Word
31 #if !defined(mingw32_HOST_OS)
32 import qualified GHC.IO.Encoding.Iconv  as Iconv
33 #endif
34 import qualified GHC.IO.Encoding.Latin1 as Latin1
35 import qualified GHC.IO.Encoding.UTF8   as UTF8
36 import qualified GHC.IO.Encoding.UTF16  as UTF16
37 import qualified GHC.IO.Encoding.UTF32  as UTF32
38
39 #if defined(mingw32_HOST_OS)
40 import Data.Maybe
41 import GHC.IO.Exception
42 #endif
43
44 -- -----------------------------------------------------------------------------
45
46 -- | The Latin1 (ISO8859-1) encoding.  This encoding maps bytes
47 -- directly to the first 256 Unicode code points, and is thus not a
48 -- complete Unicode encoding.  An attempt to write a character greater than
49 -- '\255' to a 'Handle' using the 'latin1' encoding will result in an error.
50 latin1  :: TextEncoding
51 latin1 = Latin1.latin1_checked
52
53 -- | The UTF-8 Unicode encoding
54 utf8  :: TextEncoding
55 utf8 = UTF8.utf8
56
57 -- | The UTF-16 Unicode encoding (a byte-order-mark should be used to
58 -- indicate endianness).
59 utf16  :: TextEncoding
60 utf16 = UTF16.utf16
61
62 -- | The UTF-16 Unicode encoding (litte-endian)
63 utf16le  :: TextEncoding
64 utf16le = UTF16.utf16le
65
66 -- | The UTF-16 Unicode encoding (big-endian)
67 utf16be  :: TextEncoding
68 utf16be = UTF16.utf16be
69
70 -- | The UTF-32 Unicode encoding (a byte-order-mark should be used to
71 -- indicate endianness).
72 utf32  :: TextEncoding
73 utf32 = UTF32.utf32
74
75 -- | The UTF-32 Unicode encoding (litte-endian)
76 utf32le  :: TextEncoding
77 utf32le = UTF32.utf32le
78
79 -- | The UTF-32 Unicode encoding (big-endian)
80 utf32be  :: TextEncoding
81 utf32be = UTF32.utf32be
82
83 -- | The Unicode encoding of the current locale
84 localeEncoding  :: TextEncoding
85 #if !defined(mingw32_HOST_OS)
86 localeEncoding = Iconv.localeEncoding
87 #else
88 localeEncoding = Latin1.latin1
89 #endif
90
91 -- | Look up the named Unicode encoding.  May fail with 
92 --
93 --  * 'isDoesNotExistError' if the encoding is unknown
94 --
95 -- The set of known encodings is system-dependent.
96 --
97 mkTextEncoding :: String -> IO TextEncoding
98 #if !defined(mingw32_HOST_OS)
99 mkTextEncoding = Iconv.mkTextEncoding
100 #else
101 mkTextEncoding "UTF-8"    = return utf8
102 mkTextEncoding "UTF-16"   = return utf16
103 mkTextEncoding "UTF-16LE" = return utf16le
104 mkTextEncoding "UTF-16BE" = return utf16be
105 mkTextEncoding "UTF-32"   = return utf32
106 mkTextEncoding "UTF-32LE" = return utf32le
107 mkTextEncoding "UTF-32BE" = return utf32be
108 mkTextEncoding e = ioException
109      (IOError Nothing NoSuchThing "mkTextEncoding"
110           ("unknown encoding:" ++ e)  Nothing Nothing)
111 #endif
112
113 latin1_encode :: CharBuffer -> Buffer Word8 -> IO (CharBuffer, Buffer Word8)
114 latin1_encode = Latin1.latin1_encode -- unchecked, used for binary
115 --latin1_encode = unsafePerformIO $ do mkTextEncoder Iconv.latin1 >>= return.encode
116
117 latin1_decode :: Buffer Word8 -> CharBuffer -> IO (Buffer Word8, CharBuffer)
118 latin1_decode = Latin1.latin1_decode
119 --latin1_decode = unsafePerformIO $ do mkTextDecoder Iconv.latin1 >>= return.encode