converts ISBN call to its own function
adds an enum for fork exit codes adds removing book
This commit is contained in:
parent
abdd1ed9da
commit
fb443c4310
@ -58,39 +58,74 @@ std::string get_all_books()
|
||||
|
||||
std::string remove_book(int id)
|
||||
{
|
||||
pid_t pid;
|
||||
int exec_status;
|
||||
|
||||
enum ISBN_EXIT_CODE exec_code;
|
||||
std::string program_name = "./isbn";
|
||||
std::string remove = "remove";
|
||||
std::string str_id = std::to_string(id);
|
||||
|
||||
char* args[] = {(char*) program_name.c_str(), (char*) remove.c_str(), (char*) str_id.c_str(), NULL};
|
||||
|
||||
switch (exec_code) {
|
||||
case FORK_FAILED:
|
||||
perror("removing book, failed to fork");
|
||||
return "Couldn't remove book!";
|
||||
case PROGRAM_EXIT_UNEXPECTED:
|
||||
perror("ISBN exited unexpectedly");
|
||||
return "Couldn't remove book!";
|
||||
case PROGRAM_FAIL:
|
||||
return "There was an error in removing the book!";
|
||||
case PROGRAM_SUCCESS:
|
||||
return "Successfully removed book!";
|
||||
default:
|
||||
return "Unknown error in removing book!";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
std::string add_new_book(std::string isbn)
|
||||
{
|
||||
pid_t pid;
|
||||
int exec_status;
|
||||
|
||||
enum ISBN_EXIT_CODE exec_code;
|
||||
std::string program_name = "./isbn";
|
||||
char* args[] = {(char*) program_name.c_str(), (char*) isbn.c_str(), NULL};
|
||||
|
||||
|
||||
exec_code = run_isbn_program(args);
|
||||
|
||||
switch (exec_code) {
|
||||
case FORK_FAILED:
|
||||
perror("Adding book, failed to fork");
|
||||
return "Book lookup failed!";
|
||||
case PROGRAM_EXIT_UNEXPECTED:
|
||||
perror("ISBN exited unexpectedly");
|
||||
return "Book lookup failed!";
|
||||
case PROGRAM_FAIL:
|
||||
return "Invalid ISBN submitted!";
|
||||
case PROGRAM_SUCCESS:
|
||||
return "Book added succesfully!";
|
||||
default:
|
||||
return "Unknown error in adding book!";
|
||||
}
|
||||
}
|
||||
|
||||
enum ISBN_EXIT_CODE run_isbn_program(char* args[])
|
||||
{
|
||||
pid_t pid;
|
||||
int exec_status;
|
||||
|
||||
pid = fork();
|
||||
if (0 == pid) {
|
||||
execvp(args[0], args);
|
||||
exit(EXIT_SUCCESS);
|
||||
} else if (pid < 0) {
|
||||
perror("Adding book, failed to fork");
|
||||
return "Book lookup failed!";
|
||||
return FORK_FAILED;
|
||||
} else {
|
||||
wait(&exec_status);
|
||||
|
||||
if (!WIFEXITED(exec_status)) {
|
||||
perror("ISBN exited unexpectedly!");
|
||||
return "Book lookup failed!";
|
||||
if (!WIFEXITED(exec_status))
|
||||
return PROGRAM_EXIT_UNEXPECTED;
|
||||
if (0 != WEXITSTATUS(exec_status))
|
||||
return PROGRAM_FAIL;
|
||||
return PROGRAM_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
if (0 != WEXITSTATUS(exec_status)) {
|
||||
return "Invalid ISBN submitted!";
|
||||
}
|
||||
|
||||
return "Book added successfully!";
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,14 @@
|
||||
#ifndef ISBN_INTERACTION_H
|
||||
#define ISBN_INTERACTION_H
|
||||
|
||||
enum ISBN_EXIT_CODE {
|
||||
FORK_FAILED,
|
||||
PROGRAM_EXIT_UNEXPECTED,
|
||||
PROGRAM_SUCCESS,
|
||||
PROGRAM_FAIL
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* std::string book_vec_to_json
|
||||
* std::vector<std::string> headers - Vector of strings that contain the type information of the data
|
||||
@ -19,4 +27,7 @@ std::string get_all_books();
|
||||
|
||||
std::string add_new_book(std::string isbn);
|
||||
|
||||
enum ISBN_EXIT_CODE run_isbn_program(char* args[]);
|
||||
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user