]> err.no Git - sope/blob - sope-core/NGStreams/NGFilterStream.m
bringing libFoundation-1.0.75 into the main branch
[sope] / sope-core / NGStreams / NGFilterStream.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 "NGFilterStream.h"
24
25 @implementation NGFilterStream
26
27 + (id)filterWithInputSource:(id<NGInputStream>)_s {
28   return [[(NGFilterStream *)[self alloc] initWithInputSource:_s] autorelease];
29 }
30 + (id)filterWithOutputSource:(id<NGOutputStream>)_s {
31   return [[(NGFilterStream *)[self alloc] initWithOutputSource:_s] autorelease];
32 }
33 + (id)filterWithSource:(id<NGStream>)_s {
34   return [[(NGFilterStream *)[self alloc] initWithSource:_s] autorelease];
35 }
36
37 - (id)init {
38   return [self initWithSource:nil];
39 }
40
41 - (id)initWithSource:(id<NGStream>)_source {
42   if ((self = [super init])) {
43     self->source = [_source retain];
44
45     if ([source isKindOfClass:[NSObject class]]) {
46       self->readBytes  = (NGIOReadMethodType)
47         [(NSObject *)self->source methodForSelector:@selector(readBytes:count:)];
48       self->writeBytes = (NGIOWriteMethodType)
49         [(NSObject *)self->source methodForSelector:@selector(writeBytes:count:)];
50     }
51   }
52   return self;
53 }
54
55 - (id)initWithInputSource:(id<NGInputStream>)_source {
56   if ((self = [super init])) {
57     self->source = [_source retain];
58
59     if ([source isKindOfClass:[NSObject class]]) {
60       self->readBytes  = (NGIOReadMethodType)
61         [(NSObject *)self->source methodForSelector:@selector(readBytes:count:)];
62     }
63   }
64   return self;
65 }
66 - (id)initWithOutputSource:(id<NGOutputStream>)_source {
67   if ((self = [super init])) {
68     self->source = [_source retain];
69
70     if ([source isKindOfClass:[NSObject class]]) {
71       self->writeBytes  = (NGIOWriteMethodType)
72         [(NSObject *)self->source methodForSelector:@selector(writeBytes:count:)];
73     }
74   }
75   return self;
76 }
77
78 - (void)dealloc {
79   [self->source release];
80   self->readBytes  = NULL;
81   self->writeBytes = NULL;
82   [super dealloc];
83 }
84
85 /* accessors */
86
87 - (id<NGInputStream>)inputStream {
88   return [self source];
89 }
90 - (id<NGOutputStream>)outputStream {
91   return [self source];
92 }
93 - (id<NGStream>)source {
94   return self->source;
95 }
96
97 /* primitives */
98
99 - (NSException *)lastException {
100   return [self->source lastException];
101 }
102 - (void)resetLastException {
103   [self->source resetLastException];
104 }
105 - (void)setLastException:(NSException *)_exception {
106   [self->source setLastException:_exception];
107 }
108
109 - (BOOL)isOpen {
110   return [self->source isOpen];
111 }
112
113 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
114   if (self->readBytes)
115     return (unsigned)readBytes(self->source, _cmd, _buf, _len);
116   else
117     return [self->source readBytes:_buf count:_len];
118 }
119 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
120   if (self->writeBytes)
121     return (unsigned)writeBytes(self->source, _cmd, _buf, _len);
122   else
123     return [self->source writeBytes:_buf count:_len];
124 }
125
126 - (BOOL)flush {
127   return [self->source flush];
128 }
129 - (BOOL)close {
130   return [((NGStream *)self->source) close];
131 }
132
133 - (NGStreamMode)mode {
134   return [(NGStream *)self->source mode];
135 }
136 - (BOOL)isRootStream {
137   return NO;
138 }
139
140 // all other things are forward
141
142 - (void)forwardInvocation:(NSInvocation *)_invocation {
143   if ([self->source respondsToSelector:[_invocation selector]]) {
144     [_invocation setTarget:self->source];
145     [_invocation invoke];
146   }
147   else
148     [self doesNotRecognizeSelector:[_invocation selector]];
149 }
150
151 // description
152
153 - (NSString *)description {
154   return [NSString stringWithFormat:
155                      @"<%@[0x%08X] source=%@ mode=%@>",
156                      NSStringFromClass([self class]), (unsigned)self,
157                      self->source ? (id)self->source : (id)@"nil",
158                      [self modeDescription]];
159 }
160
161 @end /* NGFilterStream */