Closes #11
This commit is contained in:
1 parent
0067cb4ddd
commit
f17ddb6aad
104 files changed
+1928
-889
No files matched your search
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* @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 {
|
||||
dispatcher.Dispatch(line, context);
|
||||
} catch (const std::exception &error) {
|
||||
throw std::runtime_error(filePath + ":" + std::to_string(lineNumber) +
|
||||
": " + error.what());
|
||||
}
|
||||
}
|
||||
|
||||
return database;
|
||||
}
|
||||
|
||||
std::string DbcParser::Trim(const std::string &text) {
|
||||
return TrimText(text);
|
||||
}
|
||||
Reference in new issue
Block a user