samedi 30 mai 2020

C++ Regex for getting all the header files used in a c program

So far I have found that using grep I can find the header names in a C program.

Regular expression to extract header name from c file

But in the above case the accepted answer uses grep command with some flags. But I want to use this regex in regex_search() of C++ programmatically. But I am unable to convert the regex in the above answer to fit in C++. I want to know how can I convert the above regex to be used in C++.

regex expr(".*#include.*(<|\")\\K.*(?=>|\")");

I have written above regex but it doesn't give the expected output. Basically I read the C file as string and use C++ regex search to extract header names data. Below code explains what I am trying to do...


#include<bits/stdc++.h>

using namespace std;

void searchContent(string content) {
     smatch match;
     regex expr(".*#include.*(<|\")\\K.*(?=>|\")");

     while (regex_search(content, match, expr)) {
          for (auto x : match)
               cout << x << " ";

           cout << std::endl;
           content = match.suffix().str();
     }
}


int main(int argc, char** argv)
{

  std::ifstream ifs("/home/ultraproton/PlacementPrep/C++/regex/testfile.c");
  std::string content( (std::istreambuf_iterator<char>(ifs) ),
                       (std::istreambuf_iterator<char>()    ) );


  searchContent(content);

  return 0;
}

Aucun commentaire:

Enregistrer un commentaire