]> err.no Git - sope/blob - sope-core/NGStreams/NGStreamExceptions.m
synced with latest additions and bumped framework versions
[sope] / sope-core / NGStreams / NGStreamExceptions.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #include "common.h"
23 #include "NGStreamExceptions.h"
24
25 @interface NSObject(setLastException)
26 - (void)setLastException:(NSException *)_exception;
27 @end
28
29 @implementation NGIOException
30
31 - (id)init {
32   self = [super initWithName:NSStringFromClass([self class])
33                 reason:@"an IO exception occured"
34                 userInfo:nil];
35   return self;
36 }
37 - (id)initWithReason:(NSString *)_reason {
38   self = [super initWithName:NSStringFromClass([self class])
39                 reason:_reason
40                 userInfo:nil];
41   return self;
42 }
43
44 + (void)raiseWithReason:(NSString *)_reason {
45   [[[self alloc] initWithReason:_reason] raise];
46 }
47
48 + (void)raiseOnStream:(id)_stream reason:(NSString *)_reason {
49   NGIOException *e;
50
51   e = [[self alloc] initWithReason:_reason];
52   
53   if (_stream) {
54     if ([_stream respondsToSelector:@selector(setLastException:)]) {
55       [_stream setLastException:e];
56       [e release];
57       return;
58     }
59   }
60   [e raise];
61 }
62 + (void)raiseOnStream:(id)_stream {
63   [self raiseOnStream:_stream reason:nil];
64 }
65
66 @end /* NGIOException */
67
68 @implementation NGStreamException
69
70 - (NSString *)defaultReason {
71   return @"a stream exception occured";
72 }
73
74 - (id)init {
75   return [self initWithStream:nil reason:[self defaultReason]];
76 }
77 - (id)initWithStream:(id<NGStream>)_stream {
78   return [self initWithStream:_stream reason:[self defaultReason]];
79 }
80
81 - (id)initWithStream:(id<NGStream>)_stream format:(NSString *)_format,... {
82   NSString *tmp = nil;
83   va_list  ap;
84   
85   va_start(ap, _format);
86   tmp = [[NSString alloc] initWithFormat:_format arguments:ap];
87   va_end(ap);
88
89   self = [self initWithStream:_stream reason:tmp];
90   [tmp release];
91   return self;
92 }
93
94 - (id)initWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
95   NSDictionary *ui = nil;
96   
97   ui = [NSDictionary dictionaryWithObjectsAndKeys:
98                        [NSNumber numberWithInt:errno], @"errno",
99                        [NSString stringWithCString:strerror(errno)], @"error",
100                        [NSValue valueWithNonretainedObject:_stream], @"stream",
101                        nil];
102   
103   self = [super initWithName:NSStringFromClass([self class])
104                 reason:_reason
105                 userInfo:ui];
106   if (self) {
107     self->streamPointer = 
108       [[NSValue valueWithNonretainedObject:_stream] retain];
109   }
110   return self;
111 }
112
113 + (id)exceptionWithStream:(id<NGStream>)_stream {
114   return [[self alloc] initWithStream:_stream];
115 }
116 + (id)exceptionWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
117   return [[self alloc] initWithStream:_stream reason:_reason];
118 }
119 + (void)raiseWithStream:(id<NGStream>)_stream {
120   [[[self alloc] initWithStream:_stream] raise];
121 }
122 + (void)raiseWithStream:(id<NGStream>)_stream reason:(NSString *)_reason {
123   [[[self alloc] initWithStream:_stream reason:_reason] raise];
124 }
125 + (void)raiseWithStream:(id<NGStream>)_stream format:(NSString *)_format,... {
126   NSString *tmp = nil;
127   va_list  ap;
128   
129   va_start(ap, _format);
130   tmp = [[NSString alloc] initWithFormat:_format arguments:ap];
131   va_end(ap);
132   tmp = [tmp autorelease];
133
134   [[[self alloc] initWithStream:_stream reason:tmp] raise];
135 }
136
137 - (void)dealloc {
138   [self->streamPointer release];
139   [super dealloc];
140 }
141
142 @end /* NGStreamException */
143
144 // ******************** NGEndOfStreamException ********************
145
146 @implementation NGEndOfStreamException
147
148 - (id)initWithStream:(id<NGStream>)_stream {
149   return [self initWithStream:_stream
150                readCount:0
151                safeCount:0
152                data:nil];
153 }
154
155 - (id)initWithStream:(id<NGStream>)_stream
156   readCount:(unsigned)_readCount
157   safeCount:(unsigned)_safeCount
158   data:(NSData *)_data {
159
160   NSString *tmp;
161
162   tmp = [NSString stringWithFormat:@"reached end of stream %@", _stream];
163
164   if ((self = [super initWithStream:_stream reason:tmp])) {
165     self->readCount = _readCount;
166     self->safeCount = _safeCount;
167     self->data      = [_data retain];
168   }
169   return self;
170 }
171
172 - (void)dealloc {
173   [self->data release];
174   [super dealloc];
175 }
176
177 /* accessors */
178
179 - (NSData *)readBytes {
180   return self->data;
181 }
182
183 @end /* NGEndOfStreamException */
184
185 // ******************** open state exceptions *********************
186
187 @implementation NGCouldNotOpenStreamException
188
189 - (NSString *)defaultReason {
190   return @"could not open stream";
191 }
192
193 @end
194
195 @implementation NGCouldNotCloseStreamException
196
197 - (NSString *)defaultReason {
198   return @"could not close stream";
199 }
200
201 @end
202
203 @implementation NGStreamNotOpenException
204
205 - (NSString *)defaultReason {
206   return @"stream is not open";
207 }
208
209 @end
210
211 // ******************** NGStreamErrors ****************************
212
213 @implementation NGStreamErrorException
214
215 - (id)initWithStream:(id<NGStream>)_stream errorCode:(int)_code {
216   NSString *tmp = nil;
217
218   tmp = [NSString stringWithFormat:@"stream error occured, errno=%i error=%s",
219                     _code, strerror(_code)];
220   if ((self = [self initWithStream:_stream reason:tmp])) {
221     osErrorCode = _code;
222   }
223   tmp = nil;
224   return self;
225 }
226
227 + (void)raiseWithStream:(id<NGStream>)_stream errorCode:(int)_code {
228   [[[self alloc] initWithStream:_stream errorCode:_code] raise];
229 }
230
231 - (int)operationSystemErrorCode {
232   return osErrorCode;
233 }
234 - (NSString *)operatingSystemError {
235   return [NSString stringWithCString:strerror(osErrorCode)];
236 }
237
238 @end /* NGStreamErrorException */
239
240 @implementation NGStreamReadErrorException
241
242 - (NSString *)defaultReason {
243   return @"read error on stream";
244 }
245
246 @end /* NGStreamReadErrorException */
247
248 @implementation NGStreamWriteErrorException
249
250 - (NSString *)defaultReason {
251   return @"write error on stream";
252 }
253
254 @end /* NGStreamWriteErrorException */
255
256 @implementation NGStreamSeekErrorException
257
258 - (NSString *)defaultReason {
259   return @"seek error on stream";
260 }
261
262 @end
263
264 // ******************** NGStreamModeExceptions ********************
265
266 @implementation NGStreamModeException
267
268 - (NSString *)defaultReason {
269   return @"stream mode failure";
270 }
271
272 @end
273
274 @implementation NGUnknownStreamModeException
275
276 - (NSString *)defaultReason {
277   return @"unknow stream mode";
278 }
279
280 - (id)initWithStream:(id<NGStream>)_stream mode:(NSString *)_streamMode {
281   if ((self = [super initWithStream:_stream
282                      format:@"unknown stream mode: %@", _streamMode])) {
283     streamMode = [_streamMode copy];
284   }
285   return self;
286 }
287
288 - (void)dealloc {
289   [self->streamMode release];
290   [super dealloc];
291 }
292
293 @end /* NGUnknownStreamModeException */
294
295 @implementation NGReadOnlyStreamException
296
297 - (NSString *)defaultReason {
298   return @"stream is read only";
299 }
300
301 @end /* NGReadOnlyStreamException */
302
303 @implementation NGWriteOnlyStreamException
304
305 - (NSString *)defaultReason {
306   return @"stream is write only";
307 }
308
309 @end /* NGWriteOnlyStreamException */
310
311 // ******************** NGIOAccessException ******************
312
313 @implementation NGIOAccessException
314 @end
315
316 @implementation NGIOSearchAccessException
317 @end