//
// $Id: FileInStream.m,v 1.7 2005/06/15 11:43:47 johnstrr Exp $
//
// vi: set ft=objc:

/*
 * ObjectiveLib - a library of containers and algorithms for Objective-C
 *
 * Copyright (c) 2004
 * Will Mason
 *
 * Portions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Copyright (c) 1996,1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Copyright (c) 1997
 * Moscow Center for SPARC Technology
 *
 * Copyright (c) 1999 
 * Boris Fomitchev
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * You may contact the author at will_mason@users.sourceforge.net.
 */

#import "FileInStream.h"
#import "Macros.h"
#import <Foundation/NSPathUtilities.h>
#import <Foundation/NSException.h>
#import <Foundation/NSFileHandle.h>
#import <unistd.h>
#import <errno.h>
#import <string.h>
#import <fcntl.h>
#import <limits.h>

@interface OLFileInStream (ProtectedMethods)

- (ssize_t) readImpl: (int)fdesc buffer: (uint8_t*)buffer max: (unsigned)max;

@end

@implementation OLFileInStream

+ (id) streamWithFileHandle: (NSFileHandle*)handle
{
    OL_BEGIN_AUTO_CTOR(OLFileInStream)
        initWithFileHandle: handle
    OL_END_AUTO_CTOR;
}

+ (id) streamWithPath: (NSString*)path
{
    OL_BEGIN_AUTO_CTOR(OLFileInStream)
        initWithPath: path
    OL_END_AUTO_CTOR;
}

- (id) initWithFD: (int)fileDesc
{
    [super init];
    fd = fileDesc;
    return self;
}

- (id) initWithFileHandle: (NSFileHandle*)handle
{
    return [self initWithFD: [handle fileDescriptor]];
}

- (id) initWithPath: (NSString*)path
{
    char nameBuf[PATH_MAX + 1];

    [super init];
    if (![path getFileSystemRepresentation: nameBuf maxLength: PATH_MAX])
    {
        [NSException raise: OLInputOutputException
            format: @"Cannot convert \"%@\" into a file name", path];
    }
    fd = open(nameBuf, O_RDONLY);
    if (fd == -1)
    {
        [NSException raise: OLInputOutputException
            format: @"Error opening file - %s", strerror(errno)];
    }
    return self;
}

- (void) dealloc
{
    [self close];
    [super dealloc];
}

- (void) close
{
    if (!(fd == -1 || fd == STDIN_FILENO))
    {
        if (close(fd) != 0)
        {
            fd = -1;
            [NSException raise: OLInputOutputException
                format: @"Error closing file - %s", strerror(errno)];
        }
    }
    fd = -1;
}

- (int) fileDescriptor
{
    return fd;
}

- (unsigned) readBytes: (uint8_t*)buffer count: (unsigned)max
{
    ssize_t readCount = [self readImpl: fd buffer: buffer max: max];

    if (readCount == -1)
    {
        [NSException raise: OLInputOutputException
            format: @"Error reading from file - %s", strerror(errno)];
    }
    else if (readCount == 0)
    {
        readCount = UINT_MAX;
    }
    return readCount;
}

- (ssize_t) readImpl: (int)fdesc buffer: (uint8_t*)buffer max: (unsigned)max
{
    return read(fdesc, buffer, max);
}

@end
