Log_Entry create_Log_Entry(const string&);
This function does the following:
The parameter is a log entry line.
Create a Log_Entry object.
Break the line into fields and subfields using split(). Several splits will be necessary.
Fill the data members with values.
Split on ' ' (space character) first, if the split does not give 10 strings set the number of bytes to 0 and return. (Some lines in the file may be bad.)
For string to int conversion use string streams.
vector<Log_Entry> parse(string);
This function does the following:
Open a log file specified by the parameter name (File I/O).
Read lines from the opened file.
Create a Log_Entry object passing the line just input.
Push each Log_Entry object onto a vector.
Return the vector of Log_Entrys.
void output_all(const vector<Log_Entry> &);
This function does the following:
Loop through the vector provided as the argument and output all information for each Log_entry object.
Format the output neatly in some manner, example output.
int byte_count(const vector<Log_Entry> &);
This function does the following:
Loop through the vector provided as the argument and sum the number_of_bytes members. The sum is returned.
void output_hosts(vector<Log_Entry>);
This function does the following:
Loop through the vector provided as the argument and output the host member of each Log_Entry object in the vector one per line.
vector<string> split(const string&, char);
This function does the following:
Takes a character parameter, uses the character as a separator in the string, and returns a vector of strings.
Ex. If the split function is given "abc:12345:xx" and ':' it will return a vector of 3 strings: "abc", "12345", and "xx".
One approach, a loop goes through the string, if the current character is not the splitter accumulate it into a string using string += char, if the current character is the splitter then push_back the string that holds the accumulated characters and clear the accumulator string variable.
There is a test function for split that can be run from the makefile. To run, for example:
./split abcx24567xqqq x
./split "abc 24567 qqq" " "
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include "log_view.hpp"
- #ifdef CS2_STRING
- #include "string.hpp"
- using cs33001::string;
- #else
- #include <string>
- using std::string;
- #endif
- using namespace std;
- vector<Log_Entry> parse(const string&)
- {
- string line;
- vector<Log_Entry> _line;
- ifstream myfile ("name.txt");
- if(!myfile)
- {
- cout << "Couldn't open file";
- }
- if (myfile.is_open())
- {
- while (! myfile.eof())
- {
- getline (myfile,line);
- Log_Entry _name(line);
- _line.push_back(_name);
- }
- myfile.close();
- }
- return _line;
- }
- vector<string> split(const string& entries, char x)
- {
- string entry;
- vector<string> _entry;
- int index;
- for(int i = 0, j = 0; i < entries.size(); i++)
- {
- index = entries.find(x,i);
- if (index == entries.size())
- {
- break;
- while (j != index)
- {
- entry += entries[j];
- j++;
- }
- i = i + j;
- _entry.push_back(entry);
- }
- // if(j != 9)
- // _entry[9] = 0;
- }
- return _entry;
- }
- int byte_count(const vector<Log_Entry> &_name)
- {
- int bytes = 0;
- for (int i = 0; i < _name.size(); i++)
- {
- bytes += _name[i].get_number_of_bytes();
- }
- return bytes;
- }
- void output_hosts(vector<Log_Entry> _name)
- {
- for(int i = 0; i < _name.size(); i++)
- {
- string a = _name[i].get_host();
- cout << "hostd: " << a[0] << endl;
- }
- }
- Log_Entry::create_Log_Entry(const string&)
- {
- vector<string> entries = split(entry, ' ');
- _host = entries[0];
- vector<string> dates = split(entries[3], '/');
- _date.day = dates[0];
- _date.month = dates[1];
- vector<string> times = split(dates[2], ':');
- istringstream my(times[0].c_str());
- int time = 0;
- my >> time;
- _date.year = time;
- istringstream my2(times[1].c_str());
- my2 >> time;
- _time.hour = time;
- istringstream my3(times[2].c_str());
- my3 >> time;
- _time.minute = time;
- istringstream my4(times[3].c_str());
- my4 >> time;
- _time.second = time;
- _request = entries[5];
- _status = entries[8];
- istringstream my5(entries[9].c_str());
- my5 >> time;
- _number_of_bytes = time;
- }
- void output_all(const vector<Log_Entry> &entries)
- {
- for(int i = 0; i < entries.size(); i++)
- {
- cout << "***********************************************" << endl;
- cout << "Date Day:" << entries[i].get_date().day << endl;
- cout << "Month :" << entries[i].get_date().month << endl;
- cout << "Year :" << entries[i].get_date().year << endl;
- cout << "Host :" << entries[i].get_host() << endl;
- cout << "------------------------------------------------" << endl;
- cout << "Time Hours:" << entries[i].get_time().hour << endl;
- cout << "Time Min :" << entries[i].get_time().minute << endl;
- cout << "Time Sec :" << entries[i].get_time().second << endl;
- cout << "Request :" << entries[i].get_request() << endl;
- cout << "Status :" << entries[i].get_status() << endl;
- cout << "Bytes :" << entries[i].get_number_of_bytes() << endl;
- cout << "***********************************************" << endl;
- }
- return;
- }