#ifndef __STD_IOS__
#define __STD_IOS__
//
//
//  Copyright Digital Equipment Corporation 1996. All rights reserved.
//
//  Restricted Rights: Use, duplication, or disclosure by the U.S.
//  Government is subject to restrictions as set forth in subparagraph
//  (c) (1) (ii) of DFARS 252.227-7013, or in FAR 52.227-19, or in FAR
//  52.227-14 Alt. III, as applicable.
//
//  This software is proprietary to and embodies the confidential
//  technology of Digital Equipment Corporation. Possession, use, or
//  copying of this software and media is authorized only pursuant to a
//  valid written license from Digital or an authorized sublicensor.
//
//

/***************************************************************************
 *
 * ios - Declarations for the Standard Library basic stream I/O
 *
 * $Id: ios,v 1.94 1996/09/24 19:17:05 smithey Exp $
 *
 ***************************************************************************
 *
 * (c) Copyright 1994-1996 Rogue Wave Software, Inc.
 * ALL RIGHTS RESERVED
 *
 * The software and information contained herein are proprietary to, and
 * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
 * intends to preserve as trade secrets such software and information.
 * This software is furnished pursuant to a written license agreement and
 * may be used, copied, transmitted, and stored only in accordance with
 * the terms of such license and with the inclusion of the above copyright
 * notice.  This software and information or any other copies thereof may
 * not be provided or otherwise made available to any other person.
 *
 * Notwithstanding any other lease or license that may pertain to, or
 * accompany the delivery of, this computer software and information, the
 * rights of the Government regarding its use, reproduction and disclosure
 * are as set forth in Section 52.227-19 of the FARS Computer
 * Software-Restricted Rights clause.
 * 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7013.
 * Contractor/Manufacturer is Rogue Wave Software, Inc.,
 * P.O. Box 2328, Corvallis, Oregon 97339.
 *
 * This computer software and information is distributed with "restricted
 * rights."  Use, duplication or disclosure is subject to restrictions as
 * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
 * Computer Software-Restricted Rights (April 1985)."  If the Clause at
 * 18-52.227-74 "Rights in Data General" is specified in the contract,
 * then the "Alternate III" clause applies.
 *
 **************************************************************************/

#include <compnent.hxx>
#ifndef __USE_STD_IOSTREAM
#error "cannot include ios -- define __USE_STD_IOSTREAM to override default"
#else

#ifndef __STD_RWCOMPILER_H__
#include <stdcomp>
#endif

#include <locale>

#if defined(__DECCXX)
#   ifdef __PRAGMA_ENVIRONMENT
#      pragma __environment __save
#      pragma __environment __header_defaults
#   endif
#endif



#ifndef _RWSTD_NO_NAMESPACE
namespace std {
#endif

#if defined(__DECCXX) && !defined (__DECFIXCXXL724)
extern string __iosbadbitset,__iosfailbitset,__ioseofbitset;
#endif

 
//
//
//     CLASS BASIC_IOS
//
//

template<class charT, class traits>
class _RWSTDExportTemplate basic_ios : public ios_base { 

  	public:

    		typedef basic_ios<charT, traits>           ios_type;
    		typedef basic_streambuf<charT, traits>     streambuf_type; 
    		typedef basic_ostream<charT, traits>       ostream_type;

      		// simplification
    		typedef _TYPENAME traits::char_type      char_type;
                typedef traits                          traits_type;

    		typedef _TYPENAME traits::int_type       int_type;
    		typedef _TYPENAME traits::off_type       off_type;
    		typedef _TYPENAME traits::pos_type       pos_type;
    

    		_EXPLICIT basic_ios(basic_streambuf<charT, traits> *sb_arg);
	 	virtual ~basic_ios();

#if defined(__DECCXX) && !defined(CXXLFIX729)
     		char_type fill() const { return fillch_;}        
#else
     		char_type fill() const; 
#endif
     		char_type fill(char_type ch);

