// Author: Brandon Winterton #include #include #include #include using namespace std; struct options_t { bool isStrictReturns; }; void autoFormat(ifstream& fin, ofstream& fout, ifstream& temp, string title, string author, options_t options); void formatText(ifstream& f, ofstream& fout, ifstream& temp, options_t options); void addTemplate(ifstream& fin, ofstream& fout, string stop); // POST: output file has information in template copied to it char getMenuChoice(void); // POST: user prompted, returns choice // (a=add report, w=worst, s=show, d=delete informant, m=map, q=quit) char getOption(void); // POST: pretty much identical to getMenuChoice(); char getSetting(void); // POST: same thing again.. string getline(ifstream& f, string stop); // POST: Cursor on: next \n, char in stop or end of file, // whichever comes first. // Returns string up to that point (counting WS) void saveSettings(options_t options); options_t loadSettings(void); int main(int argc,char* argv[]) { const string helparg="-?"; //helparg and conarg were created because I don't know const string conarg="-con"; //of any other way to compare the command line args const string strictarg="-str"; //in an if statement const string truearg="true"; const string falsearg="false"; ifstream f1; string input, output; options_t progOptions; f1.open("template.html"); progOptions=loadSettings(); if(argc==1) { if(f1.fail()) { cout << "This program expects template.html as an existing file in its\n" << "home directory. Please ensure that this file exists.\n"; } else { cout << "Welcome to the HTML formatter. Choose \'f\' to format a file,\n" << "choose \'h\' for help in using the program.\n" << endl; char option; char choice=getMenuChoice(); while(choice!='q') { if(choice=='f') { ifstream f2; ofstream fout; cout << "Input file: "; cin >> input; cout << "Output file: "; cin >> output; f2.open(input.c_str()); if(f2.fail()) { cout << input << " does not exist" << endl; } else { fout.open(output.c_str()); formatText(f2,fout,f1,progOptions); } f2.close(); fout.close(); cout << "\nFile complete..\n" << endl; } if(choice=='h') { char scrap[1]; cout << "This program will format a standard text file for display in HTML.\n" << "It adds HTML tags for line breaks and tabs since HTML ingnores white\n" << "space. To format a text file into an HTML file, choose option f from\n" << "the main menu. You will be promted for an input file and an output file.\n" << "If the ouput file does not already exist, the name is case sensitive.\n" << "You will be asked for a title and an author name for use in the HTML page.\n" << "Both of these are case sensitive, enter them as you would like them to appear.\n" << "\n" << "Press Enter to return to the menu." << endl; if(cin.peek()=='\n') cin.get(); cin.getline(scrap,1,'\n'); } if(choice=='o') { char change; option=getOption(); while(option!='b') { if(option=='s') { cout << "Setting to True enables Strict formatting.\n" << "\tStrict formatting only places returns at the end of paragraphs.\n\n" << "Setting to False enables Loose formatting.\n" << "\tLoose formatting places any returns that are present in the text.\n\n" << "Default setting is True.\n\n" << "Current setting is: "; if(progOptions.isStrictReturns) { cout << "True" << endl; } else { cout << "False" << endl; } cout << "\nWould you like to change this setting?\n"; change=getSetting(); if(change=='y') { if(progOptions.isStrictReturns) { progOptions.isStrictReturns=false; cout << "Current setting is: False" << endl; } else { progOptions.isStrictReturns=true; cout << "Current setting is: True" << endl; } } } option=getOption(); } saveSettings(progOptions); } choice=getMenuChoice(); } } } else { if(argc==2) { if(argv[1]==helparg) { cout << "\nUsage:\t" << argv[0] << " -con <author> | -str <true/false>" << endl << "\t" << argv[0] << " -?" << endl << "\t" << argv[0] << endl; } if(argv[1]==conarg) { cout << "\nUsage: \t" << argv[0] << " -con <inputfile> <outputfile> <title> <author>" << endl; } if(argv[1]==strictarg) { cout << "\nUsage: \t" << argv[0] << " -str <true/false>" << endl; } if(argv[1]!=helparg && argv[1]!=conarg && argv[1]!=strictarg) { cout << "\nUsage:\t" << argv[0] << " -con <inputfile> <outputfile> <title> <author> | -str <true/false>" << endl << "\t" << argv[0] << " -?" << endl << "\t" << argv[0] << endl; } } if(argc==3) { if(argv[1]==strictarg) { if(argv[2]==truearg || argv[2]==falsearg) { if(argv[2]==truearg) { progOptions.isStrictReturns=true; } else { progOptions.isStrictReturns=false; } } else { cout << "\nUsage: \t" << argv[0] << " -str <true/false>" << endl; } } } if(argc==6) { if(argv[1]==conarg) { if(f1.fail()) { cout << "This program expects template.html as an existing file in its\n" << "home directory. Please ensure that this file exists.\n"; } ifstream f2; ofstream fout; input=argv[2]; output=argv[3]; f2.open(input.c_str()); if(f2.fail()) { cout << input << " does not exist" << endl; } else { fout.open(output.c_str()); autoFormat(f2,fout,f1,argv[4],argv[5],progOptions); } f2.close(); fout.close(); cout << "\nFile complete..\n" << endl; } } if(argc==8) { if(argv[1]==conarg) { if(argv[6]==strictarg) { if(argv[7]==truearg || argv[7]==falsearg) { if(argv[7]==truearg) { progOptions.isStrictReturns=true; } else { progOptions.isStrictReturns=false; } } else { cout << "\nUsage: \t" << argv[0] << " -str <true/false>" << endl; return 0; } } if(f1.fail()) { cout << "This program expects template.html as an existing file in its\n" << "home directory. Please ensure that this file exists.\n"; } ifstream f2; ofstream fout; input=argv[2]; output=argv[3]; f2.open(input.c_str()); if(f2.fail()) { cout << input << " does not exist" << endl; } else { fout.open(output.c_str()); autoFormat(f2,fout,f1,argv[4],argv[5],progOptions); } f2.close(); fout.close(); cout << "\nFile complete..\n" << endl; } if(argv[1]==strictarg) { if(argv[2]==truearg || argv[2]==falsearg) { if(argv[2]==truearg) { progOptions.isStrictReturns=true; } else { progOptions.isStrictReturns=false; } } else { cout << "\nUsage: \t" << argv[0] << " -str <true/false>" << endl; } if(argv[3]==conarg) { if(f1.fail()) { cout << "This program expects template.html as an existing file in its\n" << "home directory. Please ensure that this file exists.\n"; } ifstream f2; ofstream fout; input=argv[4]; output=argv[5]; f2.open(input.c_str()); if(f2.fail()) { cout << input << " does not exist" << endl; } else { fout.open(output.c_str()); autoFormat(f2,fout,f1,argv[6],argv[7],progOptions); } f2.close(); fout.close(); cout << "\nFile complete..\n" << endl; } } } if(argc!=6 && argc!=2 && argc!=8 && argc!=3) { cout << "\nUsage:\t" << argv[0] << " -con <inputfile> <outputfile> <title> <author> | -str <true/false>" << endl << "\t" << argv[0] << " -?" << endl << "\t" << argv[0] << endl; } } saveSettings(progOptions); f1.close(); return 0; } //================================================================== void autoFormat(ifstream& fin, ofstream& fout, ifstream& temp, string title, string author, options_t options) { char add; addTemplate(temp, fout, "<title>Template"); fout << "" << title << ""; addTemplate(temp, fout, "

