Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <map>
- using namespace std;
- using isstream = istringstream;
- vector<string> split_stream(istream &strm, const char delim)
- {
- vector<string> result;
- string buf;
- while (std::getline(strm, buf, delim))
- {
- result.push_back(buf);
- }
- return result;
- };
- vector<string> split(const string &str, const char delim)
- {
- isstream isstrm(str);
- return split_stream(isstrm, delim);
- };
- pair<string, string> split_by_first(const string &str, const char delim)
- {
- size_t index = str.find_first_of(delim);
- return pair<string, string>(str.substr(0, index), str.substr(index + 1));
- };
- pair<string, map<string, string>> parse(const string &str)
- {
- map<string, string> result;
- pair<string, string> tmp = split_by_first(str, ' '); //first in pair is event type
- vector<string> properties = split(tmp.second, ',');
- for (string i : properties)
- {
- pair<string, string> tmp2 = split_by_first(i, '=');
- result.insert(pair<string, string>(tmp2.first, tmp2.second)); //second in pair is map of properties
- }
- return pair<string, map<string, string>>(tmp.first, result);
- };
- vector<pair<string, map<string, string>>> parse_all(const vector<string> &strings)
- {
- vector<pair<string, map<string, string>>> result;
- for (string i : strings)
- {
- result.push_back(parse(i));
- }
- return result;
- }
- void print_out(vector<pair<string, map<string, string>>> &events)
- {
- for (pair<string, map<string, string>> e : events)
- {
- cout << "Event: " << e.first << endl;
- for (pair<string, string> h : e.second)
- {
- cout << '\t' << h.first << ": " << h.second << endl;
- }
- }
- };
- int main()
- {
- string answer("./raw.log");
- cout << "path to file with raw logs (./raw.log): ";
- std::cin >> answer;
- ifstream raw_logs(answer);
- if (!raw_logs.is_open())
- {
- cout << "ERROR: cannot open file." << endl;
- return 1;
- }
- auto parsed_events = parse_all(split_stream(raw_logs, '\n'));
- cout << "Parsed events:\n" << endl;
- print_out(parsed_events);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement