parse_string, format_data

This commit is contained in:
David Rose 2004-06-17 19:47:03 +00:00
parent ec4539137a
commit 0b2be8bf9c
2 changed files with 46 additions and 0 deletions

View File

@ -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

View File

@ -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;