Panda3D
|
00001 // Filename: fltBeadID.cxx 00002 // Created by: drose (24Aug00) 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 "fltBeadID.h" 00016 #include "fltRecordReader.h" 00017 #include "fltRecordWriter.h" 00018 00019 TypeHandle FltBeadID::_type_handle; 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: FltBeadID::Constructor 00023 // Access: Public 00024 // Description: 00025 //////////////////////////////////////////////////////////////////// 00026 FltBeadID:: 00027 FltBeadID(FltHeader *header) : FltBead(header) { 00028 } 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function: FltBeadID::get_id 00032 // Access: Public 00033 // Description: Returns the id (name) of this particular bead. Each 00034 // MultiGen bead will have a unique name. 00035 //////////////////////////////////////////////////////////////////// 00036 const string &FltBeadID:: 00037 get_id() const { 00038 return _id; 00039 } 00040 00041 //////////////////////////////////////////////////////////////////// 00042 // Function: FltBeadID::set_id 00043 // Access: Public 00044 // Description: Changes the id (name) of this particular bead. This 00045 // should be a name that is unique to this bead. 00046 //////////////////////////////////////////////////////////////////// 00047 void FltBeadID:: 00048 set_id(const string &id) { 00049 _id = id; 00050 } 00051 00052 //////////////////////////////////////////////////////////////////// 00053 // Function: FltBeadID::output 00054 // Access: Public 00055 // Description: Writes a quick one-line description of the record, but 00056 // not its children. This is a human-readable 00057 // description, primarily for debugging; to write a flt 00058 // file, use FltHeader::write_flt(). 00059 //////////////////////////////////////////////////////////////////// 00060 void FltBeadID:: 00061 output(ostream &out) const { 00062 out << get_type(); 00063 if (!_id.empty()) { 00064 out << " " << _id; 00065 } 00066 } 00067 00068 //////////////////////////////////////////////////////////////////// 00069 // Function: FltBeadID::extract_record 00070 // Access: Protected, Virtual 00071 // Description: Fills in the information in this bead based on the 00072 // information given in the indicated datagram, whose 00073 // opcode has already been read. Returns true on 00074 // success, false if the datagram is invalid. 00075 //////////////////////////////////////////////////////////////////// 00076 bool FltBeadID:: 00077 extract_record(FltRecordReader &reader) { 00078 if (!FltBead::extract_record(reader)) { 00079 return false; 00080 } 00081 00082 _id = reader.get_iterator().get_fixed_string(8); 00083 return true; 00084 } 00085 00086 //////////////////////////////////////////////////////////////////// 00087 // Function: FltBeadID::extract_ancillary 00088 // Access: Protected, Virtual 00089 // Description: Checks whether the given bead, which follows this 00090 // bead sequentially in the file, is an ancillary record 00091 // of this bead. If it is, extracts the relevant 00092 // information and returns true; otherwise, leaves it 00093 // alone and returns false. 00094 //////////////////////////////////////////////////////////////////// 00095 bool FltBeadID:: 00096 extract_ancillary(FltRecordReader &reader) { 00097 if (reader.get_opcode() == FO_long_id) { 00098 string s = reader.get_iterator().get_remaining_bytes(); 00099 size_t zero_byte = s.find('\0'); 00100 _id = s.substr(0, zero_byte); 00101 return true; 00102 } 00103 00104 return FltBead::extract_ancillary(reader); 00105 } 00106 00107 //////////////////////////////////////////////////////////////////// 00108 // Function: FltBeadID::build_record 00109 // Access: Protected, Virtual 00110 // Description: Fills up the current record on the FltRecordWriter with 00111 // data for this record, but does not advance the 00112 // writer. Returns true on success, false if there is 00113 // some error. 00114 //////////////////////////////////////////////////////////////////// 00115 bool FltBeadID:: 00116 build_record(FltRecordWriter &writer) const { 00117 if (!FltBead::build_record(writer)) { 00118 return false; 00119 } 00120 00121 writer.update_datagram().add_fixed_string(_id.substr(0, 7), 8); 00122 return true; 00123 } 00124 00125 //////////////////////////////////////////////////////////////////// 00126 // Function: FltBeadID::write_ancillary 00127 // Access: Protected, Virtual 00128 // Description: Writes whatever ancillary records are required for 00129 // this record. Returns FE_ok on success, or something 00130 // else if there is some error. 00131 //////////////////////////////////////////////////////////////////// 00132 FltError FltBeadID:: 00133 write_ancillary(FltRecordWriter &writer) const { 00134 if (_id.length() > 7) { 00135 00136 // Although the manual mentions nothing of this, it is essential 00137 // that the length of the record be a multiple of 4 bytes. 00138 string id = _id; 00139 while ((id.length() % 4) != 0) { 00140 id += '\0'; 00141 } 00142 Datagram dc(id); 00143 00144 FltError result = writer.write_record(FO_long_id, dc); 00145 if (result != FE_ok) { 00146 return result; 00147 } 00148 } 00149 00150 return FltBead::write_ancillary(writer); 00151 }