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
25 #include "NGStreamPipe.h"
26 #include "NGFileStream.h"
27 #include "NGBufferedStream.h"
31 @implementation NGStreamPipe
36 static const int NGInvalidUnixDescriptor = -1;
38 @interface _NGConcretePipeFileHandle : NSFileHandle
44 - (id)initWithDescriptor:(int *)_fd;
48 @interface NGFileStream(PrivateMethods)
49 - (id)__initWithDescriptor:(int)_fd mode:(NGStreamMode)_mode;
52 @implementation NGStreamPipe
55 return [[[self alloc] init] autorelease];
59 if (pipe(self->fildes) == -1) {
60 NSLog (@"pipe() system call failed: %s", strerror (errno));
61 self = [self autorelease];
74 [self->fhOut release];
78 - (NSFileHandle *)fileHandleForReading {
79 if (self->fhIn == nil) {
80 self->fhIn = [[_NGConcretePipeFileHandle alloc]
81 initWithDescriptor:&(self->fildes[0])];
85 - (NSFileHandle *)fileHandleForWriting {
86 if (self->fhOut == nil) {
87 self->fhOut = [[_NGConcretePipeFileHandle alloc]
88 initWithDescriptor:&(self->fildes[1])];
93 - (id<NGByteSequenceStream>)streamForReading {
96 - (id<NGOutputStream>)streamForWriting {
100 - (NSException *)lastException {
106 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
109 if (self->fildes[0] == NGInvalidUnixDescriptor) {
110 [NGStreamReadErrorException raiseWithStream:self
111 reason:@"read end of pipe is closed"];
114 readResult = read(self->fildes[0], _buf, _len);
117 [NGEndOfStreamException raiseWithStream:self];
118 else if (readResult == -1)
119 [NGStreamReadErrorException raiseWithStream:self errorCode:errno];
123 - (BOOL)safeReadBytes:(void *)_buf count:(unsigned)_len {
124 return NGSafeReadBytesFromStream(self, _buf, _len);
130 NSLog(@"WARNING: called mark on a stream which doesn't support marking !");
134 [NGStreamException raiseWithStream:self reason:@"marking not supported"];
137 - (BOOL)markSupported {
143 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
146 if (self->fildes[1] == NGInvalidUnixDescriptor) {
147 [NGStreamWriteErrorException raiseWithStream:self
148 reason:@"write end of pipe is closed"];
151 writeResult = write(self->fildes[1], _buf, _len);
153 if (writeResult == -1)
154 [NGStreamWriteErrorException raiseWithStream:self errorCode:errno];
157 - (BOOL)safeWriteBytes:(const void *)_buf count:(unsigned)_len {
158 return NGSafeWriteBytesToStream(self, _buf, _len);
168 if (self->fildes[0] != NGInvalidUnixDescriptor) close(self->fildes[0]);
169 if (self->fildes[1] != NGInvalidUnixDescriptor) close(self->fildes[1]);
173 - (NGStreamMode)mode {
174 NGStreamMode mode = NGStreamMode_undefined;
176 if (self->fildes[0] != NGInvalidUnixDescriptor)
177 mode |= NGStreamMode_readOnly;
178 if (self->fildes[1] != NGInvalidUnixDescriptor)
179 mode |= NGStreamMode_writeOnly;
184 // NGByteSequenceStream
187 return NGReadByteFromStream(self);
193 return (self->fildes[0] == NGInvalidUnixDescriptor) &&
194 (self->fildes[1] == NGInvalidUnixDescriptor) ? NO : YES;
199 - (NSString *)description {
200 return [NSString stringWithFormat:@"<0x%08X[%@]: in=%i out=%i>",
201 self, NSStringFromClass([self class]),
202 self->fildes[0], self->fildes[1]];
205 @end /* NGStreamPipe */
207 @implementation _NGConcretePipeFileHandle
209 - (id)initWithDescriptor:(int *)_fd {
214 - (int)fileDescriptor {
220 *(self->fd) = NGInvalidUnixDescriptor;