Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <fstream>
- #include <vector>
- #include <iomanip>
- using namespace std;
- int iColumnToCompare;
- int iColumnSize[3];
- struct SDataRecord
- {
- string Column[3];
- };
- int compare(const void *a, const void *b){
- const string first =((SDataRecord *)a)->Column[iColumnToCompare];
- const string second =((SDataRecord *)b)->Column[iColumnToCompare];
- return (first.compare(second));
- }
- void print_one_row(SDataRecord a, int b){
- for(int i=0; i<b;i++){
- cout<<left<<setw(iColumnSize[i])<<a.Column[i]<<"\t";
- }
- cout<<endl;
- }
- void print_one_row_to_f(SDataRecord a, int b, fstream* f){
- for(int i=0; i<b;i++){
- *f<<left<<setw(iColumnSize[i])<<a.Column[i]<<"\t";
- }
- *f<<endl;
- }
- void display_help(){
- cout<<"/? - wyswietla ta pomoc"<<endl;
- cout<<"/i[:][patch] - ustawia sciezke pliku wejsciowego na patch"<<endl;
- cout<<"/o[:][patch] - ustawia sciezke pliku wyjsiowego na patch"<<endl;
- cout<<"/s[:][-][columname] - sortuje po kolumnie o nazwie columnname, jezeli podano '-' to sortowanie odbywa sie w odwrotnej kolejnosci"<<endl;
- cout<<"/q - ustawia program w trybie cichym"<<endl;
- }
- string remove_semicolon(string b){
- int p=2;
- if(b[2]==':') p++;
- return b.substr(p);
- }
- int main(int argc, char* argv[])
- {
- //parametry
- string sInputFilePatch;
- bool bReverseSort = false;
- bool bSilenMode = false;
- bool bOutputToFile=false;
- string sOutputPatch, sSortBy;
- //elementy niezbedne do dzialania
- vector<SDataRecord> vBase;
- fstream FInputFile, FOutputFile;
- string sInputFileLine;
- for(int i=1; i<argc; i++){
- if(argv[i][0]=='/'){
- switch(toupper(argv[i][1]))
- {
- case 'I': //input filename
- {
- sInputFilePatch=argv[i];
- sInputFilePatch=remove_semicolon(sInputFilePatch);
- }
- break;
- case 'Q': //silent
- {
- bSilenMode=true;
- }
- break;
- case 'S':
- {
- sSortBy=argv[i];
- sSortBy=remove_semicolon(sSortBy);
- if(sSortBy[0]=='-'){
- bReverseSort=true;
- sSortBy=sSortBy.substr(1);
- }
- }
- break;
- case 'O':
- {
- sOutputPatch=argv[i];
- sOutputPatch=remove_semicolon(sOutputPatch);
- bOutputToFile=true;
- }
- break;
- case '?':
- {
- display_help();
- cin.get();
- return 0;
- }
- break;
- default:
- {
- cout<<"Nieznany parametr: "<<argv[i][1];
- cin.get();
- return 0;
- }
- }} else {
- cout<<"Nieznany parametr! Uzyj: /?";
- cin.get();
- return 0;
- }
- }
- if(sInputFilePatch == sOutputPatch){
- cout<< "Plik Wejsciowy i wyjsciowy jest taki sam!"<<endl;
- cin.get();
- return 0;
- }
- if(bOutputToFile){
- FOutputFile.open(sOutputPatch.c_str(), fstream::out);
- if(!FOutputFile.good()) {
- cout<<"Nie dam rady otworzyc pliku wyjsciowego";
- cin.get();
- return 0;
- }
- }
- FInputFile.open(sInputFilePatch.c_str(), fstream::in);
- if(FInputFile.good()){
- while(!FInputFile.eof()){
- getline(FInputFile, sInputFileLine);
- size_t begin=0, end=0;
- sInputFileLine.append("\t");
- SDataRecord SRecord;
- for(int i=0; i<3; i++){
- end = sInputFileLine.find('\t', begin);
- if(end ==0) break;
- SRecord.Column[i] = sInputFileLine.substr(begin, end-begin);
- if(iColumnSize[i]<SRecord.Column[i].size())iColumnSize[i]=SRecord.Column[i].size();
- if(!(end >= (sInputFileLine.size()-1))){
- begin=end+1;
- } else {
- break;
- }
- }
- vBase.push_back(SRecord);
- }
- }else {
- cout << "Nie mozna otworzyc pliku wejsciowego" <<endl;
- cin.get();
- return 0;
- }
- FInputFile.close();
- //Ustawia wg ktorej kolumny mamy posortowac
- for(int i=0; i<3; i++){
- if (vBase[0].Column[i] == sSortBy) iColumnToCompare=i;
- }
- //sortowanie
- qsort(&vBase[1], (int)vBase.size()-1, sizeof(SDataRecord), compare);
- //wypisywanie
- bSilenMode=(bSilenMode&&bOutputToFile);
- if(!bSilenMode)print_one_row(vBase[0],3);
- if(bOutputToFile)print_one_row_to_f(vBase[0],3,&FOutputFile);
- int offset=0;
- for(int i=1; i<vBase.size(); i++){
- offset=i;
- if(bReverseSort) offset=vBase.size()-i;
- if(!bSilenMode)print_one_row(vBase[offset], 3);
- if(bOutputToFile)print_one_row_to_f(vBase[offset],3,&FOutputFile);
- }
- if(bOutputToFile)FOutputFile.close();
- cin.get();
- return 0;
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment