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.

262 lines
6.0 KiB

  1. /***
  2. *fstream.cpp -
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Contains the member functions for the fstream class.
  8. *
  9. *Revision History:
  10. * 09-21-91 KRS Created. Split off from filebuf.cxx for granularity.
  11. * 10-22-91 KRS C700 #4883: fix error status of fstream::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. *fstream::fstream() - fstream default constructor
  29. *
  30. *Purpose:
  31. * Default constructor for fstream objects.
  32. *
  33. *Entry:
  34. * None.
  35. *
  36. *Exit:
  37. * None.
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42. fstream::fstream()
  43. : iostream(_new_crt filebuf)
  44. {
  45. istream::delbuf(1);
  46. ostream::delbuf(1);
  47. }
  48. /***
  49. *fstream::fstream(const char * name, int mode, int prot) - fstream constructor
  50. *
  51. *Purpose:
  52. * Constructor for fstream objects. Creates an associated filebuf object,
  53. * opens a named file and attaches it to the new filebuf.
  54. *
  55. *Entry:
  56. * name = filename to open.
  57. * mode = see filebuf::open mode argument
  58. * prot = see filebuf::open share argument
  59. *
  60. *Exit:
  61. * None.
  62. *
  63. *Exceptions:
  64. * Sets failbit if open fails.
  65. *
  66. *******************************************************************************/
  67. fstream::fstream(const char * name, int mode, int prot)
  68. : iostream(_new_crt filebuf)
  69. {
  70. istream::delbuf(1);
  71. ostream::delbuf(1);
  72. if (!(rdbuf()->open(name, mode, prot)))
  73. {
  74. istream::state = istream::failbit;
  75. ostream::state = ostream::failbit;
  76. }
  77. }
  78. /***
  79. *fstream::fstream(filedesc fd) - fstream constructor
  80. *
  81. *Purpose:
  82. * Constructor for fstream objects. Creates an associated filebuf object
  83. * and attaches it to the given file descriptor.
  84. *
  85. *Entry:
  86. * fd = file descriptor of file previously opened using _open or _sopen.
  87. *
  88. *Exit:
  89. * None.
  90. *
  91. *Exceptions:
  92. *
  93. *******************************************************************************/
  94. fstream::fstream(filedesc _fd)
  95. : iostream(_new_crt filebuf(_fd))
  96. {
  97. istream::delbuf(1);
  98. ostream::delbuf(1);
  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. *fstream::~fstream() - fstream destructor
  127. *
  128. *Purpose:
  129. * fstream destructor.
  130. *
  131. *Entry:
  132. * None.
  133. *
  134. *Exit:
  135. * None.
  136. *
  137. *Exceptions:
  138. *
  139. *******************************************************************************/
  140. fstream::~fstream()
  141. {
  142. }
  143. /***
  144. *streambuf* fstream::setbuf(char * ptr, int len) - setbuf function
  145. *
  146. *Purpose:
  147. * fstream setbuf function
  148. *
  149. *Entry:
  150. * ptr = pointer to buffer or NULL for unbuffered.
  151. * len = length of buffer or zero for unbuffered.
  152. *
  153. *Exit:
  154. * Returns rdbuf() or NULL if error.
  155. *
  156. *Exceptions:
  157. * If fstream is already open or if rdbuf()->setbuf fails, sets failbit
  158. * and returns NULL.
  159. *
  160. *******************************************************************************/
  161. streambuf * fstream::setbuf(char * ptr, int len)
  162. {
  163. if ((is_open()) || (!(rdbuf()->setbuf(ptr, len))))
  164. {
  165. istream::clear(istream::state | istream::failbit);
  166. ostream::clear(ostream::state | ostream::failbit);
  167. return NULL;
  168. }
  169. return rdbuf();
  170. }
  171. /***
  172. *void fstream::attach(filedesc _fd) - attach member function
  173. *
  174. *Purpose:
  175. * fstream attach member function. Just calls rdbuf()->attach().
  176. *
  177. *Entry:
  178. * _fd = file descriptor of previously opened file.
  179. *
  180. *Exit:
  181. * None.
  182. *
  183. *Exceptions:
  184. * Sets failbit if rdbuf()->attach fails.
  185. *
  186. *******************************************************************************/
  187. void fstream::attach(filedesc _fd)
  188. {
  189. if (!(rdbuf()->attach(_fd)))
  190. {
  191. istream::clear(istream::state | istream::failbit);
  192. ostream::clear(ostream::state | ostream::failbit);
  193. }
  194. }
  195. /***
  196. *void fstream::open(const char * name, int mode, int prot) - fstream open()
  197. *
  198. *Purpose:
  199. * Opens a named file and attaches it to the associated filebuf.
  200. * Just calls rdbuf()->open().
  201. *
  202. *Entry:
  203. * name = filename to open.
  204. * mode = see filebuf::open mode argument
  205. * prot = see filebuf::open share argument
  206. *
  207. *Exit:
  208. * None.
  209. *
  210. *Exceptions:
  211. * Sets failbit if already open or rdbuf()->open() fails.
  212. *
  213. *******************************************************************************/
  214. void fstream::open(const char * name, int mode, int prot)
  215. {
  216. if (is_open() || !(rdbuf()->open(name, mode, prot)))
  217. {
  218. istream::clear(istream::state | istream::failbit);
  219. ostream::clear(ostream::state | ostream::failbit);
  220. }
  221. }
  222. /***
  223. *void fstream::close() - close member function
  224. *
  225. *Purpose:
  226. * fstream close member function. Just calls rdbuf()->close().
  227. * Clears rdstate() error bits if successful.
  228. *
  229. *Entry:
  230. * None.
  231. *
  232. *Exit:
  233. * None.
  234. *
  235. *Exceptions:
  236. * Sets failbit if rdbuf()->close fails.
  237. *
  238. *******************************************************************************/
  239. void fstream::close()
  240. {
  241. if (rdbuf()->close())
  242. {
  243. istream::clear();
  244. ostream::clear();
  245. }
  246. else
  247. {
  248. istream::clear(istream::state | istream::failbit);
  249. ostream::clear(ostream::state | ostream::failbit);
  250. }
  251. }