home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / fstream.h < prev    next >
C/C++ Source or Header  |  1998-06-17  |  6KB  |  193 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  /* _MSC_VER > 1000 */
  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  /* !defined (_WIN32) && !defined (_MAC) */
  27.  
  28. #ifndef _CRTBLD
  29. /* This version of the header files is NOT for user programs.
  30.  * It is intended for use when building the C runtimes ONLY.
  31.  * The version intended for public use will not have this message.
  32.  */
  33. #error ERROR: Use of C runtime library internal header file.
  34. #endif  /* _CRTBLD */
  35.  
  36. #ifdef _MSC_VER
  37. // Currently, all MS C compilers for Win32 platforms default to 8 byte
  38. // alignment.
  39. #pragma pack(push,8)
  40.  
  41. #include <useoldio.h>
  42.  
  43. #endif  /* _MSC_VER */
  44.  
  45. /* Define _CRTIMP */
  46.  
  47. #ifndef _CRTIMP
  48. #ifdef CRTDLL
  49. #define _CRTIMP __declspec(dllexport)
  50. #else  /* CRTDLL */
  51. #ifdef _DLL
  52. #define _CRTIMP __declspec(dllimport)
  53. #else  /* _DLL */
  54. #define _CRTIMP
  55. #endif  /* _DLL */
  56. #endif  /* CRTDLL */
  57. #endif  /* _CRTIMP */
  58.  
  59. #ifndef _INTERNAL_IFSTRIP_
  60. /* Define _CRTIMP1 */
  61.  
  62. #ifndef _CRTIMP1
  63. #ifdef CRTDLL1
  64. #define _CRTIMP1 __declspec(dllexport)
  65. #else  /* CRTDLL1 */
  66. #define _CRTIMP1 _CRTIMP
  67. #endif  /* CRTDLL1 */
  68. #endif  /* _CRTIMP1 */
  69. #endif  /* _INTERNAL_IFSTRIP_ */
  70.  
  71. #include <iostream.h>
  72.  
  73. #ifdef _MSC_VER
  74. // C4514: "unreferenced inline function has been removed"
  75. #pragma warning(disable:4514) // disable C4514 warning
  76. // #pragma warning(default:4514)        // use this to reenable, if desired
  77. #endif  /* _MSC_VER */
  78.  
  79. typedef int filedesc;
  80.  
  81. class _CRTIMP1 filebuf : public streambuf {
  82. public:
  83. static  const int       openprot;       // default share/prot mode for open
  84.  
  85. // optional share values for 3rd argument (prot) of open or constructor
  86. static  const int       sh_none;        // exclusive mode no sharing
  87. static  const int       sh_read;        // allow read sharing
  88. static  const int       sh_write;       // allow write sharing
  89. // use (sh_read | sh_write) to allow both read and write sharing
  90.  
  91. // options for setmode member function
  92. static  const int       binary;
  93. static  const int       text;
  94.  
  95.                         filebuf();
  96.                         filebuf(filedesc);
  97.                         filebuf(filedesc, char *, int);
  98.                         ~filebuf();
  99.  
  100.         filebuf*        attach(filedesc);
  101.         filedesc        fd() const { return (x_fd==-1) ? EOF : x_fd; }
  102.         int             is_open() const { return (x_fd!=-1); }
  103.         filebuf*        open(const char *, int, int = filebuf::openprot);
  104.         filebuf*        close();
  105.         int             setmode(int = filebuf::text);
  106.  
  107. virtual int             overflow(int=EOF);
  108. virtual int             underflow();
  109.  
  110. virtual streambuf*      setbuf(char *, int);
  111. virtual streampos       seekoff(streamoff, ios::seek_dir, int);
  112. // virtual      streampos       seekpos(streampos, int);
  113. virtual int             sync();
  114.  
  115. private:
  116.         filedesc        x_fd;
  117.         int             x_fOpened;
  118. };
  119.  
  120. class _CRTIMP1 ifstream : public istream {
  121. public:
  122.         ifstream();
  123.         ifstream(const char *, int =ios::in, int = filebuf::openprot);
  124.         ifstream(filedesc);
  125.         ifstream(filedesc, char *, int);
  126.         ~ifstream();
  127.  
  128.         streambuf * setbuf(char *, int);
  129.         filebuf* rdbuf() const { return (filebuf*) ios::rdbuf(); }
  130.  
  131.         void attach(filedesc);
  132.         filedesc fd() const { return rdbuf()->fd(); }
  133.  
  134.         int is_open() const { return rdbuf()->is_open(); }
  135.         void open(const char *, int =ios::in, int = filebuf::openprot);
  136.         void close();
  137.         int setmode(int mode = filebuf::text) { return rdbuf()->setmode(mode); }
  138. };
  139.  
  140. class _CRTIMP1 ofstream : public ostream {
  141. public:
  142.         ofstream();
  143.         ofstream(const char *, int =ios::out, int = filebuf::openprot);
  144.         ofstream(filedesc);
  145.         ofstream(filedesc, char *, int);
  146.         ~ofstream();
  147.  
  148.         streambuf * setbuf(char *, int);
  149.         filebuf* rdbuf() const { return (filebuf*) ios::rdbuf(); }
  150.  
  151.         void attach(filedesc);
  152.         filedesc fd() const { return rdbuf()->fd(); }
  153.  
  154.         int is_open() const { return rdbuf()->is_open(); }
  155.         void open(const char *, int =ios::out, int = filebuf::openprot);
  156.         void close();
  157.         int setmode(int mode = filebuf::text) { return rdbuf()->setmode(mode); }
  158. };
  159.  
  160. class _CRTIMP1 fstream : public iostream {
  161. public:
  162.         fstream();
  163.         fstream(const char *, int, int = filebuf::openprot);
  164.         fstream(filedesc);
  165.         fstream(filedesc, char *, int);
  166.         ~fstream();
  167.  
  168.         streambuf * setbuf(char *, int);
  169.         filebuf* rdbuf() const { return (filebuf*) ostream::rdbuf(); }
  170.  
  171.         void attach(filedesc);
  172.         filedesc fd() const { return rdbuf()->fd(); }
  173.  
  174.         int is_open() const { return rdbuf()->is_open(); }
  175.         void open(const char *, int, int = filebuf::openprot);
  176.         void close();
  177.         int setmode(int mode = filebuf::text) { return rdbuf()->setmode(mode); }
  178. };
  179.  
  180. // manipulators to dynamically change file access mode (filebufs only)
  181. inline  ios& binary(ios& _fstrm) \
  182.    { ((filebuf*)_fstrm.rdbuf())->setmode(filebuf::binary); return _fstrm; }
  183. inline  ios& text(ios& _fstrm) \
  184.    { ((filebuf*)_fstrm.rdbuf())->setmode(filebuf::text); return _fstrm; }
  185.  
  186. #ifdef _MSC_VER
  187. #pragma pack(pop)
  188. #endif  /* _MSC_VER */
  189.  
  190. #endif  /* _INC_FSTREAM */
  191.  
  192. #endif  /* __cplusplus */
  193.