module Agda.Utils.IO.UTF8
( readTextFile
, Agda.Utils.IO.UTF8.hPutStr
, Agda.Utils.IO.UTF8.writeFile
) where
import qualified System.IO as IO
import Control.Applicative
convertLineEndings :: String -> String
convertLineEndings ('\x000D' : '\x000A' : s) = '\n' : convertLineEndings s
convertLineEndings ('\x000A' : s) = '\n' : convertLineEndings s
convertLineEndings ('\x000D' : s) = '\n' : convertLineEndings s
convertLineEndings ('\x000C' : s) = '\n' : convertLineEndings s
convertLineEndings ('\x0085' : s) = '\n' : convertLineEndings s
convertLineEndings ('\x2028' : s) = '\n' : convertLineEndings s
convertLineEndings ('\x2029' : s) = '\n' : convertLineEndings s
convertLineEndings (c : s) = c : convertLineEndings s
convertLineEndings "" = ""
readTextFile :: FilePath -> IO String
readTextFile file = convertLineEndings <$> do
h <- IO.openFile file IO.ReadMode
IO.hSetNewlineMode h IO.noNewlineTranslation
IO.hSetEncoding h IO.utf8
IO.hGetContents h
hPutStr :: IO.Handle -> String -> IO ()
hPutStr h s = do
IO.hSetEncoding h IO.utf8
IO.hPutStr h s
writeFile :: FilePath -> String -> IO ()
writeFile file s = IO.withFile file IO.WriteMode $ \h -> do
hPutStr h s