DBC Framework
DBC parsing and CAN signal decoding framework
Loading...
Searching...
No Matches
dbc_parser.cpp
Go to the documentation of this file.
1
12#include "dbc_parser.h"
13#include "dbc_statement_handler.h"
14
15#include <cctype>
16#include <fstream>
17#include <stdexcept>
18
19namespace {
25 std::string TrimText(const std::string &text) {
26 std::string::size_type begin = 0U;
27 while ((begin < text.size()) &&
28 std::isspace(static_cast<unsigned char>(text[begin])))
29 ++begin;
30
31 std::string::size_type end = text.size();
32 while ((end > begin) &&
33 std::isspace(static_cast<unsigned char>(text[end - 1U])))
34 --end;
35
36 return text.substr(begin, end - begin);
37 }
38}// namespace
39
40DbcDatabase DbcParser::ParseFile(const std::string &filePath) const {
41 std::ifstream input(filePath.c_str());
42 if (!input.is_open())
43 throw std::runtime_error("Failed to open DBC file: " + filePath);
44
45 DbcDatabase database;
46 DbcParseContext context{database, nullptr};
47 const DbcStatementDispatcher dispatcher;
48
49 std::size_t lineNumber = 0U;
50 std::string line;
51 while (std::getline(input, line)) {
52 ++lineNumber;
53 line = Trim(line);
54 if (line.empty())
55 continue;
56
57 try {
58 dispatcher.Dispatch(line, context);
59 } catch (const std::exception &error) {
60 throw std::runtime_error(filePath + ":" + std::to_string(lineNumber) +
61 ": " + error.what());
62 }
63 }
64
65 return database;
66}
67
68std::string DbcParser::Trim(const std::string &text) {
69 return TrimText(text);
70}
DbcDatabase ParseFile(const std::string &filePath) const
Parse DBC file.
bool Dispatch(const std::string &line, DbcParseContext &context) const
Created: 2026-03-13 Author: Deeaitch (Dim. Himro)
Parsed DBC content stored in a simple internal form.