Skip to content
Snippets Groups Projects
Commit 53c7bbd5 authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Optimized command arguments reading

parent 27546ff4
Branches
No related tags found
1 merge request!1Lento
#include <sstream>
#include <stdexcept>
#include "definitions.h"
#include "dirFunctions.h"
#include "ConexaoRawSocket.c"
void printCommandsList();
vector<string> getArgs();
int main(){
int sockt = ConexaoRawSocket(DEVICE);
vector<string> args;
int pos, sockt = ConexaoRawSocket(DEVICE);
string line, command, args;
printCommandsList();
while(true){
cout << endl << "Entre com o comando:" << endl;
args = getArgs();
if(args[0] == "quit"){
break;
getline(cin,line);
pos = line.find_first_of(" ");
if(pos == string::npos){
pos = line.size();
}
if(args[0] == "cd"){
cd(args[1]);
}else if(args[0] == "ls"){
command = line.substr(0,pos);
try{
cout << ls(args);
}catch(char const* strException){
cerr<<"Error: "<< strException << endl;
if(command == "quit"){
break;
}
}else if(args[0] == "cdr"){
if(command == "cd"){
args = line.substr(pos+1, line.size());
cd(args);
}else if(command == "ls"){
cout << ls(line);
}else if(command == "cdr"){
//TODO
}else if(args[0] == "lsr"){
}else if(command == "lsr"){
//TODO
}else if(args[0] == "put"){
}else if(command == "put"){
//TODO
}else if(args[0] == "get"){
}else if(command == "get"){
}else if(args[0] == "help"){
}else if(command == "help"){
printCommandsList();
}else{
cout << "Comando inexistente." << endl;
printCommandsList();
}
}catch(char const* strException){
cerr<<"Error: "<< strException << endl;
}catch(out_of_range e){
cerr<<"Error: Esse comando requer argumentos."<<endl;
}
}
return 0;
......@@ -54,16 +60,3 @@ void printCommandsList(){
cout << "help - Lista de comandos"<< endl;
cout << "quit - Sair"<< endl;
}
vector<string> getArgs(){
string line, arg;
getline(cin,line);
stringstream ss(line);
vector<string> args;
while(getline(ss, arg, ' ')){
if(!arg.empty()){
args.push_back(arg);
}
}
return args;
}
\ No newline at end of file
......@@ -9,12 +9,6 @@
using namespace std;
string concatStringVector(vector<string> vs){
ostringstream oss;
copy(vs.begin(), vs.end(), ostream_iterator<string>(oss, " "));
return oss.str();
}
void cd(string path){
if(chdir(path.c_str()) != 0){
cout << "Error: could not change directory." << endl;
......@@ -22,9 +16,9 @@ void cd(string path){
}
}
string ls(vector<string> args){
string output, command = concatStringVector(args);
FILE *lsOut = popen(command.c_str(), "r");
string ls(string args){
string output;
FILE *lsOut = popen(args.c_str(), "r");
if(!lsOut){
throw "Couldn't execute ls";
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment