using namespace std;

This commit is contained in:
john30
2014-11-22 20:56:53 +01:00
parent d97bc1bc34
commit 3a34267bee
10 changed files with 294 additions and 277 deletions
+68 -67
View File
@@ -24,9 +24,10 @@
#include <iomanip>
#include <vector>
#include <cstring>
//include <typeinfo>
#include <math.h>
using namespace std;
/** the known data field types. */
static const dataType_t dataTypes[] = {
{"IGN",16*8,bt_str, IGN|ADJ, 0, 1, 16, 0, 0}, // >= 1 byte ignored data
@@ -94,26 +95,26 @@ unsigned int parseInt(const char* str, int base, const unsigned int minValue, co
return ret;
}
result_t DataField::create(std::vector<std::string>::iterator& it,
const std::vector<std::string>::iterator end,
const std::map< std::string, DataField*> templates,
result_t DataField::create(vector<string>::iterator& it,
const vector<string>::iterator end,
const map< string, DataField*> templates,
DataField*& returnField, const bool isSetMessage,
const unsigned char dstAddress)
{
std::vector<SingleDataField*> fields;
std::string firstName, firstComment;
vector<SingleDataField*> fields;
string firstName, firstComment;
result_t result = RESULT_OK;
while (it != end && result == RESULT_OK) {
std::string unit, comment;
string unit, comment;
PartType partType;
unsigned int divisor = 0;
const bool isTemplate = dstAddress == SYN;
std::string token;
string token;
if (it == end)
break;
// name;part;type[:len][;[divisor|values][;[unit][;[comment]]]]
const std::string name = *it++;
const string name = *it++;
if (it == end)
break;
@@ -145,25 +146,25 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
break;
}
std::string typeStr = *it++;
string typeStr = *it++;
if (typeStr.empty() == true) {
if (name.empty() == false || partStr[0] != 0)
result = RESULT_ERR_INVALID_ARG;
break;
}
std::map<unsigned int, std::string> values;
map<unsigned int, string> values;
if (it != end) {
std::string divisorStr = *it++;
string divisorStr = *it++;
if (divisorStr.empty() == false) {
if (divisorStr.find('=') == std::string::npos) {
if (divisorStr.find('=') == string::npos) {
divisor = parseInt(divisorStr.c_str(), 10, 1, 10000, result);
if (result != RESULT_OK)
break;
}
else {
std::istringstream stream(divisorStr);
while (std::getline(stream, token, VALUE_SEPARATOR) != 0) {
istringstream stream(divisorStr);
while (getline(stream, token, VALUE_SEPARATOR) != 0) {
const char* str = token.c_str();
char* strEnd = NULL;
unsigned int id = strtoul(str, &strEnd, 10);
@@ -172,7 +173,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
break;
}
values[id] = std::string(strEnd + 1);
values[id] = string(strEnd + 1);
}
if (result != RESULT_OK)
break;
@@ -198,15 +199,15 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
size_t pos = typeStr.find(LENGTH_SEPARATOR);
unsigned char length;
if (pos == std::string::npos) {
if (pos == string::npos) {
length = 0;
// check for reference(s) to templates
if (templates.empty() == false) {
std::istringstream stream(typeStr);
istringstream stream(typeStr);
bool found = false;
std::string lengthStr;
while (std::getline(stream, token, VALUE_SEPARATOR) != 0) {
std::map<std::string, DataField*>::const_iterator ref = templates.find(token);
string lengthStr;
while (getline(stream, token, VALUE_SEPARATOR) != 0) {
map<string, DataField*>::const_iterator ref = templates.find(token);
if (ref == templates.end()) {
if (found == false)
break; // fallback to direct definition
@@ -325,7 +326,7 @@ result_t DataField::create(std::vector<std::string>::iterator& it,
result_t SingleDataField::read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output,
ostringstream& output,
bool verbose, char separator)
{
SymbolString& input = m_partType != pt_slaveData ? masterData : slaveData;
@@ -365,7 +366,7 @@ result_t SingleDataField::read(SymbolString& masterData, unsigned char masterOff
return RESULT_OK;
}
result_t SingleDataField::write(std::istringstream& input,
result_t SingleDataField::write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
char separator)
@@ -388,10 +389,10 @@ result_t SingleDataField::write(std::istringstream& input,
}
result_t StringDataField::derive(std::string name, std::string comment,
std::string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields)
result_t StringDataField::derive(string name, string comment,
string unit, const PartType partType,
unsigned int divisor, map<unsigned int, string> values,
vector<SingleDataField*>& fields)
{
if (m_partType != pt_any && partType == pt_any)
return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
@@ -410,7 +411,7 @@ result_t StringDataField::derive(std::string name, std::string comment,
}
result_t StringDataField::readSymbols(SymbolString& input,
unsigned char baseOffset, std::ostringstream& output)
unsigned char baseOffset, ostringstream& output)
{
size_t start = 0, count = m_length;
int incr = 1;
@@ -439,8 +440,8 @@ result_t StringDataField::readSymbols(SymbolString& input,
case bt_hexstr:
if (i > 0)
output << ' ';
output << std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(ch);
output << nouppercase << setw(2) << hex << setfill('0')
<< static_cast<unsigned>(ch);
break;
case bt_dat:
if (i + 1 == m_length)
@@ -448,7 +449,7 @@ result_t StringDataField::readSymbols(SymbolString& input,
else if (ch < 1 || (i == 0 && ch > 31) || (i == 1 && ch > 12))
return RESULT_ERR_INVALID_ARG; // invalid date
else
output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch) << ".";
output << setw(2) << setfill('0') << static_cast<unsigned>(ch) << ".";
break;
case bt_tim:
if (m_length == 1) { // truncated time
@@ -464,7 +465,7 @@ result_t StringDataField::readSymbols(SymbolString& input,
return RESULT_ERR_INVALID_ARG; // invalid time
if (i > 0)
output << ":";
output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch);
output << setw(2) << setfill('0') << static_cast<unsigned>(ch);
break;
default:
if (ch < 0x20)
@@ -478,13 +479,13 @@ result_t StringDataField::readSymbols(SymbolString& input,
return RESULT_OK;
}
result_t StringDataField::writeSymbols(std::istringstream& input,
result_t StringDataField::writeSymbols(istringstream& input,
unsigned char baseOffset, SymbolString& output)
{
size_t start = 0, count = m_length;
int incr = 1;
unsigned long int value = 0, last = 0, lastLast = 0;
std::string token;
string token;
if ((m_dataType.flags & REV) != 0) { // reverted binary representation (most significant byte first)
start = m_length - 1;
@@ -524,7 +525,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input,
case bt_dat:
if (m_length == 4 && i == 2)
continue; // skip weekday in between
if (input.eof() == true || std::getline(input, token, '.') == 0)
if (input.eof() == true || getline(input, token, '.') == 0)
return RESULT_ERR_INVALID_ARG; // incomplete
value = parseInt(token.c_str(), 10, 0, 2099, result);
if (result != RESULT_OK)
@@ -555,7 +556,7 @@ result_t StringDataField::writeSymbols(std::istringstream& input,
return RESULT_ERR_INVALID_ARG; // invalid date part
break;
case bt_tim:
if (input.eof() == true || std::getline(input, token, LENGTH_SEPARATOR) == 0)
if (input.eof() == true || getline(input, token, LENGTH_SEPARATOR) == 0)
return RESULT_ERR_INVALID_ARG; // incomplete
value = parseInt(token.c_str(), 10, 0, 59, result);
if (result != RESULT_OK)
@@ -698,10 +699,10 @@ result_t NumericDataField::writeRawValue(unsigned int value,
}
result_t NumberDataField::derive(std::string name, std::string comment,
std::string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields)
result_t NumberDataField::derive(string name, string comment,
string unit, const PartType partType,
unsigned int divisor, map<unsigned int, string> values,
vector<SingleDataField*>& fields)
{
if (m_partType != pt_any && partType == pt_any)
return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
@@ -728,7 +729,7 @@ result_t NumberDataField::derive(std::string name, std::string comment,
}
result_t NumberDataField::readSymbols(SymbolString& input,
unsigned char baseOffset, std::ostringstream& output)
unsigned char baseOffset, ostringstream& output)
{
unsigned int value = 0;
int signedValue;
@@ -748,8 +749,8 @@ result_t NumberDataField::readSymbols(SymbolString& input,
if (m_divisor <= 1)
output << static_cast<unsigned>(value);
else
output << std::setprecision((m_bitCount % 8) == 0 ? m_dataType.precisionOrFirstBit : 0)
<< std::fixed << static_cast<float>(value / (float) m_divisor);
output << setprecision((m_bitCount % 8) == 0 ? m_dataType.precisionOrFirstBit : 0)
<< fixed << static_cast<float>(value / (float) m_divisor);
return RESULT_OK;
}
signedValue = (int) value; // negative signed value
@@ -762,13 +763,13 @@ result_t NumberDataField::readSymbols(SymbolString& input,
if (m_divisor <= 1)
output << static_cast<int>(signedValue);
else
output << std::setprecision((m_bitCount % 8) == 0 ? m_dataType.precisionOrFirstBit : 0)
<< std::fixed << static_cast<float>(signedValue / (float) m_divisor);
output << setprecision((m_bitCount % 8) == 0 ? m_dataType.precisionOrFirstBit : 0)
<< fixed << static_cast<float>(signedValue / (float) m_divisor);
return RESULT_OK;
}
result_t NumberDataField::writeSymbols(std::istringstream& input,
result_t NumberDataField::writeSymbols(istringstream& input,
unsigned char baseOffset, SymbolString& output)
{
unsigned int value;
@@ -830,10 +831,10 @@ result_t NumberDataField::writeSymbols(std::istringstream& input,
}
result_t ValueListDataField::derive(std::string name, std::string comment,
std::string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields)
result_t ValueListDataField::derive(string name, string comment,
string unit, const PartType partType,
unsigned int divisor, map<unsigned int, string> values,
vector<SingleDataField*>& fields)
{
if (m_partType != pt_any && partType == pt_any)
return RESULT_ERR_INVALID_ARG; // cannot create a template from a concrete instance
@@ -860,7 +861,7 @@ result_t ValueListDataField::derive(std::string name, std::string comment,
}
result_t ValueListDataField::readSymbols(SymbolString& input,
unsigned char baseOffset, std::ostringstream& output)
unsigned char baseOffset, ostringstream& output)
{
unsigned int value = 0;
@@ -868,7 +869,7 @@ result_t ValueListDataField::readSymbols(SymbolString& input,
if (result != RESULT_OK)
return result;
std::map<unsigned int, std::string>::iterator it = m_values.find(value);
map<unsigned int, string>::iterator it = m_values.find(value);
if (it != m_values.end()) {
output << it->second;
return RESULT_OK;
@@ -882,7 +883,7 @@ result_t ValueListDataField::readSymbols(SymbolString& input,
return RESULT_ERR_INVALID_ARG; // value assignment not found
}
result_t ValueListDataField::writeSymbols(std::istringstream& input,
result_t ValueListDataField::writeSymbols(istringstream& input,
unsigned char baseOffset, SymbolString& output)
{
if (isIgnored() == true)
@@ -890,7 +891,7 @@ result_t ValueListDataField::writeSymbols(std::istringstream& input,
const char* str = input.str().c_str();
for (std::map<unsigned int, std::string>::iterator it = m_values.begin(); it != m_values.end(); it++)
for (map<unsigned int, string>::iterator it = m_values.begin(); it != m_values.end(); it++)
if (it->second.compare(str) == 0)
return writeRawValue(it->first, baseOffset, output);
@@ -914,7 +915,7 @@ unsigned char DataFieldSet::getLength(PartType partType)
bool previousFullByteOffset[] = { true, true, true, true };
for (std::vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
SingleDataField* field = *it;
if (field->getPartType() == partType) {
if (previousFullByteOffset[partType] == false && field->hasFullByteOffset(false) == false)
@@ -929,15 +930,15 @@ unsigned char DataFieldSet::getLength(PartType partType)
return length;
}
result_t DataFieldSet::derive(std::string name, std::string comment,
std::string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields)
result_t DataFieldSet::derive(string name, string comment,
string unit, const PartType partType,
unsigned int divisor, map<unsigned int, string> values,
vector<SingleDataField*>& fields)
{
if (values.empty() == false)
return RESULT_ERR_INVALID_ARG; // value list not allowed in set derive
for (std::vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
result_t result = (*it)->derive("", "", "", partType, divisor, values, fields);
if (result != RESULT_OK)
return result;
@@ -948,7 +949,7 @@ result_t DataFieldSet::derive(std::string name, std::string comment,
result_t DataFieldSet::read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output, bool verbose, char separator)
ostringstream& output, bool verbose, char separator)
{
if (verbose)
output << m_name << "={ ";
@@ -959,7 +960,7 @@ result_t DataFieldSet::read(SymbolString& masterData, unsigned char masterOffset
offsets[pt_masterData] = masterOffset;
offsets[pt_slaveData] = slaveOffset;
bool previousFullByteOffset[] = { true, true, true, true };
for (std::vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
SingleDataField* field = *it;
bool ignored = field->isIgnored();
PartType partType = field->getPartType();
@@ -994,19 +995,19 @@ result_t DataFieldSet::read(SymbolString& masterData, unsigned char masterOffset
return RESULT_OK;
}
result_t DataFieldSet::write(std::istringstream& input,
result_t DataFieldSet::write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
char separator)
{
std::string token;
string token;
unsigned char offsets[4];
memset(offsets, 0, sizeof(offsets));
offsets[pt_masterData] = masterOffset;
offsets[pt_slaveData] = slaveOffset;
bool previousFullByteOffset[] = { true, true, true, true };
for (std::vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
for (vector<SingleDataField*>::iterator it = m_fields.begin(); it < m_fields.end(); it++) {
SingleDataField* field = *it;
bool ignored = field->isIgnored();
PartType partType = field->getPartType();
@@ -1020,10 +1021,10 @@ result_t DataFieldSet::write(std::istringstream& input,
if (m_fields.size() > 1) {
if (ignored == true)
token.clear();
else if (std::getline(input, token, separator) == 0)
else if (getline(input, token, separator) == 0)
return RESULT_ERR_INVALID_ARG; // incomplete
std::istringstream single(token);
istringstream single(token);
result = (*it)->write(single, masterData, offsets[pt_masterData], slaveData, offsets[pt_slaveData], separator);
}
else
+67 -65
View File
@@ -26,6 +26,8 @@
#include <vector>
#include <map>
using namespace std;
/** the message part in which a data field is stored. */
enum PartType {
pt_any, // stored in any data (master or slave)
@@ -92,7 +94,7 @@ public:
* @param name the field name.
* @param comment the field comment.
*/
DataField(const std::string name, const std::string comment)
DataField(const string name, const string comment)
: m_name(name), m_comment(comment) {}
/**
* @brief Destructor.
@@ -109,8 +111,8 @@ public:
* @return @a RESULT_OK on success, or an error code.
* Note: the caller needs to free the created instance.
*/
static result_t create(std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end,
const std::map<std::string, DataField*> templates, DataField*& returnField,
static result_t create(vector<string>::iterator& it, const vector<string>::iterator end,
const map<string, DataField*> templates, DataField*& returnField,
const bool isSetMessage=false, const unsigned char dstAddress=SYN);
/**
* @brief Returns the length of this field (or contained fields) in bytes.
@@ -126,27 +128,27 @@ public:
* @param partType the message part in which the field is stored.
* @param divisor the extra divisor to apply on the value, or 1 for none (if applicable).
* @param values the value=text assignments, or empty to use this fields assignments (if applicable).
* @param fields the @a std::vector to which created @a SingleDataField instances shall be added.
* @param fields the @a vector to which created @a SingleDataField instances shall be added.
*/
virtual result_t derive(std::string name, std::string comment,
std::string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields) = 0;
virtual result_t derive(string name, string comment,
string unit, const PartType partType,
unsigned int divisor, map<unsigned int, string> values,
vector<SingleDataField*>& fields) = 0;
/**
* @brief Get the field name.
* @return the field name.
*/
std::string getName() const { return m_name; }
string getName() const { return m_name; }
/**
* @brief Get the field comment.
* @return the field comment.
*/
std::string getComment() const { return m_comment; }
string getComment() const { return m_comment; }
/**
* @brief Reads the value from the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString for reading binary data.
* @param slaveData the unescaped slave data @a SymbolString for reading binary data.
* @param output the @a std::ostringstream to append the formatted value to.
* @param output the @a ostringstream to append the formatted value to.
* @param verbose whether to prepend the name, append the unit (if present), and append
* the comment in square brackets (if present).
* @param separator the separator character between multiple fields.
@@ -154,17 +156,17 @@ public:
*/
virtual result_t read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output,
ostringstream& output,
bool verbose=false, char separator=';') = 0;
/**
* @brief Writes the value to the master or slave @a SymbolString.
* @param input the @a std::istringstream to parse the formatted value from.
* @param input the @a istringstream to parse the formatted value from.
* @param masterData the unescaped master data @a SymbolString for writing binary data.
* @param slaveData the unescaped slave data @a SymbolString for writing binary data.
* @param separator the separator character between multiple fields.
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t write(std::istringstream& input,
virtual result_t write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
char separator=';') = 0;
@@ -172,9 +174,9 @@ public:
protected:
/** the field name. */
const std::string m_name;
const string m_name;
/** the field comment. */
const std::string m_comment;
const string m_comment;
};
@@ -195,8 +197,8 @@ public:
* @param partType the message part in which the field is stored.
* @param length the number of symbols in the message part in which the field is stored.
*/
SingleDataField(const std::string name, const std::string comment,
const std::string unit, const dataType_t dataType, const PartType partType,
SingleDataField(const string name, const string comment,
const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length)
: DataField(name, comment),
m_unit(unit), m_dataType(dataType), m_partType(partType),
@@ -209,7 +211,7 @@ public:
* @brief Get the value unit.
* @return the value unit.
*/
std::string getUnit() const { return m_unit; }
string getUnit() const { return m_unit; }
/**
* @brief Get whether this field is ignored.
* @return whether this field is ignored.
@@ -243,18 +245,18 @@ public:
*/
virtual result_t read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output,
ostringstream& output,
bool verbose, char separator);
/**
* @brief Writes the value to the master or slave @a SymbolString.
* @param input the @a std::istringstream to parse the formatted value from.
* @param input the @a istringstream to parse the formatted value from.
* @param masterData the unescaped master data @a SymbolString for writing binary data.
* @param masterOffset the extra offset for writing master data.
* @param slaveData the unescaped slave data @a SymbolString for writing binary data.
* @param slaveOffset the extra offset for writing slave data.
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t write(std::istringstream& input,
virtual result_t write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
char separator);
@@ -268,18 +270,18 @@ protected:
* @param output the ostringstream to append the formatted value to.
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, std::ostringstream& output) = 0;
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, ostringstream& output) = 0;
/**
* @brief Internal method for writing the field to a @a SymbolString.
* @param input the @a std::istringstream to parse the formatted value from.
* @param input the @a istringstream to parse the formatted value from.
* @param offset the offset in the @a SymbolString.
* @param output the unescaped @a SymbolString to write the binary value to.
* @return @a RESULT_OK on success, or an error code.
*/
virtual result_t writeSymbols(std::istringstream& input, const unsigned char offset, SymbolString& output) = 0;
virtual result_t writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output) = 0;
/** the value unit. */
const std::string m_unit;
const string m_unit;
/** the data type definition. */
const dataType_t m_dataType;
/** the message part in which the field is stored. */
@@ -306,8 +308,8 @@ public:
* @param partType the message part in which the field is stored.
* @param length the number of symbols in the message part in which the field is stored.
*/
StringDataField(const std::string name, const std::string comment,
const std::string unit, const dataType_t dataType, const PartType partType,
StringDataField(const string name, const string comment,
const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length)
: SingleDataField(name, comment, unit, dataType, partType, length) {}
/**
@@ -315,17 +317,17 @@ public:
*/
virtual ~StringDataField() {}
// @copydoc
virtual result_t derive(std::string name, std::string comment,
std::string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields);
virtual result_t derive(string name, string comment,
string unit, const PartType partType,
unsigned int divisor, map<unsigned int, string> values,
vector<SingleDataField*>& fields);
protected:
// @copydoc
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, std::ostringstream& output);
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, ostringstream& output);
// @copydoc
virtual result_t writeSymbols(std::istringstream& input, const unsigned char offset, SymbolString& output);
virtual result_t writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output);
};
@@ -348,8 +350,8 @@ public:
* @param bitCount the number of bits in the binary value.
* @param bitOffset the offset to the first bit in the binary value.
*/
NumericDataField(const std::string name, const std::string comment,
const std::string unit, const dataType_t dataType, const PartType partType,
NumericDataField(const string name, const string comment,
const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length, const unsigned char bitCount, const unsigned char bitOffset)
: SingleDataField(name, comment, unit, dataType, partType, length),
m_bitCount(bitCount), m_bitOffset(bitOffset) {}
@@ -406,8 +408,8 @@ public:
* @param length the number of symbols in the message part in which the field is stored.
* @param divisor the extra divisor to apply on the value, or 1 for none.
*/
NumberDataField(const std::string name, const std::string comment,
const std::string unit, const dataType_t dataType, const PartType partType,
NumberDataField(const string name, const string comment,
const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length, const unsigned char bitCount,
const unsigned int divisor)
: NumericDataField(name, comment, unit, dataType, partType, length, bitCount,
@@ -418,17 +420,17 @@ public:
*/
virtual ~NumberDataField() {}
// @copydoc
virtual result_t derive(std::string name, std::string comment,
std::string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields);
virtual result_t derive(string name, string comment,
string unit, const PartType partType,
unsigned int divisor, map<unsigned int, string> values,
vector<SingleDataField*>& fields);
protected:
// @copydoc
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, std::ostringstream& output);
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, ostringstream& output);
// @copydoc
virtual result_t writeSymbols(std::istringstream& input, const unsigned char offset, SymbolString& output);
virtual result_t writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output);
/** the combined divisor to apply on the value, or 1 for none. */
const unsigned int m_divisor;
@@ -453,10 +455,10 @@ public:
* @param length the number of symbols in the message part in which the field is stored.
* @param values the value=text assignments.
*/
ValueListDataField(const std::string name, const std::string comment,
const std::string unit, const dataType_t dataType, const PartType partType,
ValueListDataField(const string name, const string comment,
const string unit, const dataType_t dataType, const PartType partType,
const unsigned char length, const unsigned char bitCount,
const std::map<unsigned int, std::string> values)
const map<unsigned int, string> values)
: NumericDataField(name, comment, unit, dataType, partType, length, bitCount,
(dataType.maxBits < 8) ? dataType.precisionOrFirstBit : 0),
m_values(values) {}
@@ -465,20 +467,20 @@ public:
*/
virtual ~ValueListDataField() {}
// @copydoc
virtual result_t derive(std::string name, std::string comment,
std::string unit, const PartType partType, unsigned int divisor,
std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields);
virtual result_t derive(string name, string comment,
string unit, const PartType partType, unsigned int divisor,
map<unsigned int, string> values,
vector<SingleDataField*>& fields);
protected:
// @copydoc
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, std::ostringstream& output);
virtual result_t readSymbols(SymbolString& input, const unsigned char offset, ostringstream& output);
// @copydoc
virtual result_t writeSymbols(std::istringstream& input, const unsigned char offset, SymbolString& output);
virtual result_t writeSymbols(istringstream& input, const unsigned char offset, SymbolString& output);
/** the value=text assignments. */
std::map<unsigned int, std::string> m_values;
map<unsigned int, string> m_values;
};
@@ -494,10 +496,10 @@ public:
* @brief Constructs a new instance.
* @param name the field name.
* @param comment the field comment.
* @param fields the @a std::vector of @a SingleDataField instances part of this set.
* @param fields the @a vector of @a SingleDataField instances part of this set.
*/
DataFieldSet(const std::string name, const std::string comment,
const std::vector<SingleDataField*> fields)
DataFieldSet(const string name, const string comment,
const vector<SingleDataField*> fields)
: DataField(name, comment),
m_fields(fields) {}
/**
@@ -507,10 +509,10 @@ public:
// @copydoc
virtual unsigned char getLength(PartType partType);
// @copydoc
virtual result_t derive(std::string name, std::string comment,
std::string unit, const PartType partType,
unsigned int divisor, std::map<unsigned int, std::string> values,
std::vector<SingleDataField*>& fields);
virtual result_t derive(string name, string comment,
string unit, const PartType partType,
unsigned int divisor, map<unsigned int, string> values,
vector<SingleDataField*>& fields);
/**
* @brief Returns the @a SingleDataField at the specified index.
* @param index the index of the @a SingleDataField to return.
@@ -531,18 +533,18 @@ public:
// @copydoc
virtual result_t read(SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
std::ostringstream& output,
ostringstream& output,
bool verbose, char separator);
// @copydoc
virtual result_t write(std::istringstream& input,
virtual result_t write(istringstream& input,
SymbolString& masterData, unsigned char masterOffset,
SymbolString& slaveData, unsigned char slaveOffset,
char separator);
protected:
/** the @a std::vector of @a SingleDataField instances part of this set. */
std::vector<SingleDataField*> m_fields;
/** the @a vector of @a SingleDataField instances part of this set. */
vector<SingleDataField*> m_fields;
};
+13 -11
View File
@@ -25,8 +25,10 @@
#include <vector>
#include <cstring>
result_t Message::create(std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end,
const std::map<std::string, DataField*> templates, Message*& returnValue)
using namespace std;
result_t Message::create(vector<string>::iterator& it, const vector<string>::iterator end,
const map<string, DataField*> templates, Message*& returnValue)
{
result_t result;
// [type];class;name;[comment];[QQ];ZZ;id;fields...
@@ -60,17 +62,17 @@ result_t Message::create(std::vector<std::string>::iterator& it, const std::vect
isSetMessage = false;
}
std::string clazz = *it++;
string clazz = *it++;
if (it == end)
return RESULT_ERR_EOF;
std::string name = *it++;
string name = *it++;
if (it == end)
return RESULT_ERR_EOF;
if (name.length() == 0)
return RESULT_ERR_INVALID_ARG; // empty name
std::string comment = *it++;
string comment = *it++;
if (it == end)
return RESULT_ERR_EOF;
@@ -98,9 +100,9 @@ result_t Message::create(std::vector<std::string>::iterator& it, const std::vect
if (isValidAddress(dstAddress) == false)
return RESULT_ERR_INVALID_ARG;
std::istringstream input(*it++); // message id (PBSB + optional master data)
std::vector<unsigned char> id;
std::string token;
istringstream input(*it++); // message id (PBSB + optional master data)
vector<unsigned char> id;
string token;
if (it == end)
return RESULT_ERR_EOF;
while (input.eof() == false) {
@@ -131,7 +133,7 @@ result_t Message::create(std::vector<std::string>::iterator& it, const std::vect
return RESULT_OK;
}
result_t Message::prepare(const unsigned char srcAddress, SymbolString& masterData, std::istringstream& input, char separator)
result_t Message::prepare(const unsigned char srcAddress, SymbolString& masterData, istringstream& input, char separator)
{
if (m_isActiveMessage == true) {
masterData.clear();
@@ -153,7 +155,7 @@ result_t Message::prepare(const unsigned char srcAddress, SymbolString& masterDa
}
result_t Message::handle(SymbolString& masterData, SymbolString& slaveData,
std::ostringstream& output, char separator, bool answer)
ostringstream& output, char separator, bool answer)
{
if (m_isActiveMessage == true) {
result_t result = m_data->read(masterData, m_id.size() - 2, slaveData, 0, output, false, separator);
@@ -161,7 +163,7 @@ result_t Message::handle(SymbolString& masterData, SymbolString& slaveData,
return result;
}
else if (answer == true) {
std::istringstream input; // TODO create input from database of internal variables
istringstream input; // TODO create input from database of internal variables
result_t result = m_data->write(input, masterData, m_id.size() - 2, slaveData, 0, separator);
if (result != RESULT_OK)
return result;
+20 -18
View File
@@ -26,6 +26,8 @@
#include <string>
#include <vector>
using namespace std;
/**
* @brief Base class for all kinds of bus messages.
*/
@@ -48,10 +50,10 @@ public:
* @param data the @a DataField for encoding/decoding the message.
* @param pollPriority the priority for polling, or 0 for no polling at all.
*/
Message(const std::string clazz, const std::string name, const bool isSetMessage,
const bool isActiveMessage, const std::string comment,
Message(const string clazz, const string name, const bool isSetMessage,
const bool isActiveMessage, const string comment,
const unsigned char srcAddress, const unsigned char dstAddress,
const std::vector<unsigned char> id, DataField* data,
const vector<unsigned char> id, DataField* data,
const unsigned int pollPriority)
: m_class(clazz), m_name(name), m_isSetMessage(isSetMessage),
m_isActiveMessage(isActiveMessage), m_comment(comment),
@@ -70,18 +72,18 @@ public:
* @return @a RESULT_OK on success, or an error code.
* Note: the caller needs to free the created instance.
*/
static result_t create(std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end,
const std::map<std::string, DataField*> templates, Message*& returnValue);
static result_t create(vector<string>::iterator& it, const vector<string>::iterator end,
const map<string, DataField*> templates, Message*& returnValue);
/**
* @brief Get the optional device class.
* @return the optional device class.
*/
std::string getClass() const { return m_class; }
string getClass() const { return m_class; }
/**
* @brief Get the message name (unique within the same class and type).
* @return the message name (unique within the same class and type).
*/
std::string getName() const { return m_name; }
string getName() const { return m_name; }
/**
* @brief Get whether this is a set message.
* @return whether this is a set message.
@@ -99,7 +101,7 @@ public:
* @brief Get the comment.
* @return the comment.
*/
std::string getComment() const { return m_comment; }
string getComment() const { return m_comment; }
/**
* @brief Get the source address.
* @return the source address, or @a SYN for any.
@@ -114,39 +116,39 @@ public:
* @brief Get the command ID bytes.
* @return the primary, secondary, and optionally further command ID bytes.
*/
std::vector<unsigned char> getId() const { return m_id; }
vector<unsigned char> getId() const { return m_id; }
/**
* @brief Reads the value from the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString for reading binary data.
* @param slaveData the unescaped slave data @a SymbolString for reading binary data.
* @param output the @a std::ostringstream to append the formatted value to.
* @param output the @a ostringstream to append the formatted value to.
* @param verbose whether to prepend the name, append the unit (if present), and append
* the comment in square brackets (if present).
* @param separator the separator character between multiple fields.
* @return @a RESULT_OK on success, or an error code.
*/
//result_t read(SymbolString& masterData, SymbolString& slaveData, std::ostringstream& output,
//result_t read(SymbolString& masterData, SymbolString& slaveData, ostringstream& output,
// bool verbose=false, char separator=';') = 0;
/**
* @brief Writes the value to the master or slave @a SymbolString.
* @param input the @a std::istringstream to parse the formatted value from.
* @param input the @a istringstream to parse the formatted value from.
* @param masterData the unescaped master data @a SymbolString for writing binary data.
* @param slaveData the unescaped slave data @a SymbolString for writing binary data.
* @param separator the separator character between multiple fields.
* @return @a RESULT_OK on success, or an error code.
*/
result_t prepare(const unsigned char srcAddress, SymbolString& masterData,
std::istringstream& input, char separator=';');
istringstream& input, char separator=';');
result_t handle(SymbolString& masterData, SymbolString& slaveData,
std::ostringstream& output, char separator=';', bool answer=false);
ostringstream& output, char separator=';', bool answer=false);
private:
/** the optional device class. */
const std::string m_class;
const string m_class;
/** the message name (unique within the same class and type). */
const std::string m_name;
const string m_name;
/** whether this is a set message. */
const bool m_isSetMessage;
/** true if message can be initiated by the daemon itself and any other
@@ -154,13 +156,13 @@ private:
* other than the daemon. */
const bool m_isActiveMessage;
/** the comment. */
const std::string m_comment;
const string m_comment;
/** the source address (optional if passive), or @a SYN for any. */
const unsigned char m_srcAddress;
/** the destination address. */
const unsigned char m_dstAddress;
/** the primary, secondary, and optionally further command ID bytes. */
const std::vector<unsigned char> m_id;
const vector<unsigned char> m_id;
/** the @a DataField for encoding/decoding the message. */
DataField* m_data;
/** the priority for polling, or 0 for no polling at all. */
+4
View File
@@ -18,8 +18,12 @@
*/
#include "result.h"
#include <iostream>
using namespace std;
const char* getResultCode(result_t resultCode) {
cout << "DEBUG error code: " << static_cast<signed>(resultCode) << endl;
switch (resultCode) {
case RESULT_ERR_SEND: return "ERR_SEND: send error";
case RESULT_ERR_EXTRA_DATA: return "ERR_EXTRA_DATA: received bytes > sent bytes";
+8 -6
View File
@@ -22,6 +22,8 @@
#include <iostream>
#include <iomanip>
using namespace std;
/**
* @brief CRC8 lookup table for the polynom 0x9b = x^8 + x^7 + x^4 + x^3 + x^1 + 1.
*/
@@ -46,7 +48,7 @@ static const unsigned char CRC_LOOKUP_TABLE[] =
};
SymbolString::SymbolString(const std::string str)
SymbolString::SymbolString(const string str)
: m_unescapeState(0), m_crc(0)
{
// parse + escape
@@ -58,7 +60,7 @@ SymbolString::SymbolString(const std::string str)
push_back(m_crc, false, false);
}
SymbolString::SymbolString(const std::string str, bool isEscaped)
SymbolString::SymbolString(const string str, bool isEscaped)
: m_unescapeState(1), m_crc(0)
{
// parse + optionally unescape
@@ -68,9 +70,9 @@ SymbolString::SymbolString(const std::string str, bool isEscaped)
}
}
const std::string SymbolString::getDataStr(const bool unescape)
const string SymbolString::getDataStr(const bool unescape)
{
std::stringstream sstr;
stringstream sstr;
bool previousEscape = false;
for (size_t i = 0; i < m_data.size(); i++) {
@@ -89,8 +91,8 @@ const std::string SymbolString::getDataStr(const bool unescape)
previousEscape = true; // escape sequence not yet finished
}
else {
sstr << std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(value);
sstr << nouppercase << setw(2) << hex
<< setfill('0') << static_cast<unsigned>(value);
}
}
+6 -4
View File
@@ -25,6 +25,8 @@
#include <sstream>
#include <queue>
using namespace std;
static const unsigned char ESC = 0xA9; // escape symbol, either followed by 0x00 for the value 0xA9, or 0x01 for the value 0xAA
static const unsigned char SYN = 0xAA; // synchronization symbol
static const unsigned char ACK = 0x00; // positive acknowledge
@@ -48,19 +50,19 @@ public:
* @brief Creates a new escaped instance from an unescaped hex string and adds the calculated CRC.
* @param str the unescaped hex string.
*/
SymbolString(const std::string str);
SymbolString(const string str);
/**
* @brief Creates a new unescaped instance from a hex string.
* @param isEscaped whether the hex string is escaped and shall be unescaped.
* @param str the hex string.
*/
SymbolString(const std::string str, const bool isEscaped);
SymbolString(const string str, const bool isEscaped);
/**
* @brief Returns the symbols as hex string.
* @param unescape whether to unescape an escaped instance.
* @return the symbols as hex string.
*/
const std::string getDataStr(const bool unescape=true);
const string getDataStr(const bool unescape=true);
/**
* @brief Returns a reference to the symbol at the specified index.
* @param index the index of the symbol to return.
@@ -114,7 +116,7 @@ private:
/**
* @brief the string of bus symbols.
*/
std::vector<unsigned char> m_data;
vector<unsigned char> m_data;
/**
* @brief 0 if the symbols in @a m_data are escaped,
* 1 if the symbols in @a m_data are unescaped and the last symbol passed to @a push_back was a normal symbol,
+51 -51
View File
@@ -21,27 +21,28 @@
#include <iostream>
#include <iomanip>
void verify(bool expectFailMatch, std::string type, std::string input,
bool match, std::string expectStr, std::string gotStr)
using namespace std;
void verify(bool expectFailMatch, string type, string input,
bool match, string expectStr, string gotStr)
{
if (expectFailMatch == true) {
if (match == true)
std::cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << std::endl;
cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << endl;
else
std::cout << " failed " << type << " match >" << input << "< OK"
<< std::endl;
cout << " failed " << type << " match >" << input << "< OK" << endl;
}
else if (match == true)
std::cout << " " << type << " match >" << input << "< OK" << std::endl;
cout << " " << type << " match >" << input << "< OK" << endl;
else
std::cout << " " << type << " match >" << input << "< error: got >" << gotStr
<< "<, expected >" << expectStr << "<" << std::endl;
cout << " " << type << " match >" << input << "< error: got >"
<< gotStr << "<, expected >" << expectStr << "<" << endl;
}
int main()
{
std::string checks[][5] = {
string checks[][5] = {
//name;[len];type[;[divisor|values][;[unit][;[comment]]]], decoded value, master, slave, flags
{"x;;ign:10", "", "10fe07000a00000000000000000000", "00", ""},
{"x;;str:10", "Hallo, Du!", "10fe07000a48616c6c6f2c20447521", "00", ""},
@@ -180,60 +181,59 @@ int main()
{"x;;trelrel","18.004;19.008","10fe07000401120213", "00", ""}, // reference to template struct
{"x;;temp;;;;y;;d1c","18.004;9.5","10fe070003011213", "00", ""}, // reference to template, normal def
};
std::map<std::string, DataField*> templates;
map<string, DataField*> templates;
DataField* fields = NULL;
for (size_t i = 0; i < sizeof(checks) / sizeof(checks[0]); i++) {
std::string check[5] = checks[i];
std::istringstream isstr(check[0]);
std::string expectStr = check[1];
string check[5] = checks[i];
istringstream isstr(check[0]);
string expectStr = check[1];
SymbolString mstr = SymbolString(check[2], false);
SymbolString sstr = SymbolString(check[3], false);
std::string flags = check[4];
bool isSet = flags.find('s') != std::string::npos;
bool failedCreate = flags.find('c') != std::string::npos;
bool failedRead = flags.find('r') != std::string::npos;
bool failedReadMatch = flags.find('R') != std::string::npos;
bool failedWrite = flags.find('w') != std::string::npos;
bool failedWriteMatch = flags.find('W') != std::string::npos;
bool verbose = flags.find('v') != std::string::npos;
bool isTemplate = flags.find('t') != std::string::npos;
std::string item;
std::vector<std::string> entries;
string flags = check[4];
bool isSet = flags.find('s') != string::npos;
bool failedCreate = flags.find('c') != string::npos;
bool failedRead = flags.find('r') != string::npos;
bool failedReadMatch = flags.find('R') != string::npos;
bool failedWrite = flags.find('w') != string::npos;
bool failedWriteMatch = flags.find('W') != string::npos;
bool verbose = flags.find('v') != string::npos;
bool isTemplate = flags.find('t') != string::npos;
string item;
vector<string> entries;
while (std::getline(isstr, item, ';') != 0)
while (getline(isstr, item, ';') != 0)
entries.push_back(item);
if (fields != NULL) {
delete fields;
fields = NULL;
}
std::vector<std::string>::iterator it = entries.begin();
vector<string>::iterator it = entries.begin();
result_t result = DataField::create(it, entries.end(), templates, fields, isSet, isTemplate ? SYN : mstr[1]);
if (failedCreate == true) {
if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << std::endl;
cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << endl;
else
std::cout << "\"" << check[0] << "\": failed create OK" << std::endl;
cout << "\"" << check[0] << "\": failed create OK" << endl;
continue;
}
if (result != RESULT_OK) {
std::cout << "\"" << check[0] << "\": create error: "
<< getResultCode(result) << std::endl;
cout << "\"" << check[0] << "\": create error: " << getResultCode(result) << endl;
continue;
}
if (fields == NULL) {
std::cout << "\"" << check[0] << "\": create error: NULL" << std::endl;
cout << "\"" << check[0] << "\": create error: NULL" << endl;
continue;
}
if (it != entries.end()) {
std::cout << "\"" << check[0] << "\": create error: trailing input" << std::endl;
cout << "\"" << check[0] << "\": create error: trailing input" << endl;
continue;
}
std::cout << "\"" << check[0] << "\": create OK" << std::endl;
cout << "\"" << check[0] << "\": create OK" << endl;
if (isTemplate) {
// store new template
std::string name = fields->getName();
std::map<std::string, DataField*>::iterator current = templates.find(name);
string name = fields->getName();
map<string, DataField*>::iterator current = templates.find(name);
if (current == templates.end()) {
templates[name] = fields;
} else {
@@ -244,20 +244,20 @@ int main()
continue;
}
std::ostringstream output;
ostringstream output;
SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false);
SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false);
result = fields->read(mstr, 0, sstr, 0, output, verbose);
if (failedRead == true)
if (result == RESULT_OK)
std::cout << " failed read " << fields->getName() << " >"
<< check[2] << "< error: unexpectedly succeeded" << std::endl;
cout << " failed read " << fields->getName() << " >"
<< check[2] << "< error: unexpectedly succeeded" << endl;
else
std::cout << " failed read " << fields->getName() << " >"
<< check[2] << "< OK" << std::endl;
cout << " failed read " << fields->getName() << " >"
<< check[2] << "< OK" << endl;
else if (result != RESULT_OK) {
std::cout << " read " << fields->getName() << " >" << check[2] << "< error: "
<< getResultCode(result) << std::endl;
cout << " read " << fields->getName() << " >" << check[2]
<< "< error: " << getResultCode(result) << endl;
}
else {
bool match = strcasecmp(output.str().c_str(), expectStr.c_str()) == 0;
@@ -265,19 +265,19 @@ int main()
}
if (verbose == false) {
std::istringstream input(expectStr);
istringstream input(expectStr);
result = fields->write(input, writeMstr, 0, writeSstr, 0);
if (failedWrite == true) {
if (result == RESULT_OK)
std::cout << " failed write " << fields->getName() << " >"
<< expectStr << "< error: unexpectedly succeeded" << std::endl;
cout << " failed write " << fields->getName() << " >"
<< expectStr << "< error: unexpectedly succeeded" << endl;
else
std::cout << " failed write " << fields->getName() << " >"
<< expectStr << "< OK" << std::endl;
cout << " failed write " << fields->getName() << " >"
<< expectStr << "< OK" << endl;
}
else if (result != RESULT_OK) {
std::cout << " write " << fields->getName() << " >"
<< expectStr << "< error: " << getResultCode(result) << std::endl;
cout << " write " << fields->getName() << " >" << expectStr
<< "< error: " << getResultCode(result) << endl;
}
else {
bool match = mstr == writeMstr && sstr == writeSstr;
@@ -288,7 +288,7 @@ int main()
fields = NULL;
}
for (std::map<std::string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
for (map<string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
delete it->second;
return 0;
+43 -42
View File
@@ -21,27 +21,28 @@
#include <iostream>
#include <iomanip>
void verify(bool expectFailMatch, std::string type, std::string input,
bool match, std::string expectStr, std::string gotStr)
using namespace std;
void verify(bool expectFailMatch, string type, string input,
bool match, string expectStr, string gotStr)
{
if (expectFailMatch == true) {
if (match == true)
std::cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << std::endl;
cout << " failed " << type << " match >" << input
<< "< error: unexpectedly succeeded" << endl;
else
std::cout << " failed " << type << " match >" << input << "< OK"
<< std::endl;
cout << " failed " << type << " match >" << input << "< OK" << endl;
}
else if (match == true)
std::cout << " " << type << " match >" << input << "< OK" << std::endl;
cout << " " << type << " match >" << input << "< OK" << endl;
else
std::cout << " " << type << " match >" << input << "< error: got >" << gotStr
<< "<, expected >" << expectStr << "<" << std::endl;
cout << " " << type << " match >" << input << "< error: got >"
<< gotStr << "<, expected >" << expectStr << "<" << endl;
}
void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std::string>::iterator end, std::vector<std::string>::iterator pos)
void printErrorPos(vector<string>::iterator it, const vector<string>::iterator end, vector<string>::iterator pos)
{
std::cout << "Errroneous item is here:" << std::endl;
cout << "Errroneous item is here:" << endl;
bool first = true;
int cnt = 0;
if (pos > it)
@@ -50,7 +51,7 @@ void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std:
if (first == true)
first = false;
else {
std::cout << ';';
cout << ';';
if (it <= pos) {
cnt++;
}
@@ -58,86 +59,86 @@ void printErrorPos(std::vector<std::string>::iterator it, const std::vector<std:
if (it < pos) {
cnt += (*it).length();
}
std::cout << (*it++);
cout << (*it++);
}
std::cout << std::endl;
std::cout << std::setw(cnt) << " " << std::setw(0) << "^" << std::endl;
cout << endl;
cout << setw(cnt) << " " << setw(0) << "^" << endl;
}
int main()
{
// message= [type];class;name;[comment];[QQ];ZZ;PBSB;fields...
// field= name;[pos];type[;[divisor|values][;[unit][;[comment]]]]
std::string checks[][5] = {
string checks[][5] = {
// "message", "flags"
{";;first;;;fe;0700;x;;bda", "26.10.2014", "fffe0700042610001451", "00", ""},
{"w;;first;;;15;b5090400;date;;bda", "26.10.2014", "ff15b5090604002610001445", "00", ""},
};
std::map<std::string, DataField*> templates;
map<string, DataField*> templates;
Message* message = NULL;
for (size_t i = 0; i < sizeof(checks) / sizeof(checks[0]); i++) {
std::string check[5] = checks[i];
std::istringstream isstr(check[0]);
std::string inputStr = check[1];
string check[5] = checks[i];
istringstream isstr(check[0]);
string inputStr = check[1];
SymbolString mstr = SymbolString(check[2], false);
SymbolString sstr = SymbolString(check[3], false);
std::string flags = check[4];
bool failedCreate = flags.find('c') != std::string::npos;
bool failedPrepare = flags.find('p') != std::string::npos;
bool failedPrepareMatch = flags.find('P') != std::string::npos;
std::string item;
std::vector<std::string> entries;
string flags = check[4];
bool failedCreate = flags.find('c') != string::npos;
bool failedPrepare = flags.find('p') != string::npos;
bool failedPrepareMatch = flags.find('P') != string::npos;
string item;
vector<string> entries;
while (std::getline(isstr, item, ';') != 0)
while (getline(isstr, item, ';') != 0)
entries.push_back(item);
if (message != NULL) {
delete message;
message = NULL;
}
std::vector<std::string>::iterator it = entries.begin();
vector<string>::iterator it = entries.begin();
result_t result = Message::create(it, entries.end(), templates, message);
if (failedCreate == true) {
if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << std::endl;
cout << "\"" << check[0] << "\": failed create error: unexpectedly succeeded" << endl;
else
std::cout << "\"" << check[0] << "\": failed create OK" << std::endl;
cout << "\"" << check[0] << "\": failed create OK" << endl;
continue;
}
if (result != RESULT_OK) {
std::cout << "\"" << check[0] << "\": create error: "
<< getResultCode(result) << std::endl;
cout << "\"" << check[0] << "\": create error: "
<< getResultCode(result) << endl;
printErrorPos(entries.begin(), entries.end(), it);
continue;
}
if (message == NULL) {
std::cout << "\"" << check[0] << "\": create error: NULL" << std::endl;
cout << "\"" << check[0] << "\": create error: NULL" << endl;
continue;
}
if (it != entries.end()) {
std::cout << "\"" << check[0] << "\": create error: trailing input" << std::endl;
cout << "\"" << check[0] << "\": create error: trailing input" << endl;
continue;
}
std::cout << "\"" << check[0] << "\": create OK" << std::endl;
cout << "\"" << check[0] << "\": create OK" << endl;
std::istringstream input(inputStr);
istringstream input(inputStr);
SymbolString writeMstr = SymbolString();
result = message->prepare(0xff, writeMstr, input);
if (failedPrepare == true) {
if (result == RESULT_OK)
std::cout << "\"" << check[0] << "\": failed prepare error: unexpectedly succeeded" << std::endl;
cout << "\"" << check[0] << "\": failed prepare error: unexpectedly succeeded" << endl;
else
std::cout << "\"" << check[0] << "\": failed prepare OK" << std::endl;
cout << "\"" << check[0] << "\": failed prepare OK" << endl;
continue;
}
if (result != RESULT_OK) {
std::cout << " prepare >" << inputStr << "< error: "
<< getResultCode(result) << std::endl;
cout << " prepare >" << inputStr << "< error: "
<< getResultCode(result) << endl;
continue;
}
std::cout << " prepare >" << inputStr << "< OK" << std::endl;
cout << " prepare >" << inputStr << "< OK" << endl;
bool match = writeMstr==mstr;
verify(failedPrepareMatch, "prepare", inputStr, match, mstr.getDataStr(), writeMstr.getDataStr());
@@ -146,7 +147,7 @@ int main()
message = NULL;
}
for (std::map<std::string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
for (map<string, DataField*>::iterator it = templates.begin(); it != templates.end(); it++)
delete it->second;
return 0;
+14 -13
View File
@@ -21,6 +21,8 @@
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
SymbolString sstr = SymbolString("10feb5050427a915aa");
@@ -30,29 +32,28 @@ int main ()
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "ctor escaped successful." << std::endl;
else
std::cout << "ctor escaped invalid: got " << gotStr
<< ", expected " << expectStr << std::endl;
std::cout << "ctor escaped invalid: got " << gotStr << ", expected "
<< expectStr << std::endl;
unsigned char gotCrc = sstr.getCRC(), expectCrc = 0x77;
if (gotCrc == expectCrc)
std::cout << "CRC successful." << std::endl;
else
std::cout << "CRC invalid: got 0x"
<< std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(gotCrc)
<< ", expected 0x"
<< std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(expectCrc)
<< std::endl;
std::cout << "CRC invalid: got 0x" << std::nouppercase << std::setw(2)
<< std::hex << std::setfill('0')
<< static_cast<unsigned>(gotCrc) << ", expected 0x"
<< std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(expectCrc)
<< std::endl;
gotStr = sstr.getDataStr(), expectStr = "10feb5050427a915aa77";
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "unescape successful." << std::endl;
else
std::cout << "unescape invalid: got " << gotStr
<< ", expected " << expectStr << std::endl;
std::cout << "unescape invalid: got " << gotStr << ", expected "
<< expectStr << std::endl;
sstr = SymbolString("10feb5050427a90015a90177", true);
@@ -61,8 +62,8 @@ int main ()
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "ctor unescaped successful." << std::endl;
else
std::cout << "ctor unescaped invalid: got " << gotStr
<< ", expected " << expectStr << std::endl;
std::cout << "ctor unescaped invalid: got " << gotStr << ", expected "
<< expectStr << std::endl;
return 0;