home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / include / fstream.h < prev    next >
C/C++ Source or Header  |  1998-06-16  |  5KB  |  171 lines

  1. /***
  2. *fstream.h - definitions/declarations for filebuf and fstream classes
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       This file defines the classes, values, macros, and functions
  8. *       used by the filebuf and fstream classes.
  9. *       [AT&T C++]
  10. *
  11. *       [Public]
  12. *
  13. ****/
  14.  
  15. #if     _MSC_VER > 1000
  16. #pragma once
  17. #endif
  18.  
  19. #ifdef  __cplusplus
  20.  
  21. #ifndef _INC_FSTREAM
  22. #define _INC_FSTREAM
  23.  
  24. #if     !defined(_WIN32) && !defined(_MAC)
  25. #error ERROR: Only Mac or Win32 targets supported!
  26. #endif
  27.  
  28.  
  29. #ifdef  _MSC_VER
  30. // Currently, all MS C compilers for Win32 platforms default to 8 byte
  31. // alignment.
  32. #pragma pack(push,8)
  33.  
  34. #include <useoldio.h>
  35.  
  36. #endif  // _MSC_VER
  37.  
  38. /* Define _CRTIMP */
  39.  
  40. #ifndef _CRTIMP
  41. #ifdef  _DLL
  42. #define _CRTIMP __declspec(dllimport)
  43. #else   /* ndef _DLL */
  44. #define _CRTIMP
  45. #endif  /* _DLL */
  46. #endif  /* _CRTIMP */
  47.  
  48.  
  49. #include <iostream.h>
  50.  
  51. #ifdef  _MSC_VER
  52. // C4514: "unreferenced inline function has been removed"
  53. #pragma warning(disable:4514) // disable C4514 warning
  54. // #pragma warning(default:4514)        // use this to reenable, if desired
  55. #endif  // _MSC_VER
  56.  
  57. typedef int filedesc;
  58.  
  59. class _CRTIMP filebuf : public streambuf {
  60. public:
  61. static  const int       openprot;       // default share/prot mode for open
  62.  
  63. // optional share values for 3rd argument (prot) of open or constructor
  64. static  const int       sh_none;        // exclusive mode no sharing
  65. static  const int       sh_read;        // allow read sharing
  66. static  const int       sh_write;       // allow write sharing
  67. // use (sh_read | sh_write) to allow both read and write sharing
  68.  
  69. // options for setmode member function
  70. static  const int       binary;
  71. static  const int       text;
  72.  
  73.                         filebuf();
  74.                         filebuf(filedesc);
  75.                         filebuf(filedesc, char *, int);
  76.                         ~filebuf();
  77.  
  78.         filebuf*        attach(filedesc);
  79.         filedesc        fd() const { return (x_fd==-1) ? EOF : x_fd; }
  80.         int             is_open() const { return (x_fd!=-1); }
  81.         filebuf*        open(const char *, int, int = filebuf::openprot);
  82.         filebuf*        close();
  83.         int             setmode(int = filebuf::text);
  84.  
  85. virtual int             overflow(int=EOF);
  86. virtual int             underflow();
  87.  
  88. virtual streambuf*      setbuf(char *, int);
  89. virtual streampos       seekoff(streamoff, ios::seek_dir, int);
  90. // virtual      streampos       seekpos(streampos, int);
  91. virtual int             sync();
  92.  
  93. private:
  94.         filedesc        x_fd;
  95.         int             x_fOpened;
  96. };
  97.  
  98. class _CRTIMP ifstream : public istream {
  99. public:
  100.         ifstream();
  101.         ifstream(const char *, int =ios::in, int = filebuf::openprot);
  102.         ifstream(filedesc);
  103.         ifstream(filedesc, char *, int);
  104.         ~ifstream();
  105.  
  106.         streambuf * setbuf(char *, int);
  107.         filebuf* rdbuf() const { return (filebuf*) ios::rdbuf(); }
  108.  
  109.         void attach(filedesc);
  110.         filedesc fd() const { return rdbuf()->fd(); }
  111.  
  112.         int is_open() const { return rdbuf()->is_open(); }
  113.         void open(const char *, int =ios::in, int = filebuf::openprot);
  114.         void close();
  115.         int setmode(int mode = filebuf::text) { return rdbuf()->setmode(mode); }
  116. };
  117.  
  118. class _CRTIMP ofstream : public ostream {
  119. public:
  120.         ofstream();
  121.         ofstream(const char *, int =ios::out, int = filebuf::openprot);
  122.         ofstream(filedesc);
  123.         ofstream(filedesc, char *, int);
  124.         ~ofstream();
  125.  
  126.         streambuf * setbuf(char *, int);
  127.         filebuf* rdbuf() const { return (filebuf*) ios::rdbuf(); }
  128.  
  129.         void attach(filedesc);
  130.         filedesc fd() const { return rdbuf()->fd(); }
  131.  
  132.         int is_open() const { return rdbuf()->is_open(); }
  133.         void open(const char *, int =ios::out, int = filebuf::openprot);
  134.         void close();
  135.         int setmode(int mode = filebuf::text) { return rdbuf()->setmode(mode); }
  136. };
  137.         
  138. class _CRTIMP fstream : public iostream {
  139. public:
  140.         fstream();
  141.         fstream(const char *, int, int = filebuf::openprot);
  142.         fstream(filedesc);
  143.         fstream(filedesc, char *, int);
  144.         ~fstream();
  145.  
  146.         streambuf * setbuf(char *, int);
  147.         filebuf* rdbuf() const { return (filebuf*) ostream::rdbuf(); }
  148.  
  149.         void attach(filedesc);
  150.         filedesc fd() const { return rdbuf()->fd(); }
  151.  
  152.         int is_open() const { return rdbuf()->is_open(); }
  153.         void open(const char *, int, int = filebuf::openprot);
  154.         void close();
  155.         int setmode(int mode = filebuf::text) { return rdbuf()->setmode(mode); }
  156. };
  157.         
  158. // manipulators to dynamically change file access mode (filebufs only)
  159. inline  ios& binary(ios& _fstrm) \
  160.    { ((filebuf*)_fstrm.rdbuf())->setmode(filebuf::binary); return _fstrm; }
  161. inline  ios& text(ios& _fstrm) \
  162.    { ((filebuf*)_fstrm.rdbuf())->setmode(filebuf::text); return _fstrm; }
  163.  
  164. #ifdef  _MSC_VER
  165. #pragma pack(pop)
  166. #endif  // _MSC_VER
  167.  
  168. #endif  // _INC_FSTREAM
  169.  
  170. #endif  /* __cplusplus */
  171.