pandaFileStream.I 9.8 KB

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