Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <fstream>
- #include <string.h>
- #include <dirent.h>
- #include <sstream>
- #include <vector>
- #include <thread>
- /*g++ -std=c++11 -pthread main.cpp */
- using namespace std;
- volatile int lock = 0;
- int SIZE =0;
- volatile int *contThread;
- int indice = -1;
- void write(vector<string> filenames);
- void readFile(string filename);
- void* readFiles(char directory[], vector<string> *filenames);
- class Requisition
- {
- string strGet_m;
- string strReq_m;
- public:
- Requisition(string strGet, string strReq)
- {
- this->strGet_m = strGet;
- this->strReq_m = strReq;
- }
- string getStrGet()
- {
- return strGet_m;
- }
- string getStrReq()
- {
- return strReq_m;
- }
- };
- /*void readReqs()
- {
- ifstream myFile(filename);
- cout << "\n\n\n*****************\n\n\n"<<endl;
- if(!myFile.is_open())
- {
- cout << "\n\n Unable to open file \n\n"<<endl;
- exit(0);
- }
- else
- {
- myFile.seekg(0,myFile.end);
- long size = myFile.tellg();
- myFile.seekg (0);
- char* buffer = new char[size];
- myFile.read(buffer,size);
- myFile.close();
- cout << "size is: " << size << " bytes.\n"<<endl;
- //writeReqs(filename,size);
- }
- } */
- void readAnsw(string filename, string reqname)
- {
- ifstream myFile(("/home/abraao/abraao/LP2/trabalho_2-Sublime/files/" +filename), ifstream::binary);
- cout << "\n\n\n*****************\n\n\n"<<endl;
- reqname.replace((reqname.size()-4), reqname.length(), "");
- // cout << filename << " " << reqname <<endl;
- if(!myFile.is_open())
- {
- cout << "\n\n Unable to open file \n\n"<<endl;
- exit(0);
- }
- else
- {
- /* granvando na pasta*/
- string arqAll;
- ofstream myfileWrite(( "/home/abraao/abraao/LP2/trabalho_2-Sublime/answ/" + reqname + ".ans") );
- while ( !myFile.eof() )
- {
- getline (myFile, arqAll);
- myfileWrite << arqAll;
- myfileWrite << "\n";
- // cout<<"ddfkdskjjdskfkjds" <<endl;
- }
- myFile.close();
- SIZE += myfileWrite.tellp();
- cout<< SIZE <<endl;
- /*int size = myfileWrite.tellp();
- cout << "size is: " << size << " bytes.\n"<<endl; */
- myfileWrite.close();
- //cout << "size is: " << size << " bytes.\n"<<endl;
- //write(string filename);
- }
- }
- void* readFiles(char directory[], vector<string> *filenames)
- {
- DIR *dir = NULL;
- struct dirent *drnt = NULL;
- string *filesArr, str;
- int aux = 0;
- int tamanho;
- dir = opendir(directory);
- char *diretorio = NULL;
- if(dir)
- {
- while(drnt = readdir(dir)){aux++;}
- filesArr = new string[aux+1];
- aux = 0;
- seekdir(dir, 0);
- int tamanho;
- while(drnt = readdir(dir))
- {
- str = (string)directory + "/" + (string)drnt->d_name;
- diretorio = (char *) malloc(sizeof(char)*30);
- strcpy(diretorio,drnt->d_name);
- tamanho = strlen(diretorio);
- if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='t')&&(diretorio[tamanho-2]=='x')&&(diretorio[tamanho-1]=='t'))
- {
- filenames->push_back( (string)drnt->d_name );
- }
- if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='j')&&(diretorio[tamanho-2]=='p')&&(diretorio[tamanho-1]=='g'))
- {
- filenames->push_back( (string)drnt->d_name );
- }
- if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='p')&&(diretorio[tamanho-2]=='n')&&(diretorio[tamanho-1]=='g'))
- {
- filenames->push_back( (string)drnt->d_name );
- }
- if((diretorio[tamanho-4]=='.')&&(diretorio[tamanho-3]=='r')&&(diretorio[tamanho-2]=='e')&&(diretorio[tamanho-1]=='q'))
- {
- filenames->push_back( (string)drnt->d_name );
- }
- }
- }
- else
- {
- cout << "Não foi possível abrir essa pasta " << directory << "\n";
- }
- closedir(dir);
- }
- vector <Requisition> readReqs(vector<string> filenamesReqs, vector<string> filenames)
- {
- vector <Requisition> vR;
- string strGet,strReq,strFile;
- int i=0;
- for (int i = 0; i < filenamesReqs.size(); ++i)
- {
- strFile = "/home/abraao/abraao/LP2/trabalho_2-Sublime/reqs/" + filenamesReqs[i];
- //cout<< strFile <<endl;
- ifstream myfile(strFile);
- while (getline( myfile, strGet, ' ') )
- {
- getline ( myfile, strReq);
- //cout << "GET: " << strGet <<endl;;
- // getline(myfile, strReq) ;
- //cout << "requisition " << strGet <<" ---"<< strReq << " FIM"<<endl;
- vR.push_back(Requisition(strGet,strReq ));
- }
- myfile.close();
- }
- return vR;
- }
- void _unlock()
- {
- __sync_synchronize();
- lock = 0;
- }
- void _lock()
- {
- while(__sync_lock_test_and_set(&lock, 1));
- }
- void *monitor(volatile int *contThr, int tam)
- {
- for(int i = 0; i < tam; i++)
- {
- while(contThr[i] != 1);
- }
- for(int i = 0; i < tam; i++)
- {
- contThr[i] = 0;
- }
- }
- /*void* openFile(std::vector<Requisition> vR, vector<string> filenames,vector<string> filenamesReqs, int i)
- {
- // _lock();
- for (int j = 0; j < filenames.size(); ++j)
- {
- if(vR[i].getStrGet()=="GET")
- {
- if(vR[i].getStrReq()== filenames[j])
- {
- //cout<< "i= " <<i << "j= " << j <<vR[i].getStrGet()<< "FIM" <<endl;
- //cout<< "i= " <<i << "j= " << j <<vR[i].getStrReq()<< "FIM" <<endl;
- readAnsw( filenames[j], filenamesReqs[i]);
- break;
- }
- }
- else
- {
- cout<< i << " " << j <<" ERROR BAD COMMAND" <<endl;
- break;
- }
- }
- indice++;
- // _unlock();
- // __sync_fetch_and_add(&contThread[indice], 1);
- cout<<" SY " << i << " - " <<indice<<" "<< (int)contThread[indice] <<endl;
- //while(contThread[indice] == 1);
- cout<<" MERDAA "<<"valor = " << indice <<endl;
- }*/
- void openFile(std::vector<Requisition> vR, vector<string> filenames,vector<string> filenamesReqs)
- {
- for (int i = 0; i < vR.size(); ++i)
- {
- for (int j = 0; j < filenames.size(); ++j)
- {
- if(vR[i].getStrGet()=="GET")
- {
- cout<<filenames[j] <<" "<<vR[i].getStrReq() <<endl;
- if(filenames[j] == vR[i].getStrReq())
- {
- cout<< "i= " <<i << "j= " << j <<vR[i].getStrGet()<< "FIM" <<endl;
- cout<< "i= " <<i << "j= " << j <<vR[i].getStrReq()<< "FIM" <<endl;
- readAnsw( filenames[j], filenamesReqs[i]);
- break;
- }
- }
- else
- {
- cout <<" ERROR BAD COMMAND" <<endl;
- break;
- }
- }
- }
- lock = 0;
- }
- int main(int argc, char const *argv[])
- {
- /* code */
- //readFile("/home/abraao/abraao/LP2/Trabalho2/files/Homem-de-Ferro-1920x1080.jpg");
- char files[] = "files";
- vector<string> filenames,filenamesReqs;
- readFiles(files,&filenames);
- /*for (int i = 0; i <filenames.size(); ++i)
- {
- cout<< filenames[i] <<endl;
- } */
- char reqs[] = "reqs";
- readFiles(reqs,&filenamesReqs);
- /*for (int i = 0; i <filenamesReqs.size(); ++i)
- {
- cout <<filenamesReqs[i] <<endl;
- }*/
- //cout <<"\n********************"<<endl;
- vector<Requisition> vR = readReqs(filenamesReqs, filenames);
- /*for (int i = 0; i <vR.size(); ++i)
- {
- cout << vR[i].getStrGet() << " " <<vR[i].getStrReq()<< " dasas" << endl;
- }*/
- //openFile(vR, filenames,filenamesReqs);
- //cout << vR.size();
- //contThread = new int[vR.size()];
- // thread *threadReqs = new thread[vR.size()];
- openFile(vR,filenames,filenamesReqs);
- cout<< "\n Numeros de Bytes processado " << SIZE << endl;
- //for (int i = 0; i < vR.size(); ++i)
- //{
- //cout<< i <<" = "<< vR.size() << endl;
- //threadReqs[i] = thread(openFile, vR,filenames,filenamesReqs, i);
- // threadReqs[i].join();
- //}
- //thread threadMonitor(monitor, contThread , vR.size() );
- //threadMonitor.join();
- //for(int j = 0; j < vR.size(); j++)
- //{
- // threadReqs[j].join();
- //
- //openFile(vR,filenames);
- //readReqs(filename[0]);
- // Constructs the new thread and runs it. Does not block execution.
- //vector<thread> vT1;
- //vT1.push_back( thread(r, files, &filenames) );
- // Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
- //vT1[0].join();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement