add (u)int8array

This commit is contained in:
David Rose 2001-07-14 01:55:50 +00:00
parent b6dac68042
commit c11ab984ae
7 changed files with 153 additions and 37 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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: " + $<str>1);
}
}
| STRING
{
if (!atomic_element.add_default_value($1)) {
yyerror("Invalid default value: " + $<str>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
{

View File

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

View File

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