Panda3D
fltExternalReference.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 fltExternalReference.cxx
10  * @author drose
11  * @date 2000-08-30
12  */
13 
14 #include "fltExternalReference.h"
15 #include "fltRecordReader.h"
16 #include "fltRecordWriter.h"
17 #include "fltHeader.h"
18 #include "pathReplace.h"
19 
20 TypeHandle FltExternalReference::_type_handle;
21 
22 /**
23  *
24  */
25 FltExternalReference::
26 FltExternalReference(FltHeader *header) : FltBead(header) {
27  _flags = 0;
28 }
29 
30 /**
31  * Walks the hierarchy at this record and below and copies the
32  * _converted_filename record into the _orig_filename record, so the flt file
33  * will be written out with the converted filename instead of what was
34  * originally read in.
35  */
38  _orig_filename = _converted_filename.to_os_generic();
40 }
41 
42 /**
43  * Writes a quick one-line description of the record, but not its children.
44  * This is a human-readable description, primarily for debugging; to write a
45  * flt file, use FltHeader::write_flt().
46  */
48 output(std::ostream &out) const {
49  out << "External " << get_ref_filename();
50  if (!_bead_id.empty()) {
51  out << " (" << _bead_id << ")";
52  }
53 }
54 
55 /**
56  * Returns the name of the referenced file.
57  */
59 get_ref_filename() const {
60  return _converted_filename;
61 }
62 
63 /**
64  * Changes the name of the referenced file.
65  */
67 set_ref_filename(const Filename &filename) {
68  _converted_filename = filename;
69  _orig_filename = _converted_filename.to_os_generic();
70 }
71 
72 /**
73  * Fills in the information in this bead based on the information given in the
74  * indicated datagram, whose opcode has already been read. Returns true on
75  * success, false if the datagram is invalid.
76  */
77 bool FltExternalReference::
78 extract_record(FltRecordReader &reader) {
79  if (!FltBead::extract_record(reader)) {
80  return false;
81  }
82 
83  nassertr(reader.get_opcode() == FO_external_ref, false);
84  DatagramIterator &iterator = reader.get_iterator();
85 
86  std::string name = iterator.get_fixed_string(200);
87  iterator.skip_bytes(1 + 1);
88  iterator.skip_bytes(2); // Undocumented additional padding.
89  _flags = iterator.get_be_uint32();
90  iterator.skip_bytes(2);
91  iterator.skip_bytes(2); // Undocumented additional padding.
92 
93  _orig_filename = name;
94 
95  if (!name.empty() && name[name.length() - 1] == '>') {
96  // Extract out the bead name.
97  size_t open = name.rfind('<');
98  if (open != std::string::npos) {
99  _orig_filename = name.substr(0, open);
100  _bead_id = name.substr(open + 1, name.length() - open - 2);
101  }
102  }
103  _converted_filename = _header->convert_path(Filename::from_os_specific(_orig_filename));
104 
105  check_remaining_size(iterator);
106  return true;
107 }
108 
109 /**
110  * Fills up the current record on the FltRecordWriter with data for this
111  * record, but does not advance the writer. Returns true on success, false if
112  * there is some error.
113  */
114 bool FltExternalReference::
115 build_record(FltRecordWriter &writer) const {
116  if (!FltBead::build_record(writer)) {
117  return false;
118  }
119 
120  writer.set_opcode(FO_external_ref);
121  Datagram &datagram = writer.update_datagram();
122 
123  std::string name = _orig_filename;
124  if (!_bead_id.empty()) {
125  name += "<" + _bead_id + ">";
126  }
127 
128  datagram.add_fixed_string(name.substr(0, 199), 200);
129  datagram.pad_bytes(1 + 1);
130  datagram.pad_bytes(2); // Undocumented additional padding.
131  datagram.add_be_uint32(_flags);
132  datagram.pad_bytes(2);
133  datagram.pad_bytes(2); // Undocumented additional padding.
134 
135  return true;
136 }
A class to retrieve the individual data elements previously stored in a Datagram.
void skip_bytes(size_t size)
Skips over the indicated number of bytes in the datagram.
uint32_t get_be_uint32()
Extracts an unsigned 32-bit big-endian integer.
std::string get_fixed_string(size_t size)
Extracts a fixed-length string.
An ordered list of data elements, formatted in memory for transmission over a socket or writing to a ...
Definition: datagram.h:38
void add_be_uint32(uint32_t value)
Adds an unsigned 32-bit big-endian integer to the datagram.
Definition: datagram.I:181
void add_fixed_string(const std::string &str, size_t size)
Adds a fixed-length string to the datagram.
Definition: datagram.I:263
void pad_bytes(size_t size)
Adds the indicated number of zero bytes to the datagram.
Definition: datagram.cxx:99
The name of a file, such as a texture file or an Egg file.
Definition: filename.h:39
static Filename from_os_specific(const std::string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes,...
Definition: filename.cxx:328
std::string to_os_generic() const
This is similar to to_os_specific(), but it is designed to generate a filename that can be understood...
Definition: filename.cxx:1182
A base class for any of a broad family of flt records that represent particular beads in the hierarch...
Definition: fltBead.h:29
virtual void output(std::ostream &out) const
Writes a quick one-line description of the record, but not its children.
virtual void apply_converted_filenames()
Walks the hierarchy at this record and below and copies the _converted_filename record into the _orig...
Filename get_ref_filename() const
Returns the name of the referenced file.
void set_ref_filename(const Filename &filename)
Changes the name of the referenced file.
This is the first bead in the file, the top of the bead hierarchy, and the primary interface to readi...
Definition: fltHeader.h:44
This class turns an istream into a sequence of FltRecords by reading a sequence of Datagrams and extr...
FltOpcode get_opcode() const
Returns the opcode associated with the current record.
DatagramIterator & get_iterator()
Returns an iterator suitable for extracting data from the current record.
This class writes a sequence of FltRecords to an ostream, handling opcode and size counts properly.
void set_opcode(FltOpcode opcode)
Sets the opcode associated with the current record.
Datagram & update_datagram()
Returns a modifiable reference to the datagram associated with the current record.
void check_remaining_size(const DatagramIterator &di, const std::string &name=std::string()) const
Checks that the iterator has no bytes left, as it should at the end of a successfully read record.
Definition: fltRecord.cxx:254
virtual void apply_converted_filenames()
Walks the hierarchy at this record and below and copies the _converted_filename record into the _orig...
Definition: fltRecord.cxx:278
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.