How to read every string in a visual studio program? I need to read all of them, preferably at the same time, searching for a certian phrase. Something like:
This simplified example won't take care of raw string literals, won't concatenate adjacent strings, and ignores preprocessor directives like #include #ifdef etc. It also won't handle strings that span across multiple lines (std::regex_constants::multiline is not particularly well supported)
#include <iostream>
#include <string>
#include <regex>
#include <vector>
#include <fstream>
// get quoted strings in a line of text
std::vector<std::string> get_quoted_strings( const std::string& line )
{
// an opening quote character
// zero or more occurrences of either "any non-quote non-backslash character" or "a backslash followed by any character"
// a closing quote character
staticconst std::regex quoted_string_re( R"("(?:[^"\\]|\\.)*")" ) ;
using iterator = std::sregex_iterator ;
static const iterator end ;
std::vector<std::string> result ;
for( iterator iter( line.begin(), line.end(), quoted_string_re ) ; iter != end ; ++iter )
result.push_back( iter->str() ) ;
return result ;
}
// get quoted strings in all lines
std::vector<std::string> get_quoted_strings( std::istream& stm )
{
std::vector<std::string> result ;
std::string line ;
while( std::getline( stm, line ) )
for( const auto& str : get_quoted_strings(line) ) result.push_back(str) ;
return result ;
}
int main()
{
const std::string str = "string #one" "string number two" "and a string three" ;
"this is a long fourth string""finally, a fifth string containing \"escaped\" quotes" ;
"a line with a couple of """"empty""""strings." ;
std::ifstream this_file(__FILE__) ;
for( const std::string& str : get_quoted_strings(this_file) ) std::cout << str << '\n' ;
}