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

#ifndef FILEOUTSTREAM_H_
#define FILEOUTSTREAM_H_

#include <cstdio>

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

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

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

    /// Constructor
    FileOutStream( const char* filename, bool append = true, bool rewrite = false );

    virtual ~FileOutStream();

    /// writes num bytes from buffer to the output buffer/stream
    OutStream& write( const char* buffer, size_t num );

    /// writes zero-terminated buffer to the output buffer/stream
    virtual OutStream& write( const char* buffer )
    {
        return write( buffer, strlen( buffer ) );
    }

    /// Change the assoicated file
    void setFile( FILE* file )
    {
        file_ = file;
    }

    bool seek( unsigned int location );

    int tell();

    void close();

    void flush();

    bool open( const char* filename, bool append = true, bool rewrite = false );

    bool isWritable()
    {
        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.
    FileOutStream( const FileOutStream& old ); // disallow copy constructor
    Str filename_;
};

#endif /*FILEOUTSTREAM_H_*/
