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 "definitions.h"
#include "dirFunctions.h" #include "dirFunctions.h"
#include "ConexaoRawSocket.c" #include "ConexaoRawSocket.c"
void printCommandsList(); void printCommandsList();
vector<string> getArgs();
int main(){ int main(){
int sockt = ConexaoRawSocket(DEVICE); int pos, sockt = ConexaoRawSocket(DEVICE);
vector<string> args; string line, command, args;
printCommandsList(); printCommandsList();
while(true){ while(true){
cout << endl << "Entre com o comando:" << endl; cout << endl << "Entre com o comando:" << endl;
args = getArgs(); getline(cin,line);
if(args[0] == "quit"){ pos = line.find_first_of(" ");
break; if(pos == string::npos){
pos = line.size();
} }
if(args[0] == "cd"){ command = line.substr(0,pos);
cd(args[1]);
}else if(args[0] == "ls"){
try{ try{
cout << ls(args); if(command == "quit"){
}catch(char const* strException){ break;
cerr<<"Error: "<< strException << endl;
} }
}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 //TODO
}else if(args[0] == "lsr"){ }else if(command == "lsr"){
//TODO //TODO
}else if(args[0] == "put"){ }else if(command == "put"){
//TODO //TODO
}else if(args[0] == "get"){ }else if(command == "get"){
}else if(args[0] == "help"){ }else if(command == "help"){
printCommandsList(); printCommandsList();
}else{ }else{
cout << "Comando inexistente." << endl; cout << "Comando inexistente." << endl;
printCommandsList(); printCommandsList();
}
}catch(char const* strException){
cerr<<"Error: "<< strException << endl;
}catch(out_of_range e){
cerr<<"Error: Esse comando requer argumentos."<<endl;
} }
} }
return 0; return 0;
...@@ -54,16 +60,3 @@ void printCommandsList(){ ...@@ -54,16 +60,3 @@ void printCommandsList(){
cout << "help - Lista de comandos"<< endl; cout << "help - Lista de comandos"<< endl;
cout << "quit - Sair"<< 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 @@ ...@@ -9,12 +9,6 @@
using namespace std; 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){ void cd(string path){
if(chdir(path.c_str()) != 0){ if(chdir(path.c_str()) != 0){
cout << "Error: could not change directory." << endl; cout << "Error: could not change directory." << endl;
...@@ -22,9 +16,9 @@ void cd(string path){ ...@@ -22,9 +16,9 @@ void cd(string path){
} }
} }
string ls(vector<string> args){ string ls(string args){
string output, command = concatStringVector(args); string output;
FILE *lsOut = popen(command.c_str(), "r"); FILE *lsOut = popen(args.c_str(), "r");
if(!lsOut){ if(!lsOut){
throw "Couldn't execute ls"; 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