You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbc_iterator.cpp 4.5 kB

2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
11 months ago
1 year ago
11 months ago
1 year ago
11 months ago
11 months ago
11 months ago
11 months ago
1 year ago
1 year ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "dbcc/dbc_iterator.h"
  2. #include <fstream>
  3. #include <limits>
  4. namespace ad {
  5. namespace dbcc {
  6. constexpr const char* kPreambleNode = "BU_";
  7. constexpr const char* kPreambleValueTable = "VAL_TABLE_";
  8. constexpr const char* kPreambleMessage = "BO_";
  9. constexpr const char* kPreambleSignal = "SG_";
  10. constexpr const char* kPreambleComment = "CM_";
  11. constexpr const char* kPreambleAttribute = "BA_";
  12. constexpr const char* kPreambleAttributeDefinition = "BA_DEF_";
  13. constexpr const char* kPreambleEnvironmentVariable = "EV_";
  14. DbcIterator::DbcIterator(const std::string& filePath)
  15. {
  16. std::ifstream file(filePath);
  17. if (file) {
  18. Parse(file);
  19. }
  20. file.close();
  21. }
  22. DbcIterator::DbcIterator(std::istream& stream)
  23. {
  24. Parse(stream);
  25. }
  26. std::ostream& operator<<(std::ostream& out, DbcIterator& dbc)
  27. {
  28. for (auto& msg : dbc) {
  29. out << msg.Name() << " " << msg.Id() << std::endl;
  30. for (auto& sig : msg) {
  31. out << "Signal: " << sig.Name() << " [" << (sig.IsFloat() ? "float" : "int") << "] ";
  32. out << "To: ";
  33. for (auto to : sig.To()) {
  34. out << to << ", ";
  35. }
  36. out << sig.StartBit() << "," << sig.Length() << std::endl;
  37. out << "(" << sig.Factor() << ", " << sig.Offset() << ")" << std::endl;
  38. out << "[" << sig.Minimum() << ", " << sig.Maximum() << "]" << std::endl;
  39. if (sig.GetMultiplexor() == Multiplexor::kMultiplexed) {
  40. out << "#" << sig.MultiplexedNumber() << "#" << std::endl;
  41. } else if (sig.GetMultiplexor() == Multiplexor::kMultiplexor) {
  42. out << "+Multiplexor+" << std::endl;
  43. }
  44. out << std::endl;
  45. }
  46. }
  47. return out;
  48. }
  49. inline std::string ExtractQuotedContent(std::istream& is)
  50. {
  51. std::string content;
  52. char ch;
  53. bool in_quotes = false;
  54. while (is.good()) {
  55. is >> std::noskipws >> ch;
  56. if (ch == '"') {
  57. if (in_quotes) {
  58. break;
  59. } else {
  60. in_quotes = true;
  61. continue;
  62. }
  63. }
  64. if (in_quotes) {
  65. content.push_back(ch);
  66. }
  67. }
  68. return content;
  69. }
  70. void DbcIterator::Parse(std::istream& stream)
  71. {
  72. /**
  73. * Here I will guarantee each parser will always process to the EOL.
  74. * So the preamble will always be the first word also.
  75. */
  76. std::string preamble;
  77. messages_.clear();
  78. do {
  79. stream >> preamble;
  80. if (preamble == kPreambleMessage) {
  81. Message msg;
  82. stream >> msg;
  83. if (stream.fail()) {
  84. stream.clear();
  85. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  86. }
  87. if (msg.SignalCount() > 0) {
  88. messages_.emplace_back(msg);
  89. message_index_[msg.Id()] = static_cast<uint32_t>(messages_.size() - 1);
  90. }
  91. } else if (preamble == kPreambleComment) {
  92. // TODO(anjingyu): Parse the comment
  93. // std::cout << "[W] CM_ is unsupported now, ignore!" << std::endl;
  94. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  95. } else if (preamble == kPreambleValueTable) {
  96. // std::cout << "[W] VAL_TABLE_ is unsupported now, ignore!" << std::endl;
  97. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  98. } else if (preamble == kPreambleAttribute) {
  99. // std::cout << "[W] BA_ is unsupported now, ignore!" << std::endl;
  100. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  101. } else if (preamble == kPreambleAttributeDefinition) {
  102. // std::cout << "[W] BA_DEF_ is unsupported now, ignore!" << std::endl;
  103. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  104. } else if (preamble == kPreambleEnvironmentVariable) {
  105. // std::cout << "[W] EV_ is unsupported now, ignore!" << std::endl;
  106. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  107. } else if (preamble == kPreambleNode) {
  108. // std::cout << "[W] BU_ is unsupported now, ignore!" << std::endl;
  109. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  110. } else {
  111. // std::cout << "[W] " << preamble << " is unsupported now, ignore!" << std::endl;
  112. stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  113. }
  114. } while (!stream.eof());
  115. }
  116. } // namespace dbcc
  117. } // namespace ad