-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiceDocsGenerator.cpp
More file actions
175 lines (145 loc) · 4.43 KB
/
iceDocsGenerator.cpp
File metadata and controls
175 lines (145 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>
#include <vector>
using namespace std;
int getLastThing(string token, char lookFor) {
for (int c = token.length() - 1; c >= 0; c--) {
if (token[c] == lookFor) {
return c;
}
}
return 0;
}
// FIXME: pairs break the system because of ", ".
void processFile(const string& filePath, ofstream& outFile) {
ifstream file(filePath);
string line;
string currentComment = "";
bool comment = false;
string returnsStr = "N/A";
vector<pair<string, string>> params;
while (getline(file, line)) {
// NOT MY FILE
if (line.substr(0, 70) == "/*********************************************************************") {
return;
}
// cout << line << " [Comment: " << comment << "]\n";
if (comment) {
if (line.length() < 3) {
comment = false;
currentComment = "";
returnsStr = "N/A";
params.clear();
continue;
}
string trueLine = line.substr(3);
int param = trueLine.find("@param");
int returns = trueLine.find("@return");
if (param != string::npos) {
string paramStr = trueLine.substr(6);
int paramStart = paramStr.find("{");
int paramEnd = paramStr.find("}");
params.push_back({paramStr.substr(paramStart + 1, paramEnd - paramStart - 1), paramStr.substr(paramEnd + 2)});
}
else if (returns != string::npos) {
returnsStr = trueLine.substr(7);
if (returnsStr[0] == ' ') {
returnsStr = returnsStr.substr(1);
}
}
else {
currentComment += trueLine;
}
}
if (line.length() >= 3 && line.substr(0, 3) == "/**") {
comment = true;
}
if (line.length() >= 3 && line.substr(0, 3) == " */") {
comment = false;
}
int colon = line.find(" : ");
if (colon != string::npos) {
line = line.substr(0, colon);
}
int argStart = line.find("(");
int argEnd = getLastThing(line, ')');
if (line.length() >= 1 && line.at(0) != '\t' && argStart != string::npos && argEnd != string::npos) {
string start = line.substr(0, argStart);
int lastSpace = getLastThing(start, ' ');
string returnType = line.substr(0, lastSpace);
int titleStart = (lastSpace == 0) ? lastSpace : lastSpace + 1;
string functionName = line.substr(titleStart, argStart - titleStart);
if (functionName != "") {
outFile << "### " << functionName << endl;
if (currentComment != "") {
outFile << "*" << currentComment << "*" << endl;
}
string args = line.substr(argStart + 1, argEnd - argStart - 1);
outFile << "| **Type** | **Variable Name** | **Description** | \n";
outFile << "| :------: | :---------------: | :-------------: | \n";
while (args != "") {
int pos = args.find(", ");
if (pos == string::npos) {
pos = args.length();
}
string token = args.substr(0, pos);
lastSpace = getLastThing(token, ' ');
string type = token.substr(0, lastSpace);
string varName = token.substr(lastSpace + 1);
string description = "N/A";
for (pair<string, string> pss : params) {
if (pss.first == varName) {
description = pss.second;
}
}
// For a function that is passed in as an argument
int where = varName.find("(*");
if (where != string::npos) {
varName = varName.substr(0, where) + "(\\*" + varName.substr(where + 2);
}
outFile << "| " << type << " | " << varName << " | " << description << " |\n";
args.erase(0, pos + 2);
}
outFile << "\n**Return Type:** " << returnType << " (" << returnsStr << ")\n\n\n";
}
currentComment = "";
returnsStr = "N/A";
params.clear();
}
}
}
void processDirectory(const string& dirPath, ofstream& outFile) {
DIR* dir = opendir(dirPath.c_str());
if (!dir) {
cerr << "Failed to open directory: " << dirPath << endl;
return;
}
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
string fileName = entry->d_name;
if (fileName == "." || fileName == "..") {
continue;
}
string filePath = dirPath + "/" + fileName;
if (fileName.size() > 4 && fileName.substr(fileName.size() - 4) == ".cpp") {
cout << "Processing " << filePath << endl;
outFile << "## " << filePath << endl;
processFile(filePath, outFile);
}
}
closedir(dir);
}
int main() {
ofstream outFile("README.md");
if (!outFile) {
cerr << "Failed to open output file." << endl;
return 1;
}
outFile << "# Project Documentation\n\n";
processDirectory("src", outFile);
outFile.close();
cout << "Documentation generated in README.md" << endl;
return 0;
}