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