/** \file
 *
 *  Contains the FileInStream class declaration.
 *
 *  Copyright (c) 2007,2008,2009 MBARI
 *  MBARI Proprietary Information.  All Rights Reserved
 *
 */

#ifndef FILEINSTREAM_H_
#define FILEINSTREAM_H_

#include <cstdio>

#include "InStream.h"
#include "utils/Mutex.h"

/**
 *  Wraps a c FILE* handle with an InStream class.
 *
 *  Part of an attempt to avoid the
 *  standard template library
 *
 *  \ingroup io
 */
class FileInStream : public InStream
{
public:

    /// Constructor
    FileInStream( FILE* file = NULL, bool autoClose = false );

    /// Constructor
    FileInStream( const char* filename );

    virtual ~FileInStream();

    void close();

    /// reads num bytes from buffer to the output buffer/stream
    virtual InStream& read( char* buffer, size_t num );

    /// Change the assoicated file
    void setFile( FILE* file, const char* filename )
    {
        file_ = file;
        filename_ = filename;
        zeroTotalBytesRead();
    }

    bool seek( unsigned int location );

    virtual bool eof();

    virtual void clearErr();

    bool isReadable()
    {
        return NULL != file_;
    }

    const char* getName()
    {
        return filename_.cStr();
    }


protected:
    FILE* file_;
    bool autoClose_;
    Mutex fileMutex_;

private:
    // Note that the copy constructor below is private and not given a body.
    // Any attempt to call it will return a compiler error.
    FileInStream( const FileInStream& old ); // disallow copy constructor
    Str filename_;
};

#endif /*FILEINSTREAM_H_*/
