From c721d79cbd2e11ef12da7c85147bd70848feae6d Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Tue, 19 Jul 2016 04:07:38 +0300 Subject: refactoring --- src/Windows/Environment.hs | 20 +++++++++++++++----- src/Windows/Registry.hs | 3 ++- 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/Windows/Environment.hs b/src/Windows/Environment.hs index 2bea481..5c26eea 100644 --- a/src/Windows/Environment.hs +++ b/src/Windows/Environment.hs @@ -52,7 +52,10 @@ openRootProfileKey :: Profile -> Registry.KeyHandle openRootProfileKey = Registry.openRootKey . profileRootKey openProfileKey :: Profile -> IO Registry.KeyHandle -openProfileKey profile = Registry.openSubKey (openRootProfileKey profile) (profileSubKeyPath profile) +openProfileKey profile = Registry.openSubKey rootKey subKeyPath + where + rootKey = openRootProfileKey profile + subKeyPath = profileSubKeyPath profile type VarName = Registry.ValueName type VarValue = Registry.ValueData @@ -60,9 +63,14 @@ type VarValue = Registry.ValueData query :: Profile -> VarName -> IO (Maybe VarValue) query profile name = do keyHandle <- openProfileKey profile - catchIOError (Registry.getString keyHandle name >>= return . Just) emptyIfDoesNotExist + catchIOError (tryQuery keyHandle) ignoreMissing where - emptyIfDoesNotExist e = if isDoesNotExistError e then return Nothing else ioError e + tryQuery keyHandle = do + value <- Registry.getString keyHandle name + return $ Just value + ignoreMissing e + | isDoesNotExistError e = return Nothing + | otherwise = ioError e engrave :: Profile -> VarName -> VarValue -> IO () engrave profile name value = do @@ -73,10 +81,12 @@ engrave profile name value = do wipe :: Profile -> VarName -> IO () wipe profile name = do keyHandle <- openProfileKey profile - catchIOError (Registry.delValue keyHandle name) ignoreIfDoesNotExist + catchIOError (Registry.delValue keyHandle name) ignoreMissing notifyEnvironmentUpdate where - ignoreIfDoesNotExist e = if isDoesNotExistError e then return () else ioError e + ignoreMissing e + | isDoesNotExistError e = return () + | otherwise = ioError e pathSep :: VarValue pathSep = ";" diff --git a/src/Windows/Registry.hs b/src/Windows/Registry.hs index 528027f..a8504d6 100644 --- a/src/Windows/Registry.hs +++ b/src/Windows/Registry.hs @@ -31,7 +31,8 @@ import Foreign.ForeignPtr (withForeignPtr) import Foreign.Marshal.Alloc (alloca, allocaBytes) import Foreign.Ptr (castPtr, plusPtr) import Foreign.Storable (peek, poke, sizeOf) -import System.IO.Error (catchIOError, doesNotExistErrorType, mkIOError, isDoesNotExistError) +import System.IO.Error + (catchIOError, doesNotExistErrorType, mkIOError, isDoesNotExistError) import qualified System.Win32.Registry as WinAPI import qualified System.Win32.Types as WinAPI -- cgit v1.2.3