diff --git a/src/input/readers/Ascii.cc b/src/input/readers/Ascii.cc index 2af3a31a88..ab2c2721a0 100644 --- a/src/input/readers/Ascii.cc +++ b/src/input/readers/Ascii.cc @@ -68,7 +68,7 @@ Ascii::Ascii(ReaderFrontend *frontend) : ReaderBackend(frontend) unset_field.assign( (const char*) BifConst::InputAscii::unset_field->Bytes(), BifConst::InputAscii::unset_field->Len()); - io = new AsciiInputOutput(this, set_separator, unset_field, empty_field); + io = new AsciiInputOutput(this, AsciiInputOutput::SeparatorInfo(set_separator, unset_field, empty_field)); } Ascii::~Ascii() diff --git a/src/input/readers/Benchmark.cc b/src/input/readers/Benchmark.cc index 4738c6e867..9851f61b70 100644 --- a/src/input/readers/Benchmark.cc +++ b/src/input/readers/Benchmark.cc @@ -26,7 +26,7 @@ Benchmark::Benchmark(ReaderFrontend *frontend) : ReaderBackend(frontend) timedspread = double(BifConst::InputBenchmark::timedspread); heartbeat_interval = double(BifConst::Threading::heartbeat_interval); - io = new AsciiInputOutput(this); + io = new AsciiInputOutput(this, AsciiInputOutput::SeparatorInfo()); } Benchmark::~Benchmark() diff --git a/src/logging/writers/Ascii.cc b/src/logging/writers/Ascii.cc index 0ad7098852..8ed8797f09 100644 --- a/src/logging/writers/Ascii.cc +++ b/src/logging/writers/Ascii.cc @@ -52,7 +52,7 @@ Ascii::Ascii(WriterFrontend* frontend) : WriterBackend(frontend) desc.EnableEscaping(); desc.AddEscapeSequence(separator); - io = new AsciiInputOutput(this, set_separator, unset_field, empty_field); + io = new AsciiInputOutput(this, AsciiInputOutput::SeparatorInfo(set_separator, unset_field, empty_field)); } Ascii::~Ascii() diff --git a/src/threading/AsciiInputOutput.cc b/src/threading/AsciiInputOutput.cc index a9d62bd8ab..e64ca06b8f 100644 --- a/src/threading/AsciiInputOutput.cc +++ b/src/threading/AsciiInputOutput.cc @@ -7,24 +7,23 @@ #include "AsciiInputOutput.h" #include "../bro_inet_ntop.h" -AsciiInputOutput::AsciiInputOutput(threading::MsgThread* t) +AsciiInputOutput::AsciiInputOutput(threading::MsgThread* t, const SeparatorInfo info) { thread = t; + this->separators = info; } -AsciiInputOutput::AsciiInputOutput(threading::MsgThread* t, const string & set_separator, +AsciiInputOutput::SeparatorInfo::SeparatorInfo(const string & set_separator, const string & unset_field, const string & empty_field) { - thread = t; this->set_separator = set_separator; this->unset_field = unset_field; this->empty_field = empty_field; } -AsciiInputOutput::AsciiInputOutput(threading::MsgThread* t, const string & set_separator, +AsciiInputOutput::SeparatorInfo::SeparatorInfo(const string & set_separator, const string & unset_field) { - thread = t; this->set_separator = set_separator; this->unset_field = unset_field; } @@ -38,7 +37,7 @@ bool AsciiInputOutput::ValToODesc(ODesc* desc, threading::Value* val, const thre { if ( ! val->present ) { - desc->Add(unset_field); + desc->Add(separators.unset_field); return true; } @@ -94,11 +93,11 @@ bool AsciiInputOutput::ValToODesc(ODesc* desc, threading::Value* val, const thre if ( ! size ) { - desc->Add(empty_field); + desc->Add(separators.empty_field); break; } - if ( size == unset_field.size() && memcmp(data, unset_field.data(), size) == 0 ) + if ( size == separators.unset_field.size() && memcmp(data, separators.unset_field.data(), size) == 0 ) { // The value we'd write out would match exactly the // place-holder we use for unset optional fields. We @@ -124,23 +123,23 @@ bool AsciiInputOutput::ValToODesc(ODesc* desc, threading::Value* val, const thre { if ( ! val->val.set_val.size ) { - desc->Add(empty_field); + desc->Add(separators.empty_field); break; } - desc->AddEscapeSequence(set_separator); + desc->AddEscapeSequence(separators.set_separator); for ( int j = 0; j < val->val.set_val.size; j++ ) { if ( j > 0 ) - desc->AddRaw(set_separator); + desc->AddRaw(separators.set_separator); if ( ! ValToODesc(desc, val->val.set_val.vals[j], field) ) { - desc->RemoveEscapeSequence(set_separator); + desc->RemoveEscapeSequence(separators.set_separator); return false; } } - desc->RemoveEscapeSequence(set_separator); + desc->RemoveEscapeSequence(separators.set_separator); break; } @@ -149,23 +148,23 @@ bool AsciiInputOutput::ValToODesc(ODesc* desc, threading::Value* val, const thre { if ( ! val->val.vector_val.size ) { - desc->Add(empty_field); + desc->Add(separators.empty_field); break; } - desc->AddEscapeSequence(set_separator); + desc->AddEscapeSequence(separators.set_separator); for ( int j = 0; j < val->val.vector_val.size; j++ ) { if ( j > 0 ) - desc->AddRaw(set_separator); + desc->AddRaw(separators.set_separator); if ( ! ValToODesc(desc, val->val.vector_val.vals[j], field) ) { - desc->RemoveEscapeSequence(set_separator); + desc->RemoveEscapeSequence(separators.set_separator); return false; } } - desc->RemoveEscapeSequence(set_separator); + desc->RemoveEscapeSequence(separators.set_separator); break; } @@ -181,7 +180,7 @@ bool AsciiInputOutput::ValToODesc(ODesc* desc, threading::Value* val, const thre threading::Value* AsciiInputOutput::StringToVal(string s, string name, TypeTag type, TypeTag subtype) const { - if ( s.compare(unset_field) == 0 ) // field is not set... + if ( s.compare(separators.unset_field) == 0 ) // field is not set... return new threading::Value(type, false); threading::Value* val = new threading::Value(type, true); @@ -276,14 +275,14 @@ threading::Value* AsciiInputOutput::StringToVal(string s, string name, TypeTag t unsigned int length = 1; for ( unsigned int i = 0; i < s.size(); i++ ) { - if ( s[i] == set_separator[0] ) + if ( s[i] == separators.set_separator[0] ) length++; } unsigned int pos = 0; bool error = false; - if ( empty_field.size() > 0 && s.compare(empty_field) == 0 ) + if ( separators.empty_field.size() > 0 && s.compare(separators.empty_field) == 0 ) length = 0; threading::Value** lvals = new threading::Value* [length]; @@ -311,7 +310,7 @@ threading::Value* AsciiInputOutput::StringToVal(string s, string name, TypeTag t { string element; - if ( ! getline(splitstream, element, set_separator[0]) ) + if ( ! getline(splitstream, element, separators.set_separator[0]) ) break; if ( pos >= length ) @@ -338,7 +337,7 @@ threading::Value* AsciiInputOutput::StringToVal(string s, string name, TypeTag t // Test if the string ends with a set_separator... or if the // complete string is empty. In either of these cases we have // to push an empty val on top of it. - if ( ! error && (s.empty() || *s.rbegin() == set_separator[0]) ) + if ( ! error && (s.empty() || *s.rbegin() == separators.set_separator[0]) ) { lvals[pos] = StringToVal("", name, subtype); if ( lvals[pos] == 0 ) diff --git a/src/threading/AsciiInputOutput.h b/src/threading/AsciiInputOutput.h index 9f36175a5a..a8c4493914 100644 --- a/src/threading/AsciiInputOutput.h +++ b/src/threading/AsciiInputOutput.h @@ -8,22 +8,48 @@ class AsciiInputOutput { public: - // Constructor that leaves separators, etc empty. - // Use if you just need functionality like StringToAddr, etc. - AsciiInputOutput(threading::MsgThread*); - // Constructor that defines all separators, etc. - // Use if you need either ValToODesc or EntryToVal. - AsciiInputOutput(threading::MsgThread*, const string & set_separator, + /** + * A struct to pass the necessary initialization values to the AsciiInputOutput module + * on startup + */ + struct SeparatorInfo + { + //const string separator; + string set_separator; + string empty_field; + string unset_field; + string meta_prefix; + + // Constructor that leaves separators, etc empty. + // Use if you just need functionality like StringToAddr, etc. + SeparatorInfo() { }; + + // Constructor that defines all separators, etc. + // Use if you need either ValToODesc or EntryToVal. + SeparatorInfo(const string & set_separator, const string & unset_field, const string & empty_field); - - // Constructor that defines all separators, etc, besides empty_field, which is not needed for many - // non-ascii-based io sources. - // Use if you need either ValToODesc or EntryToVal. - AsciiInputOutput(threading::MsgThread*, const string & set_separator, - const string & unset_field); - ~AsciiInputOutput(); + // Constructor that defines all separators, etc, besides empty_field, which is not needed for many + // non-ascii-based io sources. + // Use if you need either ValToODesc or EntryToVal. + SeparatorInfo(const string & set_separator, + const string & unset_field); + }; + + /** + * Constructor + * + * @param t The thread that uses this class instance. Used to access thread + * message passing methods + * + * @param info + * SeparatorInfo structure defining the necessary separators + */ + AsciiInputOutput(threading::MsgThread* t, const SeparatorInfo info); + + // Destructor + ~AsciiInputOutput(); // converts a threading value to the corresponding ascii representation // returns false & logs an error with reporter in case an error occurs @@ -75,11 +101,7 @@ class AsciiInputOutput { private: bool CheckNumberError(const string& s, const char * end) const; - string separator; - string set_separator; - string empty_field; - string unset_field; - string meta_prefix; + SeparatorInfo separators; threading::MsgThread* thread; };