Finish assignment 5 with HTML Converter. (This one is actually really neat!)
This commit is contained in:
parent
d0c2763a23
commit
d2d751306e
3 changed files with 372 additions and 0 deletions
|
|
@ -12,6 +12,8 @@ Dependencies: ???
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
#include <random>
|
||||
|
|
@ -27,14 +29,75 @@ using namespace std;
|
|||
// 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 '<' and '>' 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 << "<";
|
||||
}
|
||||
else
|
||||
{
|
||||
outputFile << ">";
|
||||
}
|
||||
}
|
||||
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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue