From cc7fc400920d7f99bdff6f955c15d7c72b868c86 Mon Sep 17 00:00:00 2001 From: CaS Date: Mon, 17 May 2004 09:24:29 +0000 Subject: [PATCH] Added NSError git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@19360 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 8 ++ Headers/Foundation/Foundation.h | 1 + Headers/Foundation/NSError.h | 117 +++++++++++++++++++++++++ Source/DocMakefile | 1 + Source/GNUmakefile | 2 + Source/NSError.m | 150 ++++++++++++++++++++++++++++++++ 6 files changed, 279 insertions(+) create mode 100644 Headers/Foundation/NSError.h create mode 100644 Source/NSError.m diff --git a/ChangeLog b/ChangeLog index d2255b620..fe5b1ffb4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2004-05-17 Richard Frith-Macdonald + + * Headers/Foundation/Foundation.h: Add NSError.h + * Headers/Foundation/NSError.h: New MacOS-X compatibility header + * Source/DocMakefile: Add NSError + * Source/GNUmakefile: Add NSError + * Source/NSError.m: new MacOS-X compatibility class. + 2004-05-16 Richard Frith-Macdonald * Source/Additions/GSXML.m: Corrected value for cdata in sax-like diff --git a/Headers/Foundation/Foundation.h b/Headers/Foundation/Foundation.h index 0e9f28634..b1f16c80e 100644 --- a/Headers/Foundation/Foundation.h +++ b/Headers/Foundation/Foundation.h @@ -51,6 +51,7 @@ #include #include #include +#include #include #include #include diff --git a/Headers/Foundation/NSError.h b/Headers/Foundation/NSError.h new file mode 100644 index 000000000..f26a2bef0 --- /dev/null +++ b/Headers/Foundation/NSError.h @@ -0,0 +1,117 @@ +/** Interface for NSError for GNUStep + Copyright (C) 2004 Free Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: May 2004 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. + + AutogsdocSource: NSError.m + */ + +#ifndef __NSError_h_GNUSTEP_BASE_INCLUDE +#define __NSError_h_GNUSTEP_BASE_INCLUDE + +#ifndef STRICT_OPENSTEP + +#include + +@class NSDictionary, NSString; + +/** + * Key for user info dictionary component which describes the error in + * a human readable format. + */ +GS_EXPORT NSString* const NSLocalizedDescriptionKey; + +/** + * Where one error has caused another, the underlying error can be stored + * in the user info dictionary uisng this key. + */ +GS_EXPORT NSString* const NSUnderlyingErrorKey; + +/** + * Domain for system errors (on MACH). + */ +GS_EXPORT NSString* const NSMACHErrorDomain; +/** + * Domain for system errors. + */ +GS_EXPORT NSString* const NSOSStatusErrorDomain; +/** + * Domain for system and system library errors. + */ +GS_EXPORT NSString* const NSPOSIXErrorDomain; + +/** + * Error information class + */ +@interface NSError : NSObject +{ +@private + int _code; + NSString *_domain; + NSDictionary *_userInfo; +} + +/** + * Creates and returns an autoreleased NSError instance by calling + * -initWithDomain:code:userInfo: + */ ++ (id) errorWithDomain: (NSString*)aDomain + code: (int)aCode + userInfo: (NSDictionary*)aDictionary; + +/** + * Return the error code ... which is not globally unique, just unique for + * a particular domain. + */ +- (int) code; + +/** + * Return the domain for this instance. + */ +- (NSString*) domain; + +/** + * Initialises the receiver using the supplied domain, code, and info.
+ * The domain musat be non-nil. + */ +- (id) initWithDomain: (NSString*)aDomain + code: (int)aCode + userInfo: (NSDictionary*)aDictionary; + +/** + * Return a human readable description for the error.
+ * The default implementation uses the value from the user info dictionary + * if it is available, otherwise it generates a generic one from domain + * and code. + */ +- (NSString *)localizedDescription; + +/** + * Return the user info for this instance (or nil if none is set)
+ * The NSLocalizedDescriptionKey should locate a human readable description + * in the dictionary.
+ * The NSUnderlyingErrorKey key should locate an NSError instance if an + * error is available describing any underlying problem.
+ */ +- (NSDictionary*) userInfo; +@end + +#endif /* STRICT_OPENSTEP */ +#endif /* __NSError_h_GNUSTEP_BASE_INCLUDE*/ diff --git a/Source/DocMakefile b/Source/DocMakefile index 94a0f15b0..932561b39 100644 --- a/Source/DocMakefile +++ b/Source/DocMakefile @@ -55,6 +55,7 @@ NSDistantObject.h \ NSDistributedLock.h \ NSDistributedNotificationCenter.h \ NSEnumerator.h \ +NSError.h \ NSException.h \ NSFileHandle.h \ NSFileManager.h \ diff --git a/Source/GNUmakefile b/Source/GNUmakefile index 6c0a3be8d..d608cd543 100644 --- a/Source/GNUmakefile +++ b/Source/GNUmakefile @@ -166,6 +166,7 @@ NSDistantObject.m \ NSDistributedLock.m \ NSDistributedNotificationCenter.m \ NSEnumerator.m \ +NSError.m \ NSException.m \ NSFileHandle.m \ NSFileManager.m \ @@ -283,6 +284,7 @@ NSDistantObject.h \ NSDistributedLock.h \ NSDistributedNotificationCenter.h \ NSEnumerator.h \ +NSError.h \ NSException.h \ NSFileHandle.h \ NSFileManager.h \ diff --git a/Source/NSError.m b/Source/NSError.m new file mode 100644 index 000000000..a838dcd90 --- /dev/null +++ b/Source/NSError.m @@ -0,0 +1,150 @@ +/** Implementation for NSError for GNUStep + Copyright (C) 2004 Free Software Foundation, Inc. + + Written by: Richard Frith-Macdonald + Date: May 2004 + + This file is part of the GNUstep Base Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. + */ + +#include +#include +#include +#include + +NSString* const NSLocalizedDescriptionKey = @"NSLocalizedDescriptionKey"; +NSString* const NSUnderlyingErrorKey = @"NSUnderlyingErrorKey"; +NSString* const NSMACHErrorDomain = @"NSMACHErrorDomain"; +NSString* const NSOSStatusErrorDomain = @"NSOSStatusErrorDomain"; +NSString* const NSPOSIXErrorDomain = @"NSPOSIXErrorDomain"; + +@implementation NSError + ++ (id) errorWithDomain: (NSString*)aDomain + code: (int)aCode + userInfo: (NSDictionary*)aDictionary +{ + NSError *e = [self allocWithZone: NSDefaultMallocZone()]; + + e = [e initWithDomain: aDomain code: aCode userInfo: aDictionary]; + return AUTORELEASE(e); +} + +- (int) code +{ + return _code; +} + +- (id) copyWithZone: (NSZone*)z +{ + NSError *e = [[self class] allocWithZone: z]; + + e = [e initWithDomain: _domain code: _code userInfo: _userInfo]; + return e; +} + +- (void) dealloc +{ + DESTROY(_domain); + DESTROY(_userInfo); + [super dealloc]; +} + +- (NSString*) domain +{ + return _domain; +} + +- (void) encodeWithCoder: (NSCoder*)aCoder +{ + if ([aCoder allowsKeyedCoding]) + { + [aCoder encodeInt: _code forKey: @"NSCode"]; + [aCoder encodeObject: _domain forKey: @"NSDomain"]; + [aCoder encodeObject: _userInfo forKey: @"NSUserInfo"]; + } + else + { + [aCoder encodeValueOfObjCType: @encode(int) at: &_code]; + [aCoder encodeValueOfObjCType: @encode(id) at: &_domain]; + [aCoder encodeValueOfObjCType: @encode(id) at: &_userInfo]; + } +} + +- (id) init +{ + return [self initWithDomain: nil code: 0 userInfo: nil]; +} + +- (id) initWithCoder: (NSCoder*)aCoder +{ + if ([aCoder allowsKeyedCoding]) + { + int c; + id d; + id u; + + c = [aCoder decodeIntForKey: @"NSCode"]; + d = [aCoder decodeObjectForKey: @"NSDomain"]; + u = [aCoder decodeObjectForKey: @"NSUserInfo"]; + self = [self initWithDomain: d code: c userInfo: u]; + } + else + { + [aCoder decodeValueOfObjCType: @encode(int) at: &_code]; + [aCoder decodeValueOfObjCType: @encode(id) at: &_domain]; + [aCoder decodeValueOfObjCType: @encode(id) at: &_userInfo]; + } + return self; +} + +- (id) initWithDomain: (NSString*)aDomain + code: (int)aCode + userInfo: (NSDictionary*)aDictionary +{ + if (aDomain == nil) + { + NSLog(@"[%@-%@] with nil domain", + NSStringFromClass([self class]), NSStringFromSelector(_cmd)); + DESTROY(self); + } + else if ((self = [super init]) != nil) + { + ASSIGN(_domain, aDomain); + _code = aCode; + ASSIGN(_userInfo, aDictionary); + } + return self; +} + +- (NSString *)localizedDescription +{ + NSString *desc = [_userInfo objectForKey: NSLocalizedDescriptionKey]; + + if (desc == nil) + { + desc = [NSString stringWithFormat: @"%@ %d", _domain, _code]; + } + return desc; +} + +- (NSDictionary*) userInfo +{ + return _userInfo; +} +@end +