pandaFileStream.I 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file pandaFileStream.I
  10. * @author drose
  11. * @date 2008-09-08
  12. */
  13. /**
  14. *
  15. */
  16. INLINE IFileStream::
  17. IFileStream() : std::istream(&_buf) {
  18. }
  19. /**
  20. *
  21. */
  22. INLINE IFileStream::
  23. IFileStream(const char *filename, std::ios::openmode mode) : std::istream(&_buf) {
  24. open(filename, mode);
  25. }
  26. /**
  27. *
  28. */
  29. INLINE IFileStream::
  30. ~IFileStream() {
  31. close();
  32. }
  33. /**
  34. *
  35. */
  36. INLINE void IFileStream::
  37. open(const char *filename, std::ios::openmode mode) {
  38. clear((ios_iostate)0);
  39. _buf.open(filename, mode);
  40. if (!_buf.is_open()) {
  41. clear(std::ios::failbit);
  42. }
  43. }
  44. #ifdef _WIN32
  45. /**
  46. * Connects the file stream to the existing OS-defined stream, presumably
  47. * opened via a low-level OS call. The filename is for reporting only. When
  48. * the file stream is closed, it will also close the underlying OS handle.
  49. *
  50. * This function is the Windows-specific variant.
  51. */
  52. void IFileStream::
  53. attach(const char *filename, HANDLE handle, std::ios::openmode mode) {
  54. clear((ios_iostate)0);
  55. _buf.attach(filename, handle, mode);
  56. if (!_buf.is_open()) {
  57. clear(std::ios::failbit);
  58. }
  59. }
  60. #endif // _WIN32
  61. #ifndef _WIN32
  62. /**
  63. * Connects the file stream to the existing OS-defined stream, presumably
  64. * opened via a low-level OS call. The filename is for reporting only. When
  65. * the file stream is closed, it will also close the underlying OS handle.
  66. *
  67. * This function is the Posix-specific variant.
  68. */
  69. void IFileStream::
  70. attach(const char *filename, int fd, std::ios::openmode mode) {
  71. clear((ios_iostate)0);
  72. _buf.attach(filename, fd, mode);
  73. if (!_buf.is_open()) {
  74. clear(std::ios::failbit);
  75. }
  76. }
  77. #endif // _WIN32
  78. /**
  79. *
  80. */
  81. INLINE void IFileStream::
  82. close() {
  83. _buf.close();
  84. }
  85. /**
  86. *
  87. */
  88. INLINE OFileStream::
  89. OFileStream() : std::ostream(&_buf) {
  90. }
  91. /**
  92. *
  93. */
  94. INLINE OFileStream::
  95. OFileStream(const char *filename, std::ios::openmode mode) : std::ostream(&_buf) {
  96. open(filename, mode);
  97. }
  98. /**
  99. *
  100. */
  101. INLINE OFileStream::
  102. ~OFileStream() {
  103. close();
  104. }
  105. /**
  106. *
  107. */
  108. INLINE void OFileStream::
  109. open(const char *filename, std::ios::openmode mode) {
  110. clear((ios_iostate)0);
  111. _buf.open(filename, mode);
  112. if (!_buf.is_open()) {
  113. clear(std::ios::failbit);
  114. }
  115. }
  116. #ifdef _WIN32
  117. /**
  118. * Connects the file stream to the existing OS-defined stream, presumably
  119. * opened via a low-level OS call. The filename is for reporting only. When
  120. * the file stream is closed, it will also close the underlying OS handle.
  121. *
  122. * This function is the Windows-specific variant.
  123. */
  124. void OFileStream::
  125. attach(const char *filename, HANDLE handle, std::ios::openmode mode) {
  126. clear((ios_iostate)0);
  127. _buf.attach(filename, handle, mode);
  128. if (!_buf.is_open()) {
  129. clear(std::ios::failbit);
  130. }
  131. }
  132. #endif // _WIN32
  133. #ifndef _WIN32
  134. /**
  135. * Connects the file stream to the existing OS-defined stream, presumably
  136. * opened via a low-level OS call. The filename is for reporting only. When
  137. * the file stream is closed, it will also close the underlying OS handle.
  138. *
  139. * This function is the Posix-specific variant.
  140. */
  141. void OFileStream::
  142. attach(const char *filename, int fd, std::ios::openmode mode) {
  143. clear((ios_iostate)0);
  144. _buf.attach(filename, fd, mode);
  145. if (!_buf.is_open()) {
  146. clear(std::ios::failbit);
  147. }
  148. }
  149. #endif // _WIN32
  150. /**
  151. *
  152. */
  153. INLINE void OFileStream::
  154. close() {
  155. _buf.close();
  156. }
  157. /**
  158. *
  159. */
  160. INLINE FileStream::
  161. FileStream() : std::iostream(&_buf) {
  162. }
  163. /**
  164. *
  165. */
  166. INLINE FileStream::
  167. FileStream(const char *filename, std::ios::openmode mode) : std::iostream(&_buf) {
  168. open(filename, mode);
  169. }
  170. /**
  171. *
  172. */
  173. INLINE FileStream::
  174. ~FileStream() {
  175. close();
  176. }
  177. /**
  178. *
  179. */
  180. INLINE void FileStream::
  181. open(const char *filename, std::ios::openmode mode) {
  182. clear((ios_iostate)0);
  183. _buf.open(filename, mode);
  184. if (!_buf.is_open()) {
  185. clear(std::ios::failbit);
  186. }
  187. }
  188. #ifdef _WIN32
  189. /**
  190. * Connects the file stream to the existing OS-defined stream, presumably
  191. * opened via a low-level OS call. The filename is for reporting only. When
  192. * the file stream is closed, it will also close the underlying OS handle.
  193. *
  194. * This function is the Windows-specific variant.
  195. */
  196. void FileStream::
  197. attach(const char *filename, HANDLE handle, std::ios::openmode mode) {
  198. clear((ios_iostate)0);
  199. _buf.attach(filename, handle, mode);
  200. if (!_buf.is_open()) {
  201. clear(std::ios::failbit);
  202. }
  203. }
  204. #endif // _WIN32
  205. #ifndef _WIN32
  206. /**
  207. * Connects the file stream to the existing OS-defined stream, presumably
  208. * opened via a low-level OS call. The filename is for reporting only. When
  209. * the file stream is closed, it will also close the underlying OS handle.
  210. *
  211. * This function is the Posix-specific variant.
  212. */
  213. void FileStream::
  214. attach(const char *filename, int fd, std::ios::openmode mode) {
  215. clear((ios_iostate)0);
  216. _buf.attach(filename, fd, mode);
  217. if (!_buf.is_open()) {
  218. clear(std::ios::failbit);
  219. }
  220. }
  221. #endif // _WIN32
  222. /**
  223. *
  224. */
  225. INLINE void FileStream::
  226. close() {
  227. _buf.close();
  228. }