DBC parser initial commit.
This commit is contained in:
99
main.cpp
Normal file
99
main.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <cstddef>
|
||||
|
||||
#include "dbc_parser.h"
|
||||
#include "dbc_tree_builder.h"
|
||||
#include "tree_node.h"
|
||||
|
||||
static void PrintReceivers (const std::vector<std::string> &receivers) {
|
||||
for (std::size_t index = 0U; index < receivers.size(); ++index) {
|
||||
if (index != 0U)
|
||||
std::cout << ",";
|
||||
|
||||
std::cout << receivers[index];
|
||||
}
|
||||
}
|
||||
|
||||
static void PrintTree (const TreeNode *node, int indent) {
|
||||
if (node == nullptr)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < indent; ++i)
|
||||
std::cout << " ";
|
||||
|
||||
switch (node->GetType()) {
|
||||
case NodeType::Root:
|
||||
std::cout << "[root] " << node->GetName() << "\n";
|
||||
break;
|
||||
|
||||
case NodeType::Frame: {
|
||||
const FrameInfo *frame = node->GetFrame();
|
||||
std::cout << "[frame] " << node->GetName();
|
||||
|
||||
if (frame != nullptr) {
|
||||
std::cout << " id=" << frame->canId
|
||||
<< " dlc=" << static_cast<unsigned int> (frame->dlc)
|
||||
<< " tx=" << frame->transmitter;
|
||||
|
||||
if (frame->hasPgn)
|
||||
std::cout << " pgn=" << frame->pgn;
|
||||
|
||||
if (!frame->comment.empty())
|
||||
std::cout << " comment=\"" << frame->comment << "\"";
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
break;
|
||||
}
|
||||
|
||||
case NodeType::Signal: {
|
||||
const SignalInfo *signal = node->GetSignal();
|
||||
std::cout << "[signal] " << node->GetName();
|
||||
|
||||
if (signal != nullptr) {
|
||||
std::cout << " start=" << signal->startBit
|
||||
<< " len=" << signal->length
|
||||
<< " unit=" << signal->unit
|
||||
<< " rx=";
|
||||
|
||||
PrintReceivers (signal->receivers);
|
||||
|
||||
if (!signal->comment.empty())
|
||||
std::cout << " comment=\"" << signal->comment << "\"";
|
||||
}
|
||||
|
||||
std::cout << "\n";
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
std::cout << "[unknown]\n";
|
||||
break;
|
||||
}
|
||||
|
||||
for (std::size_t i = 0U; i < node->GetChildCount(); ++i)
|
||||
PrintTree (node->GetChild (i), indent + 1);
|
||||
}
|
||||
|
||||
int main (int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: dbc_demo <file.dbc>\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
try {
|
||||
DbcParser parser;
|
||||
DbcDatabase database = parser.ParseFile (argv[1]);
|
||||
|
||||
DbcTreeBuilder builder;
|
||||
std::unique_ptr<TreeNode> root = builder.Build (database);
|
||||
|
||||
PrintTree (root.get(), 0);
|
||||
} catch (const std::exception &ex) {
|
||||
std::cerr << "Error: " << ex.what() << "\n";
|
||||
return 2;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user