home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / win3 / patches / symantec / rtlinc.exe / FSTREAM.H < prev    next >
C/C++ Source or Header  |  1993-05-27  |  10KB  |  360 lines

  1. // IOStreams Package
  2. // Steve Teale April 1992
  3. // Copyright Symantec Corp 1990-1992. All Rights Reserved.
  4.  
  5. #ifndef __FSTREAM_H
  6. #define __FSTREAM_H
  7.  
  8. #include <iostream.h>
  9.  
  10. #pragma pack(__DEFALIGN)
  11. class filebuf : public streambuf {
  12.  
  13. // This is a streambuf class specialized for handling files.
  14. // The get and put pointers are locked together so that reads and writes
  15. // happen at the same offset into the file.
  16.  
  17. public:
  18.     enum { openprot = 0644 };
  19.  
  20.     filebuf();
  21. // The default constructor. Creats a filebuf that is not associated
  22. // with any file. open() or attach() can then be used to connect
  23. // to a file.
  24.  
  25.     filebuf(int file_descriptor, int io_mode = ios::in|ios::out
  26. #if M_UNIX || M_XENIX
  27.             );
  28. #else
  29.             |ios::translated);
  30. #endif
  31. // Constructs a filebuf for the open file attached to the argument
  32. // file descriptor.     More comprehensive io_mode information can
  33. // be specified e.g. ios::app, if required
  34.  
  35.     filebuf(int descriptor, char *memory, int length,
  36.                             int io_mode = ios::in|ios::out
  37. #if M_UNIX || M_XENIX
  38.             );
  39. #else
  40.             |ios::translated);
  41. #endif
  42. // Constructs a filebuf for the open file attached to the
  43. // file_descriptor, and sets the buffer to "memory", which is of
  44. // "length" bytes in size. If memory is 0 or length is <= 0,
  45. // it is taken as a request that the file be unbuffered.
  46.  
  47.     ~filebuf();
  48.  
  49.     filebuf *attach(int file_descriptor,
  50.                             int io_mode = ios::in|ios::out
  51. #if M_UNIX_ || M_XENIX
  52.             );
  53. #else
  54.             |ios::translated);
  55. #endif
  56. // Attaches an open file to a filebuf. Returns "this" if successful,
  57. // 0 if the filebuf is already attached to a file.
  58.  
  59.     filebuf *close();
  60. // Flushes output, closes the file, and detaches the file from this
  61. // filebuf. Clears the error state unless there is an error flushing
  62. // the output. Will always close the file and detach it from the
  63. // filebuf, even if there are errors.
  64.  
  65.     int fd() const { return file; };
  66. // Returns the file descriptor for the connected file. If the
  67. // filebuf is closed or not attached to a file, returns EOF.
  68.  
  69.     int is_open() const { return file != EOF; };
  70. // Returns non-zero when this filebuf is attached to a file,
  71. // otherwise returns zero.
  72.  
  73.     filebuf *open(const char *name, int io_mode,
  74.                     int protection = openprot);
  75. // Opens the file "name", and connects it to this filebuf.
  76. // io_mode is a bit-mask containing one or more of the values of
  77. // enum open_mode:
  78. // ios::in       Open for reading.
  79. // ios::out       Open for writing.
  80. // ios::ate       Position to the end-of-file.
  81. // ios::app       Open the file in append mode.
  82. // ios::trunc  Truncate the file on open.
  83. // ios::nocreate   Do not attempt to create the file if it
  84. //               does not exist.
  85. // ios::noreplace  Cause the open to fail if the file exists.
  86. // ios::translate  Convert CR/LF to newline on input and
  87. //               vice versa on output
  88.  
  89.     streampos seekoff(streamoff offset, ios::relative_to,
  90.                                 int which);
  91. // Relative seek the get and put pointers within the file.
  92. // The get and put pointers of a filebuf always point to the
  93. // same byte of the file.
  94.  
  95.     streambuf *setbuf(char *memory, int length);
  96. // Set the buffer to use "memory", of "length" bytes.
  97. // If memory == 0 or length <= 0, it is taken as a request that
  98. // I/O to the file be unbuffered.
  99.  
  100.     int sync();
  101. // Flush any bytes in the get buffer, and re-position the file so
  102. // that is appears they were never read. Write any bytes in the
  103. // put buffer to the file.
  104.  
  105. #if __SC__ > 0x214
  106.     int overflow(int c);
  107. #else
  108.     int overflow(int c = EOF);
  109. #endif
  110. // Flush bytes in the put area to the file.
  111.  
  112.     int underflow();
  113. // Get more bytes for reading.
  114.  
  115. protected:
  116.     int doallocate();
  117.  
  118.     int pbackfail(int c);
  119. // Called to atempt recovery if putback attempted at
  120. // start of get buffer
  121.  
  122. private:
  123.  
  124.     void buffer_setup();
  125.                 // Internal. Set up I/O buffer.
  126.     int newlines();
  127.                 // count newline chars in the get buffer
  128.     int syncin();
  129.     int syncout();
  130.                 // two halves of sync() function
  131.     int fillbuf();
  132.     int flushbuf();
  133.                 // Functions which actually transfer to/from
  134.                 // the file
  135.  
  136.     int file;    // File descriptor for the associated file.
  137.     short mode; // I/O mode from the argument to open().
  138.    char unbuf[2];
  139.                // pseudo buffer for unbuffered operation
  140.    char *gptr_;
  141.    char *egptr_;
  142.                // Save old gptr() & egptr() while using the
  143.                // pushback buffer.
  144.    char pushback_buf[4];
  145.                // Reserve buffer for pushback.
  146.                // Only used if there is no room for pushback in
  147.                // the regular buffer.
  148.     char do_not_seek;
  149.                 // Set if the file (device) does not support seeks.
  150.                 // This is set for a TTY, or a Unix pipe.
  151.     char own_file_descriptor;
  152.                 // Set if the file descriptor is from open, and
  153.                 // the file should be closed by the destructor.
  154.     static const int lseek_consts[3];
  155.                 // A look up table for the lseek constants from
  156.                 // the appropriate C header file
  157. };
  158.  
  159. class fstream_common : virtual public ios {
  160.  
  161. // Features common to ifstream, ofstream, and fstream.
  162.  
  163. public:
  164.  
  165.     void attach(int file_descriptor, int io_mode);
  166. // Attach the filebuf to the argument file descriptor, error state
  167. // set to ios::failbit|ios::badbit on failure.
  168.  
  169.     void close();
  170. // Flush the filebuf, and close the file attached to it. Error state
  171. // set ios::failbit|ios::badbit if rdbuf()->sync() fails. File closed
  172. // regardless.
  173.  
  174.     void open(const char *name, int io_mode,
  175.                 int protection = filebuf::openprot);
  176. // Open a file, and attach it to the filebuf. Error state set to
  177. // ios::failbit|ios::badbit on failure
  178.  
  179.     void setbuf(char *memory, int length)
  180.     {
  181.         buffer.setbuf(memory, length);
  182.     }
  183. // Use the argument memory, of the given length, as the I/O buffer.
  184.     filebuf *rdbuf() { return &buffer; }
  185. // Note that fstream_common::rdbuf returns a filebuf*
  186. // instead of a streambuf*.
  187.  
  188. protected:
  189.     fstream_common();
  190.     filebuf buffer;
  191. };
  192.  
  193. class ifstream : public fstream_common, public istream {
  194. public:
  195.     ifstream();
  196. // Create an ifstream not attached to any file.
  197.  
  198.     ifstream(const char *name, int io_mode = ios::in | ios::nocreate
  199. #if M_UNIX || M_XENIX
  200.         ,int protection = filebuf::openprot);
  201. #else
  202.         | ios::translated, int protection = filebuf::openprot);
  203. #endif
  204. // Open the argument file and create an ifstream attached to it.
  205.                 
  206.     ifstream(int file_descriptor, int io_mode = ios::in
  207. #if M_UNIX || M_XENIX
  208.         );
  209. #else
  210.         | ios::translated);
  211. #endif
  212. // Create an ifstream attached to the argument file descriptor.
  213.  
  214.     ifstream(int file_descriptor, char *memory, int length,
  215.                                             int io_mode = ios::in
  216. #if M_UNIX || M_XENIX
  217.         );
  218. #else
  219.         | ios::translated);
  220. #endif
  221. // Create an ifstream attached to the argument file descriptor, and
  222. // using the argument memory as the I/O buffer.
  223.  
  224.     ~ifstream();
  225.  
  226.     void attach(int file_descriptor, int io_mode = ios::in
  227. #if M_UNIX || M_XENIX
  228.         )
  229. #else
  230.         | ios::translated)
  231. #endif
  232.     {
  233.         fstream_common::attach(file_descriptor, io_mode);
  234.     }
  235.  
  236.     void open(const char *name, int io_mode = ios::in
  237. #if M_UNIX || M_XENIX
  238.         ,int protection = filebuf::openprot)
  239. #else
  240.         | ios::translated, int protection = filebuf::openprot)
  241. #endif
  242.     {
  243.         fstream_common::open(name, io_mode, protection);
  244.     }
  245.  
  246. };
  247.  
  248. class ofstream : public fstream_common, public ostream {
  249. public:
  250.     ofstream();
  251. // Create an ofstream not attached to any file.
  252.  
  253.     ofstream(const char *name, int io_mode = ios::out
  254. #if M_UNIX || M_XENIX
  255.         ,int protection = filebuf::openprot);
  256. #else
  257.         | ios::translated, int protection = filebuf::openprot);
  258. #endif
  259. // Open the argument file and create an ofstream attached to it.
  260.                 
  261.     ofstream(int file_descriptor, int io_mode = ios::out
  262. #if M_UNIX || M_XENIX
  263.         );
  264. #else
  265.         | ios::translated);
  266. #endif
  267. // Create an ofstream attached to the argument file descriptor.
  268.  
  269.     ofstream(int file_descriptor, char *memory, int length,
  270.                                     int io_mode = ios::out
  271. #if M_UNIX || M_XENIX
  272.         );
  273. #else
  274.         | ios::translated);
  275. #endif
  276. // Create an ofstream attached to the argument file descriptor, and
  277. // using the argument memory as the I/O buffer.
  278.  
  279.     ~ofstream();
  280.  
  281.     void attach(int file_descriptor, int io_mode = ios::out
  282. #if M_UNIX || M_XENIX
  283.         )
  284. #else
  285.         | ios::translated)
  286. #endif
  287.     {
  288.         fstream_common::attach(file_descriptor, io_mode);
  289.     }
  290.  
  291.     void open(const char *name, int io_mode = ios::out
  292. #if M_UNIX_ || M_XENIX
  293.         ,int protection = filebuf::openprot)
  294. #else
  295.         | ios::translated, int protection = filebuf::openprot)
  296. #endif
  297.     {
  298.         fstream_common::open(name, io_mode, protection);
  299.     }
  300.  
  301. };
  302.  
  303.  
  304. class fstream : public fstream_common, public iostream {
  305. public:
  306.     fstream();
  307. // Create an fstream not attached to any file.
  308.  
  309.     fstream(const char *name, int io_mode = ios::in|ios::out
  310. #if M_UNIX || M_XENIX
  311.         ,int protection = filebuf::openprot);
  312. #else
  313.         | ios::translated, int protection = filebuf::openprot);
  314. #endif
  315. // Open the argument file and create an fstream attached to it.
  316.                 
  317.     fstream(int file_descriptor, int io_mode = ios::in | ios::out
  318. #if M_UNIX || M_XENIX
  319.         );
  320. #else
  321.         | ios::translated);
  322. #endif
  323. // Create an fstream attached to the argument file descriptor.
  324.  
  325.     fstream(int file_descriptor, char *memory, int length,
  326.                                         int io_mode = ios::in | ios::out
  327. #if M_UNIX || M_XENIX
  328.         );
  329. #else
  330.         | ios::translated);
  331. #endif
  332. // Create an fstream attached to the argument file descriptor, and
  333. // using the argument memory as the I/O buffer.
  334.  
  335.     ~fstream();
  336.  
  337.     void attach(int file_descriptor, int io_mode = ios::in | ios::out
  338. #if M_UNIX || M_XENIX
  339.         )
  340. #else
  341.         | ios::translated)
  342. #endif
  343.     {
  344.         fstream_common::attach(file_descriptor, io_mode);
  345.     }
  346.  
  347.     void open(const char *name, int io_mode = ios::in | ios::out
  348. #if M_UNIX || M_XENIX
  349.         ,int protection = filebuf::openprot)
  350. #else
  351.         | ios::translated, int protection = filebuf::openprot)
  352. #endif
  353.     {
  354.         fstream_common::open(name, io_mode, protection);
  355.     }
  356.  
  357. };
  358. #pragma pack()
  359. #endif    // __FSTREAM_H
  360.