CS396-Projects/assignmentFive/htmlConvert.cpp

104 lines
No EOL
2.8 KiB
C++

/*
Program Name: HTML Converter
Process: ???
Function Input/Parameters: ???
Function Output/Parameters: ???
Function Output/Returned: ???
Device Input/Device: ???
Device Output/Monitor: ???
Dependencies: ???
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <cstring>
#include <vector>
#include <random>
#include <algorithm>
#include <time.h>
using namespace std;
// class definitions
// function prototypes
// main program
int main(int argc, char **argv)
{
string fileName;
filesystem::path filePath;
ifstream inputFile;
ofstream outputFile;
char currentChar;
// menu for user
cout << "CPP to HTML Converter" << endl;
// ask the user for a cpp file. (check to see if valid?)
cout << "Please enter a CPP source file: ";
cin >> fileName;
// check if the file has the .cpp extension
filePath = fileName;
if(filePath.extension() != ".cpp" && filePath.extension() != ".cp" &&
filePath.extension() != ".C" && filePath.extension() != ".cc")
{
cout << "Invalid File, please enter something else and try again." << endl;
return -1;
}
// check if the file exists
if(!filesystem::exists(fileName))
{
cout << "File is valid, but does not exist." << endl;
return -1;
}
// the file exists, start the parsing according to the comment below.
inputFile.open(fileName);
outputFile.open(filePath.replace_extension(".html"));
// add the <pre> tag, read each character, check to see if the character is one that needs to be
// modified, like '<' and '>' and turned into '&lt;' and '&gt;' once at the end of input file,
// add the </pre> tag, and output to a new html file that uses the file name
// and rids itself of everything after the dot and replaces it with '.html' and writes it out.
// add the '<pre>' tag to the output file
outputFile << "<pre>" << endl;
// read in the characters one at a time, check if the character is a '<' or a '>' and replace.
currentChar = inputFile.get();
// outputFile << (char)inputFile.get();
while(!inputFile.eof())
{
if(currentChar == '<' || currentChar == '>')
{
//
if(currentChar == '<')
{
outputFile << "&lt;";
}
else
{
outputFile << "&gt;";
}
}
else
{
outputFile << currentChar;
}
currentChar = inputFile.get();
// outputFile << (char)inputFile.get();
}
// add the '</pre> tag to the output file
outputFile << "</pre>";
// close the input and output files
inputFile.close();
outputFile.close();
// return 0
return 0;
}