Panda3D
fltBeadID.cxx
1 // Filename: fltBeadID.cxx
2 // Created by: drose (24Aug00)
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 "fltBeadID.h"
16 #include "fltRecordReader.h"
17 #include "fltRecordWriter.h"
18 
19 TypeHandle FltBeadID::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: FltBeadID::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 FltBeadID::
27 FltBeadID(FltHeader *header) : FltBead(header) {
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: FltBeadID::get_id
32 // Access: Public
33 // Description: Returns the id (name) of this particular bead. Each
34 // MultiGen bead will have a unique name.
35 ////////////////////////////////////////////////////////////////////
36 const string &FltBeadID::
37 get_id() const {
38  return _id;
39 }
40 
41 ////////////////////////////////////////////////////////////////////
42 // Function: FltBeadID::set_id
43 // Access: Public
44 // Description: Changes the id (name) of this particular bead. This
45 // should be a name that is unique to this bead.
46 ////////////////////////////////////////////////////////////////////
47 void FltBeadID::
48 set_id(const string &id) {
49  _id = id;
50 }
51 
52 ////////////////////////////////////////////////////////////////////
53 // Function: FltBeadID::output
54 // Access: Public
55 // Description: Writes a quick one-line description of the record, but
56 // not its children. This is a human-readable
57 // description, primarily for debugging; to write a flt
58 // file, use FltHeader::write_flt().
59 ////////////////////////////////////////////////////////////////////
60 void FltBeadID::
61 output(ostream &out) const {
62  out << get_type();
63  if (!_id.empty()) {
64  out << " " << _id;
65  }
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: FltBeadID::extract_record
70 // Access: Protected, Virtual
71 // Description: Fills in the information in this bead based on the
72 // information given in the indicated datagram, whose
73 // opcode has already been read. Returns true on
74 // success, false if the datagram is invalid.
75 ////////////////////////////////////////////////////////////////////
76 bool FltBeadID::
77 extract_record(FltRecordReader &reader) {
78  if (!FltBead::extract_record(reader)) {
79  return false;
80  }
81 
82  _id = reader.get_iterator().get_fixed_string(8);
83  return true;
84 }
85 
86 ////////////////////////////////////////////////////////////////////
87 // Function: FltBeadID::extract_ancillary
88 // Access: Protected, Virtual
89 // Description: Checks whether the given bead, which follows this
90 // bead sequentially in the file, is an ancillary record
91 // of this bead. If it is, extracts the relevant
92 // information and returns true; otherwise, leaves it
93 // alone and returns false.
94 ////////////////////////////////////////////////////////////////////
95 bool FltBeadID::
96 extract_ancillary(FltRecordReader &reader) {
97  if (reader.get_opcode() == FO_long_id) {
98  string s = reader.get_iterator().get_remaining_bytes();
99  size_t zero_byte = s.find('\0');
100  _id = s.substr(0, zero_byte);
101  return true;
102  }
103 
104  return FltBead::extract_ancillary(reader);
105 }
106 
107 ////////////////////////////////////////////////////////////////////
108 // Function: FltBeadID::build_record
109 // Access: Protected, Virtual
110 // Description: Fills up the current record on the FltRecordWriter with
111 // data for this record, but does not advance the
112 // writer. Returns true on success, false if there is
113 // some error.
114 ////////////////////////////////////////////////////////////////////
115 bool FltBeadID::
116 build_record(FltRecordWriter &writer) const {
117  if (!FltBead::build_record(writer)) {
118  return false;
119  }
120 
121  writer.update_datagram().add_fixed_string(_id.substr(0, 7), 8);
122  return true;
123 }
124 
125 ////////////////////////////////////////////////////////////////////
126 // Function: FltBeadID::write_ancillary
127 // Access: Protected, Virtual
128 // Description: Writes whatever ancillary records are required for
129 // this record. Returns FE_ok on success, or something
130 // else if there is some error.
131 ////////////////////////////////////////////////////////////////////
132 FltError FltBeadID::
133 write_ancillary(FltRecordWriter &writer) const {
134  if (_id.length() > 7) {
135 
136  // Although the manual mentions nothing of this, it is essential
137  // that the length of the record be a multiple of 4 bytes.
138  string id = _id;
139  while ((id.length() % 4) != 0) {
140  id += '\0';
141  }
142  Datagram dc(id);
143 
144  FltError result = writer.write_record(FO_long_id, dc);
145  if (result != FE_ok) {
146  return result;
147  }
148  }
149 
150  return FltBead::write_ancillary(writer);
151 }
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly...
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
A base class for any of a broad family of flt records that represent particular beads in the hierarch...
Definition: fltBead.h:33
const string & get_id() const
Returns the id (name) of this particular bead.
Definition: fltBeadID.cxx:37
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
void set_id(const string &id)
Changes the id (name) of this particular bead.
Definition: fltBeadID.cxx:48
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:48
virtual void output(ostream &out) const
Writes a quick one-line description of the record, but not its children.
Definition: fltBeadID.cxx:61
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
void add_fixed_string(const string &str, size_t size)
Adds a fixed-length string to the datagram.
Definition: datagram.I:404
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
string get_remaining_bytes() const
Returns the remaining bytes in the datagram as a string, but does not extract them from the iterator...
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:43
FltError write_record(FltOpcode opcode, const Datagram &datagram=Datagram())
A convenience function to quickly write a simple record that consists of an opcode and possibly a dat...
string get_fixed_string(size_t size)
Extracts a fixed-length string.