| | |
| | | |
| | | // 读取报警文件 |
| | | bool AlarmManager::readAlarmFile(const std::string& filename) { |
| | | std::ifstream file(filename); |
| | | std::string line; |
| | | bool first_line = true; |
| | | |
| | | std::ifstream file(filename, std::ios::binary); |
| | | if (!file.is_open()) { |
| | | std::cerr << "Error opening file!" << std::endl; |
| | | return false; |
| | | } |
| | | |
| | | while (std::getline(file, line)) { |
| | | auto getline_cross = [](std::ifstream& f, std::string& out) -> bool { |
| | | out.clear(); |
| | | char ch; |
| | | while (f.get(ch)) { |
| | | if (ch == '\r') { |
| | | // 处理 \r\n 或 单独 \r |
| | | if (f.peek() == '\n') f.get(); |
| | | break; |
| | | } |
| | | else if (ch == '\n') { |
| | | break; |
| | | } |
| | | out.push_back(ch); |
| | | } |
| | | return !out.empty() || !f.eof(); |
| | | }; |
| | | |
| | | std::string line; |
| | | bool first_line = true; |
| | | |
| | | while (getline_cross(file, line)) { |
| | | if (first_line) { |
| | | first_line = false; |
| | | continue; |
| | |
| | | AlarmInfo alarm; |
| | | |
| | | try { |
| | | // 逐字段解析并验证 |
| | | if (!std::getline(ss, cell, ',')) throw std::runtime_error("Missing field: No"); |
| | | if (!std::getline(ss, alarm.strUnitID, ',')) throw std::runtime_error("Missing field: UnitID"); |
| | | if (!std::getline(ss, alarm.strUnitNo, ',')) throw std::runtime_error("Missing field: UnitNo"); |
| | |
| | | if (!std::getline(ss, alarm.strAlarmText, ',')) throw std::runtime_error("Missing field: AlarmText"); |
| | | if (!std::getline(ss, alarm.strDescription, ',')) throw std::runtime_error("Missing field: Description"); |
| | | |
| | | // 检查是否重复 |
| | | if (m_mapAlarm.find(alarm.nAlarmID) == m_mapAlarm.end()) { |
| | | m_mapAlarm[alarm.nAlarmID] = alarm; |
| | | } |
| | |
| | | } |
| | | } |
| | | catch (const std::exception& e) { |
| | | // 捕获并记录解析错误 |
| | | std::cerr << "Error parsing line: " << line << " - " << e.what() << std::endl; |
| | | continue; |
| | | } |