/* Eric Knibbe, David Streng
*  CS 232
*  Project 3
*/

#ifndef COMMANDLINE_CPP
#define COMMANDLINE_CPP

#include "CommandLine.h"

// constructor - recieves command line arguments as a stream
CommandLine::CommandLine(istream& in) {
	int argCount = 0;
	vector<char*> v;
	while (in.good()) {
		argument_ptr = new char[30];	//each argument is limited to 30 characters
    		in >> argument_ptr;
	if (!strcmp(argument_ptr, "&")) {
		hasAmpersand = true;
	} else {
		v.push_back(argument_ptr);
    		argCount++;
	}
    	}
	argc = argCount;
	argv = new char*[argCount + 1];
	for (int i = 0; i < argCount; i++) {
		argv[i] = v.at(i);
	}
}

//destructor - frees any dynamically allocated memory 
CommandLine::~CommandLine(void) {
	delete [] argument_ptr;
	delete [] argv;
}

// returns a pointer to the command of the command string.
char* CommandLine::getCommand() const {
	return argv[0];
}

// returns the number of arguments in the command string
int CommandLine::getArgCount() const {
	return argc;	
}

// returns a pointer to an array of pointers to the arguments
char** CommandLine::getArgVector() const {
	return argv;
}

// returns a pointer to the argument at the ith position
char* CommandLine::getArgVector(int i) const {
	return argv[i];
}

// returns true if hasAmpersand is false
bool CommandLine::noAmpersand() const {
	return !hasAmpersand;
}

#endif