                inline void exceptions(iostate excpt);
	        inline iostate exceptions() const;

                inline void clear(iostate state = goodbit);

	        inline void setstate(iostate state);
                inline iostate rdstate() const;

                inline operator void*() const;
    	        inline bool operator! () const;

    	        inline bool good() const;
    	        inline bool eof()  const;
    	        inline bool fail() const;
    	        inline bool bad()  const;

    		ios_type& copyfmt(const ios_type& rhs);

    		inline ostream_type *tie() const;
    		ostream_type *tie(ostream_type *tie_arg);

    		inline streambuf_type *rdbuf() const;
    	        streambuf_type *rdbuf( streambuf_type *sb);

                locale imbue(const locale& loc);

                inline char  narrow(charT, char) const;
                inline charT widen(char) const;

  	protected:

    		basic_ios();

    		void init(basic_streambuf<charT, traits> *sb);

                inline void _RW_UNSAFE_clear(iostate state = goodbit);

  	private:

    		streambuf_type       *sb_;    

    		ostream_type         *tiestr_; 	

    		char_type             fillch_; 

                iostate            state_;   
    	        iostate            except_; 

};


//
//
//     INLINE DEFINITIONS
//
//

 
//
// basic_streambuf *rdbuf() const
//

template<class charT, class traits>
inline basic_streambuf<charT, traits> *
basic_ios<charT, traits>::rdbuf() const
{
  return sb_;
}

//
// basic_ostream *tie() const
//

template<class charT, class traits>
inline basic_ostream<charT, traits> *
basic_ios<charT, traits>::tie() const
{
  return tiestr_;
}


//
// void exceptions(iostate)
//

template<class charT, class traits>
inline void 
basic_ios<charT, traits>::exceptions(ios_base::iostate st)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif

  except_ = st;

  #ifdef _RWSTD_MULTI_THREAD
   _RW_UNSAFE_clear(rdstate()); 
  #else
   clear(rdstate());
  #endif
}

//
// iostate exceptions() const
//

template<class charT, class traits>
inline ios_base::iostate 
basic_ios<charT, traits>::exceptions() const
{ 
  return except_;
}


//
// void clear(iostate )
//

template<class charT, class traits>
inline void 
basic_ios<charT, traits>::clear(iostate st)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif
  
  _RW_UNSAFE_clear(st); 

}

//
// void setstate(iostate)
//

template<class charT, class traits>
inline void
basic_ios<charT, traits>::setstate(iostate st)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif

  #ifdef _RWSTD_MULTI_THREAD
   _RW_UNSAFE_clear(state_ | st); 
  #else
   clear(state_ | st);
  #endif
}

//
// iostate rdstate() const
//

template<class charT, class traits>
inline ios_base::iostate 
basic_ios<charT, traits>::rdstate() const
{
  return state_;
}

//
// operator bool() const
//

template<class charT, class traits>
inline basic_ios<charT, traits>::operator void*() const
{
  return fail() ? (void*)0 : (void*)1;
}

//
// bool operator!() const
//

template<class charT, class traits>
inline bool 
basic_ios<charT, traits>::operator!() const
{
  return fail();
}

//
// bool good() const
//

template<class charT, class traits>
inline bool 
basic_ios<charT, traits>::good() const
{
  return (state_ == ios_base::goodbit);
}

//
// bool eof() const
//

template<class charT, class traits>
inline bool 
basic_ios<charT, traits>::eof() const
{
  return (state_ & ios_base::eofbit);
}

//
// bool fail() const
//

template<class charT, class traits>
inline bool 
basic_ios<charT, traits>::fail() const
{
  return (state_ & (ios_base::failbit | ios_base::badbit));
}

//
// bool bad() const
//

template<class charT, class traits>
inline bool 
basic_ios<charT, traits>::bad() const
{
  return (state_ & ios_base::badbit);
}

//
// void _RW_UNSAFE_clear(iostate )
//

