|
30 | 30 | #include <fstream> |
31 | 31 |
|
32 | 32 | using namespace drogon_ctl; |
| 33 | + |
| 34 | +static void forEachControllerHeaderIn( |
| 35 | + const std::string &path, |
| 36 | + const std::function<void(const std::string &)> &cb) |
| 37 | +{ |
| 38 | + DIR *dp; |
| 39 | + struct dirent *dirp; |
| 40 | + struct stat st; |
| 41 | + |
| 42 | + /* open dirent directory */ |
| 43 | + if ((dp = opendir(path.c_str())) == NULL) |
| 44 | + { |
| 45 | + // perror("opendir:"); |
| 46 | + LOG_ERROR << "can't open dir,path:" << path; |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * read all files in this dir |
| 52 | + **/ |
| 53 | + while ((dirp = readdir(dp)) != NULL) |
| 54 | + { |
| 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) |
| 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); |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + cb(fullname); |
| 79 | + } |
| 80 | + } |
| 81 | + closedir(dp); |
| 82 | + return; |
| 83 | +} |
| 84 | +static void parseControllerHeader(const std::string &headerFile, |
| 85 | + Json::Value &docs) |
| 86 | +{ |
| 87 | + std::ifstream infile(headerFile); |
| 88 | + |
| 89 | + for (std::string buffer; std::getline(infile, buffer);) |
| 90 | + { |
| 91 | + } |
| 92 | +} |
33 | 93 | static std::string makeSwaggerDocument(const Json::Value &config) |
34 | 94 | { |
35 | 95 | Json::Value ret; |
36 | 96 | ret["swagger"] = "2.0"; |
37 | 97 | ret["info"] = config.get("info", {}); |
| 98 | + forEachControllerHeaderIn("controllers", [&ret](const std::string &header) { |
| 99 | + std::cout << "Parsing " << header << " ...\n"; |
| 100 | + parseControllerHeader(header, ret); |
| 101 | + }); |
38 | 102 | return ret.toStyledString(); |
39 | 103 | } |
40 | 104 |
|
|
0 commit comments