Skip to content

Commit

Permalink
pass default destination address, circuit and suffix to load instruct…
Browse files Browse the repository at this point in the history
…ion, fix for instruction cleanup
  • Loading branch information
john30 committed Mar 13, 2016
1 parent f8dce83 commit 6e6e05c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/lib/ebus/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,7 +1689,7 @@ result_t DataFieldTemplates::add(DataField* field, string name, bool replace)
}

result_t DataFieldTemplates::addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end,
vector< vector<string> >* defaults,
vector< vector<string> >* defaults, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix,
const string& filename, unsigned int lineNo)
{
vector<string>::iterator restart = begin;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ebus/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,7 @@ class DataFieldTemplates : public FileReader

// @copydoc
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end,
vector< vector<string> >* defaults,
vector< vector<string> >* defaults, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix,
const string& filename, unsigned int lineNo);

/**
Expand Down
34 changes: 18 additions & 16 deletions src/lib/ebus/filereader.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ class FileReader
* Read the definitions from a file.
* @param filename the name of the file being read.
* @param verbose whether to verbosely log problems.
* @param defaultDest the default destination address (may be overwritten by file name), or empty.
* @param defaultCircuit the default circuit name (may be overwritten by file name), or empty.
* @param defaultSuffix the default circuit name suffix (starting with a ".", may be overwritten by file name, or empty.
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t readFromFile(const string filename, bool verbose=false)
virtual result_t readFromFile(const string filename, bool verbose=false,
string defaultDest = "", string defaultCircuit = "", string defaultSuffix = "")
{
ifstream ifs;
ifs.open(filename.c_str(), ifstream::in);
Expand All @@ -90,22 +94,17 @@ class FileReader
}
size_t lastSep = filename.find_last_of('/');
size_t firstDot = filename.find_first_of('.', lastSep+1);
string defaultDest = "";
string defaultCircuit = "";
string defaultSuffix = "";
if (lastSep!=string::npos && firstDot==lastSep+1+2) { // potential destination address, matches "^ZZ."
result_t result;
defaultDest = filename.substr(lastSep+1, 2);
unsigned char zz = (unsigned char)parseInt(defaultDest.c_str(), 16, 0, 0xff, result, NULL);
if (result!=RESULT_OK || !isValidAddress(zz))
defaultDest = ""; // invalid: not in hex or no master/slave/broadcast address
else {
string str = filename.substr(lastSep+1, 2);
unsigned char zz = (unsigned char)parseInt(str.c_str(), 16, 0, 0xff, result, NULL);
if (result==RESULT_OK && isValidAddress(zz)) {
defaultDest = str;
size_t endDot = filename.find_first_of('.', firstDot+1);
if (endDot>firstDot && endDot-firstDot<=6) { // potential ident, matches "^ZZ.IDENT."
defaultCircuit = filename.substr(firstDot+1, endDot-firstDot-1); // IDENT
if (defaultCircuit.find_first_of(' ')!=string::npos)
defaultCircuit = ""; // invalid: contains spaces
else {
str = filename.substr(firstDot+1, endDot-firstDot-1); // IDENT
if (str.find_first_of(' ')==string::npos) {
defaultCircuit = str;
size_t nextDot = filename.find_first_of('.', endDot+1);
if (nextDot!=string::npos && nextDot>endDot+1) { // potential index suffix, matches "^ZZ.IDENT.[0-9]*."
parseInt(filename.substr(endDot+1, nextDot-endDot-1).c_str(), 10, 1, 16, result, NULL);
Expand Down Expand Up @@ -133,10 +132,10 @@ class FileReader
if (result == RESULT_OK)
continue;
} else
result = addFromFile(it, end, &defaults, filename, lineNo);
result = addFromFile(it, end, &defaults, defaultDest, defaultCircuit, defaultSuffix, filename, lineNo);
}
else
result = addFromFile(it, end, NULL, filename, lineNo);
result = addFromFile(it, end, NULL, defaultDest, defaultCircuit, defaultSuffix, filename, lineNo);

if (result != RESULT_OK) {
if (!verbose) {
Expand Down Expand Up @@ -193,12 +192,15 @@ class FileReader
* @param begin an iterator to the first column of the definition row to read.
* @param end the end iterator of the definition row to read.
* @param defaults all previously read default rows (initial star char removed), or NULL if not supported.
* @param defaultDest the valid destination address extracted from the file name (from ZZ part), or empty.
* @param defaultCircuit the valid circuit name extracted from the file name (from IDENT part), or empty.
* @param defaultSuffix the valid circuit name suffix (starting with a ".") extracted from the file name (number after after IDENT part and "."), or empty.
* @param filename the name of the file being read.
* @param lineNo the current line number in the file being read.
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end,
vector< vector<string> >* defaults,
vector< vector<string> >* defaults, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix,
const string& filename, unsigned int lineNo) = 0;

/**
Expand Down
34 changes: 21 additions & 13 deletions src/lib/ebus/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,8 @@ bool CombinedCondition::isTrue()
}


result_t Instruction::create(const string contextPath, Condition* condition, const string type, vector<string>::iterator& it, const vector<string>::iterator end, Instruction*& returnValue)
result_t Instruction::create(const string contextPath, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix,
Condition* condition, const string type, vector<string>::iterator& it, const vector<string>::iterator end, Instruction*& returnValue)
{
// type[,argument]* (type already skipped by caller)
bool singleton = false;
Expand All @@ -1274,7 +1275,7 @@ result_t Instruction::create(const string contextPath, Condition* condition, con
} else {
path = contextPath.substr(0, pos+1);
}
returnValue = new LoadInstruction(condition, singleton, path+(*it));
returnValue = new LoadInstruction(condition, singleton, path+(*it), defaultDest, defaultCircuit, defaultSuffix);
return RESULT_OK;
}
// unknown instruction
Expand All @@ -1283,7 +1284,7 @@ result_t Instruction::create(const string contextPath, Condition* condition, con


result_t LoadInstruction::execute(MessageMap* messages) {
return messages->readFromFile(m_filename);
return messages->readFromFile(m_filename, false, m_defaultDest, m_defaultCircuit, m_defaultSuffix);
}


Expand Down Expand Up @@ -1452,7 +1453,7 @@ result_t MessageMap::readConditions(string& types, const string& filename, Condi
}

result_t MessageMap::addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end,
vector< vector<string> >* defaults,
vector< vector<string> >* defaults, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix,
const string& filename, unsigned int lineNo)
{
vector<string>::iterator restart = begin;
Expand All @@ -1465,7 +1466,7 @@ result_t MessageMap::addFromFile(vector<string>::iterator& begin, const vector<s
// instruction
types = types.substr(1);
Instruction* instruction = NULL;
result_t result = Instruction::create(filename, condition, types, ++begin, end, instruction);
result_t result = Instruction::create(filename, defaultDest, defaultCircuit, defaultSuffix, condition, types, ++begin, end, instruction);
if (instruction==NULL || result!=RESULT_OK) {
m_lastError = "invalid instruction";
return result;
Expand Down Expand Up @@ -1560,9 +1561,11 @@ result_t MessageMap::executeInstructions(bool verbose) {
for (map<string, vector<Instruction*> >::iterator it = m_instructions.begin(); it != m_instructions.end(); it++) {
vector<Instruction*> instructions = it->second;
bool removeSingletons = false;
vector<Instruction*> remain;
for (vector<Instruction*>::iterator lit = instructions.begin(); lit != instructions.end(); lit++) {
Instruction* instruction = *lit;
if (removeSingletons && instruction->isSingleton()) {
delete instruction;
continue;
}
Condition* condition = instruction->getCondition();
Expand All @@ -1576,30 +1579,35 @@ result_t MessageMap::executeInstructions(bool verbose) {
}
}
if (execute) {
if (instruction->isSingleton())
if (instruction->isSingleton()) {
removeSingletons = true;
}
result_t result = instruction->execute(this);
if (result!=RESULT_OK) {
overallResult = result;
}
delete instruction;
instructions.erase(lit--);
} else {
remain.push_back(instruction);
}
}
if (removeSingletons) {
if (removeSingletons && !remain.empty()) {
instructions = remain;
remain.clear();
for (vector<Instruction*>::iterator lit = instructions.begin(); lit != instructions.end(); lit++) {
Instruction* instruction = *lit;
if (!instruction->isSingleton()) {
remain.push_back(instruction);
continue;
}
delete instruction;
instructions.erase(lit);
lit--;
}
if (instructions.empty()) {
m_instructions.erase(it--);
}
}
if (remain.empty()) {
m_instructions.erase(it--);
} else {
it->second = remain;
}
}
return overallResult;
}
Expand Down
24 changes: 20 additions & 4 deletions src/lib/ebus/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -955,14 +955,18 @@ class Instruction
/**
* Factory method for creating a new instance.
* @param contextPath the path and/or filename context being loaded.
* @param defaultDest the default destination address (may be overwritten by file name), or empty.
* @param defaultCircuit the default circuit name (may be overwritten by file name), or empty.
* @param defaultSuffix the default circuit name suffix (starting with a ".", may be overwritten by file name, or empty.
* @param condition the @a Condition for the instruction, or NULL.
* @param type the type of the instruction.
* @param it the iterator to traverse for the definition parts.
* @param end the iterator pointing to the end of the definition parts.
* @param returnValue the variable in which to store the created instance.
* @return @a RESULT_OK on success, or an error code.
*/
static result_t create(const string contextPath, Condition* condition, const string type, vector<string>::iterator& it, const vector<string>::iterator end, Instruction*& returnValue);
static result_t create(const string contextPath, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix,
Condition* condition, const string type, vector<string>::iterator& it, const vector<string>::iterator end, Instruction*& returnValue);