Title area

"); fout << "

" << title << "

by " << author; addTemplate(temp, fout, ""); while(!fin.eof()) { if(fin.peek()!='\t' && fin.peek()!='\n' && fin.peek()!=13) { add = fin.get(); if(!fin.eof()) { fout << add; } } else { if(fin.peek()=='\t') { fin.get(); fout << "        "; } else { if(fin.peek()=='\n') { fin.get(); if(fin.peek()=='\t' && options.isStrictReturns) { fout << "
\n"; } if(fin.peek()!='\t' && options.isStrictReturns) { fout << " "; } if(!options.isStrictReturns) { fout << "
\n"; } } else { if(fin.peek()==13) { fin.get(); } } } } } addTemplate(temp, fout, ""); return; } void formatText(ifstream& fin, ofstream& fout, ifstream& temp, options_t options) { char add; char pagetitle[101], author[101]; addTemplate(temp, fout, "Template"); cout << "Title of page: "; if(cin.peek()=='\n') cin.get(); cin.getline(pagetitle,100,'\n'); fout << "" << pagetitle << ""; addTemplate(temp, fout, "

Title area

"); cout << "Author: "; if(cin.peek()=='\n') cin.get(); cin.getline(author,100,'\n'); fout << "

" << pagetitle << "

by " << author; addTemplate(temp, fout, ""); while(!fin.eof()) { if(fin.peek()!='\t' && fin.peek()!='\n' && fin.peek()!=13) { add = fin.get(); if(!fin.eof()) { fout << add; } } else { if(fin.peek()=='\t') { fin.get(); fout << "        "; } else { if(fin.peek()=='\n') { fin.get(); if(fin.peek()=='\t' && options.isStrictReturns) { fout << "
\n"; } if(fin.peek()!='\t' && options.isStrictReturns) { fout << " "; } if(!options.isStrictReturns) { fout << "
\n"; } } else { if(fin.peek()==13) { fin.get(); } } } } } addTemplate(temp, fout, ""); return; } //================================================================== void addTemplate(ifstream& fin, ofstream& fout, string stop) { string add; bool isStop=false; while(!fin.eof() && !isStop) { add = getline(fin,""); //get line excluding /n at end fin.get(); //remove /n at end of line if(add==stop) { isStop=true; } else { fout << add << '\n'; } } return; } //================================================================== char getMenuChoice(void) { cout << "f)ormat, h)elp, o)ptions, q)uit" << endl; const string valid="fhoq"; char ans; int tries=0; // give them 10 tries cin >> ans; while(valid.find(ans)==string::npos && tries<10) { cout << "Must be f, h, o or q:"<> ans; tries++; } if(valid.find(ans)==string::npos) { ans='q'; } return ans; } //================================================================== char getOption(void) { cout << "s)trict returns\n" << "b)ack to main menu" << endl; const string valid="sb"; char ans; int tries=0; // give them 10 tries cin >> ans; while(valid.find(ans)==string::npos && tries<10) { cout << "Must be b or s:"<> ans; tries++; } if(valid.find(ans)==string::npos) { ans='b'; } return ans; } //================================================================== char getSetting(void) { cout << "y)es\n" << "n)o" << endl; const string valid="yn"; char ans; int tries=0; // give them 10 tries cin >> ans; while(valid.find(ans)==string::npos && tries<10) { cout << "Must be y or n:"<> ans; tries++; } if(valid.find(ans)==string::npos) { ans='n'; } return ans; } //================================================================== string getline(ifstream& f, string stop) { string ans; bool isStop=false; while(f.peek()!='\n' && !f.eof() && !isStop) { for(int i=0;i> options.isStrictReturns; settings.get(); } settings.close(); return options; }