Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
5.5 KiB

  1. /***
  2. *ofstream.cpp -
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Contains the member functions for the ofstream class.
  8. *
  9. *Revision History:
  10. * 09-21-91 KRS Created. Split up fstream.cxx for granularity.
  11. * 10-22-91 KRS C700 #4883: fix error status for ofstream::open().
  12. * 01-12-95 CFW Debug CRT allocs.
  13. * 06-14-95 CFW Comment cleanup.
  14. *
  15. *******************************************************************************/
  16. #include <cruntime.h>
  17. #include <internal.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <fcntl.h>
  21. #include <sys\types.h>
  22. #include <io.h>
  23. #include <fstream.h>
  24. #include <dbgint.h>
  25. #pragma hdrstop
  26. #include <sys\stat.h>
  27. /***
  28. *ofstream::ofstream() - ofstream default constructor
  29. *
  30. *Purpose:
  31. * Default constructor for ofstream objects.
  32. *
  33. *Entry:
  34. * None.
  35. *
  36. *Exit:
  37. * None.
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42. ofstream::ofstream()
  43. : ostream(_new_crt filebuf)
  44. {
  45. delbuf(1);
  46. }
  47. /***
  48. *ofstream::ofstream(const char * name, int mode, int prot) - ofstream ctor
  49. *
  50. *Purpose:
  51. * Constructor for ofstream objects. Creates an associated filebuf object,
  52. * opens a named file and attaches it to the new filebuf.
  53. *
  54. *Entry:
  55. * name = filename to open.
  56. * mode = see filebuf::open mode argument. ios::out is implied.
  57. * prot = see filebuf::open share argument
  58. *
  59. *Exit:
  60. * None.
  61. *
  62. *Exceptions:
  63. * Sets failbit if open fails.
  64. *
  65. *******************************************************************************/
  66. ofstream::ofstream(const char * name, int mode, int prot)
  67. : ostream(_new_crt filebuf)
  68. {
  69. delbuf(1);
  70. if (!(rdbuf()->open(name, (mode|ios::out), prot)))
  71. state |= ios::failbit;
  72. }
  73. /***
  74. *ofstream::ofstream(filedesc fd) - ofstream constructor
  75. *
  76. *Purpose:
  77. * Constructor for ofstream objects. Creates an associated filebuf object
  78. * and attaches it to the given file descriptor.
  79. *
  80. *Entry:
  81. * fd = file descriptor of file previously opened using _open or _sopen.
  82. *
  83. *Exit:
  84. * None.
  85. *
  86. *Exceptions:
  87. *
  88. *******************************************************************************/
  89. ofstream::ofstream(filedesc _fd)
  90. : ostream(_new_crt filebuf(_fd))
  91. {
  92. delbuf(1);
  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. *ofstream::~ofstream() - ofstream destructor
  120. *
  121. *Purpose:
  122. * ofstream destructor.
  123. *
  124. *Entry:
  125. * None.
  126. *
  127. *Exit:
  128. * None.
  129. *
  130. *Exceptions:
  131. *
  132. *******************************************************************************/
  133. ofstream::~ofstream()
  134. {
  135. }
  136. /***
  137. *streambuf* ofstream::setbuf(char * ptr, int len) - setbuf function
  138. *
  139. *Purpose:
  140. * ofstream setbuf function
  141. *
  142. *Entry:
  143. * ptr = pointer to buffer or NULL for unbuffered.
  144. * len = length of buffer or zero for unbuffered.
  145. *
  146. *Exit:
  147. * Returns rdbuf() or NULL if error.
  148. *
  149. *Exceptions:
  150. * If ofstream is already open or if rdbuf()->setbuf fails, sets failbit
  151. * and returns NULL.
  152. *
  153. *******************************************************************************/
  154. streambuf * ofstream::setbuf(char * ptr, int len)
  155. {
  156. if ((is_open()) || (!(rdbuf()->setbuf(ptr, len))))
  157. {
  158. clear(state | ios::failbit);
  159. return NULL;
  160. }
  161. return rdbuf();
  162. }
  163. /***
  164. *void ofstream::attach(filedesc _fd) - attach member function
  165. *
  166. *Purpose:
  167. * ofstream attach member function. Just calls rdbuf()->attach().
  168. *
  169. *Entry:
  170. * _fd = file descriptor of previously opened file.
  171. *
  172. *Exit:
  173. * None.
  174. *
  175. *Exceptions:
  176. * Sets failbit if rdbuf()->attach fails.
  177. *
  178. *******************************************************************************/
  179. void ofstream::attach(filedesc _fd)
  180. {
  181. if (!(rdbuf()->attach(_fd)))
  182. clear(state | ios::failbit);
  183. }
  184. /***
  185. *void ofstream::open(const char * name, int mode, int prot) - ofstream open()
  186. *
  187. *Purpose:
  188. * Opens a named file and attaches it to the associated filebuf.
  189. * Just calls rdbuf()->open().
  190. *
  191. *Entry:
  192. * name = filename to open.
  193. * mode = see filebuf::open mode argument. ios::out is implied.
  194. * prot = see filebuf::open share argument
  195. *
  196. *Exit:
  197. * None.
  198. *
  199. *Exceptions:
  200. * Sets failbit if already open or rdbuf()->open() fails.
  201. *
  202. *******************************************************************************/
  203. void ofstream::open(const char * name, int mode, int prot)
  204. {
  205. if (is_open() || !(rdbuf()->open(name, (mode|ios::out), prot)))
  206. clear(state | ios::failbit);
  207. }
  208. /***
  209. *void ofstream::close() - close member function
  210. *
  211. *Purpose:
  212. * ofstream close member function. Just calls rdbuf()->close().
  213. * Clears rdstate() error bits if successful.
  214. *
  215. *Entry:
  216. * None.
  217. *
  218. *Exit:
  219. * None.
  220. *
  221. *Exceptions:
  222. * Sets failbit if rdbuf()->close fails.
  223. *
  224. *******************************************************************************/
  225. void ofstream::close()
  226. {
  227. clear((rdbuf()->close()) ? 0 : (state | ios::failbit));
  228. }