Panda3D
fltBeadID.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file fltBeadID.cxx
10  * @author drose
11  * @date 2000-08-24
12  */
13 
14 #include "fltBeadID.h"
15 #include "fltRecordReader.h"
16 #include "fltRecordWriter.h"
17 
18 TypeHandle FltBeadID::_type_handle;
19 
20 /**
21  *
22  */
23 FltBeadID::
24 FltBeadID(FltHeader *header) : FltBead(header) {
25 }
26 
27 /**
28  * Returns the id (name) of this particular bead. Each MultiGen bead will
29  * have a unique name.
30  */
31 const std::string &FltBeadID::
32 get_id() const {
33  return _id;
34 }
35 
36 /**
37  * Changes the id (name) of this particular bead. This should be a name that
38  * is unique to this bead.
39  */
40 void FltBeadID::
41 set_id(const std::string &id) {
42  _id = id;
43 }
44 
45 /**
46  * Writes a quick one-line description of the record, but not its children.
47  * This is a human-readable description, primarily for debugging; to write a
48  * flt file, use FltHeader::write_flt().
49  */
50 void FltBeadID::
51 output(std::ostream &out) const {
52  out << get_type();
53  if (!_id.empty()) {
54  out << " " << _id;
55  }
56 }
57 
58 /**
59  * Fills in the information in this bead based on the information given in the
60  * indicated datagram, whose opcode has already been read. Returns true on
61  * success, false if the datagram is invalid.
62  */
63 bool FltBeadID::
64 extract_record(FltRecordReader &reader) {
65  if (!FltBead::extract_record(reader)) {
66  return false;
67  }
68 
69  _id = reader.get_iterator().get_fixed_string(8);
70  return true;
71 }
72 
73 /**
74  * Checks whether the given bead, which follows this bead sequentially in the
75  * file, is an ancillary record of this bead. If it is, extracts the relevant
76  * information and returns true; otherwise, leaves it alone and returns false.
77  */
78 bool FltBeadID::
79 extract_ancillary(FltRecordReader &reader) {
80  if (reader.get_opcode() == FO_long_id) {
81  DatagramIterator &di = reader.get_iterator();
82  _id = di.get_fixed_string(di.get_remaining_size());
83  return true;
84  }
85 
86  return FltBead::extract_ancillary(reader);
87 }
88 
89 /**
90  * Fills up the current record on the FltRecordWriter with data for this
91  * record, but does not advance the writer. Returns true on success, false if
92  * there is some error.
93  */
94 bool FltBeadID::
95 build_record(FltRecordWriter &writer) const {
96  if (!FltBead::build_record(writer)) {
97  return false;
98  }
99 
100  writer.update_datagram().add_fixed_string(_id.substr(0, 7), 8);
101  return true;
102 }
103 
104 /**
105  * Writes whatever ancillary records are required for this record. Returns
106  * FE_ok on success, or something else if there is some error.
107  */
108 FltError FltBeadID::
109 write_ancillary(FltRecordWriter &writer) const {
110  if (_id.length() > 7) {
111  Datagram dc;
112 
113  // Although the manual mentions nothing of this, it is essential that the
114  // length of the record be a multiple of 4 bytes.
115  dc.add_fixed_string(_id, (_id.length() + 3) & ~3);
116 
117  FltError result = writer.write_record(FO_long_id, dc);
118  if (result != FE_ok) {
119  return result;
120  }
121  }
122 
123  return FltBead::write_ancillary(writer);
124 }
void set_id(const std::string &id)
Changes the id (name) of this particular bead.
Definition: fltBeadID.cxx:41
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...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
A base class for any of a broad family of flt records that represent particular beads in the hierarch...
Definition: fltBead.h:29
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
size_t get_remaining_size() const
Return the bytes left in the datagram.
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
virtual void output(std::ostream &out) const
Writes a quick one-line description of the record, but not its children.
Definition: fltBeadID.cxx:51
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
A class to retrieve the individual data elements previously stored in a Datagram.
void add_fixed_string(const std::string &str, size_t size)
Adds a fixed-length string to the datagram.
Definition: datagram.I:263
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
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:38
const std::string & get_id() const
Returns the id (name) of this particular bead.
Definition: fltBeadID.cxx:32
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...
std::string get_fixed_string(size_t size)
Extracts a fixed-length string.