00001
00002
00003 #include "Parenth.hpp"
00004
00005 int Parenth::read(const char* datafile)
00006 {
00007 FILE* record = NULL;
00008 DBase* _db;
00009 Stack* _stk;
00010
00011 char c = 0;
00012 char FLine[ LINE_SIZE ];
00013 int pos[ LINE_SIZE ];
00014
00015 bool perr;
00016 int p = 0;
00017 int i = 0;
00018 int j = 0;
00019
00020 for( i = 0; i < LINE_SIZE; i++ ) {
00021 pos[i] = 0;
00022 }
00023
00024
00025 if ( (record = fopen(datafile, "rt") ) == NULL)
00026 {
00027 cout << "Cannot open the data file [" << datafile;
00028 cout << "] in the current directory." << endl;
00029 return 1;
00030 }
00031
00032 while ( !feof(record) )
00033 {
00034 c = fgetc( record );
00035
00036 if ( c != 13 && c != 10 )
00037 {
00038 FLine[p] = c;
00039 p++;
00040 FLine[p] = '\0';
00041 } else if ( p > 0 ) {
00042 FLine[p] = '\0';
00043
00044
00045
00046
00047 _stk = new Stack();
00048 perr = false;
00049
00050 cout << FLine << endl;
00051
00052 for( i = 0; i < p; i++ ) {
00053
00054 c = FLine[i];
00055
00056 if ( c == '(' ) {
00057 _db = new DBase( i );
00058 _stk->push( _db );
00059 } else if ( c == ')' ) {
00060 _db = _stk->pop();
00061 if ( _db == NULL ) {
00062 pos[i] = -1;
00063 perr = true;
00064 } else {
00065 delete _db;
00066 }
00067 }
00068
00069 }
00070
00071 while ( _stk->isNotEmpty() ) {
00072 _db = _stk->pop();
00073 pos[ _db->pos() ] = 1;
00074 perr = true;
00075 delete _db;
00076 }
00077
00078 if ( perr ) {
00079 for( i = 0; i < LINE_SIZE; i++ ) {
00080 if ( pos[i] == 0 ) {
00081 cout << " ";
00082 } else {
00083 cout << "?";
00084 pos[i] = 0;
00085 }
00086 }
00087 cout << endl << msg_BAD;
00088 } else {
00089 cout << msg_OK;
00090 }
00091
00092 if ( _stk->isNotEmpty() )
00093 delete _stk;
00094
00095 p = 0;
00096
00097 }
00098
00099 }
00100
00101 fclose(record);
00102
00103 return 0;
00104 }
00105