Moved from FILE* to fstream

This commit is contained in:
Jonny007-MKD 2014-01-07 19:51:29 +01:00
parent b77e4e0127
commit 1325c11caa
5 changed files with 103 additions and 59 deletions

View file

@ -3,6 +3,9 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <map> #include <map>
#include <fstream>
#include <iostream>
#include <iomanip>
#include "PrimImplikantCollection.h" #include "PrimImplikantCollection.h"
#include "CParser.h" #include "CParser.h"
using namespace std; using namespace std;
@ -56,7 +59,7 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector<string>* &variables)
{ {
bool KNFset = false; bool KNFset = false;
int tok; int tok;
if(prflag)fprintf(IP_List,"%5d ", IP_LineNumber); if(prflag) *IP_List << setw(5) << setfill('0') << IP_LineNumber;
/* /*
* Go parse things! * Go parse things!
@ -82,11 +85,11 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector<string>* &variables)
switch(tok) switch(tok)
{ {
case IDENTIFIER: 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()); variables->push_back(yylval.s.c_str());
break; break;
case TERMS: case TERMS:
fprintf(IP_List, "\n", yylval.s.c_str()); *IP_List << endl;
pState = P_TERMS_KEY; pState = P_TERMS_KEY;
break; break;
} }
@ -95,11 +98,11 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector<string>* &variables)
switch(tok) switch(tok)
{ {
case STRING1: 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()); pic->add(yylval.s.c_str());
break; break;
case INTEGER1: 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); pic->add(yylval.i);
break; break;
case (int)'>': case (int)'>':
@ -120,14 +123,14 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector<string>* &variables)
} }
else if ((yylval.i == 0) ^ KNF) else if ((yylval.i == 0) ^ KNF)
{ {
fprintf(IP_Error, "*** FATAL ERROR *** You can only define either KNF or DNF!\n"); *IP_Error << "*** FATAL ERROR *** You can only define either KNF or DNF!" << endl;
fprintf(IP_Error, "In line %3d: %s>%i\n", (int)IP_LineNumber, pic->back()->name.c_str(), yylval.i); *IP_Error << "In line " << setw(3) << setfill('0') << IP_LineNumber << ": " << pic->back()->name << '>' << yylval.i << endl;
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"); *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;
printf("*** FATAL ERROR *** You can only define either KNF or DNF!\n"); cout << "*** FATAL ERROR *** You can only define either KNF or DNF!" << endl;
return 1; return 1;
} }
fprintf(IP_List, "Term Value %d\n\n",yylval.i); *IP_List << "Term Value " << yylval.i << endl << endl;
pState = P_TERMS_KEY; pState = P_TERMS_KEY;
} }
break; break;
@ -148,16 +151,16 @@ int CParser::yyparse(PrimImplikantCollection* &pic, vector<string>* &variables)
* It is passed two file streams, the first is where the input comes * It is passed two file streams, the first is where the input comes
* from; the second is where error messages get printed. * 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. * Set up the file state to something useful.
*/ */
IP_Input = inp; IP_Input = &inp;
IP_Error = err; IP_Error = &err;
IP_List = lst; IP_List = &lst;
IP_LineNumber = 1; IP_LineNumber = 1;
ugetflag=0; ugetflag=0;
@ -175,9 +178,8 @@ void CParser::InitParse(FILE *inp,FILE *err,FILE *lst)
* preceeded by the current filename and line number. * preceeded by the current filename and line number.
*/ */
void CParser::yyerror(char *ers) 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 //Locals
int c; int c;
lexstate s; lexstate s = L_START;
/* /*
* Keep on sucking up characters until we find something which * Keep on sucking up characters until we find something which
* explicitly forces us out of this function. * explicitly forces us out of this function.
@ -210,11 +212,12 @@ int CParser::yylex()
yytext = ""; yytext = "";
yylval.s = ""; yylval.s = "";
yylval.i = 0; yylval.i = 0;
for (s = L_START; 1;){ while(!IP_Input->eof())
c = Getc(IP_Input); {
c = (*IP_Input).get();
yytext = yytext + (char)c; yytext = yytext + (char)c;
if(!ugetflag) { if(!ugetflag) {
if(c != EOF)if(prflag)fprintf(IP_List,"%c",c); if(c != EOF && prflag) *IP_List << c;
}else ugetflag = 0; }else ugetflag = 0;
switch (s){ switch (s){
//Starting state, look for something resembling a token. //Starting state, look for something resembling a token.
@ -228,7 +231,7 @@ int CParser::yylex()
if (c == '\n'){ if (c == '\n'){
IP_LineNumber += 1; IP_LineNumber += 1;
if(prflag) if(prflag)
fprintf(IP_List,"%5d ",(int)IP_LineNumber); *IP_List << setw(3) << setfill('9') << IP_LineNumber;
s = L_START; s = L_START;
yytext = ""; yytext = "";
} }
@ -252,14 +255,14 @@ int CParser::yylex()
else if(c == '*') else if(c == '*')
s = L_TEXT_COMMENT; s = L_TEXT_COMMENT;
else{ else{
Ungetc(c); (*IP_Input).unget();
return('/'); /* its the division operator not a comment */ return '/'; // its the division operator not a comment
} }
break; break;
case L_LINE_COMMENT: case L_LINE_COMMENT:
if ( c == '\n'){ if ( c == '\n'){
s = L_START; s = L_START;
Ungetc(c); (*IP_Input).unget();
} }
yytext = ""; yytext = "";
break; break;
@ -286,7 +289,7 @@ int CParser::yylex()
if (isdigit(c)){ if (isdigit(c)){
break; break;
}else { }else {
Ungetc(c); (*IP_Input).unget();
yylval.s = yytext.substr(0,yytext.size()-1); yylval.s = yytext.substr(0,yytext.size()-1);
yylval.i = atoi(yylval.s.c_str()); yylval.i = atoi(yylval.s.c_str());
return (INTEGER1); return (INTEGER1);
@ -301,7 +304,7 @@ int CParser::yylex()
case L_IDENT: case L_IDENT:
if (isalpha(c) || isdigit(c) || c == '_') if (isalpha(c) || isdigit(c) || c == '_')
break; break;
Ungetc(c); (*IP_Input).unget();
yytext = yytext.substr(0,yytext.size()-1); yytext = yytext.substr(0,yytext.size()-1);
yylval.s = yytext; yylval.s = yytext;
if (c = IP_MatchToken(yytext)){ if (c = IP_MatchToken(yytext)){
@ -342,9 +345,10 @@ int CParser::yylex()
PushString((char)c); PushString((char)c);
break; break;
default: default:
fprintf(IP_Error, "*** FATAL ERROR *** Wrong case label in yylex\n"); *IP_Error << "*** FATAL ERROR *** Wrong case label in yylex" << endl;
fprintf(IP_Error, "In line %3d: state %i", IP_LineNumber, s); *IP_Error << "In line " << setw(3) << setfill('0') << IP_LineNumber << ": state " << s << endl;
printf("***Fatal Error*** Wrong case label in yylex\n"); cout << "***Fatal Error*** Wrong case label in yylex" << endl;
} }
} }
return 0;
} }

