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

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