// C03EX3Func.h
#include <string>
#include <vector>
std::vector<std::string> ReadFile(const std::string& fileName);
std::vector<std::string> ReadConsole(const std::string& commandLine);
std::string FormatDisplay(const std::vector<std::string>& unformatedString);
std::string MainMenu();
void ShowFileMenu(const std::string& formattedString, const std::string& fileName);
int Interpret(const std::vector<std::string>& Commands);
// C03EX3Func.cpp
#include <iostream>
#include <cstring>
#include <fstream>
#include <vector>
#include <sstream>
#define VERSION "V0.16" // Version of the program.
using namespace std;
// Used to read each word in a file, seperated by
// spaces and return a vector.
vector<string> ReadFile(const string& fileName)
{
// Vector to hold the words/commands
// in the order read.
vector<string> commandWords;
// Opens the passed fileName.
ifstream inFile(fileName.c_str());
string line; // variable to hold individual lines
while(getline(inFile, line)) // step through the file
{
stringstream ssLine;
string word; // variable to hold individual words
ssLine.str(line);
while(ssLine >> word) // step through the line
{
commandWords.push_back(word); // place each word seperatly
}
commandWords.push_back("\n"); // add a new line identifier
}
return commandWords; // return the vector
}
// Used to read each typed word, seperated by spaces
// from the commandLine and return a vector.
vector<string> ReadConsole(const string& commandLine)
{
vector<string> commandWords; // holds indexed commands
stringstream ssCommandLine; // stream used to step through each command
ssCommandLine << commandLine; // puts commandLine in a stream
string word; // variable to hold individual words
int numCmds = 0; // variable to hold the number of commands entered
while(ssCommandLine >> word) // step through the commandLine
{
commandWords.push_back(word); // place each word seperatly
numCmds++; // get +1 for each command read
}
switch(numCmds) // logical switch to handle the number of commands
{
case 0: // user just pressed enter without anything else
{
commandWords.push_back("nocmd");
}
case 1: // no secondary command supplied, correct sometimes
{
commandWords.push_back("nocmd");
}
}
return commandWords; // return the vector
}
// Takes a vector and then formats it into a single string
// for easy reading. It then returns the formatted string.
string FormatDisplay(const vector<string>& unformattedString)
{
string formattedString;// string var to hold formatted text.
for(int i = 0; i < unformattedString.size(); i++) // step through each word.
formattedString += " " + unformattedString; // seperate by spaces.
return formattedString; // returns a formatted string.
}
// Basic splash screen detailing all the information about
// the program and explaining it's usage. Returns a
// command statment from the user.
string MainMenu()
{
system("cls"); // clears the screen
cout<< "Command Interpreter Alpha " << VERSION
<< "\n\tBy -JetSirus- (email: jetsirus@gmail.com)"
<< endl; // flush buffer
cout<< "--------------------------------------------------\n"
<< "DIRECTIONS:\n"
<< "Type any of the following commands at the\n"
<< "prompt and then press 'enter'. All input is\n"
<< "case sensitive.\n"
<< endl; // flush buffer
cout<< "open - Used to open a file and display it.\n"
<< "\tUSAGE: open [filename.ext]\n"
<< "\tEXAMPLE: open readme.txt\n"
<< endl; // flush buffer
cout<< "exit - Exits Command Interpreter Alpha " << VERSION
<< "\n\tUSAGE: exit [noargs]\n"
<< "\tEXAMPLE: exit\n"
<< endl; // flush buffer
cout<< "Command->";
string command; // var to store command(s) entered
getline(cin, command); // gets command(s) from user
return command; // returns the command(s)
}
// Displays a file after it has been read into the program
// and formated, then waits for the user to press any key
// befor breaking. Returns nothing.
void ShowFileMenu(const string& formattedString, const string& fileName)
{
system("cls"); // clears the screen
cout<< "FILENAME: " << fileName << "\n"
<< "-----------------------------------------------------------------"
<< "------------\n"
<< endl; // flush buffer
cout<< formattedString << endl; // show string and flush
cout<< "-----------------------------------------------------------------"
<< "------------\n";
system("pause"); // pauses, waiting for any key
cout << endl; // flush buffer
}
// Takes a vector, determins what the command is,
// then performs the required tasks for the command.
// If the command is invalid or a exit command,
// Interpret returns false, otherwise it returns true.
int Interpret(const vector<string>& Commands)
{
string Command1, Command2;
Command1 = Commands[0]; //Commands[0] will always exist. See ReadConsole()
Command2 = Commands[1]; //Commands[1] will always exist. See ReadConsole()
// turns debug mode on
if(!strcmp(Command1.c_str(), "debug") && !strcmp(Command2.c_str(), "on"))
{
return 0;
}
// turns debug mode off
else if(!strcmp(Command1.c_str(), "debug") && !strcmp(Command2.c_str(), "off"))
{
return 1;
}
// opens file if name supplied
else if(!strcmp(Command1.c_str(), "open") && strcmp(Command2.c_str(), "nocmd"))
{
// sends ShowFileMenu information needed to display file
ShowFileMenu(FormatDisplay(ReadFile(Commands[1])), Commands[1]);
return 3;
}
// exits if no secondary command given
else if(!strcmp(Command1.c_str(), "exit") && !strcmp(Command2.c_str(), "nocmd"))
{
cout << "Exiting now..." << endl;
return 2;
}
// all other commands responded to with error message
else
{
cout << "Bad command or filename." << endl;
system("pause");
return 3;
}
}
// C03EX3Main.cpp
#include <iostream>
#include "C03EX3Func.h"
// macro to save typeing
#define PAUSE system("pause"); cout << endl;
// bad habbit :p
using namespace std;
int main()
{
int interpretReturn = 3; // used to break loop.
while(true) // 2 = command 'exit'
{
while(interpretReturn == 0) // 0 = command 'debug now'
{
// BEGIN DEBUG MODE
// All code here is set up for maximum feedback.
system("cls"); // clears the screen for the current run.
cout << "System call to 'cls' successfull.." << endl;
cout << "Screen cleared..." << endl;
PAUSE;
// variables to handle function output
string dmenuInput;
// begin menuInput debug
cout << "string dmenuInput declared!" << endl;
cout << "non-initialized value: " << dmenuInput << endl << endl;
//~end menuInput debug
vector<string> drcReturn;
// begin rcReturn debug
cout << "vector<string> drcReturn declared!" << endl;
if(drcReturn.size())
{
cout << "Possible Error! drcReturn already holds data!" << endl;
cout << "Displaying data..." << endl;
for(unsigned int i = 0; i < drcReturn.size(); i++)
{
cout << "drcReturn[" << i << "] = " << drcReturn << endl;
}
PAUSE;
}
else if(!drcReturn.size())
{
cout << "drcReturn contains no data..." << endl << endl;
PAUSE;
}
else
{
cout << "Unknown sitiuation with drcReturn..." << endl << endl;
PAUSE;
}
//~end drcReturn debug
// begin MainMenu() debug
// variables are initialized with output from fuctions.
cout << "Calling MainMenu() and initializing dmenuInput with return..." << endl;
dmenuInput = MainMenu(); // gets user commands
cout << "MainMenu() called successfully and returned data..." << endl;
cout << "dmenuInput initialized with return data from MainMenu()..." << endl;
cout << "dmenuInput = " << dmenuInput << endl << endl;
PAUSE;
//~end MainMenu() debug
// begin ReadConsole debug
cout << "Calling ReadConsole and initializing drcReturn with return..." << endl;
drcReturn = ReadConsole(dmenuInput); // places commands in a vector
cout << "ReadConsole() called successfully and returned data..." << endl;
cout << "drcReturn initialized with return data from ReadConsole()..." << endl;
if(drcReturn.size())
{
cout << "drcReturn holds " << drcReturn.size() << " data elements..." << endl;
cout << "Displaying data..." << endl;
for(unsigned int i = 0; i < drcReturn.size(); i++)
{
cout << "drcReturn[" << i << "] = " << drcReturn << endl;
}
cout << endl;
PAUSE;
}
else if(!drcReturn.size())
{
cout << "ERROR! drcReturn contains no data..." << endl << endl;
PAUSE;
}
else
{
cout << "Unknown sitiuation with drcReturn..." << endl << endl;
PAUSE;
}
//~end ReadConsole() debug
// begin Interpret() debug
cout << "Calling Interpret and initializing interpretReturn with return..." << endl;
interpretReturn = Interpret(drcReturn); // performs commands
cout << "Interpret Return called successfully and returned data..." << endl;
cout << "interpretReturn = " << interpretReturn << endl << endl;
if(interpretReturn == 1)
{
cout << "Debug Mode reveived shutdown command..." << endl;
interpretReturn = 3;
PAUSE;
break;
}
if(interpretReturn == 2)
{
return 0;
}
//~end Interpret() debug
}//~DEBUG MODE
// NORMAL MODE
system("cls");
// variables to handle function output
string menuInput;
vector<string> rcReturn;
// variables are initialized with output from fuctions.
menuInput = MainMenu(); // gets user commands
rcReturn = ReadConsole(menuInput); // places commands in a vector
interpretReturn = Interpret(rcReturn); // performs commands
if(interpretReturn == 2)
{
break;
}
}//~NORMAL MODE
return 0; // Safty net kill command.
}