added list values, use list of predefined data types instead of coding each one separately

This commit is contained in:
john30
2014-11-02 22:13:51 +01:00
parent cb695ca132
commit b5b5a49c4e
3 changed files with 528 additions and 415 deletions
Regular → Executable
+355 -331
View File
@@ -30,8 +30,34 @@
namespace libebus namespace libebus
{ {
/** the known data field types. */
static const dataType_t dataTypes[] = {
{"STR",16, bt_str, ADJ,' ', 1, 16, 0}, // >= 1 byte character string filled up with space
{"HEX",16, bt_hexstr,ADJ, 0, 2, 47, 0}, // >= 1 byte hex digit string, usually separated by space, e.g. 0a 1b 2c 3d
{"BDA", 4, bt_date, BCD, 0, 10, 10, 0}, // date in BCD, 01.01.2000 - 31.12.2099 (0x01,0x01,WW,0x00 - 0x31,0x12,WW,0x99, WW is ignored weekday)
{"BDA", 3, bt_date, BCD, 0, 10, 10, 0}, // date in BCD, 01.01.2000 - 31.12.2099 (0x01,0x01,0x00 - 0x31,0x12,0x99)
//{"HDA", 3, bt_date, 0, 0, 10, 10, 0}, // date, 01.01.2000 - 31.12.2099 (0x01,0x01,0x00 - 0x31,0x12,0x99)
//{"HDA", 4, bt_date, 0, 0, 10, 10, 0}, // date, 01.01.2000 - 31.12.2099 (0x0101WW00 - 0x3112WW99, WW is ignored weekday)
{"BTI", 3, bt_time,BCD|REV,0, 8, 8, 0}, // time in BCD, 00:00:00 - 23:59:59 (0x00,0x00,0x00 - 0x59,0x59,0x23)
{"TTM", 1, bt_time, 0, 0, 5, 5, 0}, // truncated time (only multiple of 10 minutes), 00:00 - 24:00 (minutes div 10 + hour * 6 as integer)
{"BDY", 1, bt_list,BCD|DAY,0, 0, 6, 0}, // weekday, "Mon" - "Sun"
{"HDY", 1, bt_list,BCD|DAY,0, 1, 7, 0}, // weekday, "Mon" - "Sun" // TODO different range
{"BCD", 1, bt_number,BCD|LST,0xff, 0, 0x99, 1}, // unsigned decimal in BCD, 0 - 99
{"UCH", 1, bt_number, LST, 0xff, 0, 0xff, 1}, // unsigned integer, 0 - 255
{"SCH", 1, bt_number, SIG, 0x80, 0x80, 0x7f, 1}, // signed integer, -128 - +127
{"D1B", 1, bt_number, SIG, 0x80, 0x81, 0x7f, 1}, // signed integer, -127 - +127
{"D1C", 1, bt_number, 0, 0xff, 0x00, 0xc8, 2}, // unsigned number (fraction 1/2), 0 - 100 (0x00 - 0xc8, replacement 0xff)
{"UIN", 2, bt_number, LST, 0xffff, 0, 0xffff, 1}, // unsigned integer, 0 - 65535
{"SIN", 2, bt_number, SIG, 0x8000, 0x8000, 0x7fff, 1}, // signed integer, -32768 - +32767
{"FLT", 2, bt_number, SIG, 0x8000, 0x8000, 0x7fff, 1000}, // signed number (fraction 1/1000), -32.768 - +32.767
{"D2B", 2, bt_number, SIG, 0x8000, 0x8001, 0x7fff, 256}, // signed number (fraction 1/256), -127.99 - +127.99
{"D2C", 2, bt_number, SIG, 0x8000, 0x8001, 0x7fff, 16}, // signed number (fraction 1/16), -2047.9 - +2047.9
{"ULG", 4, bt_number, LST, 0xffffffff, 0, 0xffffffff, 1}, // unsigned integer, 0 - 4294967295
{"SLG", 4, bt_number, SIG, 0x80000000, 0x80000000, 0xffffffff, 1}, // signed integer, -2147483648 - +2147483647
};
static const char* days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}; /** the week day names. */
static const char* dayNames[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
DataField* DataField::create(const unsigned char dstAddress, const bool isSetMessage, DataField* DataField::create(const unsigned char dstAddress, const bool isSetMessage,
@@ -44,60 +70,49 @@ DataField* DataField::create(const unsigned char dstAddress, const bool isSetMes
if (it == end) if (it == end)
return NULL; return NULL;
name = *it++; name = *it++;
if (it == end) if (it == end || name.size() == 0)
return NULL; return NULL;
const char* posStr = (*it++).c_str(); const char* posStr = (*it++).c_str();
if (it == end) if (it == end)
return NULL; return NULL;
if (posStr[0] >= 'a' && posStr[1] == 0) { if (dstAddress == BROADCAST
if (posStr[0] == 's') { // slave ACK || isMaster(dstAddress)
// QQ ZZ PB SB NN + Dx + CRC || (isSetMessage == true && posStr[0] <= '9')
//offset = 5+command.getMasterDataLength()+1; // skip QQ ZZ PB SB NN Dx CRC || posStr[0] == 'm') { // master data
} else if (posStr[0] == 'm') { // master ACK partType = pt_masterData;
//offset = 5+command.getMasterDataLength()+3+command.getSlaveDataLength()+1; // skip QQ ZZ PB SB NN Dx CRC ACK NN Dx CRC baseOffset = 5; // skip QQ ZZ PB SB NN
} else { //len = command.getMasterDataLength();
return NULL; // TODO error code: invalid pos definition if (posStr[0] == 'm')
} posStr++;
} else if ((isSetMessage == false && posStr[0] <= '9')
|| posStr[0] == 's') { // slave data
baseOffset = 1;
//offset = 5+command.getMasterDataLength()+3; // skip QQ ZZ PB SB NN Dx CRC ACK NN
//len = command.getSlaveDataLength();
if (posStr[0] == 's')
posStr++;
} else { } else {
if (dstAddress == BROADCAST return NULL; // TODO error code: invalid pos definition
|| isMaster(dstAddress) }
|| (isSetMessage == true && posStr[0] <= '9') std::string token;
|| posStr[0] == 'm') { // master data std::istringstream stream(posStr);
partType = pt_masterData; while (std::getline(stream, token, '-') != 0) {
baseOffset = 5; // skip QQ ZZ PB SB NN if (++offsetCnt > 2)
//len = command.getMasterDataLength();
if (posStr[0] == 'm')
posStr++;
} else if ((isSetMessage == false && posStr[0] <= '9')
|| posStr[0] == 's') { // slave data
baseOffset = 1;
//offset = 5+command.getMasterDataLength()+3; // skip QQ ZZ PB SB NN Dx CRC ACK NN
//len = command.getSlaveDataLength();
if (posStr[0] == 's')
posStr++;
} else {
return NULL; // TODO error code: invalid pos definition return NULL; // TODO error code: invalid pos definition
} const char* start = token.c_str();
std::string token; char* end = NULL;
std::istringstream stream(posStr); unsigned int pos = strtoul(start, &end, 10)-1; // 1-based
while (std::getline(stream, token, '-') != 0) { if (end != start+strlen(start))
if (++offsetCnt > 2) return NULL; // TODO error code: invalid pos definition
return NULL; // TODO error code: invalid pos definition if (baseOffset+pos > maxPos)
const char* start = token.c_str(); return NULL; // TODO error code: invalid pos definition
char* end = NULL; else if (offsetCnt==1)
int pos = strtoul(start, &end, 10)-1; // 1-based offset = baseOffset+pos;
if (end != start+strlen(start)) else if (baseOffset+pos >= offset)
return NULL; // TODO error code: invalid pos definition length = baseOffset+pos+1-offset;
if (pos < 0 || baseOffset+pos > maxPos) else { // wrong order e.g. 4-3
return NULL; // TODO error code: invalid pos definition length = offset-(baseOffset+pos+1);
else if (offsetCnt==1) offset = baseOffset+pos;
offset = baseOffset+pos;
else if (baseOffset+pos >= offset)
length = baseOffset+pos+1-offset;
else { // wrong order e.g. 4-3
length = offset-(baseOffset+pos+1);
offset = baseOffset+pos;
}
} }
} }
@@ -130,104 +145,66 @@ DataField* DataField::create(const unsigned char dstAddress, const bool isSetMes
comment.clear(); comment.clear();
} }
if (strcasecmp(typeStr, "STR") == 0) { std::map<unsigned int, std::string> values;
if (length == 0) if (it != end) {
length = 1; std::istringstream stream(*it++);
return new StringDataField(name, partType, offset, length, dt_string, unit, comment); while (std::getline(stream, token, ',') != 0) {
const char* start = token.c_str();
char* end = NULL;
unsigned int id = strtoul(start, &end, 10)-1; // 1-based
if (end == NULL || end == start || *end != '=')
return NULL; // TODO error code: invalid values definition
values[id] = std::string(end+1);
}
} }
if (strcasecmp(typeStr, "HEX") == 0) {
if (length == 0) for (size_t i = 0; i < sizeof(dataTypes)/sizeof(dataTypes[0]); i++) {
length = 1; dataType_t dataType = dataTypes[i];
return new StringDataField(name, partType, offset, length, dt_hex, unit, comment); if (strcasecmp(typeStr, dataType.name) == 0) {
} if ((dataType.flags&ADJ) != 0) {
if (strcasecmp(typeStr, "UCH") == 0) { if (length == 0)
if (length != 0 && length != 1) length = 1; // minimum length defaults to 1
return NULL; // TODO error code: invalid pos definition else if (length > dataType.numBytes)
return new NumericDataField(name, partType, offset, 1, dt_uchar, unit, comment, factor, 0xff); return NULL; // invalid length
} }
if (strcasecmp(typeStr, "SCH") == 0) { else if (length == 0)
if (length != 0 && length != 1) length = dataType.numBytes;
return NULL; // TODO error code: invalid pos definition else if (length != dataType.numBytes)
return new NumericDataField(name, partType, offset, 1, dt_schar, unit, comment, factor, 0x80); continue; // check for another one with same name but different length
}
if (strcasecmp(typeStr, "BCD") == 0) { switch (dataType.type) {
if (length != 0 && length != 1) case bt_str:
return NULL; // TODO error code: invalid pos definition case bt_hexstr:
return new NumericDataField(name, partType, offset, 1, dt_bcd, unit, comment, factor, 0xff); // TODO max value 99 case bt_date: // TODO better numeric?
} case bt_time: // TODO better numeric?
if (strcasecmp(typeStr, "D1B") == 0) { return new StringDataField(name, partType, offset, length, dataType, unit, comment);
if (length != 0 && length != 1) case bt_list:
return NULL; // TODO error code: invalid pos definition if (values.empty() == false) {
return new NumericDataField(name, partType, offset, 1, dt_d1b, unit, comment, factor, 0x80); if (values.begin()->first < dataType.minValueOrLength)
} return NULL; // invalid value id
if (strcasecmp(typeStr, "D1C") == 0) { std::map<unsigned int, std::string>::iterator end = values.end();
if (length != 0 && length != 1) end--;
return NULL; // TODO error code: invalid pos definition if (end->first > dataType.maxValueOrLength)
return new NumericDataField(name, partType, offset, 1, dt_d1c, unit, comment, factor*0.5, 0xff); // TODO max value 100 return NULL; // invalid value id
} }
if (strcasecmp(typeStr, "UIN") == 0) { else if ((dataType.flags&DAY) != 0) {
if (length != 0 && length != 2) for (unsigned int i=0; i<sizeof(dayNames)/sizeof(dayNames[0]); i++)
return NULL; // TODO error code: invalid pos definition values[dataType.minValueOrLength + i] = dayNames[i];
return new NumericDataField(name, partType, offset, 2, dt_uint, unit, comment, factor, 0xffff); }
} return new ValueListDataField(name, partType, offset, length, dataType, unit, comment, values);
if (strcasecmp(typeStr, "SIN") == 0) { case bt_number:
if (length != 0 && length != 2) if (values.empty() || (dataType.flags&LST) == 0)
return NULL; // TODO error code: invalid pos definition return new NumberDataField(name, partType, offset, length, dataType, unit, comment, factor);
return new NumericDataField(name, partType, offset, 2, dt_sint, unit, comment, factor, 0x8000); return new ValueListDataField(name, partType, offset, length, dataType, unit, comment, values);
} }
if (strcasecmp(typeStr, "D2B") == 0) { }
if (length != 0 && length != 2)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 2, dt_d2b, unit, comment, factor/256.0, 0x8000);
}
if (strcasecmp(typeStr, "D2C") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 2, dt_d2c, unit, comment, factor/16.0, 0x8000);
}
if (strcasecmp(typeStr, "ULG") == 0) {
if (length != 0 && length != 4)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 4, dt_ulong, unit, comment, factor, 0xffffffff);
}
if (strcasecmp(typeStr, "SLG") == 0) {
if (length != 0 && length != 4)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 4, dt_slong, unit, comment, factor, 0x80000000);
}
if (strcasecmp(typeStr, "FLT") == 0) {
if (length != 0 && length != 2)
return NULL; // TODO error code: invalid pos definition
return new NumericDataField(name, partType, offset, 2, dt_float, unit, comment, factor/1000.0, 0x8000); // TODO replacement
}
if (strcasecmp(typeStr, "BDA") == 0 || strcasecmp(typeStr, "HDA") == 0) {
if (length == 0)
length = 4;
else if (length != 3 && length != 4)
return NULL; // TODO error code: invalid pos definition
return new StringDataField(name, partType, offset, length, dt_date, unit, comment); // TODO better numeric?
}
if (strcasecmp(typeStr, "BDY") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new StringDataField(name, partType, offset, 1, dt_day, unit, comment); // TODO better numeric?
}
if (strcasecmp(typeStr, "BTI") == 0 || strcasecmp(typeStr, "HTI") == 0) {
if (length != 0 && length != 3)
return NULL; // TODO error code: invalid pos definition
return new StringDataField(name, partType, offset, 3, dt_time, unit, comment); // TODO better numeric?
}
if (strcasecmp(typeStr, "TTM") == 0) {
if (length != 0 && length != 1)
return NULL; // TODO error code: invalid pos definition
return new StringDataField(name, partType, offset, 1, dt_tTime, unit, comment); // TODO better numeric?
} }
return NULL; // TODO error code: invalid type definition return NULL; // TODO error code: invalid type definition
} }
const std::string DataField::parseSymbols(SymbolString& masterData, SymbolString& slaveData, bool verbose) const std::string DataField::read(SymbolString& masterData, SymbolString& slaveData, bool verbose)
{ {
SymbolString& data = m_partType == pt_masterData ? masterData : slaveData; SymbolString& input = m_partType == pt_masterData ? masterData : slaveData;
switch (m_partType) { switch (m_partType) {
case pt_masterData: case pt_masterData:
break; break;
@@ -240,7 +217,7 @@ const std::string DataField::parseSymbols(SymbolString& masterData, SymbolString
if (verbose) if (verbose)
output << m_name << "="; output << m_name << "=";
if (parse(data, output) == false) if (readSymbols(input, output) == false)
return "unable to parse"; return "unable to parse";
if (verbose && m_unit.length() > 0) if (verbose && m_unit.length() > 0)
@@ -250,82 +227,80 @@ const std::string DataField::parseSymbols(SymbolString& masterData, SymbolString
return output.str(); return output.str();
} }
bool DataField::formatSymbols(const std::string& value, SymbolString& masterData, SymbolString& slaveData) bool DataField::write(const std::string& value, SymbolString& masterData, SymbolString& slaveData)
{ {
SymbolString& data = m_partType == pt_masterData ? masterData : slaveData; SymbolString& output = m_partType == pt_masterData ? masterData : slaveData;
switch (m_partType) { switch (m_partType) {
case pt_masterData: case pt_masterData:
break;
case pt_slaveData: case pt_slaveData:
break; break;
default: default:
return false; // TODO error code return false; // TODO error code
} }
std::istringstream input(value); std::istringstream input(value);
if (format(data, input) == false) if (writeSymbols(input, output) == false)
return false; // TODO error code return false; // TODO error code
return true; return true;
} }
bool StringDataField::parse(SymbolString& data, std::ostringstream& output) bool StringDataField::readSymbols(SymbolString& input, std::ostringstream& output)
{ {
size_t start = m_offset, end = m_offset+m_length; size_t start = m_offset, end = m_offset + m_length;
int incr = 1;
unsigned char ch; unsigned char ch;
if (end > data.size()) { if (end > input.size())
return false; // TODO error not enough data available return false; // TODO error not enough data available
}
if (m_dataType == dt_time) { // reverse order if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first)
for (size_t i = end-1; i >= start; i--) { end = start - 1;
ch = data[i]; start = m_offset + m_length - 1;
if ((ch&0xf0) > 0x90 || (ch&0x0f) > 0x09) incr = -1;
return false; // invalid BCD
if ((i == end-1 && ch > 0x23) || (i != end-1 && ch > 0x59))
return false; // invalid time
if (i != end-1)
output << ":";
output << std::setw(2) << std::setfill('0') << std::hex << static_cast<unsigned>(ch);
}
return true;
} }
for (size_t i = start; i < end; i++) { for (size_t pos = start, i = 0; pos != end; pos += incr, i++) {
ch = data[i]; if (m_length == 4 && i == 2 && m_dataType.type == bt_date)
switch (m_dataType) { continue; // skip weekday in between
case dt_hex: ch = input[pos];
if (i != start) if ((m_dataType.flags & BCD) != 0) {
output << " "; if ((ch & 0xf0) > 0x90 || (ch & 0x0f) > 0x09)
return false; // invalid BCD
ch = (ch >> 4) * 10 + (ch & 0x0f);
}
switch (m_dataType.type) {
case bt_hexstr:
if (i > 0)
output << ' ';
output << std::nouppercase << std::setw(2) << std::hex output << std::nouppercase << std::setw(2) << std::hex
<< std::setfill('0') << static_cast<unsigned>(ch); << std::setfill('0') << static_cast<unsigned>(ch);
break; break;
case dt_date: case bt_date:
if (m_length == 4u && i == m_offset+2u) if (i + 1 == m_length)
break; // skip weekday in between output << (2000+ch);
if ((ch&0xf0) > 0x90 || (ch&0x0f) > 0x09) else if (ch < 1 || (i == 0 && ch > 31) || (i == 1 && ch > 12))
return false; // invalid BCD
if (i == end-1)
output << std::hex << (0x2000+ch);
else if (ch < 0x01 || (i == start && ch > 0x31) || (i == start+1 && ch > 0x12))
return false; // invalid date return false; // invalid date
else else
output << std::setw(2) << std::setfill('0') << std::hex << static_cast<unsigned>(ch) << "."; output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch) << ".";
break; break;
case dt_day: case bt_time:
if (ch < 1 || ch > 7) if (m_length == 1) { // truncated time
return false; // invalid day if (ch > 24*6)
output << days[ch-1]; return false; // invalid time
break; output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch/6) << ":"
case dt_tTime: << std::setw(2) << std::setfill('0') << static_cast<unsigned>((ch%6)*10);
if (ch/6 > 23) break;
}
if (i > 0)
output << ":";
if ((i == 0 && ch > 23) || (i > 0 && ch > 59))
return false; // invalid time return false; // invalid time
output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch/6) << ":" output << std::setw(2) << std::setfill('0') << static_cast<unsigned>(ch);
<< std::setw(2) << std::setfill('0') << static_cast<unsigned>((ch%6)*10);
break; break;
default: default:
if (ch < 0x20) if (ch < 0x20)
ch = 0x20; ch = m_dataType.replacement;
output << static_cast<char>(ch); output << static_cast<char>(ch);
break; break;
} }
@@ -334,36 +309,24 @@ bool StringDataField::parse(SymbolString& data, std::ostringstream& output)
return true; return true;
} }
bool StringDataField::format(SymbolString& data, std::istringstream& input) bool StringDataField::writeSymbols(std::istringstream& input, SymbolString& output)
{ {
size_t start = m_offset, end = m_offset+m_length; size_t start = m_offset, end = m_offset + m_length;
int incr = 1;
const char* str; const char* str;
char* strEnd; char* strEnd;
unsigned long int value, hours; unsigned long int value = 0, minutes = 0;
unsigned char ch;
std::string token; std::string token;
if (m_dataType == dt_time) { // reverse order
for (size_t i = end-1; i >=start; i--) { if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first)
if (std::getline(input, token, ':') == 0) end = start - 1;
return false; start = m_offset + m_length - 1;
str = token.c_str(); incr = -1;
strEnd = NULL;
value = strtoul(str, &strEnd, 16);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
if ((value&0xf0) > 0x90 || (value&0x0f) > 0x09)
return false; // invalid BCD
if ((i == end-1 && value > 0x23) || (i != end-1 && value > 0x59))
return false; // invalid time
data[i] = (unsigned char)value;
}
return true;
} }
for (size_t i = start; i < end; i++) { for (size_t pos = start, i = 0; pos != end; pos += incr, i++) {
switch (m_dataType) { switch (m_dataType.type) {
case dt_hex: case bt_hexstr:
while (input.peek()==' ') while (input.peek()==' ')
input.get(); input.get();
token.clear(); token.clear();
@@ -379,50 +342,23 @@ bool StringDataField::format(SymbolString& data, std::istringstream& input)
value = strtoul(str, &strEnd, 16); value = strtoul(str, &strEnd, 16);
if (strEnd != str+strlen(str)) if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value return false; // TODO error code: invalid value
data[i] = (unsigned char)value;
break; break;
case dt_date: case bt_date:
if (m_length == 4u && i == m_offset+2u) if (m_length == 4 && i == 2)
break; // skip weekday in between continue; // skip weekday in between
if (std::getline(input, token, '.') == 0) if (std::getline(input, token, '.') == 0)
return false; return false;
str = token.c_str(); str = token.c_str();
strEnd = NULL; strEnd = NULL;
value = strtoul(str, &strEnd, 16); value = strtoul(str, &strEnd, 10);
if (strEnd != str+strlen(str)) if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value return false; // TODO error code: invalid value
if ((value&0xf0) > 0x90 || (value&0x0f) > 0x09) if (i + 1 == m_length && value >= 2000)
return false; // invalid BCD value -= 2000;
if (i == end-1) { else if (value < 1 || (i == 0 && value > 31) || (i == 1 && value > 12))
if (value>=0x2000)
value -= 0x2000;
if (value>=0x100)
return false; // invalid year
}
else if (value < 1 || (i == start && value > 0x31) || (i == start+1 && value > 0x12))
return false; // invalid date return false; // invalid date
data[i] = (unsigned char)value;
break; break;
case dt_day: case bt_time:
str = input.str().c_str();
for (ch = 0; ch < 7; ch++)
if (strcasecmp(days[ch], str) == 0)
break;
if (ch == 7)
return false; // invalid day
data[i] = ch+1;
break;
case dt_tTime:
if (std::getline(input, token, ':') == 0)
return false;
str = token.c_str();
strEnd = NULL;
hours = strtoul(str, &strEnd, 10);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
if (hours > 23)
return false; // invalid time
if (std::getline(input, token, ':') == 0) if (std::getline(input, token, ':') == 0)
return false; return false;
str = token.c_str(); str = token.c_str();
@@ -430,146 +366,234 @@ bool StringDataField::format(SymbolString& data, std::istringstream& input)
value = strtoul(str, &strEnd, 10); value = strtoul(str, &strEnd, 10);
if (strEnd != str+strlen(str)) if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value return false; // TODO error code: invalid value
if (value > 59 || (value%10) != 0) if (m_length == 1) { // truncated time
if (std::getline(input, token, ':') == 0)
return false;
str = token.c_str();
strEnd = NULL;
minutes = strtoul(str, &strEnd, 10);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value
if ((minutes % 10) != 0)
return false; // invalid time
value = value*6 + (minutes / 10);
if (value > 24*6)
return false; // invalid time
break;
}
if ((i == 0 && value > 23) || (i > 0 && value > 59))
return false; // invalid time return false; // invalid time
data[i] = (unsigned char)(hours*6 + value/10);
break; break;
default: default:
ch = input.get(); value = input.get();
if (input.eof() == true || ch < 0x20) if (input.eof() == true || value < 0x20)
ch = 0x20; value = m_dataType.replacement;
data[i] = ch;
break; break;
} }
if ((m_dataType.flags & BCD) != 0) {
if (value > 99)
return false; // invalid BCD
value = (value/10)<<4 | (value%10);
}
if (value > 0xff)
return false;
output[pos] = (unsigned char)value;
} }
return true; return true;
} }
bool NumericDataField::parse(SymbolString& data, std::ostringstream& output) bool NumericDataField::readRawValue(SymbolString& input, unsigned int& value)
{ {
size_t start = m_offset, end = m_offset+m_length; size_t start = m_offset, end = m_offset + m_length;
unsigned int value = 0; int incr = 1;
int signedValue;
unsigned char ch; unsigned char ch;
if (end > data.size()) if (end > input.size())
return false; // TODO error not enough data available return false; // TODO error not enough data available
for (size_t i = start, exp = 1; i < end; i++) { if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first)
ch = data[i]; end = start - 1;
switch (m_dataType) { start = m_offset + m_length - 1;
case dt_bcd: incr = -1;
if (ch == m_replacement) { }
output << "-";
value = 0;
for (size_t pos = start, exp = 1; pos != end; pos += incr) {
ch = input[pos];
if ((m_dataType.flags & BCD) != 0) {
if (ch == m_dataType.replacement) {
value = m_dataType.replacement;
return true; return true;
} }
else if ((ch&0xf0) > 0x90 || (ch&0x0f) > 0x09) if ((ch & 0xf0) > 0x90 || (ch & 0x0f) > 0x09)
return false; // invalid BCD return false; // invalid BCD
else
value |= ((ch>>4)*10 + (ch&0x0f))*exp; ch = (ch >> 4) * 10 + (ch & 0x0f);
value += ch*exp;
exp = exp*100; exp = exp*100;
break; }
default: else {
value |= ch*exp; value |= ch*exp;
exp = exp<<8; exp = exp<<8;
break;
} }
} }
if (value == m_replacement) { return true;
}
bool NumericDataField::writeRawValue(unsigned int value, SymbolString& output)
{
size_t start = m_offset, end = m_offset + m_length;
int incr = 1;
unsigned char ch;
if ((m_dataType.flags&REV) != 0) { // reverted binary representation (most significant byte first)
end = start - 1;
start = m_offset + m_length - 1;
incr = -1;
}
for (size_t pos = start, exp = 1; pos != end; pos += incr) {
if ((m_dataType.flags & BCD) != 0) {
if (value == m_dataType.replacement)
ch = m_dataType.replacement;
else {
ch = (value/exp)%100;
ch = ((ch/10)<<4) | (ch%10);
}
exp = exp*100;
}
else {
ch = (value/exp)&0xff;
exp = exp<<8;
}
output[pos] = ch;
}
return true;
}
bool NumberDataField::readSymbols(SymbolString& input, std::ostringstream& output)
{
unsigned int value = 0;
int signedValue;
if (readRawValue(input, value) == false)
return false;
if (value == m_dataType.replacement) {
output << "-"; output << "-";
return true; return true;
} }
switch (m_dataType) { if ((m_dataType.flags&SIG) != 0 && (value & (1 << (m_dataType.numBytes*8 - 1))) != 0) // negative signed value
case dt_schar: if (m_dataType.numBytes == 4)
case dt_d1b: signedValue = (int)value;
signedValue = (char)value;
break;
case dt_d2b:
case dt_d2c:
case dt_sint:
signedValue = (short)value;
break;
case dt_slong:
signedValue = (int)value;
break;
case dt_ulong:
if (m_factor == 1.0)
output << std::setprecision(0) << std::fixed << static_cast<float>(value);
else else
output << std::setprecision(3) << std::fixed << static_cast<float>(value * m_factor); signedValue = (int)value - (1 << (m_dataType.numBytes*8));
return true; else {
default: if (m_dataType.numBytes == 4) {
signedValue = value; if (m_factor == 1.0)
break; output << static_cast<unsigned>(value);
else
output << std::setprecision(3) << std::fixed << static_cast<float>(value * m_factor);
return true;
}
signedValue = (int)value;
} }
if (m_factor == 1.0) if (m_factor == 1.0)
output << std::setprecision(0) << std::fixed << static_cast<float>(signedValue); output << static_cast<int>(signedValue);
else else
output << std::setprecision(3) << std::fixed << static_cast<float>(signedValue * m_factor); output << std::setprecision(3) << std::fixed << static_cast<float>(signedValue * m_factor);
return true; return true;
} }
bool NumericDataField::format(SymbolString& data, std::istringstream& input) bool NumberDataField::writeSymbols(std::istringstream& input, SymbolString& output)
{ {
size_t start = m_offset, end = m_offset+m_length;
unsigned int value; unsigned int value;
unsigned char ch;
const char* str = input.str().c_str(); const char* str = input.str().c_str();
if (strcasecmp(str, "-") == 0) if (strcasecmp(str, "-") == 0)
// replacement value // replacement value
value = m_replacement; value = m_dataType.replacement;
else { else {
char* strEnd = NULL; char* strEnd = NULL;
double dvalue = strtod(str, &strEnd); if (m_factor == 1.0) {
if (strEnd != str+strlen(str)) if ((m_dataType.flags&SIG) != 0) {
return false; // TODO error code: invalid value int signedValue = strtol(str, &strEnd, 10);
dvalue /= m_factor; if (signedValue < 0 && m_dataType.numBytes != 4)
switch (m_dataType) { value = (unsigned int)(signedValue + (1<<(m_dataType.numBytes*8)));
case dt_schar: else
case dt_sint: value = (unsigned int)signedValue;
case dt_slong: }
case dt_d1b: else
case dt_d2b: value = strtoul(str, &strEnd, 10);
case dt_d2c: if (strEnd != str+strlen(str))
if (dvalue < -(1LL<<(8*m_length)) || dvalue >= (1LL<<(8*m_length)))
return false; // TODO error code: invalid value return false; // TODO error code: invalid value
break; }
default: else {
// no special handling char* strEnd = NULL;
if (dvalue < 0.0 || dvalue >= (1LL<<(8*m_length))) double dvalue = strtod(str, &strEnd);
if (strEnd != str+strlen(str))
return false; // TODO error code: invalid value return false; // TODO error code: invalid value
break; dvalue /= m_factor;
} if ((m_dataType.flags&SIG) != 0) {
value = (unsigned int)dvalue; if (dvalue < -(1LL<<(8*m_length)) || dvalue >= (1LL<<(8*m_length)))
} return false; // TODO error code: invalid value
if (dvalue < 0 && m_dataType.numBytes != 4)
for (size_t i = start, exp = 1; i < end; i++) { value = (unsigned int)(dvalue + (1<<(m_dataType.numBytes*8)));
switch (m_dataType) { else
case dt_bcd: value = (unsigned int)dvalue;
if (value == m_replacement) }
ch = m_replacement;
else { else {
ch = (value/exp)%100; if (dvalue < 0.0 || dvalue >= (1LL<<(8*m_length)))
ch = ((ch/10)<<4) | (ch%10); return false; // TODO error code: invalid value
value = (unsigned int)dvalue;
} }
exp = exp*100;
break;
default:
ch = (value/exp)&0xff;
exp = exp<<8;
break;
} }
data[i] = ch;
} }
return writeRawValue(value, output);
}
bool ValueListDataField::readSymbols(SymbolString& input, std::ostringstream& output)
{
unsigned int value = 0;
if (readRawValue(input, value) == false)
return false;
if (value == m_dataType.replacement) {
output << "-";
return true;
}
std::map<unsigned int, std::string>::iterator it = m_values.find(value);
if (it == m_values.end())
return false;
output << it->second;
return true; return true;
} }
bool ValueListDataField::writeSymbols(std::istringstream& input, SymbolString& output)
{
std::string str;
input >> str;
for (std::map<unsigned int, std::string>::iterator it = m_values.begin(); it != m_values.end(); it++)
if (it->second.compare(str) == 0)
return writeRawValue(it->first, output);
return false;
}
} //namespace } //namespace
Regular → Executable
+151 -64
View File
@@ -23,6 +23,7 @@
#include "symbol.h" #include "symbol.h"
#include <string> #include <string>
#include <vector> #include <vector>
#include <map>
namespace libebus namespace libebus
{ {
@@ -30,33 +31,39 @@ namespace libebus
/** the message part in which a data field is stored. */ /** the message part in which a data field is stored. */
enum PartType { enum PartType {
pt_masterData, // stored in master data pt_masterData, // stored in master data
//pt_slaveAck, // stored in slave acknowledge // TODO implement if reasonable pt_slaveData, // stored in slave data
pt_slaveData, // stored in slave data };
//pt_masterAck // stored in master acknowledge // TODO implement if reasonable
};
/** the available data types. */ /** the available base data types. */
enum DataType { enum BaseType {
dt_string, // string of characters with fixed length>0, filled up with space bt_str, // text string in a StringDataField
dt_hex, // string of hex digits with fixed length>0, separated by space bt_hexstr, // hex digit string in a StringDataField
dt_bcd, // length: 1 byte, binary coded number, string format: "%d" bt_date, // date in a StringDataField
dt_uchar, bt_time, // time in a StringDataField
dt_schar, bt_list, // numeric list value in a ValueListDataField
dt_uint, bt_number // number value in a NumberDataField
dt_sint, };
dt_ulong,
dt_slong, /** flags for dataType_t. */
dt_float, const unsigned int ADJ = 0x01; // adjustable length, numBytes is maximum length
dt_d1b, const unsigned int BCD = 0x02; // binary representation is BCD
dt_d1c, const unsigned int REV = 0x04; // reverted binary representation (most significant byte first)
dt_d2b, const unsigned int SIG = 0x08; // signed value
dt_d2c, const unsigned int LST = 0x10; // value list is possible (without applied factor)
dt_date, // length: 4 bytes (with skipped weekday) or 3 bytes (without weekday), string format: "dd.mm.yyyy" const unsigned int DAY = 0x20; // default value list is week days
dt_day, // length: 1 byte, string format: "www" (3 character weekday name)
dt_time, // length: 3 bytes, string format: "hh:mm" /** the structure for defining field types with their properties. */
dt_tTime // length: 1 byte, string format: "hh:mm" typedef struct {
}; const char* name; // field identifier
const unsigned int numBytes; // number of bytes (maximum length if ADJ flag is set)
const BaseType type; // base data type
const unsigned int flags; // flags (e.g. BCD)
const unsigned int replacement; // replacement value (fill-up value for bt_str/bt_hexstr)
const unsigned int minValueOrLength; // minimum binary value (minimum length of string for StringDataField)
const unsigned int maxValueOrLength; // maximum binary value (maximum length of string for StringDataField)
const unsigned int divisor; // divisor for bt_number values (or 0 for non-numeric)
} dataType_t;
/** /**
@@ -71,13 +78,13 @@ public:
* @param partType the message part in which the field is stored. * @param partType the message part in which the field is stored.
* @param offset the offset to the first symbol in the message part in which the field is stored. * @param offset the offset to the first symbol in 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. * @param length the number of symbols in the message part in which the field is stored.
* @param dataType the data type. * @param dataType the data type definition.
* @param unit the value unit. * @param unit the value unit.
* @param comment the field comment. * @param comment the field comment.
*/ */
DataField(const std::string name, const PartType partType, DataField(const std::string name, const PartType partType,
const unsigned char offset, const unsigned char length, const unsigned char offset, const unsigned char length,
const DataType dataType, const std::string unit, const dataType_t dataType, const std::string unit,
const std::string comment) const std::string comment)
: m_name(name), m_partType(partType), m_offset(offset), : m_name(name), m_partType(partType), m_offset(offset),
m_length(length), m_dataType(dataType), m_unit(unit), m_length(length), m_dataType(dataType), m_unit(unit),
@@ -98,35 +105,35 @@ public:
std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end); std::vector<std::string>::iterator& it, const std::vector<std::string>::iterator end);
/** /**
* @brief Parses the value from the master or slave @a SymbolString. * @brief Reads the value from the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString to parse from. * @param masterData the unescaped master data @a SymbolString for reading binary data.
* @param slaveData the unescaped slave data @a SymbolString to parse from. * @param slaveData the unescaped slave data @a SymbolString for reading binary data.
* @return the parsed value as string. * @return the formatted value as string.
*/ */
const std::string parseSymbols(SymbolString& masterData, SymbolString& slaveData, bool verbose=false); const std::string read(SymbolString& masterData, SymbolString& slaveData, bool verbose=false);
/** /**
* @brief Formats the value to the master or slave @a SymbolString. * @brief Writes the value to the master or slave @a SymbolString.
* @param masterData the unescaped master data @a SymbolString to format to. * @param masterData the unescaped master data @a SymbolString for writing binary data.
* @param slaveData the unescaped slave data @a SymbolString to format to. * @param slaveData the unescaped slave data @a SymbolString for writing binary data.
* @param value the value as string. * @param value the formatted value as string.
*/ */
bool formatSymbols(const std::string& value, SymbolString& masterData, SymbolString& slaveData); bool write(const std::string& value, SymbolString& masterData, SymbolString& slaveData);
protected: protected:
/** /**
* @brief Internal method doing the actual parse for the individual data type. * @brief Internal method for reading the field from a @a SymbolString.
* @param data the unescaped data SymbolString to parse from. * @param input the unescaped @a SymbolString to read the binary value from.
* @param output the ostringstream to append to. * @param output the ostringstream to append the formatted value to.
* @return true if the value was parsed successfully. * @return true if the value was parsed successfully.
*/ */
virtual bool parse(SymbolString& data, std::ostringstream& output) = 0; virtual bool readSymbols(SymbolString& input, std::ostringstream& output) = 0;
/** /**
* @brief Internal method doing the actual format for the individual data type. * @brief Internal method for writing the field to a @a SymbolString.
* @param data the unescaped data SymbolString to format to. * @param input the istringstream to parse the formatted value from.
* @param input the istringstream to interpret. * @param output the unescaped @a SymbolString to write the binary value to.
* @return true if the value was formatted successfully. * @return true if the value was formatted successfully.
*/ */
virtual bool format(SymbolString& data, std::istringstream& input) = 0; virtual bool writeSymbols(std::istringstream& input, SymbolString& output) = 0;
/** the field name. */ /** the field name. */
const std::string m_name; const std::string m_name;
@@ -136,9 +143,8 @@ protected:
const unsigned char m_offset; const unsigned char m_offset;
/** the number of symbols in the message part in which the field is stored. */ /** the number of symbols in the message part in which the field is stored. */
const unsigned char m_length; const unsigned char m_length;
/** the data type. */ /** the data type definition. */
const DataType m_dataType; const dataType_t m_dataType;
//std::vector<std::string> m_valid;//TODO add list of possible values
/** the value unit. */ /** the value unit. */
const std::string m_unit; const std::string m_unit;
/** the field comment. */ /** the field comment. */
@@ -158,13 +164,13 @@ public:
* @param partType the message part in which the field is stored. * @param partType the message part in which the field is stored.
* @param offset the offset to the first symbol in the message part in which the field is stored. * @param offset the offset to the first symbol in 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. * @param length the number of symbols in the message part in which the field is stored.
* @param dataType the data type. * @param dataType the data type definition.
* @param unit the value unit. * @param unit the value unit.
* @param comment the field comment. * @param comment the field comment.
*/ */
StringDataField(const std::string name, const PartType partType, StringDataField(const std::string name, const PartType partType,
const unsigned char offset, const unsigned char length, const unsigned char offset, const unsigned char length,
const DataType dataType, const std::string unit, const dataType_t dataType, const std::string unit,
const std::string comment) const std::string comment)
: DataField(name, partType, offset, length, dataType, unit, comment) {} : DataField(name, partType, offset, length, dataType, unit, comment) {}
/** /**
@@ -173,8 +179,8 @@ public:
virtual ~StringDataField() {} virtual ~StringDataField() {}
protected: protected:
virtual bool parse(SymbolString& data, std::ostringstream& output); virtual bool readSymbols(SymbolString& input, std::ostringstream& output);
virtual bool format(SymbolString& data, std::istringstream& input); virtual bool writeSymbols(std::istringstream& input, SymbolString& output);
}; };
@@ -191,32 +197,113 @@ public:
* @param partType the message part in which the field is stored. * @param partType the message part in which the field is stored.
* @param offset the offset to the first symbol in the message part in which the field is stored. * @param offset the offset to the first symbol in 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. * @param length the number of symbols in the message part in which the field is stored.
* @param dataType the data type. * @param dataType the data type definition.
* @param comment the field comment. * @param comment the field comment.
* @param unit the value unit. * @param unit the value unit.
* @param factor the factor to apply on the value.
* @param replacement the (binary) replacement value to use if the value is not set. * @param replacement the (binary) replacement value to use if the value is not set.
*/ */
NumericDataField(const std::string name, const PartType partType, NumericDataField(const std::string name, const PartType partType,
const unsigned char offset, const unsigned char length, const unsigned char offset, const unsigned char length,
const DataType dataType, const std::string unit, const dataType_t dataType, const std::string unit,
const std::string comment, const float factor, const std::string comment)
const unsigned int replacement) : DataField(name, partType, offset, length, dataType, unit, comment) {}
: DataField(name, partType, offset, length, dataType, unit, comment),
m_factor(factor), m_replacement(replacement) {}
/** /**
* @brief Destructor. * @brief Destructor.
*/ */
virtual ~NumericDataField() {} virtual ~NumericDataField() {}
protected: protected:
virtual bool parse(SymbolString& data, std::ostringstream& output); /**
virtual bool format(SymbolString& data, std::istringstream& input); * @brief Internal method for reading the raw value from a @a SymbolString.
* @param input the unescaped @a SymbolString to read the binary value from.
* @param value the variable in which to store the raw value.
* @return true if the value was read successfully.
*/
bool readRawValue(SymbolString& input, unsigned int& value);
/**
* @brief Internal method for writing the raw value to a @a SymbolString.
* @param value the raw value to write.
* @param output the unescaped @a SymbolString to write the binary value to.
* @return true if the value was written successfully.
*/
bool writeRawValue(unsigned int value, SymbolString& output);
};
/**
* @brief Base class for all numeric data fields with a number representation.
*/
class NumberDataField : public NumericDataField
{
public:
/**
* @brief Constructs a new instance.
* @param name the field name.
* @param partType the message part in which the field is stored.
* @param offset the offset to the first symbol in 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.
* @param dataType the data type definition.
* @param comment the field comment.
* @param unit the value unit.
* @param replacement the (binary) replacement value to use if the value is not set.
* @param factor the factor to apply on the value.
*/
NumberDataField(const std::string name, const PartType partType,
const unsigned char offset, const unsigned char length,
const dataType_t dataType, const std::string unit,
const std::string comment, const float factor)
: NumericDataField(name, partType, offset, length, dataType, unit, comment),
m_factor(factor / dataType.divisor) {}
/**
* @brief Destructor.
*/
virtual ~NumberDataField() {}
protected:
virtual bool readSymbols(SymbolString& input, std::ostringstream& output);
virtual bool writeSymbols(std::istringstream& input, SymbolString& output);
/** the factor to apply on the value. */ /** the factor to apply on the value. */
const float m_factor; const float m_factor;
/** the (binary) replacement value to use if the value is not set. */
const unsigned int m_replacement; };
/**
* @brief A numeric data field with a list of value=text assignments and a string representation.
*/
class ValueListDataField : public NumericDataField
{
public:
/**
* @brief Constructs a new instance.
* @param name the field name.
* @param partType the message part in which the field is stored.
* @param offset the offset to the first symbol in 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.
* @param dataType the data type definition.
* @param comment the field comment.
* @param unit the value unit.
* @param factor the factor to apply on the value.
* @param replacement the (binary) replacement value to use if the value is not set.
* @param values the value=text assignments.
*/
ValueListDataField(const std::string name, const PartType partType,
const unsigned char offset, const unsigned char length,
const dataType_t dataType, const std::string unit,
const std::string comment, const std::map<unsigned int, std::string> values)
: NumericDataField(name, partType, offset, length, dataType, unit, comment),
m_values(values) {}
/**
* @brief Destructor.
*/
virtual ~ValueListDataField() {}
protected:
virtual bool readSymbols(SymbolString& input, std::ostringstream& output);
virtual bool writeSymbols(std::istringstream& input, SymbolString& output);
/** the value=text assignments. */
std::map<unsigned int, std::string> m_values;
}; };
Regular → Executable
+22 -20
View File
@@ -25,7 +25,6 @@ using namespace libebus;
int main () int main ()
{ {
//TODO dt_float,
//TODO dt_d1b, //TODO dt_d1b,
//TODO dt_d1c, //TODO dt_d1c,
//TODO dt_d2c, //TODO dt_d2c,
@@ -33,23 +32,26 @@ int main ()
//name;position(s);type;factor;unit;comment //name;position(s);type;factor;unit;comment
// {"temp;1;d2b;;°C;Aussentemperatur","temp=18.004 °C [Aussentemperatur]","10fe070009019258042126100714cc", "00"}, // {"temp;1;d2b;;°C;Aussentemperatur","temp=18.004 °C [Aussentemperatur]","10fe070009019258042126100714cc", "00"},
// {"zeit;1;ttm;2;Uhr;","zeit=22:40 Uhr","10feffff0188", "00"}, // {"zeit;1;ttm;2;Uhr;","zeit=22:40 Uhr","10feffff0188", "00"},
{"hex;1-10;hex","53 70 65 69 63 68 65 72 20 20", "10fe07000a53706569636865722020", "00"}, {"x;1-10;hex","53 70 65 69 63 68 65 72 20 20", "10fe07000a53706569636865722020", "00"},
{"zeit;1;bti","21:04:58","10fe070009580421", "00"}, {"x;1;bti","21:04:58","10fe070009580421", "00"},
{"datum;1;bda","26.10.2014","10fe07000926100714", "00"}, {"x;1;bda","26.10.2014","10fe07000926100714", "00"},
{"datum;1-3;bda","26.10.2014","10fe070003261014", "00"}, {"x;1-3;bda","26.10.2014","10fe070003261014", "00"},
{"tag;1;bdy","Sun","10fe07000307", "00"}, {"x;1;hdy","Sun","10fe07000307", "00"},
{"temp;1;d2b","18.004","10fe0700090112", "00"}, {"x;1;bdy","Sun","10fe07000306", "00"},
{"zeit;1;ttm","22:40","10feffff0188", "00"}, {"x;1;d2b","18.004","10fe0700090112", "00"},
{"bcd;1;bcd","26","10feffff0126", "00"}, {"x;1;ttm","22:40","10feffff0188", "00"},
{"bcd;1;bcd","-","10feffff01ff", "00"}, {"x;1;bcd","26","10feffff0126", "00"},
{"uch;1;uch","38","10feffff0126", "00"}, {"x;1;bcd","-","10feffff01ff", "00"},
{"sch;1;sch","-90","10feffff01a6", "00"}, {"x;1;uch","38","10feffff0126", "00"},
{"uin;1;uin","38","10feffff022600", "00"}, {"x;1;sch","-90","10feffff01a6", "00"},
{"sin;1;sin","-90","10feffff02a6ff", "00"}, {"x;1;d1b","-90","10feffff01a6", "00"},
{"ulg;1;ulg","38","10feffff0426000000", "00"}, {"x;1;uin","38","10feffff022600", "00"},
{"slg;1;slg","-90","10feffff04a6ffffff", "00"}, {"x;1;sin","-90","10feffff02a6ff", "00"},
{"str;1-9;str","hallo Du!","10feffff0868616c6c6f20447521", "00"}, {"x;1;ulg","38","10feffff0426000000", "00"},
{"str;1-9;str","hallo Du ","10feffff0868616c6c6f20447500", "00"}, {"x;1;slg","-90","10feffff04a6ffffff", "00"},
{"x;1;flt","-0.090","10feffff02a6ff", "00"},
{"x;1-9;str","hallo Du!","10feffff0868616c6c6f20447521", "00"},
{"x;1-9;str","hallo Du ","10feffff0868616c6c6f20447520", "00"},
}; };
for (size_t i = 0; i < sizeof(checks)/sizeof(checks[0]); i++) { for (size_t i = 0; i < sizeof(checks)/sizeof(checks[0]); i++) {
std::istringstream isstr(checks[i][0]); std::istringstream isstr(checks[i][0]);
@@ -72,7 +74,7 @@ int main ()
std::cout << "create \"" << checks[i][0] << "\" successful" << std::endl; std::cout << "create \"" << checks[i][0] << "\" successful" << std::endl;
std::string gotStr = field->parseSymbols(mstr, sstr); std::string gotStr = field->read(mstr, sstr);
if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0) if (strcasecmp(gotStr.c_str(), expectStr.c_str()) == 0)
std::cout << "parse successful: " << gotStr << std::endl; std::cout << "parse successful: " << gotStr << std::endl;
@@ -82,7 +84,7 @@ int main ()
SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false); SymbolString writeMstr = SymbolString(mstr.getDataStr().substr(0, 10), false);
SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false); SymbolString writeSstr = SymbolString(sstr.getDataStr().substr(0, 2), false);
if (field->formatSymbols(gotStr, writeMstr, writeSstr) == false) if (field->write(gotStr, writeMstr, writeSstr) == false)
std::cout << "format failed" << std::endl; std::cout << "format failed" << std::endl;
else { else {
if (mstr == writeMstr && sstr == writeSstr) if (mstr == writeMstr && sstr == writeSstr)