diff options
| -rw-r--r-- | src/Trivmix.hs | 13 | 
1 files changed, 10 insertions, 3 deletions
| diff --git a/src/Trivmix.hs b/src/Trivmix.hs index a754e76..c1fbe8a 100644 --- a/src/Trivmix.hs +++ b/src/Trivmix.hs | |||
| @@ -26,6 +26,8 @@ import System.IO.Error | |||
| 26 | 26 | ||
| 27 | import System.INotify | 27 | import System.INotify | 
| 28 | 28 | ||
| 29 | import Data.Char | ||
| 30 | |||
| 29 | data Options = Options | 31 | data Options = Options | 
| 30 | { input :: String | 32 | { input :: String | 
| 31 | , output :: String | 33 | , output :: String | 
| @@ -107,7 +109,7 @@ onLevelFile file initial action = do | |||
| 107 | exists <- doesFileExist file | 109 | exists <- doesFileExist file | 
| 108 | let acquire = case exists of | 110 | let acquire = case exists of | 
| 109 | True -> return () | 111 | True -> return () | 
| 110 | False -> createFile file mode >>= closeFd >> writeFile file (show initial) | 112 | False -> createFile file mode >>= closeFd >> writeFile file (show initial ++ "\n") | 
| 111 | mode = foldl unionFileModes nullFileMode [ ownerReadMode | 113 | mode = foldl unionFileModes nullFileMode [ ownerReadMode | 
| 112 | , ownerWriteMode | 114 | , ownerWriteMode | 
| 113 | , groupReadMode | 115 | , groupReadMode | 
| @@ -121,7 +123,12 @@ onLevelFile file initial action = do | |||
| 121 | handleLevel :: MVar Float -> FilePath -> IO () | 123 | handleLevel :: MVar Float -> FilePath -> IO () | 
| 122 | handleLevel level file = catch action handler | 124 | handleLevel level file = catch action handler | 
| 123 | where | 125 | where | 
| 124 | action = readFile file >>= readIO >>= swapMVar level >>= const (return ()) | 126 | action = readFile file >>= readIO . stripSpace >>= swapMVar level >>= const (return ()) | 
| 125 | handler e = if isUserError e | 127 | handler e = if isUserError e | 
| 126 | then readMVar level >>= \l -> writeFile file (show l) | 128 | then readMVar level >>= \l -> writeFile file (show l ++ "\n") | 
| 127 | else throw e | 129 | else throw e | 
| 130 | stripSpace = reverse . stripSpace' . reverse . stripSpace' | ||
| 131 | stripSpace' [] = [] | ||
| 132 | stripSpace' l@(x:xs) = if isSpace x | ||
| 133 | then stripSpace' xs | ||
| 134 | else l | ||
