00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "numeric_types.h"
00021 #include "datagramIterator.h"
00022 #include "profileTimer.h"
00023 #include "config_util.h"
00024 #include "config_express.h"
00025 #include "virtualFileSystem.h"
00026
00027 #include "datagramInputFile.h"
00028
00029
00030 #ifdef SKYLER_TIMER //[
00031 EXPCL_PANDAEXPRESS ProfileTimer Skyler_timer_file;
00032 #endif //]
00033
00034
00035
00036
00037
00038
00039
00040 bool DatagramInputFile::
00041 open(Filename filename) {
00042
00043 _read_first_datagram = false;
00044 _error = false;
00045 filename.set_binary();
00046
00047 if (use_vfs) {
00048 VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
00049 PT(VirtualFile) file = vfs->get_file(filename);
00050 if (file == (VirtualFile *)NULL) {
00051
00052 return false;
00053 }
00054 _in = file->open_read_file();
00055 _owns_in = (_in != (istream *)NULL);
00056 return _owns_in && !_in->fail();
00057
00058 } else {
00059 _in = &_in_file;
00060 _owns_in = false;
00061 return filename.open_read(_in_file);
00062 }
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 bool DatagramInputFile::
00075 read_header(string &header, size_t num_bytes) {
00076 nassertr(!_read_first_datagram, false);
00077
00078 char *buffer = (char *)alloca(num_bytes);
00079 nassertr(buffer != (char *)NULL, false);
00080
00081 _in->read(buffer, num_bytes);
00082 if (_in->fail() || _in->eof()) {
00083 return false;
00084 }
00085
00086 header = string(buffer, num_bytes);
00087 return true;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096
00097 bool DatagramInputFile::
00098 get_datagram(Datagram &data) {
00099 #ifdef SKYLER_TIMER //[
00100 Skyler_timer_file.on();
00101 #endif //]
00102 _read_first_datagram = true;
00103
00104
00105
00106 char sizebuf[sizeof(PN_uint32)];
00107 _in->read(sizebuf, sizeof(PN_uint32));
00108 if (_in->fail() || _in->eof()) {
00109 #ifdef SKYLER_TIMER //[
00110 Skyler_timer_file.off("DatagramInputFile::get_datagram");
00111 #endif //]
00112 return false;
00113 }
00114
00115 Datagram size(sizebuf, sizeof(PN_uint32));
00116 DatagramIterator di(size);
00117 PN_uint32 num_bytes = di.get_uint32();
00118
00119
00120 char *buffer = new char[num_bytes];
00121 nassertr(buffer != (char *)NULL, false);
00122
00123 _in->read(buffer, num_bytes);
00124 if (_in->fail() || _in->eof()) {
00125 _error = true;
00126 delete[] buffer;
00127 #ifdef SKYLER_TIMER //[
00128 Skyler_timer_file.off("DatagramInputFile::get_datagram");
00129 #endif //]
00130 return false;
00131 }
00132
00133 data = Datagram(buffer, num_bytes);
00134 delete[] buffer;
00135 #ifdef SKYLER_TIMER //[
00136 Skyler_timer_file.off("DatagramInputFile::get_datagram");
00137 #endif //]
00138 return true;
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148 bool DatagramInputFile::
00149 is_eof() {
00150 return _in->eof();
00151 }
00152
00153
00154
00155
00156
00157
00158
00159 bool DatagramInputFile::
00160 is_error() {
00161 if (_in->fail()) {
00162 _error = true;
00163 }
00164 return _error;
00165 }