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

#ifndef DEVICEIOSTREAM_H_
#define DEVICEIOSTREAM_H_

#include <cstdio>

#include "data/ElementURI.h"
#include "io/IOStream.h"
#include "logger/Logger.h"
/**
 *  Wraps a linux device with an IOStream class.
 *
 *  \ingroup io
 */
class DeviceIOStream : public IOStream
{
public:

    /// Constructor 1
    /// argument filename: device filename
    DeviceIOStream( const char* filename, bool useHardware );

    /// Constructor 2
    /// argument configUri: device name in vehicle.cfg file
    /// argument logger
    DeviceIOStream( const ConfigURI& configUri, bool useHardware, Logger& logger );

    virtual ~DeviceIOStream();

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

    bool isReadable()
    {
        return -1 != file_;
    }

    /// Indicates that the end of file can not be reached
    virtual bool eof()
    {
        return -1 != file_;
    }

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

    /// indicates that the stream can actually be written to.
    virtual bool isWritable()
    {
        return -1 != file_;
    }

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

    int getFile()
    {
        return file_;
    }

protected:
    int file_;

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

    static Str GetFilename( const ConfigURI& configUri, Logger& logger );

    bool useHardware_;
    const Str name_;

};

#endif /*DEVICEIOSTREAM_H_*/
