Ensure one cannot save DBC files with blank message and signal names,

fix up some warnings.
This commit is contained in:
Collin Kidder
2018-09-18 19:23:38 -04:00
parent db4ea80bbc
commit b3a5f2498f
3 changed files with 66 additions and 44 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef CONFIG #ifndef CONFIG
#define CONFIG #define CONFIG
#define VERSION 187 #define VERSION 189
//try to keep this in sync. //try to keep this in sync.
//SavvyCAN will complain if you connect a GVRET board with a revision //SavvyCAN will complain if you connect a GVRET board with a revision
+63 -42
View File
@@ -9,20 +9,20 @@
#include <QPalette> #include <QPalette>
#include "utility.h" #include "utility.h"
DBCHandler* DBCHandler::instance = NULL; DBCHandler* DBCHandler::instance = nullptr;
DBC_SIGNAL* DBCSignalHandler::findSignalByIdx(int idx) DBC_SIGNAL* DBCSignalHandler::findSignalByIdx(int idx)
{ {
if (sigs.count() == 0) return NULL; if (sigs.count() == 0) return nullptr;
if (idx < 0) return NULL; if (idx < 0) return nullptr;
if (idx >= sigs.count()) return NULL; if (idx >= sigs.count()) return nullptr;
return &sigs[idx]; return &sigs[idx];
} }
DBC_SIGNAL* DBCSignalHandler::findSignalByName(QString name) DBC_SIGNAL* DBCSignalHandler::findSignalByName(QString name)
{ {
if (sigs.count() == 0) return NULL; if (sigs.count() == 0) return nullptr;
for (int i = 0; i < sigs.count(); i++) for (int i = 0; i < sigs.count(); i++)
{ {
if (sigs[i].name.compare(name, Qt::CaseInsensitive) == 0) if (sigs[i].name.compare(name, Qt::CaseInsensitive) == 0)
@@ -30,7 +30,7 @@ DBC_SIGNAL* DBCSignalHandler::findSignalByName(QString name)
return &sigs[i]; return &sigs[i];
} }
} }
return NULL; return nullptr;
} }
bool DBCSignalHandler::addSignal(DBC_SIGNAL &sig) bool DBCSignalHandler::addSignal(DBC_SIGNAL &sig)
@@ -82,7 +82,7 @@ int DBCSignalHandler::getCount()
DBC_MESSAGE* DBCMessageHandler::findMsgByID(uint32_t id) DBC_MESSAGE* DBCMessageHandler::findMsgByID(uint32_t id)
{ {
if (messages.count() == 0) return NULL; if (messages.count() == 0) return nullptr;
for (int i = 0; i < messages.count(); i++) for (int i = 0; i < messages.count(); i++)
{ {
if (isJ1939Handler) if (isJ1939Handler)
@@ -115,20 +115,20 @@ DBC_MESSAGE* DBCMessageHandler::findMsgByID(uint32_t id)
} }
} }
} }
return NULL; return nullptr;
} }
DBC_MESSAGE* DBCMessageHandler::findMsgByIdx(int idx) DBC_MESSAGE* DBCMessageHandler::findMsgByIdx(int idx)
{ {
if (messages.count() == 0) return NULL; if (messages.count() == 0) return nullptr;
if (idx < 0) return NULL; if (idx < 0) return nullptr;
if (idx >= messages.count()) return NULL; if (idx >= messages.count()) return nullptr;
return &messages[idx]; return &messages[idx];
} }
DBC_MESSAGE* DBCMessageHandler::findMsgByName(QString name) DBC_MESSAGE* DBCMessageHandler::findMsgByName(QString name)
{ {
if (messages.count() == 0) return NULL; if (messages.count() == 0) return nullptr;
for (int i = 0; i < messages.count(); i++) for (int i = 0; i < messages.count(); i++)
{ {
if (messages[i].name.compare(name, Qt::CaseInsensitive) == 0) if (messages[i].name.compare(name, Qt::CaseInsensitive) == 0)
@@ -136,7 +136,7 @@ DBC_MESSAGE* DBCMessageHandler::findMsgByName(QString name)
return &messages[i]; return &messages[i];
} }
} }
return NULL; return nullptr;
} }
bool DBCMessageHandler::addMessage(DBC_MESSAGE &msg) bool DBCMessageHandler::addMessage(DBC_MESSAGE &msg)
@@ -251,14 +251,14 @@ DBCFile& DBCFile::operator=(const DBCFile& cpy)
DBC_NODE* DBCFile::findNodeByIdx(int idx) DBC_NODE* DBCFile::findNodeByIdx(int idx)
{ {
if (idx < 0) return NULL; if (idx < 0) return nullptr;
if (idx >= dbc_nodes.count()) return NULL; if (idx >= dbc_nodes.count()) return nullptr;
return &dbc_nodes[idx]; return &dbc_nodes[idx];
} }
DBC_NODE* DBCFile::findNodeByName(QString name) DBC_NODE* DBCFile::findNodeByName(QString name)
{ {
if (dbc_nodes.length() == 0) return NULL; if (dbc_nodes.length() == 0) return nullptr;
for (int i = 0; i < dbc_nodes.length(); i++) for (int i = 0; i < dbc_nodes.length(); i++)
{ {
if (dbc_nodes[i].name.compare(name, Qt::CaseInsensitive) == 0) if (dbc_nodes[i].name.compare(name, Qt::CaseInsensitive) == 0)
@@ -266,7 +266,7 @@ DBC_NODE* DBCFile::findNodeByName(QString name)
return &dbc_nodes[i]; return &dbc_nodes[i];
} }
} }
return NULL; return nullptr;
} }
QString DBCFile::getFullFilename() QString DBCFile::getFullFilename()
@@ -298,7 +298,7 @@ void DBCFile::setAssocBus(int bus)
DBC_ATTRIBUTE *DBCFile::findAttributeByName(QString name) DBC_ATTRIBUTE *DBCFile::findAttributeByName(QString name)
{ {
if (dbc_attributes.length() == 0) return NULL; if (dbc_attributes.length() == 0) return nullptr;
for (int i = 0; i < dbc_attributes.length(); i++) for (int i = 0; i < dbc_attributes.length(); i++)
{ {
if (dbc_attributes[i].name.compare(name, Qt::CaseInsensitive) == 0) if (dbc_attributes[i].name.compare(name, Qt::CaseInsensitive) == 0)
@@ -306,13 +306,13 @@ DBC_ATTRIBUTE *DBCFile::findAttributeByName(QString name)
return &dbc_attributes[i]; return &dbc_attributes[i];
} }
} }
return NULL; return nullptr;
} }
DBC_ATTRIBUTE *DBCFile::findAttributeByIdx(int idx) DBC_ATTRIBUTE *DBCFile::findAttributeByIdx(int idx)
{ {
if (idx < 0) return NULL; if (idx < 0) return nullptr;
if (idx >= dbc_attributes.count()) return NULL; if (idx >= dbc_attributes.count()) return nullptr;
return &dbc_attributes[idx]; return &dbc_attributes[idx];
} }
@@ -345,7 +345,7 @@ DBC_MESSAGE* DBCFile::parseMessageLine(QString line)
DBC_MESSAGE msg; DBC_MESSAGE msg;
msg.ID = match.captured(1).toULong() & 0x7FFFFFFFul; //the ID is always stored in decimal format msg.ID = match.captured(1).toULong() & 0x7FFFFFFFul; //the ID is always stored in decimal format
msg.name = match.captured(2); msg.name = match.captured(2);
msg.len = match.captured(3).toInt(); msg.len = match.captured(3).toUInt();
msg.sender = findNodeByName(match.captured(4)); msg.sender = findNodeByName(match.captured(4));
if (!msg.sender) msg.sender = findNodeByIdx(0); if (!msg.sender) msg.sender = findNodeByIdx(0);
messageHandler->addMessage(msg); messageHandler->addMessage(msg);
@@ -480,11 +480,11 @@ bool DBCFile::parseValueLine(QString line)
if (match.hasMatch()) if (match.hasMatch())
{ {
//qDebug() << "Data was: " << match.captured(3); //qDebug() << "Data was: " << match.captured(3);
DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toUInt());
if (msg != NULL) if (msg != nullptr)
{ {
DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(match.captured(2)); DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(match.captured(2));
if (sig != NULL) if (sig != nullptr)
{ {
QString tokenString = match.captured(3); QString tokenString = match.captured(3);
DBC_VAL_ENUM_ENTRY val; DBC_VAL_ENUM_ENTRY val;
@@ -529,7 +529,7 @@ bool DBCFile::parseAttributeLine(QString line)
if (foundAttr) if (foundAttr)
{ {
qDebug() << "That attribute does exist"; qDebug() << "That attribute does exist";
DBC_MESSAGE *foundMsg = messageHandler->findMsgByID(match.captured(2).toInt()); DBC_MESSAGE *foundMsg = messageHandler->findMsgByID(match.captured(2).toUInt());
if (foundMsg) if (foundMsg)
{ {
qDebug() << "It references a valid, registered message"; qDebug() << "It references a valid, registered message";
@@ -561,7 +561,7 @@ bool DBCFile::parseAttributeLine(QString line)
if (foundAttr) if (foundAttr)
{ {
qDebug() << "That attribute does exist"; qDebug() << "That attribute does exist";
DBC_MESSAGE *foundMsg = messageHandler->findMsgByID(match.captured(2).toInt()); DBC_MESSAGE *foundMsg = messageHandler->findMsgByID(match.captured(2).toUInt());
if (foundMsg) if (foundMsg)
{ {
qDebug() << "It references a valid, registered message"; qDebug() << "It references a valid, registered message";
@@ -666,7 +666,7 @@ void DBCFile::loadFile(QString fileName)
QString line, rawLine; QString line, rawLine;
QRegularExpression regex; QRegularExpression regex;
QRegularExpressionMatch match; QRegularExpressionMatch match;
DBC_MESSAGE *currentMessage = NULL; DBC_MESSAGE *currentMessage = nullptr;
DBC_ATTRIBUTE attr; DBC_ATTRIBUTE attr;
int numSigFaults = 0, numMsgFaults = 0; int numSigFaults = 0, numMsgFaults = 0;
@@ -750,11 +750,11 @@ void DBCFile::loadFile(QString fileName)
if (match.hasMatch()) if (match.hasMatch())
{ {
//qDebug() << "Comment was: " << match.captured(3); //qDebug() << "Comment was: " << match.captured(3);
DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toUInt());
if (msg != NULL) if (msg != nullptr)
{ {
DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(match.captured(2)); DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(match.captured(2));
if (sig != NULL) if (sig != nullptr)
{ {
sig->comment = match.captured(3); sig->comment = match.captured(3);
} }
@@ -771,8 +771,8 @@ void DBCFile::loadFile(QString fileName)
if (match.hasMatch()) if (match.hasMatch())
{ {
//qDebug() << "Comment was: " << match.captured(2); //qDebug() << "Comment was: " << match.captured(2);
DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toUInt());
if (msg != NULL) if (msg != nullptr)
{ {
msg->comment = match.captured(2); msg->comment = match.captured(2);
} }
@@ -789,7 +789,7 @@ void DBCFile::loadFile(QString fileName)
{ {
//qDebug() << "Comment was: " << match.captured(2); //qDebug() << "Comment was: " << match.captured(2);
DBC_NODE *node = findNodeByName(match.captured(1)); DBC_NODE *node = findNodeByName(match.captured(1));
if (node != NULL) if (node != nullptr)
{ {
node->comment = match.captured(2); node->comment = match.captured(2);
} }
@@ -1040,6 +1040,9 @@ bool DBCFile::parseAttribute(QString inpString, DBC_ATTRIBUTE &attr)
void DBCFile::saveFile(QString fileName) void DBCFile::saveFile(QString fileName)
{ {
int nodeNumber = 1;
int msgNumber = 1;
int sigNumber = 1;
QFile *outFile = new QFile(fileName); QFile *outFile = new QFile(fileName);
QString nodesOutput, msgOutput, commentsOutput, valuesOutput; QString nodesOutput, msgOutput, commentsOutput, valuesOutput;
QString defaultsOutput, attrValOutput; QString defaultsOutput, attrValOutput;
@@ -1093,6 +1096,11 @@ void DBCFile::saveFile(QString fileName)
DBC_NODE node = dbc_nodes[x]; DBC_NODE node = dbc_nodes[x];
if (node.name.compare("Vector__XXX", Qt::CaseInsensitive) != 0) if (node.name.compare("Vector__XXX", Qt::CaseInsensitive) != 0)
{ {
if (node.name.length() < 1) //detect an empty string and fill it out with something
{
node.name = "NODE" + QString::number(nodeNumber);
nodeNumber++;
}
nodesOutput.append(node.name + " "); nodesOutput.append(node.name + " ");
if (node.comment.length() > 0) if (node.comment.length() > 0)
{ {
@@ -1123,6 +1131,12 @@ void DBCFile::saveFile(QString fileName)
{ {
DBC_MESSAGE *msg = messageHandler->findMsgByIdx(x); DBC_MESSAGE *msg = messageHandler->findMsgByIdx(x);
if (msg->name.length() < 1) //detect an empty string and fill it out with something
{
msg->name = "MSG" + QString::number(msgNumber);
msgNumber++;
}
msgOutput.append("BO_ " + QString::number(msg->ID) + " " + msg->name + ": " + QString::number(msg->len) + msgOutput.append("BO_ " + QString::number(msg->ID) + " " + msg->name + ": " + QString::number(msg->len) +
" " + msg->sender->name + "\n"); " " + msg->sender->name + "\n");
if (msg->comment.length() > 0) if (msg->comment.length() > 0)
@@ -1150,6 +1164,13 @@ void DBCFile::saveFile(QString fileName)
for (int s = 0; s < msg->sigHandler->getCount(); s++) for (int s = 0; s < msg->sigHandler->getCount(); s++)
{ {
DBC_SIGNAL *sig = msg->sigHandler->findSignalByIdx(s); DBC_SIGNAL *sig = msg->sigHandler->findSignalByIdx(s);
if (sig->name.length() < 1) //detect an empty string and fill it out with something
{
sig->name = "SIG" + QString::number(sigNumber);
sigNumber++;
}
msgOutput.append(" SG_ " + sig->name); msgOutput.append(" SG_ " + sig->name);
if (sig->isMultiplexor) msgOutput.append(" M"); if (sig->isMultiplexor) msgOutput.append(" M");
@@ -1403,7 +1424,7 @@ DBCFile* DBCHandler::loadDBCFile(int idx)
return &loadedFiles.last(); return &loadedFiles.last();
} }
return NULL; return nullptr;
} }
void DBCHandler::removeDBCFile(int idx) void DBCHandler::removeDBCFile(int idx)
@@ -1443,10 +1464,10 @@ DBC_MESSAGE* DBCHandler::findMessage(const CANFrame &frame)
if (loadedFiles[i].getAssocBus() == -1 || frame.bus == (unsigned int)loadedFiles[i].getAssocBus()) if (loadedFiles[i].getAssocBus() == -1 || frame.bus == (unsigned int)loadedFiles[i].getAssocBus())
{ {
DBC_MESSAGE* msg = loadedFiles[i].messageHandler->findMsgByID(frame.ID); DBC_MESSAGE* msg = loadedFiles[i].messageHandler->findMsgByID(frame.ID);
if (msg != NULL) return msg; if (msg != nullptr) return msg;
} }
} }
return NULL; return nullptr;
} }
int DBCHandler::getFileCount() int DBCHandler::getFileCount()
@@ -1456,15 +1477,15 @@ int DBCHandler::getFileCount()
DBCFile* DBCHandler::getFileByIdx(int idx) DBCFile* DBCHandler::getFileByIdx(int idx)
{ {
if (loadedFiles.count() == 0) return NULL; if (loadedFiles.count() == 0) return nullptr;
if (idx < 0) return NULL; if (idx < 0) return nullptr;
if (idx >= loadedFiles.count()) return NULL; if (idx >= loadedFiles.count()) return nullptr;
return &loadedFiles[idx]; return &loadedFiles[idx];
} }
DBCFile* DBCHandler::getFileByName(QString name) DBCFile* DBCHandler::getFileByName(QString name)
{ {
if (loadedFiles.count() == 0) return NULL; if (loadedFiles.count() == 0) return nullptr;
for (int i = 0; i < loadedFiles.count(); i++) for (int i = 0; i < loadedFiles.count(); i++)
{ {
if (loadedFiles[i].getFilename().compare(name, Qt::CaseInsensitive) == 0) if (loadedFiles[i].getFilename().compare(name, Qt::CaseInsensitive) == 0)
@@ -1472,7 +1493,7 @@ DBCFile* DBCHandler::getFileByName(QString name)
return &loadedFiles[i]; return &loadedFiles[i];
} }
} }
return NULL; return nullptr;
} }
DBCHandler::DBCHandler() DBCHandler::DBCHandler()
+2 -1
View File
@@ -156,7 +156,8 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) :
[=]() [=]()
{ {
if (currentSignal == NULL) return; if (currentSignal == NULL) return;
currentSignal->name = ui->txtName->text().simplified().replace(' ', '_'); QString tempNameStr = ui->txtName->text().simplified().replace(' ', '_');
if (tempNameStr.length() > 0) currentSignal->name = tempNameStr;
//need to update the list too. //need to update the list too.
ui->signalsList->currentItem()->setText(currentSignal->name); ui->signalsList->currentItem()->setText(currentSignal->name);
}); });