Panda3D
|
00001 // Filename: physxFileStream.cxx 00002 // Created by: enn0x (11Oct09) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "physxFileStream.h" 00016 00017 #include "stdio.h" 00018 00019 #include "virtualFileSystem.h" 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: PhysxFileStream::Constructor 00023 // Access: Public 00024 // Description: 00025 //////////////////////////////////////////////////////////////////// 00026 PhysxFileStream::PhysxFileStream(const Filename &fn, bool load) : _fp(NULL), _vf(NULL), _in(NULL) 00027 { 00028 if (load) { 00029 _vf = VirtualFileSystem::get_global_ptr()->get_file(fn); 00030 _in = _vf->open_read_file(true); 00031 } 00032 else { 00033 _fp = fopen(fn.c_str(), "wb"); 00034 } 00035 } 00036 00037 //////////////////////////////////////////////////////////////////// 00038 // Function: PhysxFileStream::Destructor 00039 // Access: Public 00040 // Description: 00041 //////////////////////////////////////////////////////////////////// 00042 PhysxFileStream::~PhysxFileStream() 00043 { 00044 if (_fp) fclose(_fp); 00045 if (_vf) _vf->close_read_file(_in); 00046 } 00047 00048 //////////////////////////////////////////////////////////////////// 00049 // Function: PhysxFileStream::readByte 00050 // Access: Public 00051 // Description: 00052 //////////////////////////////////////////////////////////////////// 00053 NxU8 PhysxFileStream::readByte() const 00054 { 00055 NxU8 b; 00056 _in->read((char *)&b, sizeof(NxU8)); 00057 NX_ASSERT(!(_in->bad())); 00058 return b; 00059 } 00060 00061 //////////////////////////////////////////////////////////////////// 00062 // Function: PhysxFileStream::readWord 00063 // Access: Public 00064 // Description: 00065 //////////////////////////////////////////////////////////////////// 00066 NxU16 PhysxFileStream::readWord() const 00067 { 00068 NxU16 w; 00069 _in->read((char *)&w, sizeof(NxU16)); 00070 NX_ASSERT(!(_in->bad())); 00071 return w; 00072 } 00073 00074 //////////////////////////////////////////////////////////////////// 00075 // Function: PhysxFileStream::readDword 00076 // Access: Public 00077 // Description: 00078 //////////////////////////////////////////////////////////////////// 00079 NxU32 PhysxFileStream::readDword() const 00080 { 00081 NxU32 d; 00082 _in->read((char *)&d, sizeof(NxU32)); 00083 NX_ASSERT(!(_in->bad())); 00084 return d; 00085 } 00086 00087 //////////////////////////////////////////////////////////////////// 00088 // Function: PhysxFileStream::readFloat 00089 // Access: Public 00090 // Description: 00091 //////////////////////////////////////////////////////////////////// 00092 float PhysxFileStream::readFloat() const 00093 { 00094 NxReal f; 00095 _in->read((char *)&f, sizeof(NxReal)); 00096 NX_ASSERT(!(_in->bad())); 00097 return f; 00098 } 00099 00100 //////////////////////////////////////////////////////////////////// 00101 // Function: PhysxFileStream::readDouble 00102 // Access: Public 00103 // Description: 00104 //////////////////////////////////////////////////////////////////// 00105 double PhysxFileStream::readDouble() const 00106 { 00107 NxF64 f; 00108 _in->read((char *)&f, sizeof(NxF64)); 00109 NX_ASSERT(!(_in->bad())); 00110 return f; 00111 } 00112 00113 //////////////////////////////////////////////////////////////////// 00114 // Function: PhysxFileStream::readBuffer 00115 // Access: Public 00116 // Description: 00117 //////////////////////////////////////////////////////////////////// 00118 void PhysxFileStream::readBuffer(void *buffer, NxU32 size) const 00119 { 00120 _in->read((char *)buffer, size); 00121 NX_ASSERT(!(_in->bad())); 00122 } 00123 00124 //////////////////////////////////////////////////////////////////// 00125 // Function: PhysxFileStream::storeByte 00126 // Access: Public 00127 // Description: 00128 //////////////////////////////////////////////////////////////////// 00129 NxStream &PhysxFileStream::storeByte(NxU8 b) 00130 { 00131 size_t w = fwrite(&b, sizeof(NxU8), 1, _fp); 00132 NX_ASSERT(w); 00133 return *this; 00134 } 00135 00136 //////////////////////////////////////////////////////////////////// 00137 // Function: PhysxFileStream::storeWord 00138 // Access: Public 00139 // Description: 00140 //////////////////////////////////////////////////////////////////// 00141 NxStream &PhysxFileStream::storeWord(NxU16 w) 00142 { 00143 size_t ww = fwrite(&w, sizeof(NxU16), 1, _fp); 00144 NX_ASSERT(ww); 00145 return *this; 00146 } 00147 00148 //////////////////////////////////////////////////////////////////// 00149 // Function: PhysxFileStream::storeDword 00150 // Access: Public 00151 // Description: 00152 //////////////////////////////////////////////////////////////////// 00153 NxStream &PhysxFileStream::storeDword(NxU32 d) 00154 { 00155 size_t w = fwrite(&d, sizeof(NxU32), 1, _fp); 00156 NX_ASSERT(w); 00157 return *this; 00158 } 00159 00160 //////////////////////////////////////////////////////////////////// 00161 // Function: PhysxFileStream::storeFloat 00162 // Access: Public 00163 // Description: 00164 //////////////////////////////////////////////////////////////////// 00165 NxStream &PhysxFileStream::storeFloat(NxReal f) 00166 { 00167 size_t w = fwrite(&f, sizeof(NxReal), 1, _fp); 00168 NX_ASSERT(w); 00169 return *this; 00170 } 00171 00172 //////////////////////////////////////////////////////////////////// 00173 // Function: PhysxFileStream::storeDouble 00174 // Access: Public 00175 // Description: 00176 //////////////////////////////////////////////////////////////////// 00177 NxStream &PhysxFileStream::storeDouble(NxF64 f) 00178 { 00179 size_t w = fwrite(&f, sizeof(NxF64), 1, _fp); 00180 NX_ASSERT(w); 00181 return *this; 00182 } 00183 00184 //////////////////////////////////////////////////////////////////// 00185 // Function: PhysxFileStream::storeBuffer 00186 // Access: Public 00187 // Description: 00188 //////////////////////////////////////////////////////////////////// 00189 NxStream &PhysxFileStream::storeBuffer(const void *buffer, NxU32 size) 00190 { 00191 size_t w = fwrite(buffer, size, 1, _fp); 00192 NX_ASSERT(w); 00193 return *this; 00194 } 00195