Advertisement
Guest User

Untitled

a guest
Jul 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <vector>
  6. #include <fstream>
  7. #include <map>
  8.  
  9. using namespace std;
  10. using isstream = istringstream;
  11.  
  12. vector<string> split_stream(istream &strm, const char delim)
  13. {
  14.     vector<string> result;
  15.     string buf;
  16.     while (std::getline(strm, buf, delim))
  17.     {
  18.         result.push_back(buf);
  19.     }
  20.     return result;
  21. };
  22.  
  23. vector<string> split(const string &str, const char delim)
  24. {
  25.     isstream isstrm(str);
  26.     return split_stream(isstrm, delim);
  27. };
  28.  
  29. pair<string, string> split_by_first(const string &str, const char delim)
  30. {
  31.     size_t index = str.find_first_of(delim);
  32.     return pair<string, string>(str.substr(0, index), str.substr(index + 1));
  33. };
  34.  
  35. pair<string, map<string, string>> parse(const string &str)
  36. {
  37.     map<string, string> result;
  38.     pair<string, string> tmp = split_by_first(str, ' '); //first in pair is event type
  39.     vector<string> properties = split(tmp.second, ',');
  40.     for (string i : properties)
  41.     {
  42.         pair<string, string> tmp2 = split_by_first(i, '=');
  43.         result.insert(pair<string, string>(tmp2.first, tmp2.second)); //second in pair is map of properties
  44.     }
  45.     return pair<string, map<string, string>>(tmp.first, result);
  46. };
  47.  
  48. vector<pair<string, map<string, string>>> parse_all(const vector<string> &strings)
  49. {
  50.     vector<pair<string, map<string, string>>> result;
  51.     for (string i : strings)
  52.     {
  53.         result.push_back(parse(i));
  54.     }
  55.     return result;
  56. }
  57.  
  58. void print_out(vector<pair<string, map<string, string>>> &events)
  59. {
  60.     for (pair<string, map<string, string>> e : events)
  61.     {
  62.         cout << "Event: " << e.first << endl;
  63.         for (pair<string, string> h : e.second)
  64.         {
  65.             cout << '\t' << h.first << ": " << h.second << endl;
  66.         }
  67.     }
  68. };
  69.  
  70. int main()
  71. {
  72.     string answer("./raw.log");
  73.     cout << "path to file with raw logs (./raw.log): ";
  74.     std::cin >> answer;
  75.     ifstream raw_logs(answer);
  76.     if (!raw_logs.is_open())
  77.     {
  78.         cout << "ERROR: cannot open file." << endl;
  79.         return 1;
  80.     }
  81.     auto parsed_events = parse_all(split_stream(raw_logs, '\n'));
  82.     cout << "Parsed events:\n" << endl;
  83.     print_out(parsed_events);
  84.     system("pause");
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement