I use various regular expressions to parse the source C file line by line. First I read the entire contents of the file in a line:
ifstream file_stream("commented.cpp",ifstream::binary);
std::string txt((std::istreambuf_iterator<char>(file_stream)),
std::istreambuf_iterator<char>());
Then I use a set of regular expressions, which should be applied continuously until it finds a match, here I will give only one example:
vector<regex> rules = { regex("^//[^\n]*$") };
char * search =(char*)txt.c_str();
int position = 0, length = 0;
for (int i = 0; i < rules.size(); i++) {
cmatch match;
if (regex_search(search + position, match, rules[i],regex_constants::match_not_bol | regex_constants::match_not_eol))
{
position += ( match.position() + match.length() );
}
}
But that will not work. It will correspond to the comment not in the current line, but it will search for the whole line for the first match regex_constants::match_not_boland regex_constants::match_not_eolshould make it regex_searchrecognize ^$only as the beginning / end of the line, and not end start / end of the entire block. So here is my file:
commented.cpp:
#include <stdio.h>
, regex_search, , :
#include <stdio.h>
//comment. , regex_search . match_not_bol match_not_eol . , , , , , , , regex .