diff --git a/direct/src/dcparser/dcField.cxx b/direct/src/dcparser/dcField.cxx index 6c2c70087f..6c7cc3442b 100644 --- a/direct/src/dcparser/dcField.cxx +++ b/direct/src/dcparser/dcField.cxx @@ -57,6 +57,49 @@ as_molecular_field() { return (DCMolecularField *)NULL; } +//////////////////////////////////////////////////////////////////// +// Function: DCField::format_data +// Access: Published +// Description: Given a blob that represents the packed data for this +// field, returns a string formatting it for human +// consumption. Returns empty string if there is an error. +//////////////////////////////////////////////////////////////////// +string DCField:: +format_data(const string &packed_data) { + DCPacker packer; + packer.begin_unpack(packed_data, this); + string result = packer.unpack_and_format(); + if (!packer.end_unpack()) { + return string(); + } + return result; +} + +//////////////////////////////////////////////////////////////////// +// Function: DCField::parse_string +// Access: Published +// Description: Given a human-formatted string (for instance, as +// returned by format_data(), above) that represents the +// value of this field, parse the string and return the +// corresponding packed data. Returns empty string if +// there is an error. +//////////////////////////////////////////////////////////////////// +string DCField:: +parse_string(const string &formatted_string) { + DCPacker packer; + packer.begin_pack(this); + if (!packer.parse_and_pack(formatted_string)) { + // Parse error. + return string(); + } + if (!packer.end_pack()) { + // Data type mismatch. + return string(); + } + + return packer.get_string(); +} + #ifdef HAVE_PYTHON //////////////////////////////////////////////////////////////////// // Function: DCField::pack_args diff --git a/direct/src/dcparser/dcField.h b/direct/src/dcparser/dcField.h index f275c6cc1c..c451afc84d 100644 --- a/direct/src/dcparser/dcField.h +++ b/direct/src/dcparser/dcField.h @@ -49,6 +49,9 @@ PUBLISHED: virtual DCAtomicField *as_atomic_field(); virtual DCMolecularField *as_molecular_field(); + string format_data(const string &packed_data); + string parse_string(const string &formatted_string); + #ifdef HAVE_PYTHON bool pack_args(Datagram &datagram, PyObject *sequence) const; PyObject *unpack_args(DatagramIterator &iterator) const;