diff --git a/hledger-lib/Hledger/Read/JournalReader.hs b/hledger-lib/Hledger/Read/JournalReader.hs index edd68f028..e21018f8d 100644 --- a/hledger-lib/Hledger/Read/JournalReader.hs +++ b/hledger-lib/Hledger/Read/JournalReader.hs @@ -63,7 +63,6 @@ import System.Time (getClockTime) import Hledger.Data import Hledger.Utils import Prelude hiding (readFile) -import Hledger.Utils.UTF8IOCompat (readFile) -- standard reader exports @@ -196,7 +195,7 @@ includedirective = do Right (ju,_) -> combineJournalUpdates [return $ journalAddFile (filepath,txt), ju] `catchError` (throwError . (inIncluded ++)) Left err -> throwError $ inIncluded ++ show err where readFileOrError pos fp = - ErrorT $ liftM Right (readFile fp) `C.catch` + ErrorT $ liftM Right (readFile' fp) `C.catch` \e -> return $ Left $ printf "%s reading %s:\n%s" (show pos) fp (show (e::C.IOException)) journalAddFile :: (FilePath,String) -> Journal -> Journal diff --git a/hledger-lib/Hledger/Utils.hs b/hledger-lib/Hledger/Utils.hs index dc777116e..81ab28b24 100644 --- a/hledger-lib/Hledger/Utils.hs +++ b/hledger-lib/Hledger/Utils.hs @@ -40,6 +40,7 @@ import Data.Tree import Debug.Trace import System.Directory (getHomeDirectory) import System.FilePath((), isRelative) +import System.IO import Test.HUnit import Text.ParserCombinators.Parsec import Text.Printf @@ -490,3 +491,10 @@ expandPath curdir p = (if isRelative p then (curdir ) else id) `liftM` expand firstJust ms = case dropWhile (==Nothing) ms of [] -> Nothing (md:_) -> md + +-- | Read a file in universal newline mode, handling whatever newline convention it may contain. +readFile' :: FilePath -> IO String +readFile' name = do + h <- openFile name ReadMode + hSetNewlineMode h universalNewlineMode + hGetContents h diff --git a/hledger/Hledger/Cli/Utils.hs b/hledger/Hledger/Cli/Utils.hs index 10ed758c3..686845607 100644 --- a/hledger/Hledger/Cli/Utils.hs +++ b/hledger/Hledger/Cli/Utils.hs @@ -141,7 +141,7 @@ openBrowserOn u = trybrowsers browsers u -- indicating whether we did anything. writeFileWithBackupIfChanged :: FilePath -> String -> IO Bool writeFileWithBackupIfChanged f t = do - s <- readFile f + s <- readFile' f if t == s then return False else backUpFile f >> writeFile f t >> return True @@ -151,7 +151,7 @@ writeFileWithBackup :: FilePath -> String -> IO () writeFileWithBackup f t = backUpFile f >> writeFile f t readFileStrictly :: FilePath -> IO String -readFileStrictly f = readFile f >>= \s -> C.evaluate (length s) >> return s +readFileStrictly f = readFile' f >>= \s -> C.evaluate (length s) >> return s -- | Back up this file with a (incrementing) numbered suffix, or give an error. backUpFile :: FilePath -> IO ()