home *** CD-ROM | disk | FTP | other *** search
/ PC Format (South-Africa) 2001 June / PCFJune.iso / Xenon / C++ / FreeCommandLineTools.exe / Include / fstream.cc < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-31  |  36.2 KB  |  1,443 lines

  1. #ifndef __FSTREAM_CC
  2. #define __FSTREAM_CC
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. /***************************************************************************
  5.  *
  6.  * fstream.cc - Definition for the Standard Library file streams
  7.  *
  8.  ***************************************************************************
  9.  *
  10.  * Copyright (c) 1994-1999 Rogue Wave Software, Inc.  All Rights Reserved.
  11.  *
  12.  * This computer software is owned by Rogue Wave Software, Inc. and is
  13.  * protected by U.S. copyright laws and other laws and by international
  14.  * treaties.  This computer software is furnished by Rogue Wave Software,
  15.  * Inc. pursuant to a written license agreement and may be used, copied,
  16.  * transmitted, and stored only in accordance with the terms of such
  17.  * license and with the inclusion of the above copyright notice.  This
  18.  * computer software or any other copies thereof may not be provided or
  19.  * otherwise made available to any other person.
  20.  *
  21.  * U.S. Government Restricted Rights.  This computer software is provided
  22.  * with Restricted Rights.  Use, duplication, or disclosure by the
  23.  * Government is subject to restrictions as set forth in subparagraph (c)
  24.  * (1) (ii) of The Rights in Technical Data and Computer Software clause
  25.  * at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
  26.  * Commercial Computer Software รป Restricted Rights at 48 CFR 52.227-19,
  27.  * as applicable.  Manufacturer is Rogue Wave Software, Inc., 5500
  28.  * Flatiron Parkway, Boulder, Colorado 80301 USA.
  29.  *
  30.  **************************************************************************/
  31.  
  32. #ifndef _RWSTD_NO_NEW_HEADER
  33. #include <cstdio>
  34. #else
  35. #include <stdio.h>
  36. #endif // _RWSTD_NO_NEW_HEADER
  37.  
  38. #ifndef _RWSTD_STRICT_ANSI
  39. #include <fcntl.h>
  40. #endif
  41.  
  42. #ifndef _RWSTD_NO_NAMESPACE
  43. namespace std {
  44. #endif
  45.  
  46. #ifdef __BORLANDC__
  47.   template <class T>
  48.   void __io_initialize (T& t, T val) { t = val; }
  49. #pragma option -w-par
  50. #endif
  51.  
  52.   /*
  53.    * class basic_filebuf
  54.    */
  55.  
  56.   /*
  57.    * basic_filebuf()
  58.    */
  59.  
  60.   template<class charT, class traits>
  61.   basic_filebuf<charT, traits>::basic_filebuf()
  62.   : basic_streambuf<charT, traits>()
  63.   , __file(__closed())
  64.   , __data(0)
  65.   , __read_buff(false)
  66.   , __write_buff(false)
  67.   , __last_numb_read(0)
  68.   , __rwBufferSize(512)
  69.   , __absolute_pos(0)
  70.   {
  71.     __state_beg = new state_t;
  72.     __state_end = new state_t;
  73.   }
  74.  
  75.   /*
  76.    * basic_filebuf(__file_type)
  77.    */
  78.  
  79.   template<class charT, class traits>
  80.   basic_filebuf<charT, traits>::basic_filebuf(__file_type fd)
  81.   : basic_streambuf<charT, traits>()
  82.   , __file(fd)
  83.   , __read_buff(false)
  84.   , __write_buff(false)
  85.   , __last_numb_read(0)
  86.   , __rwBufferSize(512)
  87.   , __absolute_pos(0)
  88.   {
  89.     __data = new char_type[__rwBufferSize+1];
  90.     __state_beg = new state_t;
  91.     __state_end = new state_t;
  92.  
  93. #ifndef _RWSTD_STRICT_ANSI
  94.     if ( fd == 0 )
  95.       basic_streambuf<charT,traits>::mode_ = ios_base::in;
  96.     else
  97.     {
  98.       if ( fd < 3 )
  99.         basic_streambuf<charT,traits>::mode_ = ios_base::out;
  100.     }
  101. #else
  102.     if ( fd == stdin )
  103.       basic_streambuf<charT,traits>::mode_ = ios_base::in;
  104.     else
  105.     {
  106.       if ( __file_is_std(fd) )
  107.         basic_streambuf<charT,traits>::mode_ = ios_base::out;
  108.     }
  109. #endif // _RWSTD_STRICT_ANSI
  110.  
  111.   }
  112.   /*
  113.    * ~basic_filebuf()
  114.    */
  115.  
  116.   template<class charT, class traits>
  117.   basic_filebuf<charT, traits>::~basic_filebuf()
  118.   {
  119.     close();
  120.     delete __state_beg;
  121.     delete __state_end;
  122.   }
  123.  
  124.   /*
  125.    * bool is_open() const
  126.    */
  127.  
  128.   template<class charT, class traits>
  129.   bool basic_filebuf<charT, traits>::is_open() const
  130.   {
  131. #ifndef _RWSTD_STRICT_ANSI
  132.     return !(__file == -1);
  133. #else
  134.     return !(__file == 0);
  135. #endif
  136.   }
  137.  
  138.   /*
  139.    * basic_filebuf *open(int fd)
  140.    * fstream compatibility
  141.    */
  142.  
  143. #ifndef _RWSTD_NO_EXTENSION
  144.  
  145.   template<class charT, class traits>
  146.   basic_filebuf<charT, traits> *
  147.   basic_filebuf<charT, traits>::
  148.   open(int fd )
  149.   {   
  150.     if(__file != __closed())
  151.       return 0;
  152.  
  153.     this->streambuf_init(false); 
  154.  
  155.     __file           = fd;
  156.     __read_buff      = false;
  157.     __write_buff     = false;
  158.     __last_numb_read = 0; 
  159.  
  160.     delete [] __data;
  161.     __data = new char_type[__rwBufferSize+1];
  162.  
  163.     if ( fd == 0 )
  164.       basic_streambuf<charT,traits>::mode_ = ios_base::in;
  165.     else
  166.     {
  167.       if ( fd < 3 )
  168.         basic_streambuf<charT,traits>::mode_ = ios_base::out;
  169.     }
  170.     return this;
  171.   }
  172. #endif // _RWSTD_NO_EXTENSION
  173.   /*
  174.    * basic_filebuf *open(const char *, ios_base::openmode, long )
  175.    * opens a file and allocates a buffer
  176.    */
  177.  
  178.   template<class charT, class traits>
  179.   basic_filebuf<charT, traits> *
  180.   basic_filebuf<charT, traits>::
  181.   open(const char *s, ios_base::openmode mode, long protection)
  182.   {
  183.     long    m;
  184.     if ( mode & ios_base::ate )
  185. //      m = mode & (~ios_base::ate);  // RW_BUG
  186.       m = mode & (~ios_base::ate) | ios_base::app; // RW_BUG: fix for bts-46733
  187.     else
  188.       m = mode;
  189. #ifndef _RWSTD_STRICT_ANSI
  190.     // Unset the nocreate/noreplace flags in case they exist,
  191.     // these flags are not in the __RWSTD::__rwOpenTable table
  192.     m = m & (~ios_base::nocreate);
  193.     m = m & (~ios_base::noreplace);
  194. #endif
  195.     if(__file != __closed())
  196.       return 0;
  197.  
  198.     basic_streambuf<charT,traits>::mode_ = mode;
  199.     this->streambuf_init(false);
  200.  
  201. #ifndef _RWSTD_STRICT_ANSI
  202.     if(__RWSTD::__rwOpenTable[m] == -1)
  203.       return 0;
  204.     int mm = __RWSTD::__rwOpenTable[m];
  205.     // now do the right thing for nocreate/noreplace
  206.     if ( mode & ios_base::out ) {
  207.          if ( mode & ios_base::nocreate )
  208.          mm &= ~O_CREAT;
  209.        if ( mode & ios_base::noreplace )
  210.          mm |= O_EXCL;
  211.     }
  212.     if((__file = ::open(s, mm, protection)) == __closed())
  213.       return 0;
  214. #else
  215.     if(__RWSTD::__rwOpenTable[m] == 0)
  216.       return 0;
  217.     if((__file = fopen(s, __RWSTD::__rwOpenTable[m])) == __closed())
  218.       return 0;
  219.     setvbuf(__file,0,_IONBF,0);
  220. #endif // _RWSTD_STRICT_ANSI
  221.  
  222.     delete [] __data;
  223.     __data = new char_type[__rwBufferSize+1];
  224.  
  225.     if(mode & ios_base::ate)
  226.     {
  227.       if( (__absolute_pos = __file_seek(__file, 0, SEEK_END))== -1  )
  228.       {
  229.         close();
  230.         delete [] __data;  
  231.         __data = 0;
  232.         __file = __closed();
  233.         return 0;
  234.       }
  235.     }
  236.     return this;
  237.   }
  238.  
  239.   /*
  240.    * basic_filebuf *close()
  241.    * closes a file if one was already open.  Also deletes the
  242.    * data, if it was allocated
  243.    */
  244.  
  245.   template<class charT, class traits>
  246.   basic_filebuf<charT, traits> *
  247.   basic_filebuf<charT, traits>::close()
  248.   {
  249.     delete __state_beg;
  250.     delete __state_end;
  251.     __state_beg = new state_t;
  252.     __state_end = new state_t;
  253.  
  254.     __absolute_pos = 0;
  255.  
  256.     if ( (__file_is_std(__file))  && (__file != __closed()) )
  257.     {
  258.       if ( __write_buff )
  259.         overflow( traits::eof() );
  260.       __read_buff=false;
  261.       __write_buff=false;
  262.       __last_numb_read=0;
  263.       return this;
  264.     }
  265.    
  266.     if(__file != __closed())
  267.     {          
  268.       if ( __write_buff )
  269.         overflow( traits::eof() );
  270.       delete [] __data;
  271.       __data = 0;
  272.       __read_buff=false;
  273.       __write_buff=false;
  274.       __last_numb_read=0;
  275.  
  276.  
  277.       if(__file_close(__file) == -1)
  278.         return 0;
  279.  
  280.       __file = __closed();
  281.  
  282.       return this;
  283.     }
  284.     return 0;          
  285.   }
  286.  
  287.   /*
  288.    * streamsize showmanyc()
  289.    */
  290.  
  291.   template<class charT, class traits>
  292.   streamsize
  293.   basic_filebuf<charT, traits>::showmanyc()
  294.   { 
  295.     if ( this->gptr() )
  296.       return (int)(this->egptr()-this->gptr());
  297.    
  298.     if ( this->pptr() && __read_buff )
  299.       return (streamsize)(this->epptr()-this->pptr());   
  300.  
  301.     return 0;
  302.  
  303.   }
  304.  
  305.   /*
  306.    * int_type overflow(int_type)
  307.    * this is called when the put pointer overruns the buffer.
  308.    * it should flush what was in the put buffer, and move the pointer
  309.    * back to the beginning
  310.    */
  311.  
  312.   template<class charT, class traits>
  313.   _TYPENAME basic_filebuf<charT, traits>::int_type
  314.   basic_filebuf<charT, traits>::overflow(int_type c)
  315.   {
  316.     long  count = this->pptr() - this->pbase();
  317.     bool   do_noconv;
  318.  
  319.     if(__file == __closed())        
  320.       return traits::eof();
  321.  
  322.     if ( (this->pptr()==0) && this->gptr() )
  323.     {
  324.       this->setp(this->eback(),this->eback()+__rwBufferSize);
  325.       pbump((int)(this->gptr()-this->eback()));
  326.       this->setg(0,0,0);
  327.       if ( this->pptr()<this->epptr() )
  328.       { 
  329.         if( !traits::eq_int_type(c,traits::eof()) ) 
  330.         {
  331.           sputc( traits::to_char_type(c) );
  332.           this->gbump(1);
  333.           __write_buff=true;
  334.           return traits::to_int_type(*this->pptr());
  335.         }
  336.       } 
  337.       count=  this->pptr() - this->pbase();
  338.     }
  339.  
  340.     if ( (this->pptr()==0) && (this->gptr()==0) )
  341.       this->setp(__data,__data+ __rwBufferSize);  
  342.  
  343.     __write_buff= false;
  344.  
  345.     if(count)
  346.     {        
  347. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  348.       do_noconv = use_facet< codecvt<charT,char,state_t> >(this->getloc())
  349. #else
  350.       do_noconv = use_facet( this->getloc(), (codecvt<charT,char,state_t>* )0)
  351. #endif
  352.       .always_noconv();
  353.  
  354.       if ( do_noconv )
  355.       {
  356.         if ( __read_buff ) 
  357.         {
  358.           if ( __file_seek(__file, -__last_numb_read, SEEK_CUR) == -1 )
  359.             return traits::eof();
  360.         }
  361.         if(!__file_write(__file,(char *)this->pbase(),sizeof(charT),count))
  362.           return traits::eof();
  363.       }
  364.       else
  365.       {
  366.         long size_to = count*sizeof(charT);
  367.         const charT *from_next =0;
  368.         char *to= new char[size_to];
  369.         char *to_next = 0;
  370.         codecvt_base::result conv_result;
  371.  
  372.         if ( __read_buff )
  373.         {
  374.           if ( __file_seek(__file, -__last_numb_read, SEEK_CUR) == -1 )
  375.             return traits::eof();
  376.           else
  377.           {
  378.             state_t *tmp = __state_beg;
  379.             __state_beg = __state_end;
  380.             __state_end = tmp;
  381.           }
  382.         }
  383.  
  384.         *__state_beg = *__state_end;
  385.  
  386.         do {
  387.  
  388. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  389.           conv_result = use_facet< codecvt<charT,char,state_t> >(this->getloc())
  390. #else
  391.           conv_result = use_facet( this->getloc(), (codecvt<charT,char,state_t>* )0)
  392. #endif
  393.           .out(*__state_end,this->pbase(),this->pptr(),from_next,
  394.                to,to+size_to,to_next); 
  395.  
  396.           if ( conv_result == codecvt_base::noconv ) break;
  397.  
  398.           if (  (conv_result == codecvt_base::partial) || ( from_next != this->pptr() ) )
  399.           {
  400.             size_to *= 2;
  401.             char *to_bis = new char[size_to];
  402.             long diff_to = to_next - to;
  403.             memcpy(to_bis,to,diff_to);
  404.             delete [] to;
  405.             to = to_bis;
  406.             to_next = to + diff_to;
  407.           }
  408.  
  409.         }while( (conv_result == codecvt_base::partial) || ( from_next != this->pptr()));
  410.  
  411.         if ( conv_result==codecvt_base::error )
  412.         {
  413.           delete [] to;
  414.           return traits::eof();
  415.         }
  416.  
  417.         if (conv_result == codecvt_base::noconv) 
  418.         {
  419.           if(!__file_write(__file, (char*)this->pbase(), sizeof(charT), count))
  420.           {
  421.             delete [] to;
  422.             return traits::eof();              
  423.           }
  424.         } 
  425.         else 
  426.         {
  427.           if(!__file_write(__file, to, sizeof(char),to_next-to))
  428.           {
  429.             delete [] to;
  430.             return traits::eof();                
  431.           }
  432.         }
  433.         delete [] to;
  434.       }
  435.     }
  436.  
  437.     this->setp(__data, __data+__rwBufferSize);    
  438.     this->setg(0,0,0);                       
  439.     __read_buff = false;              
  440.     __write_buff= false; 
  441.  
  442.     __absolute_pos = __file_seek(__file,0,SEEK_CUR);
  443.  
  444.     if( !traits::eq_int_type(c,traits::eof()) ) 
  445.     {
  446.       sputc(traits::to_char_type(c));
  447.       this->gbump(1);
  448.       __write_buff = true;
  449.     }
  450.     else
  451.       this->setp(0,0);
  452.  
  453.     return traits::not_eof(c);
  454.   }
  455.  
  456.   /*
  457.    * int_type pbackfail(int_type)
  458.    */
  459.  
  460.   template<class charT, class traits>
  461.   _TYPENAME basic_filebuf<charT, traits>::int_type
  462.   basic_filebuf<charT, traits>::pbackfail(int_type c)
  463.   {
  464.     if(__file == __closed())
  465.       return traits::eof();
  466.  
  467.     if ( (this->gptr()==0) && this->pptr() )
  468.     {
  469.       if ( __read_buff )
  470.         this->setg(this->pbase(),this->pptr(),this->epptr());
  471.       else
  472.         this->setg(this->pbase(),this->pptr(),this->pptr()); 
  473.     }
  474.  
  475.     if ( (!traits::eq_int_type(c,traits::eof())) && (this->gptr()>this->eback()) )
  476.     {
  477.       if ( traits::eq(*(this->gptr()-1),traits::to_char_type(c)) ) 
  478.       {
  479.         this->gbump(-1);
  480.         return c;
  481.       }
  482.       else
  483.       {
  484.         this->gbump(-1);
  485.         *this->gptr()=traits::to_char_type(c);
  486.         return c;
  487.       }
  488.     }       
  489.  
  490.     if ( (traits::eq_int_type(c,traits::eof())) && (this->gptr()>this->eback()) )  
  491.     { 
  492.       this->gbump(-1);
  493.       return traits::not_eof(c);
  494.     }
  495.                                                 
  496.     return traits::eof();
  497.   }
  498.  
  499.   /*
  500.    * basic_streambuf<charT,traits>* setbuf(char_type*,streamsize)
  501.    */
  502.  
  503.   template<class charT, class traits>
  504.   basic_streambuf<charT, traits>*
  505.   basic_filebuf<charT, traits>::setbuf(char_type *s, streamsize n)
  506.   {
  507.  
  508.     if (n>0)
  509.     { 
  510.       if ( __file != __closed() )
  511.       {
  512.         if ( !traits::eq_int_type(overflow( traits::eof() ),traits::eof()) )
  513.         {
  514.           if ( s )
  515.           { 
  516.             delete [] __data;
  517.             __rwBufferSize = n-1; 
  518.             __data=s;
  519.           }
  520.           else
  521.           {
  522.             charT *tmp;
  523.             __rwBufferSize = n;  
  524.             tmp = new char_type[__rwBufferSize+1];
  525.             delete [] __data;
  526.             __data = tmp;
  527.           }
  528.           this->setp(0,0);    
  529.           this->setg(0,0,0);                         
  530.           __read_buff=false;
  531.           __write_buff=false;
  532.         }
  533.       }
  534.       else
  535.       {
  536.         if ( s )
  537.         {
  538.           __rwBufferSize = n-1;
  539.           __data =s;
  540.           this->setp(0,0);    
  541.           this->setg(0,0,0);                        
  542.           __write_buff= false;
  543.           __read_buff = false; 
  544.         }
  545.         else
  546.           __rwBufferSize = n;
  547.       }  
  548.     } 
  549.     return (basic_streambuf<charT,traits>*)(this); 
  550.   }
  551.   /*
  552.    * int_type underflow()
  553.    * this is called when the get pointer "underflows" the buffer,
  554.    * or when there are no more "get" characters.
  555.    */
  556.  
  557.   template<class charT, class traits>
  558.   _TYPENAME basic_filebuf<charT, traits>::int_type
  559.   basic_filebuf<charT, traits>::underflow()
  560.   {
  561.     bool do_noconv;
  562.     long offset;
  563. #ifndef __BORLANDC__
  564.     offset = 0;
  565. #else
  566.     __io_initialize(offset,0L);
  567. #endif
  568.  
  569.     if(__file == __closed())
  570.       return traits::eof();
  571.  
  572.     if ( (this->gptr()==0) && this->pptr() )
  573.     {
  574.       if ( __read_buff )
  575.         this->setg(this->pbase(),this->pptr(),this->epptr());
  576.       else
  577.         this->setg(this->pbase(),this->pptr(),this->pptr());
  578.  
  579.       this->setp(0,0);
  580.     }
  581.  
  582.     if(this->gptr() && (this->gptr()<this->egptr()) ) return traits::to_int_type(*this->gptr());     
  583.  
  584.     if( ((__write_buff) && this->gptr() ) || ((__write_buff) && this->pptr() ) )
  585.     {
  586.       if ( traits::eq_int_type(overflow( traits::eof() ),traits::eof()) ) 
  587.         return traits::eof();
  588.       this->setg(this->pbase(),this->pptr(),this->epptr());
  589.       this->setp(0,0);
  590.       __write_buff = false; 
  591.     }                 
  592.  
  593.     __absolute_pos = __file_seek(__file,0,SEEK_CUR);
  594.  
  595. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  596.     do_noconv = use_facet< codecvt<charT,char,state_t> >(this->getloc())
  597. #else
  598.     do_noconv = use_facet( this->getloc(), (codecvt<charT,char,state_t>* )0)
  599. #endif
  600.       .always_noconv();
  601.  
  602.     if ( do_noconv )
  603.     {
  604.       streamoff last_numb_read = __file_read(__file, (char *)__data,sizeof(charT),__rwBufferSize);
  605.       if( last_numb_read <= 0 ) 
  606.         return traits::eof();
  607.       offset = last_numb_read/sizeof(charT);
  608.       __last_numb_read = __get_chars(__data,__data + last_numb_read);
  609.     }
  610.     else
  611.     {
  612.       long from_size = __rwBufferSize*sizeof(charT);
  613.       const char *from_next =0;
  614.       char *from= new char[from_size];
  615.       charT *to_next = 0;
  616.       codecvt_base::result conv_result;
  617.  
  618.       streamoff last_numb_read = 0;
  619.       long new_numb_read;
  620.  
  621.       do {
  622.         new_numb_read = __file_read(__file, from+last_numb_read
  623.                              ,sizeof(char),__rwBufferSize);
  624.  
  625.         if( new_numb_read <= 0 ) 
  626.           break;
  627.  
  628.         last_numb_read += new_numb_read;
  629.         *__state_beg = *__state_end;
  630.  
  631. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  632.         conv_result = use_facet< codecvt<charT,char,state_t> >(this->getloc())
  633. #else
  634.         conv_result = use_facet(this->getloc(), (codecvt<charT,char,state_t>* )0)
  635. #endif
  636.         .in(*__state_end,from,from+last_numb_read,from_next,__data,
  637.             __data+__rwBufferSize,to_next); 
  638.  
  639.         if ( (conv_result == codecvt_base::error) ||
  640.              (conv_result == codecvt_base::noconv) ) break;
  641.  
  642.         if (  (to_next != (__data + __rwBufferSize) ) &&
  643.               ( (size_t)new_numb_read == __rwBufferSize*sizeof(charT) ) )
  644.         {
  645.           from_size *= 2;
  646.           char *from_bis = new char[from_size];
  647.           long diff_from = from_next - from;
  648.           memcpy(from_bis,from,last_numb_read);
  649.           delete [] from;
  650.           from = from_bis;
  651.           from_next = from + diff_from;
  652.         }
  653.  
  654.       } while ( (to_next != (__data + __rwBufferSize) ) &&
  655.                 ( (size_t)new_numb_read == __rwBufferSize*sizeof(charT) ) ); 
  656.  
  657.       if( last_numb_read <= 0 ) 
  658.         return traits::eof();
  659.       __last_numb_read = last_numb_read;
  660.                
  661.       if ( (conv_result==codecvt_base::error) ||
  662.            (conv_result==codecvt_base::partial) )
  663.       {
  664.         delete [] from;
  665.         return traits::eof();
  666.       } 
  667.       else 
  668.       {
  669.         if (conv_result==codecvt_base::noconv)
  670.         {
  671.           memcpy((char*)__data,from,__last_numb_read);
  672.           offset = __last_numb_read;
  673.         }
  674.         else
  675.         {
  676.           long diff_minus = new_numb_read - (from_next - from);
  677.           if ( __file_seek(__file, -diff_minus, SEEK_CUR) == -1 )
  678.             return traits::eof();
  679.           __last_numb_read -= diff_minus;
  680.           offset = to_next - __data;
  681.         }
  682.       }
  683.  
  684.       delete [] from;
  685.     }
  686.         
  687.     if(__last_numb_read) 
  688.     {   
  689.       this->setg(__data, __data, __data+offset); 
  690.       this->setp(0, 0);
  691.       __read_buff=true;
  692.       return traits::to_int_type(*this->gptr());
  693.     }
  694.  
  695.     return traits::eof();
  696.   }
  697.  
  698.   /*
  699.    * pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode)
  700.    */
  701.  
  702.   template<class charT, class traits>
  703.   _TYPENAME basic_filebuf<charT, traits>::pos_type
  704.   basic_filebuf<charT, traits>::seekoff(off_type off,
  705.                                         ios_base::seekdir way,
  706.                                         ios_base::openmode which)
  707.   {
  708.     off_type            loff = off;
  709.     pos_type            newoff;
  710.  
  711.     if(__file == __closed())
  712.       return pos_type(off_type(-1));
  713.  
  714.     if ( (off == off_type(-1)) && (way==ios_base::beg) )
  715.       return pos_type(off_type(-1));
  716.  
  717.     if ( (way==ios_base::cur) )
  718.     {
  719.       streamsize remain_in_buffer=0;
  720.       charT * remain_start=0;
  721.       charT * remain_end=0;
  722.      
  723.       if ( __read_buff )
  724.       {
  725.         if ( this->gptr() )  
  726.         {
  727.           if (!this->eback())
  728.             remain_in_buffer= this->egptr()-this->gptr();
  729.           else
  730.             remain_in_buffer= __get_chars(this->gptr(),this->egptr());
  731.           remain_start = this->gptr();
  732.           remain_end = this->egptr();
  733.         }
  734.         else
  735.         {
  736.           if ( this->pbase() )  
  737.           {
  738.             remain_in_buffer= __get_chars(this->pptr(),this->epptr());
  739.             remain_start = this->pptr();
  740.             remain_end = this->epptr();
  741.           }
  742.         }
  743.       }
  744.       else 
  745.       {
  746.         if ( __write_buff )
  747.         {
  748.           if ( this->pbase() )  
  749.           {
  750.             remain_in_buffer= -(__get_chars(this->pbase(),this->pptr()));
  751.             remain_start = this->pbase();
  752.             remain_end = this->pptr();
  753.           }
  754.  
  755.           else
  756.           { 
  757.             if ( this->eback() )  
  758.             {
  759.               remain_in_buffer= -(__get_chars(this->eback(),this->gptr()));
  760.               remain_start = this->eback();
  761.               remain_end = this->gptr();
  762.             }
  763.           }
  764.         }     
  765.       }
  766.  
  767.       int const_conv;
  768.  
  769. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  770.       const_conv = use_facet< codecvt<charT,char,state_t> >(this->getloc())
  771. #else
  772.       const_conv = use_facet( this->getloc(), (codecvt<charT,char,state_t>* )0)
  773. #endif
  774.       .encoding(); 
  775.  
  776.       // If characters are not a constant size can't seek from current position
  777.       if ( (off != off_type(0)) && (const_conv <= 0) ) {
  778.         return pos_type(off_type(-1));
  779.       }
  780.  
  781.       if ( const_conv > 0 )
  782.       {
  783.         if (off !=off_type(0)) {
  784.           if (__write_buff ) {
  785.             overflow( traits::eof() );
  786.             __write_buff = false;
  787.             return ( __file_seek(__file, loff*const_conv,SEEK_CUR)/const_conv);
  788.           }
  789.           this->setp(0,0);
  790.           this->setg(0,0,0);
  791.           return ( __file_seek(__file, -((remain_in_buffer-loff)*const_conv), SEEK_CUR)/const_conv);
  792.         }
  793.         return ( (__file_seek(__file,0,SEEK_CUR)-remain_in_buffer*const_conv)/const_conv);
  794.       }
  795.       else
  796.       {
  797.         // First find current position in file
  798.         newoff = __file_seek(__file, 0, SEEK_CUR);
  799.  
  800.         // If necessary convert characters in buffer to 
  801.         // find offset from file position
  802.         if (remain_in_buffer != 0 && this->eback())
  803.         {
  804.           long to_size = __rwBufferSize;
  805.           const charT *from = remain_start;
  806.           const charT *from_next = from;
  807.           const charT *from_end = remain_end;
  808.           char *to_base = new char[to_size];
  809.           char *to = to_base;
  810.           char *to_next = to;
  811.           char *to_end = to + to_size;
  812.           codecvt_base::result conv_result;
  813.  
  814.           do {
  815.             *__state_beg = *__state_end;
  816.  
  817. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  818.             conv_result = use_facet< codecvt<charT,char,state_t> >(this->getloc())
  819. #else
  820.             conv_result = use_facet(this->getloc(), (codecvt<charT,char,state_t>* )0)
  821. #endif
  822.              .out(*__state_end,from,from_end,from_next,to,to_end,to_next); 
  823.  
  824.             if ( (conv_result == codecvt_base::error) ||
  825.                (conv_result == codecvt_base::noconv) ) break;
  826.  
  827.             if ( to_next == to_end && from_next != from_end )
  828.             {
  829.               long diff_to = to_next - to_base;
  830.               to_size *= 2;
  831.               to = new char[to_size];
  832.               memcpy(to,to_base,diff_to);
  833.               delete [] to_base;
  834.               to_base = to;
  835.               to = to_base + diff_to;
  836.               to_next = to;
  837.               from = from_next;
  838.               to_end = to_base + to_size;
  839.             }
  840.  
  841.           } while ( from_next != from_end );
  842.           
  843.           // Calculate new file position
  844.           // The  + remain_in_buffer - ... stuff is used to deal
  845.           // with WIN32 CR/LF translations (see __get_chars())
  846.           if (remain_in_buffer > 0)
  847.             newoff -= (to_next - to_base) 
  848.                       + (remain_in_buffer - (remain_end-remain_start));
  849.           else
  850.             newoff += (to_next - to_base)
  851.                       + (-remain_in_buffer - (remain_end-remain_start));
  852.  
  853.           delete [] to;
  854.         }
  855.         return newoff;
  856.       }
  857.     } 
  858.  
  859.     int const_conv;
  860.  
  861. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  862.     const_conv = use_facet< codecvt<charT,char,state_t> >(this->getloc())
  863. #else
  864.     const_conv = use_facet( this->getloc(), (codecvt<charT,char,state_t>* )0)
  865. #endif
  866.     .encoding(); 
  867.  
  868.     if ( const_conv < 1 )
  869.     {
  870.       if ( (way==ios_base::beg) && (off==off_type(0)) )
  871.       {
  872.         seekpos(0);
  873.       }
  874.       else 
  875.         return pos_type(off_type(-1));
  876.     }
  877.  
  878.     if ( __write_buff)
  879.     {  
  880.       overflow( traits::eof() );
  881.       __write_buff =false;  
  882.     }
  883.  
  884.     int w = (way == ios_base::beg) ?
  885.           SEEK_SET : ((way == ios_base::cur) ? SEEK_CUR : SEEK_END);  
  886.     newoff = __file_seek(__file, loff*const_conv, w);
  887.  
  888.     this->setp(0,0);
  889.     this->setg(0,0,0);
  890.  
  891.     return newoff;
  892.   }
  893.  
  894.   /*
  895.    * pos_type seekpos(pos_type, ios_base::openmode)
  896.    */
  897.  
  898.   template<class charT, class traits>
  899.   _TYPENAME basic_filebuf<charT, traits>::pos_type
  900.   basic_filebuf<charT, traits>::
  901.   seekpos(pos_type sp, ios_base::openmode which)
  902.   { 
  903.     if(__file == __closed())
  904.       return pos_type(off_type(-1));
  905.  
  906.     if( sp == pos_type(off_type(-1)) )
  907.       return pos_type(off_type(-1)); 
  908.  
  909.     int const_conv;
  910.  
  911. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  912.     const_conv = use_facet< codecvt<charT,char,state_t> >(this->getloc())
  913. #else
  914.     const_conv = use_facet( this->getloc(), (codecvt<charT,char,state_t>* )0)
  915. #endif
  916.     .encoding();
  917.  
  918.     if ( const_conv > 0 )
  919.       return seekoff(off_type(sp), ios_base::beg, which);
  920.  
  921.     _TYPENAME traits::off_type end_pos = __file_seek(__file,0,SEEK_END);
  922.    
  923.     if ( __file_seek(__file, sp, SEEK_SET) == -1 )
  924.       return pos_type(off_type(-1));
  925.  
  926.     this->setp(0,0);
  927.     this->setg(0,0,0); 
  928.  
  929.     *__state_beg = *__state_end = sp.state();
  930.  
  931.     if ( traits::eq_int_type(underflow(),traits::eof()) && (end_pos > sp))
  932.       return pos_type(off_type(-1));
  933.  
  934.     return sp;   
  935.   }
  936.  
  937.   /*
  938.    * int sync()
  939.    * this synchronizes the buffers and their streams
  940.    */
  941.  
  942.   template<class charT, class traits>
  943.   int basic_filebuf<charT, traits>::sync()
  944.   {
  945.     if ( this->which_open_mode( ) & ios_base::out )
  946.     {
  947. #ifndef _RWSTD_STRICT_ANSI
  948.       if ( __file == 1 || __file == 2)
  949. #else
  950.       if ( __file == stdout || __file == stderr )
  951. #endif
  952.       {
  953.         if( traits::eq_int_type(overflow(traits::eof()),traits::eof()) ) 
  954.           return -1;
  955.       }
  956.       else
  957.       { 
  958.         pos_type p_out = seekoff(0,ios_base::cur,ios_base::out);
  959.         pos_type tmp_out = __file_seek(__file,0,SEEK_CUR);
  960.         pos_type end_out = __file_seek(__file,0,SEEK_END);
  961.         __file_seek(__file,tmp_out,SEEK_SET);
  962.         if ( end_out > p_out )
  963.         {
  964.           if( seekpos(p_out) == pos_type(off_type(-1)))
  965.             return -1;
  966.         }
  967.         else if (__write_buff)
  968.         {
  969.           if( traits::eq_int_type(overflow(traits::eof()),traits::eof()) )
  970.             return -1;
  971.         }
  972.       }
  973.     }
  974.  
  975.     if ( this->which_open_mode( ) & ios_base::in )
  976.     {
  977. #ifndef _RWSTD_STRICT_ANSI
  978.       if ( __file != 0 )
  979. #else
  980.       if ( __file != stdin )
  981. #endif
  982.       {
  983.         pos_type p_in = seekoff(0,ios_base::cur,ios_base::in);
  984.         pos_type tmp_in = __file_seek(__file,0,SEEK_CUR);
  985.         pos_type end_in = __file_seek(__file,0,SEEK_END);
  986.         __file_seek(__file,tmp_in,SEEK_SET);
  987.         if ( end_in > p_in )
  988.         {
  989.           if( seekpos(p_in) == pos_type(off_type(-1)))
  990.             return -1;
  991.         }
  992.       }
  993.     }
  994.  
  995.     return 0;
  996.   }
  997.   /*
  998.    * streamsize xsputn(const char_type *, streamsize)
  999.    */
  1000.  
  1001.   template<class charT, class traits>
  1002.   streamsize basic_filebuf<charT, traits>::
  1003.   xsputn(const char_type *s, streamsize n)
  1004.   {
  1005.     if ( !s || (n == 0) ) return 0;
  1006.  
  1007.     if ( n >  __rwBufferSize )
  1008.     {
  1009.  
  1010.       if ( traits::eq_int_type(overflow( traits::eof() ),traits::eof()) )
  1011.         return 0;
  1012.  
  1013.       char_type   *eback_s = this->eback();
  1014.       char_type   *gptr_s  = this->gptr();
  1015.       char_type   *egptr_s = this->egptr();
  1016.  
  1017.       char_type   *pbase_s = this->pbase();
  1018.       char_type   *pptr_s  = this->pptr();
  1019.       char_type   *epptr_s = this->epptr();
  1020.  
  1021.       this->setg(0,0,0);
  1022.       this->setp((char_type *)s,(char_type *)(s+n));
  1023.       basic_streambuf<charT, traits>::pbump(n);
  1024.  
  1025.       if ( traits::eq_int_type(overflow( traits::eof() ),traits::eof()) )
  1026.         return 0; 
  1027.   
  1028.       this->setg(eback_s,gptr_s,egptr_s);
  1029.       this->setp(pbase_s,epptr_s);
  1030.  
  1031.       pbump(pptr_s-pbase_s); 
  1032.  
  1033.       return n;
  1034.     }
  1035.     else
  1036.     {
  1037.       int         i=0;
  1038.       while((i < n) && ( !traits::eq_int_type(sputc(*s++),traits::eof())))
  1039.         i++;
  1040.       return i;
  1041.     }
  1042.  
  1043.   }
  1044.  
  1045.  
  1046.   /*
  1047.    * class basic_ifstream : public basic_istream
  1048.    */
  1049.  
  1050.   /*
  1051.    * basic_ifstream()
  1052.    */
  1053.  
  1054.   template<class charT, class traits>
  1055.   basic_ifstream<charT, traits>::basic_ifstream()
  1056.   : basic_istream<charT, traits>( )
  1057.   {
  1058.     init(&__fb);
  1059.   }
  1060.  
  1061.   /*
  1062.    * basic_ifstream(const char *, ios_base::openmode, long)
  1063.    * opens a filebuf for (most likely) reading
  1064.    */
  1065.  
  1066.   template<class charT, class traits>
  1067.   basic_ifstream<charT, traits>::basic_ifstream(const char *s,
  1068.                                                 ios_base::openmode mode,
  1069.                                                 long protection )
  1070.   : basic_istream<charT, traits>( )
  1071.   {
  1072.     init(&__fb);
  1073.     open(s, mode, protection);
  1074.   }
  1075.  
  1076.   /*
  1077.    * basic_ifstream(int fd)
  1078.    * opens a filebuf for reading
  1079.    */
  1080. #ifndef _RWSTD_NO_EXTENSION
  1081.  
  1082.   template<class charT, class traits>
  1083.   basic_ifstream<charT, traits>::
  1084.   basic_ifstream(int fd)
  1085.   : basic_istream<charT, traits>( )
  1086.   {
  1087.  
  1088.     init(&__fb);
  1089.  
  1090.     if ( !__fb.open(fd) ) 
  1091.       this->setstate(ios_base::failbit);
  1092.   }
  1093.  
  1094.   /*
  1095.    * basic_ifstream(int fd, char_type *buf, int len)
  1096.    * opens a filebuf for reading
  1097.    */
  1098.  
  1099.   template<class charT, class traits>
  1100.   basic_ifstream<charT, traits>::
  1101.   basic_ifstream(int fd, char_type *buf, int len)
  1102.   : basic_istream<charT, traits>( )
  1103.   {
  1104.  
  1105.     init(&__fb);
  1106.  
  1107.     if ( ( buf!=0 ) && ( len>0 ) )
  1108.       __fb.pubsetbuf(buf,len);
  1109.  
  1110.     if ( !__fb.open(fd) ) 
  1111.       this->setstate(ios_base::failbit);
  1112.   }
  1113. #endif // _RWSTD_NO_EXTENSION
  1114.  
  1115.   /*
  1116.    * ~basic_ifstream()
  1117.    */
  1118.  
  1119.   template<class charT, class traits>
  1120.   basic_ifstream<charT, traits>::~basic_ifstream()
  1121.   {
  1122.   }
  1123.  
  1124.   /*
  1125.    * basic_filebuf *rdbuf() const
  1126.    * returns the streambuf associated with this stream
  1127.    */
  1128.  
  1129.   template<class charT, class traits>
  1130.   basic_filebuf<charT, traits> *
  1131.   basic_ifstream<charT, traits>::rdbuf() const
  1132.   {
  1133.     return (basic_filebuf<charT, traits> *)&__fb;
  1134.   }
  1135.  
  1136.   /*
  1137.    * bool is_open()
  1138.    */
  1139.  
  1140.   template<class charT, class traits>
  1141.   bool basic_ifstream<charT, traits>::is_open()
  1142.   {
  1143.     return __fb.is_open();
  1144.   }
  1145.  
  1146.   /*
  1147.    * void open(const char *, ios_base::openmode, long )
  1148.    * opens up a file
  1149.    */
  1150.  
  1151.   template<class charT, class traits>
  1152.   void basic_ifstream<charT, traits>::open(const char *s,
  1153.                                            ios_base::openmode mode,
  1154.                                            long protection )
  1155.   {
  1156.     this->clear();
  1157.     mode |= ios_base::in; 
  1158.  
  1159.     if ( !__fb.open(s, mode, protection) )  
  1160.       this->setstate(ios_base::failbit);
  1161.   }
  1162.  
  1163.   /* 
  1164.    * void close()
  1165.    */
  1166.  
  1167.   template<class charT, class traits>
  1168.   void basic_ifstream<charT, traits>::close()
  1169.   {
  1170.     if(!is_open())
  1171.       return;
  1172.   
  1173.     if(!__fb.close())
  1174.       this->setstate(ios_base::failbit);
  1175.     else
  1176.       this->clear();
  1177.   }
  1178.   /*
  1179.    * class basic_ofstream : public basic_ostream
  1180.    */
  1181.  
  1182.   /*
  1183.    * basic_ofstream()
  1184.    */
  1185.  
  1186.   template<class charT, class traits>
  1187.   basic_ofstream<charT, traits>::basic_ofstream()
  1188.   : basic_ostream<charT, traits>( )
  1189.   {
  1190.     init(&__fb);
  1191.   }
  1192.  
  1193.   /*
  1194.    * basic_ofstream(const char *, ios_base::openmode, long )
  1195.    */
  1196.  
  1197.   template<class charT, class traits>
  1198.   basic_ofstream<charT, traits>::
  1199.   basic_ofstream(const char *s, ios_base::openmode mode, long protection)
  1200.   : basic_ostream<charT, traits>()
  1201.   {
  1202.        
  1203.     init(&__fb);
  1204.     open(s, mode, protection);
  1205.   }
  1206.  
  1207.   /*
  1208.    * basic_ofstream(int fd)
  1209.    * opens a filebuf for writing
  1210.    */
  1211. #ifndef _RWSTD_NO_EXTENSION
  1212.  
  1213.   template<class charT, class traits>
  1214.   basic_ofstream<charT, traits>::
  1215.   basic_ofstream(int fd)
  1216.   : basic_ostream<charT, traits>( )
  1217.   {
  1218.  
  1219.     init(&__fb);
  1220.  
  1221.     if ( !__fb.open(fd) ) 
  1222.       this->setstate(ios_base::failbit);
  1223.   }
  1224.  
  1225.   /*
  1226.    * basic_ofstream(int fd, char_type *buf, int len)
  1227.    * opens a filebuf for writing
  1228.    */
  1229.  
  1230.   template<class charT, class traits>
  1231.   basic_ofstream<charT, traits>::
  1232.   basic_ofstream(int fd, char_type *buf, int len)
  1233.   : basic_ostream<charT, traits>( )
  1234.   {
  1235.  
  1236.     init(&__fb);
  1237.  
  1238.     if ( ( buf!=0 ) && ( len>0 ) )
  1239.       __fb.pubsetbuf(buf,len);
  1240.  
  1241.     if ( !__fb.open(fd) ) 
  1242.       this->setstate(ios_base::failbit);
  1243.   }
  1244. #endif // _RWSTD_NO_EXTENSION
  1245.  
  1246.   /*
  1247.    * ~basic_ofstream()
  1248.    */
  1249.  
  1250.   template<class charT, class traits>
  1251.   basic_ofstream<charT, traits>::~basic_ofstream()
  1252.   {
  1253.   }
  1254.  
  1255.   /*
  1256.    * basic_filebuf *rdbuf() const
  1257.    */
  1258.  
  1259.   template<class charT, class traits>
  1260.   basic_filebuf<charT, traits> *
  1261.   basic_ofstream<charT, traits>::rdbuf() const
  1262.   {
  1263.     return (basic_filebuf<charT, traits> *)&__fb;
  1264.   }
  1265.  
  1266.   /*
  1267.    * bool is_open()
  1268.    */
  1269.  
  1270.   template<class charT, class traits>
  1271.   bool basic_ofstream<charT, traits>::is_open()
  1272.   {
  1273.     return __fb.is_open();
  1274.   }
  1275.  
  1276.   /*
  1277.    * void open(const char *, ios_base::openmode)
  1278.    */
  1279.  
  1280.   template<class charT, class traits>
  1281.   void basic_ofstream<charT, traits>::open(const char *s,
  1282.                                            ios_base::openmode mode,
  1283.                                            long protection )
  1284.   {
  1285.     this->clear();
  1286.     mode |= ios_base::out;
  1287.  
  1288.     if ( !__fb.open(s, mode, protection) )
  1289.       this->setstate(ios_base::failbit);
  1290.   }
  1291.  
  1292.   /*
  1293.    * void close()
  1294.    */
  1295.  
  1296.   template<class charT, class traits>
  1297.   void basic_ofstream<charT, traits>::close()
  1298.   {
  1299.     if(!is_open())
  1300.       return;
  1301.  
  1302.     if(!__fb.close())
  1303.       this->setstate(ios_base::failbit);
  1304.     else
  1305.       this->clear();
  1306.   }
  1307.  
  1308.   /*
  1309.    * class basic_fstream : public basic_iostream
  1310.    */
  1311.  
  1312.   /*
  1313.    * basic_fstream()
  1314.    */
  1315.  
  1316.   template<class charT, class traits>
  1317.   basic_fstream<charT, traits>::basic_fstream()
  1318.   : basic_iostream<charT, traits>( )
  1319.   {
  1320.     init(&__fb);
  1321.   }
  1322.  
  1323.   /*
  1324.    * basic_fstream(const char *, ios_base::openmode, long)
  1325.    *
  1326.    * opens a filebuf for reading and writing
  1327.    */
  1328.  
  1329.   template<class charT, class traits>
  1330.   basic_fstream<charT, traits>::
  1331.   basic_fstream(const char *s, ios_base::openmode mode, long protection)
  1332.   : basic_iostream<charT, traits>( )
  1333.   {
  1334.     init(&__fb);
  1335.     open(s, mode, protection);
  1336.   }
  1337.  
  1338.   /*
  1339.    * basic_fstream(int fd)
  1340.    * opens a filebuf for reading and writing
  1341.    */
  1342. #ifndef _RWSTD_NO_EXTENSION
  1343.  
  1344.   template<class charT, class traits>
  1345.   basic_fstream<charT, traits>::
  1346.   basic_fstream(int fd)
  1347.   : basic_iostream<charT, traits>( )
  1348.   {
  1349.  
  1350.     init(&__fb);
  1351.  
  1352.     if ( !__fb.open(fd) ) 
  1353.       this->setstate(ios_base::failbit);
  1354.   }
  1355.  
  1356.   /*
  1357.    * basic_fstream(int fd, char_type *buf, int len)
  1358.    * opens a filebuf for reading and writing
  1359.    */
  1360.  
  1361.   template<class charT, class traits>
  1362.   basic_fstream<charT, traits>::
  1363.   basic_fstream(int fd, char_type *buf, int len)
  1364.   : basic_iostream<charT, traits>( )
  1365.   {
  1366.  
  1367.     init(&__fb);
  1368.  
  1369.     if ( ( buf!=0 ) && ( len>0 ) )
  1370.       __fb.pubsetbuf(buf,len);
  1371.  
  1372.     if ( !__fb.open(fd) ) 
  1373.       this->setstate(ios_base::failbit);
  1374.   }
  1375. #endif // _RWSTD_NO_EXTENSION
  1376.   /*
  1377.    * ~basic_fstream()
  1378.    */
  1379.  
  1380.   template<class charT, class traits>
  1381.   basic_fstream<charT, traits>::~basic_fstream()
  1382.   {
  1383.   }
  1384.  
  1385.   /*
  1386.    * basic_filebuf *rdbuf() const
  1387.    * returns the streambuf associated with this stream
  1388.    */
  1389.  
  1390.   template<class charT, class traits>
  1391.   basic_filebuf<charT, traits> *
  1392.   basic_fstream<charT, traits>::rdbuf() const
  1393.   {
  1394.     return (basic_filebuf<charT, traits> *)&__fb;
  1395.   }
  1396.  
  1397.   /*
  1398.    * bool is_open()
  1399.    */
  1400.  
  1401.   template<class charT, class traits>
  1402.   bool basic_fstream<charT, traits>::is_open()
  1403.   {
  1404.     return __fb.is_open();
  1405.   }
  1406.  
  1407.   /*
  1408.    * void open(const char *, ios_base::openmode, long)
  1409.    * opens up a file
  1410.    */
  1411.  
  1412.   template<class charT, class traits>
  1413.   void basic_fstream<charT, traits>::
  1414.   open(const char *s, ios_base::openmode mode, long protection)
  1415.   {
  1416.     this->clear();
  1417.     if ( !__fb.open(s, mode, protection) ) 
  1418.       this->setstate(ios_base::failbit);
  1419.   }
  1420.  
  1421.   /* 
  1422.    * void close()
  1423.    */
  1424.  
  1425.   template<class charT, class traits>
  1426.   void basic_fstream<charT, traits>::close()
  1427.   {
  1428.     if(!is_open())
  1429.       return;
  1430.   
  1431.     if(!__fb.close())
  1432.       this->setstate(ios_base::failbit);
  1433.     else
  1434.       this->clear();
  1435.   }
  1436.  
  1437. #ifndef _RWSTD_NO_NAMESPACE
  1438. }
  1439. #endif
  1440.  
  1441. #pragma option pop
  1442. #endif /* __FSTREAM_CC */
  1443.