diff options
author | Joey Hess <joey@kitenet.net> | 2013-11-20 13:41:13 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2013-11-20 13:41:13 -0400 |
commit | 22265ce41a4bb0ada9070a0ae358a9f321f437be (patch) | |
tree | 131d2d24a58be5dde5638ce69c2a87a81151f6cf /Utility | |
parent | 8dbffd39ffc30879bd1d5068114c18e341a8e8ae (diff) |
add readFileStrictAnyEncoding
Diffstat (limited to 'Utility')
-rw-r--r-- | Utility/Misc.hs | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/Utility/Misc.hs b/Utility/Misc.hs index 4b0e9a1e4..68199c828 100644 --- a/Utility/Misc.hs +++ b/Utility/Misc.hs @@ -21,6 +21,9 @@ import System.Posix.Process (getAnyProcessStatus) import Utility.Exception #endif +import Utility.FileSystemEncoding +import Utility.Monad + {- A version of hgetContents that is not lazy. Ensures file is - all read before it gets closed. -} hGetContentsStrict :: Handle -> IO String @@ -30,6 +33,13 @@ hGetContentsStrict = hGetContents >=> \s -> length s `seq` return s readFileStrict :: FilePath -> IO String readFileStrict = readFile >=> \s -> length s `seq` return s +{- Reads a file strictly, and using the FileSystemEncofing, so it will + - never crash on a badly encoded file. -} +readFileStrictAnyEncoding :: FilePath -> IO String +readFileStrictAnyEncoding f = withFile f ReadMode $ \h -> do + fileEncoding h + hClose h `after` hGetContentsStrict h + {- Like break, but the item matching the condition is not included - in the second result list. - |