read vector.

still missing: enums, empty fields for optional parameters.
This commit is contained in:
Bernhard Amann 2011-11-15 16:32:35 -08:00
parent fb5f26e7fc
commit 821878835a
5 changed files with 83 additions and 36 deletions

View file

@ -244,12 +244,10 @@ bool InputMgr::IsCompatibleType(BroType* t, bool atomic_only)
case TYPE_VECTOR:
{
return false; // do me...
//if ( atomic_only )
// return false;
//
//return IsCompatibleType(t->AsVectorType()->YieldType());
if ( atomic_only )
return false;
return IsCompatibleType(t->AsVectorType()->YieldType(), true);
}
default:
@ -342,7 +340,9 @@ bool InputMgr::UnrollRecordType(vector<LogField*> *fields, const RecordType *rec
field->name = nameprepend + rec->FieldName(i);
field->type = rec->FieldType(i)->Tag();
if ( field->type == TYPE_TABLE ) {
field->set_type = rec->FieldType(i)->AsSetType()->Indices()->PureType()->Tag();
field->subtype = rec->FieldType(i)->AsSetType()->Indices()->PureType()->Tag();
} else if ( field->type == TYPE_VECTOR ) {
field->subtype = rec->FieldType(i)->AsVectorType()->YieldType()->Tag();
}
fields->push_back(field);
@ -870,6 +870,13 @@ int InputMgr::GetLogValLength(const LogVal* val) {
break;
}
case TYPE_VECTOR: {
for ( int i = 0; i < val->val.vector_val.size; i++ ) {
length += GetLogValLength(val->val.vector_val.vals[i]);
}
break;
}
default:
reporter->InternalError("unsupported type %d for GetLogValLength", val->type);
}
@ -936,6 +943,15 @@ int InputMgr::CopyLogVal(char *data, const int startpos, const LogVal* val) {
break;
}
case TYPE_VECTOR: {
int length = 0;
for ( int i = 0; i < val->val.vector_val.size; i++ ) {
length += CopyLogVal(data, startpos+length, val->val.vector_val.vals[i]);
}
return length;
break;
}
default:
reporter->InternalError("unsupported type %d for CopyLogVal", val->type);
return 0;
@ -1039,6 +1055,21 @@ Val* InputMgr::LogValToVal(const LogVal* val, TypeTag request_type) {
break;
}
case TYPE_VECTOR: {
assert ( val->val.vector_val.size > 1 ); // implement empty vector...
// all entries have to have the same type...
TypeTag type = val->val.vector_val.vals[0]->type;
VectorType* vt = new VectorType(base_type(type));
VectorVal* v = new VectorVal(vt);
for ( int i = 0; i < val->val.vector_val.size; i++ ) {
assert( val->val.vector_val.vals[i]->type == type);
v->Assign(i, LogValToVal( val->val.set_val.vals[i], type ), 0);
}
return v;
}
case TYPE_ENUM:
reporter->InternalError("Sorry, Enum reading does not yet work, missing internal inferface");

View file

