|
12 | 12 | * |
13 | 13 | */ |
14 | 14 |
|
| 15 | +#include "../lib/src/filesystem.h" |
15 | 16 | #include "create_swagger.h" |
16 | 17 | #include <drogon/DrTemplateBase.h> |
17 | 18 | #include <drogon/utils/Utilities.h> |
|
30 | 31 | #include <fstream> |
31 | 32 |
|
32 | 33 | using namespace drogon_ctl; |
| 34 | +using drogon::filesystem; |
33 | 35 |
|
34 | 36 | static void forEachControllerHeaderIn( |
35 | | - const std::string &path, |
| 37 | + std::string strPath, |
36 | 38 | const std::function<void(const std::string &)> &cb) |
37 | 39 | { |
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 | + } |
41 | 52 |
|
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)) |
44 | 59 | { |
45 | | - // perror("opendir:"); |
46 | | - LOG_ERROR << "can't open dir,path:" << path; |
47 | 60 | return; |
48 | 61 | } |
49 | | - |
50 | | - /** |
51 | | - * read all files in this dir |
52 | | - **/ |
53 | | - while ((dirp = readdir(dp)) != NULL) |
| 62 | + for (auto &itr : filesystem::directory_iterator(fsPath)) |
54 | 63 | { |
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())) |
65 | 65 | { |
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); |
75 | 67 | } |
76 | 68 | else |
77 | 69 | { |
78 | | - cb(fullname); |
| 70 | + cb(itr.path().string()); |
79 | 71 | } |
80 | 72 | } |
81 | | - closedir(dp); |
82 | 73 | return; |
83 | 74 | } |
84 | 75 | static void parseControllerHeader(const std::string &headerFile, |
|
0 commit comments