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