2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
24 #include "NGFilterStream.h"
26 @implementation NGFilterStream
28 + (id)filterWithInputSource:(id<NGInputStream>)_s {
29 return [[(NGFilterStream *)[self alloc] initWithInputSource:_s] autorelease];
31 + (id)filterWithOutputSource:(id<NGOutputStream>)_s {
32 return [[(NGFilterStream *)[self alloc] initWithOutputSource:_s] autorelease];
34 + (id)filterWithSource:(id<NGStream>)_s {
35 return [[(NGFilterStream *)[self alloc] initWithSource:_s] autorelease];
39 return [self initWithSource:nil];
42 - (id)initWithSource:(id<NGStream>)_source {
43 if ((self = [super init])) {
44 self->source = [_source retain];
46 if ([source isKindOfClass:[NSObject class]]) {
47 self->readBytes = (NGIOReadMethodType)
48 [(NSObject *)self->source methodForSelector:@selector(readBytes:count:)];
49 self->writeBytes = (NGIOWriteMethodType)
50 [(NSObject *)self->source methodForSelector:@selector(writeBytes:count:)];
56 - (id)initWithInputSource:(id<NGInputStream>)_source {
57 if ((self = [super init])) {
58 self->source = [_source retain];
60 if ([source isKindOfClass:[NSObject class]]) {
61 self->readBytes = (NGIOReadMethodType)
62 [(NSObject *)self->source methodForSelector:@selector(readBytes:count:)];
67 - (id)initWithOutputSource:(id<NGOutputStream>)_source {
68 if ((self = [super init])) {
69 self->source = [_source retain];
71 if ([source isKindOfClass:[NSObject class]]) {
72 self->writeBytes = (NGIOWriteMethodType)
73 [(NSObject *)self->source methodForSelector:@selector(writeBytes:count:)];
80 [self->source release];
81 self->readBytes = NULL;
82 self->writeBytes = NULL;
88 - (id<NGInputStream>)inputStream {
91 - (id<NGOutputStream>)outputStream {
94 - (id<NGStream>)source {
100 - (NSException *)lastException {
101 return [self->source lastException];
103 - (void)resetLastException {
104 [self->source resetLastException];
106 - (void)setLastException:(NSException *)_exception {
107 [self->source setLastException:_exception];
111 return [self->source isOpen];
114 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
116 return (unsigned)readBytes(self->source, _cmd, _buf, _len);
118 return [self->source readBytes:_buf count:_len];
120 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
121 if (self->writeBytes)
122 return (unsigned)writeBytes(self->source, _cmd, _buf, _len);
124 return [self->source writeBytes:_buf count:_len];
128 return [self->source flush];
131 return [((NGStream *)self->source) close];
134 - (NGStreamMode)mode {
135 return [(NGStream *)self->source mode];
137 - (BOOL)isRootStream {
141 // all other things are forward
143 - (void)forwardInvocation:(NSInvocation *)_invocation {
144 if ([self->source respondsToSelector:[_invocation selector]]) {
145 [_invocation setTarget:self->source];
146 [_invocation invoke];
149 [self doesNotRecognizeSelector:[_invocation selector]];
154 - (NSString *)description {
155 return [NSString stringWithFormat:
156 @"<%@[0x%08X] source=%@ mode=%@>",
157 NSStringFromClass([self class]), (unsigned)self,
158 self->source ? (id)self->source : (id)@"nil",
159 [self modeDescription]];
162 @end /* NGFilterStream */