Skip to content

Commit 28c47b6

Browse files
committed
update
1 parent 9b9be33 commit 28c47b6

1 file changed

Lines changed: 25 additions & 34 deletions

File tree

drogon_ctl/create_swagger.cc

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*
1313
*/
1414

15+
#include "../lib/src/filesystem.h"
1516
#include "create_swagger.h"
1617
#include <drogon/DrTemplateBase.h>
1718
#include <drogon/utils/Utilities.h>
@@ -30,55 +31,45 @@
3031
#include <fstream>
3132

3233
using namespace drogon_ctl;
34+
using drogon::filesystem;
3335

3436
static void forEachControllerHeaderIn(
35-
const std::string &path,
37+
std::string strPath,
3638
const std::function<void(const std::string &)> &cb)
3739
{
38-
DIR *dp;
39-
struct dirent *dirp;
40-
struct stat st;
40+
while (1)
41+
{
42+
char cEnd = *strPath.rbegin();
43+
if (cEnd == '\\' || cEnd == '/')
44+
{
45+
strPath = strPath.substr(0, strPath.length() - 1);
46+
}
47+
else
48+
{
49+
break;
50+
}
51+
}
4152

42-
/* open dirent directory */
43-
if ((dp = opendir(path.c_str())) == NULL)
53+
if (strPath.empty() || strPath == (".") || strPath == (".."))
54+
return;
55+
56+
std::error_code ec;
57+
filesystem::path fsPath(strPath);
58+
if (!filesystem::exists(strPath, ec))
4459
{
45-
// perror("opendir:");
46-
LOG_ERROR << "can't open dir,path:" << path;
4760
return;
4861
}
49-
50-
/**
51-
* read all files in this dir
52-
**/
53-
while ((dirp = readdir(dp)) != NULL)
62+
for (auto &itr : filesystem::directory_iterator(fsPath))
5463
{
55-
/* ignore hidden files */
56-
if (dirp->d_name[0] == '.')
57-
continue;
58-
/* get dirent status */
59-
std::string filename = dirp->d_name;
60-
if (filename.find(".h") != filename.length() - 2)
61-
continue;
62-
std::string fullname = path;
63-
fullname.append("/").append(filename);
64-
if (stat(fullname.c_str(), &st) == -1)
64+
if (filesystem::is_directory(itr.status()))
6565
{
66-
perror("stat");
67-
closedir(dp);
68-
return;
69-
}
70-
71-
/* if dirent is a directory, find files recursively */
72-
if (S_ISDIR(st.st_mode))
73-
{
74-
forEachControllerHeaderIn(fullname, cb);
66+
forEachControllerHeaderIn(itr.path().string(), cb);
7567
}
7668
else
7769
{
78-
cb(fullname);
70+
cb(itr.path().string());
7971
}
8072
}
81-
closedir(dp);
8273
return;
8374
}
8475
static void parseControllerHeader(const std::string &headerFile,

0 commit comments

Comments
 (0)