chenluhua1980
2025-12-10 5cd7a0bae083c1dcaf674772d8a71ff3b4c37102
SourceCode/Bond/Servo/AlarmManager.cpp
@@ -784,16 +784,33 @@
// 读取报警文件
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;
@@ -804,7 +821,6 @@
        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");
@@ -817,7 +833,6 @@
            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;
            }
@@ -826,7 +841,6 @@
            }
        }
        catch (const std::exception& e) {
            // 捕获并记录解析错误
            std::cerr << "Error parsing line: " << line << " - " << e.what() << std::endl;
            continue;
        }