2 Copyright (C) 2000-2005 SKYRIX Software AG
4 This file is part of SOPE.
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
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.
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
24 #include "NGStreamPipe.h"
25 #include "NGFileStream.h"
26 #include "NGBufferedStream.h"
30 @implementation NGStreamPipe
35 static const int NGInvalidUnixDescriptor = -1;
37 @interface _NGConcretePipeFileHandle : NSFileHandle
43 - (id)initWithDescriptor:(int *)_fd;
47 @interface NGFileStream(PrivateMethods)
48 - (id)__initWithDescriptor:(int)_fd mode:(NGStreamMode)_mode;
51 @implementation NGStreamPipe
54 return [[[self alloc] init] autorelease];
58 if (pipe(self->fildes) == -1) {
59 NSLog (@"pipe() system call failed: %s", strerror (errno));
60 self = [self autorelease];
73 [self->fhOut release];
77 - (NSFileHandle *)fileHandleForReading {
78 if (self->fhIn == nil) {
79 self->fhIn = [[_NGConcretePipeFileHandle alloc]
80 initWithDescriptor:&(self->fildes[0])];
84 - (NSFileHandle *)fileHandleForWriting {
85 if (self->fhOut == nil) {
86 self->fhOut = [[_NGConcretePipeFileHandle alloc]
87 initWithDescriptor:&(self->fildes[1])];
92 - (id<NGByteSequenceStream>)streamForReading {
95 - (id<NGOutputStream>)streamForWriting {
99 - (NSException *)lastException {
105 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
108 if (self->fildes[0] == NGInvalidUnixDescriptor) {
109 [NGStreamReadErrorException raiseWithStream:self
110 reason:@"read end of pipe is closed"];
113 readResult = read(self->fildes[0], _buf, _len);
116 [NGEndOfStreamException raiseWithStream:self];
117 else if (readResult == -1)
118 [NGStreamReadErrorException raiseWithStream:self errorCode:errno];
122 - (BOOL)safeReadBytes:(void *)_buf count:(unsigned)_len {
123 return NGSafeReadBytesFromStream(self, _buf, _len);
129 NSLog(@"WARNING: called mark on a stream which doesn't support marking !");
133 [NGStreamException raiseWithStream:self reason:@"marking not supported"];
136 - (BOOL)markSupported {
142 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
145 if (self->fildes[1] == NGInvalidUnixDescriptor) {
146 [NGStreamWriteErrorException raiseWithStream:self
147 reason:@"write end of pipe is closed"];
150 writeResult = write(self->fildes[1], _buf, _len);
152 if (writeResult == -1)
153 [NGStreamWriteErrorException raiseWithStream:self errorCode:errno];
156 - (BOOL)safeWriteBytes:(const void *)_buf count:(unsigned)_len {
157 return NGSafeWriteBytesToStream(self, _buf, _len);
167 if (self->fildes[0] != NGInvalidUnixDescriptor) close(self->fildes[0]);
168 if (self->fildes[1] != NGInvalidUnixDescriptor) close(self->fildes[1]);
172 - (NGStreamMode)mode {
173 NGStreamMode mode = NGStreamMode_undefined;
175 if (self->fildes[0] != NGInvalidUnixDescriptor)
176 mode |= NGStreamMode_readOnly;
177 if (self->fildes[1] != NGInvalidUnixDescriptor)
178 mode |= NGStreamMode_writeOnly;
183 // NGByteSequenceStream
186 return NGReadByteFromStream(self);
192 return (self->fildes[0] == NGInvalidUnixDescriptor) &&
193 (self->fildes[1] == NGInvalidUnixDescriptor) ? NO : YES;
198 - (NSString *)description {
199 return [NSString stringWithFormat:@"<0x%08X[%@]: in=%i out=%i>",
200 self, NSStringFromClass([self class]),
201 self->fildes[0], self->fildes[1]];
204 @end /* NGStreamPipe */
206 @implementation _NGConcretePipeFileHandle
208 - (id)initWithDescriptor:(int *)_fd {
213 - (int)fileDescriptor {
219 *(self->fd) = NGInvalidUnixDescriptor;