Skip to content

Instantly share code, notes, and snippets.

Created July 8, 2013 21:45
Show Gist options
  • Save anonymous/ab8ecfd09bddba0d4fcc to your computer and use it in GitHub Desktop.
Save anonymous/ab8ecfd09bddba0d4fcc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <string>
#include <locale>
#include <cstdlib>
using namespace std;
#define tab "&nbsp;&nbsp;&nbsp;&nbsp;"
static void show_usage( )
{
cerr << "Error: Incorrect number of parameters [1]." << endl;
cerr << "Usage: txt2html [-q] sourcefile [destfile]" << endl;
}
int main(int argc, char* argv [])
{
ifstream infile;
bool quiet = false; //Output flag in case the user enters the quiet switch.
ofstream outfile; //Used for the destinational file if the user provides it.
string outfilename (argv[1]);
char ch; //Will be used to parse through the text in the input file and output to a dest file.
int inputLines = 0;
int outputParas = 0;
locale here("");
cout.imbue( here );
if( strcmp( argv[1], "-q" ) != 0) //This is an example of what I am trying to do.
{
quiet = true;
infile.open( argv[2] );
}
if( argc < 2 )
{
show_usage( );
return EXIT_FAILURE;
}
if( argc == 3)
{
outfile.open( argv[2] );
}
else
{
outfilename.erase(outfilename.find_last_of("."), string::npos).append(".html");
outfile.open(outfilename.c_str( ) );
}
//Hardcode the tags into the html document.
outfilename.erase(outfilename.find_last_of("."), string::npos);
outfile << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
outfile << "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\">\n";
outfile << "<head>\n";
outfile << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";
outfile << "<title>" << "cunt lips" << "</title\n";
outfile << "</head>\n";
outfile << "<body>\n";
while( infile.get( ch ) )
{
switch( ch )
{
case '\n':
outfile << "<br />";
break;
case '\t':
outfile << tab;
outputParas++;
break;
case '&':
outfile << "&amp;";
break;
case '<':
outfile << "&lt;";
break;
case '>':
outfile << "&gt;";
break;
case '"':
outfile << "&quot;";
break;
default:
break;
}
outfile << ch;
if( ch == '\n' )
{
inputLines++;
}
}
outfile << "</body>\n";
outfile << "</html>";
infile.close();
cout << "Number of input lines processed: " << inputLines << "." << endl;
cout << "Number of output paragraphs processed: " << outputParas << "." << endl;
}//end main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment