Advertisement
Queen4

Parsing logs 1.05

Jan 19th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <unordered_set>
  5. #include <map>
  6.  
  7. // Переменные для поиска времени
  8. struct MainTime {
  9.     int hour = 0;
  10.     int nexthour = 0;
  11.     int minute = 0;
  12.     int nextminute = 0;
  13.     float sec = 0.0;
  14.     float nextsec = 0.0;
  15.     float sum = 0.0;
  16. };
  17.  
  18. // Cкладывает по принципу XOR
  19. int XOR(const char* s) {
  20.     int c = 0;
  21.  
  22.     while (*s)
  23.         c ^= *s++;
  24.  
  25.     return c;
  26. }
  27.  
  28. int main() {
  29.     int comma, key; // Переменная для подсчета запятых и проверки контрольной суммы
  30.     MainTime time; //Структура времени
  31.     // Переменные для работы со строками
  32.     char linec_h[200];
  33.     std::string line, key_s;
  34.     // Работа с ведром номеров спутника
  35.     std::unordered_set<std::string> Set; //сет номеров спутника
  36.     std::string SatelliteNumber;
  37.     // Работа со среднем значением времени к каждому SNR
  38.     int SNRnumber;
  39.     std::map <int, float> Map;
  40.  
  41.     std::ifstream logs_("C:/Users/Ivan/Desktop/Logs.txt"); // Откуда берем данные
  42.     std::ofstream pout("C:/Users/Ivan/Desktop/SNR.txt"); // Куда загружаем данные о спутниках
  43.     std::ofstream tout("C:/Users/Ivan/Desktop/Avarage Time.txt"); // Куда загружаем данные о среднем времени
  44.  
  45.     if (logs_.is_open()) {
  46.         while (getline(logs_, line)) {
  47.  
  48.             if (line.substr(0, 10) == "RE005%off%") {
  49.                 pout << "End of cycle" << std::endl;
  50.             }
  51.             else if (line.substr(0, 9) == "RE004%on%") { // Если строка с включением, то
  52.                 pout << "Time of work: " << time.nexthour + time.nextminute + time.nextsec << " sec" << std::endl; // Выводим время работы цикла
  53.                 // Обнуляем переменные времени
  54.                 time.hour = 0;
  55.                 time.minute = 0;
  56.                 time.sec = 0.0;
  57.                 time.nexthour = 0;
  58.                 time.nextminute = 0;
  59.                 time.nextsec = 0.0;
  60.                 // Очищаем сет
  61.                 Set.clear();
  62.             }
  63.             else {
  64.                 //Проверяем контрольную сумму
  65.                 key_s = line.substr(line.length() - 2, 2);
  66.                 key = strtol(key_s.c_str(), nullptr, 16);
  67.                 line = line.substr(1, line.length() - 4);
  68.  
  69.                 strcpy_s(linec_h, line.c_str());
  70.                 if (line != "E00" && key != XOR(linec_h)) pout << "Line is corrupted!" << std::endl;
  71.                 else { //Если выполняется, то
  72.                     comma = 0;
  73.                     // Находим время, переводим в секунды, находим отношение первого значения и всех следующих
  74.                     if (line.substr(0, 5) == "GPGGA") {
  75.                         if (time.hour == 0 && time.minute == 0 && time.sec == 0.0) {
  76.                             time.hour = stoi(line.substr(6, 2)) * 3600;
  77.                             time.minute = stoi(line.substr(8, 2)) * 60;
  78.                             time.sec = stof(line.substr(10, 4));
  79.                         }
  80.                         else {
  81.                             time.nexthour = stoi(line.substr(6, 2)) * 3600 - time.hour;
  82.                             time.nextminute = stoi(line.substr(8, 2)) * 60 - time.minute;
  83.                             time.nextsec = stof(line.substr(10, 4)) - time.sec;
  84.                         }
  85.                         time.sum = time.nexthour + time.nextminute + time.nextsec;
  86.                     }
  87.                     // Если строка GPGSV
  88.                     else if (line.substr(0, 5) == "GPGSV") {
  89.                         // Проходимся по строке
  90.                         for (size_t i = 0, N = 4, SNR = 7; i < line.size(); i++) {
  91.                             // Считаем количество запятых
  92.                             if (line[i] == ',') comma++;
  93.                             // При нахождении нужного количества
  94.                             if (comma == N) {
  95.                                 SatelliteNumber = line.substr(i - 1, 2);
  96.                             }
  97.                             else if (comma == SNR) {
  98.                                 if (Set.find(SatelliteNumber) == Set.end()) { // Если значение встречается в первый раз, то
  99.                                     SNRnumber = stoi(line.substr(i + 1, 2));
  100.                                     int count = 0;
  101.                                     /*if (Map.find(SNRnumber) == Map.end()) {
  102.                                         count++;
  103.                                     }
  104.                                     else {
  105.                                         count++;
  106.                                         time.sum /= count;
  107.                                         Map.at(SNRnumber) = time.sum;
  108.                                     }*/
  109.                                     Map.insert(std::pair<int, float>(SNRnumber, time.sum));
  110.                                     pout << "Satellite name: " << SatelliteNumber << "  " << "SNR: " << SNRnumber << "  Elapsed time : " << time.sum << " sec" << std::endl;
  111.                                     Set.insert(SatelliteNumber); // Добавляем его в сет
  112.  
  113.                                 }
  114.                                 N += 4;
  115.                                 SNR += 4;
  116.                             }
  117.                         }
  118.                     }
  119.                 }
  120.             }
  121.         }
  122.         logs_.close();
  123.  
  124.         for (auto & it : Map) {
  125.             tout << "SNR:  " << it.first << " Average time: " << it.second << " sec" << std::endl;
  126.         }
  127.  
  128.         std::cout << "Success" << std::endl;
  129.     }
  130.     else std::cout << "File is not open" << std::endl;
  131.     pout.close();
  132.     tout.close();
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement