2016-11-23 01:28:39 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
2012-03-30 02:10:43 +04:00
|
|
|
{- |
|
2012-03-29 23:06:31 +04:00
|
|
|
|
|
|
|
UTF-8 aware string IO functions that will work across multiple platforms
|
|
|
|
and GHC versions. Includes code from Text.Pandoc.UTF8 ((C) 2010 John
|
2012-03-30 02:10:43 +04:00
|
|
|
MacFarlane).
|
2012-03-29 23:06:31 +04:00
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
|
|
|
import UTF8IOCompat (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
|
|
|
import UTF8IOCompat (SystemString,fromSystemString,toSystemString,error',userError')
|
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
2013/4/10 update: we now trust that current GHC versions & platforms
|
|
|
|
do the right thing, so this file is a no-op and on its way to being removed.
|
|
|
|
Not carefully tested.
|
|
|
|
|
2019-10-20 01:21:42 +03:00
|
|
|
2019/10/20 update: all packages have base>=4.9 which corresponds to GHC v8.0.1
|
|
|
|
and higher. Tear this file apart!
|
|
|
|
|
2012-03-29 23:06:31 +04:00
|
|
|
-}
|
2019-07-15 13:28:52 +03:00
|
|
|
-- TODO obsolete ?
|
2012-03-29 23:06:31 +04:00
|
|
|
|
|
|
|
module Hledger.Utils.UTF8IOCompat (
|
|
|
|
readFile,
|
|
|
|
writeFile,
|
|
|
|
appendFile,
|
|
|
|
getContents,
|
|
|
|
hGetContents,
|
|
|
|
putStr,
|
|
|
|
putStrLn,
|
|
|
|
hPutStr,
|
|
|
|
hPutStrLn,
|
|
|
|
--
|
|
|
|
error',
|
|
|
|
userError',
|
2017-03-29 18:00:30 +03:00
|
|
|
usageError,
|
2012-03-29 23:06:31 +04:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2013-04-13 03:28:29 +04:00
|
|
|
-- import Control.Monad (liftM)
|
|
|
|
-- import qualified Data.ByteString.Lazy as B
|
|
|
|
-- import qualified Data.ByteString.Lazy.Char8 as B8
|
|
|
|
-- import qualified Data.ByteString.Lazy.UTF8 as U8 (toString, fromString)
|
2012-03-29 23:06:31 +04:00
|
|
|
import Prelude hiding (readFile, writeFile, appendFile, getContents, putStr, putStrLn)
|
2013-04-13 03:18:20 +04:00
|
|
|
import System.IO -- (Handle)
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- bom :: B.ByteString
|
|
|
|
-- bom = B.pack [0xEF, 0xBB, 0xBF]
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- stripBOM :: B.ByteString -> B.ByteString
|
|
|
|
-- stripBOM s | bom `B.isPrefixOf` s = B.drop 3 s
|
|
|
|
-- stripBOM s = s
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- readFile :: FilePath -> IO String
|
|
|
|
-- readFile = liftM (U8.toString . stripBOM) . B.readFile
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- writeFile :: FilePath -> String -> IO ()
|
|
|
|
-- writeFile f = B.writeFile f . U8.fromString
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- appendFile :: FilePath -> String -> IO ()
|
|
|
|
-- appendFile f = B.appendFile f . U8.fromString
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- getContents :: IO String
|
|
|
|
-- getContents = liftM (U8.toString . stripBOM) B.getContents
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- hGetContents :: Handle -> IO String
|
|
|
|
-- hGetContents h = liftM (U8.toString . stripBOM) (B.hGetContents h)
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- putStr :: String -> IO ()
|
|
|
|
-- putStr = bs_putStr . U8.fromString
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- putStrLn :: String -> IO ()
|
|
|
|
-- putStrLn = bs_putStrLn . U8.fromString
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- hPutStr :: Handle -> String -> IO ()
|
|
|
|
-- hPutStr h = bs_hPutStr h . U8.fromString
|
2012-03-29 23:06:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- hPutStrLn :: Handle -> String -> IO ()
|
|
|
|
-- hPutStrLn h = bs_hPutStrLn h . U8.fromString
|
2012-03-30 00:40:31 +04:00
|
|
|
|
2013-04-13 03:18:20 +04:00
|
|
|
-- -- span GHC versions including 6.12.3 - 7.4.1:
|
|
|
|
-- bs_putStr = B8.putStr
|
|
|
|
-- bs_putStrLn = B8.putStrLn
|
|
|
|
-- bs_hPutStr = B8.hPut
|
|
|
|
-- bs_hPutStrLn h bs = B8.hPut h bs >> B8.hPut h (B.singleton 0x0a)
|
2012-03-30 00:40:31 +04:00
|
|
|
|
2012-03-29 23:06:31 +04:00
|
|
|
-- | A SystemString-aware version of error.
|
|
|
|
error' :: String -> a
|
2016-11-23 01:28:39 +03:00
|
|
|
error' =
|
|
|
|
#if __GLASGOW_HASKELL__ < 800
|
|
|
|
-- (easier than if base < 4.9)
|
2019-10-20 01:21:42 +03:00
|
|
|
error
|
2016-11-23 01:28:39 +03:00
|
|
|
#else
|
2019-10-20 01:21:42 +03:00
|
|
|
errorWithoutStackTrace
|
2016-11-23 01:28:39 +03:00
|
|
|
#endif
|
2012-03-29 23:06:31 +04:00
|
|
|
|
|
|
|
-- | A SystemString-aware version of userError.
|
|
|
|
userError' :: String -> IOError
|
2019-10-20 01:21:42 +03:00
|
|
|
userError' = userError
|
2017-03-29 18:00:30 +03:00
|
|
|
|
|
|
|
-- | A SystemString-aware version of error that adds a usage hint.
|
|
|
|
usageError :: String -> a
|
2019-07-15 13:28:52 +03:00
|
|
|
usageError = error' . (++ " (use -h to see usage)")
|
2017-03-29 18:00:30 +03:00
|
|
|
|