// Author: Matt Johnson // FileName: parsedTokens.cpp // Course: CS 284 - Operating Systems // Section: A // Instructor: Matt Johnson // // This file contains the implementation of the parsedTokens class. // // ------------------------------------------------------------------------ // Current Version is 1.2. // See header file for Revision History. // ------------------------------------------------------------------------ #include #include #include #include #include "parsedTokens.h" using std::string; using std::vector; using std::ostream; using std::endl; using std::cerr; ParsedTokens::ParsedTokens() { } ParsedTokens::~ParsedTokens() { } void ParsedTokens::ParseAndAddTokens(char * commandLine) { int ii = 0; int leftSide = 0; int rightSide = 0; std::string temp = ""; bool foundToken = false; while(commandLine[ii] != '\0') { // Strip out any delimiters, store any special characters while((commandLine[ii] != '\0') && (IsDelimiter(commandLine[ii]) || IsSpecialCharacter(commandLine[ii], true))) { ++ii; } leftSide = ii; foundToken = false; while(commandLine[ii] != '\0' && !IsDelimiter(commandLine[ii]) && !IsSpecialCharacter(commandLine[ii], false)) { ++ii; foundToken = true; } if(foundToken) { rightSide = ii - 1; temp = ""; temp.append(commandLine, leftSide, rightSide - leftSide + 1); tokens_.push_back(temp); } } } bool ParsedTokens::IsDelimiter(char ch) { return ch == ' '; } bool ParsedTokens::IsSpecialCharacter(char ch, bool addToContainer) { static const int NUM_CHARS = 7; char specialChars[NUM_CHARS + 1] = {"&;|<>!@"}; bool special = false; std::string chString = ""; int ii = 0; for(ii = 0; ii < NUM_CHARS && !special; ++ii) { if(ch == specialChars[ii]) { special = true; } } if(special && addToContainer) { chString = ch; tokens_.push_back(chString); } return special; } void ParsedTokens::StartIterator() { iterCount_ = 0; } bool ParsedTokens::GetNextToken(std::string &inToken) { bool success = false; if(iterCount_ < tokens_.size()) { inToken = tokens_[iterCount_]; iterCount_++; success = true; } return success; } void ParsedTokens::PrintTokens(std::ostream &ofile, char *args[]) { unsigned int ii = 0; if(args) { while(args[ii] != NULL) { ofile << args[ii] << endl; ii++; } } else if(tokens_.size()) { for(ii = 0; ii < tokens_.size(); ++ii) { ofile << tokens_[ii] << endl; } } } void ParsedTokens::Clear() { tokens_.clear(); } unsigned int ParsedTokens::GetNumTokens() { return tokens_.size(); } unsigned int ParsedTokens::GetNumPipes() { unsigned int count = 0; for(unsigned int ii = 0; ii < tokens_.size(); ii++) { if(tokens_[ii] == "|") { count++; } } return count; } string ParsedTokens::operator[](unsigned int index) { if(index < tokens_.size()) { return tokens_[index]; } else { cerr << "ParsedTokens::operator[] index error." << endl; exit(EXIT_FAILURE); } return "0"; }