Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include std::vector parseCommandLine ( const std::string& aCommandLine ) { std::vector tokens; size _ t start = 0 ; while ( start < aCommandLine.size (

#include
#include
std::vector parseCommandLine(const std::string& aCommandLine){
std::vector tokens;
size_t start =0;
while (start < aCommandLine.size()){
size_t end = start;
while (end < aCommandLine.size() &&
aCommandLine[end]!='' &&
aCommandLine[end]!='<' &&
aCommandLine[end]!='>' &&
aCommandLine[end]!='|'){
++end;
}
// Break out of the loop when a special character is encountered
if (end < aCommandLine.size() &&
(aCommandLine[end]=='<'|| aCommandLine[end]=='>'|| aCommandLine[end]=='|')){
std::string token = aCommandLine.substr(start, end - start);
if (!token.empty() && (token.back()=='<'|| token.back()=='>')){
tokens.push_back(token.substr(0, token.size()-1));
tokens.push_back(std::string(1, token.back()));
} else if (token =="<<" && start ==0){
tokens.push_back(token);
} else if (token =="|"){
tokens.push_back(token);
} else if (!token.empty()){
tokens.push_back(token);
}
tokens.push_back(std::string(1, aCommandLine[end]));
++end; // Move past the special character
} else {
std::string token = aCommandLine.substr(start, end - start);
if (!token.empty() && (token.back()=='<'|| token.back()=='>')){
tokens.push_back(token.substr(0, token.size()-1));
tokens.push_back(std::string(1, token.back()));
} else if (token =="<<" && start ==0){
tokens.push_back(token);
} else if (token =="|"){
tokens.push_back(token);
} else if (!token.empty()){
tokens.push_back(token);
}
}
while (end < aCommandLine.size() && aCommandLine[end]==''){
++end;
}
start = end;
}
return tokens;
}
int main(){
std::string commandLine = "grep x < myfile.textbfin > myfile.out";
std::vector result = parseCommandLine(commandLine);
for (size_t i =0; i < result.size(); ++i){
std::cout << "Token "<< i +1<<": "<< result[i]<< std::endl;
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions