Auto comment code

I'm working on a program for my cs class and it has to auto comment the code. We have set comments like //header , //function , //var , and we have to replace them with proper comments from a text file.

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstring>
using namespace std;

//function
void openFiles(ifstream& input_file, ifstream& header_file, ifstream& function_file, ofstream& output_file);

//function
void add_coments(ifstream& input_file, ifstream& header_file, ifstream& function_file, ofstream& output_file);

//function
void close_files(ifstream& input_file, ifstream& header_file, ifstream& function_file, ofstream& output_file);

int main()
{
//var
ifstream input_file;
//var
ifstream header_file;
//var
ifstream function_file;
//var
ofstream output_file;

openFiles(input_file, header_file, function_file, output_file);
add_coments(input_file, header_file, function_file, output_file);
close_files(input_file, header_file, function_file, output_file);

return (EXIT_SUCCESS);
}

void openFiles(ifstream& input_file, ifstream& header_file, ifstream& function_file, ofstream& output_file)
{
//var
string input_file_name;
//var
string header_file_name;
//var
string function_file_name;
//var
string output_file_name;
cout << "Enter the file that you wish to add comments.\n";
cin >> input_file_name;

input_file.open(input_file_name.c_str());
if (input_file.fail())
{
cout << "ERROR FILE FAILED TO OPEN EXITING PROGRAM\n";
exit(EXIT_FAILURE);
}

header_file.open("header.txt");
if (header_file.fail())
{
cout << "ERROR FILE FAILED TO OPEN EXITING PROGRAM\n";
exit(EXIT_FAILURE);
}

function_file.open("function.txt");
if (function_file.fail())
{
cout << "ERROR FILE FAILED TO OPEN EXITING PROGRAM\n";
exit(EXIT_FAILURE);
}

cout << "Enter the file that you wish create.\n";
cin >> output_file_name;

output_file.open(output_file_name.c_str());
if (output_file.fail())
{
cout << "ERROR FILE FAILED TO OPEN EXITING PROGRAM\n";
exit(EXIT_FAILURE);
}
}

void add_coments(ifstream& input_file, ifstream& header_file, ifstream& function_file, ofstream& output_file)
{
//var
char content;
//used to hold the content of the file
//var
char header_content;
//used to hold the contents of the header file
//var
char function_content;
//used to hold the contents of the function file
//var
string line;
//used to compare the lines with either header, function, or var strings
//var
string header_line = "//header";
//used to check if the header comment is present and replaces it with the proper comment
//var
string function_line = "//function";
//used to check if the function comment is present and replaces it with the proper comment
//var
string var_line = "//var";
//used to check if the var comment is present and changes the var comment.

while (!input_file.eof())
{
if (strcmp(line.c_str(), header_line.c_str()) == 0)
{
header_file.get(header_content);
output_file << header_content;
}

input_file.get(content);
output_file << content;

if (strcmp(line.c_str(), function_line.c_str()) == 0)
{
function_file.get(function_content);
output_file << function_content;
}
if (strcmp(line.c_str(), var_line.c_str()) == 0)
{
skip_line = true;
output_file << line << setw(30) << "//" << endl;
}
}
}

void close_files(ifstream& input_file, ifstream& header_file, ifstream& function_file, ofstream& output_file)
{
input_file.close();
header_file.close();
function_file.close();
output_file.close();
}
Last edited on
If I understand you correctly, you're trying to find and replace potentially multiple substrings from a given string – I first found this SO link:
http://stackoverflow.com/questions/3895006/c-replace-multiple-strings-in-a-string-in-a-single-pass
and through above link I got here which is a more 'traditional' solution:
https://www.codeproject.com/kb/string/replacemany.aspx
However std::regex could also be an alternative though bear in mind that having upto 3 std::regex objects in your program might incur a performance penalty:
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
#include <iostream>
#include <regex>
#include <fstream>
#include <string>

int main()
{
    std::ifstream inFile{"D:\\test.txt"};
    std::string line{};
    if (inFile)
    {
        while(getline(inFile, line))
        {
            std::regex regHeader("//header");
            std::regex regVar("//var");
            std::regex regFunc("//function");
            line = std::regex_replace(line, regHeader, "proper comments for header ");
            line = std::regex_replace(line, regVar, "proper comments for variable " );
            line = std::regex_replace(line, regFunc, "proper comments for function ");
            std::cout << line;
        }
    }
}
/*
sample text:
//header//var 
output: 
proper comments for header proper comments for variable
Process returned 0 (0x0)   execution time : 0.188 s
Press any key to continue.*/

Topic archived. No new replies allowed.