diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..567609b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ diff --git a/Makefile b/Makefile index b70c2c2..7e0a76d 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,6 @@ -all: - g++ main.cpp csv.cpp +all: src/*.cpp + g++ src/main.cpp src/csv.cpp -o manager + mkdir -p build + mv manager build/ +clean: + rm -rf build diff --git a/a.out b/a.out deleted file mode 100755 index fb724e6..0000000 Binary files a/a.out and /dev/null differ diff --git a/books.csv b/books.csv deleted file mode 100644 index 2319ee3..0000000 --- a/books.csv +++ /dev/null @@ -1,2 +0,0 @@ -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/build/books.csv b/build/books.csv new file mode 100644 index 0000000..2d32dda --- /dev/null +++ b/build/books.csv @@ -0,0 +1,2 @@ +id,isbn,title,authors,year of publication,page length +"1","0984782850","Cracking the Coding Interview","Gayle Laakmann McDowell",2015,697 diff --git a/build/manager b/build/manager new file mode 100755 index 0000000..1cabc81 Binary files /dev/null and b/build/manager differ diff --git a/csv.cpp b/src/csv.cpp similarity index 69% rename from csv.cpp rename to src/csv.cpp index 0cda8d9..cf225a0 100644 --- a/csv.cpp +++ b/src/csv.cpp @@ -1,10 +1,11 @@ #include "csv.h" +#include std::string strip_quotes(std::string input) { if (input[0] != '"') return input; - return input.substr(1, input.size() - 1); + return input.substr(1, input.size() - 2); } @@ -25,11 +26,14 @@ std::vector get_csv_as_vector(std::string input) { std::vector output; int next_comma; + std::string current_string; 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())); + output.push_back(strip_quotes(input.substr(0, next_comma))); + + input = input.substr(next_comma + 1, input.size()); } + output.push_back(input); return output; } diff --git a/csv.h b/src/csv.h similarity index 100% rename from csv.h rename to src/csv.h diff --git a/main.cpp b/src/main.cpp similarity index 57% rename from main.cpp rename to src/main.cpp index 5484588..e9769b6 100644 --- a/main.cpp +++ b/src/main.cpp @@ -6,22 +6,26 @@ #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 book_vec_to_json(std::vector headers, std::vector book) +{ + int i; + std::string out = "{"; + for (i = 0; i < book.size(); i++) { + out += "\"" + headers[i] + "\":"; + out += "\"" + book[i] + "\","; + } + return out + "}"; + +} std::string get_all_books() { std::ifstream file; int file_exists; std::string line, total_lines; - book_t book; + std::vector book_vec; + std::vector header_vec; file.open(BOOK_FILENAME); @@ -30,16 +34,19 @@ std::string get_all_books() return "No books saved!"; } - std::getline(file, line); // theres probably a better way to skip the first line + std::getline(file, line); + + // this contains the headers so that we can fill the json file + header_vec = get_csv_as_vector(line); - std::vector book_vec; + total_lines = "{"; while (std::getline(file, line)) { book_vec = get_csv_as_vector(line); - total_lines += print_vec(book_vec); + total_lines += book_vec_to_json(header_vec, book_vec) + ","; } - return total_lines; + return total_lines + "}"; } std::string print_hello()