irda-app-file-parser.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "irda-app-file-parser.hpp"
  2. std::unique_ptr<IrdaAppFileParser::IrdaFileMessage> IrdaAppFileParser::read_message(File* file) {
  3. while(1) {
  4. auto str = getline(file);
  5. if(str.empty()) return nullptr;
  6. auto message = parse_message(str);
  7. if(message) return message;
  8. }
  9. }
  10. std::unique_ptr<IrdaAppFileParser::IrdaFileMessage>
  11. IrdaAppFileParser::parse_message(const std::string& str) const {
  12. char protocol_name[32];
  13. uint32_t address;
  14. uint32_t command;
  15. auto irda_file_message = std::make_unique<IrdaFileMessage>();
  16. int parsed = std::sscanf(
  17. str.c_str(),
  18. "%31s %31s A:%lX C:%lX",
  19. irda_file_message->name,
  20. protocol_name,
  21. &address,
  22. &command);
  23. if(parsed != 4) {
  24. return nullptr;
  25. }
  26. IrdaProtocol protocol = irda_get_protocol_by_name(protocol_name);
  27. if(!irda_is_protocol_valid((IrdaProtocol)protocol)) {
  28. return nullptr;
  29. }
  30. int address_length = irda_get_protocol_address_length(protocol);
  31. uint32_t address_mask = (1LU << (4 * address_length)) - 1;
  32. if(address != (address & address_mask)) {
  33. return nullptr;
  34. }
  35. int command_length = irda_get_protocol_command_length(protocol);
  36. uint32_t command_mask = (1LU << (4 * command_length)) - 1;
  37. if(command != (command & command_mask)) {
  38. return nullptr;
  39. }
  40. irda_file_message->message = {
  41. .protocol = protocol,
  42. .address = address,
  43. .command = command,
  44. .repeat = false,
  45. };
  46. return irda_file_message;
  47. }