Skip to content

Commit 947776e

Browse files
committed
use regex to find namespace
1 parent 2f303de commit 947776e

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

drogon_ctl/create_swagger.cc

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929
#include <direct.h>
3030
#endif
3131
#include <fstream>
32-
33-
#include <clang-c/Index.h>
32+
#include <regex>
3433

3534
using namespace drogon_ctl;
3635

@@ -69,7 +68,8 @@ static void forEachControllerHeaderIn(
6968
else
7069
{
7170
auto fileName = itr.path().string();
72-
if (fileName.find(".h") == fileName.length() - 2)
71+
if (fileName.find(".h") == fileName.length() - 2 ||
72+
fileName.find(".hpp") == fileName.length() - 4)
7373
{
7474
cb(fileName);
7575
}
@@ -80,20 +80,32 @@ static void forEachControllerHeaderIn(
8080
static void parseControllerHeader(const std::string &headerFile,
8181
Json::Value &docs)
8282
{
83-
CXIndex index = clang_createIndex(0, 0);
84-
CXTranslationUnit unit = clang_parseTranslationUnit(
85-
index,
86-
headerFile.c_str(), nullptr, 0,
87-
nullptr, 0,
88-
CXTranslationUnit_None);
89-
if (unit == nullptr)
83+
std::ifstream infile(utils::toNativePath(headerFile),
84+
std::ifstream::binary);
85+
if (!infile)
9086
{
91-
std::cerr << "Unable to parse translation unit. Quitting." << std::endl;
92-
exit(-1);
87+
std::cout << "can't open the header file:" << headerFile << "\n";
88+
return;
9389
}
90+
std::streambuf *pbuf = infile.rdbuf();
91+
std::streamsize filesize = pbuf->pubseekoff(0, std::ifstream::end);
92+
pbuf->pubseekoff(0, std::ifstream::beg); // rewind
93+
std::string fileContent;
94+
fileContent.resize(filesize);
95+
pbuf->sgetn(&fileContent[0], filesize);
96+
std::cout << fileContent;
9497

95-
clang_disposeTranslationUnit(unit);
96-
clang_disposeIndex(index);
98+
std::regex rx(R"(namespace[ \r\n]+([^ \r\n]+)[ \r\n]*\{)");
99+
std::smatch results;
100+
while (std::regex_search(fileContent, results, rx))
101+
{
102+
if (results.size() > 1)
103+
{
104+
for (int i = 1; i < results.size(); i++)
105+
std::cout << "nnn:" << i << results[i] << std::endl;
106+
}
107+
fileContent = results.suffix();
108+
}
97109
}
98110
static std::string makeSwaggerDocument(const Json::Value &config)
99111
{

0 commit comments

Comments
 (0)