Source code of Windows XP (NT5)
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.

240 lines
5.4 KiB

  1. /***
  2. *ifstream.cpp - ifstream member function definitions
  3. *
  4. * Copyright (c) 1991-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Contains the member functions for the ifstream 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 ifstream::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. *ifstream::ifstream() - ifstream default constructor
  29. *
  30. *Purpose:
  31. * Default constructor for ifstream objects.
  32. *
  33. *Entry:
  34. * None.
  35. *
  36. *Exit:
  37. * None.
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42. ifstream::ifstream()
  43. : istream(_new_crt filebuf)
  44. {
  45. delbuf(1); // schedule automatic deletion too...
  46. }
  47. /***
  48. *ifstream::ifstream(const char * name, int mode, int prot) - ifstream ctor
  49. *
  50. *Purpose:
  51. * Constructor for ifstream 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::in 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. ifstream::ifstream(const char * name, int mode, int prot)
  67. : istream(_new_crt filebuf)
  68. {
  69. delbuf(1); // schedule automatic deletion too...
  70. if (!(rdbuf()->open(name, (mode|ios::in), prot)))
  71. state |= ios::failbit;
  72. }
  73. /***
  74. *ifstream::ifstream(filedesc fd) - ifstream constructor
  75. *
  76. *Purpose:
  77. * Constructor for ifstream 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. ifstream::ifstream(filedesc _fd)
  90. : istream(_new_crt filebuf(_fd))
  91. {
  92. delbuf(1);
  93. }
  94. /***
  95. *ifstream::ifstream(filedesc fd, char * sbuf, int len) - ifstream constructor
  96. *
  97. *Purpose:
  98. * Constructor for ifstream 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. ifstream::ifstream(filedesc _fd, char * sbuf, int len)
  114. : istream(_new_crt filebuf(_fd, sbuf, len))
  115. //: istream(new filebuf);
  116. {
  117. delbuf(1);
  118. }
  119. /***
  120. *ifstream::~ifstream() - ifstream destructor
  121. *
  122. *Purpose:
  123. * ifstream destructor.
  124. *
  125. *Entry:
  126. * None.
  127. *
  128. *Exit:
  129. * None.
  130. *
  131. *Exceptions:
  132. *
  133. *******************************************************************************/
  134. ifstream::~ifstream()
  135. {
  136. }
  137. /***
  138. *streambuf* ifstream::setbuf(char * ptr, int len) - setbuf function
  139. *
  140. *Purpose:
  141. * ifstream setbuf function
  142. *
  143. *Entry:
  144. * ptr = pointer to buffer or NULL for unbuffered.
  145. * len = length of buffer or zero for unbuffered.
  146. *
  147. *Exit:
  148. * Returns rdbuf() or NULL if error.
  149. *
  150. *Exceptions:
  151. * If ifstream is already open or if rdbuf()->setbuf fails, sets failbit
  152. * and returns NULL.
  153. *
  154. *******************************************************************************/
  155. streambuf * ifstream::setbuf(char * ptr, int len)
  156. {
  157. if ((is_open()) || (!(rdbuf()->setbuf(ptr, len))))
  158. {
  159. clear(state | ios::failbit);
  160. return NULL;
  161. }
  162. return rdbuf();
  163. }
  164. /***
  165. *void ifstream::attach(filedesc _fd) - attach member function
  166. *
  167. *Purpose:
  168. * ifstream attach member function. Just calls rdbuf()->attach().
  169. *
  170. *Entry:
  171. * _fd = file descriptor of previously opened file.
  172. *
  173. *Exit:
  174. * None.
  175. *
  176. *Exceptions:
  177. * Sets failbit if rdbuf()->attach fails.
  178. *
  179. *******************************************************************************/
  180. void ifstream::attach(filedesc _fd)
  181. {
  182. if (!(rdbuf()->attach(_fd)))
  183. clear(state | ios::failbit);
  184. }
  185. /***
  186. *void ifstream::open(const char * name, int mode, int prot) - ifstream open()
  187. *
  188. *Purpose:
  189. * Opens a named file and attaches it to the associated filebuf.
  190. * Just calls rdbuf()->open().
  191. *
  192. *Entry:
  193. * name = filename to open.
  194. * mode = see filebuf::open mode argument. ios::in is implied.
  195. * prot = see filebuf::open share argument
  196. *
  197. *Exit:
  198. * None.
  199. *
  200. *Exceptions:
  201. * Sets failbit if already open or rdbuf()->open() fails.
  202. *
  203. *******************************************************************************/
  204. void ifstream::open(const char * name, int mode, int prot)
  205. {
  206. if (is_open() || !(rdbuf()->open(name, (mode|ios::in), prot)))
  207. clear(state | ios::failbit);
  208. }
  209. /***
  210. *void ifstream::close() - close member function
  211. *
  212. *Purpose:
  213. * ifstream close member function. Just calls rdbuf()->close().
  214. * Clears rdstate() error bits if successful.
  215. *
  216. *Entry:
  217. * None.
  218. *
  219. *Exit:
  220. * None.
  221. *
  222. *Exceptions:
  223. * Sets failbit if rdbuf()->close fails.
  224. *
  225. *******************************************************************************/
  226. void ifstream::close()
  227. {
  228. clear((rdbuf()->close()) ? 0 : (state | ios::failbit));
  229. }