View file

@ -35,9 +35,9 @@ public:
string s; //structure string s; //structure
int i; int i;
}yylval; }yylval;
FILE *IP_Input; //Input File ifstream* IP_Input; //Input File
FILE *IP_Error; //Error Output ofstream* IP_Error; //Error Output
FILE *IP_List; //List Output ofstream* IP_List; //List Output
int IP_LineNumber; //Line counter int IP_LineNumber; //Line counter
int ugetflag; //checks ungets int ugetflag; //checks ungets
int prflag; //controls printing int prflag; //controls printing
@ -48,14 +48,14 @@ public:
int CParser::yylex(); //lexial analyser int CParser::yylex(); //lexial analyser
void CParser::yyerror(char *ers); //error reporter void CParser::yyerror(char *ers); //error reporter
int CParser::IP_MatchToken(string &tok); //checks the token 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<string>* &variables); //parser int CParser::yyparse(PrimImplikantCollection* &pic, vector<string>* &variables); //parser
void CParser::pr_tokentable(); //test output for tokens void CParser::pr_tokentable(); //test output for tokens
void CParser::IP_init_token_table(); //loads the tokens void CParser::IP_init_token_table(); //loads the tokens
void CParser::Load_tokenentry(string str,int index);//load one token void CParser::Load_tokenentry(string str,int index);//load one token
void CParser::PushString(char c); //Used for dtring assembly void CParser::PushString(char c); //Used for dtring assembly
CParser(){IP_LineNumber = 1;ugetflag=0;prflag=0;}; //Constructor 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->IP_init_token_table();
this->InitParse(input, error, list); this->InitParse(input, error, list);