@ -11,20 +11,20 @@ FieldMapping::FieldMapping(const string& arg_name, const TypeTag& arg_type, int
position = arg_position;
}
FieldMapping::FieldMapping(const string& arg_name, const TypeTag& arg_type, const TypeTag& arg_set_type, int arg_position)
: name(arg_name), type(arg_type), set_type(arg_set_type)
FieldMapping::FieldMapping(const string& arg_name, const TypeTag& arg_type, const TypeTag& arg_subtype, int arg_position)
: name(arg_name), type(arg_type), subtype(arg_subtype)
{
position = arg_position;
}
FieldMapping::FieldMapping(const FieldMapping& arg)
: name(arg.name), type(arg.type), set_type(arg.set_type)
: name(arg.name), type(arg.type), subtype(arg.subtype)
{
position = arg.position;
}
FieldMapping FieldMapping::setType() {
return FieldMapping(name, set_type, position);
FieldMapping FieldMapping::subType() {
return FieldMapping(name, subtype, position);
}
InputReaderAscii::InputReaderAscii()
@ -91,7 +91,7 @@ bool InputReaderAscii::ReadHeader() {
const LogField* field = fields[i];
if ( field->name == s ) {
// cool, found field. note position
FieldMapping f(field->name, field->type, field->set_type, i);
FieldMapping f(field->name, field->type, field->subtype, i);
columnMap.push_back(f);
wantFields++;
break; // done with searching
@ -112,7 +112,7 @@ bool InputReaderAscii::ReadHeader() {
if ( wantFields != (int) num_fields ) {
// we did not find all fields?
// :(
Error("One of the requested fields could not be found in the input data file");
Error(Fmt("One of the requested fields could not be found in the input data file. Found %d fields, wanted %d", wantFields, num_fields));
return false;
}
@ -199,25 +199,40 @@ LogVal* InputReaderAscii::EntryToVal(string s, FieldMapping field) {
break;
}
case TYPE_TABLE: {
// construct a table from entry...
// for the moment assume, that entries are split by ",".
if ( s == "-" ) {
// empty
val->val.set_val.size = 0;
break;
}
case TYPE_TABLE:
case TYPE_VECTOR:
// First - common initialization
// Then - initialization for table.
// Then - initialization for vector.
// Then - common stuff
{
// how many entries do we have...
unsigned int length = 1;
for ( unsigned int i = 0; i < s.size(); i++ )
if ( s[i] == ',') length++;
unsigned int pos = 0;
LogVal** lvals = new LogVal* [length];
val->val.set_val.vals = lvals;
val->val.set_val.size = length;
if ( field.type == TYPE_TABLE ) {
// construct a table from entry...
// for the moment assume, that entries are split by ",".
/* Fix support for emtyp tables if ( s == "-" ) {
// empty
val->val.set_val.size = 0;
break;
} */
val->val.set_val.vals = lvals;
val->val.set_val.size = length;
} else if ( field.type == TYPE_VECTOR ) {
val->val.vector_val.vals = lvals;
val->val.vector_val.size = length;
} else {
assert(false);
}
istringstream splitstream(s);
while ( splitstream ) {
@ -232,7 +247,7 @@ LogVal* InputReaderAscii::EntryToVal(string s, FieldMapping field) {
break;
LogVal* newval = EntryToVal(element, field.setType());
LogVal* newval = EntryToVal(element, field.subType());
if ( newval == 0 ) {
Error("Error while reading set");
return 0;
@ -243,6 +258,7 @@ LogVal* InputReaderAscii::EntryToVal(string s, FieldMapping field) {
}
if ( pos != length ) {
Error("Internal error while parsing set: did not find all elements");
return 0;

View file

@ -12,15 +12,15 @@
struct FieldMapping {
string name;
TypeTag type;
TypeTag set_type;
TypeTag subtype;
int position;
FieldMapping(const string& arg_name, const TypeTag& arg_type, int arg_position);
FieldMapping(const string& arg_name, const TypeTag& arg_type, const TypeTag& arg_set_type, int arg_position);
FieldMapping(const string& arg_name, const TypeTag& arg_type, const TypeTag& arg_subtype, int arg_position);
FieldMapping(const FieldMapping& arg);
FieldMapping() { position = -1; }
FieldMapping setType();
FieldMapping subType();
bool IsEmpty() { return position == -1; }
};

View file

@ -83,16 +83,16 @@ bool LogField::Read(SerializationFormat* fmt)
int t;
int it;
bool success = (fmt->Read(&name, "name") && fmt->Read(&t, "type") && fmt->Read(&it, "set_type") );
bool success = (fmt->Read(&name, "name") && fmt->Read(&t, "type") && fmt->Read(&it, "subtype") );
type = (TypeTag) t;
set_type = (TypeTag) it;
subtype = (TypeTag) it;
return success;
}
bool LogField::Write(SerializationFormat* fmt) const
{
return (fmt->Write(name, "name") && fmt->Write((int)type, "type") && fmt->Write((int)set_type, "set_type"));
return (fmt->Write(name, "name") && fmt->Write((int)type, "type") && fmt->Write((int)subtype, "subtype"));
}
LogVal::~LogVal()

View file

@ -15,12 +15,12 @@ class SerializationFormat;
struct LogField {
string name;
TypeTag type;
// needed by input framework. otherwise it cannot determine the inner type of a set.
TypeTag set_type;
// needed by input framework. otherwise it cannot determine the inner type of a set or vector.
TypeTag subtype;
LogField() { }
LogField(const LogField& other)
: name(other.name), type(other.type), set_type(other.set_type) { }
: name(other.name), type(other.type), subtype(other.subtype) { }
// (Un-)serialize.
bool Read(SerializationFormat* fmt);