]> err.no Git - sope/blob - sope-core/NGStreams/NGConcreteStreamFileHandle.m
Id fixes
[sope] / sope-core / NGStreams / NGConcreteStreamFileHandle.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 <objc/objc-api.h>
24 #include "common.h"
25 #include "NGConcreteStreamFileHandle.h"
26 #include "NGStreamProtocols.h"
27 #include "NGStreamExceptions.h"
28 #include "NGBufferedStream.h"
29
30 @interface NGStream(FileHandleReset)
31
32 - (void)resetFileHandle;
33
34 @end
35
36 @implementation NGConcreteStreamFileHandle
37
38 - (id)initWithStream:(id<NGStream>)_stream {
39   if ((self = [super init])) {
40     self->stream = [_stream retain];
41   }
42   return self;
43 }
44
45 - (void)dealloc {
46   if ([stream respondsToSelector:@selector(resetFileHandle)])
47     [(NGStream *)self->stream resetFileHandle];
48   [self->stream release];
49   [super dealloc];
50 }
51
52 // accessors
53
54 - (id<NGStream>)stream {
55   return self->stream;
56 }
57
58 /* NSFileHandle operations */
59
60 - (void)closeFile {
61   [self->stream close];
62 }
63
64 - (int)fileDescriptor {
65   if ([self->stream respondsToSelector:@selector(fileDescriptor)])
66     return [(id)self->stream fileDescriptor];
67   else {
68     [self subclassResponsibility:_cmd];
69     return -1;
70   }
71 }
72
73 /* buffering */
74
75 - (void)synchronizeFile {
76   [self->stream flush];
77 }
78
79 /* reading */
80
81 - (NSData *)readDataOfLength:(unsigned int)_length {
82   char   *buffer;
83   NSData *data;
84
85   *(&buffer) = NGMallocAtomic(_length);
86   *(&data)   = nil;
87   
88   NS_DURING {
89     [stream safeReadBytes:buffer count:_length];
90     data = [[NSData alloc] initWithBytes:buffer length:_length];
91   }
92   NS_HANDLER {
93     if ([localException isKindOfClass:[NGEndOfStreamException class]]) {
94       data = [(NGEndOfStreamException *)localException readBytes];
95
96       data = data ? [data retain] : [[NSData alloc] init];
97     }
98     else {
99       if (buffer) {
100         NGFree(buffer);
101         buffer = NULL;
102       }
103       [localException raise];
104     }
105   }
106   NS_ENDHANDLER;
107   
108   if (buffer) {
109     NGFree(buffer);
110     buffer = NULL;
111   }
112
113   return [data autorelease];
114 }
115
116 - (NSData *)readDataToEndOfFile {
117   NGBufferedStream *bs;
118   NSMutableData    *data;
119   char buf[2048];
120
121   *(&data) = [NSMutableData dataWithCapacity:2048];
122   *(&bs) = [self->stream isKindOfClass:[NGBufferedStream class]]
123     ? [self->stream retain]
124     : [(NGBufferedStream *)[NGBufferedStream alloc] 
125           initWithSource:self->stream];
126
127   NS_DURING {
128     while (1 == 1) {
129       unsigned got = [bs readBytes:buf count:2048];
130       [data appendBytes:buf length:got];
131     }
132   }
133   NS_HANDLER {
134     if (![localException isKindOfClass:[NGEndOfStreamException class]]) {
135       [bs release];
136       bs = nil;
137       [localException raise];
138     }
139   }
140   NS_ENDHANDLER;
141   [bs release]; bs = nil;
142
143   return data;
144 }
145
146 - (NSData *)availableData {
147   NSLog(@"NGConcreteStreamFileHandle(availableData) not implemented");
148   [self notImplemented:_cmd];
149   return nil;
150 }
151
152 /* writing */
153
154 - (void)writeData:(NSData *)_data {
155   [self->stream safeWriteBytes:[_data bytes] count:[_data length]];
156 }
157
158 /* seeking */
159
160 - (unsigned long long)seekToEndOfFile {
161   NSLog(@"NGConcreteStreamFileHandle(seekToEndOfFile) not implemented");
162   [self notImplemented:_cmd];
163   return 0;
164 }
165 - (void)seekToFileOffset:(unsigned long long)_offset {
166   [(id<NGPositionableStream>)stream moveToLocation:_offset];
167 }
168
169 - (unsigned long long)offsetInFile {
170   NSLog(@"_NGConcreteFileStreamFileHandle(offsetInFile:) not implemented, abort");
171   [self notImplemented:_cmd];
172   return 0;
173 }
174
175 /* asynchronous operations */
176
177 - (void)acceptConnectionInBackgroundAndNotify {
178   NSLog(@"NGConcreteStreamFileHandle(acceptConnectionInBackgroundAndNotify) "
179         @"not implemented");
180   [self notImplemented:_cmd];
181 }
182 - (void)acceptConnectionInBackgroundAndNotifyForModes:(NSArray *)_modes {
183   NSLog(@"NGConcreteStreamFileHandle(acceptConnectionInBackgroundAndNotifyForModes:) "
184         @"not implemented");
185   [self notImplemented:_cmd];
186 }
187
188 - (void)readInBackgroundAndNotify {
189   NSLog(@"NGConcreteStreamFileHandle(readInBackgroundAndNotify) not implemented");
190   [self notImplemented:_cmd];
191 }
192 - (void)readInBackgroundAndNotifyForModes:(NSArray *)_modes {
193   NSLog(@"NGConcreteStreamFileHandle(readInBackgroundAndNotifyForModes:) "
194         @"not implemented");
195   [self notImplemented:_cmd];
196 }
197 - (void)readToEndOfFileInBackgroundAndNotify {
198   NSLog(@"NGConcreteStreamFileHandle(readToEndOfFileInBackgroundAndNotify)"
199         @"not implemented");
200   [self notImplemented:_cmd];
201 }
202 - (void)readToEndOfFileInBackgroundAndNotifyForModes:(NSArray *)_modes {
203   NSLog(@"NGConcreteStreamFileHandle("
204         @"readToEndOfFileInBackgroundAndNotifyForModes:)"
205         @"not implemented");
206   [self notImplemented:_cmd];
207 }
208
209 - (void)waitForDataInBackgroundAndNotify {
210   NSLog(@"NGConcreteStreamFileHandle("
211         @"waitForDataInBackgroundAndNotify)"
212         @"not implemented");
213   [self notImplemented:_cmd];
214 }
215 - (void)waitForDataInBackgroundAndNotifyForModes:(NSArray *)_modes {
216   NSLog(@"NGConcreteStreamFileHandle("
217         @"waitForDataInBackgroundAndNotifyForModes:)"
218         @"not implemented");
219   [self notImplemented:_cmd];
220 }
221
222 @end /* NGConcreteStreamFileHandle */