View file

@ -20,35 +20,46 @@ bool fileChosen = false;
char fnInput[256]; 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 if (fileChosen == true) // don't reopen files
return 9; return 9;
GetCurrentDirectory(sizeof(fnInput), fnInput); GetCurrentDirectory(sizeof(fnInput), fnInput);
if (error == NULL) if (!error.is_open())
{ {
char fnError[256]; char fnError[256];
strcpy_s(fnError, (string(fnInput) + "\\res\\errorParser.txt").c_str()); strcpy_s(fnError, (string(fnInput) + "\\res\\errorParser.txt").c_str());
fopen_s(&error, fnError, "a"); error.open(fnError, ofstream::out|ofstream::app);
if (error == NULL) if (!error.is_open())
{ {
perror("Fehler beim Öffnen der Fehlerdatei"); perror("Fehler beim Öffnen der Fehlerdatei");
return 1; return 1;
} }
} }
if (list == NULL) if (!list.is_open())
{ {
char fnLists[256]; char fnLists[256];
strcpy_s(fnLists, (string(fnInput) + "\\res\\listParser.txt").c_str()); strcpy_s(fnLists, (string(fnInput) + "\\res\\listParser.txt").c_str());
fopen_s(&list, fnLists, "w"); list.open(fnLists, ofstream::out|ofstream::trunc);
if (list == NULL) if (!list.is_open())
{ {
perror("Fehler beim Öffnen der Listdatei"); perror("Fehler beim Öffnen der Listdatei");
return 1; 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()); 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; fileChosen = true;
if (input != NULL) if (input.is_open())
fclose(input); input.close();
fopen_s(&input, fnInput, "r"); input.open(fnInput, ifstream::in);
if (input == NULL) if (!input.is_open())
{ {
perror("Fehler beim Lesen der Inputdatei"); perror("Fehler beim Lesen der Inputdatei");
return 1; return 1;
@ -92,11 +103,12 @@ void pause()
void user_main(void) void user_main(void)
{ {
FILE * input = NULL, * error = NULL, * list = NULL; ifstream fInput;
ofstream fError, fList, fWt;
KV* kv; KV* kv;
while(1) while(1)
{ {
switch(open_files(input, error, list)) switch(open_files(fInput, fError, fList, fWt))
{ {
case 1: // some error case 1: // some error
fileChosen = false; fileChosen = false;
@ -114,8 +126,9 @@ void user_main(void)
PrimImplikantCollection* globalPIC = new PrimImplikantCollection(); PrimImplikantCollection* globalPIC = new PrimImplikantCollection();
vector<string>* variables = new vector<string>(); vector<string>* variables = new vector<string>();
rewind(input); fInput.clear();
CParser* parser = new CParser(input, error, list); fInput.seekg(0, fInput.beg);
CParser* parser = new CParser(fInput, fError, fList);
int parseFailure = parser->yyparse(globalPIC, variables); int parseFailure = parser->yyparse(globalPIC, variables);
delete parser; delete parser;
@ -128,6 +141,7 @@ void user_main(void)
error += buf; error += buf;
error += ")"; error += ")";
perror(error.c_str()); perror(error.c_str());
fError << error << endl;
continue; continue;
} }
@ -135,8 +149,9 @@ void user_main(void)
CellCollection* allCells = new CellCollection(globalPIC); CellCollection* allCells = new CellCollection(globalPIC);
fWt.seekp(0, fWt.beg);
// print Wertetabelle and KV of imported data // print Wertetabelle and KV of imported data
Wertetabelle* wt = new Wertetabelle(allCells, variables); Wertetabelle* wt = new Wertetabelle(allCells, variables, fWt);
wt->Print(); wt->Print();
kv = new KV(globalPIC, allCells, 30,variables); kv = new KV(globalPIC, allCells, 30,variables);
@ -174,7 +189,10 @@ void user_main(void)
if (kv != NULL) if (kv != NULL)
delete kv; delete kv;
kv = NULL; kv = NULL;
_fcloseall(); fInput.close();
fList.close();
fError.close();
fWt.close();
fileChosen = false; fileChosen = false;
break; break;
} }

View file

@ -22,19 +22,30 @@ void Wertetabelle::Print()
cout << "| "; // => | cout << "| "; // => |
cout << setfill(' ') << setw((uint)ceil(log10((float)numElements))) << i; // => 4 cout << setfill(' ') << setw((uint)ceil(log10((float)numElements))) << i; // => 4
cout << " |"; // => | cout << " |"; // => |
*fot << "| "; // => |
*fot << setfill(' ') << setw((uint)ceil(log10((float)numElements))) << i; // => 4
*fot << " |"; // => |
this->printI(i); // => 0 1 0 0 this->printI(i); // => 0 1 0 0
cout << "| "; // => | cout << "| "; // => |
cout << ((*this->cells)[i]->value ^ KNF); // => 1 cout << ((*this->cells)[i]->value ^ KNF); // => 1
cout << " |"; // => | cout << " |"; // => |
*fot << "| "; // => |
*fot << ((*this->cells)[i]->value ^ KNF); // => 1
*fot << " |"; // => |
this->printPrimImplikanten(i); // => 0 0x1 4 this->printPrimImplikanten(i); // => 0 0x1 4
cout << endl; // ==> | 4 | 0 1 0 0 | 1 | 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; //cout << this->makeHeader() << endl;
printHeader(); printHeader();
} }
cout << string(this->width, '-') << endl; cout << string(this->width, '-') << endl;
*fot << string(this->width, '-') << endl;
} }
string Wertetabelle::makeHeader() string Wertetabelle::makeHeader()
@ -65,6 +76,10 @@ void Wertetabelle::printHeader()
cout << string(this->width, '-') << endl; // repeat '-' several times => --------------------- cout << string(this->width, '-') << endl; // repeat '-' several times => ---------------------
cout << row2 << endl; // print header row => | a bärchen c d | y | PrimtImpl. cout << row2 << endl; // print header row => | a bärchen c d | y | PrimtImpl.
cout << string(this->width, '-') << endl; // repeat '-' several times => --------------------- 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) 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; row = string((uint)ceil(padding[j]) + 1, ' ') + iAtJ + string((uint)floor(padding[j]), ' ') + row;
} }
cout << row; cout << row;
*fot << row;
} }
void Wertetabelle::printPrimImplikanten(unsigned int i) void Wertetabelle::printPrimImplikanten(uint i)
{ {
cout << ' '; cout << ' ';
*fot << ' ';
Cell* cell = cells->at(i); 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 << " "; cout << cell->primImplikanten[pi]->name << " ";
*fot << cell->primImplikanten[pi]->name << " ";
}
} }

View file

@ -3,7 +3,7 @@
#include "stdafx.h" #include "stdafx.h"
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream> #include <fstream>
#include "PrimImplikant.h" #include "PrimImplikant.h"
#include "Cell.h" #include "Cell.h"
#include "CellCollection.h" #include "CellCollection.h"
@ -18,22 +18,24 @@ class Wertetabelle
public: public:
void Print(); void Print();
Wertetabelle(CellCollection* cells, vector<string>* variables) Wertetabelle(CellCollection* cells, vector<string>* variables, ofstream &fWt)
{ {
this->cells = cells; this->cells = cells;
this->variables = variables; this->variables = variables;
this->fot = &fWt;
} }
private: private:
string makeHeader(); string makeHeader();
void printHeader(); void printHeader();
void printI(unsigned int i); void printI(uint i);
void printPrimImplikanten(unsigned int i); void printPrimImplikanten(uint i);
CellCollection* cells; CellCollection* cells;
vector<string>* variables; vector<string>* variables;
vector<float> padding; vector<float> padding;
uint width; uint width;
ofstream* fot;
}; };
#endif #endif