home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 May / PCP163A.iso / Runimage / Cbuilder4 / Include / FSTREAM.H < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-26  |  13.2 KB  |  440 lines

  1. #ifndef __FSTREAM_H
  2. #define __FSTREAM_H
  3. #pragma option push -b -a8 -pc -Vx- -Ve- -w-inl -w-aus -w-sig
  4. // -*- C++ -*-
  5. #ifndef __STD_FSTREAM__
  6. #define __STD_FSTREAM__
  7.  
  8. /***************************************************************************
  9.  *
  10.  * fstream -- Declarations for the Standard Library file bufs and streams
  11.  *
  12.  
  13.  ***************************************************************************
  14.  *
  15.  * (c) Copyright 1994, 1998 Rogue Wave Software, Inc.
  16.  * ALL RIGHTS RESERVED
  17.  *
  18.  * The software and information contained herein are proprietary to, and
  19.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  20.  * intends to preserve as trade secrets such software and information.
  21.  * This software is furnished pursuant to a written license agreement and
  22.  * may be used, copied, transmitted, and stored only in accordance with
  23.  * the terms of such license and with the inclusion of the above copyright
  24.  * notice.  This software and information or any other copies thereof may
  25.  * not be provided or otherwise made available to any other person.
  26.  *
  27.  * Notwithstanding any other lease or license that may pertain to, or
  28.  * accompany the delivery of, this computer software and information, the
  29.  * rights of the Government regarding its use, reproduction and disclosure
  30.  * are as set forth in Section 52.227-19 of the FARS Computer
  31.  * Software-Restricted Rights clause.
  32.  * 
  33.  * Use, duplication, or disclosure by the Government is subject to
  34.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  35.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  36.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  37.  * P.O. Box 2328, Corvallis, Oregon 97339.
  38.  *
  39.  * This computer software and information is distributed with "restricted
  40.  * rights."  Use, duplication or disclosure is subject to restrictions as
  41.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  42.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  43.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  44.  * then the "Alternate III" clause applies.
  45.  *
  46.  **************************************************************************/
  47. #include <stdcomp.h>
  48.  
  49. #include <istream>
  50. #include <ostream>
  51. #include <streambuf>
  52.  
  53. #ifndef _RWSTD_NO_NEW_HEADER
  54. #include <cstdlib>
  55. #include <cfcntl>
  56. #else
  57. #include <stdlib.h>
  58. #include <fcntl.h>
  59. #endif
  60.  
  61. #ifndef _RWSTD_STRICT_ANSI
  62. # if defined (_MSC_VER) || defined (__TURBOC__)
  63. #  include <io.h>
  64. # else
  65. #  ifndef _RWSTD_NO_NEW_HEADER
  66. #   include <cunistd>
  67. #  else
  68. #   include <unistd.h>
  69. #  endif
  70. # endif // defined (_MSC_VER) || defined (__TURBOC__)
  71. #else
  72. # ifndef _RWSTD_NO_EXTENSION
  73. #  define _RWSTD_NO_EXTENSION 1
  74. # endif
  75. #endif // _RWSTD_STRICT_ANSI
  76.  
  77. #ifndef _RWSTD_NO_NAMESPACE
  78. namespace __rwstd {
  79. #endif  
  80.  
  81. #ifndef _RWSTD_STRICT_ANSI
  82.   extern const int _RWSTDExport __rwOpenTable[32];
  83. #else
  84.   extern const char _RWSTDExportFunc(*) __rwOpenTable[32];
  85. #endif
  86.  
  87. #ifndef _RWSTD_NO_NAMESPACE
  88. } namespace std {
  89. #endif  
  90.  
  91.   /*
  92.    *  Class basic_filebuf
  93.    */
  94.   
  95.   template<class charT, class traits>
  96.   class _RWSTDExportTemplate basic_filebuf : public basic_streambuf<charT, traits> {
  97.  
  98. #ifndef _RWSTD_STRICT_ANSI
  99.     typedef int __file_type;
  100. #else
  101.     typedef FILE* __file_type;
  102. #endif
  103.  
  104.     typedef basic_ios<charT, traits>      ios_type;
  105.     typedef _TYPENAME traits::state_type  state_t; 
  106.     
  107.   public:
  108.     //
  109.     // Types:
  110.     //
  111.     typedef charT                         char_type;
  112.     typedef _TYPENAME traits::int_type    int_type;
  113.     typedef _TYPENAME traits::pos_type    pos_type;
  114.     typedef _TYPENAME traits::off_type    off_type;
  115.     typedef traits                        traits_type;
  116.       
  117.     basic_filebuf();
  118.     virtual ~basic_filebuf();
  119.  
  120.     bool is_open() const;
  121.     basic_filebuf<charT, traits> *
  122.     open(const char *s, ios_base::openmode, long protection = 0666 );
  123.     basic_filebuf<charT, traits> *close();
  124.     basic_filebuf(__file_type fd);
  125.  
  126. #ifndef _RWSTD_NO_EXTENSION
  127.     basic_filebuf<charT, traits> *open(int fd);
  128.     int fd() const { return __file;}
  129. #endif //_RWSTD_NO_EXTENSION
  130.  
  131.   protected:
  132.  
  133.     virtual int      showmanyc();
  134.     virtual int_type underflow();
  135.     virtual int_type overflow(int_type c = traits::eof());
  136.     virtual int_type pbackfail(int_type c = traits::eof());
  137.       
  138.     virtual basic_streambuf<charT,traits>* 
  139.     setbuf(char_type *s,streamsize n);
  140.  
  141.     virtual pos_type seekoff(off_type off,
  142.                              ios_base::seekdir way,
  143.                              ios_base::openmode which =
  144.                              ios_base::in | ios_base::out);
  145.     virtual pos_type seekpos(pos_type sp,
  146.                              ios_base::openmode which =
  147.                              ios_base::in | ios_base::out);
  148.  
  149.     virtual int sync( );
  150.     virtual streamsize xsputn(const char_type *s, streamsize n);
  151.  
  152.   private:
  153.  
  154.     // Deal with CR/LF translation on win32
  155.     off_type __get_chars(charT* start, charT* finish)
  156.     {
  157. #ifndef __WIN32__
  158.       return finish-start;
  159. #else
  160.       if (mode_ & ios_base::binary)
  161.         return finish-start;
  162.  
  163. #ifndef _RWSTD_NO_TEMPLATE_ON_RETURN_TYPE
  164.       charT eol = use_facet< ctype<charT> >(this->getloc()).widen('\n'); 
  165. #else
  166.       charT eol = use_facet( this->getloc(), (ctype<charT>* )0).widen('\n'); 
  167. #endif
  168.  
  169.       off_type n=0;
  170.       while (start != finish)
  171.       {
  172.         if (*start++ == eol)
  173.           n+=2;
  174.         else
  175.           ++n;
  176.       }
  177.       return n;
  178. #endif // __WIN32__
  179.     }
  180.        
  181.  
  182.     off_type __file_seek(__file_type f, long offset, int origin)
  183.     {
  184. #ifndef _RWSTD_STRICT_ANSI
  185.       return lseek(f, offset, origin);
  186. #else
  187.       fseek(f, offset, origin);
  188.       return ftell(f);
  189. #endif // _RWSTD_STRICT_ANSI
  190.     }
  191.  
  192.     int __file_close(__file_type f)
  193.     {
  194. #ifndef _RWSTD_STRICT_ANSI
  195.       return ::close(f);
  196. #else
  197.       return fclose(f);
  198. #endif // _RWSTD_STRICT_ANSI
  199.     }
  200.  
  201.     bool __file_write(__file_type f, const char* buffer, size_t size, size_t count)
  202.     {
  203. #ifndef _RWSTD_STRICT_ANSI
  204.       return (size_t) ::write(f,buffer,size*count) == size*count ? true : false;
  205. #else
  206.       return fwrite(buffer,size,count,f) == count ? true : false;
  207. #endif // _RWSTD_STRICT_ANSI
  208.     }
  209.  
  210.     size_t __file_read(__file_type f, char* buffer, size_t size, size_t count)
  211.     {
  212. #ifndef _RWSTD_STRICT_ANSI
  213.       int n = ::read(f,buffer,size*count);
  214.       return n < 0 ? 0 : (size_t)n;
  215. #else
  216.       return fread(buffer,size,count,f)*size;
  217. #endif
  218.     }
  219.  
  220.     __file_type __closed()
  221.     {
  222. #ifndef _RWSTD_STRICT_ANSI
  223.        return  -1;    
  224. #else
  225.        return 0;
  226. #endif
  227.     }
  228.  
  229.     bool __file_is_std(__file_type f)
  230.     {
  231. #ifndef _RWSTD_STRICT_ANSI
  232.        return  f < 3;    
  233. #else
  234.        return (f == stdin || f == stdout || f == stderr);
  235. #endif
  236.     }
  237.  
  238.     __file_type                 __file;
  239.     bool                        __read_buff;
  240.     bool                        __write_buff;
  241.     streamsize                  __last_numb_read;
  242.     streamsize                  __rwBufferSize;
  243.     streamsize                  __absolute_pos;
  244.     state_t                    *__state_beg;
  245.     state_t                    *__state_end;
  246.     char_type                  *__data;
  247.   };
  248.   template<class charT, class traits>
  249.   class _RWSTDExportTemplate basic_ifstream : public basic_istream<charT, traits> {
  250.  
  251.   public:
  252.     //
  253.     // Types:
  254.     //
  255.     typedef charT                              char_type;
  256.     typedef _TYPENAME traits::int_type         int_type;
  257.     typedef _TYPENAME traits::pos_type         pos_type;
  258.     typedef _TYPENAME traits::off_type         off_type;
  259.     typedef traits                             traits_type;
  260.     typedef basic_ios<charT, traits>           ios_type;
  261.       
  262.   public:
  263.  
  264.     basic_ifstream();
  265.     _EXPLICIT basic_ifstream(const char *s, 
  266.                              ios_base::openmode mode = ios_base::in,
  267.                              long protection = 0666 );
  268.  
  269. #ifndef _RWSTD_NO_EXTENSION
  270.     _EXPLICIT basic_ifstream(int fd);
  271.     basic_ifstream(int fd, char_type *buf, int len);
  272. #endif //_RWSTD_NO_EXTENSION
  273.  
  274.     virtual ~basic_ifstream();
  275.     basic_filebuf<charT, traits> *rdbuf() const;
  276.     bool is_open();
  277.     void open(const char *s, ios_base::openmode mode = ios_base::in,
  278.               long protection = 0666 );
  279.  
  280.     void close();
  281.   protected:
  282.  
  283.   private:
  284.     basic_filebuf<charT, traits>              __fb;
  285.   };
  286.   template<class charT, class traits>
  287.   class _RWSTDExportTemplate basic_ofstream : public basic_ostream<charT, traits> {
  288.  
  289.   public:
  290.     //
  291.     //Types:
  292.     //
  293.     typedef charT                                       char_type;
  294.     typedef _TYPENAME traits::int_type                  int_type;
  295.     typedef _TYPENAME traits::pos_type                  pos_type;
  296.     typedef _TYPENAME traits::off_type                  off_type;
  297.     typedef traits                                      traits_type;
  298.  
  299.     typedef basic_ios<charT, traits>                   ios_type;
  300.       
  301.   public:
  302.  
  303.     basic_ofstream();
  304.     _EXPLICIT basic_ofstream(const char *s, ios_base::openmode mode =
  305.                              ios_base::out,
  306.                              long protection = 0666 );
  307.  
  308. #ifndef _RWSTD_NO_EXTENSION
  309.     _EXPLICIT basic_ofstream(int fd);
  310.     basic_ofstream(int fd, char_type *buf, int len);
  311. #endif //_RWSTD_NO_EXTENSION
  312.  
  313.     virtual ~basic_ofstream();
  314.  
  315.     basic_filebuf<charT, traits> *rdbuf() const;
  316.     bool is_open();
  317.      
  318.     void open(const char *s, ios_base::openmode mode = 
  319.               ios_base::out,
  320.               long protection = 0666 );
  321.      
  322.     void close();
  323.   protected:
  324.  
  325.   private:
  326.     basic_filebuf<charT, traits>              __fb;
  327.   };
  328.  
  329.   /*
  330.    *  Class basic_fstream
  331.    */
  332.  
  333.   template<class charT, class traits>
  334.   class _RWSTDExportTemplate basic_fstream : public basic_iostream<charT, traits> {
  335.  
  336.   public:
  337.     //
  338.     // Types:
  339.     //
  340.     typedef charT                             char_type;
  341.     typedef _TYPENAME traits::int_type        int_type;
  342.     typedef _TYPENAME traits::pos_type        pos_type;
  343.     typedef _TYPENAME traits::off_type        off_type;
  344.     typedef traits                            traits_type;
  345.     typedef basic_ios<charT, traits>          ios_type;
  346.  
  347.     basic_fstream();
  348.     _EXPLICIT basic_fstream(const char *s, ios_base::openmode mode = 
  349.                             ios_base::in | ios_base::out, 
  350.                             long protection= 0666 );
  351.  
  352. #ifndef _RWSTD_NO_EXTENSION
  353.     _EXPLICIT basic_fstream(int fd);
  354.     basic_fstream(int fd, char_type *buf, int len);
  355. #endif //_RWSTD_NO_EXTENSION
  356.  
  357.     virtual ~basic_fstream();
  358.  
  359.     basic_filebuf<charT, traits> *rdbuf() const;
  360.     bool is_open();
  361.     void open(const char *s, ios_base::openmode mode = 
  362.               ios_base::in | ios_base::out, 
  363.               long protection = 0666 );
  364.  
  365.     void close();
  366.   protected:
  367.  
  368.   private:
  369.     basic_filebuf<charT, traits>              __fb;
  370.   };
  371. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  372.   typedef basic_filebuf<char>                             filebuf;
  373. #else
  374.   typedef basic_filebuf<char, char_traits<char> >         filebuf;
  375. #endif // _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  376.  
  377. #ifndef _RWSTD_NO_WIDE_CHAR
  378. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  379.   typedef basic_filebuf<wchar_t>                          wfilebuf;
  380. #else
  381.   typedef basic_filebuf<wchar_t, char_traits<wchar_t> >   wfilebuf;
  382. #endif // _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  383. #endif // _RWSTD_NO_WIDE_CHAR
  384. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  385.   typedef basic_ifstream<char>                            ifstream;
  386. #else
  387.   typedef basic_ifstream<char, char_traits<char> >        ifstream;
  388. #endif // _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  389.  
  390. #ifndef _RWSTD_NO_WIDE_CHAR
  391. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  392.   typedef basic_ifstream<wchar_t>                         wifstream;
  393. #else
  394.   typedef basic_ifstream<wchar_t, char_traits<wchar_t> >  wifstream;
  395. #endif // _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  396. #endif // _RWSTD_NO_WIDE_CHAR 
  397. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  398.   typedef basic_ofstream<char>                            ofstream;
  399. #else
  400.   typedef basic_ofstream<char, char_traits<char> >        ofstream;
  401. #endif // _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES 
  402.  
  403. #ifndef _RWSTD_NO_WIDE_CHAR
  404. #ifndef _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  405.   typedef basic_ofstream<wchar_t>                         wofstream;
  406. #else
  407.   typedef basic_ofstream<wchar_t, char_traits<wchar_t> >  wofstream;
  408. #endif // _RWSTD_NO_COMPLEX_DEFAULT_TEMPLATES
  409. #endif // _RWSTD_NO_WIDE_CHAR
  410.  
  411. #ifndef _RWSTD_NO_DEFAULT_TEMPLATES
  412.   typedef basic_fstream<char>                             fstream;
  413. #else
  414.   typedef basic_fstream<char, char_traits<char> >         fstream;
  415. #endif // _RWSTD_NO_DEFAULT_TEMPLATES 
  416.  
  417. #ifndef _RWSTD_NO_WIDE_CHAR
  418. #ifndef _RWSTD_NO_DEFAULT_TEMPLATES  
  419.   typedef basic_fstream<wchar_t>                          wfstream;
  420. #else
  421.   typedef basic_fstream<wchar_t, char_traits<wchar_t> >   wfstream;
  422. #endif // _RWSTD_NO_DEFAULT_TEMPLATES  
  423. #endif // _RWSTD_NO_WIDE_CHAR 
  424.  
  425. #ifndef _RWSTD_NO_NAMESPACE
  426. }
  427. #endif
  428.  
  429. #ifdef _RWSTD_COMPILE_INSTANTIATE
  430. #include <fstream.cc>
  431. #endif
  432.  
  433. #endif //__STD_FSTREAM__ 
  434.  
  435. #ifndef __USING_STD_NAMES__
  436.   using namespace std;
  437. #endif
  438. #pragma option pop
  439. #endif /* __FSTREAM_H */
  440.