home *** CD-ROM | disk | FTP | other *** search
/ QuickTime 2.0 Developer Kit / QuickTime 2.0 Developer Kit.iso / mac / MAC / Programming Stuff / Interfaces / CIncludes / fstream.h < prev    next >
Encoding:
Text File  |  1994-11-28  |  9.4 KB  |  359 lines  |  [TEXT/MPS ]

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