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

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