Panda3D
physxFileStream.cxx
Go to the documentation of this file.
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 physxFileStream.cxx
10  * @author enn0x
11  * @date 2009-10-11
12  */
13 
14 #include "physxFileStream.h"
15 
16 #include <stdio.h>
17 
18 #include "virtualFileSystem.h"
19 
20 /**
21  *
22  */
23 PhysxFileStream::PhysxFileStream(const Filename &fn, bool load) : _fp(nullptr), _vf(nullptr), _in(nullptr)
24 {
25  if (load) {
27  _in = _vf->open_read_file(true);
28  }
29  else {
30  _fp = fopen(fn.c_str(), "wb");
31  }
32 }
33 
34 /**
35  *
36  */
37 PhysxFileStream::~PhysxFileStream()
38 {
39  if (_fp) fclose(_fp);
40  if (_vf) _vf->close_read_file(_in);
41 }
42 
43 /**
44  *
45  */
46 NxU8 PhysxFileStream::readByte() const
47 {
48  NxU8 b;
49  _in->read((char *)&b, sizeof(NxU8));
50  NX_ASSERT(!(_in->bad()));
51  return b;
52 }
53 
54 /**
55  *
56  */
57 NxU16 PhysxFileStream::readWord() const
58 {
59  NxU16 w;
60  _in->read((char *)&w, sizeof(NxU16));
61  NX_ASSERT(!(_in->bad()));
62  return w;
63 }
64 
65 /**
66  *
67  */
68 NxU32 PhysxFileStream::readDword() const
69 {
70  NxU32 d;
71  _in->read((char *)&d, sizeof(NxU32));
72  NX_ASSERT(!(_in->bad()));
73  return d;
74 }
75 
76 /**
77  *
78  */
79 float PhysxFileStream::readFloat() const
80 {
81  NxReal f;
82  _in->read((char *)&f, sizeof(NxReal));
83  NX_ASSERT(!(_in->bad()));
84  return f;
85 }
86 
87 /**
88  *
89  */
90 double PhysxFileStream::readDouble() const
91 {
92  NxF64 f;
93  _in->read((char *)&f, sizeof(NxF64));
94  NX_ASSERT(!(_in->bad()));
95  return f;
96 }
97 
98 /**
99  *
100  */
101 void PhysxFileStream::readBuffer(void *buffer, NxU32 size) const
102 {
103  _in->read((char *)buffer, size);
104  NX_ASSERT(!(_in->bad()));
105 }
106 
107 /**
108  *
109  */
110 NxStream &PhysxFileStream::storeByte(NxU8 b)
111 {
112  size_t w = fwrite(&b, sizeof(NxU8), 1, _fp);
113  NX_ASSERT(w);
114  return *this;
115 }
116 
117 /**
118  *
119  */
120 NxStream &PhysxFileStream::storeWord(NxU16 w)
121 {
122  size_t ww = fwrite(&w, sizeof(NxU16), 1, _fp);
123  NX_ASSERT(ww);
124  return *this;
125 }
126 
127 /**
128  *
129  */
130 NxStream &PhysxFileStream::storeDword(NxU32 d)
131 {
132  size_t w = fwrite(&d, sizeof(NxU32), 1, _fp);
133  NX_ASSERT(w);
134  return *this;
135 }
136 
137 /**
138  *
139  */
140 NxStream &PhysxFileStream::storeFloat(NxReal f)
141 {
142  size_t w = fwrite(&f, sizeof(NxReal), 1, _fp);
143  NX_ASSERT(w);
144  return *this;
145 }
146 
147 /**
148  *
149  */
150 NxStream &PhysxFileStream::storeDouble(NxF64 f)
151 {
152  size_t w = fwrite(&f, sizeof(NxF64), 1, _fp);
153  NX_ASSERT(w);
154  return *this;
155 }
156 
157 /**
158  *
159  */
160 NxStream &PhysxFileStream::storeBuffer(const void *buffer, NxU32 size)
161 {
162  size_t w = fwrite(buffer, size, 1, _fp);
163  NX_ASSERT(w);
164  return *this;
165 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
PointerTo< VirtualFile > get_file(const Filename &filename, bool status_only=false) const
Looks up the file by the indicated name in the file system.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.