Skip to content

Commit

Permalink
is_number error checks for all functions supporting index
Browse files Browse the repository at this point in the history
  • Loading branch information
hexaclock committed Dec 5, 2015
1 parent 24b0a76 commit 7edcc8a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions include/keylocker.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ class KLCrypto
int tls_send(std::string &hostname, int portnum,
std::string &data, std::string &dbpath);
bool prompt_y_n(std::string question, std::string ans_default);
bool is_number(const std::string& s);

#endif
20 changes: 5 additions & 15 deletions src/client/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void parse_get(int argc, std::vector<std::string> argv)

if (argc == 3) /* get service username */
get_entry(&passdb, (std::string)argv[1] + "_" + (std::string)argv[2]);
else if (argc == 2) /* get numRef */
else if (argc == 2 && is_number(argv[1])) /* get numRef */
{
if (!(numRef = get_numRef(std::stoi(argv[1]) - 1)).empty())
get_entry(&passdb, numRef);
Expand Down Expand Up @@ -387,7 +387,7 @@ void parse_clip(int argc, std::vector<std::string> argv)

if (argc == 3) //clip service username
clip(&passdb, (std::string)argv[1] + "_" + (std::string)argv[2]);
else if (argc == 2) //clip numRef
else if (argc == 2 && is_number(argv[1])) //clip numRef
{
if (!(numRef = get_numRef(std::stoi(argv[1]) - 1)).empty())
clip(&passdb, numRef);
Expand All @@ -413,17 +413,6 @@ int delete_entry(Json::Value *passdb, std::string request)
return 1;
}

/* pre: takes in string
* post: return if the string is a number
* return: 1 on number characters; 0 on non-digit characters
*/
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}

/* pre: takes in int argc and std::vector<std::string> argv, the first item of
* argv MUST be 'delete'
* post: parses the command saved in argv and runs the appropriate function if
Expand Down Expand Up @@ -541,7 +530,7 @@ void parse_edit(int argc, std::vector<std::string> argv)
std::cout << "No such entry, please check your input" << std::endl;
}
}
else if (argc == 2)
else if (argc == 2 && is_number(argv[1]))
{
if (!(numRef = get_numRef(std::stoi(argv[1]) - 1)).empty())
{
Expand Down Expand Up @@ -778,7 +767,8 @@ bool parse_command(int argc, std::vector<std::string> argv)
parse_add(argc, argv);
else if (argv[0] == "get" ||
argv[0] == "list" ||
argv[0] == "print")
argv[0] == "print" ||
argv[0] == "ls")
parse_get(argc, argv);
else if (argv[0] == "search")
parse_search(argc, argv);
Expand Down
13 changes: 13 additions & 0 deletions src/shared/Util.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
#include "keylocker.h"


/* pre: takes in string
* post: return if the string is a number
* return: 1 on number characters; 0 on non-digit characters
*/
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}


/* pre: takes in a std::string question and std::string ans_default which MUST
* be either 'yes' or 'no'
* post: prompts the user with question followed by a (yes/no) string which will
Expand Down

0 comments on commit 7edcc8a

Please sign in to comment.