From 1325c11caa1c23b957569dca0e63c6f1bc4bacaa Mon Sep 17 00:00:00 2001 From: Jonny007-MKD <1-23-4-5@web.de> Date: Tue, 7 Jan 2014 19:51:29 +0100 Subject: [PATCH] Moved from FILE* to fstream --- Hazard/Hazard/CParser.cpp | 62 ++++++++++++++++++---------------- Hazard/Hazard/Cparser.h | 10 +++--- Hazard/Hazard/Hazard.cpp | 54 +++++++++++++++++++---------- Hazard/Hazard/Wertetabelle.cpp | 26 ++++++++++++-- Hazard/Hazard/Wertetabelle.h | 10 +++--- 5 files changed, 103 insertions(+), 59 deletions(-) diff --git a/Hazard/Hazard/CParser.cpp b/Hazard/Hazard/CParser.cpp index a7e3a21..94b7a10 100644 --- a/Hazard/Hazard/CParser.cpp +++ b/Hazard/Hazard/CParser.cpp @@ -3,6 +3,9 @@ #include #include #include +#include +#include +#include #include "PrimImplikantCollection.h" #include "CParser.h" using namespace std; @@ -56,7 +59,7 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector* &variables) { bool KNFset = false; int tok; - if(prflag)fprintf(IP_List,"%5d ", IP_LineNumber); + if(prflag) *IP_List << setw(5) << setfill('0') << IP_LineNumber; /* * Go parse things! @@ -82,11 +85,11 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector* &variables) switch(tok) { case IDENTIFIER: - fprintf(IP_List, "Variable %s\n", yylval.s.c_str()); + *IP_List << "Variable " << yylval.s << endl; variables->push_back(yylval.s.c_str()); break; case TERMS: - fprintf(IP_List, "\n", yylval.s.c_str()); + *IP_List << endl; pState = P_TERMS_KEY; break; } @@ -95,11 +98,11 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector* &variables) switch(tok) { case STRING1: - fprintf(IP_List, "Term Key %s\n", yylval.s.c_str()); + *IP_List << "Term Key " << yylval.s << endl; pic->add(yylval.s.c_str()); break; case INTEGER1: - fprintf(IP_List, "Term Key %d\n", (unsigned int)yylval.i); + *IP_List << "Term Key " << (unsigned int)yylval.i << endl; pic->add(yylval.i); break; case (int)'>': @@ -120,14 +123,14 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector* &variables) } else if ((yylval.i == 0) ^ KNF) { - fprintf(IP_Error, "*** FATAL ERROR *** You can only define either KNF or DNF!\n"); - fprintf(IP_Error, "In line %3d: %s>%i\n", (int)IP_LineNumber, pic->back()->name.c_str(), yylval.i); - fprintf(IP_Error, "In line %3d: Defined was: %s, but now shall be changed to %s\n\n", (int)IP_LineNumber, KNF ? "KNF" : "DNF", KNF ? "DNF" : "KNF"); - printf("*** FATAL ERROR *** You can only define either KNF or DNF!\n"); + *IP_Error << "*** FATAL ERROR *** You can only define either KNF or DNF!" << endl; + *IP_Error << "In line " << setw(3) << setfill('0') << IP_LineNumber << ": " << pic->back()->name << '>' << yylval.i << endl; + *IP_Error << "In line " << setw(3) << setfill('0') << IP_LineNumber << ": Defined was: " << (KNF ? "KNF" : "DNF") << " but now shall be changed to " << (KNF ? "DNF" : "KNF") << endl << endl; + cout << "*** FATAL ERROR *** You can only define either KNF or DNF!" << endl; return 1; } - fprintf(IP_List, "Term Value %d\n\n",yylval.i); + *IP_List << "Term Value " << yylval.i << endl << endl; pState = P_TERMS_KEY; } break; @@ -148,16 +151,16 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector* &variables) * It is passed two file streams, the first is where the input comes * from; the second is where error messages get printed. */ -void CParser::InitParse(FILE *inp,FILE *err,FILE *lst) +void CParser::InitParse(ifstream &inp, ofstream &err, ofstream &lst) { /* * Set up the file state to something useful. */ - IP_Input = inp; - IP_Error = err; - IP_List = lst; + IP_Input = &inp; + IP_Error = &err; + IP_List = &lst; IP_LineNumber = 1; ugetflag=0; @@ -175,9 +178,8 @@ void CParser::InitParse(FILE *inp,FILE *err,FILE *lst) * preceeded by the current filename and line number. */ void CParser::yyerror(char *ers) - { - fprintf(IP_Error,"line %d: %s\n",IP_LineNumber,ers); + *IP_Error << "In line " << setw(3) << setfill('0') << IP_LineNumber << ": " << ers << endl; } //------------------------------------------------------------------------ @@ -202,7 +204,7 @@ int CParser::yylex() { //Locals int c; - lexstate s; + lexstate s = L_START; /* * Keep on sucking up characters until we find something which * explicitly forces us out of this function. @@ -210,11 +212,12 @@ int CParser::yylex() yytext = ""; yylval.s = ""; yylval.i = 0; - for (s = L_START; 1;){ - c = Getc(IP_Input); + while(!IP_Input->eof()) + { + c = (*IP_Input).get(); yytext = yytext + (char)c; if(!ugetflag) { - if(c != EOF)if(prflag)fprintf(IP_List,"%c",c); + if(c != EOF && prflag) *IP_List << c; }else ugetflag = 0; switch (s){ //Starting state, look for something resembling a token. @@ -228,7 +231,7 @@ int CParser::yylex() if (c == '\n'){ IP_LineNumber += 1; if(prflag) - fprintf(IP_List,"%5d ",(int)IP_LineNumber); + *IP_List << setw(3) << setfill('9') << IP_LineNumber; s = L_START; yytext = ""; } @@ -252,14 +255,14 @@ int CParser::yylex() else if(c == '*') s = L_TEXT_COMMENT; else{ - Ungetc(c); - return('/'); /* its the division operator not a comment */ + (*IP_Input).unget(); + return '/'; // its the division operator not a comment } break; case L_LINE_COMMENT: if ( c == '\n'){ s = L_START; - Ungetc(c); + (*IP_Input).unget(); } yytext = ""; break; @@ -286,7 +289,7 @@ int CParser::yylex() if (isdigit(c)){ break; }else { - Ungetc(c); + (*IP_Input).unget(); yylval.s = yytext.substr(0,yytext.size()-1); yylval.i = atoi(yylval.s.c_str()); return (INTEGER1); @@ -301,7 +304,7 @@ int CParser::yylex() case L_IDENT: if (isalpha(c) || isdigit(c) || c == '_') break; - Ungetc(c); + (*IP_Input).unget(); yytext = yytext.substr(0,yytext.size()-1); yylval.s = yytext; if (c = IP_MatchToken(yytext)){ @@ -342,9 +345,10 @@ int CParser::yylex() PushString((char)c); break; default: - fprintf(IP_Error, "*** FATAL ERROR *** Wrong case label in yylex\n"); - fprintf(IP_Error, "In line %3d: state %i", IP_LineNumber, s); - printf("***Fatal Error*** Wrong case label in yylex\n"); + *IP_Error << "*** FATAL ERROR *** Wrong case label in yylex" << endl; + *IP_Error << "In line " << setw(3) << setfill('0') << IP_LineNumber << ": state " << s << endl; + cout << "***Fatal Error*** Wrong case label in yylex" << endl; } } + return 0; } \ No newline at end of file diff --git a/Hazard/Hazard/Cparser.h b/Hazard/Hazard/Cparser.h index 2ffa16d..b7944b3 100644 --- a/Hazard/Hazard/Cparser.h +++ b/Hazard/Hazard/Cparser.h @@ -35,9 +35,9 @@ public: string s; //structure int i; }yylval; - FILE *IP_Input; //Input File - FILE *IP_Error; //Error Output - FILE *IP_List; //List Output + ifstream* IP_Input; //Input File + ofstream* IP_Error; //Error Output + ofstream* IP_List; //List Output int IP_LineNumber; //Line counter int ugetflag; //checks ungets int prflag; //controls printing @@ -48,14 +48,14 @@ public: int CParser::yylex(); //lexial analyser void CParser::yyerror(char *ers); //error reporter int CParser::IP_MatchToken(string &tok); //checks the token - void CParser::InitParse(FILE *inp,FILE *err,FILE *lst); + void CParser::InitParse(ifstream &inp,ofstream &err,ofstream &lst); int CParser::yyparse(PrimImplikantCollection* &pic, vector* &variables); //parser void CParser::pr_tokentable(); //test output for tokens void CParser::IP_init_token_table(); //loads the tokens void CParser::Load_tokenentry(string str,int index);//load one token void CParser::PushString(char c); //Used for dtring assembly CParser(){IP_LineNumber = 1;ugetflag=0;prflag=0;}; //Constructor - CParser(FILE * input, FILE * error, FILE * list) + CParser(ifstream &input, ofstream &error, ofstream &list) { this->IP_init_token_table(); this->InitParse(input, error, list); diff --git a/Hazard/Hazard/Hazard.cpp b/Hazard/Hazard/Hazard.cpp index 4e1ddcc..f9c40f1 100644 --- a/Hazard/Hazard/Hazard.cpp +++ b/Hazard/Hazard/Hazard.cpp @@ -20,35 +20,46 @@ bool fileChosen = false; char fnInput[256]; -int open_files(FILE * &input, FILE * &error, FILE * &list) +int open_files(ifstream &input, ofstream &error, ofstream &list, ofstream &wt) { if (fileChosen == true) // don't reopen files return 9; GetCurrentDirectory(sizeof(fnInput), fnInput); - if (error == NULL) + if (!error.is_open()) { char fnError[256]; strcpy_s(fnError, (string(fnInput) + "\\res\\errorParser.txt").c_str()); - fopen_s(&error, fnError, "a"); - if (error == NULL) + error.open(fnError, ofstream::out|ofstream::app); + if (!error.is_open()) { perror("Fehler beim Öffnen der Fehlerdatei"); return 1; } } - if (list == NULL) + if (!list.is_open()) { char fnLists[256]; strcpy_s(fnLists, (string(fnInput) + "\\res\\listParser.txt").c_str()); - fopen_s(&list, fnLists, "w"); - if (list == NULL) + list.open(fnLists, ofstream::out|ofstream::trunc); + if (!list.is_open()) { perror("Fehler beim Öffnen der Listdatei"); return 1; } } + if (!wt.is_open()) + { + char fnWt[256]; + strcpy_s(fnWt, (string(fnInput) + "\\res\\Wertetabelle.txt").c_str()); + wt.open(fnWt, ofstream::out|ofstream::trunc); + if (!wt.is_open()) + { + perror("Fehler beim Öffnen der Wertetabellendatei"); + return 1; + } + } strcpy_s(fnInput, (string(fnInput) + "\\res\\input.txt").c_str()); @@ -72,10 +83,10 @@ int open_files(FILE * &input, FILE * &error, FILE * &list) } fileChosen = true; - if (input != NULL) - fclose(input); - fopen_s(&input, fnInput, "r"); - if (input == NULL) + if (input.is_open()) + input.close(); + input.open(fnInput, ifstream::in); + if (!input.is_open()) { perror("Fehler beim Lesen der Inputdatei"); return 1; @@ -92,11 +103,12 @@ void pause() void user_main(void) { - FILE * input = NULL, * error = NULL, * list = NULL; + ifstream fInput; + ofstream fError, fList, fWt; KV* kv; while(1) { - switch(open_files(input, error, list)) + switch(open_files(fInput, fError, fList, fWt)) { case 1: // some error fileChosen = false; @@ -114,8 +126,9 @@ void user_main(void) PrimImplikantCollection* globalPIC = new PrimImplikantCollection(); vector* variables = new vector(); - rewind(input); - CParser* parser = new CParser(input, error, list); + fInput.clear(); + fInput.seekg(0, fInput.beg); + CParser* parser = new CParser(fInput, fError, fList); int parseFailure = parser->yyparse(globalPIC, variables); delete parser; @@ -128,15 +141,17 @@ void user_main(void) error += buf; error += ")"; perror(error.c_str()); + fError << error << endl; continue; } // initialize Cells CellCollection* allCells = new CellCollection(globalPIC); - + + fWt.seekp(0, fWt.beg); // print Wertetabelle and KV of imported data - Wertetabelle* wt = new Wertetabelle(allCells, variables); + Wertetabelle* wt = new Wertetabelle(allCells, variables, fWt); wt->Print(); kv = new KV(globalPIC, allCells, 30,variables); @@ -174,7 +189,10 @@ void user_main(void) if (kv != NULL) delete kv; kv = NULL; - _fcloseall(); + fInput.close(); + fList.close(); + fError.close(); + fWt.close(); fileChosen = false; break; } diff --git a/Hazard/Hazard/Wertetabelle.cpp b/Hazard/Hazard/Wertetabelle.cpp index e4408db..ca42365 100644 --- a/Hazard/Hazard/Wertetabelle.cpp +++ b/Hazard/Hazard/Wertetabelle.cpp @@ -22,19 +22,30 @@ void Wertetabelle::Print() cout << "| "; // => | cout << setfill(' ') << setw((uint)ceil(log10((float)numElements))) << i; // => 4 cout << " |"; // => | + *fot << "| "; // => | + *fot << setfill(' ') << setw((uint)ceil(log10((float)numElements))) << i; // => 4 + *fot << " |"; // => | this->printI(i); // => 0 1 0 0 cout << "| "; // => | cout << ((*this->cells)[i]->value ^ KNF); // => 1 cout << " |"; // => | + *fot << "| "; // => | + *fot << ((*this->cells)[i]->value ^ KNF); // => 1 + *fot << " |"; // => | this->printPrimImplikanten(i); // => 0 0x1 4 cout << endl; // ==> | 4 | 0 1 0 0 | 1 | 0 0x1 4 + *fot << endl; // ==> | 4 | 0 1 0 0 | 1 | 0 0x1 4 - if (i > 0 && i % 15 == 0 && numElements - i > 5) // reprint header so you dont have to scroll + this->printI(i); // => 0 1 0 0 + this->printPrimImplikanten(i); // => 0 0x1 4 + + if (i > 0 && i % 31 == 0 && numElements - i > 5) // reprint header so you dont have to scroll //cout << this->makeHeader() << endl; printHeader(); } cout << string(this->width, '-') << endl; + *fot << string(this->width, '-') << endl; } string Wertetabelle::makeHeader() @@ -65,6 +76,10 @@ void Wertetabelle::printHeader() cout << string(this->width, '-') << endl; // repeat '-' several times => --------------------- cout << row2 << endl; // print header row => | a bärchen c d | y | PrimtImpl. cout << string(this->width, '-') << endl; // repeat '-' several times => --------------------- + + *fot << string(this->width, '-') << endl; // repeat '-' several times => --------------------- + *fot << row2 << endl; // print header row => | a bärchen c d | y | PrimtImpl. + *fot << string(this->width, '-') << endl; // repeat '-' several times => --------------------- } void Wertetabelle::printI(uint i) @@ -78,13 +93,18 @@ void Wertetabelle::printI(uint i) row = string((uint)ceil(padding[j]) + 1, ' ') + iAtJ + string((uint)floor(padding[j]), ' ') + row; } cout << row; + *fot << row; } -void Wertetabelle::printPrimImplikanten(unsigned int i) +void Wertetabelle::printPrimImplikanten(uint i) { cout << ' '; + *fot << ' '; Cell* cell = cells->at(i); - for (unsigned int pi = 0; pi < cell->primImplikanten.size(); pi++) // for every PrimImplikant in Cell + for (uint pi = 0; pi < cell->primImplikanten.size(); pi++) // for every PrimImplikant in Cell + { cout << cell->primImplikanten[pi]->name << " "; + *fot << cell->primImplikanten[pi]->name << " "; + } } \ No newline at end of file diff --git a/Hazard/Hazard/Wertetabelle.h b/Hazard/Hazard/Wertetabelle.h index 9170289..926f6d5 100644 --- a/Hazard/Hazard/Wertetabelle.h +++ b/Hazard/Hazard/Wertetabelle.h @@ -3,7 +3,7 @@ #include "stdafx.h" #include #include -#include +#include #include "PrimImplikant.h" #include "Cell.h" #include "CellCollection.h" @@ -18,22 +18,24 @@ class Wertetabelle public: void Print(); - Wertetabelle(CellCollection* cells, vector* variables) + Wertetabelle(CellCollection* cells, vector* variables, ofstream &fWt) { this->cells = cells; this->variables = variables; + this->fot = &fWt; } private: string makeHeader(); void printHeader(); - void printI(unsigned int i); - void printPrimImplikanten(unsigned int i); + void printI(uint i); + void printPrimImplikanten(uint i); CellCollection* cells; vector* variables; vector padding; uint width; + ofstream* fot; }; #endif \ No newline at end of file