module Hledger.Data.UTF8 ( readFile
, writeFile
, appendFile
, getContents
, hGetContents
, putStr
, putStrLn
, hPutStr
, hPutStrLn
)
where
import qualified Data.ByteString.Lazy as B
import Data.ByteString.Lazy.UTF8 (toString, fromString)
import Prelude hiding (readFile, writeFile, appendFile, getContents, putStr, putStrLn)
import System.IO (Handle)
import Control.Monad (liftM)
bom :: B.ByteString
bom = B.pack [0xEF, 0xBB, 0xBF]
stripBOM :: B.ByteString -> B.ByteString
stripBOM s | bom `B.isPrefixOf` s = B.drop 3 s
stripBOM s = s
readFile :: FilePath -> IO String
readFile = liftM (toString . stripBOM) . B.readFile
writeFile :: FilePath -> String -> IO ()
writeFile f = B.writeFile f . fromString
appendFile :: FilePath -> String -> IO ()
appendFile f = B.appendFile 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
putStrLn :: String -> IO ()
putStrLn = B.putStrLn . fromString
hPutStr :: Handle -> String -> IO ()
hPutStr h = B.hPutStr h . fromString
hPutStrLn :: Handle -> String -> IO ()
hPutStrLn h s = hPutStr h (s ++ "\n")