home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 May / PCFMay2001.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / Rw / iosbase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  13.0 KB  |  612 lines

  1. #ifndef __IOSBASE_H
  2. #define __IOSBASE_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. #ifndef __STD_IOSBASE__
  6. #define __STD_IOSBASE__
  7. /***************************************************************************
  8.  *
  9.  * iosbase - Declarations for the Standard Library basic stream I/O
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  14.  *
  15.  * This computer software is owned by Rogue Wave Software, Inc. and is
  16.  * protected by U.S. copyright laws and other laws and by international
  17.  * treaties.  This computer software is furnished by Rogue Wave Software,
  18.  * Inc. pursuant to a written license agreement and may be used, copied,
  19.  * transmitted, and stored only in accordance with the terms of such
  20.  * license and with the inclusion of the above copyright notice.  This
  21.  * computer software or any other copies thereof may not be provided or
  22.  * otherwise made available to any other person.
  23.  *
  24.  * U.S. Government Restricted Rights.  This computer software is provided
  25.  * with Restricted Rights.  Use, duplication, or disclosure by the
  26.  * Government is subject to restrictions as set forth in subparagraph (c)
  27.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  28.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  29.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  30.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  31.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  32.  *
  33.  **************************************************************************/
  34.  
  35. #include <stdcomp.h>
  36.  
  37. #ifndef _RWSTD_NO_NEW_HEADER
  38. #include <cstdio>
  39. #include <cstdlib>
  40. #include <cstring>
  41. #ifdef _HPACC_
  42. #include <wchar.h>
  43. #else
  44. #ifndef _RWSTD_NO_WIDE_CHAR
  45. #include <cwchar>
  46. #ifndef _RWSTD_NO_WCTYPE_H
  47. #include <cwctype>
  48. #endif 
  49. #endif // _RWSTD_NO_WIDE_CHAR
  50. #endif // _HPACC_
  51. #include <cstddef>
  52. #else
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #ifndef _RWSTD_NO_WIDE_CHAR
  57. #ifndef _RWSTD_NO_WCHAR_H
  58. #include <wchar.h>
  59. #endif
  60. #ifndef _RWSTD_NO_WCTYPE_H
  61. #include <wctype.h>
  62. #endif
  63. #endif // _RWSTD_NO_WIDE_CHAR
  64. #include <stddef.h>
  65. #endif // _RWSTD_NO_NEW_HEADER
  66.  
  67. #include <rw/stdmutex.h> 
  68.  
  69. #ifndef _RWSTD_NO_EXCEPTIONS
  70. #ifdef _RW_STD_EXCEPT
  71. #include <exception>
  72. #endif
  73. #endif // _RWSTD_NO_EXCEPTIONS
  74.  
  75. #ifndef _RWSTD_NO_NAMESPACE
  76. namespace std {
  77. #endif
  78.  
  79.   typedef long         SZ_T;       
  80.   typedef SZ_T         streamsize;
  81.  
  82.   class _RWSTDExportTemplate __Wide_Init
  83.   {
  84.     static int    __initcnt;
  85.   public:
  86.     static int    getinit_cnt_() { return __initcnt;}
  87.     __Wide_Init();
  88.     ~__Wide_Init();
  89.   };
  90.  
  91.   /*
  92.    *     Class ios_base
  93.    */
  94.  
  95.   class _RWSTDExport ios_base
  96.   {
  97.   public:
  98.  
  99. #ifndef _RWSTD_NO_EXCEPTIONS
  100. #ifdef  _RW_STD_EXCEPT
  101.     class _RWSTDExport failure : public exception
  102.     {
  103.     public:
  104.       _EXPLICIT failure(const string& msg);
  105.       virtual ~failure() _RWSTD_THROW_SPEC_NULL;
  106.       virtual const char* what() const _RWSTD_THROW_SPEC_NULL;
  107.  
  108.     private:
  109.       string __str;
  110.     };
  111. #endif // _RW_STD_EXCEPT
  112. #endif // _RWSTD_NO_EXCEPTIONS
  113.     typedef int      fmtflags;
  114.     
  115.     enum fmt_flags {
  116.       boolalpha   = 0x0001,
  117.       dec         = 0x0002,
  118.       fixed       = 0x0004,
  119.       hex         = 0x0008,
  120.       internal    = 0x0010,
  121.       left        = 0x0020,
  122.       oct         = 0x0040,
  123.       right       = 0x0080,
  124.       scientific  = 0x0100,
  125.       showbase    = 0x0200, 
  126.       showpoint   = 0x0400, 
  127.       showpos     = 0x0800, 
  128.       skipws      = 0x1000, 
  129.       unitbuf     = 0x2000, 
  130.       uppercase   = 0x4000, 
  131.       adjustfield = left | right | internal,
  132.       basefield   = dec | oct | hex,
  133.       floatfield  = scientific | fixed
  134.     };
  135.     
  136.     typedef int      iostate;
  137.  
  138.     enum io_state {
  139.       goodbit     = 0x00,   
  140.       badbit      = 0x01,   
  141.       eofbit      = 0x02,  
  142.       failbit     = 0x04  
  143.     };
  144.  
  145.     typedef int      openmode;
  146.  
  147.     enum open_mode {
  148.       app         = 0x01,   
  149.       binary      = 0x02,  
  150.       in          = 0x04, 
  151.       out         = 0x08,   
  152.       trunc       = 0x10,                  
  153. #ifdef _RWSTD_STRICT_ANSI
  154.       ate         = 0x20
  155. #else
  156.       ate         = 0x20,
  157.       nocreate    = 0x40,
  158.       noreplace   = 0x80
  159. #endif
  160.     };
  161.  
  162.     typedef int      seekdir;
  163.  
  164.     enum seek_dir {
  165.       beg         = 0x0,    
  166.       cur         = 0x1,    
  167.       end         = 0x2   
  168.     };  
  169.  
  170.     enum EmptyCtor {emptyctor};      // enum type used for empty constructors in derived classes
  171.  
  172.     class _RWSTDExportTemplate Init {
  173.       static int    __initcnt;
  174.     public:
  175.       static int    getinit_cnt_() { return __initcnt;}
  176.       Init();
  177.       ~Init();
  178.     };
  179.  
  180.     inline fmtflags flags() const;
  181.     inline fmtflags flags(fmtflags fmtfl);
  182.     inline fmtflags setf(fmtflags fmtfl);
  183.     inline fmtflags setf(fmtflags fmtfl, fmtflags mask);
  184.     inline void unsetf(fmtflags mask);
  185.  
  186.     inline streamsize precision() const;
  187.     inline streamsize precision(streamsize prec);
  188.     inline streamsize width() const;
  189.     inline streamsize width(streamsize wide);
  190.  
  191.     locale imbue(const locale& loc);
  192.     locale getloc() const
  193.     { return __loc; }
  194.  
  195.     static int xalloc();
  196.     long&  iword(int index);
  197.     void*& pword(int index);
  198.  
  199.     enum event { 
  200.       erase_event   = 0x0001,
  201.       imbue_event   = 0x0002,
  202.       copyfmt_event = 0x0004
  203.     };
  204.  
  205.     typedef void (*event_callback) (event, ios_base&, int index);
  206.     void register_callback( event_callback fn, int index);
  207.     
  208.     ios_base& copyfmt(const ios_base& rhs);
  209.     ios_base& _RW_UNSAFE_copyfmt(const ios_base& rhs);
  210.  
  211.     bool is_synch() 
  212.     { return __sync_with_stdio; }
  213.  
  214.     static bool sync_with_stdio(bool sync = true)
  215.     { 
  216.       bool tmp =__sync_with_stdio;
  217.       __sync_with_stdio = sync;
  218.       return tmp;
  219.     }
  220.  
  221.     virtual ~ios_base();    
  222.  
  223.   protected:
  224.  
  225.     ios_base();
  226.     ios_base(int) {} // empty constructor for creating uninitialized stream
  227.  
  228.     fmtflags           __fmtfl; 
  229.     streamsize         __prec;   
  230.     streamsize         __wide;   
  231.     
  232. #ifdef _RWSTD_MULTI_THREAD
  233.     _RWSTDMutex __stream_mutex;
  234. #endif
  235.  
  236.   private:
  237.  
  238.     void usersize(int);
  239.     locale             __loc;
  240.  
  241.     union ios_user_union {
  242.       long         __lword;
  243.       void        *__pword;
  244.     };
  245.  
  246.     struct register_call {
  247.       int             __index;
  248.       event_callback  __fn;
  249.     }; 
  250.  
  251.     static int                  __index;
  252.     int                         __index_cp;
  253.     int                         __index_callback;  
  254.     union  ios_user_union      *__userwords; 
  255.     struct register_call       *__registered_call;
  256.     static bool                 __sync_with_stdio;
  257.   };
  258.  
  259.   /*
  260.    *     inline definitions
  261.    */
  262.  
  263.   /*
  264.    * fmtflags flags() const
  265.    */
  266.  
  267.   inline ios_base::fmtflags 
  268.   ios_base::flags() const
  269.   {
  270.     return __fmtfl;
  271.   }
  272.  
  273.   /*
  274.    * fmtflags flags(fmtflags)
  275.    */
  276.  
  277.   inline ios_base::fmtflags 
  278.   ios_base::flags(ios_base::fmtflags flgs)
  279.   {
  280. #ifdef _RWSTD_MULTI_THREAD
  281.     _RWSTDGuard guard(this->__stream_mutex);
  282. #endif
  283.     ios_base::fmtflags      f = __fmtfl;
  284.     __fmtfl = flgs;
  285.     return f;
  286.   }
  287.  
  288.   /*
  289.    * fmtflags setf(fmtflags)
  290.    */
  291.  
  292.   inline ios_base::fmtflags 
  293.   ios_base::setf(ios_base::fmtflags flgs)
  294.   {
  295. #ifdef _RWSTD_MULTI_THREAD
  296.     _RWSTDGuard guard(this->__stream_mutex);
  297. #endif
  298.     ios_base::fmtflags       f = __fmtfl;
  299.     __fmtfl |= flgs;
  300.     return f;
  301.   }
  302.  
  303.   /*
  304.    * fmtflags setf(fmtflags, fmtflags)
  305.    */
  306.  
  307.   inline ios_base::fmtflags 
  308.   ios_base::setf(ios_base::fmtflags flgs, ios_base::fmtflags mask)
  309.   {
  310. #ifdef _RWSTD_MULTI_THREAD
  311.     _RWSTDGuard guard(this->__stream_mutex);
  312. #endif
  313.     ios_base::fmtflags      old = __fmtfl;
  314.     __fmtfl &= ~mask;
  315.     __fmtfl |= (flgs & mask);
  316.     return old;
  317.   }
  318.  
  319.   /*
  320.    * void unsetf(fmtflags)
  321.    */
  322.  
  323.   inline void 
  324.   ios_base::unsetf(ios_base::fmtflags fg)
  325.   {
  326. #ifdef _RWSTD_MULTI_THREAD
  327.     _RWSTDGuard guard(this->__stream_mutex);
  328. #endif
  329.     __fmtfl &= ~fg;
  330.   }
  331.  
  332.   /*
  333.    * streamsize precision() const
  334.    */
  335.  
  336.   inline streamsize 
  337.   ios_base::precision() const
  338.   {
  339.     return __prec;
  340.   }
  341.  
  342.   /*
  343.    * streamsize precision(streamsize)
  344.    */
  345.   inline streamsize 
  346.   ios_base::precision(streamsize p)
  347.   {
  348. #ifdef _RWSTD_MULTI_THREAD
  349.     _RWSTDGuard guard(this->__stream_mutex);
  350. #endif
  351.     streamsize    temp = __prec;
  352.     __prec = p;
  353.     return temp;
  354.   }
  355.  
  356.   /*
  357.    * streamsize width() const
  358.    */
  359.  
  360.   inline streamsize 
  361.   ios_base::width() const
  362.   {
  363.     return __wide;
  364.   }
  365.  
  366.   /*
  367.    * streamsize width(streamsize)
  368.    */
  369.  
  370.   inline streamsize 
  371.   ios_base::width(streamsize w)
  372.   {
  373. #ifdef _RWSTD_MULTI_THREAD
  374.     _RWSTDGuard guard(this->__stream_mutex);
  375. #endif
  376.     streamsize      i = __wide;
  377.     __wide = w;
  378.     return i;
  379.   }
  380.  
  381.   /*
  382.    *     External declarations for manipulators
  383.    */
  384.  
  385.   inline ios_base& unitbuf(ios_base& strm)
  386.   {
  387.     strm.setf(ios_base::unitbuf);
  388.     return strm;
  389.   }
  390.  
  391.   /*
  392.    * unset the unitbuf mode
  393.    */
  394.  
  395.   inline ios_base& nounitbuf(ios_base& strm)
  396.   {
  397.     strm.unsetf(ios_base::unitbuf);
  398.     return strm;
  399.   }
  400.  
  401.   /*
  402.    * says that bool will be inserted/extracted as
  403.    * false/true depending on locale
  404.    */
  405.  
  406.   inline ios_base& boolalpha(ios_base& strm)
  407.   {
  408.     strm.setf(ios_base::boolalpha);
  409.     return strm;
  410.   }
  411.  
  412.   /*
  413.    * bool is inserted/extraced as 0/1
  414.    v   */
  415.  
  416.   inline ios_base& noboolalpha(ios_base& strm)
  417.   {
  418.     strm.unsetf(ios_base::boolalpha);
  419.     return strm;
  420.   }
  421.  
  422.   /*
  423.    * shows the base of the number e.g. 0777 for oct, 0x for hex
  424.    */
  425.  
  426.   inline ios_base& showbase(ios_base& strm)
  427.   {
  428.     strm.setf(ios_base::showbase);
  429.     return strm;
  430.   }
  431.  
  432.   /*
  433.    * does not show the base
  434.    */
  435.  
  436.   inline ios_base& noshowbase(ios_base& strm)
  437.   {
  438.     strm.unsetf(ios_base::showbase);
  439.     return strm;
  440.   }
  441.  
  442.   /*
  443.    * shows the decimal point on floating insertion even if not necessary
  444.    */
  445.  
  446.   inline ios_base& showpoint(ios_base& strm)
  447.   { 
  448.     strm.setf(ios_base::showpoint);
  449.     return strm;
  450.   }
  451.  
  452.   /*
  453.    * does not show the decimal point if not necessary
  454.    */
  455.  
  456.   inline ios_base& noshowpoint(ios_base& strm)
  457.   {
  458.     strm.unsetf(ios_base::showpoint);
  459.     return strm;
  460.   }
  461.  
  462.   /*
  463.    * prints the sign of the number.  will also print '+' for positive #'s
  464.    */
  465.  
  466.   inline ios_base& showpos(ios_base& strm)
  467.   {
  468.     strm.setf(ios_base::showpos);
  469.     return strm;
  470.   }
  471.  
  472.   /*
  473.    * only shows the sign for negative values
  474.    */
  475.  
  476.   inline ios_base& noshowpos(ios_base& strm)
  477.   {
  478.     strm.unsetf(ios_base::showpos);
  479.     return strm;
  480.   }
  481.  
  482.   /*
  483.    * skips the white space when reading characters
  484.    */
  485.  
  486.   inline ios_base& skipws(ios_base& strm)
  487.   {
  488.     strm.setf(ios_base::skipws);
  489.     return strm;
  490.   }
  491.  
  492.   /*
  493.    * doesn't skip white space when reading characters
  494.    */
  495.  
  496.   inline ios_base& noskipws(ios_base& strm)
  497.   {
  498.     strm.unsetf(ios_base::skipws);
  499.     return strm;
  500.   }
  501.  
  502.   /*
  503.    * prints floating point and hex values in uppercase.
  504.    * e.g. 1.234E05, 0X1B
  505.    */
  506.  
  507.   inline ios_base& uppercase(ios_base& strm)
  508.   {
  509.     strm.setf(ios_base::uppercase);
  510.     return strm;
  511.   }
  512.  
  513.   /*
  514.    * uses lowercase for floating point and hex values
  515.    */
  516.  
  517.   inline ios_base& nouppercase(ios_base& strm)
  518.   {
  519.     strm.unsetf(ios_base::uppercase);
  520.     return strm;
  521.   }
  522.  
  523.   inline ios_base& internal(ios_base& strm)
  524.   {
  525.     strm.setf(ios_base::internal, ios_base::adjustfield);
  526.     return strm;
  527.   }
  528.  
  529.   /*
  530.    * does left justifying
  531.    */
  532.  
  533.   inline ios_base& left(ios_base& strm)
  534.   {
  535.     strm.setf(ios_base::left, ios_base::adjustfield);
  536.     return strm;
  537.   }
  538.  
  539.   /*
  540.    * does right justifying
  541.    */
  542.  
  543.   inline ios_base& right(ios_base& strm)
  544.   {
  545.     strm.setf(ios_base::right, ios_base::adjustfield);
  546.     return strm;
  547.   }
  548.  
  549.   /*
  550.    * prints integer values in decimal
  551.    */
  552.  
  553.   inline ios_base& dec(ios_base& strm)
  554.   {
  555.     strm.setf(ios_base::dec, ios_base::basefield);
  556.     return strm;
  557.   }
  558.  
  559.   /*
  560.    * prints integer values in hexadecimal
  561.    */
  562.  
  563.   inline ios_base& hex(ios_base& strm)
  564.   {
  565.     strm.setf(ios_base::hex, ios_base::basefield);
  566.     return strm;
  567.   }
  568.  
  569.   /*
  570.    * prints integer values in octal
  571.    */
  572.  
  573.   inline ios_base& oct(ios_base& strm)
  574.   {
  575.     strm.setf(ios_base::oct, ios_base::basefield);
  576.     return strm;
  577.   }
  578.  
  579.   /*
  580.    * generate floating point numbers in fixed-point notation
  581.    */
  582.  
  583.   inline ios_base& fixed(ios_base& strm)
  584.   {
  585.     strm.setf(ios_base::fixed, ios_base::floatfield);
  586.     return strm;
  587.   }
  588.  
  589.   /*
  590.    * generates floating-point numbers in scientific notation
  591.    */
  592.  
  593.   inline ios_base& scientific(ios_base& strm)
  594.   {
  595.     strm.setf(ios_base::scientific, ios_base::floatfield);
  596.     return strm;
  597.   }
  598.  
  599.   // end inlining
  600.  
  601. #if defined ( __SUNPRO_CC )
  602.   static ios_base::Init      __rwStreamInit;
  603. #endif
  604.  
  605. #ifndef _RWSTD_NO_NAMESPACE
  606. }
  607. #endif
  608.  
  609. #endif // __STD_IOSBASE__ 
  610. #pragma option pop
  611. #endif /* __IOSBASE_H */
  612.