#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } static QString firstline; static std::vector str; static std::vector http; static std::vector ebitda; static std::vector newstr; static std::vector newhttp; static unsigned long logNum=0; bool MainWindow::readLog(){//Remember to edit as name editable. char fileName[40] = "access_test.log"; QString input=""; input = ui->lineEdit->text(); for (int i=0;i<40;i++) { if(input[i]!=nullptr&&input[i]!='\n'){ fileName[i]=input[i].toLatin1(); }else{ fileName[i]=NULL; break; } } FILE* f = fopen(fileName,"r"); if(f==nullptr) return false; int isNewline =1; logNum = 0; while(true){ char c; if(fscanf(f,"%c",&c)!=EOF){ if(c=='\n') { str[logNum++]+=c; isNewline = 1; continue; } if(isNewline==1){ str.push_back(""); isNewline=0; } if(c=='"'){ str[logNum]+=c; if(fscanf(f,"%c",&c)!=EOF){ if(c==' '){ //Start getting http code str[logNum]+=c;//Save that space int temp=0; fscanf(f,"%c",&c); while(c!=' '&&c<='9'&&c>='0'){ temp = temp*10+(c-48); str[logNum]+=c;//Save read number fscanf(f,"%c",&c);//Read next char } //temp is calculated if(temp>0) http.push_back(temp); //c == ' ' immediately after http code or not http code str[logNum]+=c;//Save that space }else if(c=='\n'){ str[logNum++]+=c; isNewline = 1; continue; }else{ str[logNum]+=c; } }else{ break; } }else{ str[logNum]+=c; } }else{ break; } } str[logNum]+='\n'; return true; } bool MainWindow::readCsv(){//Remember to edit as name editable. char fileName[40] = "access_test.log"; QString input=""; input = ui->lineEdit->text(); for (int i=0;i<40;i++) { if(input[i]!=nullptr&&input[i]!='\n'){ fileName[i]=input[i].toLatin1(); }else{ fileName[i]=NULL; break; } } if(input=="P1-SuperStoreUS-2015.csv"){ FILE* f = fopen(fileName,"r"); if(f==nullptr) return false; int isNewline =1; int isFirstLine = 1; logNum = 0; while(true){ char c; if(fscanf(f,"%c",&c)!=EOF){ if(isNewline==1){ str.push_back(""); isNewline=0; } if(c=='\n') { if(isFirstLine==1){ isFirstLine =0; isNewline = 1; firstline+=c; }else{ str[logNum++]+=c; isNewline = 1; } //Start getting EBI long long temp=0; bool negative = false; fscanf(f,"%c",&c); while((c!=','&&c<='9'&&c>='0')||c=='-'){ if(c!='-'){ temp = temp*10+(c-48); }else{ negative=true; } str[logNum]+=c;//Save read number fscanf(f,"%c",&c);//Read next char } if(negative==true){ temp*=(-1); } //temp is calculated ebitda.push_back(temp); //c == ',' immediately after http code or not http code str[logNum]+=c;//Save that comma }else{ if(isFirstLine==1){ firstline+=c; }else{ str[logNum]+=c; } } }else{ break; } } return true; }else{ FILE* f = fopen(fileName,"r"); if(f==nullptr) return false; int isNewline =1; int isFirstLine = 1; int commaCount = 0; logNum = 0; while(true){ char c; if(fscanf(f,"%c",&c)!=EOF){ if(isNewline==1){ str.push_back(""); isNewline=0; } if(c=='\n') { if(isFirstLine==1){ isFirstLine =0; commaCount = 0; isNewline = 1; firstline+=c; continue; } str[logNum++]+=c; commaCount = 0; isNewline = 1; continue; } if(c=='"'&&commaCount==2){ if(isFirstLine==1){ firstline+=c; continue; }else{ commaCount--; str[logNum]+=c; continue; } } if(c==','){ if(isFirstLine==1){ firstline+=c; }else{ str[logNum]+=c; commaCount++; if(commaCount==10){ //Start getting EBI long long temp=0; bool negative = false; fscanf(f,"%c",&c); while((c!=','&&c<='9'&&c>='0')||c=='-'){ if(c!='-'){ temp = temp*10+(c-48); }else{ negative=true; } str[logNum]+=c;//Save read number fscanf(f,"%c",&c);//Read next char } if(negative==true){ temp*=(-1); } //temp is calculated ebitda.push_back(temp); //c == ',' immediately after http code or not http code str[logNum]+=c;//Save that comma } } }else{ if(isFirstLine==1){ firstline+=c; }else{ str[logNum]+=c; } } } else{ break; } } //str[logNum]+='\n'; return true; } } void MainWindow::countingSort(){ //Sort the log by http status code unsigned long count[500]={0};//index+100 = status code or status code-100 =index //Count the frequency for (unsigned long i =0;i< logNum+1;i++) { count[http[i]-100]+=1; } //Calculate frequency for (int i =1;i<500;i++) { count[i]+=count[i-1]; } //Placing item in new array newstr.resize(logNum+1); newhttp.resize(logNum+1); for (unsigned long i =0;i< logNum+1;i++) { newstr[count[http[i]-100]-1] = ""+str[i]; newhttp[count[http[i]-100]-1] = http[i]; count[http[i]-100]--; } } void MainWindow::shellSort(){ // Start with a big gap, then reduce the gap for (unsigned long gap = (logNum)/2; gap > 0; gap /= 2) { // Do a gapped insertion sort for this gap size. // The first gap elements a[0..gap-1] are already in gapped order // keep adding one more element until the entire array is // gap sorted for (unsigned long i = gap; i < logNum; i += 1) { // add a[i] to the elements that have been gap sorted // save a[i] in temp and make a hole at position i long long temp = ebitda[i]; QString tempStr = str[i]; // shift earlier gap-sorted elements up until the correct // location for a[i] is found unsigned long j; for (j = i; j>=gap && ebitda[j - gap] > temp; j -= gap){ ebitda[j] = ebitda[j - gap]; str[j] = str[j - gap]; // put temp (the original a[i]) in its correct location } ebitda[j] = temp; str[j]=tempStr; } } /*unsigned long span, i; span = (logNum-1)/2; while(span>=1){ for(i = 1; i<((logNum-1)-span); i++){ if(ebitda[i]>ebitda[i+span]){ //swap(&A[i], &A[i+span]); QString tempStr = str[i]; long long temp = ebitda[i]; str[i]=str[i+span]; ebitda[i]=ebitda[i+span]; str[i+span]=tempStr; ebitda[i+span]=temp; } } span = span/2; }*/ } void MainWindow::on_pushButton_clicked() { if(readLog()==false) return; countingSort(); //Printing Result QString outputStr=""; for (unsigned long i =0;i< logNum+1;i++) { if(newhttp[i]>=100&&newhttp[i]<=199) outputStr+=""+newstr[i]+""+"
"; if(newhttp[i]>=200&&newhttp[i]<=299) outputStr+=""+newstr[i]+""+"
"; if(newhttp[i]>=300&&newhttp[i]<=399) outputStr+=""+newstr[i]+""+"
"; if(newhttp[i]>=400&&newhttp[i]<=499) outputStr+=""+newstr[i]+""+"
"; if(newhttp[i]>=500&&newhttp[i]<=599) outputStr+=""+newstr[i]+""+"
"; }/* for (unsigned long i =0;i< logNum+1;i++) { outputStr+=QString::number(newhttp[i])+"\n"; }*/ ui->textBrowser->setText(outputStr); //ui->textBrowser->setText(QString::number(logNum)); } void MainWindow::on_pushButton_2_clicked() { if(readCsv()==false) return; shellSort(); //Printing Result QString outputStr=""; for (unsigned long i =0;i< logNum;i++) { if(ebitda[i]<0) outputStr+=""+str[i]+""+"
"; if(ebitda[i]==0) outputStr+=""+str[i]+""+"
"; if(ebitda[i]>0&&ebitda[i]<10000000000) outputStr+=""+str[i]+""+"
"; if(ebitda[i]>=10000000000) outputStr+=""+str[i]+""+"
"; } //Output csv FILE* f = fopen("output.csv","w"); fprintf(f,firstline.toUtf8()); for (int i=0;itextBrowser->setText(outputStr); } void MainWindow::on_pushButton_3_clicked() {//Reset firstline=""; logNum=0; str.clear(); http.clear(); ebitda.clear(); newstr.clear(); newhttp.clear(); //Clear Display ui->textBrowser->setText(""); }