Advertisement
Queen4

Parsing logs 1.2

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