pass default destination address, circuit and suffix to load instruction, fix for instruction cleanup

This commit is contained in:
john30
2016-03-13 16:04:20 +01:00
parent f8dce83b6a
commit 6e6e05c10e
5 changed files with 61 additions and 35 deletions
+1 -1
View File
@@ -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, 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) const string& filename, unsigned int lineNo)
{ {
vector<string>::iterator restart = begin; vector<string>::iterator restart = begin;
+1 -1
View File
@@ -884,7 +884,7 @@ public:
// @copydoc // @copydoc
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, 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); const string& filename, unsigned int lineNo);
/** /**
+18 -16
View File
@@ -78,9 +78,13 @@ public:
* Read the definitions from a file. * Read the definitions from a file.
* @param filename the name of the file being read. * @param filename the name of the file being read.
* @param verbose whether to verbosely log problems. * @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. * @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; ifstream ifs;
ifs.open(filename.c_str(), ifstream::in); ifs.open(filename.c_str(), ifstream::in);
@@ -90,22 +94,17 @@ public:
} }
size_t lastSep = filename.find_last_of('/'); size_t lastSep = filename.find_last_of('/');
size_t firstDot = filename.find_first_of('.', lastSep+1); 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." if (lastSep!=string::npos && firstDot==lastSep+1+2) { // potential destination address, matches "^ZZ."
result_t result; result_t result;
defaultDest = filename.substr(lastSep+1, 2); string str = filename.substr(lastSep+1, 2);
unsigned char zz = (unsigned char)parseInt(defaultDest.c_str(), 16, 0, 0xff, result, NULL); unsigned char zz = (unsigned char)parseInt(str.c_str(), 16, 0, 0xff, result, NULL);
if (result!=RESULT_OK || !isValidAddress(zz)) if (result==RESULT_OK && isValidAddress(zz)) {
defaultDest = ""; // invalid: not in hex or no master/slave/broadcast address defaultDest = str;
else {
size_t endDot = filename.find_first_of('.', firstDot+1); size_t endDot = filename.find_first_of('.', firstDot+1);
if (endDot>firstDot && endDot-firstDot<=6) { // potential ident, matches "^ZZ.IDENT." if (endDot>firstDot && endDot-firstDot<=6) { // potential ident, matches "^ZZ.IDENT."
defaultCircuit = filename.substr(firstDot+1, endDot-firstDot-1); // IDENT str = filename.substr(firstDot+1, endDot-firstDot-1); // IDENT
if (defaultCircuit.find_first_of(' ')!=string::npos) if (str.find_first_of(' ')==string::npos) {
defaultCircuit = ""; // invalid: contains spaces defaultCircuit = str;
else {
size_t nextDot = filename.find_first_of('.', endDot+1); size_t nextDot = filename.find_first_of('.', endDot+1);
if (nextDot!=string::npos && nextDot>endDot+1) { // potential index suffix, matches "^ZZ.IDENT.[0-9]*." 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); parseInt(filename.substr(endDot+1, nextDot-endDot-1).c_str(), 10, 1, 16, result, NULL);
@@ -133,10 +132,10 @@ public:
if (result == RESULT_OK) if (result == RESULT_OK)
continue; continue;
} else } else
result = addFromFile(it, end, &defaults, filename, lineNo); result = addFromFile(it, end, &defaults, defaultDest, defaultCircuit, defaultSuffix, filename, lineNo);
} }
else else
result = addFromFile(it, end, NULL, filename, lineNo); result = addFromFile(it, end, NULL, defaultDest, defaultCircuit, defaultSuffix, filename, lineNo);
if (result != RESULT_OK) { if (result != RESULT_OK) {
if (!verbose) { if (!verbose) {
@@ -193,12 +192,15 @@ public:
* @param begin an iterator to the first column of the definition row to read. * @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 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 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 filename the name of the file being read.
* @param lineNo the current line number in 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. * @return @a RESULT_OK on success, or an error code.
*/ */
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, 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; const string& filename, unsigned int lineNo) = 0;
/** /**
+20 -12
View File
@@ -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) // type[,argument]* (type already skipped by caller)
bool singleton = false; bool singleton = false;
@@ -1274,7 +1275,7 @@ result_t Instruction::create(const string contextPath, Condition* condition, con
} else { } else {
path = contextPath.substr(0, pos+1); 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; return RESULT_OK;
} }
// unknown instruction // unknown instruction
@@ -1283,7 +1284,7 @@ result_t Instruction::create(const string contextPath, Condition* condition, con
result_t LoadInstruction::execute(MessageMap* messages) { result_t LoadInstruction::execute(MessageMap* messages) {
return messages->readFromFile(m_filename); return messages->readFromFile(m_filename, false, m_defaultDest, m_defaultCircuit, m_defaultSuffix);
} }
@@ -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, 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) const string& filename, unsigned int lineNo)
{ {
vector<string>::iterator restart = begin; vector<string>::iterator restart = begin;
@@ -1465,7 +1466,7 @@ result_t MessageMap::addFromFile(vector<string>::iterator& begin, const vector<s
// instruction // instruction
types = types.substr(1); types = types.substr(1);
Instruction* instruction = NULL; 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) { if (instruction==NULL || result!=RESULT_OK) {
m_lastError = "invalid instruction"; m_lastError = "invalid instruction";
return result; return result;
@@ -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++) { for (map<string, vector<Instruction*> >::iterator it = m_instructions.begin(); it != m_instructions.end(); it++) {
vector<Instruction*> instructions = it->second; vector<Instruction*> instructions = it->second;
bool removeSingletons = false; bool removeSingletons = false;
vector<Instruction*> remain;
for (vector<Instruction*>::iterator lit = instructions.begin(); lit != instructions.end(); lit++) { for (vector<Instruction*>::iterator lit = instructions.begin(); lit != instructions.end(); lit++) {
Instruction* instruction = *lit; Instruction* instruction = *lit;
if (removeSingletons && instruction->isSingleton()) { if (removeSingletons && instruction->isSingleton()) {
delete instruction;
continue; continue;
} }
Condition* condition = instruction->getCondition(); Condition* condition = instruction->getCondition();
@@ -1576,29 +1579,34 @@ result_t MessageMap::executeInstructions(bool verbose) {
} }
} }
if (execute) { if (execute) {
if (instruction->isSingleton()) if (instruction->isSingleton()) {
removeSingletons = true; removeSingletons = true;
}
result_t result = instruction->execute(this); result_t result = instruction->execute(this);
if (result!=RESULT_OK) { if (result!=RESULT_OK) {
overallResult = result; overallResult = result;
} }
delete instruction; 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++) { for (vector<Instruction*>::iterator lit = instructions.begin(); lit != instructions.end(); lit++) {
Instruction* instruction = *lit; Instruction* instruction = *lit;
if (!instruction->isSingleton()) { if (!instruction->isSingleton()) {
remain.push_back(instruction);
continue; continue;
} }
delete instruction; delete instruction;
instructions.erase(lit);
lit--;
} }
if (instructions.empty()) { }
if (remain.empty()) {
m_instructions.erase(it--); m_instructions.erase(it--);
} } else {
it->second = remain;
} }
} }
return overallResult; return overallResult;
+20 -4
View File
@@ -955,6 +955,9 @@ public:
/** /**
* Factory method for creating a new instance. * Factory method for creating a new instance.
* @param contextPath the path and/or filename context being loaded. * @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 condition the @a Condition for the instruction, or NULL.
* @param type the type of the instruction. * @param type the type of the instruction.
* @param it the iterator to traverse for the definition parts. * @param it the iterator to traverse for the definition parts.
@@ -962,7 +965,8 @@ public:
* @param returnValue the variable in which to store the created instance. * @param returnValue the variable in which to store the created instance.
* @return @a RESULT_OK on success, or an error code. * @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. * Return the @a Condition this instruction requires.
@@ -1006,9 +1010,12 @@ public:
* @param condition the @a Condition this instruction requires, or null. * @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 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 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) LoadInstruction(Condition* condition, const bool singleton, const string filename, const string& defaultDest, const string& defaultCircuit, const string& defaultSuffix)
: Instruction(condition, singleton), m_filename(filename) { } : Instruction(condition, singleton), m_filename(filename), m_defaultDest(defaultDest), m_defaultCircuit(defaultCircuit), m_defaultSuffix(defaultSuffix) { }
/** /**
* Destructor. * Destructor.
@@ -1023,6 +1030,15 @@ private:
/** the name of the file to load. */ /** the name of the file to load. */
const string m_filename; 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;
}; };
@@ -1076,7 +1092,7 @@ public:
// @copydoc // @copydoc
virtual result_t addFromFile(vector<string>::iterator& begin, const vector<string>::iterator end, 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); const string& filename, unsigned int lineNo);
/** /**