#ifndef __STD_IOSBASE__
#define __STD_IOSBASE__
//
//
//  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.
//
//

/***************************************************************************
 *
 * iosbase - Declarations for the Standard Library basic stream I/O
 *
 * $Id: iosbase,v 1.9 1996/09/20 03:56:46 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 iosbase -- define __USE_STD_IOSTREAM to override default"
#else

#ifndef __STD_RWCOMPILER_H__
#include <stdcomp>
#endif

#ifndef _RWSTD_NO_NEW_HEADER
#include <cstdio>
#include <cstdlib>
#include <cstring>
#ifndef _RWSTD_NO_WIDE_CHAR
#include <cwchar>
#ifndef _RWSTD_NO_WCTYPE_H
#include <cwctype>
#endif
#endif
#include <cstddef>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _RWSTD_NO_WIDE_CHAR
#ifndef _RWSTD_NO_WCHAR_H
#include <iosfwd>
#include <wchar.h>
#endif
#ifndef _RWSTD_NO_WCTYPE_H
#include <wctype.h>
#endif
#endif
#include <stddef.h>
#endif

#include <stdmutex> 

#ifndef _RWSTD_NO_EXCEPTIONS
#ifdef _RW_STD_EXCEPT
#include <exception>
#endif
#endif

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

#ifndef _RWSTD_NO_NAMESPACE
namespace std {
#endif

typedef long         SZ_T;       
typedef SZ_T         streamsize;


//
//
//     CLASS IOS_BASE
//
//

class _RWSTDExport ios_base {

public:

#ifndef _RWSTD_NO_EXCEPTIONS
#ifdef _RW_STD_EXCEPT
  class _RWSTDExport failure : public exception {

     public:
 	_EXPLICIT failure(const string& msg);
        virtual ~failure() _RWSTD_THROW_SPEC_NULL;
        virtual const char* what() const _RWSTD_THROW_SPEC_NULL;

     private:
      string str_;
  };
#endif
#endif

typedef int      iostate;

enum io_state {
                goodbit     = 0x00,   
                badbit      = 0x01,   
                eofbit      = 0x02,  
                failbit     = 0x04  
              };

typedef int      openmode;

enum open_mode {
                 app         = 0x01,   
                 binary      = 0x02,  
                 in          = 0x04, 
                 out         = 0x08,   
                 trunc       = 0x10,                  
                 ate         = 0x20 
               };

typedef int      seekdir;

enum seek_dir {
                 beg         = 0x0,    
                 cur         = 0x1,    
                 end         = 0x2   
              };        

typedef int      fmtflags;

enum fmt_flags {
	          boolalpha   = 0x0001,
                  dec         = 0x0002,
                  fixed       = 0x0004,
                  hex         = 0x0008,
                  internal    = 0x0010,
                  left        = 0x0020,
                  oct         = 0x0040,
                  right       = 0x0080,
                  scientific  = 0x0100,
                  showbase    = 0x0200, 
                  showpoint   = 0x0400, 
                  showpos     = 0x0800, 
                  skipws      = 0x1000, 
                  unitbuf     = 0x2000, 
                  uppercase   = 0x4000, 
                  adjustfield = left | right | internal,
                  basefield   = dec | oct | hex,
                  floatfield  = scientific | fixed
              };

enum event { 
             erase_event  = 0x0001,
             imbue_event  = 0x0002,
             copyfmt_event = 0x0004
           };

typedef void (*event_callback) (event, ios_base&, int index);

void register_callback( event_callback fn, int index);

class Init {
		 static int    init_cnt_;

              public:

		static int    getinit_cnt_() { return init_cnt_;}
		Init();
		~Init();
            };


	inline fmtflags flags() const;
	inline fmtflags flags(fmtflags fmtfl);
	inline fmtflags setf(fmtflags fmtfl);
        inline fmtflags setf(fmtflags fmtfl, fmtflags mask);

    	inline void unsetf(fmtflags mask);

    	ios_base& copyfmt(const ios_base& rhs);
        ios_base& _RW_UNSAFE_copyfmt(const ios_base& rhs);

        inline streamsize precision() const;
    	inline streamsize precision(streamsize prec);

    	inline streamsize width() const;
    	inline streamsize width(streamsize wide);

	static int xalloc();

    	long&  iword(int index);
    	void*& pword(int index);

    	locale imbue(const locale& loc);
    	locale getloc() const
        { return loc_; }

        bool is_synch() 
         { return _sync_with_stdio; }

        bool sync_with_stdio(bool sync = true)
         { 
           bool tmp = _sync_with_stdio;
           _sync_with_stdio = sync;
           return tmp;
         }

  protected:

	ios_base();
	~ios_base();    


    	fmtflags           fmtfl_; 
	streamsize         prec_;   
    	streamsize         wide_;   
    
	#ifdef _RWSTD_MULTI_THREAD
         _RWSTDMutex stream_mutex_;
        #endif

 private:

        void usersize(int);
    
        locale             loc_;

        union ios_user_union {
  				long         lword;
  				void         *pword;
			     };

        struct register_call {
                                int             index_;
                                event_callback  fn_;
                             }; 


        static int                  index_;
        int                         index_cp_;
        int                         index_callback_;  
    	union  ios_user_union        *userwords_; 
        struct register_call         *registered_call_;

        static bool _sync_with_stdio;
};
 


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

 
//
// fmtflags flags() const
//

inline ios_base::fmtflags 
ios_base::flags() const
{
  return fmtfl_;
}

//
// fmtflags flags(fmtflags)
//

inline ios_base::fmtflags 
ios_base::flags(ios_base::fmtflags flgs)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif

  ios_base::fmtflags      f = fmtfl_;

  fmtfl_ = flgs;

  return f;
}

//
// fmtflags setf(fmtflags)
//

inline ios_base::fmtflags 
ios_base::setf(ios_base::fmtflags flgs)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif

  ios_base::fmtflags       f = fmtfl_;

  fmtfl_ |= flgs;

  return f;
}

//
// fmtflags setf(fmtflags, fmtflags)
//

inline ios_base::fmtflags 
ios_base::setf(ios_base::fmtflags flgs, ios_base::fmtflags mask)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif

  ios_base::fmtflags      old = fmtfl_;

  fmtfl_ &= ~mask;

  fmtfl_ |= (flgs & mask);

  return old;
}

//
// void unsetf(fmtflags)
//

inline void 
ios_base::unsetf(ios_base::fmtflags fg)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif

  fmtfl_ &= ~fg;
}

//
// streamsize precision() const
//

inline streamsize 
ios_base::precision() const
{
  return prec_;
}

//
// streamsize precision(streamsize)
//


inline streamsize 
ios_base::precision(streamsize p)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif

  streamsize    temp = prec_;

  prec_ = p;

  return temp;
}

//
// streamsize width() const
//

inline streamsize 
ios_base::width() const
{
  return wide_;
}

//
// streamsize width(streamsize)
//

inline streamsize 
ios_base::width(streamsize w)
{
  #ifdef _RWSTD_MULTI_THREAD
    _RWSTDGuard guard(this->stream_mutex_);
  #endif

  streamsize      i = wide_;

  wide_ = w;

  return i;
}

// end inlining

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


 	ios_base& _RWSTDExport boolalpha(ios_base&);
	ios_base& _RWSTDExport noboolalpha(ios_base&);
	ios_base& _RWSTDExport showbase(ios_base&);
	ios_base& _RWSTDExport noshowbase(ios_base&);
	ios_base& _RWSTDExport showpoint(ios_base&);
	ios_base& _RWSTDExport noshowpoint(ios_base&);
	ios_base& _RWSTDExport showpos(ios_base&);
	ios_base& _RWSTDExport noshowpos(ios_base&);
	ios_base& _RWSTDExport skipws(ios_base&);
	ios_base& _RWSTDExport noskipws(ios_base&);
	ios_base& _RWSTDExport uppercase(ios_base&);
	ios_base& _RWSTDExport nouppercase(ios_base&);
	ios_base& _RWSTDExport internal(ios_base&);
	ios_base& _RWSTDExport left(ios_base&);
	ios_base& _RWSTDExport right(ios_base&);
	ios_base& _RWSTDExport dec(ios_base&);
	ios_base& _RWSTDExport hex(ios_base&);
	ios_base& _RWSTDExport oct(ios_base&);
	ios_base& _RWSTDExport fixed(ios_base&);
	ios_base& _RWSTDExport scientific(ios_base&);
        ios_base& _RWSTDExport unitbuf(ios_base&);
	ios_base& _RWSTDExport nounitbuf(ios_base&);
 
#ifndef _RWSTD_NO_NAMESPACE
}
#endif

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

#endif // __USE_STD_IOSTREAM

#endif // __STD_IOSBASE__ 


