From b3a5f2498f2c6cc619c83307cc32e65864956e48 Mon Sep 17 00:00:00 2001 From: Collin Kidder Date: Tue, 18 Sep 2018 19:23:38 -0400 Subject: [PATCH] Ensure one cannot save DBC files with blank message and signal names, fix up some warnings. --- config.h | 2 +- dbc/dbchandler.cpp | 105 ++++++++++++++++++++++++---------------- dbc/dbcsignaleditor.cpp | 3 +- 3 files changed, 66 insertions(+), 44 deletions(-) diff --git a/config.h b/config.h index e277117..f8a1769 100644 --- a/config.h +++ b/config.h @@ -1,7 +1,7 @@ #ifndef CONFIG #define CONFIG -#define VERSION 187 +#define VERSION 189 //try to keep this in sync. //SavvyCAN will complain if you connect a GVRET board with a revision diff --git a/dbc/dbchandler.cpp b/dbc/dbchandler.cpp index 6f1c8e3..3a86bb0 100644 --- a/dbc/dbchandler.cpp +++ b/dbc/dbchandler.cpp @@ -9,20 +9,20 @@ #include #include "utility.h" -DBCHandler* DBCHandler::instance = NULL; +DBCHandler* DBCHandler::instance = nullptr; DBC_SIGNAL* DBCSignalHandler::findSignalByIdx(int idx) { - if (sigs.count() == 0) return NULL; - if (idx < 0) return NULL; - if (idx >= sigs.count()) return NULL; + if (sigs.count() == 0) return nullptr; + if (idx < 0) return nullptr; + if (idx >= sigs.count()) return nullptr; return &sigs[idx]; } 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++) { if (sigs[i].name.compare(name, Qt::CaseInsensitive) == 0) @@ -30,7 +30,7 @@ DBC_SIGNAL* DBCSignalHandler::findSignalByName(QString name) return &sigs[i]; } } - return NULL; + return nullptr; } bool DBCSignalHandler::addSignal(DBC_SIGNAL &sig) @@ -82,7 +82,7 @@ int DBCSignalHandler::getCount() 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++) { if (isJ1939Handler) @@ -115,20 +115,20 @@ DBC_MESSAGE* DBCMessageHandler::findMsgByID(uint32_t id) } } } - return NULL; + return nullptr; } DBC_MESSAGE* DBCMessageHandler::findMsgByIdx(int idx) { - if (messages.count() == 0) return NULL; - if (idx < 0) return NULL; - if (idx >= messages.count()) return NULL; + if (messages.count() == 0) return nullptr; + if (idx < 0) return nullptr; + if (idx >= messages.count()) return nullptr; return &messages[idx]; } 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++) { if (messages[i].name.compare(name, Qt::CaseInsensitive) == 0) @@ -136,7 +136,7 @@ DBC_MESSAGE* DBCMessageHandler::findMsgByName(QString name) return &messages[i]; } } - return NULL; + return nullptr; } bool DBCMessageHandler::addMessage(DBC_MESSAGE &msg) @@ -251,14 +251,14 @@ DBCFile& DBCFile::operator=(const DBCFile& cpy) DBC_NODE* DBCFile::findNodeByIdx(int idx) { - if (idx < 0) return NULL; - if (idx >= dbc_nodes.count()) return NULL; + if (idx < 0) return nullptr; + if (idx >= dbc_nodes.count()) return nullptr; return &dbc_nodes[idx]; } 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++) { 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 NULL; + return nullptr; } QString DBCFile::getFullFilename() @@ -298,7 +298,7 @@ void DBCFile::setAssocBus(int bus) 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++) { 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 NULL; + return nullptr; } DBC_ATTRIBUTE *DBCFile::findAttributeByIdx(int idx) { - if (idx < 0) return NULL; - if (idx >= dbc_attributes.count()) return NULL; + if (idx < 0) return nullptr; + if (idx >= dbc_attributes.count()) return nullptr; return &dbc_attributes[idx]; } @@ -345,7 +345,7 @@ DBC_MESSAGE* DBCFile::parseMessageLine(QString line) DBC_MESSAGE msg; msg.ID = match.captured(1).toULong() & 0x7FFFFFFFul; //the ID is always stored in decimal format msg.name = match.captured(2); - msg.len = match.captured(3).toInt(); + msg.len = match.captured(3).toUInt(); msg.sender = findNodeByName(match.captured(4)); if (!msg.sender) msg.sender = findNodeByIdx(0); messageHandler->addMessage(msg); @@ -480,11 +480,11 @@ bool DBCFile::parseValueLine(QString line) if (match.hasMatch()) { //qDebug() << "Data was: " << match.captured(3); - DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); - if (msg != NULL) + DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toUInt()); + if (msg != nullptr) { DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(match.captured(2)); - if (sig != NULL) + if (sig != nullptr) { QString tokenString = match.captured(3); DBC_VAL_ENUM_ENTRY val; @@ -529,7 +529,7 @@ bool DBCFile::parseAttributeLine(QString line) if (foundAttr) { 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) { qDebug() << "It references a valid, registered message"; @@ -561,7 +561,7 @@ bool DBCFile::parseAttributeLine(QString line) if (foundAttr) { 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) { qDebug() << "It references a valid, registered message"; @@ -666,7 +666,7 @@ void DBCFile::loadFile(QString fileName) QString line, rawLine; QRegularExpression regex; QRegularExpressionMatch match; - DBC_MESSAGE *currentMessage = NULL; + DBC_MESSAGE *currentMessage = nullptr; DBC_ATTRIBUTE attr; int numSigFaults = 0, numMsgFaults = 0; @@ -750,11 +750,11 @@ void DBCFile::loadFile(QString fileName) if (match.hasMatch()) { //qDebug() << "Comment was: " << match.captured(3); - DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); - if (msg != NULL) + DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toUInt()); + if (msg != nullptr) { DBC_SIGNAL *sig = msg->sigHandler->findSignalByName(match.captured(2)); - if (sig != NULL) + if (sig != nullptr) { sig->comment = match.captured(3); } @@ -771,8 +771,8 @@ void DBCFile::loadFile(QString fileName) if (match.hasMatch()) { //qDebug() << "Comment was: " << match.captured(2); - DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toInt()); - if (msg != NULL) + DBC_MESSAGE *msg = messageHandler->findMsgByID(match.captured(1).toUInt()); + if (msg != nullptr) { msg->comment = match.captured(2); } @@ -789,7 +789,7 @@ void DBCFile::loadFile(QString fileName) { //qDebug() << "Comment was: " << match.captured(2); DBC_NODE *node = findNodeByName(match.captured(1)); - if (node != NULL) + if (node != nullptr) { node->comment = match.captured(2); } @@ -1040,6 +1040,9 @@ bool DBCFile::parseAttribute(QString inpString, DBC_ATTRIBUTE &attr) void DBCFile::saveFile(QString fileName) { + int nodeNumber = 1; + int msgNumber = 1; + int sigNumber = 1; QFile *outFile = new QFile(fileName); QString nodesOutput, msgOutput, commentsOutput, valuesOutput; QString defaultsOutput, attrValOutput; @@ -1093,6 +1096,11 @@ void DBCFile::saveFile(QString fileName) DBC_NODE node = dbc_nodes[x]; 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 + " "); if (node.comment.length() > 0) { @@ -1123,6 +1131,12 @@ void DBCFile::saveFile(QString fileName) { 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) + " " + msg->sender->name + "\n"); if (msg->comment.length() > 0) @@ -1150,6 +1164,13 @@ void DBCFile::saveFile(QString fileName) for (int s = 0; s < msg->sigHandler->getCount(); 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); if (sig->isMultiplexor) msgOutput.append(" M"); @@ -1403,7 +1424,7 @@ DBCFile* DBCHandler::loadDBCFile(int idx) return &loadedFiles.last(); } - return NULL; + return nullptr; } 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()) { 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() @@ -1456,15 +1477,15 @@ int DBCHandler::getFileCount() DBCFile* DBCHandler::getFileByIdx(int idx) { - if (loadedFiles.count() == 0) return NULL; - if (idx < 0) return NULL; - if (idx >= loadedFiles.count()) return NULL; + if (loadedFiles.count() == 0) return nullptr; + if (idx < 0) return nullptr; + if (idx >= loadedFiles.count()) return nullptr; return &loadedFiles[idx]; } 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++) { if (loadedFiles[i].getFilename().compare(name, Qt::CaseInsensitive) == 0) @@ -1472,7 +1493,7 @@ DBCFile* DBCHandler::getFileByName(QString name) return &loadedFiles[i]; } } - return NULL; + return nullptr; } DBCHandler::DBCHandler() diff --git a/dbc/dbcsignaleditor.cpp b/dbc/dbcsignaleditor.cpp index 02ea467..fb179e7 100644 --- a/dbc/dbcsignaleditor.cpp +++ b/dbc/dbcsignaleditor.cpp @@ -156,7 +156,8 @@ DBCSignalEditor::DBCSignalEditor(QWidget *parent) : [=]() { 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. ui->signalsList->currentItem()->setText(currentSignal->name); });