added parameter to default skip last symbol in SymbolString::getDataStr (removes CRC from logging and hex result)

This commit is contained in:
john30
2015-03-07 15:36:17 +01:00
parent 97561ff87d
commit 353404e5b0
4 changed files with 20 additions and 17 deletions
+10 -9
View File
@@ -79,7 +79,7 @@ result_t SymbolString::parseHex(const string& str, const bool isEscaped)
return RESULT_OK; return RESULT_OK;
} }
const string SymbolString::getDataStr(const bool unescape) const string SymbolString::getDataStr(const bool unescape, const bool skipLastSymbol)
{ {
stringstream sstr; stringstream sstr;
bool previousEscape = false; bool previousEscape = false;
@@ -87,19 +87,20 @@ const string SymbolString::getDataStr(const bool unescape)
for (size_t i = 0; i < m_data.size(); i++) { for (size_t i = 0; i < m_data.size(); i++) {
unsigned char value = m_data[i]; unsigned char value = m_data[i];
if (m_unescapeState == 0 && unescape && previousEscape) { if (m_unescapeState == 0 && unescape && previousEscape) {
if (value == 0x00) if (!skipLastSymbol || i+1 < m_data.size()) {
sstr << "a9"; // ESC if (value == 0x00)
else if (value == 0x01) sstr << "a9"; // ESC
sstr << "aa"; // SYN else if (value == 0x01)
else sstr << "aa"; // SYN
sstr << "XX"; // invalid escape sequence else
sstr << "XX"; // invalid escape sequence
}
previousEscape = false; previousEscape = false;
} }
else if (m_unescapeState == 0 && unescape && value == ESC) { else if (m_unescapeState == 0 && unescape && value == ESC) {
previousEscape = true; // escape sequence not yet finished previousEscape = true; // escape sequence not yet finished
} }
else { else if (!skipLastSymbol || i+1 < m_data.size()) {
sstr << nouppercase << setw(2) << hex sstr << nouppercase << setw(2) << hex
<< setfill('0') << static_cast<unsigned>(value); << setfill('0') << static_cast<unsigned>(value);
} }
+2 -1
View File
@@ -67,9 +67,10 @@ public:
/** /**
* Returns the symbols as hex string. * Returns the symbols as hex string.
* @param unescape whether to unescape an escaped instance. * @param unescape whether to unescape an escaped instance.
* @param skipLastSymbol whether to skip the last symbol (probably the CRC).
* @return the symbols as hex string. * @return the symbols as hex string.
*/ */
const string getDataStr(const bool unescape=true); const string getDataStr(const bool unescape=true, const bool skipLastSymbol=true);
/** /**
* Returns a reference to the symbol at the specified index. * Returns a reference to the symbol at the specified index.
+5 -4
View File
@@ -26,6 +26,7 @@ using namespace std;
void verify(bool expectFailMatch, string type, string input, void verify(bool expectFailMatch, string type, string input,
bool match, string expectStr, string gotStr) bool match, string expectStr, string gotStr)
{ {
match = match && expectStr == gotStr;
if (expectFailMatch) { if (expectFailMatch) {
if (match) if (match)
cout << " failed " << type << " match >" << input cout << " failed " << type << " match >" << input
@@ -301,14 +302,14 @@ int main()
ostringstream output; ostringstream output;
SymbolString writeMstr(false); SymbolString writeMstr(false);
result = writeMstr.parseHex(mstr.getDataStr().substr(0, 10)); result = writeMstr.parseHex(mstr.getDataStr(true, false).substr(0, 10));
if (result != RESULT_OK) { if (result != RESULT_OK) {
cout << " parse \"" << mstr.getDataStr().substr(0, 10) << "\" error: " << getResultCode(result) << endl; cout << " parse \"" << mstr.getDataStr(true, false).substr(0, 10) << "\" error: " << getResultCode(result) << endl;
} }
SymbolString writeSstr(false); SymbolString writeSstr(false);
result = writeSstr.parseHex(sstr.getDataStr().substr(0, 2)); result = writeSstr.parseHex(sstr.getDataStr(true, false).substr(0, 2));
if (result != RESULT_OK) { if (result != RESULT_OK) {
cout << " parse \"" << sstr.getDataStr().substr(0, 2) << "\" error: " << getResultCode(result) << endl; cout << " parse \"" << sstr.getDataStr(true, false).substr(0, 2) << "\" error: " << getResultCode(result) << endl;
} }
result = fields->read(pt_masterData, mstr, 0, output, false, verbose); result = fields->read(pt_masterData, mstr, 0, output, false, verbose);
if (result >= RESULT_OK) { if (result >= RESULT_OK) {
+3 -3
View File
@@ -31,7 +31,7 @@ int main ()
if (result != RESULT_OK) if (result != RESULT_OK)
std::cout << "parse escaped error: " << getResultCode(result) << std::endl; std::cout << "parse escaped error: " << getResultCode(result) << std::endl;
std::string gotStr = sstr.getDataStr(false), expectStr = "10feb5050427a90015a90177"; std::string gotStr = sstr.getDataStr(false, false), expectStr = "10feb5050427a90015a90177";
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "parse escaped OK" << std::endl; std::cout << "parse escaped OK" << std::endl;
@@ -51,7 +51,7 @@ int main ()
<< std::setfill('0') << static_cast<unsigned>(expectCrc) << std::setfill('0') << static_cast<unsigned>(expectCrc)
<< std::endl; << std::endl;
gotStr = sstr.getDataStr(), expectStr = "10feb5050427a915aa77"; gotStr = sstr.getDataStr(true, false), expectStr = "10feb5050427a915aa77";
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "unescape OK" << std::endl; std::cout << "unescape OK" << std::endl;
@@ -64,7 +64,7 @@ int main ()
if (result != RESULT_OK) if (result != RESULT_OK)
std::cout << "parse unescaped error: " << getResultCode(result) << std::endl; std::cout << "parse unescaped error: " << getResultCode(result) << std::endl;
gotStr = sstr.getDataStr(); gotStr = sstr.getDataStr(true, false);
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "parse unescaped OK" << std::endl; std::cout << "parse unescaped OK" << std::endl;