home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / fstream.cpp < prev    next >
C/C++ Source or Header  |  1998-06-17  |  6KB  |  257 lines

  1. /***
  2. *fstream.cpp -
  3. *
  4. *       Copyright (c) 1991-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Contains the member functions for the fstream class.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <internal.h>
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <sys\types.h>
  17. #include <io.h>
  18. #include <fstream.h>
  19. #include <dbgint.h>
  20. #pragma hdrstop
  21.  
  22. #include <sys\stat.h>
  23.  
  24. /***
  25. *fstream::fstream() - fstream default constructor
  26. *
  27. *Purpose:
  28. *       Default constructor for fstream objects.
  29. *
  30. *Entry:
  31. *       None.
  32. *
  33. *Exit:
  34. *       None.
  35. *
  36. *Exceptions:
  37. *
  38. *******************************************************************************/
  39.         fstream::fstream()
  40. : iostream(_new_crt filebuf)
  41. {
  42.     istream::delbuf(1);
  43.     ostream::delbuf(1);
  44. }
  45.  
  46. /***
  47. *fstream::fstream(const char * name, int mode, int prot) - fstream constructor
  48. *
  49. *Purpose:
  50. *       Constructor for fstream objects.  Creates an associated filebuf object,
  51. *       opens a named file and attaches it to the new filebuf.
  52. *
  53. *Entry:
  54. *       name = filename to open.
  55. *       mode = see filebuf::open mode argument
  56. *       prot = see filebuf::open share argument
  57. *
  58. *Exit:
  59. *       None.
  60. *
  61. *Exceptions:
  62. *       Sets failbit if open fails.
  63. *
  64. *******************************************************************************/
  65.         fstream::fstream(const char * name, int mode, int prot)
  66. : iostream(_new_crt filebuf)
  67. {
  68.     istream::delbuf(1);
  69.     ostream::delbuf(1);
  70.     if (!(rdbuf()->open(name, mode, prot)))
  71.         {
  72.         istream::state = istream::failbit;
  73.         ostream::state = ostream::failbit;
  74.         }
  75. }
  76.  
  77. /***
  78. *fstream::fstream(filedesc fd) - fstream constructor
  79. *
  80. *Purpose:
  81. *       Constructor for fstream objects.  Creates an associated filebuf object
  82. *       and attaches it to the given file descriptor.
  83. *
  84. *Entry:
  85. *       fd = file descriptor of file previously opened using _open or _sopen.
  86. *
  87. *Exit:
  88. *       None.
  89. *
  90. *Exceptions:
  91. *
  92. *******************************************************************************/
  93.         fstream::fstream(filedesc _fd)
  94. : iostream(_new_crt filebuf(_fd))
  95. {
  96.     istream::delbuf(1);
  97.     ostream::delbuf(1);
  98. }
  99.  
  100. /***
  101. *fstream::fstream(filedesc fd, char * sbuf, int len) - fstream constructor
  102. *
  103. *Purpose:
  104. *       Constructor for fstream objects.  Creates an associated filebuf object
  105. *       and attaches it to the given file descriptor.  Filebuf object uses
  106. *       user-supplied buffer or is unbuffered if sbuf or len = 0.
  107. *
  108. *Entry:
  109. *       fd   = file descriptor of file previously opened using _open or _sopen.
  110. *       sbuf = pointer to character buffer or NULL if request for unbuffered.
  111. *       len  = lenght of character buffer sbuf or 0 if request for unbuffered.
  112. *
  113. *Exit:
  114. *       None.
  115. *
  116. *Exceptions:
  117. *
  118. *******************************************************************************/
  119.         fstream::fstream(filedesc _fd, char * sbuf, int len)
  120. : iostream(_new_crt filebuf(_fd, sbuf, len))
  121. {
  122.     istream::delbuf(1);
  123.     ostream::delbuf(1);
  124. }
  125.  
  126. /***
  127. *fstream::~fstream() - fstream destructor
  128. *
  129. *Purpose:
  130. *       fstream destructor.
  131. *
  132. *Entry:
  133. *       None.
  134. *
  135. *Exit:
  136. *       None.
  137. *
  138. *Exceptions:
  139. *
  140. *******************************************************************************/
  141.         fstream::~fstream()
  142. {
  143. }
  144.  
  145. /***
  146. *streambuf* fstream::setbuf(char * ptr, int len) - setbuf function
  147. *
  148. *Purpose:
  149. *       fstream setbuf function
  150. *
  151. *Entry:
  152. *       ptr = pointer to buffer or NULL for unbuffered.
  153. *       len = length of buffer or zero for unbuffered.
  154. *
  155. *Exit:
  156. *       Returns rdbuf() or NULL if error.
  157. *
  158. *Exceptions:
  159. *       If fstream is already open or if rdbuf()->setbuf fails, sets failbit
  160. *       and returns NULL.
  161. *
  162. *******************************************************************************/
  163. streambuf * fstream::setbuf(char * ptr, int len)
  164. {
  165.     if ((is_open()) || (!(rdbuf()->setbuf(ptr, len))))
  166.         {
  167.         istream::clear(istream::state | istream::failbit);
  168.         ostream::clear(ostream::state | ostream::failbit);
  169.         return NULL;
  170.         }
  171.     return rdbuf();
  172. }
  173.  
  174. /***
  175. *void fstream::attach(filedesc _fd) - attach member function
  176. *
  177. *Purpose:
  178. *       fstream attach member function.  Just calls rdbuf()->attach().
  179. *
  180. *Entry:
  181. *       _fd = file descriptor of previously opened file.
  182. *
  183. *Exit:
  184. *       None.
  185. *
  186. *Exceptions:
  187. *       Sets failbit if rdbuf()->attach fails.
  188. *
  189. *******************************************************************************/
  190. void fstream::attach(filedesc _fd)
  191. {
  192.     if (!(rdbuf()->attach(_fd)))
  193.         {
  194.         istream::clear(istream::state | istream::failbit);
  195.         ostream::clear(ostream::state | ostream::failbit);
  196.         }
  197. }
  198.  
  199. /***
  200. *void fstream::open(const char * name, int mode, int prot) - fstream open()
  201. *
  202. *Purpose:
  203. *       Opens a named file and attaches it to the associated filebuf.
  204. *       Just calls rdbuf()->open().
  205. *
  206. *Entry:
  207. *       name = filename to open.
  208. *       mode = see filebuf::open mode argument
  209. *       prot = see filebuf::open share argument
  210. *
  211. *Exit:
  212. *       None.
  213. *
  214. *Exceptions:
  215. *       Sets failbit if already open or rdbuf()->open() fails.
  216. *
  217. *******************************************************************************/
  218. void fstream::open(const char * name, int mode, int prot)
  219. {
  220.     if (is_open() || !(rdbuf()->open(name, mode, prot)))
  221.         {
  222.         istream::clear(istream::state | istream::failbit);
  223.         ostream::clear(ostream::state | ostream::failbit);
  224.         }
  225. }
  226.  
  227. /***
  228. *void fstream::close() - close member function
  229. *
  230. *Purpose:
  231. *       fstream close member function.  Just calls rdbuf()->close().
  232. *       Clears rdstate() error bits if successful.
  233. *
  234. *Entry:
  235. *       None.
  236. *
  237. *Exit:
  238. *       None.
  239. *
  240. *Exceptions:
  241. *       Sets failbit if rdbuf()->close fails.
  242. *
  243. *******************************************************************************/
  244. void fstream::close()
  245. {
  246.     if (rdbuf()->close())
  247.         {
  248.         istream::clear();
  249.         ostream::clear();
  250.         }
  251.     else
  252.         {
  253.         istream::clear(istream::state | istream::failbit);
  254.         ostream::clear(ostream::state | ostream::failbit);
  255.         }
  256. }
  257.