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