Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

comfile.cpp

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 
00003 #include "comfile.h"
00004 
00005 
00006 //---------------------------------------------------------------------------
00007 TComFileParser::TComFileParser()
00008 {
00009    ReadMode       = CS_MODE_FILE;
00010    OpenFileCount  = 0;
00011    for ( int _f = 0 ; _f < MAX_COM_FILE ; _f++ )
00012    {
00013       files[_f] = NULL;
00014    }
00015 }
00016 //---------------------------------------------------------------------------
00017 TComFileParser::~TComFileParser()
00018 {
00019 }
00020 //---------------------------------------------------------------------------
00021 bool TComFileParser::Open(const char* p_filename)  // open first file
00022 {
00023    Close();
00024    LastError = 0;
00025    strcpy( LastMessage, "" );
00026    eof = false;
00027    if( (files[OpenFileCount] = fopen( p_filename, "r")) == NULL )
00028    {
00029       sprintf( LastMessage, "File <%s> not found.\n", p_filename );
00030       return false;
00031    }
00032    ReadMode = CS_MODE_FILE;
00033    OpenFileCount++;  // now 1
00034    return true;
00035 }
00036 //---------------------------------------------------------------------------
00037 bool TComFileParser::DesignateArchive(FILE* p_file)
00038 {
00039    if ( p_file == NULL )
00040    {
00041       return false;
00042    }
00043         ReadMode = CS_MODE_ARCHIVE;
00044         eof = false;
00045         Archive = p_file;
00046         return true;
00047 }
00048 //---------------------------------------------------------------------------
00049 // close all opened files
00050 void TComFileParser::Close()
00051 {
00052    while( 0 < OpenFileCount )
00053    {
00054       OpenFileCount--;
00055       if ( files[OpenFileCount] != NULL )
00056       {
00057                  fclose( files[OpenFileCount] );
00058          files[OpenFileCount] = NULL;
00059       }
00060    }
00061 }
00062 //---------------------------------------------------------------------------
00063 //
00064 // RETURNS        0 - n  = length of line read
00065 //         COMFILE_EOF   = eof
00066 //         COMFILE_ERROR = error
00067 //
00068 int TComFileParser::ReadLine()
00069 {
00070    int r_value = COMFILE_EOF;
00071 
00072    bool _readyToReturn = false;
00073 
00074         char  buf[MAX_LINE_LENGTH+1];   // read buffer
00075 
00076         LastError = 0;
00077    strcpy( LastMessage, "" );
00078 
00079         if( eof )
00080    {
00081       // Already finished parsing all comfiles, return EOF
00082                 return COMFILE_EOF;
00083         }
00084 
00085 
00086    switch( ReadMode )
00087    {
00088      case CS_MODE_FILE:   // file:
00089 
00090           while( ! _readyToReturn )
00091           {
00092              // Get string from current file
00093                   if( (fgets(buf, sizeof(buf) - 1, files[OpenFileCount-1])) != NULL )
00094                   {
00095                 // able to read line from file
00096 
00097                           LineParseIndex = -1; // i = -1; start of line, no tokens yet
00098 
00099                 switch( buf[0] )
00100                 {
00101                   case '@':
00102                        if ( OpenFileCount == MAX_COM_FILE )
00103                        {
00104                           // failed opening new file
00105                           sprintf( LastMessage, "Attempt to open more than %d command files.", MAX_COM_FILE );
00106                           r_value = COMFILE_ERROR;
00107                           _readyToReturn = true;
00108                        }
00109                        else
00110                        {
00111                           // Found indicator of new file to open
00112                                       buf[0] = ' ';       // replace file name indicator with token spacer
00113                        strcpy( CurrentLine, buf);  // make line with filename available to tokenizer code
00114                                       char* filename = NextToken(); // get filename from current line
00115 
00116                           // open new file
00117                               if( (files[OpenFileCount] = fopen(filename, "r")) != NULL )
00118                           {
00119                                              OpenFileCount++;
00120                              // loop again to get next [first] line from [newly-opened] file for parsing
00121                           }
00122                           else
00123                           {
00124                              // failed opening new file
00125                                                  sprintf( LastMessage, "File <%s> not found.\n", filename );
00126                              r_value = COMFILE_ERROR;
00127                              _readyToReturn = true;
00128                           }
00129                        }
00130                        break;
00131 
00132                   default:
00133                        // remove terminating newline
00134                        if ( buf[strlen(buf)-1] == '\n' )
00135                        {
00136                           buf[strlen(buf)-1] = 0;  // replace end-of-line with end-of-string
00137                        }
00138 
00139                        // check for empty spaces or comment start
00140                        for ( int _c = 0, _sz = strlen(buf) ; _c < _sz ; _c++ )
00141                        {
00142                           switch( buf[_c] )
00143                           {
00144                             case ' ':
00145                             case '\t':
00146                                  break;
00147 
00148                             case '\n':
00149                             case '#':
00150                                  // empty line or comment, go to next line
00151                                  _c = _sz;
00152                                  break;
00153 
00154                             default:
00155                                  // not an indication of new file, return info about current line
00156                                  strcpy( CurrentLine, buf );
00157                                  r_value = strlen( CurrentLine );
00158                                  _readyToReturn = true;
00159                                  LineParseIndex = _c - 1;
00160                                  _c = _sz;
00161                                  break;
00162                           }
00163                        }
00164 
00165                 }
00166              }
00167              else
00168              {
00169                 // nothing read from current file, must be end of current file
00170                 fclose( files[--OpenFileCount] );
00171                 files[OpenFileCount] = NULL;
00172 
00173                      if( OpenFileCount == 0 )
00174                 {
00175                    // no files remain open, return indication of end-of-file
00176                    strcpy( Token , "eof" );
00177                    eof = true;
00178                    r_value = COMFILE_EOF;
00179                    _readyToReturn = true;
00180                 }
00181                 // else a file remains open, get the next line from that file
00182              }
00183           } // while( ! _notReadyToReturn )
00184           break;
00185 
00186 
00187      case CS_MODE_ARCHIVE:  // This mode reads archive files char-by-char into CurrentLine
00188           {
00189                   char  chr;        // read char
00190                   int   charsread = 0;   // chars read from archive file
00191 
00192              while( ! _readyToReturn )
00193              {
00194                 switch( (chr = (char)fgetc( Archive )) )
00195                 {
00196                   case EOF:
00197                        // could also be an error
00198                        strcpy( Token , "eof" );
00199                        eof = true;
00200                        if ( charsread == 0 )
00201                        {
00202                           // encountered EOF with no line data in buffer
00203                           r_value = COMFILE_EOF;
00204                           _readyToReturn = true;
00205                           break;
00206                        }
00207                        // encountered EOF without preceeding end-of-line NULL termination
00208                        // unlikely, but....
00209                        // fall through
00210 
00211                   case '\n':
00212                        LineParseIndex = -1; // i = -1;
00213                        buf[charsread] = 0;         // null-terminate
00214                        strcpy( CurrentLine, buf);
00215                        r_value = strlen( CurrentLine );
00216                        _readyToReturn = true;
00217                        break;
00218 
00219                   case '\r':
00220                        // drop this char, read the next one
00221                        //goto archive;
00222                        break;
00223 
00224                   default:
00225                        // append this char
00226                        buf[charsread++] = chr;
00227                        // continue loop to get next one
00228                        break;
00229                 }
00230              } // while( ! _readyToReturn )
00231 
00232           }
00233           break;
00234    }
00235 
00236    return r_value;
00237 }

Generated on Tue May 6 09:16:00 2003 for Earthworm Libs by doxygen1.3-rc3