// File:       poscmd00.c++
// Version:    1.00
// Author:     (c) Miles Sabin, 1997
// Purpose:    private header for ostream command classes.

// Change log:
//  28/03/97   v. 1.00

#include "poscmds.h"

#include "exception.h"
#include "ostream.h"
#include "streambuf.h"


// Implementation of OStreamCommand

OStreamCommand::OStreamCommand()
  : rethrow_(false)
  {}

OStreamCommand::~OStreamCommand()
  {}

void OStreamCommand::execute_template(basic_ostream_char& os)
  {
    ios::iostate state = ios::goodbit;

    try
    {
      basic_ostream_char_sentry sentry(os);
      DESTROY_ON_THROW(sentry);

      if(!sentry)
        state |= ios::failbit;
      else
        state |= execute(os);
    }
    BEGIN_HANDLERS
    catch_DOTS
    {
      if(rethrow_)
        rethrow;

      os.setstate_no_throw(state|ios::badbit);
      if((os.exceptions()&ios::badbit) != 0)
        rethrow;
    }
    END_HANDLERS

    if(state != ios::goodbit)
      os.setstate(state);
  }


ios::iostate OStreamCommand::insert_fill(basic_ostream_char& os, char fill_char, int width)
  {
    basic_streambuf_char* sb = os.rdbuf();

    while(width-- > 0)
      if(sb->sputc(fill_char) == basic_ostream_char::traits::eof())
        return ios::failbit;

    return ios::goodbit;
  }

