Skip to content

Commit 55cc2b0

Browse files
author
Avi SZYCHTER
committed
Warnings are now inserted into an optional data structure instead of being directly printed to the standard error
This allows one to parse a DBC with or without considering the warnings and clears the outputs in the console
1 parent e914afd commit 55cc2b0

File tree

6 files changed

+128
-114
lines changed

6 files changed

+128
-114
lines changed

include/CANDatabase.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,33 @@
2424
* retrieve the name of the source file.
2525
*/
2626
class CANDatabase {
27+
public:
28+
/**
29+
* @brief A parsing warning and its location
30+
*/
31+
struct parsing_warning {
32+
int line;
33+
std::string description;
34+
};
35+
2736
public:
2837
/**
2938
* @brief Parse a CANDatabase from the given source file.
3039
* @param filename Path to the file to parse
40+
* @param warnings (Optional) Filled with all the warnings found during the parsing
3141
* @throw CANDatabaseException if the parsing failed
3242
*/
33-
static CANDatabase fromFile(const std::string& filename);
43+
static CANDatabase fromFile(
44+
const std::string& filename, std::vector<parsing_warning>* warnings = nullptr);
3445

3546
/**
3647
* @brief Construct a CANDatabase object from a database described by src_string
3748
* @param src_string Source string to parse
49+
* @param warnings (Optional) Filled with all the warnings found during the parsing
3850
* @throw CANDatabaseException if the parsing failed
3951
*/
40-
static CANDatabase fromString(const std::string& src_string);
52+
static CANDatabase fromString(
53+
const std::string& src_string, std::vector<parsing_warning>* warnings = nullptr);
4154

4255
public:
4356
struct IDKey {

include/DBCParser.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
#include <memory>
88

99
namespace DBCParser {
10-
CANDatabase fromTokenizer(const std::string& name,
11-
Tokenizer& tokenizer);
10+
CANDatabase fromTokenizer(
11+
const std::string& name, Tokenizer& tokenizer,
12+
std::vector<CANDatabase::parsing_warning>* warnings = nullptr);
1213

13-
CANDatabase fromTokenizer(Tokenizer& tokenizer);
14+
CANDatabase fromTokenizer(
15+
Tokenizer& tokenizer,
16+
std::vector<CANDatabase::parsing_warning>* warnings = nullptr);
1417
};
1518

1619
#endif

include/ParsingUtils.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
#include "Tokenizer.h"
55
#include <string>
66
#include <iostream>
7+
#include "CANDatabase.h"
78
#include "CANDatabaseException.h"
89

9-
void throw_error(const std::string& category, const std::string& description,
10-
unsigned long long line);
10+
void throw_error(
11+
const std::string& category, const std::string& description,
12+
unsigned long long line);
1113

12-
void warning(const std::string& description, unsigned long long line) ;
14+
void warning(
15+
std::vector<CANDatabase::parsing_warning>* warnings,
16+
const std::string& description, unsigned long long line);
1317

1418
const Token&
1519
assert_token(Tokenizer& tokenizer, const std::string& token);

src/models/CANDatabase.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ std::size_t CANDatabase::size() const {
1414
return map_.size();
1515
}
1616

17-
CANDatabase CANDatabase::fromFile(const std::string& filename) {
17+
CANDatabase CANDatabase::fromFile(const std::string& filename, std::vector<parsing_warning>* warnings) {
1818
std::ifstream test_stream(filename);
1919
if (!test_stream.good()) {
2020
throw CANDatabaseException("Cannot find file " + filename);
2121
}
2222

2323
FileTokenizer tokenizer(filename);
24-
return DBCParser::fromTokenizer(filename, tokenizer);
24+
return DBCParser::fromTokenizer(filename, tokenizer, warnings);
2525
}
2626

27-
CANDatabase CANDatabase::fromString(const std::string & src_string) {
27+
CANDatabase CANDatabase::fromString(const std::string & src_string, std::vector<parsing_warning>* warnings) {
2828
StringTokenizer tokenizer(src_string);
29-
return DBCParser::fromTokenizer(tokenizer);
29+
return DBCParser::fromTokenizer(tokenizer, warnings);
3030
}
3131

3232
const CANFrame& CANDatabase::at(const std::string& name) const {
@@ -50,16 +50,6 @@ CANFrame& CANDatabase::at(unsigned long long id) {
5050
}
5151

5252
void CANDatabase::addFrame(const CANFrame& frame) {
53-
if(strKeyIndex_.find(frame.name()) != strKeyIndex_.end()) {
54-
std::cout << "WARNING: Double declaration of a frame with name "
55-
<< "\"" << frame.name() << "\"" << std::endl;
56-
}
57-
58-
if(intKeyIndex_.find(frame.can_id()) != intKeyIndex_.end()) {
59-
std::cout << "WARNING: Double declaration of a frame with id "
60-
<< frame.can_id() << std::endl;
61-
}
62-
6353
IDKey map_key = { frame.name(), frame.can_id() };
6454

6555
map_.insert(std::make_pair(map_key, frame));

0 commit comments

Comments
 (0)