MeshKit  1.0
parser.cpp
Go to the documentation of this file.
00001 /*********************************************
00002 Reactor Geometry Generator
00003 Argonne National Laboratory
00004 
00005 Contains CParser class implementation.
00006 *********************************************/
00007 #include <cctype>
00008 #include "meshkit/parser.hpp"
00009 #include <stdlib.h>
00010 
00011 CParser::CParser ()
00012 // ---------------------------------------------------------------------------
00013 // Function: default constructor
00014 // Input:    none
00015 // Output:   none
00016 // ---------------------------------------------------------------------------
00017 {
00018 }
00019 
00020 CParser::~CParser ()
00021 // ---------------------------------------------------------------------------
00022 // Function: destructor
00023 // Input:    none
00024 // Output:   none
00025 // ---------------------------------------------------------------------------
00026 {
00027 }
00028 
00029 bool CParser::ReadNextLine (std::ifstream& FileInput, int& nLineNum, 
00030                             std::string& szInputString, const int MAXCHARS,
00031                             const std::string& szComment, bool bLowerCase)
00032 // ---------------------------------------------------------------------------
00033 // Function: reads the next line skipping over the comment lines
00034 //           and converts all alphabets to lower case if requested
00035 // Input:    file istream, line #, string to hold the input line,
00036 //           max. # of characters expected in each input line,
00037 //           comment character(s) at the beginning of a comment line,
00038 //           lowercase conversion option
00039 // Output:   updated values of line # and the string
00040 //           return value is true if successful
00041 //                           false if an error state is encountered
00042 // Restriction: Cannot read a line over 256 characters
00043 // ---------------------------------------------------------------------------
00044 {
00045   int flag = 0;
00046   int flag1 =0;
00047   bool bWhSpc = false;
00048   int tokenfound = 1;
00049   const int MAXCH = 10000;
00050   char szInp[MAXCH];
00051   char szTemp [MAXCH];
00052   std::vector<std::string> tokens;
00053 
00054   // enough capacity to read and store?
00055   if (MAXCHARS > MAXCH)
00056     return false;
00057 
00058   // comment character(s)
00059   int nCLen = static_cast<int>(szComment.length());
00060   // read the line (skip over comment lines)
00061   for(;;)
00062     {
00063       ++nLineNum;
00064       FileInput.getline (szInp, MAXCHARS);
00065       //       // end-of-file?
00066       //       if (FileInput.eof())
00067       //        return false;
00068       if (FileInput.fail())
00069         FileInput.clear (FileInput.rdstate() & ~std::ios::failbit);
00070       // unrecoverable error?
00071       if (FileInput.bad())
00072         return false;
00073 
00074       // successful read
00075       szInputString = szInp;
00076       GetTokens(szInputString, " ", tokens);
00077       bWhSpc = EatWhiteSpace(szInputString);
00078       if ((szInputString.substr(0,nCLen) != szComment)&& (bWhSpc ==false)){        
00079         szInputString = szInp;
00080         GetTokens(szInputString, " ", tokens);
00081         for(unsigned int i=0; i< tokens.size(); i++){
00082           std::string const& temptoken = tokens[i];
00083           if (temptoken == "&")
00084             flag1 = 1;
00085         }
00086 
00087         //Filter the comment tokens
00088         //  FilterComment(szInputString, szComment);
00089 
00090         //if "&" is found continue to read the next line      
00091         std::string szTempString = szInputString;
00092 
00093         // check if line is continued &
00094         while(flag1 ==1 && tokenfound == 1){    
00095           GetTokens(szTempString, " ", tokens);
00096           for(unsigned int i=1; i<=tokens.size(); i++){
00097             std::string const& temptoken = tokens[i-1];
00098             if (temptoken == "&"){
00099               tokenfound = 1;
00100               flag = 1;
00101             }
00102             else{
00103               if(flag==1)
00104                 flag = 1;//do nothing token already found
00105               else
00106                 tokenfound = 0;
00107             }
00108           }
00109           if(tokenfound ==1){
00110             ++nLineNum;
00111             RemoveToken(szInputString);
00112             //- getting more tokens and add to the existing 
00113             FileInput.getline (szTemp, MAXCHARS);
00114             // end-of-file?
00115             if (FileInput.eof())
00116               return false;
00117             if (FileInput.fail())
00118               FileInput.clear (FileInput.rdstate() & ~std::ios::failbit);
00119             // unrecoverable error?
00120             if (FileInput.bad())
00121               return false;
00122             // successful read 
00123             szTempString = szTemp;
00124             FilterComment(szTempString, szComment);
00125             szInputString+=" ";
00126             szInputString+=szTemp;
00127           }
00128           else{
00129             break;//while loop ents
00130           }
00131           flag = 0;
00132         }
00133         // while loop ends
00134         // convert to lower case?
00135         if (bLowerCase){
00136           for (int i=0; i < static_cast<int>(szInputString.length()); i++)
00137             szInputString[i] = tolower(szInputString[i]);
00138         }
00139         break;
00140       }
00141     }
00142   return true;
00143 }
00144 
00145 void CParser::GetTokens (const std::string& input, 
00146                          const std::string& delims, 
00147                          std::vector<std::string>& tokens)
00148 // ----------------------------------------------------------------------------
00149 // Function: Parses the input line and gets the tokens
00150 // Input:    string, delimiters
00151 // Output:   vector containing the tokens
00152 // ----------------------------------------------------------------------------
00153 {
00154   std::string::size_type beg_index, end_index;
00155 
00156   // clear the vector that will store the tokens
00157   tokens.clear();
00158 
00159   // get location of the first character that is not a delimiter
00160   beg_index = input.find_first_not_of(delims);
00161 
00162   // loop while the beginning index is not the end of string
00163   while (beg_index != std::string::npos)
00164     {
00165       // get location of the next delimiter
00166       end_index = input.find_first_of (delims, beg_index);
00167 
00168       // if this location is the end of string then adjust the value
00169       // as string length
00170       if (end_index == std::string::npos) end_index = input.length();
00171 
00172       // save the string between beg_index and end_index
00173       tokens.push_back (input.substr(beg_index,end_index-beg_index));
00174 
00175       // get location of the next character that is not a delimiter
00176       beg_index = input.find_first_not_of (delims, end_index);
00177     }
00178 }
00179 
00180 
00181 void CParser:: FilterComment (std::string& input,  const std::string& szComment)
00182 
00183 // ----------------------------------------------------------------------------
00184 // Function: Parses the input line and gets the tokens
00185 // Input:    string, delimiters
00186 // Output:   vector containing the tokens
00187 // ----------------------------------------------------------------------------
00188 {
00189   // remove comment from the line obtained
00190   unsigned int i;
00191   std::vector<std::string> tokens;
00192   std::string tempInput;
00193   GetTokens(input, " ", tokens);
00194   for(i=0; i<tokens.size(); i++){
00195     std::string const& temptoken = tokens[i];
00196     if(temptoken == szComment){
00197       break;
00198     }
00199     else{
00200       tempInput+=temptoken;
00201       if(i!=(tokens.size()-1)){ //indent{
00202         if (tokens[i+1]!=szComment)
00203           tempInput+=" ";
00204       }
00205       else{
00206         tempInput+=""; // no indent
00207       }
00208     }
00209   }
00210   input = tempInput.c_str();
00211 }
00212 
00213 void CParser:: RemoveToken (std::string& input)
00214 
00215 // ----------------------------------------------------------------------------
00216 // Function: Parses the input line and gets the tokens
00217 // Input:    string, delimiters
00218 // Output:   vector containing the tokens
00219 // ----------------------------------------------------------------------------
00220 {
00221   // remove comment from the line obtained
00222   unsigned int i;
00223   std::vector<std::string> tokens;
00224   std::string tempInput;
00225   GetTokens(input, " ", tokens);
00226   for(i=0; i<tokens.size(); i++){
00227     std::string const& temptoken = tokens[i];
00228     if(temptoken == "&"){
00229       break;
00230     }
00231     else{
00232       tempInput+=temptoken;
00233       if(i!=(tokens.size()-1)){ //indent{
00234         if (tokens[i+1]!="&")
00235           tempInput+=" ";
00236       }
00237       else{
00238         tempInput+=""; // no indent
00239       }
00240     }
00241   }
00242   input = tempInput.c_str();
00243 }
00244 
00245 bool CParser:: EatWhiteSpace (std::string& input)
00246 
00247 // ----------------------------------------------------------------------------
00248 // Function: Parses the input line and gets the tokens
00249 // Input:    string, delimiters
00250 // Output:   vector containing the tokens
00251 // ----------------------------------------------------------------------------
00252 {
00253   // remove comment from the line obtained
00254   unsigned int i;
00255   std::vector<std::string> tokens;
00256   std::string tempInput;
00257   GetTokens(input, " ", tokens);
00258   for(i=0; i<tokens.size(); i++){
00259     std::string const& temptoken = tokens[i];
00260     if(temptoken != " " && temptoken !="!"){
00261       return false;
00262       break;  
00263     }
00264     else if(temptoken =="!"){
00265       return true;
00266       break;
00267     }
00268   }
00269   return false;
00270 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines