73 lines
1.9 KiB
C++
73 lines
1.9 KiB
C++
/**
|
|
* @file dbc_parser.cpp
|
|
* @brief
|
|
*
|
|
* Created: 2026-03-13
|
|
* Author: Deeaitch (Dim. Himro)
|
|
*
|
|
* Licensed under the MIT License.
|
|
* See LICENSE file in the project root for full license text.
|
|
*/
|
|
|
|
#include "dbc_parser.h"
|
|
#include "dbc_statement_handler.h"
|
|
|
|
#include <cctype>
|
|
#include <fstream>
|
|
#include <stdexcept>
|
|
|
|
namespace {
|
|
/**
|
|
* @brief Remove leading and trailing spaces.
|
|
* @param text Input text.
|
|
* @return Trimmed text.
|
|
*/
|
|
std::string TrimText(const std::string &text) {
|
|
std::string::size_type begin = 0U;
|
|
while ((begin < text.size()) &&
|
|
std::isspace(static_cast<unsigned char>(text[begin])))
|
|
++begin;
|
|
|
|
std::string::size_type end = text.size();
|
|
while ((end > begin) &&
|
|
std::isspace(static_cast<unsigned char>(text[end - 1U])))
|
|
--end;
|
|
|
|
return text.substr(begin, end - begin);
|
|
}
|
|
}// namespace
|
|
|
|
DbcDatabase DbcParser::ParseFile(const std::string &filePath) const {
|
|
std::ifstream input(filePath.c_str());
|
|
if (!input.is_open())
|
|
throw std::runtime_error("Failed to open DBC file: " + filePath);
|
|
|
|
DbcDatabase database;
|
|
DbcParseContext context{database, nullptr};
|
|
const DbcStatementDispatcher dispatcher;
|
|
|
|
std::size_t lineNumber = 0U;
|
|
std::string line;
|
|
while (std::getline(input, line)) {
|
|
++lineNumber;
|
|
line = Trim(line);
|
|
if (line.empty())
|
|
continue;
|
|
|
|
try {
|
|
context.lineNumber = lineNumber;
|
|
dispatcher.Dispatch(line, context);
|
|
} catch (const std::exception &error) {
|
|
throw std::runtime_error(filePath + ":" + std::to_string(lineNumber) +
|
|
": " + error.what());
|
|
}
|
|
}
|
|
|
|
context.ResolveValueDescriptions(filePath);
|
|
return database;
|
|
}
|
|
|
|
std::string DbcParser::Trim(const std::string &text) {
|
|
return TrimText(text);
|
|
}
|