/**
* Return the @a Condition this instruction requires.
Expand Down Expand Up @@ -1006,9 +1010,12 @@ class LoadInstruction : public Instruction
* @param condition the @a Condition this instruction requires, or null.
* @param singleton whether this @a Instruction belongs to a set of instructions of which only the first one may be executed for the same source file.
* @param filename the name of the file to load.
* @param defaultDest the default destination address (may be overwritten by file name), or empty.
* @param defaultCircuit the default circuit name (may be overwritten by file name), or empty.
* @param defaultSuffix the default circuit name suffix (starting with a ".", may be overwritten by file name, or empty.
*/
LoadInstruction(Condition* condition, const bool singleton, const string filename)
: Instruction(condition, singleton), m_filename(filename) { }
LoadInstruction(Condition* condition, const bool singleton, const string filename, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix)
: Instruction(condition, singleton), m_filename(filename), m_defaultDest(defaultDest), m_defaultCircuit(defaultCircuit), m_defaultSuffix(defaultSuffix) { }

/**
* Destructor.
Expand All @@ -1023,6 +1030,15 @@ class LoadInstruction : public Instruction
/** the name of the file to load. */
const string m_filename;

/** the default destination address (may be overwritten by file name), or empty. */
const string m_defaultDest;

/** the default circuit name (may be overwritten by file name), or empty. */
const string m_defaultCircuit;

/** the default circuit name suffix (starting with a ".", may be overwritten by file name, or empty. */
const string m_defaultSuffix;

};


Expand Down Expand Up @@ -1076,7 +1092,7 @@ class MessageMap : public FileReader

// @copydoc
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end,
vector< vector<string> >* defaults,
vector< vector<string> >* defaults, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix,
const string& filename, unsigned int lineNo);

/**
Expand Down

0 comments on commit 6e6e05c

Please sign in to comment.