commit 34274812b872cf7db29bf05ca53cc4c49fd06c5c Author: SuperNovaa41 Date: Thu Jan 18 13:36:24 2024 -0500 intial commit diff --git a/Library Manager.pdf b/Library Manager.pdf new file mode 100644 index 0000000..4b72dd1 Binary files /dev/null and b/Library Manager.pdf differ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b70c2c2 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + g++ main.cpp csv.cpp diff --git a/a.out b/a.out new file mode 100755 index 0000000..fb724e6 Binary files /dev/null and b/a.out differ diff --git a/books.csv b/books.csv new file mode 100644 index 0000000..2319ee3 --- /dev/null +++ b/books.csv @@ -0,0 +1,2 @@ +isbn,title,authors,year of publication,page length +"0262033844","Introduction to Algorithms","Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein",1990,1292 diff --git a/csv.cpp b/csv.cpp new file mode 100644 index 0000000..0cda8d9 --- /dev/null +++ b/csv.cpp @@ -0,0 +1,35 @@ +#include "csv.h" + +std::string strip_quotes(std::string input) +{ + if (input[0] != '"') + return input; + return input.substr(1, input.size() - 1); + +} + +int get_next_comma(std::string input) +{ + int i; + bool in_string = false; + for (i = 0; i < input.size(); i++) { + if (input[i] == '"') + in_string = !in_string; + if (input[i] == ',' && !in_string) + return i; + } + return -1; +} + +std::vector get_csv_as_vector(std::string input) +{ + std::vector output; + int next_comma; + + while ((next_comma = get_next_comma(input)) != -1) { + output.push_back(input.substr(0, next_comma)); + input = strip_quotes(input.substr(next_comma + 1, input.size())); + } + + return output; +} diff --git a/csv.h b/csv.h new file mode 100644 index 0000000..2344b48 --- /dev/null +++ b/csv.h @@ -0,0 +1,15 @@ +#ifndef CSV_H +#define CSV_H + +#include +#include + +std::string strip_quotes(std::string input); + +int get_next_comma(std::string input); + +std::vector get_csv_as_vector(std::string input); + + + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..5484588 --- /dev/null +++ b/main.cpp @@ -0,0 +1,64 @@ +#include +#include + +#include + +#include "csv.h" + +#define BOOK_FILENAME "books.csv" +typedef struct book_t { + std::string isbn; + std::string title; + std::string authors; + int year_of_publication; + int page_len; +} book_t; + +std::string book_json_output(std) + +std::string get_all_books() +{ + std::ifstream file; + int file_exists; + std::string line, total_lines; + book_t book; + + file.open(BOOK_FILENAME); + + if (!file.good()) { + fprintf(stderr, "Theres no books available!\n"); + return "No books saved!"; + } + + std::getline(file, line); // theres probably a better way to skip the first line + + std::vector book_vec; + + while (std::getline(file, line)) { + book_vec = get_csv_as_vector(line); + total_lines += print_vec(book_vec); + } + + return total_lines; +} + +std::string print_hello() +{ + return "Hello, world!"; +} + +int main() +{ + crow::SimpleApp app; + + CROW_ROUTE(app, "/")([](){ + return crow::response(print_hello()); + }); + + CROW_ROUTE(app, "/books")([](){ + return crow::response(get_all_books()); + }); + + app.port(18080).run(); + +}