parsing: read files in universal newline mode

Line endings other than the unix style, are now accepted in journal and rules files.
This commit is contained in:
Simon Michael 2013-03-29 18:46:10 +00:00
parent 7b6a59123d
commit af5c0e80e0
3 changed files with 11 additions and 4 deletions

View File

@ -63,7 +63,6 @@ import System.Time (getClockTime)
import Hledger.Data import Hledger.Data
import Hledger.Utils import Hledger.Utils
import Prelude hiding (readFile) import Prelude hiding (readFile)
import Hledger.Utils.UTF8IOCompat (readFile)
-- standard reader exports -- standard reader exports
@ -196,7 +195,7 @@ includedirective = do
Right (ju,_) -> combineJournalUpdates [return $ journalAddFile (filepath,txt), ju] `catchError` (throwError . (inIncluded ++)) Right (ju,_) -> combineJournalUpdates [return $ journalAddFile (filepath,txt), ju] `catchError` (throwError . (inIncluded ++))
Left err -> throwError $ inIncluded ++ show err Left err -> throwError $ inIncluded ++ show err
where readFileOrError pos fp = 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)) \e -> return $ Left $ printf "%s reading %s:\n%s" (show pos) fp (show (e::C.IOException))
journalAddFile :: (FilePath,String) -> Journal -> Journal journalAddFile :: (FilePath,String) -> Journal -> Journal

View File

@ -40,6 +40,7 @@ import Data.Tree
import Debug.Trace import Debug.Trace
import System.Directory (getHomeDirectory) import System.Directory (getHomeDirectory)
import System.FilePath((</>), isRelative) import System.FilePath((</>), isRelative)
import System.IO
import Test.HUnit import Test.HUnit
import Text.ParserCombinators.Parsec import Text.ParserCombinators.Parsec
import Text.Printf 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 firstJust ms = case dropWhile (==Nothing) ms of
[] -> Nothing [] -> Nothing
(md:_) -> md (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

View File

@ -141,7 +141,7 @@ openBrowserOn u = trybrowsers browsers u
-- indicating whether we did anything. -- indicating whether we did anything.
writeFileWithBackupIfChanged :: FilePath -> String -> IO Bool writeFileWithBackupIfChanged :: FilePath -> String -> IO Bool
writeFileWithBackupIfChanged f t = do writeFileWithBackupIfChanged f t = do
s <- readFile f s <- readFile' f
if t == s then return False if t == s then return False
else backUpFile f >> writeFile f t >> return True else backUpFile f >> writeFile f t >> return True
@ -151,7 +151,7 @@ writeFileWithBackup :: FilePath -> String -> IO ()
writeFileWithBackup f t = backUpFile f >> writeFile f t writeFileWithBackup f t = backUpFile f >> writeFile f t
readFileStrictly :: FilePath -> IO String 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. -- | Back up this file with a (incrementing) numbered suffix, or give an error.
backUpFile :: FilePath -> IO () backUpFile :: FilePath -> IO ()