//
// file_bitwriter  --  file bitwriter
//
// Copyright (c) 2003 by Joergen Ibsen / Jibz
// All Rights Reserved
//
// http://www.ibsensoftware.com/
//

/// @file   file_bitwriter.hpp
/// @brief  bitwriter specialization for files.
/// @author Jibz
/// @date   2003.08.02

#ifndef FILE_BITWRITER_HPP_INCLUDED
#define FILE_BITWRITER_HPP_INCLUDED

#include <fstream>

#include "bitwriter.hpp"

/// Specialization for files.
///
/// This class creates a file and writes to it. Unlike the stream_bitwriter
/// it manages the file that it operates on, and thus hides the details of
/// file access.
///
/// It adds the member function error(), which checks if there was
/// an error creating the file in the constructor.
///
/// @ingroup bitio
///
class file_bitwriter : public bitwriter {

public:
    /// Constructor.
    /// @param name - the name of the file to create.
    file_bitwriter(const char *name) : s(name, std::ios::binary) { ; }

    ~file_bitwriter() { finalize(); }

    /// Check the state of the file.
    /// @return @c true if a file error has occurred.
    bool error() const { return !s; }

protected:
    void write_next_byte(char c) { s.put(c); }

    std::ofstream s;
};

#endif // FILE_BITWRITER_HPP_INCLUDED
