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