Sierra Toolkit  Version of the Day
PreParse.hpp
1 #ifndef STK_UTIL_DIAG_PreParse_hpp
2 #define STK_UTIL_DIAG_PreParse_hpp
3 
4 #include <string>
5 #include <iostream>
6 #include <iomanip>
7 #include <sstream>
8 #include <fstream>
9 #include <stdexcept>
10 #include <vector>
11 #include <boost/regex.hpp>
12 
13 namespace sierra {
14 
15  inline bool CaseInSensitiveRegexInFile( const std::string& regExp, const std::string& file, bool debug = false ) {
16  std::ifstream fileStream(file.c_str());
17  if ( fileStream.bad() ) {
18  std::cerr << "Unable to open file " << file << std::endl;
19  }
20  std::string line;
21  boost::regex re;
22  re.assign(regExp, boost::regex_constants::icase);
23  while( std::getline( fileStream, line ) ) {
24  if ( boost::regex_search(line,re) ) {
25  if ( debug ) {
26  std::cout << "CaseInSensitiveRegexInFile() found: " << std::endl << line << std::endl;
27  std::cout << "CaseInSensitiveRegexInFile() with: " << std::endl << regExp << std::endl;
28  }
29  return true;
30  }
31  }
32  return false;
33  }
34 
35  inline std::vector< std::vector< std::string > > ExtractCommandBlocksInFile( const std::string& beginRegExp, const std::string& file, bool debug = false ) {
36  std::vector< std::vector< std::string > > extractedCommandBlocks;
37  std::vector< std::string > extractedCommandBlock;
38  std::ifstream fileStream(file.c_str());
39  if ( fileStream.bad() ) {
40  std::cerr << "Unable to open file " << file << std::endl;
41  }
42  std::string line;
43  boost::regex reBeginStart;
44  reBeginStart.assign(beginRegExp, boost::regex_constants::icase);
45  boost::regex reBegin;
46  reBegin.assign("^\\s*begin\\>", boost::regex_constants::icase);
47  boost::regex reEnd;
48  reEnd.assign("^\\s*end\\>", boost::regex_constants::icase);
49  bool extract = false;
50  int numOpen = 0;
51  while( std::getline( fileStream, line ) ) {
52  if ( !extract && boost::regex_search(line,reBeginStart) ) {
53  extract = true;
54  if ( debug ) {
55  std::cout << "ExtractCommandBlocksInFile() started: " << std::endl << line << std::endl;
56  }
57  }
58  if ( extract ) {
59  extractedCommandBlock.push_back(line);
60  if ( boost::regex_search(line,reBegin) ) {
61  numOpen += 1;
62  } else if ( boost::regex_search(line,reEnd) ) {
63  numOpen -= 1;
64  }
65  if ( numOpen == 0 ) {
66  extract = false;
67  extractedCommandBlocks.push_back(extractedCommandBlock);
68  extractedCommandBlock.clear();
69  if ( debug ) {
70  std::cout << "ExtractCommandBlocksInFile() stopped: " << std::endl << line << std::endl;
71  }
72  }
73  }
74  }
75  return extractedCommandBlocks;
76  }
77 
78  std::string CreateSubCycleInputFile( const std::string& ifile, bool debug = false );
79 
80 } // namespace sierra
81 
82 #endif // STK_UTIL_DIAG_PreParse_hpp
Definition: Env.cpp:53
std::istream & getline(std::istream &is, sierra::String &s, char eol)
Function getline returns a string from the input stream which has been terminated by the newline char...
Definition: StringUtil.cpp:33