Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / Foundation / NSStream / NSOutputStream_file.m
blob7aa2102186dd9b14b0ae48deaac5cfa5bb9930c0
1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
9 // Original - Christopher Lloyd <cjwl@objc.net>
10 #import <Foundation/NSOutputStream_file.h>
11 #import <Foundation/NSFileHandle.h>
12 #import <Foundation/NSString.h>
13 #import <Foundation/NSError.h>
14 #import <Foundation/NSData.h>
16 @implementation NSOutputStream_file
18 -initToFileAtPath:(NSString *)path append:(BOOL)append {
19    _delegate=self;
20    _error=nil;
21    _status=NSStreamStatusNotOpen;
22    _path=[path copy];
23    _append=append;
24    _fileHandle=nil;
25    return self;
28 -(void)dealloc {
29    [_error release];
30    [_path release];
31    [_fileHandle release];
32    [super dealloc];
35 -delegate {
36    return _delegate;
39 -(void)setDelegate:delegate {
40    _delegate=delegate;
41    if(_delegate==nil)
42     _delegate=self;
45 -(NSError *)streamError {
46    return _error;
49 -(NSStreamStatus)streamStatus {
50    return _status;
53 -(void)open {
54    if(_status==NSStreamStatusNotOpen){
55     _status=NSStreamStatusOpen;
56     _fileHandle=[[NSFileHandle fileHandleForWritingAtPath:_path] retain];
57     if(_append)
58      [_fileHandle seekToEndOfFile];
59    }
62 -(void)close {
63    _status=NSStreamStatusClosed;
64    [_fileHandle closeFile];
67 -propertyForKey:(NSString *)key {
68    return nil;
71 -(BOOL)hasSpaceAvailable {
72    return (_status==NSStreamStatusOpen)?YES:NO;
75 -(NSInteger)write:(const uint8_t *)buffer maxLength:(NSUInteger)maxLength {
76    if(_status!=NSStreamStatusOpen)
77     return -1;
79 // alright this is terrible   
80    [_fileHandle writeData:[NSData dataWithBytes:buffer length:maxLength]];
82    return maxLength;
85 @end