Index: configure.ac ================================================================== --- configure.ac +++ configure.ac @@ -91,10 +91,11 @@ LDFLAGS="$LDFLAGS -Wl,--allow-multiple-definition" LIBS="$LIBS -lversion" AC_SUBST(ALLOW_MULTIPLE_DEFINITION, [-Wl,--allow-multiple-definition]) + AC_SUBST(USE_SRCS_WINDOWS, '${SRCS_WINDOWS}') ;; *-psp-*) AS_IF([test x"$DEVKITPSP" = x""], [ AC_MSG_ERROR( [DEVKITPSP is not set! Please set DEVKITPSP.]) @@ -817,16 +818,10 @@ AC_MSG_RESULT($ac_cv_snprintf_useful_ret) ]) test x"$have_asprintf" != x"yes" -a x"$ac_cv_snprintf_useful_ret" != x"yes" && \ AC_MSG_ERROR(No asprintf and no snprintf returning required space!) -case "$host_os" in -mingw*) - AC_SUBST(OFSTDIOSTREAM_WIN32CONSOLE_M, "OFStdIOStream_Win32Console.m") - ;; -esac - AC_ARG_ENABLE(unicode-tables, AS_HELP_STRING([--disable-unicode-tables], [Disable Unicode tables])) AS_IF([test x"$enable_unicode_tables" != x"no"], [ AC_DEFINE(OF_HAVE_UNICODE_TABLES, 1, [Whether to build with Unicode tables]) Index: extra.mk.in ================================================================== --- extra.mk.in +++ extra.mk.in @@ -80,7 +80,8 @@ USE_INCLUDES_ATOMIC = @USE_INCLUDES_ATOMIC@ USE_SRCS_FILES = @USE_SRCS_FILES@ USE_SRCS_PLUGINS = @USE_SRCS_PLUGINS@ USE_SRCS_SOCKETS = @USE_SRCS_SOCKETS@ USE_SRCS_THREADS = @USE_SRCS_THREADS@ +USE_SRCS_WINDOWS = @USE_SRCS_WINDOWS@ WEAK_NSFOUNDATIONVERSIONNUMBER = @WEAK_NSFOUNDATIONVERSIONNUMBER@ WRAPPER = @WRAPPER@ Index: src/Makefile ================================================================== --- src/Makefile +++ src/Makefile @@ -75,11 +75,10 @@ OFSHA384Hash.m \ OFSHA384Or512Hash.m \ OFSHA512Hash.m \ OFSortedList.m \ OFStdIOStream.m \ - ${OFSTDIOSTREAM_WIN32CONSOLE_M} \ OFStream.m \ OFString.m \ OFString+CryptoHashing.m \ OFString+JSONValue.m \ OFString+PropertyListValue.m \ @@ -118,11 +117,12 @@ scrypt.m \ ${UNICODE_M} \ ${USE_SRCS_FILES} \ ${USE_SRCS_PLUGINS} \ ${USE_SRCS_SOCKETS} \ - ${USE_SRCS_THREADS} + ${USE_SRCS_THREADS} \ + ${USE_SRCS_WINDOWS} SRCS_FILES = OFFile.m \ OFFileManager.m \ OFINICategory.m \ OFINIFile.m \ OFSettings.m \ @@ -138,10 +138,12 @@ SRCS_THREADS = OFCondition.m \ OFMutex.m \ OFRecursiveMutex.m \ OFThreadPool.m \ threading.m +SRCS_WINDOWS = OFStdIOStream_Win32Console.m \ + OFWindowsRegistryKey.m INCLUDES_ATOMIC = atomic.h \ atomic_builtins.h \ atomic_no_threads.h \ atomic_osatomic.h \ ADDED src/OFWindowsRegistryKey.h Index: src/OFWindowsRegistryKey.h ================================================================== --- src/OFWindowsRegistryKey.h +++ src/OFWindowsRegistryKey.h @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFObject.h" +#import "OFString.h" + +#include + +OF_ASSUME_NONNULL_BEGIN + +/*! + * @class OFWindowsRegistryKey \ + * OFWindowsRegistryKey.h ObjFW/OFWindowsRegistryKey.h + */ +@interface OFWindowsRegistryKey: OFObject +{ + HKEY _hKey; + bool _close; +} + +/*! + * @brief Returns the OFWindowsRegistryKey for the HKEY_CLASSES_ROOT key. + * + * @return The OFWindowsRegistryKey for the HKEY_CLASSES_ROOT key + */ ++ (instancetype)classesRootKey; + +/*! + * @brief Returns the OFWindowsRegistryKey for the HKEY_CURRENT_CONFIG key. + * + * @return The OFWindowsRegistryKey for the HKEY_CURRENT_CONFIG key + */ ++ (instancetype)currentConfigKey; + +/*! + * @brief Returns the OFWindowsRegistryKey for the HKEY_CURRENT_USER key. + * + * @return The OFWindowsRegistryKey for the HKEY_CURRENT_USER key + */ ++ (instancetype)currentUserKey; + +/*! + * @brief Returns the OFWindowsRegistryKey for the HKEY_LOCAL_MACHINE key. + * + * @return The OFWindowsRegistryKey for the HKEY_LOCAL_MACHINE key + */ ++ (instancetype)localMachineKey; + +/*! + * @brief Returns the OFWindowsRegistryKey for the HKEY_USERS key. + * + * @return The OFWindowsRegistryKey for the HKEY_USERS key + */ ++ (instancetype)usersKey; + +- (instancetype)init OF_UNAVAILABLE; + +/*! + * @brief Opens the sub key at the specified path. + * + * @param path The path of the sub key to open + * @param options Please refer to the `RegOpenKeyEx()` documentation + * @param securityAndAccessRights Please refer to the `RegOpenKeyEx()` + * documentation + * @return The sub key with the specified path, or nil if it does not exist + */ +- (nullable OFWindowsRegistryKey *) + openSubKeyWithPath: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights; + +/*! + * @brief Creates a sub key at the specified path or opens it if it already + * exists. + * + * @param path The path of the sub key to create + * @param options Please refer to the `RegCreateKeyEx()` documentation + * @param securityAndAccessRights Please refer to the `RegCreateKeyEx()` + * documentation + * @param securityAttributes Please refer to the `RegCreateKeyEx()` + * documentation + * @param disposition Please refer to the `RegCreateKeyEx()` documentation + * @return The sub key with the specified path + */ +- (OFWindowsRegistryKey *) + createSubKeyWithPath: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + securityAttributes: (nullable LPSECURITY_ATTRIBUTES)securityAttributes + disposition: (nullable LPDWORD)disposition; + +/*! + * @brief Returns the string for the specified value at the specified path. + * + * @param value The name of the value to return + * @param subKeyPath The path of the key from which to retrieve the value + * @param flags Extra flags for `RegGetValue()`. Usually 0. + * @return The string for the specified value + */ +- (nullable OFString *)stringForValue: (nullable OFString *)value + subKeyPath: (nullable OFString *)subKeyPath + flags: (DWORD)flags; +@end + +OF_ASSUME_NONNULL_END ADDED src/OFWindowsRegistryKey.m Index: src/OFWindowsRegistryKey.m ================================================================== --- src/OFWindowsRegistryKey.m +++ src/OFWindowsRegistryKey.m @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#include + +#import "OFWindowsRegistryKey.h" + +#import "OFCreateWindowsRegistryKeyFailedException.h" +#import "OFOpenWindowsRegistryKeyFailedException.h" +#import "OFReadWindowsRegistryValueFailedException.h" + +@interface OFWindowsRegistryKey () +- (instancetype)of_initWithHKey: (HKEY)hKey + close: (bool)close; +@end + +@implementation OFWindowsRegistryKey ++ (instancetype)classesRootKey +{ + return [[[self alloc] of_initWithHKey: HKEY_CLASSES_ROOT + close: false] autorelease]; +} + ++ (instancetype)currentConfigKey +{ + return [[[self alloc] of_initWithHKey: HKEY_CURRENT_CONFIG + close: false] autorelease]; +} + ++ (instancetype)currentUserKey +{ + return [[[self alloc] of_initWithHKey: HKEY_CURRENT_USER + close: false] autorelease]; +} + ++ (instancetype)localMachineKey +{ + return [[[self alloc] of_initWithHKey: HKEY_LOCAL_MACHINE + close: false] autorelease]; +} + ++ (instancetype)usersKey +{ + return [[[self alloc] of_initWithHKey: HKEY_USERS + close: false] autorelease]; +} + +- (instancetype)of_initWithHKey: (HKEY)hKey + close: (bool)close +{ + self = [super init]; + + _hKey = hKey; + _close = close; + + return self; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (void)dealloc +{ + if (_close) + RegCloseKey(_hKey); + + [super dealloc]; +} + +- (OFWindowsRegistryKey *)openSubKeyWithPath: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights +{ + void *pool = objc_autoreleasePoolPush(); + LSTATUS status; + HKEY subKey; + + if ((status = RegOpenKeyExW(_hKey, [path UTF16String], options, + securityAndAccessRights, &subKey)) != ERROR_SUCCESS) { + if (status == ERROR_FILE_NOT_FOUND) { + objc_autoreleasePoolPop(pool); + return nil; + } + + @throw [OFOpenWindowsRegistryKeyFailedException + exceptionWithRegistryKey: self + path: path + options: options + securityAndAccessRights: securityAndAccessRights + status: status]; + } + + objc_autoreleasePoolPop(pool); + + return [[[OFWindowsRegistryKey alloc] of_initWithHKey: subKey + close: true] + autorelease]; +} + +- (OFWindowsRegistryKey *) + createSubKeyWithPath: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + securityAttributes: (LPSECURITY_ATTRIBUTES)securityAttributes + disposition: (LPDWORD)disposition +{ + void *pool = objc_autoreleasePoolPush(); + LSTATUS status; + HKEY subKey; + + if ((status = RegCreateKeyExW(_hKey, [path UTF16String], 0, + NULL, options, securityAndAccessRights, securityAttributes, + &subKey, NULL)) != ERROR_SUCCESS) + @throw [OFCreateWindowsRegistryKeyFailedException + exceptionWithRegistryKey: self + path: path + options: options + securityAndAccessRights: securityAndAccessRights + securityAttributes: securityAttributes + status: status]; + + objc_autoreleasePoolPop(pool); + + return [[[OFWindowsRegistryKey alloc] of_initWithHKey: subKey + close: true] + autorelease]; +} + +- (OFString *)stringForValue: (OFString *)value + subKeyPath: (OFString *)subKeyPath + flags: (DWORD)flags +{ + void *pool = objc_autoreleasePoolPush(); + of_char16_t stackBuffer[256], *buffer = stackBuffer; + DWORD length = sizeof(stackBuffer); + LSTATUS status; + OFString *ret; + + if ((status = RegGetValueW(_hKey, [subKeyPath UTF16String], + [value UTF16String], flags | RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ, + NULL, buffer, &length)) != ERROR_SUCCESS) { + OFObject *tmp; + + if (status == ERROR_FILE_NOT_FOUND) { + objc_autoreleasePoolPop(pool); + return nil; + } + + if (status != ERROR_MORE_DATA) + @throw [OFReadWindowsRegistryValueFailedException + exceptionWithRegistryKey: self + value: value + subKeyPath: subKeyPath + flags: flags + status: status]; + + tmp = [[[OFObject alloc] init] autorelease]; + buffer = [tmp allocMemoryWithSize: length]; + + if ((status = RegGetValueW(_hKey, [subKeyPath UTF16String], + [value UTF16String], flags | RRF_RT_REG_SZ | + RRF_RT_REG_EXPAND_SZ, NULL, buffer, &length)) != + ERROR_SUCCESS) + @throw [OFReadWindowsRegistryValueFailedException + exceptionWithRegistryKey: self + value: value + subKeyPath: subKeyPath + flags: flags + status: status]; + } + + /* + * We do not specify a length, as the length returned by RegGetValue() + * sometimes seems to be larger than the string. + */ + ret = [[OFString alloc] initWithUTF16String: buffer]; + + objc_autoreleasePoolPop(pool); + + return [ret autorelease]; +} +@end Index: src/ObjFW.h ================================================================== --- src/ObjFW.h +++ src/ObjFW.h @@ -121,10 +121,14 @@ #import "OFLocalization.h" #import "OFOptionsParser.h" #import "OFTimer.h" #import "OFRunLoop.h" #import "OFSandbox.h" + +#ifdef OF_WINDOWS +# import "OFWindowsRegistryKey.h" +#endif #import "OFAllocFailedException.h" #import "OFException.h" #ifdef OF_HAVE_SOCKETS # import "OFAcceptFailedException.h" @@ -144,10 +148,13 @@ # import "OFConnectionFailedException.h" #endif #import "OFCopyItemFailedException.h" #import "OFCreateDirectoryFailedException.h" #import "OFCreateSymbolicLinkFailedException.h" +#ifdef OF_WINDOWS +# import "OFCreateWindowsRegistryKeyFailedException.h" +#endif #import "OFEnumerationMutationException.h" #ifdef OF_HAVE_FILES # import "OFGetCurrentDirectoryPathFailedException.h" #endif #import "OFGetOptionFailedException.h" @@ -176,14 +183,20 @@ #import "OFNotOpenException.h" #ifdef OF_HAVE_SOCKETS # import "OFObserveFailedException.h" #endif #import "OFOpenItemFailedException.h" +#ifdef OF_WINDOWS +# import "OFOpenWindowsRegistryKeyFailedException.h" +#endif #import "OFOutOfMemoryException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFReadOrWriteFailedException.h" +#ifdef OF_WINDOWS +# import "OFReadWindowsRegistryValueFailedException.h" +#endif #import "OFRemoveItemFailedException.h" #import "OFRetrieveItemAttributesFailedException.h" #import "OFSandboxActivationFailedException.h" #import "OFSeekFailedException.h" #import "OFSetItemAttributesFailedException.h" Index: src/exceptions/Makefile ================================================================== --- src/exceptions/Makefile +++ src/exceptions/Makefile @@ -46,14 +46,16 @@ OFUnlockFailedException.m \ OFUnsupportedProtocolException.m \ OFUnsupportedVersionException.m \ OFWriteFailedException.m \ ${USE_SRCS_FILES} \ + ${USE_SRCS_PLUGINS} \ ${USE_SRCS_SOCKETS} \ ${USE_SRCS_THREADS} \ - ${USE_SRCS_PLUGINS} + ${USE_SRCS_WINDOWS} SRCS_FILES = OFGetCurrentDirectoryPathFailedException.m +SRCS_PLUGINS = OFLoadPluginFailedException.m SRCS_SOCKETS = OFAcceptFailedException.m \ OFAddressTranslationFailedException.m \ OFAlreadyConnectedException.m \ OFBindFailedException.m \ OFConnectionFailedException.m \ @@ -65,12 +67,14 @@ OFConditionStillWaitingException.m \ OFConditionWaitFailedException.m \ OFThreadJoinFailedException.m \ OFThreadStartFailedException.m \ OFThreadStillRunningException.m -SRCS_PLUGINS = OFLoadPluginFailedException.m +SRCS_WINDOWS = OFCreateWindowsRegistryKeyFailedException.m \ + OFOpenWindowsRegistryKeyFailedException.m \ + OFReadWindowsRegistryValueFailedException.m INCLUDES = ${SRCS:.m=.h} include ../../buildsys.mk CPPFLAGS += -I. -I.. -I../.. -I../runtime ADDED src/exceptions/OFCreateWindowsRegistryKeyFailedException.h Index: src/exceptions/OFCreateWindowsRegistryKeyFailedException.h ================================================================== --- src/exceptions/OFCreateWindowsRegistryKeyFailedException.h +++ src/exceptions/OFCreateWindowsRegistryKeyFailedException.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFException.h" +#import "OFWindowsRegistryKey.h" + +#include + +OF_ASSUME_NONNULL_BEGIN + +/*! + * @class OFCreateWindowsRegistryKeyFailedException \ + * OFCreateWindowsRegistryKeyFailedException.h \ + * ObjFW/OFCreateWindowsRegistryKeyFailedException.h + * + * @brief An exception indicating that creating a Windows registry key failed. + */ +@interface OFCreateWindowsRegistryKeyFailedException: OFException +{ + OFWindowsRegistryKey *_registryKey; + OFString *_path; + DWORD _options; + REGSAM _securityAndAccessRights; + LPSECURITY_ATTRIBUTES _Nullable _securityAttributes; + LSTATUS _status; +} + +/*! + * @brief The registry key on which creating the sub key failed. + */ +@property (readonly, nonatomic) OFWindowsRegistryKey *registryKey; + +/*! + * @brief The path for the sub key that could not be created. + */ +@property (readonly, nonatomic) OFString *path; + +/*! + * @brief The options for the sub key that could not be created. + */ +@property (readonly, nonatomic) DWORD options; + +/*! + * @brief The security and access rights for the sub key that could not be + * created. + */ +@property (readonly, nonatomic) REGSAM securityAndAccessRights; + +/*! + * @brief The security options for the sub key that could not be created. + */ +@property OF_NULLABLE_PROPERTY (readonly, nonatomic) + LPSECURITY_ATTRIBUTES securityAttributes; + +/*! + * @brief The status returned by RegCreateKeyEx(). + */ +@property (readonly, nonatomic) LSTATUS status; + +/*! + * @brief Creates a new, autoreleased create Windows registry key failed + * exception. + * + * @param registryKey The registry key on which creating the sub key failed + * @param path The path for the sub key that could not be created + * @param options The options for the sub key that could not be created + * @param securityAndAccessRights The security and access rights for the sub + * key that could not be created + * @param securityAttributes The security options for the sub key that could + * not be created + * @param status The status returned by RegCreateKeyEx() + * @return A new, autoreleased creates Windows registry key failed exception + */ ++ (instancetype) + exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey + path: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + securityAttributes: (nullable LPSECURITY_ATTRIBUTES)securityAttributes + status: (LSTATUS)status; + +- (instancetype)init OF_UNAVAILABLE; + +/*! + * @brief Initializes an already allocated create Windows registry key failed + * exception. + * + * @param registryKey The registry key on which creating the sub key failed + * @param path The path for the sub key that could not be created + * @param options The options for the sub key that could not be created + * @param securityAndAccessRights The security and access rights for the sub + * key that could not be created + * @param securityAttributes The security options for the sub key that could + * not be created + * @param status The status returned by RegCreateKeyEx() + * @return An initialized create Windows registry key failed exception + */ +- (instancetype) + initWithRegistryKey: (OFWindowsRegistryKey *)registryKey + path: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + securityAttributes: (nullable LPSECURITY_ATTRIBUTES)securityAttributes + status: (LSTATUS)status OF_DESIGNATED_INITIALIZER; +@end + +OF_ASSUME_NONNULL_END ADDED src/exceptions/OFCreateWindowsRegistryKeyFailedException.m Index: src/exceptions/OFCreateWindowsRegistryKeyFailedException.m ================================================================== --- src/exceptions/OFCreateWindowsRegistryKeyFailedException.m +++ src/exceptions/OFCreateWindowsRegistryKeyFailedException.m @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFCreateWindowsRegistryKeyFailedException.h" + +@implementation OFCreateWindowsRegistryKeyFailedException +@synthesize registryKey = _registryKey, path = _path, options = _options; +@synthesize securityAndAccessRights = _securityAndAccessRights; +@synthesize securityAttributes = _securityAttributes, status = _status; + ++ (instancetype) + exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey + path: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + securityAttributes: (LPSECURITY_ATTRIBUTES)securityAttributes + status: (LSTATUS)status +{ + return [[[self alloc] initWithRegistryKey: registryKey + path: path + options: options + securityAndAccessRights: securityAndAccessRights + securityAttributes: securityAttributes + status: status] autorelease]; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (instancetype) + initWithRegistryKey: (OFWindowsRegistryKey *)registryKey + path: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + securityAttributes: (LPSECURITY_ATTRIBUTES)securityAttributes + status: (LSTATUS)status +{ + self = [super init]; + + @try { + _registryKey = [registryKey retain]; + _path = [path copy]; + _options = options; + _securityAndAccessRights = securityAndAccessRights; + _securityAttributes = securityAttributes; + _status = status; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [_registryKey release]; + [_path release]; + + [super dealloc]; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: + @"Failed to create sub key at path %@: Status code %u!", + _path, _status]; +} +@end ADDED src/exceptions/OFOpenWindowsRegistryKeyFailedException.h Index: src/exceptions/OFOpenWindowsRegistryKeyFailedException.h ================================================================== --- src/exceptions/OFOpenWindowsRegistryKeyFailedException.h +++ src/exceptions/OFOpenWindowsRegistryKeyFailedException.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFException.h" +#import "OFWindowsRegistryKey.h" + +#include + +OF_ASSUME_NONNULL_BEGIN + +/*! + * @class OFOpenWindowsRegistryKeyFailedException \ + * OFOpenWindowsRegistryKeyFailedException.h \ + * ObjFW/OFOpenWindowsRegistryKeyFailedException.h + * + * @brief An exception indicating that opening a Windows registry key failed. + */ +@interface OFOpenWindowsRegistryKeyFailedException: OFException +{ + OFWindowsRegistryKey *_registryKey; + OFString *_path; + DWORD _options; + REGSAM _securityAndAccessRights; + LPSECURITY_ATTRIBUTES _Nullable _securityAttributes; + LSTATUS _status; +} + +/*! + * @brief The registry key on which opening the sub key failed. + */ +@property (readonly, nonatomic) OFWindowsRegistryKey *registryKey; + +/*! + * @brief The path for the sub key that could not be opened. + */ +@property (readonly, nonatomic) OFString *path; + +/*! + * @brief The options for the sub key that could not be opened. + */ +@property (readonly, nonatomic) DWORD options; + +/*! + * @brief The security and access rights for the sub key that could not be + * opened. + */ +@property (readonly, nonatomic) REGSAM securityAndAccessRights; + +/*! + * @brief The status returned by RegOpenKeyEx(). + */ +@property (readonly, nonatomic) LSTATUS status; + +/*! + * @brief Creates a new, autoreleased open Windows registry key failed + * exception. + * + * @param registryKey The registry key on which opening the sub key failed + * @param path The path for the sub key that could not be opened + * @param options The options for the sub key that could not be opened + * @param securityAndAccessRights The security and access rights for the sub + * key that could not be opened + * @param status The status returned by RegOpenKeyEx() + * @return A new, autoreleased open Windows registry key failed exception + */ ++ (instancetype) + exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey + path: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + status: (LSTATUS)status; + +- (instancetype)init OF_UNAVAILABLE; + +/*! + * @brief Initializes an already allocated open Windows registry key failed + * exception. + * + * @param registryKey The registry key on which opening the sub key failed + * @param path The path for the sub key that could not be opened + * @param options The options for the sub key that could not be opened + * @param securityAndAccessRights The security and access rights for the sub + * key that could not be opened + * @param status The status returned by RegOpenKeyEx() + * @return An initialized open Windows registry key failed exception + */ +- (instancetype) + initWithRegistryKey: (OFWindowsRegistryKey *)registryKey + path: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + status: (LSTATUS)status OF_DESIGNATED_INITIALIZER; +@end + +OF_ASSUME_NONNULL_END ADDED src/exceptions/OFOpenWindowsRegistryKeyFailedException.m Index: src/exceptions/OFOpenWindowsRegistryKeyFailedException.m ================================================================== --- src/exceptions/OFOpenWindowsRegistryKeyFailedException.m +++ src/exceptions/OFOpenWindowsRegistryKeyFailedException.m @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFOpenWindowsRegistryKeyFailedException.h" + +@implementation OFOpenWindowsRegistryKeyFailedException +@synthesize registryKey = _registryKey, path = _path, options = _options; +@synthesize securityAndAccessRights = _securityAndAccessRights; +@synthesize status = _status; + ++ (instancetype) + exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey + path: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + status: (LSTATUS)status +{ + return [[[self alloc] initWithRegistryKey: registryKey + path: path + options: options + securityAndAccessRights: securityAndAccessRights + status: status] autorelease]; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (instancetype) + initWithRegistryKey: (OFWindowsRegistryKey *)registryKey + path: (OFString *)path + options: (DWORD)options + securityAndAccessRights: (REGSAM)securityAndAccessRights + status: (LSTATUS)status +{ + self = [super init]; + + @try { + _registryKey = [registryKey retain]; + _path = [path copy]; + _options = options; + _securityAndAccessRights = securityAndAccessRights; + _status = status; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [_registryKey release]; + [_path release]; + + [super dealloc]; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: + @"Failed to open sub key at path %@: Status code %u!", + _path, _status]; +} +@end ADDED src/exceptions/OFReadWindowsRegistryValueFailedException.h Index: src/exceptions/OFReadWindowsRegistryValueFailedException.h ================================================================== --- src/exceptions/OFReadWindowsRegistryValueFailedException.h +++ src/exceptions/OFReadWindowsRegistryValueFailedException.h @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#import "OFException.h" +#import "OFWindowsRegistryKey.h" + +#include + +OF_ASSUME_NONNULL_BEGIN + +/*! + * @class OFReadWindowsRegistryValueFailedException \ + * OFReadWindowsRegistryValueFailedException.h \ + * ObjFW/OFReadWindowsRegistryValueFailedException.h + * + * @brief An exception indicating that reading a Windows registry value failed. + */ +@interface OFReadWindowsRegistryValueFailedException: OFException +{ + OFWindowsRegistryKey *_registryKey; + OFString *_Nullable _value, *_Nullable _subKeyPath; + DWORD _flags; + LSTATUS _status; +} + +/*! + * @brief The registry key on which reading the value at the sub key path + * failed. + */ +@property (readonly, nonatomic) OFWindowsRegistryKey *registryKey; + +/*! + * @brief The value for which reading failed. + */ +@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *value; + +/*! + * @brief The sub key path at which reading the value failed. + */ +@property OF_NULLABLE_PROPERTY (readonly, nonatomic) OFString *subKeyPath; + +/*! + * @brief The flags with which reading the value failed. + */ +@property (readonly, nonatomic) DWORD flags; + +/*! + * @brief The status returned by RegGetValueEx(). + */ +@property (readonly, nonatomic) LSTATUS status; + +/*! + * @brief Creates a new, autoreleased read Windows registry value failed + * exception. + * + * @param registryKey The registry key on which reading the value at the sub + * key path failed + * @param value The value for which reading failed + * @param subKeyPath The sub key path at which reading the value failed + * @param flags The flags with which reading the value failed + * @param status The status returned by RegGetValueEx() + * @return A new, autoreleased read Windows registry value failed exception + */ ++ (instancetype)exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey + value: (nullable OFString *)value + subKeyPath: (nullable OFString *)subKeyPath + flags: (DWORD)flags + status: (LSTATUS)status; + +- (instancetype)init OF_UNAVAILABLE; + +/*! + * @brief Initializes an already allocated read Windows registry value failed + * exception. + * + * @param registryKey The registry key on which reading the value at the sub + * key path failed + * @param value The value for which reading failed + * @param subKeyPath The sub key path at which reading the value failed + * @param flags The flags with which reading the value failed + * @param status The status returned by RegGetValueEx() + * @return An initialized read Windows registry value failed exception + */ +- (instancetype)initWithRegistryKey: (OFWindowsRegistryKey *)registryKey + value: (nullable OFString *)value + subKeyPath: (nullable OFString *)subKeyPath + flags: (DWORD)flags + status: (LSTATUS)status OF_DESIGNATED_INITIALIZER; +@end + +OF_ASSUME_NONNULL_END ADDED src/exceptions/OFReadWindowsRegistryValueFailedException.m Index: src/exceptions/OFReadWindowsRegistryValueFailedException.m ================================================================== --- src/exceptions/OFReadWindowsRegistryValueFailedException.m +++ src/exceptions/OFReadWindowsRegistryValueFailedException.m @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, + * 2018 + * Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "OFReadWindowsRegistryValueFailedException.h" + +@implementation OFReadWindowsRegistryValueFailedException +@synthesize registryKey = _registryKey, value = _value; +@synthesize subKeyPath = _subKeyPath, flags = _flags, status = _status; + ++ (instancetype)exceptionWithRegistryKey: (OFWindowsRegistryKey *)registryKey + value: (nullable OFString *)value + subKeyPath: (nullable OFString *)subKeyPath + flags: (DWORD)flags + status: (LSTATUS)status +{ + return [[[self alloc] initWithRegistryKey: registryKey + value: value + subKeyPath: subKeyPath + flags: flags + status: status] autorelease]; +} + +- (instancetype)init +{ + OF_INVALID_INIT_METHOD +} + +- (instancetype)initWithRegistryKey: (OFWindowsRegistryKey *)registryKey + value: (nullable OFString *)value + subKeyPath: (nullable OFString *)subKeyPath + flags: (DWORD)flags + status: (LSTATUS)status +{ + self = [super init]; + + @try { + _registryKey = [registryKey retain]; + _value = [value copy]; + _subKeyPath = [subKeyPath copy]; + _flags = flags; + _status = status; + } @catch (id e) { + [self release]; + @throw e; + } + + return self; +} + +- (void)dealloc +{ + [_registryKey release]; + [_value release]; + [_subKeyPath release]; + + [super dealloc]; +} + +- (OFString *)description +{ + return [OFString stringWithFormat: + @"Failed to read value %@ at sub key path %@: Status code %u!", + _value, _subKeyPath, _status]; +} +@end