2024-01-22 15:12:28 -05:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
2024-01-31 18:24:10 -05:00
|
|
|
#include <sqlite3.h>
|
2024-01-22 15:12:28 -05:00
|
|
|
|
2024-01-24 10:54:38 -05:00
|
|
|
#include <sys/wait.h>
|
2024-01-22 15:36:53 -05:00
|
|
|
#include <unistd.h>
|
2024-01-24 10:54:38 -05:00
|
|
|
#include <stdlib.h>
|
2024-01-22 15:36:53 -05:00
|
|
|
|
2024-01-22 15:12:28 -05:00
|
|
|
#include "isbn-interaction.h"
|
|
|
|
|
|
|
|
std::string book_vec_to_json(std::vector<std::string> headers, std::vector<std::string> book)
|
2024-01-24 18:40:48 -05:00
|
|
|
{ int i;
|
2024-01-22 15:12:28 -05:00
|
|
|
std::string out = "{";
|
2024-01-24 10:54:38 -05:00
|
|
|
|
2024-01-22 15:12:28 -05:00
|
|
|
for (i = 0; i < book.size(); i++) {
|
|
|
|
out += "\"" + headers[i] + "\":";
|
|
|
|
out += "\"" + book[i] + "\",";
|
|
|
|
}
|
2024-01-29 12:17:44 -05:00
|
|
|
// remove the trailing comma
|
|
|
|
out.pop_back();
|
|
|
|
|
2024-01-22 15:12:28 -05:00
|
|
|
return out + "}";
|
2024-01-31 18:24:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> get_db_headers(sqlite3* db)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
std::vector<std::string> res;
|
|
|
|
sqlite3_stmt* getmsgs;
|
2024-01-22 15:12:28 -05:00
|
|
|
|
2024-01-31 18:24:10 -05:00
|
|
|
sqlite3_prepare(db, "SELECT * FROM books;", -1, &getmsgs, NULL);
|
|
|
|
sqlite3_step(getmsgs);
|
|
|
|
for (i = 0; i < sqlite3_column_count(getmsgs); i++)
|
|
|
|
res.push_back(sqlite3_column_name(getmsgs, i));
|
|
|
|
|
|
|
|
sqlite3_finalize(getmsgs);
|
|
|
|
return res;
|
2024-01-22 15:12:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string get_all_books()
|
|
|
|
{
|
2024-01-31 18:24:10 -05:00
|
|
|
int rc, i;
|
|
|
|
sqlite3* db;
|
|
|
|
char* text;
|
|
|
|
std::string total_lines;
|
2024-01-22 15:12:28 -05:00
|
|
|
std::vector<std::string> book_vec;
|
|
|
|
std::vector<std::string> header_vec;
|
|
|
|
|
2024-01-31 18:24:10 -05:00
|
|
|
std::ifstream file("books.db");
|
|
|
|
if (!file.is_open())
|
|
|
|
return "No books found!\n";
|
|
|
|
file.close();
|
2024-01-24 10:54:38 -05:00
|
|
|
|
2024-01-31 18:24:10 -05:00
|
|
|
rc = sqlite3_open("books.db", &db);
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
|
|
|
sqlite3_close(db);
|
2024-01-22 15:12:28 -05:00
|
|
|
|
2024-01-31 18:24:10 -05:00
|
|
|
exit(EXIT_FAILURE);
|
2024-01-22 15:12:28 -05:00
|
|
|
}
|
|
|
|
|
2024-01-31 18:24:10 -05:00
|
|
|
total_lines += "{\"books\": [";
|
|
|
|
|
|
|
|
header_vec = get_db_headers(db);
|
2024-01-22 15:12:28 -05:00
|
|
|
|
2024-01-31 18:24:10 -05:00
|
|
|
sqlite3_stmt* getmsgs;
|
|
|
|
sqlite3_prepare(db, "SELECT * FROM books;", -1, &getmsgs, NULL);
|
|
|
|
while(sqlite3_step(getmsgs) == SQLITE_ROW) {
|
|
|
|
book_vec.clear();
|
|
|
|
for (i = 0; i < sqlite3_column_count(getmsgs); i++) {
|
|
|
|
text = (char*) sqlite3_column_text(getmsgs, i);
|
|
|
|
book_vec.push_back(std::string(text));
|
|
|
|
}
|
2024-01-22 15:12:28 -05:00
|
|
|
total_lines += book_vec_to_json(header_vec, book_vec) + ",";
|
|
|
|
}
|
2024-01-31 18:24:10 -05:00
|
|
|
sqlite3_finalize(getmsgs);
|
|
|
|
sqlite3_close(db);
|
2024-01-22 15:12:28 -05:00
|
|
|
|
2024-01-31 18:24:10 -05:00
|
|
|
// remove trailing comma
|
2024-01-29 12:17:44 -05:00
|
|
|
total_lines.pop_back();
|
|
|
|
|
2024-01-24 10:54:38 -05:00
|
|
|
|
2024-01-29 12:17:44 -05:00
|
|
|
return total_lines + "]}";
|
2024-01-22 15:12:28 -05:00
|
|
|
}
|
2024-01-22 15:36:53 -05:00
|
|
|
|
2024-01-24 18:40:48 -05:00
|
|
|
std::string remove_book(std::string id)
|
2024-01-24 18:24:06 -05:00
|
|
|
{
|
2024-01-24 18:37:54 -05:00
|
|
|
enum ISBN_EXIT_CODE exec_code;
|
2024-01-24 18:24:06 -05:00
|
|
|
std::string program_name = "./isbn";
|
2024-01-24 18:37:54 -05:00
|
|
|
std::string remove = "remove";
|
|
|
|
|
2024-01-24 18:40:48 -05:00
|
|
|
char* args[] = {(char*) program_name.c_str(), (char*) remove.c_str(), (char*) id.c_str(), NULL};
|
2024-01-24 18:37:54 -05:00
|
|
|
|
2024-01-24 18:44:46 -05:00
|
|
|
exec_code = run_isbn_program(args);
|
|
|
|
|
2024-01-24 18:37:54 -05:00
|
|
|
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!";
|
|
|
|
}
|
2024-01-24 18:24:06 -05:00
|
|
|
}
|
|
|
|
|
2024-01-24 18:37:54 -05:00
|
|
|
|
2024-01-24 10:54:38 -05:00
|
|
|
std::string add_new_book(std::string isbn)
|
2024-01-22 15:36:53 -05:00
|
|
|
{
|
2024-01-24 18:37:54 -05:00
|
|
|
enum ISBN_EXIT_CODE exec_code;
|
2024-01-22 15:36:53 -05:00
|
|
|
std::string program_name = "./isbn";
|
2024-01-24 18:24:06 -05:00
|
|
|
char* args[] = {(char*) program_name.c_str(), (char*) isbn.c_str(), NULL};
|
2024-01-22 15:36:53 -05:00
|
|
|
|
2024-01-24 18:37:54 -05:00
|
|
|
|
|
|
|
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) {
|
2024-01-24 10:54:38 -05:00
|
|
|
execvp(args[0], args);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (pid < 0) {
|
2024-01-24 18:37:54 -05:00
|
|
|
return FORK_FAILED;
|
2024-01-24 10:54:38 -05:00
|
|
|
} else {
|
|
|
|
wait(&exec_status);
|
|
|
|
|
2024-01-24 18:37:54 -05:00
|
|
|
if (!WIFEXITED(exec_status))
|
|
|
|
return PROGRAM_EXIT_UNEXPECTED;
|
|
|
|
if (0 != WEXITSTATUS(exec_status))
|
|
|
|
return PROGRAM_FAIL;
|
|
|
|
return PROGRAM_SUCCESS;
|
2024-01-22 15:36:53 -05:00
|
|
|
}
|
|
|
|
}
|
2024-01-24 18:37:54 -05:00
|
|
|
|