template<class charT, class traits>
inline void 
basic_ios<charT, traits>::_RW_UNSAFE_clear(iostate st)
{
  if ( !sb_ ) 
  {
    st |= ios_base::badbit;
    state_ |= ios_base::badbit; 
  }

  state_ = st;

  if( rdstate() & exceptions() )
 {
    
#ifdef _RW_STD_EXCEPT
#ifndef _RWSTD_NO_EXCEPTIONS
 if ( rdstate() & exceptions() & ios_base::failbit )
    { 
      state_=st;
#if defined(__DECCXX) && !defined (__DECFIXCXXL724)
      throw ios_base::failure(__iosfailbitset);
#else
      throw ios_base::failure( string("iostream object has failbit set") );
#endif
    }

  if ( rdstate() & exceptions() & ios_base::badbit )
    { 
      state_=st;
#if defined(__DECCXX) && !defined (__DECFIXCXXL724)
      throw ios_base::failure(__iosbadbitset);
#else
      throw ios_base::failure( string("iostream object has badbit set") );
#endif
    }
 if ( rdstate() & exceptions() & ios_base::eofbit )
    { 
      state_=st;
#if defined(__DECCXX) && !defined (__DECFIXCXXL724)
      throw ios_base::failure(__ioseofbitset);
#else
      throw ios_base::failure( string("iostream object has eofbit set") );
#endif
    }
#endif
#endif
  }
  
}

//
// char narrow(charT, char) const
//

template<class charT, class traits>
inline char 
basic_ios<charT, traits>::narrow(charT c, char dfault) const
{
   #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
#if defined(__DECCXX) && !defined(__DECFIXCXXL801)
     return use_facet< ctype<charT> >(getloc()).narrow(c,dfault);
#else
     return use_facet< ctype<charT> >(getloc()).narrow(c.dfault);
#endif
   #else
     return use_facet(getloc(),(ctype<charT>*)0).narrow(c,dfault);
   #endif
     
}

//
// charT widen(char) const
//

template<class charT, class traits>
inline charT 
basic_ios<charT, traits>::widen(char c) const
{
   #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
     return use_facet< ctype<charT> >(getloc()).widen(c);
   #else
     return use_facet(getloc(),(ctype<charT>*)0).widen(c);
   #endif
     
}


// end inlining


#ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
	typedef basic_ios<char>                   ios;
#else
	typedef basic_ios<char, char_traits<char> >      ios;
#endif

#ifndef _RWSTD_NO_WIDE_CHAR
	#ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
		typedef basic_ios<wchar_t>              wios;
	#else
		typedef basic_ios<wchar_t, char_traits<wchar_t> >  wios;
	#endif
#endif

//
//
//     EXTERNAL DECLARATIONS FOR MANIPULATORS
//
//


#ifdef _RWSTD_MULTI_THREAD
  template <class charT, class traits>
  basic_ios<charT,traits>& lock(basic_ios<charT,traits>&);

  template <class charT, class traits>
  basic_ios<charT,traits>& unlock(basic_ios<charT,traits>&);
#endif



#ifndef _RWSTD_NO_NAMESPACE
}
#endif

#ifndef __FORCE_INSTANTIATIONS
// basic_ios
#pragma do_not_instantiate std::basic_ios<char, std::char_traits<char > >::~basic_ios()
#pragma do_not_instantiate std::basic_ios<char, std::char_traits<char > >::basic_ios()
#pragma do_not_instantiate void std::basic_ios<char, std::char_traits<char > >::init(std::basic_streambuf<char, std::char_traits<char > > *)
#endif

#ifdef _RWSTD_COMPILE_INSTANTIATE
#include <ios.cc>
#endif


#if defined(__DECCXX)
#   ifdef __PRAGMA_ENVIRONMENT
#      pragma __environment __restore
#   endif
#endif

#endif // __USE_STD_IOSTREAM

#endif // __STD_IOS__ 
