From b1b6d0f8590dceea588495f316ef1eac9f4c1078 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 30 Jan 2011 16:01:31 -0800 Subject: UTF8 module: Use base 4.2 IO if available. This gives us proper line endings on windows, and some speed improvements. We fall back to the old functions if base < 4.2. hGetContents is now exported. --- src/Text/Pandoc/UTF8.hs | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/UTF8.hs b/src/Text/Pandoc/UTF8.hs index eba79c734..6d9ac1d1a 100644 --- a/src/Text/Pandoc/UTF8.hs +++ b/src/Text/Pandoc/UTF8.hs @@ -25,7 +25,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Stability : alpha Portability : portable -UTF-8 aware string IO functions that will work with GHC 6.10 or 6.12. +UTF-8 aware string IO functions that will work with GHC 6.10, 6.12, or 7. -} module Text.Pandoc.UTF8 ( readFile , writeFile @@ -34,9 +34,46 @@ module Text.Pandoc.UTF8 ( readFile , putStrLn , hPutStr , hPutStrLn + , hGetContents ) where + +#ifdef MIN_VERSION_base(4,2,0) + +import System.IO hiding (readFile, writeFile, getContents, + putStr, putStrLn, hPutStr, hPutStrLn, hGetContents) +import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn ) +import qualified System.IO as IO + +readFile :: FilePath -> IO String +readFile f = do + h <- openFile f ReadMode + hGetContents h + +writeFile :: FilePath -> String -> IO () +writeFile f s = withFile f WriteMode $ \h -> hPutStr h s + +getContents :: IO String +getContents = hGetContents stdin + +putStr :: String -> IO () +putStr s = hPutStr stdout s + +putStrLn :: String -> IO () +putStrLn s = hPutStrLn stdout s + +hPutStr :: Handle -> String -> IO () +hPutStr h s = hSetEncoding h utf8 >> IO.hPutStr h s + +hPutStrLn :: Handle -> String -> IO () +hPutStrLn h s = hSetEncoding h utf8 >> IO.hPutStrLn h s + +hGetContents :: Handle -> IO String +hGetContents h = hSetEncoding h utf8_bom >> IO.hGetContents h + +#else + import qualified Data.ByteString as B import Codec.Binary.UTF8.String (encodeString) import Data.ByteString.UTF8 (toString, fromString) @@ -44,6 +81,7 @@ import Prelude hiding (readFile, writeFile, getContents, putStr, putStrLn) import System.IO (Handle) import Control.Monad (liftM) + bom :: B.ByteString bom = B.pack [0xEF, 0xBB, 0xBF] @@ -60,6 +98,9 @@ writeFile f = B.writeFile (encodeString f) . fromString getContents :: IO String getContents = liftM (toString . stripBOM) B.getContents +hGetContents :: Handle -> IO String +hGetContents h = liftM (toString . stripBOM) (B.hGetContents h) + putStr :: String -> IO () putStr = B.putStr . fromString @@ -71,3 +112,5 @@ hPutStr h = B.hPutStr h . fromString hPutStrLn :: Handle -> String -> IO () hPutStrLn h s = hPutStr h (s ++ "\n") + +#endif -- cgit v1.2.3