Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/DataType/Parenth.Frd/Parenth.cpp

Go to the documentation of this file.
00001 // Parenth.cpp - Parenthesis Checking Class Implementation
00002 
00003 #include "Parenth.hpp"
00004 
00005 int Parenth::read(const char* datafile)
00006 {
00007    FILE*  record = NULL;         // Initialize variable
00008    DBase*   _db;              // Contains current data info
00009    Stack*   _stk;             // Contains the Double-Linked list Object
00010 
00011    char c = 0;
00012    char FLine[ LINE_SIZE ];      // MaxLength of a file line
00013    int  pos[   LINE_SIZE ];      // Storing '?' or ' ' on the next line
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++ ) {  // Initialize to zero for stability.
00021       pos[i] = 0;
00022    }
00023 
00024    // Open a file in read-only text mode, if the file exist.
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) )       // Loop until end of file
00033    {
00034       c = fgetc( record );    // Read a Byte from the record file
00035 
00036       if ( c != 13 && c != 10 )   // Scanning for CR, LF ASCII Code 
00037       {                    
00038          FLine[p] = c;        // Add the new char to the array.
00039          p++;
00040          FLine[p] = '\0';
00041       } else if ( p > 0 ) {      // We reached a end of line byte.
00042          FLine[p] = '\0';
00043 
00044 
00045       // We just read a line, so let's find the parenthesis match...
00046 
00047          _stk  = new Stack(); // Create a Stack
00048          perr  = false;
00049 
00050          cout << FLine << endl;
00051 
00052          for( i = 0; i < p; i++ ) {    // Read the line again...
00053 
00054             c = FLine[i];
00055 
00056             if ( c == '(' ) {
00057                _db = new DBase( i );   // Will be deleted by ~Stack()
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() ) {   // Scan for remaining '('
00072             _db = _stk->pop();
00073             pos[ _db->pos() ] = 1;
00074             perr = true;
00075             delete _db;
00076          }
00077 
00078          if ( perr ) {              // Initialize to zero for stability
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() )     // Delete the Stack object
00093             delete _stk;            
00094 
00095          p = 0;               // Reset the counter for the next file read
00096 
00097       } // End of line
00098 
00099    } // End of file while loop
00100 
00101    fclose(record);
00102 
00103    return 0;
00104 }
00105 

Generated on Sun Oct 14 18:46:18 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001