mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 09:04:13 +00:00
NSStream additions
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@22496 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
c91bf05a5e
commit
5f074036fb
10 changed files with 2503 additions and 1 deletions
15
ChangeLog
15
ChangeLog
|
@ -1,3 +1,18 @@
|
|||
2006-02-15 Derek Zhou <dzhou@nvidea.com> Richard Frith-Macdonald <rfm@gnu.org>
|
||||
|
||||
* Headers/Foundation/NSStream.h: New NSStream, NSInputStream and
|
||||
NSOutputStream classes and constants.
|
||||
* Headers/Foundation/Foundation.h: include NSStream.h
|
||||
* Source/GSStream.m: System independent parts of NSStream
|
||||
* Source/unix/NSStream.m: Unix specific class implementations
|
||||
* Source/unix/GNUmakefile: Build NSStream.m
|
||||
* Source/win32/NSStreamWin32.m: Win32 placeholders
|
||||
* Source/win32/GNUmakefile: Build NSStreamWin32.m
|
||||
* Source/GNUmakefile: Build GSStream.m
|
||||
* Source/DocMakefile: Build NSStream documentation
|
||||
Partial implementation of NSStream ... missing SSL, SOCKS, and
|
||||
Win32 (apart from memory streams).
|
||||
|
||||
2006-02-15 Andrew Ruder <aeruder@ksu.edu>
|
||||
* Headers/Foundation/NSString.h: Fixing the oldest bug in GNUstep?
|
||||
The header has says NSObject since Andrew McCallum added the file
|
||||
|
|
|
@ -84,6 +84,7 @@
|
|||
#include <Foundation/NSSerialization.h>
|
||||
#include <Foundation/NSSet.h>
|
||||
#include <Foundation/NSSortDescriptor.h>
|
||||
#include <Foundation/NSStream.h>
|
||||
#include <Foundation/NSString.h>
|
||||
#include <Foundation/NSTask.h>
|
||||
#include <Foundation/NSThread.h>
|
||||
|
|
266
Headers/Foundation/NSStream.h
Normal file
266
Headers/Foundation/NSStream.h
Normal file
|
@ -0,0 +1,266 @@
|
|||
/** Interface for NSStream for GNUStep
|
||||
Copyright (C) 2006 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Derek Zhou <derekzhou@gmail.com>
|
||||
Date: 2006
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
|
||||
*/
|
||||
|
||||
#ifndef __NSStream_h_GNUSTEP_BASE_INCLUDE
|
||||
#define __NSStream_h_GNUSTEP_BASE_INCLUDE
|
||||
|
||||
#include <Foundation/NSObject.h>
|
||||
|
||||
#if OS_API_VERSION(100400,GS_API_LATEST) && GS_API_VERSION(010200,GS_API_LATEST)
|
||||
|
||||
typedef enum {
|
||||
NSStreamStatusNotOpen = 0,
|
||||
NSStreamStatusOpening = 1,
|
||||
NSStreamStatusOpen = 2,
|
||||
NSStreamStatusReading = 3,
|
||||
NSStreamStatusWriting = 4,
|
||||
NSStreamStatusAtEnd = 5,
|
||||
NSStreamStatusClosed = 6,
|
||||
NSStreamStatusError = 7
|
||||
} NSStreamStatus;
|
||||
|
||||
typedef enum {
|
||||
NSStreamEventNone = 0,
|
||||
NSStreamEventOpenCompleted = 1,
|
||||
NSStreamEventHasBytesAvailable = 2,
|
||||
NSStreamEventHasSpaceAvailable = 4,
|
||||
NSStreamEventErrorOccurred = 8,
|
||||
NSStreamEventEndEncountered = 16
|
||||
} NSStreamEvent;
|
||||
|
||||
@class NSError;
|
||||
@class NSHost;
|
||||
@class NSInputStream;
|
||||
@class NSOutputStream;
|
||||
@class NSString;
|
||||
|
||||
/**
|
||||
* NSStream is an abstract class for objects representing streams.
|
||||
*/
|
||||
@interface NSStream : NSObject
|
||||
|
||||
/**
|
||||
* Creates and returns by reference an NSInputStream object and NSOutputStream
|
||||
* object for a socket connection with the specified port on host.
|
||||
*/
|
||||
+ (void) getStreamsToHost: (NSHost *)host
|
||||
port: (int)port
|
||||
inputStream: (NSInputStream **)inputStream
|
||||
outputStream: (NSOutputStream **)outputStream;
|
||||
|
||||
/**
|
||||
* Closes the receiver.
|
||||
*/
|
||||
- (void) close;
|
||||
|
||||
/**
|
||||
* Returns the receiver's delegate.
|
||||
*/
|
||||
- (id) delegate;
|
||||
|
||||
/**
|
||||
* Opens the receiving stream.
|
||||
*/
|
||||
- (void) open;
|
||||
|
||||
/**
|
||||
* Returns the receiver's property for the specified key.
|
||||
*/
|
||||
- (id) propertyForKey: (NSString *)key;
|
||||
|
||||
/**
|
||||
* Removes the receiver from the NSRunLoop specified by aRunLoop
|
||||
* running in the mode.
|
||||
*/
|
||||
- (void) removeFromRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode;
|
||||
|
||||
/**
|
||||
* Schedules the receiver on aRunLoop using the specified mode.
|
||||
*/
|
||||
- (void) scheduleInRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode;
|
||||
|
||||
/**
|
||||
* Sets the receiver's delegate.
|
||||
*/
|
||||
- (void) setDelegate: (id)delegate;
|
||||
|
||||
/**
|
||||
* Sets the value of the property specified by key to property, returns YES
|
||||
* if the key-value pair are accepted by the receiver.
|
||||
*/
|
||||
- (BOOL) setProperty: (id)property forKey: (NSString *)key;
|
||||
|
||||
/**
|
||||
* Returns an NSError object representing the stream error, or nil if no error
|
||||
* has been encountered.
|
||||
*/
|
||||
- (NSError *) streamError;
|
||||
|
||||
/**
|
||||
* Returns the receiver's status.
|
||||
*/
|
||||
- (NSStreamStatus) streamStatus;
|
||||
|
||||
@end
|
||||
|
||||
@class NSData;
|
||||
|
||||
/**
|
||||
* NSInputStream is a subclass of NSStream that provides read-only
|
||||
* stream functionality.
|
||||
*/
|
||||
@interface NSInputStream : NSStream
|
||||
|
||||
/**
|
||||
* Creates and returns an initialized NSInputStream object
|
||||
* for reading from data.
|
||||
*/
|
||||
+ (id) inputStreamWithData: (NSData *)data;
|
||||
|
||||
/**
|
||||
* Creates and returns an initialized NSInputStream object that reads data from
|
||||
* the file at the specified path.
|
||||
*/
|
||||
+ (id) inputStreamWithFileAtPath: (NSString *)path;
|
||||
|
||||
/**
|
||||
* Returns a pointer to the read buffer in buffer and, by reference, the number
|
||||
* of bytes available in len.
|
||||
*/
|
||||
- (BOOL) getBuffer: (uint8_t **)buffer length: (unsigned int *)len;
|
||||
|
||||
/**
|
||||
* Returns YES if the receiver has bytes available to read.
|
||||
* The receiver may also return YES if a read must be attempted
|
||||
* in order to determine the availability of bytes.
|
||||
*/
|
||||
- (BOOL) hasBytesAvailable;
|
||||
|
||||
/**
|
||||
* Returns an initialized NSInputStream object for reading from data.
|
||||
*/
|
||||
- (id) initWithData: (NSData *)data;
|
||||
|
||||
/**
|
||||
* Returns an initialized NSInputStream object for reading from the file at the
|
||||
* specified path.
|
||||
*/
|
||||
- (id) initWithFileAtPath: (NSString *)path;
|
||||
|
||||
/**
|
||||
* Reads up to len bytes into buffer, returning the actual number of bytes read.
|
||||
*/
|
||||
- (int) read: (uint8_t *)buffer maxLength: (unsigned int)len;
|
||||
|
||||
@end
|
||||
|
||||
/**
|
||||
* NSOutputStream is a subclass of NSStream that provides
|
||||
* write-only stream functionality.
|
||||
*/
|
||||
@interface NSOutputStream : NSStream
|
||||
|
||||
/**
|
||||
* Creates and returns an initialized NSOutputStream object
|
||||
* that can write to buffer, up to a maximum of capacity bytes.
|
||||
*/
|
||||
+ (id) outputStreamToBuffer: (uint8_t *)buffer capacity: (unsigned int)capacity;
|
||||
|
||||
/**
|
||||
* Creates and returns an initialized NSOutputStream object
|
||||
* for writing to the file specified by path.
|
||||
*/
|
||||
+ (id) outputStreamToFileAtPath: (NSString *)path append: (BOOL)shouldAppend;
|
||||
|
||||
/**
|
||||
* Creates and returns an initialized NSOutputStream object
|
||||
* that will write stream data to memory.
|
||||
*/
|
||||
+ (id) outputStreamToMemory;
|
||||
|
||||
/**
|
||||
* Returns YES if the receiver can be written to,
|
||||
* or if a write must be attempted
|
||||
* in order to determine if space is available.
|
||||
*/
|
||||
- (BOOL) hasSpaceAvailable;
|
||||
|
||||
/**
|
||||
* Returns an initialized NSOutputStream object that can write to buffer,
|
||||
* up to a maximum of capacity bytes.
|
||||
*/
|
||||
- (id) initToBuffer: (uint8_t *)buffer capacity: (unsigned int)capacity;
|
||||
|
||||
/**
|
||||
* Returns an initialized NSOutputStream object for writing to the file
|
||||
* specified by path.<br />
|
||||
* If shouldAppend is YES, newly written data will be appended to any
|
||||
* existing file contents.
|
||||
*/
|
||||
- (id) initToFileAtPath: (NSString *)path append: (BOOL)shouldAppend;
|
||||
|
||||
/**
|
||||
* Returns an initialized NSOutputStream object that will write to memory.
|
||||
*/
|
||||
- (id) initToMemory;
|
||||
|
||||
/**
|
||||
* Writes the contents of buffer, up to a maximum of len bytes,
|
||||
* to the receiver.
|
||||
*/
|
||||
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len;
|
||||
|
||||
@end
|
||||
|
||||
@protocol GSStreamListener
|
||||
/**
|
||||
* The delegate receives this message when streamEvent
|
||||
* has occurred on theStream.
|
||||
*/
|
||||
- (void) stream: (NSStream *)theStream handleEvent: (NSStreamEvent)streamEvent;
|
||||
@end
|
||||
|
||||
GS_EXPORT NSString * const NSStreamDataWrittenToMemoryStreamKey;
|
||||
GS_EXPORT NSString * const NSStreamFileCurrentOffsetKey;
|
||||
|
||||
GS_EXPORT NSString * const NSStreamSocketSecurityLevelKey;
|
||||
GS_EXPORT NSString * const NSStreamSocketSecurityLevelNone;
|
||||
GS_EXPORT NSString * const NSStreamSocketSecurityLevelSSLv2;
|
||||
GS_EXPORT NSString * const NSStreamSocketSecurityLevelSSLv3;
|
||||
GS_EXPORT NSString * const NSStreamSocketSecurityLevelTLSv1;
|
||||
GS_EXPORT NSString * const NSStreamSocketSecurityLevelNegotiatedSSL;
|
||||
GS_EXPORT NSString * const NSStreamSocketSSLErrorDomain;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSErrorDomain;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSProxyConfigurationKey;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSProxyHostKey;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSProxyPasswordKey;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSProxyPortKey;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSProxyUserKey;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSProxyVersion4;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSProxyVersion5;
|
||||
GS_EXPORT NSString * const NSStreamSOCKSProxyVersionKey;
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* __NSStream_h_GNUSTEP_BASE_INCLUDE */
|
|
@ -92,6 +92,7 @@ NSRunLoop.h \
|
|||
NSScanner.h \
|
||||
NSSerialization.h \
|
||||
NSSet.h \
|
||||
NSStream.h \
|
||||
NSString.h \
|
||||
NSTask.h \
|
||||
NSThread.h \
|
||||
|
|
|
@ -133,6 +133,7 @@ GSFormat.m \
|
|||
GSFTPURLHandle.m \
|
||||
GSHTTPURLHandle.m \
|
||||
GSSet.m \
|
||||
GSStream.m \
|
||||
GSString.m \
|
||||
GSValue.m \
|
||||
NSAttributedString.m \
|
||||
|
@ -321,6 +322,7 @@ NSScanner.h \
|
|||
NSSerialization.h \
|
||||
NSSet.h \
|
||||
NSSortDescriptor.h \
|
||||
NSStream.h \
|
||||
NSString.h \
|
||||
NSTask.h \
|
||||
NSThread.h \
|
||||
|
|
511
Source/GSStream.m
Normal file
511
Source/GSStream.m
Normal file
|
@ -0,0 +1,511 @@
|
|||
/** Implementation for GSStream for GNUStep
|
||||
Copyright (C) 2006 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Derek Zhou <derekzhou@gmail.com>
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
Date: 2006
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
|
||||
*/
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <Foundation/NSData.h>
|
||||
#include <Foundation/NSArray.h>
|
||||
#include <Foundation/NSRunLoop.h>
|
||||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSError.h>
|
||||
#include <Foundation/NSValue.h>
|
||||
#include <Foundation/NSHost.h>
|
||||
|
||||
#include "GSStream.h"
|
||||
|
||||
NSString * const NSStreamDataWrittenToMemoryStreamKey
|
||||
= @"NSStreamDataWrittenToMemoryStreamKey";
|
||||
NSString * const NSStreamFileCurrentOffsetKey
|
||||
= @"NSStreamFileCurrentOffsetKey";
|
||||
|
||||
NSString * const NSStreamSocketSecurityLevelKey
|
||||
= @"NSStreamSocketSecurityLevelKey";
|
||||
NSString * const NSStreamSocketSecurityLevelNone
|
||||
= @"NSStreamSocketSecurityLevelNone";
|
||||
NSString * const NSStreamSocketSecurityLevelSSLv2
|
||||
= @"NSStreamSocketSecurityLevelSSLv2";
|
||||
NSString * const NSStreamSocketSecurityLevelSSLv3
|
||||
= @"NSStreamSocketSecurityLevelSSLv3";
|
||||
NSString * const NSStreamSocketSecurityLevelTLSv1
|
||||
= @"NSStreamSocketSecurityLevelTLSv1";
|
||||
NSString * const NSStreamSocketSecurityLevelNegotiatedSSL
|
||||
= @"NSStreamSocketSecurityLevelNegotiatedSSL";
|
||||
NSString * const NSStreamSocketSSLErrorDomain
|
||||
= @"NSStreamSocketSSLErrorDomain";
|
||||
NSString * const NSStreamSOCKSErrorDomain
|
||||
= @"NSStreamSOCKSErrorDomain";
|
||||
NSString * const NSStreamSOCKSProxyConfigurationKey
|
||||
= @"NSStreamSOCKSProxyConfigurationKey";
|
||||
NSString * const NSStreamSOCKSProxyHostKey
|
||||
= @"NSStreamSOCKSProxyHostKey";
|
||||
NSString * const NSStreamSOCKSProxyPasswordKey
|
||||
= @"NSStreamSOCKSProxyPasswordKey";
|
||||
NSString * const NSStreamSOCKSProxyPortKey
|
||||
= @"NSStreamSOCKSProxyPortKey";
|
||||
NSString * const NSStreamSOCKSProxyUserKey
|
||||
= @"NSStreamSOCKSProxyUserKey";
|
||||
NSString * const NSStreamSOCKSProxyVersion4
|
||||
= @"NSStreamSOCKSProxyVersion4";
|
||||
NSString * const NSStreamSOCKSProxyVersion5
|
||||
= @"NSStreamSOCKSProxyVersion5";
|
||||
NSString * const NSStreamSOCKSProxyVersionKey
|
||||
= @"NSStreamSOCKSProxyVersionKey";
|
||||
|
||||
|
||||
@implementation GSStream
|
||||
|
||||
- (void) close
|
||||
{
|
||||
NSAssert(_currentStatus != NSStreamStatusNotOpen
|
||||
&& _currentStatus != NSStreamStatusClosed,
|
||||
@"Attempt to close a stream not yet opened.");
|
||||
[self _setStatus: NSStreamStatusClosed];
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
DESTROY(_runloop);
|
||||
DESTROY(_modes);
|
||||
DESTROY(_properties);
|
||||
DESTROY(_lastError);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (id) delegate
|
||||
{
|
||||
return _delegate;
|
||||
}
|
||||
|
||||
- (void) open
|
||||
{
|
||||
NSAssert(_currentStatus == NSStreamStatusNotOpen
|
||||
|| _currentStatus == NSStreamStatusOpening,
|
||||
@"Attempt to open a stream already opened.");
|
||||
[self _setStatus: NSStreamStatusOpen];
|
||||
}
|
||||
|
||||
- (void) setDelegate: (id)delegate
|
||||
{
|
||||
if (delegate)
|
||||
{
|
||||
_delegate = delegate;
|
||||
}
|
||||
else
|
||||
{
|
||||
_delegate = self;
|
||||
}
|
||||
_delegateValid
|
||||
= [_delegate respondsToSelector: @selector(stream:handleEvent:)];
|
||||
}
|
||||
|
||||
- (id) init
|
||||
{
|
||||
if ((self = [super init]) != nil)
|
||||
{
|
||||
_delegate = self;
|
||||
_properties = nil;
|
||||
_lastError = nil;
|
||||
_modes = [NSMutableArray new];
|
||||
_currentStatus = NSStreamStatusNotOpen;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL) setProperty: (id)property forKey: (NSString *)key
|
||||
{
|
||||
if (_properties == nil)
|
||||
{
|
||||
_properties = [NSMutableDictionary new];
|
||||
}
|
||||
[_properties setObject: property forKey: key];
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (id) propertyForKey: (NSString *)key
|
||||
{
|
||||
return [_properties objectForKey: key];
|
||||
}
|
||||
|
||||
- (NSError *) streamError
|
||||
{
|
||||
if (_currentStatus == NSStreamStatusError)
|
||||
{
|
||||
return _lastError;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSStreamStatus) streamStatus
|
||||
{
|
||||
return _currentStatus;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation GSStream (Private)
|
||||
|
||||
- (BOOL) _isOpened
|
||||
{
|
||||
return !(_currentStatus == NSStreamStatusNotOpen
|
||||
|| _currentStatus == NSStreamStatusOpening
|
||||
|| _currentStatus == NSStreamStatusClosed);
|
||||
}
|
||||
|
||||
- (void) _recordError
|
||||
{
|
||||
// make an error
|
||||
NSError *theError = [NSError errorWithDomain: NSPOSIXErrorDomain
|
||||
code: errno
|
||||
userInfo: nil];
|
||||
perror("");
|
||||
ASSIGN(_lastError, theError);
|
||||
_currentStatus = NSStreamStatusError;
|
||||
}
|
||||
|
||||
- (void) _sendEvent: (NSStreamEvent)event
|
||||
{
|
||||
if (_delegateValid)
|
||||
{
|
||||
[(id <GSStreamListener>)_delegate stream: self handleEvent: event];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) _setStatus: (NSStreamStatus)newStatus
|
||||
{
|
||||
// last error before closing is preserved
|
||||
if (_currentStatus != NSStreamStatusError
|
||||
|| newStatus != NSStreamStatusClosed)
|
||||
{
|
||||
_currentStatus = newStatus;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation GSInputStream
|
||||
+ (void) initialize
|
||||
{
|
||||
if (self == [GSInputStream class])
|
||||
{
|
||||
GSObjCAddClassBehavior(self, [GSStream class]);
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation GSOutputStream
|
||||
+ (void) initialize
|
||||
{
|
||||
if (self == [GSOutputStream class])
|
||||
{
|
||||
GSObjCAddClassBehavior(self, [GSStream class]);
|
||||
}
|
||||
}
|
||||
@end
|
||||
|
||||
|
||||
@implementation GSMemoryInputStream
|
||||
|
||||
/**
|
||||
* the designated initializer
|
||||
*/
|
||||
- (id) initWithData: (NSData *)data
|
||||
{
|
||||
if ((self = [super init]) != nil)
|
||||
{
|
||||
ASSIGN(_data, data);
|
||||
_pointer = 0;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
if ([self _isOpened])
|
||||
[self close];
|
||||
RELEASE(_data);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (int) read: (uint8_t *)buffer maxLength: (unsigned int)len
|
||||
{
|
||||
unsigned long dataSize = [_data length];
|
||||
unsigned long copySize;
|
||||
|
||||
NSAssert(dataSize >= _pointer, @"Buffer overflow!");
|
||||
if (len + _pointer > dataSize)
|
||||
{
|
||||
copySize = dataSize - _pointer;
|
||||
}
|
||||
else
|
||||
{
|
||||
copySize = len;
|
||||
}
|
||||
if (copySize)
|
||||
{
|
||||
memcpy(buffer, [_data bytes] + _pointer, copySize);
|
||||
_pointer = _pointer + copySize;
|
||||
}
|
||||
else
|
||||
{
|
||||
[self _setStatus: NSStreamStatusAtEnd];
|
||||
}
|
||||
return copySize;
|
||||
}
|
||||
|
||||
- (BOOL) getBuffer: (uint8_t **)buffer length: (unsigned int *)len
|
||||
{
|
||||
unsigned long dataSize = [_data length];
|
||||
|
||||
NSAssert(dataSize >= _pointer, @"Buffer overflow!");
|
||||
*buffer = (uint8_t*)[_data bytes] + _pointer;
|
||||
*len = dataSize - _pointer;
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL) hasBytesAvailable
|
||||
{
|
||||
unsigned long dataSize = [_data length];
|
||||
|
||||
return (dataSize > _pointer);
|
||||
}
|
||||
|
||||
- (void) scheduleInRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
||||
{
|
||||
NSAssert(!_runloop || _runloop == aRunLoop,
|
||||
@"Attempt to schedule in more than one runloop.");
|
||||
ASSIGN(_runloop, aRunLoop);
|
||||
if (![_modes containsObject: mode])
|
||||
[_modes addObject: mode];
|
||||
if ([self _isOpened])
|
||||
[_runloop performSelector:@selector(dispatch:) target: self
|
||||
argument: nil order: 0 modes: _modes];
|
||||
}
|
||||
|
||||
- (void) removeFromRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
||||
{
|
||||
NSAssert(_runloop == aRunLoop,
|
||||
@"Attempt to remove unscheduled runloop");
|
||||
if ([_modes containsObject: mode])
|
||||
{
|
||||
[_modes removeObject: mode];
|
||||
if ([self _isOpened])
|
||||
[_runloop cancelPerformSelector:@selector(dispatch:)
|
||||
target: self argument: nil];
|
||||
if ([_modes count] == 0)
|
||||
DESTROY(_runloop);
|
||||
}
|
||||
}
|
||||
|
||||
- (id) propertyForKey: (NSString *)key
|
||||
{
|
||||
if ([key isEqualToString: NSStreamFileCurrentOffsetKey])
|
||||
return [NSNumber numberWithLong: _pointer];
|
||||
return [super propertyForKey: key];
|
||||
}
|
||||
|
||||
- (void) open
|
||||
{
|
||||
[super open];
|
||||
if (_runloop)
|
||||
[_runloop performSelector: @selector(dispatch:)
|
||||
target: self
|
||||
argument: nil
|
||||
order: 0
|
||||
modes: _modes];
|
||||
}
|
||||
|
||||
- (void) close
|
||||
{
|
||||
if (_runloop)
|
||||
[_runloop cancelPerformSelectorsWithTarget: self];
|
||||
[super close];
|
||||
}
|
||||
|
||||
- (void) dispatch
|
||||
{
|
||||
BOOL av = [self hasBytesAvailable];
|
||||
NSStreamEvent myEvent = av ? NSStreamEventHasBytesAvailable :
|
||||
NSStreamEventEndEncountered;
|
||||
NSStreamStatus myStatus = av ? NSStreamStatusReading :
|
||||
NSStreamStatusAtEnd;
|
||||
|
||||
[self _setStatus: myStatus];
|
||||
[self _sendEvent: myEvent];
|
||||
// dispatch again iff still opened, and last event is not eos
|
||||
if (av && [self _isOpened])
|
||||
{
|
||||
[_runloop performSelector: @selector(dispatch:)
|
||||
target: self
|
||||
argument: nil
|
||||
order: 0
|
||||
modes: _modes];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@implementation GSMemoryOutputStream
|
||||
|
||||
- (id) initToBuffer: (uint8_t *)buffer capacity: (unsigned int)capacity
|
||||
{
|
||||
if ((self = [super init]) != nil)
|
||||
{
|
||||
if (!buffer)
|
||||
{
|
||||
_data = [NSMutableData new];
|
||||
_fixedSize = NO;
|
||||
}
|
||||
else
|
||||
{
|
||||
_data = [[NSMutableData alloc] initWithBytesNoCopy: buffer
|
||||
length: capacity freeWhenDone: NO];
|
||||
_fixedSize = YES;
|
||||
}
|
||||
_pointer = 0;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_data);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len
|
||||
{
|
||||
if (_fixedSize)
|
||||
{
|
||||
unsigned long dataLen = [_data length];
|
||||
uint8_t *origin = (uint8_t *)[_data mutableBytes];
|
||||
|
||||
if (_pointer+len>dataLen)
|
||||
len = dataLen - _pointer;
|
||||
memcpy(origin+_pointer, buffer, len);
|
||||
_pointer = _pointer + len;
|
||||
}
|
||||
else
|
||||
[_data appendBytes: buffer length: len];
|
||||
return len;
|
||||
}
|
||||
|
||||
- (BOOL) hasSpaceAvailable
|
||||
{
|
||||
if (_fixedSize)
|
||||
return [_data length]>_pointer;
|
||||
else
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (void) scheduleInRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
||||
{
|
||||
NSAssert(!_runloop || _runloop == aRunLoop,
|
||||
@"Attempt to schedule in more than one runloop.");
|
||||
ASSIGN(_runloop, aRunLoop);
|
||||
if (![_modes containsObject: mode])
|
||||
[_modes addObject: mode];
|
||||
if ([self _isOpened])
|
||||
[_runloop performSelector: @selector(dispatch:)
|
||||
target: self
|
||||
argument: nil
|
||||
order: 0
|
||||
modes: _modes];
|
||||
}
|
||||
|
||||
- (void) removeFromRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
||||
{
|
||||
NSAssert(_runloop == aRunLoop,
|
||||
@"Attempt to remove unscheduled runloop");
|
||||
if ([_modes containsObject: mode])
|
||||
{
|
||||
[_modes removeObject: mode];
|
||||
if ([self _isOpened])
|
||||
[_runloop cancelPerformSelector: @selector(dispatch:)
|
||||
target: self
|
||||
argument: nil];
|
||||
if ([_modes count] == 0)
|
||||
DESTROY(_runloop);
|
||||
}
|
||||
}
|
||||
|
||||
- (id) propertyForKey: (NSString *)key
|
||||
{
|
||||
if ([key isEqualToString: NSStreamFileCurrentOffsetKey])
|
||||
{
|
||||
if (_fixedSize)
|
||||
return [NSNumber numberWithLong: _pointer];
|
||||
else
|
||||
return [NSNumber numberWithLong:[_data length]];
|
||||
}
|
||||
else if ([key isEqualToString: NSStreamDataWrittenToMemoryStreamKey])
|
||||
return _data;
|
||||
return [super propertyForKey: key];
|
||||
}
|
||||
|
||||
- (void) open
|
||||
{
|
||||
[super open];
|
||||
if (_runloop)
|
||||
{
|
||||
[_runloop performSelector: @selector(dispatch:)
|
||||
target: self
|
||||
argument: nil
|
||||
order: 0
|
||||
modes: _modes];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) close
|
||||
{
|
||||
if (_runloop)
|
||||
[_runloop cancelPerformSelectorsWithTarget: self];
|
||||
[super close];
|
||||
}
|
||||
|
||||
- (void) dispatch
|
||||
{
|
||||
BOOL av = [self hasSpaceAvailable];
|
||||
NSStreamEvent myEvent = av ? NSStreamEventHasSpaceAvailable :
|
||||
NSStreamEventEndEncountered;
|
||||
|
||||
[self _sendEvent: myEvent];
|
||||
// dispatch again iff still opened, and last event is not eos
|
||||
if (av && [self _isOpened])
|
||||
[_runloop performSelector: @selector(dispatch:)
|
||||
target: self
|
||||
argument: nil
|
||||
order: 0
|
||||
modes: _modes];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -31,7 +31,8 @@ SUBPROJECT_NAME = unix
|
|||
|
||||
unix_OBJC_FILES = \
|
||||
GSRunLoopCtxt.m \
|
||||
GSRunLoopWatcher.m
|
||||
GSRunLoopWatcher.m \
|
||||
NSStream.m
|
||||
|
||||
-include Makefile.preamble
|
||||
|
||||
|
|
1505
Source/unix/NSStream.m
Normal file
1505
Source/unix/NSStream.m
Normal file
File diff suppressed because it is too large
Load diff
|
@ -36,6 +36,7 @@ win32_OBJC_FILES =\
|
|||
NSMessagePortWin32.m \
|
||||
NSMessagePortNameServerWin32.m \
|
||||
NSRunLoopWin32.m \
|
||||
NSStreamWin32.m \
|
||||
NSUserDefaultsWin32.m \
|
||||
|
||||
|
||||
|
|
199
Source/win32/NSStreamWin32.m
Normal file
199
Source/win32/NSStreamWin32.m
Normal file
|
@ -0,0 +1,199 @@
|
|||
/** Implementation for NSStream for GNUStep
|
||||
Copyright (C) 2006 Free Software Foundation, Inc.
|
||||
|
||||
Written by: Derek Zhou <derekzhou@gmail.com>
|
||||
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
||||
Date: 2006
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public
|
||||
License along with this library; if not, write to the Free
|
||||
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02111 USA.
|
||||
|
||||
*/
|
||||
|
||||
#include <Foundation/NSData.h>
|
||||
#include <Foundation/NSArray.h>
|
||||
#include <Foundation/NSRunLoop.h>
|
||||
#include <Foundation/NSException.h>
|
||||
#include <Foundation/NSError.h>
|
||||
#include <Foundation/NSValue.h>
|
||||
#include <Foundation/NSHost.h>
|
||||
|
||||
#include "../GSStream.h"
|
||||
|
||||
@implementation NSStream
|
||||
|
||||
+ (void) getStreamsToHost: (NSHost *)host
|
||||
port: (int)port
|
||||
inputStream: (NSInputStream **)inputStream
|
||||
outputStream: (NSOutputStream **)outputStream
|
||||
{
|
||||
[self notImplemented:_cmd]
|
||||
}
|
||||
|
||||
- (void) close
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
}
|
||||
|
||||
- (void) open
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
}
|
||||
|
||||
- (void) setDelegate: (id)delegate
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
}
|
||||
|
||||
- (id) delegate
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (BOOL) setProperty: (id)property forKey: (NSString *)key
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (id) propertyForKey: (NSString *)key
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void) scheduleInRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
}
|
||||
|
||||
- (void) removeFromRunLoop: (NSRunLoop *)aRunLoop forMode: (NSString *)mode;
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
}
|
||||
|
||||
- (NSError *) streamError
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSStreamStatus) streamStatus
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return 0;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSInputStream
|
||||
|
||||
+ (id) inputStreamWithData: (NSData *)data
|
||||
{
|
||||
[self notImplemented:_cmd]
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (id) inputStreamWithFileAtPath: (NSString *)path
|
||||
{
|
||||
[self notImplemented:_cmd]
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (id) initWithData: (NSData *)data
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (id) initWithFileAtPath: (NSString *)path
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (int) read: (uint8_t *)buffer maxLength: (unsigned int)len
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return -1;
|
||||
}
|
||||
|
||||
- (BOOL) getBuffer: (uint8_t **)buffer length: (unsigned int *)len
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return NO;
|
||||
}
|
||||
|
||||
- (BOOL) hasBytesAvailable
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSOutputStream
|
||||
|
||||
+ (id) outputStreamToMemory
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (id) outputStreamToBuffer: (uint8_t *)buffer capacity: (unsigned int)capacity
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (id) outputStreamToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (id) initToMemory
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (id) initToBuffer: (uint8_t *)buffer capacity: (unsigned int)capacity
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (id) initToFileAtPath: (NSString *)path append: (BOOL)shouldAppend
|
||||
{
|
||||
[self notImplemented:_cmd];
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (int) write: (const uint8_t *)buffer maxLength: (unsigned int)len
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return -1;
|
||||
}
|
||||
|
||||
- (BOOL) hasSpaceAvailable
|
||||
{
|
||||
[self subclassResponsibility: _cmd];
|
||||
return NO;
|
||||
}
|
||||
|
||||
@end
|
||||
|
Loading…
Reference in a new issue