added SymbolString::adjustHeader(), avoid strcasecmp, code style

This commit is contained in:
john30
2017-04-17 17:46:32 +02:00
parent aaea8f72ab
commit 47ed6dde3f
8 changed files with 33 additions and 19 deletions
+2 -1
View File
@@ -179,7 +179,8 @@ static const struct argp_option argpoptions[] = {
"\"none\" or empty for no initial scan message, \"full\" for full scan, or a single hex address to scan, " "\"none\" or empty for no initial scan message, \"full\" for full scan, or a single hex address to scan, "
"default is broadcast ident message). If combined with --checkconfig, you can add scan message data as " "default is broadcast ident message). If combined with --checkconfig, you can add scan message data as "
"arguments for checking a particular scan configuration, e.g. \"FF08070400/0AB5454850303003277201\".", 0 }, "arguments for checking a particular scan configuration, e.g. \"FF08070400/0AB5454850303003277201\".", 0 },
{"configlang", O_CFGLNG, "LANG", 0, "Prefer LANG in multilingual configuration files [system default language]", 0 }, {"configlang", O_CFGLNG, "LANG", 0,
"Prefer LANG in multilingual configuration files [system default language]", 0 },
{"checkconfig", O_CHKCFG, NULL, 0, "Check CSV config files, then stop", 0 }, {"checkconfig", O_CHKCFG, NULL, 0, "Check CSV config files, then stop", 0 },
{"dumpconfig", O_DMPCFG, NULL, 0, "Check and dump CSV config files, then stop", 0 }, {"dumpconfig", O_DMPCFG, NULL, 0, "Check and dump CSV config files, then stop", 0 },
{"pollinterval", O_POLINT, "SEC", 0, "Poll for data every SEC seconds (0=disable) [5]", 0 }, {"pollinterval", O_POLINT, "SEC", 0, "Poll for data every SEC seconds (0=disable) [5]", 0 },
+6 -6
View File
@@ -1328,16 +1328,16 @@ string MainLoop::executeGrab(vector<string> &args) {
if (args.size() == 1) { if (args.size() == 1) {
return m_busHandler->enableGrab(true) ? "grab started" : "grab continued"; return m_busHandler->enableGrab(true) ? "grab started" : "grab continued";
} }
if (args.size() == 2 && strcasecmp(args[1].c_str(), "STOP") == 0) { if (args.size() == 2 && args[1] == "stop") {
return m_busHandler->enableGrab(false) ? "grab stopped" : "grab not running"; return m_busHandler->enableGrab(false) ? "grab stopped" : "grab not running";
} }
if (args.size() >= 2 && strcasecmp(args[1].c_str(), "RESULT") == 0) { if (args.size() >= 2 && args[1] == "result") {
if (args.size() == 2 || strcasecmp(args[2].c_str(), "ALL") == 0) { if (args.size() == 2 || args[2] == "all") {
ostringstream result; ostringstream result;
m_busHandler->formatGrabResult(args.size() == 2, result); m_busHandler->formatGrabResult(args.size() == 2, result);
return result.str(); return result.str();
} }
if (args.size() == 3 || strcasecmp(args[2].c_str(), "decode") == 0) { // TODO remove strcasecmp if (args.size() == 3 || args[2] == "decode") {
ostringstream result; ostringstream result;
m_busHandler->formatGrabResult(true, result, true); m_busHandler->formatGrabResult(true, result, true);
return result.str(); return result.str();
@@ -1361,7 +1361,7 @@ string MainLoop::executeScan(vector<string> &args, string levels) {
} }
if (args.size() == 2) { if (args.size() == 2) {
if (strcasecmp(args[1].c_str(), "FULL") == 0) { if (args[1] == "full") {
result_t result = m_busHandler->startScan(true, levels); result_t result = m_busHandler->startScan(true, levels);
if (result != RESULT_OK) { if (result != RESULT_OK) {
logError(lf_main, "full scan: %s", getResultCode(result)); logError(lf_main, "full scan: %s", getResultCode(result));
@@ -1369,7 +1369,7 @@ string MainLoop::executeScan(vector<string> &args, string levels) {
return getResultCode(result); return getResultCode(result);
} }
if (strcasecmp(args[1].c_str(), "RESULT") == 0) { if (args[1] == "result") {
ostringstream ret; ostringstream ret;
m_busHandler->formatScanResult(ret); m_busHandler->formatScanResult(ret);
return ret.str(); return ret.str();
+1 -1
View File
@@ -98,7 +98,7 @@ result_t TemParamDataType::writeSymbols(istringstream& input,
string token; string token;
const char* str = input.str().c_str(); const char* str = input.str().c_str();
if (strcasecmp(str, NULL_VALUE) == 0) { if (strcmp(str, NULL_VALUE) == 0) {
value = m_replacement; // replacement value value = m_replacement; // replacement value
} else { } else {
if (input.eof() || !getline(input, token, '-')) { if (input.eof() || !getline(input, token, '-')) {
+1 -1
View File
@@ -792,7 +792,7 @@ result_t NumberDataType::writeSymbols(istringstream& input,
unsigned int value; unsigned int value;
const char* str = input.str().c_str(); const char* str = input.str().c_str();
if (!hasFlag(REQ) && (isIgnored() || strcasecmp(str, NULL_VALUE) == 0)) { if (!hasFlag(REQ) && (isIgnored() || strcmp(str, NULL_VALUE) == 0)) {
value = m_replacement; // replacement value value = m_replacement; // replacement value
} else if (str == NULL || *str == 0) { } else if (str == NULL || *str == 0) {
return RESULT_ERR_EOF; // input too short return RESULT_ERR_EOF; // input too short
+1 -1
View File
@@ -59,7 +59,7 @@ using std::mutex;
#define VALUE_SEPARATOR ';' #define VALUE_SEPARATOR ';'
/** special marker string for skipping columns in @a MappedFileReader. */ /** special marker string for skipping columns in @a MappedFileReader. */
static const string SKIP_COLUMN = "\b"; static const char SKIP_COLUMN[] = "\b";
/** /**
* An abstract class that support reading definitions from a file. * An abstract class that support reading definitions from a file.
+4 -7
View File
@@ -669,7 +669,6 @@ result_t Message::prepareMasterPart(MasterSymbolString& master, istringstream& i
if (index != 0) { if (index != 0) {
return RESULT_ERR_NOTFOUND; return RESULT_ERR_NOTFOUND;
} }
size_t pos = master.size();
master.push_back(0); // length, will be set later master.push_back(0); // length, will be set later
for (size_t i = 2; i < m_id.size(); i++) { for (size_t i = 2; i < m_id.size(); i++) {
master.push_back(m_id[i]); master.push_back(m_id[i]);
@@ -678,7 +677,7 @@ result_t Message::prepareMasterPart(MasterSymbolString& master, istringstream& i
if (result != RESULT_OK) { if (result != RESULT_OK) {
return result; return result;
} }
master[pos] = (symbol_t)(master.size()-pos-1); master.adjustHeader();
return result; return result;
} }
@@ -692,7 +691,7 @@ result_t Message::prepareSlave(istringstream& input, SlaveSymbolString& slave) {
if (result != RESULT_OK) { if (result != RESULT_OK) {
return result; return result;
} }
slave[0] = (symbol_t)(slave.size()-1); slave.adjustHeader();
time(&m_lastUpdateTime); time(&m_lastUpdateTime);
if (slave != m_lastSlaveData) { if (slave != m_lastSlaveData) {
m_lastChangeTime = m_lastUpdateTime; m_lastChangeTime = m_lastUpdateTime;
@@ -1201,11 +1200,9 @@ result_t ChainedMessage::combineLastParts() {
} }
} }
// adjust NN // adjust NN
if (master.size()-5 > 255 || slave.size()-1 > 255) { if (!master.adjustHeader() || !slave.adjustHeader()) {
return RESULT_ERR_INVALID_POS; return RESULT_ERR_INVALID_POS;
} }
master[4] = (symbol_t)(master.size()-5);
slave[0] = (symbol_t)(slave.size()-1);
result_t result = Message::storeLastData(master, 0); result_t result = Message::storeLastData(master, 0);
if (result == RESULT_OK) { if (result == RESULT_OK) {
result = Message::storeLastData(slave, 0); result = Message::storeLastData(slave, 0);
@@ -2449,7 +2446,7 @@ deque<Message*> MessageMap::findAll(const string& circuit, const string& name, c
Message* MessageMap::find(MasterSymbolString& master, bool anyDestination, Message* MessageMap::find(MasterSymbolString& master, bool anyDestination,
const bool withRead, const bool withWrite, const bool withPassive, const bool onlyAvailable) const { const bool withRead, const bool withWrite, const bool withPassive, const bool onlyAvailable) const {
if (master.size() >= 5 && master[4] == 0 && anyDestination && master[2] == 0x07 && master[3] == 0x04) { if (anyDestination && master.size() >= 5 && master[4] == 0 && master[2] == 0x07 && master[3] == 0x04) {
return m_scanMessage; return m_scanMessage;
} }
uint64_t baseKey = Message::createKey(master, uint64_t baseKey = Message::createKey(master,
+2 -1
View File
@@ -1215,7 +1215,8 @@ class MessageMap : public MappedFileReader {
* @param addAll whether to add all messages, even if duplicate. * @param addAll whether to add all messages, even if duplicate.
* @param preferLanguage the preferred language to use, or empty. * @param preferLanguage the preferred language to use, or empty.
*/ */
explicit MessageMap(const bool addAll = false, const string preferLanguage = "") : MappedFileReader::MappedFileReader(true), explicit MessageMap(const bool addAll = false, const string preferLanguage = "")
: MappedFileReader::MappedFileReader(true),
m_addAll(addAll), m_additionalScanMessages(false), m_maxIdLength(0), m_maxBroadcastIdLength(0), m_addAll(addAll), m_additionalScanMessages(false), m_maxIdLength(0), m_maxBroadcastIdLength(0),
m_messageCount(0), m_conditionalMessageCount(0), m_passiveMessageCount(0) { m_messageCount(0), m_conditionalMessageCount(0), m_passiveMessageCount(0) {
m_scanMessage = Message::createScanMessage(); m_scanMessage = Message::createScanMessage();
+16 -1
View File
@@ -130,7 +130,7 @@ class SymbolString {
* @param value the escaped value to add to the current CRC. * @param value the escaped value to add to the current CRC.
*/ */
static void updateCrc(symbol_t& crc, const symbol_t value); static void updateCrc(symbol_t& crc, const symbol_t value);
// TODO add SymbolString::updateHeader() for adjusting length field
/** /**
* Return whether this instance if for the master part. * Return whether this instance if for the master part.
* @return whether this instance if for the master part. * @return whether this instance if for the master part.
@@ -226,6 +226,21 @@ class SymbolString {
*/ */
size_t size() const { return m_data.size(); } size_t size() const { return m_data.size(); }
/**
* Adjust the header NN field to the number of data bytes DD.
* @return true on success, false if the number of data bytes DD is too big.
*/
bool adjustHeader() {
size_t lengthOffset = (m_isMaster ? 4 : 0);
if (m_data.size() <= lengthOffset) {
m_data.resize(lengthOffset+1);
} else if (m_data.size() >= lengthOffset+255) {
return false;
}
m_data[lengthOffset] = (symbol_t)(m_data.size() - 1 - lengthOffset);
return true;
}
/** /**
* Return the offset to the first data byte DD. * Return the offset to the first data byte DD.
* @return the offset to the first data byte DD. * @return the offset to the first data byte DD.