From 53c7bbd56804a0feec4f9e7adfe95e221f40b49f Mon Sep 17 00:00:00 2001 From: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br> Date: Fri, 27 Nov 2015 16:49:33 -0200 Subject: [PATCH] Optimized command arguments reading Signed-off-by: Israel Barreto Sant'Anna <ibsa14@inf.ufpr.br> --- client.cpp | 73 ++++++++++++++++++++++-------------------------- dirFunctions.cpp | 12 ++------ 2 files changed, 36 insertions(+), 49 deletions(-) diff --git a/client.cpp b/client.cpp index dd84899..6088525 100644 --- a/client.cpp +++ b/client.cpp @@ -1,43 +1,49 @@ -#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"){ - try{ - cout << ls(args); - }catch(char const* strException){ - cerr<<"Error: "<< strException << endl; + command = line.substr(0,pos); + try{ + if(command == "quit"){ + break; } - }else if(args[0] == "cdr"){ - //TODO - }else if(args[0] == "lsr"){ - //TODO - }else if(args[0] == "put"){ - //TODO - }else if(args[0] == "get"){ - - }else if(args[0] == "help"){ - printCommandsList(); - }else{ - cout << "Comando inexistente." << endl; - printCommandsList(); + 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(command == "lsr"){ + //TODO + }else if(command == "put"){ + //TODO + }else if(command == "get"){ + }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 diff --git a/dirFunctions.cpp b/dirFunctions.cpp index f3ae475..527175c 100644 --- a/dirFunctions.cpp +++ b/dirFunctions.cpp @@ -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"; } -- GitLab