Panda3D
physxFileStream.cxx
1 // Filename: physxFileStream.cxx
2 // Created by: enn0x (11Oct09)
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 #include "physxFileStream.h"
16 
17 #include "stdio.h"
18 
19 #include "virtualFileSystem.h"
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: PhysxFileStream::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 PhysxFileStream::PhysxFileStream(const Filename &fn, bool load) : _fp(NULL), _vf(NULL), _in(NULL)
27 {
28  if (load) {
30  _in = _vf->open_read_file(true);
31  }
32  else {
33  _fp = fopen(fn.c_str(), "wb");
34  }
35 }
36 
37 ////////////////////////////////////////////////////////////////////
38 // Function: PhysxFileStream::Destructor
39 // Access: Public
40 // Description:
41 ////////////////////////////////////////////////////////////////////
42 PhysxFileStream::~PhysxFileStream()
43 {
44  if (_fp) fclose(_fp);
45  if (_vf) _vf->close_read_file(_in);
46 }
47 
48 ////////////////////////////////////////////////////////////////////
49 // Function: PhysxFileStream::readByte
50 // Access: Public
51 // Description:
52 ////////////////////////////////////////////////////////////////////
53 NxU8 PhysxFileStream::readByte() const
54 {
55  NxU8 b;
56  _in->read((char *)&b, sizeof(NxU8));
57  NX_ASSERT(!(_in->bad()));
58  return b;
59 }
60 
61 ////////////////////////////////////////////////////////////////////
62 // Function: PhysxFileStream::readWord
63 // Access: Public
64 // Description:
65 ////////////////////////////////////////////////////////////////////
66 NxU16 PhysxFileStream::readWord() const
67 {
68  NxU16 w;
69  _in->read((char *)&w, sizeof(NxU16));
70  NX_ASSERT(!(_in->bad()));
71  return w;
72 }
73 
74 ////////////////////////////////////////////////////////////////////
75 // Function: PhysxFileStream::readDword
76 // Access: Public
77 // Description:
78 ////////////////////////////////////////////////////////////////////
79 NxU32 PhysxFileStream::readDword() const
80 {
81  NxU32 d;
82  _in->read((char *)&d, sizeof(NxU32));
83  NX_ASSERT(!(_in->bad()));
84  return d;
85 }
86 
87 ////////////////////////////////////////////////////////////////////
88 // Function: PhysxFileStream::readFloat
89 // Access: Public
90 // Description:
91 ////////////////////////////////////////////////////////////////////
92 float PhysxFileStream::readFloat() const
93 {
94  NxReal f;
95  _in->read((char *)&f, sizeof(NxReal));
96  NX_ASSERT(!(_in->bad()));
97  return f;
98 }
99 
100 ////////////////////////////////////////////////////////////////////
101 // Function: PhysxFileStream::readDouble
102 // Access: Public
103 // Description:
104 ////////////////////////////////////////////////////////////////////
105 double PhysxFileStream::readDouble() const
106 {
107  NxF64 f;
108  _in->read((char *)&f, sizeof(NxF64));
109  NX_ASSERT(!(_in->bad()));
110  return f;
111 }
112 
113 ////////////////////////////////////////////////////////////////////
114 // Function: PhysxFileStream::readBuffer
115 // Access: Public
116 // Description:
117 ////////////////////////////////////////////////////////////////////
118 void PhysxFileStream::readBuffer(void *buffer, NxU32 size) const
119 {
120  _in->read((char *)buffer, size);
121  NX_ASSERT(!(_in->bad()));
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: PhysxFileStream::storeByte
126 // Access: Public
127 // Description:
128 ////////////////////////////////////////////////////////////////////
129 NxStream &PhysxFileStream::storeByte(NxU8 b)
130 {
131  size_t w = fwrite(&b, sizeof(NxU8), 1, _fp);
132  NX_ASSERT(w);
133  return *this;
134 }
135 
136 ////////////////////////////////////////////////////////////////////
137 // Function: PhysxFileStream::storeWord
138 // Access: Public
139 // Description:
140 ////////////////////////////////////////////////////////////////////
141 NxStream &PhysxFileStream::storeWord(NxU16 w)
142 {
143  size_t ww = fwrite(&w, sizeof(NxU16), 1, _fp);
144  NX_ASSERT(ww);
145  return *this;
146 }
147 
148 ////////////////////////////////////////////////////////////////////
149 // Function: PhysxFileStream::storeDword
150 // Access: Public
151 // Description:
152 ////////////////////////////////////////////////////////////////////
153 NxStream &PhysxFileStream::storeDword(NxU32 d)
154 {
155  size_t w = fwrite(&d, sizeof(NxU32), 1, _fp);
156  NX_ASSERT(w);
157  return *this;
158 }
159 
160 ////////////////////////////////////////////////////////////////////
161 // Function: PhysxFileStream::storeFloat
162 // Access: Public
163 // Description:
164 ////////////////////////////////////////////////////////////////////
165 NxStream &PhysxFileStream::storeFloat(NxReal f)
166 {
167  size_t w = fwrite(&f, sizeof(NxReal), 1, _fp);
168  NX_ASSERT(w);
169  return *this;
170 }
171 
172 ////////////////////////////////////////////////////////////////////
173 // Function: PhysxFileStream::storeDouble
174 // Access: Public
175 // Description:
176 ////////////////////////////////////////////////////////////////////
177 NxStream &PhysxFileStream::storeDouble(NxF64 f)
178 {
179  size_t w = fwrite(&f, sizeof(NxF64), 1, _fp);
180  NX_ASSERT(w);
181  return *this;
182 }
183 
184 ////////////////////////////////////////////////////////////////////
185 // Function: PhysxFileStream::storeBuffer
186 // Access: Public
187 // Description:
188 ////////////////////////////////////////////////////////////////////
189 NxStream &PhysxFileStream::storeBuffer(const void *buffer, NxU32 size)
190 {
191  size_t w = fwrite(buffer, size, 1, _fp);
192  NX_ASSERT(w);
193  return *this;
194 }
195 
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:44
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.