Sierra Toolkit Version of the Day
PreParse.hpp
00001 #ifndef STK_UTIL_DIAG_PreParse_hpp
00002 #define STK_UTIL_DIAG_PreParse_hpp
00003 
00004 #include <string>
00005 #include <iostream>
00006 #include <iomanip>
00007 #include <sstream>
00008 #include <fstream>
00009 #include <stdexcept>
00010 #include <vector>
00011 #include <boost/regex.hpp>
00012 
00013 namespace sierra {
00014 
00015   inline bool CaseInSensitiveRegexInFile( const std::string& regExp, const std::string& file, bool debug = false ) {
00016     std::ifstream fileStream(file.c_str());
00017     if ( fileStream.bad() ) {
00018       std::cerr << "Unable to open file " << file << std::endl;
00019     }
00020     std::string line;
00021     boost::regex re;
00022     re.assign(regExp, boost::regex_constants::icase);
00023     while( std::getline( fileStream, line ) ) {
00024       if ( boost::regex_search(line,re) ) {
00025         if ( debug ) {
00026           std::cout << "CaseInSensitiveRegexInFile() found: " << std::endl << line << std::endl;
00027           std::cout << "CaseInSensitiveRegexInFile() with: " << std::endl << regExp << std::endl;
00028         }
00029         return true;
00030       }
00031     }
00032     return false;
00033   }
00034 
00035   inline std::vector< std::vector< std::string > > ExtractCommandBlocksInFile( const std::string& beginRegExp, const std::string& file, bool debug = false ) {
00036     std::vector< std::vector< std::string > > extractedCommandBlocks;
00037     std::vector< std::string > extractedCommandBlock;
00038     std::ifstream fileStream(file.c_str());
00039     if ( fileStream.bad() ) {
00040       std::cerr << "Unable to open file " << file << std::endl;
00041     }
00042     std::string line;
00043     boost::regex reBeginStart;
00044     reBeginStart.assign(beginRegExp, boost::regex_constants::icase);
00045     boost::regex reBegin;
00046     reBegin.assign("^\\s*begin\\>", boost::regex_constants::icase);
00047     boost::regex reEnd;
00048     reEnd.assign("^\\s*end\\>", boost::regex_constants::icase);
00049     bool extract = false;
00050     int numOpen = 0;
00051     while( std::getline( fileStream, line ) ) {
00052       if ( !extract && boost::regex_search(line,reBeginStart) ) {
00053         extract = true; 
00054         if ( debug ) {
00055           std::cout << "ExtractCommandBlocksInFile() started: " << std::endl << line << std::endl;
00056         }
00057       }
00058       if ( extract ) {
00059         extractedCommandBlock.push_back(line);
00060         if ( boost::regex_search(line,reBegin) ) {
00061           numOpen += 1;
00062         } else if ( boost::regex_search(line,reEnd) ) {
00063           numOpen -= 1;
00064         }
00065         if ( numOpen == 0 ) {
00066           extract = false;
00067           extractedCommandBlocks.push_back(extractedCommandBlock);
00068           extractedCommandBlock.clear();
00069           if ( debug ) {
00070             std::cout << "ExtractCommandBlocksInFile() stopped: " << std::endl << line << std::endl;
00071           }
00072         }
00073       }
00074     }
00075     return extractedCommandBlocks;
00076   }
00077 
00078   std::string CreateSubCycleInputFile( const std::string& ifile, bool debug = false );
00079 
00080 } // namespace sierra
00081 
00082 #endif // STK_UTIL_DIAG_PreParse_hpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines