From c11ab984ae356d47bd1ad8668f4b28de1efc790f Mon Sep 17 00:00:00 2001 From: David Rose Date: Sat, 14 Jul 2001 01:55:50 +0000 Subject: [PATCH] add (u)int8array --- direct/src/dcparse/dcparse.cxx | 2 +- direct/src/dcparser/dcAtomicField.cxx | 127 +++++++++++++++++------- direct/src/dcparser/dcAtomicField.h | 4 +- direct/src/dcparser/dcLexer.lxx | 10 ++ direct/src/dcparser/dcParser.yxx | 35 ++++++- direct/src/dcparser/dcSubatomicType.cxx | 6 ++ direct/src/dcparser/dcSubatomicType.h | 6 ++ 7 files changed, 153 insertions(+), 37 deletions(-) diff --git a/direct/src/dcparse/dcparse.cxx b/direct/src/dcparse/dcparse.cxx index efdef12823..63d7db0bae 100644 --- a/direct/src/dcparse/dcparse.cxx +++ b/direct/src/dcparse/dcparse.cxx @@ -40,7 +40,7 @@ main(int argc, char *argv[]) { } long hash = file.get_hash(); - cout << "File hash is " << hash << "\n"; + cerr << "File hash is " << hash << "\n"; return (0); } diff --git a/direct/src/dcparser/dcAtomicField.cxx b/direct/src/dcparser/dcAtomicField.cxx index 1df7fc9f8b..e5da73031d 100644 --- a/direct/src/dcparser/dcAtomicField.cxx +++ b/direct/src/dcparser/dcAtomicField.cxx @@ -66,15 +66,18 @@ ElementType() { bool DCAtomicField::ElementType:: set_default_value(double num) { switch (_type) { - case ST_int16array: - case ST_uint16array: - case ST_int32array: - case ST_uint32array: - case ST_blob: - // These array types don't take numbers. - return false; - default: + // Only fields of these types accept numbers. + case ST_int8: + case ST_int16: + case ST_int32: + case ST_uint8: + case ST_uint16: + case ST_uint32: + case ST_float64: break; + + default: + return false; } string formatted; @@ -98,23 +101,12 @@ set_default_value(double num) { //////////////////////////////////////////////////////////////////// bool DCAtomicField::ElementType:: set_default_value(const string &str) { - switch (_type) { - case ST_int16array: - case ST_uint16array: - case ST_int32array: - case ST_uint32array: - // These array types don't take strings. - return false; - default: - break; - } - - string formatted; - if (!format_default_value(str, formatted)) { + if (_type != ST_string && _type != ST_blob) { + // Only fields of type string or blob accept quoted strings. return false; } - _default_value = formatted; + _default_value = str; _has_default_value = true; return true; } @@ -125,10 +117,11 @@ set_default_value(const string &str) { // Description: Explicitly sets the default value to the given // pre-formatted string. //////////////////////////////////////////////////////////////////// -void DCAtomicField::ElementType:: +bool DCAtomicField::ElementType:: set_default_value_literal(const string &str) { _default_value = str; _has_default_value = true; + return true; } //////////////////////////////////////////////////////////////////// @@ -151,6 +144,46 @@ add_default_value(double num) { return true; } +//////////////////////////////////////////////////////////////////// +// Function: DCAtomicField::ElementType::add_default_value +// Access: Public +// Description: Appends the indicated value as the next array element +// value for the default value for this type. +// +// Returns true if the element type reasonably accepts a +// default value of numeric type, false otherwise. +//////////////////////////////////////////////////////////////////// +bool DCAtomicField::ElementType:: +add_default_value(const string &str) { + string formatted; + if (!format_default_value(str, formatted)) { + return false; + } + + _default_value += formatted; + return true; +} + +//////////////////////////////////////////////////////////////////// +// Function: DCAtomicField::ElementType::add_default_value_literal +// Access: Public +// Description: Appends the indicated value as the next array element +// value for the default value for this type. +// +// Returns true if the element type reasonably accepts a +// default value of numeric type, false otherwise. +//////////////////////////////////////////////////////////////////// +bool DCAtomicField::ElementType:: +add_default_value_literal(const string &str) { + if (_type != ST_blob) { + // Only blobs can have literal hex strings nested within arrays. + return false; + } + + _default_value += str; + return true; +} + //////////////////////////////////////////////////////////////////// // Function: DCAtomicField::ElementType::end_array // Access: Public @@ -161,20 +194,14 @@ add_default_value(double num) { bool DCAtomicField::ElementType:: end_array() { switch (_type) { + case ST_int8array: case ST_int16array: - case ST_uint16array: case ST_int32array: + case ST_uint8array: + case ST_uint16array: case ST_uint32array: case ST_blob: - { - // We've accumulated all the elements of the array; now we must - // prepend the array length. - int length = _default_value.length(); - _default_value = - string(1, (char)(length & 0xff)) + - string(1, (char)((length >> 8) & 0xff)) + - _default_value; - } + // These types accept arrays. return true; default: @@ -198,6 +225,8 @@ format_default_value(double num, string &formatted) const { switch (_type) { case ST_int8: case ST_uint8: + case ST_int8array: + case ST_uint8array: case ST_blob: formatted = string(1, (char)(int_value & 0xff)); break; @@ -375,12 +404,42 @@ get_element_divisor(int n) const { // valid if has_element_default() returns true, in which // case this string represents the bytes that should be // assigned to the field as a default value. +// +// If the element is an array-type element, the returned +// value will include the two-byte length preceding the +// array data. //////////////////////////////////////////////////////////////////// string DCAtomicField:: get_element_default(int n) const { nassertr(has_element_default(n), string()); nassertr(n >= 0 && n < (int)_elements.size(), string()); - return _elements[n]._default_value; + + string default_value = _elements[n]._default_value; + + switch (_elements[n]._type) { + case ST_int8array: + case ST_int16array: + case ST_int32array: + case ST_uint8array: + case ST_uint16array: + case ST_uint32array: + case ST_blob: + case ST_string: + // These array types also want an implicit length. + { + int length = default_value.length(); + default_value = + string(1, (char)(length & 0xff)) + + string(1, (char)((length >> 8) & 0xff)) + + default_value; + } + break; + + default: + break; + } + + return default_value; } //////////////////////////////////////////////////////////////////// diff --git a/direct/src/dcparser/dcAtomicField.h b/direct/src/dcparser/dcAtomicField.h index b7dcd64c7f..5a797526a4 100644 --- a/direct/src/dcparser/dcAtomicField.h +++ b/direct/src/dcparser/dcAtomicField.h @@ -63,9 +63,11 @@ public: ElementType(); bool set_default_value(double num); bool set_default_value(const string &str); - void set_default_value_literal(const string &str); + bool set_default_value_literal(const string &str); bool add_default_value(double num); + bool add_default_value(const string &str); + bool add_default_value_literal(const string &str); bool end_array(); DCSubatomicType _type; diff --git a/direct/src/dcparser/dcLexer.lxx b/direct/src/dcparser/dcLexer.lxx index b4780233a1..598cd092a6 100644 --- a/direct/src/dcparser/dcLexer.lxx +++ b/direct/src/dcparser/dcLexer.lxx @@ -388,6 +388,11 @@ REALNUM ([+-]?(([0-9]+[.])|([0-9]*[.][0-9]+))([eE][+-]?[0-9]+)?) return KW_BLOB; } +"int8array" { + accept(); + return KW_INT8ARRAY; +} + "int16array" { accept(); return KW_INT16ARRAY; @@ -398,6 +403,11 @@ REALNUM ([+-]?(([0-9]+[.])|([0-9]*[.][0-9]+))([eE][+-]?[0-9]+)?) return KW_INT32ARRAY; } +"uint8array" { + accept(); + return KW_UINT8ARRAY; +} + "uint16array" { accept(); return KW_UINT16ARRAY; diff --git a/direct/src/dcparser/dcParser.yxx b/direct/src/dcparser/dcParser.yxx index 266fd7dbd3..7acf76b6e7 100644 --- a/direct/src/dcparser/dcParser.yxx +++ b/direct/src/dcparser/dcParser.yxx @@ -59,8 +59,10 @@ dc_cleanup_parser() { %token KW_FLOAT64 %token KW_STRING %token KW_BLOB +%token KW_INT8ARRAY %token KW_INT16ARRAY %token KW_INT32ARRAY +%token KW_UINT8ARRAY %token KW_UINT16ARRAY %token KW_UINT32ARRAY @@ -218,7 +220,9 @@ atomic_element_definition: } | atomic_element_definition '=' HEX_STRING { - atomic_element.set_default_value($3); + if (!atomic_element.set_default_value_literal($3)) { + yyerror("Invalid default hex string value"); + } } | atomic_element_definition '=' '{' default_array '}' { @@ -257,6 +261,18 @@ default_array_element: if (!atomic_element.add_default_value($1)) { yyerror("Invalid default value: " + $1); } +} + | STRING +{ + if (!atomic_element.add_default_value($1)) { + yyerror("Invalid default value: " + $1); + } +} + | HEX_STRING +{ + if (!atomic_element.add_default_value_literal($1)) { + yyerror("Invalid hex literal in default array"); + } } | INTEGER '*' INTEGER { @@ -275,6 +291,15 @@ default_array_element: break; } } +} + | HEX_STRING '*' INTEGER +{ + for (int i = 0; i < $3; i++) { + if (!atomic_element.add_default_value_literal($1)) { + yyerror("Invalid hex literal in default array"); + break; + } + } } ; @@ -322,6 +347,10 @@ type_token: | KW_BLOB { $$ = ST_blob; +} + | KW_INT8ARRAY +{ + $$ = ST_int8array; } | KW_INT16ARRAY { @@ -330,6 +359,10 @@ type_token: | KW_INT32ARRAY { $$ = ST_int32array; +} + | KW_UINT8ARRAY +{ + $$ = ST_uint8array; } | KW_UINT16ARRAY { diff --git a/direct/src/dcparser/dcSubatomicType.cxx b/direct/src/dcparser/dcSubatomicType.cxx index dcc931f1de..12e533b0c8 100644 --- a/direct/src/dcparser/dcSubatomicType.cxx +++ b/direct/src/dcparser/dcSubatomicType.cxx @@ -54,12 +54,18 @@ operator << (ostream &out, DCSubatomicType type) { case ST_blob: return out << "blob"; + case ST_int8array: + return out << "int8array"; + case ST_int16array: return out << "int16array"; case ST_int32array: return out << "int32array"; + case ST_uint8array: + return out << "uint8array"; + case ST_uint16array: return out << "uint16array"; diff --git a/direct/src/dcparser/dcSubatomicType.h b/direct/src/dcparser/dcSubatomicType.h index 0a5dca9706..8e78c16ad5 100644 --- a/direct/src/dcparser/dcSubatomicType.h +++ b/direct/src/dcparser/dcSubatomicType.h @@ -49,6 +49,12 @@ enum DCSubatomicType { ST_uint16array, ST_uint32array, + ST_int8array, + ST_uint8array, + + // New additions should be added at the end to prevent the file hash + // code from changing. + ST_invalid }; END_PUBLISH