⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nsfilehandle_cfrnonblockingio.m

📁 处理WORD文档的
💻 M
字号:
/*
**
** File:         NSFileHandle_NonBlockingIO.m
**
** Created by:   bbum
**
**
** Standard disclaimer here.
**
** This code may not be correct.  It may not even work [but it sure seems to].
** Potentially,  it may corrupt your data irretrievably [if it does,
** please let us know-- we will do our best to make sure the same thing
** doesn't happen to us or anyone else].  
*/

// platform specific low-level I/O
#ifdef WIN32
#import <System/windows.h>
#elif defined(__MACH__)
#import <libc.h>
#elif defined(__svr4__)
#import <libc.h>
#import <unistd.h>
#import <sys/filio.h>
#endif

// import APIs to NeXT provided frameworks
#import <Foundation/Foundation.h>

// import local framework's API
//#import "CodeFabRuntime.h"

// import API required througout this file's scope
#import "NSFileHandle_CFRNonBlockingIO.h"

@implementation NSFileHandle (CFRNonBlockingIO)
/*"
 * Adds non-blocking I/O API to NSFileHandle.
"*/

- (NSData *)availableDataNonBlocking;
    /*"
     * Returns an NSData object containing all of the currently available data.  Does not block if there is no data; returns nil
     * instead.
    "*/
{
    return [self readDataOfLengthNonBlocking: UINT_MAX];
}

- (NSData *)readDataToEndOfFileNonBlocking;
    /*"
     * Returns an NSData object containing all of the currently available data.  Does not block if there is no data; returns nil
    * instead.  Cover for #{-availableDataNonBlocking}.
    "*/
{
    return [self readDataOfLengthNonBlocking: UINT_MAX];
}

- (unsigned int) _availableByteCountNonBlocking
{
#ifdef WIN32
    HANDLE nativeHandle = [self nativeHandle];
    DWORD lpTotalBytesAvail;
    BOOL peekSuccess;

    peekSuccess = PeekNamedPipe(nativeHandle, NULL, 0L, NULL, &lpTotalBytesAvail, NULL);

    if (peekSuccess == NO)
        [NSException raise: NSFileHandleOperationException
                    format: @"PeekNamedPipe() NT Err # %d", GetLastError()];

    return lpTotalBytesAvail;
#elif defined(__MACH__) || defined(__svr4__)
    int numBytes;
    int fd = [self fileDescriptor];

    if(ioctl(fd, FIONREAD, (char *) &numBytes) == -1)
        [NSException raise: NSFileHandleOperationException
                    format: @"ioctl() Err # %d", errno];

    return numBytes;
#else
#warning Non-blocking I/O not supported on this platform....
    abort();
    return nil;
#endif
}

- (NSData *)readDataOfLengthNonBlocking:(unsigned int)length;
    /*"
     * Reads up to length bytes of data from the file handle.  If no data is available, returns nil.   Does not block.
    "*/
{
    unsigned int readLength;

    readLength = [self _availableByteCountNonBlocking];
    readLength = (readLength < length) ? readLength : length;

    if (readLength == 0)
        return nil;
    
    return [self readDataOfLength: readLength];
}

@end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -