]> err.no Git - sope/blob - sope-core/NGStreams/NGStreamPipe.m
renamed packages as discussed in the developer list
[sope] / sope-core / NGStreams / NGStreamPipe.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 "config.h"
24 #include "common.h"
25 #include "NGStreamPipe.h"
26 #include "NGFileStream.h"
27 #include "NGBufferedStream.h"
28
29 #if defined(WIN32)
30
31 @implementation NGStreamPipe
32 @end
33
34 #else
35
36 static const int NGInvalidUnixDescriptor = -1;
37
38 @interface _NGConcretePipeFileHandle : NSFileHandle
39 {
40 @public
41   int *fd;
42 }
43
44 - (id)initWithDescriptor:(int *)_fd;
45
46 @end
47
48 @interface NGFileStream(PrivateMethods)
49 - (id)__initWithDescriptor:(int)_fd mode:(NGStreamMode)_mode;
50 @end
51
52 @implementation NGStreamPipe
53
54 + (id)pipe {
55   return [[[self alloc] init] autorelease];
56 }
57
58 - (id)init {
59   if (pipe(self->fildes) == -1) {
60     NSLog (@"pipe() system call failed: %s", strerror (errno));
61     self = [self autorelease];
62     return nil;
63   }
64   return self;
65 }
66
67 - (void)gcFinalize {
68   [self close];
69 }
70
71 - (void)dealloc {
72   [self gcFinalize];
73   [self->fhIn  release];
74   [self->fhOut release];
75   [super dealloc];
76 }
77
78 - (NSFileHandle *)fileHandleForReading {
79   if (self->fhIn == nil) {
80     self->fhIn = [[_NGConcretePipeFileHandle alloc]
81                       initWithDescriptor:&(self->fildes[0])];
82   }
83   return self->fhIn;
84 }
85 - (NSFileHandle *)fileHandleForWriting {
86   if (self->fhOut == nil) {
87     self->fhOut = [[_NGConcretePipeFileHandle alloc]
88                        initWithDescriptor:&(self->fildes[1])];
89   }
90   return self->fhOut;
91 }
92
93 - (id<NGByteSequenceStream>)streamForReading {
94   return self;
95 }
96 - (id<NGOutputStream>)streamForWriting {
97   return self;
98 }
99
100 - (NSException *)lastException {
101   return nil;
102 }
103
104 /* NGInputStream */
105
106 - (unsigned)readBytes:(void *)_buf count:(unsigned)_len {
107   int readResult;
108
109   if (self->fildes[0] == NGInvalidUnixDescriptor) {
110     [NGStreamReadErrorException raiseWithStream:self
111                                 reason:@"read end of pipe is closed"];
112   }
113   
114   readResult = read(self->fildes[0], _buf, _len);
115
116   if (readResult == 0)
117     [NGEndOfStreamException raiseWithStream:self];
118   else if (readResult == -1)
119     [NGStreamReadErrorException raiseWithStream:self errorCode:errno];
120
121   return readResult;
122 }
123 - (BOOL)safeReadBytes:(void *)_buf count:(unsigned)_len {
124   return NGSafeReadBytesFromStream(self, _buf, _len);
125 }
126
127 /* marks */
128
129 - (BOOL)mark {
130   NSLog(@"WARNING: called mark on a stream which doesn't support marking !");
131   return NO;
132 }
133 - (BOOL)rewind {
134   [NGStreamException raiseWithStream:self reason:@"marking not supported"];
135   return NO;
136 }
137 - (BOOL)markSupported {
138   return NO;
139 }
140
141 /* NGOutputStream */
142
143 - (unsigned)writeBytes:(const void *)_buf count:(unsigned)_len {
144   int writeResult;
145
146   if (self->fildes[1] == NGInvalidUnixDescriptor) {
147     [NGStreamWriteErrorException raiseWithStream:self
148                                  reason:@"write end of pipe is closed"];
149   }
150   
151   writeResult = write(self->fildes[1], _buf, _len);
152
153   if (writeResult == -1)
154     [NGStreamWriteErrorException raiseWithStream:self errorCode:errno];
155   return writeResult;
156 }
157 - (BOOL)safeWriteBytes:(const void *)_buf count:(unsigned)_len {
158   return NGSafeWriteBytesToStream(self, _buf, _len);
159 }
160
161 - (BOOL)flush {
162   return YES;
163 }
164
165 /* NGStream */
166
167 - (BOOL)close {
168   if (self->fildes[0] != NGInvalidUnixDescriptor) close(self->fildes[0]);
169   if (self->fildes[1] != NGInvalidUnixDescriptor) close(self->fildes[1]);
170   return YES;
171 }
172
173 - (NGStreamMode)mode {
174   NGStreamMode mode = NGStreamMode_undefined;
175
176   if (self->fildes[0] != NGInvalidUnixDescriptor)
177     mode |= NGStreamMode_readOnly;
178   if (self->fildes[1] != NGInvalidUnixDescriptor)
179     mode |= NGStreamMode_writeOnly;
180
181   return mode;
182 }
183
184 // NGByteSequenceStream
185
186 - (int)readByte {
187   return NGReadByteFromStream(self);
188 }
189
190 // Extensions
191
192 - (BOOL)isOpen {
193   return (self->fildes[0] == NGInvalidUnixDescriptor) &&
194          (self->fildes[1] == NGInvalidUnixDescriptor) ? NO : YES;
195 }
196
197 /* description */
198
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]];
203 }
204
205 @end /* NGStreamPipe */
206
207 @implementation _NGConcretePipeFileHandle
208
209 - (id)initWithDescriptor:(int *)_fd {
210   self->fd = _fd;
211   return self;
212 }
213
214 - (int)fileDescriptor {
215   return *(self->fd);
216 }
217
218 - (void)closeFile {
219   close(*(self->fd));
220   *(self->fd) = NGInvalidUnixDescriptor;
221 }
222
223 @end
224
225 #endif /* WIN32 */