-
Notifications
You must be signed in to change notification settings - Fork 0
/
doc_gen.cpp
244 lines (219 loc) · 5.5 KB
/
doc_gen.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
@progName HTML Documentator
@desc Crea Documentación de archivos en HTML
@author Kevin Chinchilla
A00825945
@date 15 de febrero, 2019
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Global variables
string filename;
/**
@funcName parseLine
@desc Revisa si la línea tiene una tag de documentación, y elimina los espacios en blanco antes de la tag si existen
@param La string a revisar
@return La línea, si contiene el tag, o nulo, si no
*/
string parseLine(string buffer)
{
for (unsigned short int i = 1; i < buffer.size(); i++ )
{
if (buffer[i] != 32 && buffer[i] != 9)
{
if (buffer[i] == 64)
{
buffer = buffer.substr(i);
return buffer;
}
else
{
i = buffer.size();
return "\0";
}
}
}
return "\0";
}
/**
@funcName findComments
@desc Busca comentarios en el formato correcto para documentar
@param Un arreglo de strings donde se guardaran los comentarios para procesar, y el tamaño de este arreglo
@return Verdadero, si todo sale bien, falso si no encuentra el archivo a revisar
*/
bool findComments(string comments[1000], int &size)
{
size = 0;
ifstream file;
file.open (filename.c_str());
if (!file.is_open())
{
cout << "No se encontro el archivo " << filename;
return false;
}
string buffer;
while (getline(file, buffer))
{
if (buffer.find("/**") != buffer.npos)
{
do
{
getline(file, buffer);
if ( buffer.find('@') != buffer.npos || buffer.find("*/") != buffer.npos)
{
if ( buffer[0] == '@' || buffer.find("*/") != buffer.npos)
{
comments[size++] = buffer;
}
else if (buffer[0] == 32 || buffer[0] == 9)
{
buffer = parseLine(buffer);
if (buffer != "\0")
{
comments[size++] = buffer;
}
}
}
}
while ( buffer.find("*/") > 1);
}
}
file.close();
return true;
}
/**
@funcName findTitle
@desc Busca la tag que contiene el nombre del programa
@param El arreglo de strings con los comentarios a procesar, y el tamaño de este.
@return La posición del arreglo donde está el nombre del programa, o -1 si no se euncuentra un nombre para el programa
*/
int findTitle(string comments [1000], int size)
{
for ( short int i = 0; i < size; i++)
{
if (comments[i].find("@progName") != comments[i].npos)
{
return i;
}
}
return -1;
}
/**
@funcName writeSection
@desc Escribe una sección de docmentación que incluyen autor, fecha de creación, parámetros y valor de retorno
@param El stream para escribir el archivo, la posición del arreglo dónde inicia la sección, y el arreglo con los comentarios a procesar
@return NONE
*/
void writeSection ( ofstream &oFile, int pos, string comments[1000])
{
string buffer = comments[pos++];
while (buffer.find("*/") == buffer.npos)
{
int tagEnd = buffer.find(" ");
if (buffer.substr(0,tagEnd) == "@desc")
{
oFile << "<strong>Descripición:</strong> " << buffer.substr(tagEnd + 1) << "<br>\n";
}
if (buffer.substr(0,tagEnd) == "@author")
{
oFile << "<strong>Autor:</strong> " << buffer.substr(tagEnd + 1) << "<br>\n";
}
if (buffer.substr(0,tagEnd) == "@date")
{
oFile << "<strong>Fecha de elaboración:</strong> " << buffer.substr(tagEnd + 1) << "<br>\n";
}
if (buffer.substr(0,tagEnd) == "@param")
{
oFile << "<strong>Parametros:</strong> " << buffer.substr(tagEnd + 1) << "<br>\n";
}
if (buffer.substr(0,tagEnd) == "@return")
{
oFile << "<strong>Valor de retorno:</strong> " << buffer.substr(tagEnd + 1) << "<br>\n";
}
buffer = comments[pos++];
}
oFile << "<hr>";
}
/**
@funcName writeHTML
@desc Escribe el archivo HTML con la documentación
@param El arreglo con los comentarios a procesar, y el tamaño de este
@return NONE
*/
void writeHTML( string comments[1000], int size)
{
ofstream oFile;
string htmlName = filename.substr(0, filename.size()-3);
htmlName.append("html");
oFile.open(htmlName.c_str());
oFile << "<!DOCTYPE html>\n<html>\n";
//write header
string buffer;
oFile << "<head>\n";
oFile << "<meta charset=\"UTF-8\">\n";
oFile << "<title>Documentación del archivo " << filename << "</title>\n";
oFile << "</head>\n";
oFile << "<body>\n";
//make title
int counter = findTitle(comments, size);
if (counter > -1)
{
int bufferStart = comments[counter].find(" ");
buffer = comments[counter].substr(bufferStart+1);
}
else
{
buffer = filename.substr(0, filename.size()-4);
}
oFile << "<h2>\nPrograma: " << buffer << "<br>\n</h2>\n";
writeSection (oFile, counter, comments);
//rest of the program
for (short int i = 0; i < size; i++)
{
buffer = comments[i];
int tagEnd = buffer.find(" ");
if (buffer.substr(0,tagEnd) == "@funcName")
{
oFile << "<h3>\nFunción: " << buffer.substr(buffer.find(" ")) << "<br>\n</h3>\n";
writeSection(oFile, i, comments);
}
}
oFile << "</body>\n";
oFile << "</html>";
oFile.close();
}
/**
@funcName main
@desc Llama todas las otras funciones
@param Se puede pasar como parametro el nombre del programa a revisar
@return 0
*/
int main (int argc, char *argv[])
{
if (argc < 2)
{
cout << "No se entraron parametros. \nNota: El programa también funciona si el archivo que se desea documentar se pasa como parametro.\n";
//Talvez añadir flag de -h
cin >> filename;
filename.append(".cpp");
}
else
{
filename = argv[1];
}
//Variables
string comments[1000];
int size;
bool check;
//Input
check = findComments(comments, size);
if (!check)
{
return 0;
}
//Output
writeHTML(comments, size);
return 0;
}