~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Source/NSError.m

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** Implementation for NSError for GNUStep
 
2
   Copyright (C) 2004 Free Software Foundation, Inc.
 
3
 
 
4
   Written by:  Richard Frith-Macdonald <rfm@gnu.org>
 
5
   Date: May 2004
 
6
 
 
7
   This file is part of the GNUstep Base Library.
 
8
 
 
9
   This library is free software; you can redistribute it and/or
 
10
   modify it under the terms of the GNU Library General Public
 
11
   License as published by the Free Software Foundation; either
 
12
   version 2 of the License, or (at your option) any later version.
 
13
 
 
14
   This library is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
   Library General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU Library General Public
 
20
   License along with this library; if not, write to the Free
 
21
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
 
22
   */
 
23
 
 
24
#include        <Foundation/NSDictionary.h>
 
25
#include        <Foundation/NSString.h>
 
26
#include        <Foundation/NSError.h>
 
27
#include        <Foundation/NSCoder.h>
 
28
 
 
29
NSString* const NSLocalizedDescriptionKey = @"NSLocalizedDescriptionKey";
 
30
NSString* const NSUnderlyingErrorKey = @"NSUnderlyingErrorKey";
 
31
NSString* const NSMACHErrorDomain = @"NSMACHErrorDomain";
 
32
NSString* const NSOSStatusErrorDomain = @"NSOSStatusErrorDomain";
 
33
NSString* const NSPOSIXErrorDomain = @"NSPOSIXErrorDomain";
 
34
 
 
35
@implementation NSError
 
36
 
 
37
+ (id) errorWithDomain: (NSString*)aDomain
 
38
                  code: (int)aCode
 
39
              userInfo: (NSDictionary*)aDictionary
 
40
{
 
41
  NSError       *e = [self allocWithZone: NSDefaultMallocZone()];
 
42
 
 
43
  e = [e initWithDomain: aDomain code: aCode userInfo: aDictionary];
 
44
  return AUTORELEASE(e);
 
45
}
 
46
 
 
47
- (int) code
 
48
{
 
49
  return _code;
 
50
}
 
51
 
 
52
- (id) copyWithZone: (NSZone*)z
 
53
{
 
54
  NSError       *e = [[self class] allocWithZone: z];
 
55
 
 
56
  e = [e initWithDomain: _domain code: _code userInfo: _userInfo];
 
57
  return e;
 
58
}
 
59
 
 
60
- (void) dealloc
 
61
{
 
62
  DESTROY(_domain);
 
63
  DESTROY(_userInfo);
 
64
  [super dealloc];
 
65
}
 
66
 
 
67
- (NSString*) domain
 
68
{
 
69
  return _domain;
 
70
}
 
71
 
 
72
- (void) encodeWithCoder: (NSCoder*)aCoder
 
73
{
 
74
  if ([aCoder allowsKeyedCoding])
 
75
    {
 
76
      [aCoder encodeInt: _code forKey: @"NSCode"];
 
77
      [aCoder encodeObject: _domain forKey: @"NSDomain"];
 
78
      [aCoder encodeObject: _userInfo forKey: @"NSUserInfo"];
 
79
    }
 
80
  else
 
81
    {
 
82
      [aCoder encodeValueOfObjCType: @encode(int) at: &_code];
 
83
      [aCoder encodeValueOfObjCType: @encode(id) at: &_domain];
 
84
      [aCoder encodeValueOfObjCType: @encode(id) at: &_userInfo];
 
85
    }
 
86
}
 
87
 
 
88
- (id) init
 
89
{
 
90
  return [self initWithDomain: nil code: 0 userInfo: nil];
 
91
}
 
92
 
 
93
- (id) initWithCoder: (NSCoder*)aCoder
 
94
{
 
95
  if ([aCoder allowsKeyedCoding])
 
96
    {
 
97
      int       c;
 
98
      id        d;
 
99
      id        u;
 
100
 
 
101
      c = [aCoder decodeIntForKey: @"NSCode"];
 
102
      d = [aCoder decodeObjectForKey: @"NSDomain"];
 
103
      u = [aCoder decodeObjectForKey: @"NSUserInfo"];
 
104
      self = [self initWithDomain: d code: c userInfo: u];
 
105
    }
 
106
  else
 
107
    {
 
108
      [aCoder decodeValueOfObjCType: @encode(int) at: &_code];
 
109
      [aCoder decodeValueOfObjCType: @encode(id) at: &_domain];
 
110
      [aCoder decodeValueOfObjCType: @encode(id) at: &_userInfo];
 
111
    }
 
112
  return self;
 
113
}
 
114
 
 
115
- (id) initWithDomain: (NSString*)aDomain
 
116
                 code: (int)aCode
 
117
             userInfo: (NSDictionary*)aDictionary
 
118
{
 
119
  if (aDomain == nil)
 
120
    {
 
121
      NSLog(@"[%@-%@] with nil domain",
 
122
        NSStringFromClass([self class]), NSStringFromSelector(_cmd));
 
123
      DESTROY(self);
 
124
    }
 
125
  else if ((self = [super init]) != nil)
 
126
    {
 
127
      ASSIGN(_domain, aDomain);
 
128
      _code = aCode;
 
129
      ASSIGN(_userInfo, aDictionary);
 
130
    }
 
131
  return self;
 
132
}
 
133
 
 
134
- (NSString *)localizedDescription
 
135
{
 
136
  NSString      *desc = [_userInfo objectForKey: NSLocalizedDescriptionKey];
 
137
 
 
138
  if (desc == nil)
 
139
    {
 
140
      desc = [NSString stringWithFormat: @"%@ %d", _domain, _code];
 
141
    }
 
142
  return desc;
 
143
}
 
144
 
 
145
- (NSDictionary*) userInfo
 
146
{
 
147
  return _userInfo;
 
148
}
 
149
@end
 
150