#include #include #include #include #include #include "vt100.h" using namespace std; struct Param { string key; string value; }; struct Request { string url; string timestamp; vector params; }; struct PreparedLog { vector requests; vector columns; float total_lines; }; void unique_add(vector& v, string& s) { unsigned int i; bool found = false; for (i=0; i < v.size(); i++) { if (v.at(i) == s) { found = true; } } if (!found) { v.push_back(s); } } string get_param(vector v, string key) { unsigned int i; for (i=0; i < v.size(); i++) { Param e = v.at(i); if (key == e.key) { return e.value; } } return ""; } PreparedLog prepareLog(istream& input_stream) { string line; unsigned int linenum = 0; PreparedLog return_log; while (getline (input_stream, line)) { linenum++; // unique struct for line Request this_request; istringstream linestream(line); string item; int itemnum = 0; while (getline (linestream, item, ' ')) { itemnum++; if (itemnum == 4) { this_request.timestamp = item.substr(1); } if (itemnum == 7) { int qloc = item.find_first_of("?"); string url_params; string url; if (qloc != string::npos) { this_request.url = item.substr(0, qloc); url_params = item.substr(qloc+1); int paramnumber = 0; istringstream paramstream(url_params); string param; // Check parameters to see what kind of colums we will have to create while (getline (paramstream, param, '&')) { int eqloc = param.find_first_of("="); if (eqloc != string::npos) { Param x; x.key = param.substr(0,eqloc); x.value = param.substr(eqloc+1); this_request.params.push_back(x); // if not in column db add it unique_add(return_log.columns, x.key); } } } else { // No Parameters lets just get the url this_request.url = item; } return_log.requests.push_back(this_request); } } } return_log.total_lines = linenum; return return_log; } void write_csv(PreparedLog& plog, ostream& outfile) { outfile << "\"timestamp\",\"url\","; unsigned int c; for (c=0; c < plog.columns.size(); c++) { string f = plog.columns.at(c); outfile << "\"" << f << "\""; if (c+1 < plog.columns.size()) { outfile << ","; } } outfile << endl; unsigned int i; for (i=0; i < plog.requests.size(); i++) { Request n = plog.requests.at(i); outfile << "\"" << n.timestamp << "\"," << "\"" << n.url << "\","; unsigned int b; for (b=0; b < plog.columns.size(); b++) { string e = plog.columns.at(b); outfile << "\"" << get_param(n.params, e) << "\""; if (b+1 < plog.columns.size()) { outfile << ","; } } outfile << endl; } } string view_vector(vector& v) { unsigned int i; ostringstream oss; for (i=0; i < v.size(); i++) { oss << v.at(i); if (i+1 < v.size()) { oss << ", "; } } return oss.str(); } int main (int argc, char *argv[]) { int i; bool std_in = false; bool std_out = false; bool show_help = true; bool input_set = false; bool output_set = false; bool quiet = false; string outFilename; string inFilename; for (i=0; i