0% found this document useful (0 votes)
62 views

"Doxygenizer.h" : #Include #Include #Include

The document describes a Doxygenizer class that processes C++ source code files and inserts Doxygen comments. The Doxygenizer opens a file, reads it line by line, and writes to a temporary file while checking for code elements to add comments for. It then copies the temporary file back to the original file location.

Uploaded by

Khan Sweetman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

"Doxygenizer.h" : #Include #Include #Include

The document describes a Doxygenizer class that processes C++ source code files and inserts Doxygen comments. The Doxygenizer opens a file, reads it line by line, and writes to a temporary file while checking for code elements to add comments for. It then copies the temporary file back to the original file location.

Uploaded by

Khan Sweetman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*******************************************************************************/

/*!
\file Screenshake.cs
\author Khan Sweetman
\par All content 2015 DigiPen (USA) Corporation, all rights reserved.
\brief
File does does things. COOL things.
*/
/*******************************************************************************/
#include "Doxygenizer.h"
#include <stdio.h>
#include <string.h>
// Syntactic shortcuts
using std::cout;
using std::cin;
using std::endl;
using std::string;
Doxygenizer::Doxygenizer() : bracesIn(0)
{
// ...
}
Doxygenizer::~Doxygenizer()
{
// ...
}
void Doxygenizer::LoadFile(std::string fileName)
{
// Open the file
m_fileName = fileName;
m_inStream.open(fileName.c_str());
m_outStream.open("temp.txt");
// Check for validity
if (m_inStream.is_open())
{
std::cout << "Opening " << fileName << "..." << std::endl;
std::string fileEnding = fileName.substr(m_fileName.size() - 3, m_fileName.size()).c_str();
if(!HeaderAlreadyDefined())
{
std::cout << "Inserting header..." << std::endl;
if(strcmp(fileEnding.c_str(), "cpp") == 0)
InsertImplementationFileHeader();
else
InsertInterfaceFileHeader();
}
// Copy the file into a temporary file, generate headers along the way
std::cout << "Processing file " << fileName << "..." << std::endl;
std::vector<std::string> lines;
std::string workingLine;

while(!m_inStream.eof())
{
std::getline(m_inStream, workingLine);
lines.push_back(workingLine);
// Check for reasons to insert Doxy comments
if(IsFunctionBeginning(workingLine))
{
// Dump all but the last two stored lines
for(unsigned i = 0; i < lines.size() - 2; ++i)
m_outStream << lines[i] << std::endl;
// Then write the header
InsertFunctionHeader(lines[lines.size() - 2]);
// Then insert the last two lines for proper alignment
m_outStream << lines[lines.size() - 2] << std::endl;
m_outStream << lines[lines.size() - 1] << std::endl;
lines.clear();
}
}
// Dump out any remaining lines
for(unsigned i = 0; i < lines.size() - 1; ++i)
m_outStream << lines[i] << std::endl;
CopyFileBack();
}
// Tell the user that the file can't be opened
else
{
std::cout << "Can't open file." << std::endl;
}

std::cout << "Closing file" << fileName << "..." << std::endl << std::endl;
m_inStream.close();
m_outStream.close();

void Doxygenizer::CopyFileBack()
{
m_inStream.close();
m_inStream.open("temp.txt");
m_outStream.close();
m_outStream.open(m_fileName.c_str());

while(!m_inStream.eof())
{
std::string line;
std::getline(m_inStream, line);
m_outStream << line << std::endl;
}

bool Doxygenizer::IsFunctionBeginning(std::string line)

int startBracesIn = bracesIn;


if(line.find("{") != string::npos)
++bracesIn;
if(line.find("}") != string::npos)
--bracesIn;
if(startBracesIn < bracesIn && startBracesIn == 0)
return true;

return false;

bool Doxygenizer::HeaderAlreadyDefined()
{
std::ifstream headerStream(m_fileName.c_str());
std::string firstLine;
std::getline(headerStream, firstLine);
bool alreadyDefined = false;
if(strcmp(firstLine.c_str(),
"/*******************************************************************************/") == 0)
alreadyDefined = true;
headerStream.close();
return alreadyDefined;
}
void Doxygenizer::InsertFunctionHeader(std::string headerLine)
{
// Get the parameters
// - Doesn't work with function pointer return types, unless they are typdef'd first
// - Doesn't work if there are extra spaces
// - Doesn't work with const
int numSpaces = 0;
size_t pos = headerLine.find_first_of("(");
size_t prevPos = pos;
std::vector<std::string> parameters;
bool onParam = false;
while(headerLine.find_first_of(" ", pos) != string::npos)
{
++numSpaces;
pos = headerLine.find_first_of(" ", pos + 1);
if(onParam)
parameters.push_back(headerLine.substr(prevPos + 1, pos - 2));
onParam = !onParam;
prevPos = pos;
}
// Get the return type
size_t index = headerLine.find_first_of(" ");
std::string returnType = headerLine.substr(0, index + 1);

m_outStream <<
"/******************************************************************************/\n";
m_outStream << "/*!\n";
m_outStream << "\\brief\n";
m_outStream << " \n";
m_outStream << " \n";
// Print the parameters
for(unsigned i = 0; i < parameters.size(); ++i)
{
m_outStream << "\\param " << parameters[i].c_str() << "\n";
//std::cout << parameters[i].c_str() << std::endl;
m_outStream << " \n";
m_outStream << "\n";
}
// Print the return type
if(strcmp(returnType.c_str(), "void") != 0)
{
m_outStream << "\\return\n";
m_outStream << " \n";
}
m_outStream << "*/\n";
m_outStream <<
"/******************************************************************************/\n";
return;
}
std::string Doxygenizer::GetDate()
{
// Get the current date/time
time_t rawTime;
struct tm * timeInfo;
time(&rawTime);
timeInfo = localtime(&rawTime);
string date = string(asctime(timeInfo)).substr(0, 11) + string(asctime(timeInfo)).substr(20, 24);

return date;

void Doxygenizer::KDebug(const char* msg)


{
std::cout << "* * * * * * * * * *" << std::endl;
std::cout << msg;
std::cout << "* * * * * * * * * *" << std::endl;
}
void Doxygenizer::InsertImplementationFileHeader()
{
m_outStream <<
"/*******************************************************************************/\n";
m_outStream << "/*!\n";
m_outStream << "\\file ";
m_outStream << m_fileName << endl;
m_outStream << "\\author Khan Sweetman\n";
m_outStream << "\\par
DP email: k.sweetman\\@digipen.edu\n";
m_outStream << "\\par
Course: --\n";

m_outStream << "\\par


Section: --\n";
m_outStream << "\\par
Assignment: --\n";
m_outStream << "\\date ";
m_outStream << GetDate() << std::endl;
m_outStream << "\n";
m_outStream << "\\brief\n";
m_outStream << "This program calculates the distance to the moon using only a compass\n";
m_outStream << "and a straight-edge. Functions include:\n";
m_outStream << "\n";
m_outStream << "- Distance\n";
m_outStream << "- Calculate\n";
m_outStream << "- Convert\n";
m_outStream << "- Convolve\n";
m_outStream << "- Contort\n";
m_outStream << "\n";
m_outStream << "Brief description of the assignment:\n";
m_outStream << "\n";
m_outStream << "Bullet point list of functions for this assignment\n";
m_outStream << "\n";
m_outStream << "Hours spent on this assignment:\n";
m_outStream << "\n";
m_outStream << "Specific portions that gave you the most trouble:\n";
m_outStream << "\n";
m_outStream << "*/\n";
m_outStream <<
"/*******************************************************************************/\n";
}
void Doxygenizer::InsertInterfaceFileHeader()
{
m_outStream <<
"/*******************************************************************************/\n";
m_outStream << "/*!\n";
m_outStream << "\\file\n";
m_outStream << "\\author Khan Sweetman\n";
m_outStream << "\\par DP email: k.sweetman\\@digipen.edu\n";
m_outStream << "\\par DigiPen login: k.sweetman\n";
m_outStream << "\\par Course: --\n";
m_outStream << "\\par Assignment #--\n";
m_outStream << "\\date ";
m_outStream.width(72);
m_outStream << GetDate() << std::endl;
m_outStream << "\\brief\n";
m_outStream << " This is the interface file for all member functions\n";
m_outStream << " of a class called Stack.\n";
m_outStream << "\n";
m_outStream << "*/\n";
m_outStream <<
"/******************************************************************************/\n";
}
/*************************************************************************/
/*!
\brief
Doxygenizes specified files
\param argc

Number of arguments passed to main


\param argv
Arguments. Passed as an array of pointers to NUL-terminated strings.
First element in argv is the name of the program.
*/
/*************************************************************************/
int main(int argc, char *argv[])
{
Doxygenizer doxy;
for(int i = 1; i < argc; ++i)
{
doxy.LoadFile(argv[i]);
}
return 0;
}

You might also like