1 {- 2 From pandoc, slightly extended. Example usage: 3 4 import Prelude hiding (readFile,writeFile,getContents,putStr,putStrLn) 5 import Hledger.Utils.UTF8 (readFile,writeFile,getContents,putStr,putStrLn) 6 7 8 ---------------------------------------------------------------------- 9 Copyright (C) 2010 John MacFarlane <jgm@berkeley.edu> 10 11 This program is free software; you can redistribute it and/or modify 12 it under the terms of the GNU General Public License as published by 13 the Free Software Foundation; either version 2 of the License, or 14 (at your option) any later version. 15 16 This program is distributed in the hope that it will be useful, 17 but WITHOUT ANY WARRANTY; without even the implied warranty of 18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 GNU General Public License for more details. 20 21 You should have received a copy of the GNU General Public License 22 along with this program; if not, write to the Free Software 23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24 -} 25 26 {- | 27 Module : Text.Pandoc.UTF8 28 Copyright : Copyright (C) 2010 John MacFarlane 29 License : GNU GPL, version 2 or above 30 31 Maintainer : John MacFarlane <jgm@berkeley.edu> 32 Stability : alpha 33 Portability : portable 34 35 UTF-8 aware string IO functions that will work with GHC 6.10 or 6.12. 36 -} 37 module Hledger.Utils.UTF8 ( readFile 38 , writeFile 39 , appendFile 40 , getContents 41 , hGetContents 42 , putStr 43 , putStrLn 44 , hPutStr 45 , hPutStrLn 46 ) 47 48 where 49 import qualified Data.ByteString.Lazy as B 50 import Data.ByteString.Lazy.UTF8 (toString, fromString) 51 import Prelude hiding (readFile, writeFile, appendFile, getContents, putStr, putStrLn) 52 import System.IO (Handle) 53 import Control.Monad (liftM) 54 55 bom :: B.ByteString 56 bom = B.pack [0xEF, 0xBB, 0xBF] 57 58 stripBOM :: B.ByteString -> B.ByteString 59 stripBOM s | bom `B.isPrefixOf` s = B.drop 3 s 60 stripBOM s = s 61 62 readFile :: FilePath -> IO String 63 readFile = liftM (toString . stripBOM) . B.readFile 64 65 writeFile :: FilePath -> String -> IO () 66 writeFile f = B.writeFile f . fromString 67 68 appendFile :: FilePath -> String -> IO () 69 appendFile f = B.appendFile f . fromString 70 71 getContents :: IO String 72 getContents = liftM (toString . stripBOM) B.getContents 73 74 hGetContents :: Handle -> IO String 75 hGetContents h = liftM (toString . stripBOM) (B.hGetContents h) 76 77 putStr :: String -> IO () 78 putStr = B.putStr . fromString 79 80 putStrLn :: String -> IO () 81 putStrLn = B.putStrLn . fromString 82 83 hPutStr :: Handle -> String -> IO () 84 hPutStr h = B.hPutStr h . fromString 85 86 hPutStrLn :: Handle -> String -> IO () 87 hPutStrLn h s = hPutStr h (s ++ "\n")