adds git ignore
This commit is contained in:
parent
34274812b8
commit
3dec8586b5
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
build/
|
8
Makefile
8
Makefile
@ -1,2 +1,6 @@
|
|||||||
all:
|
all: src/*.cpp
|
||||||
g++ main.cpp csv.cpp
|
g++ src/main.cpp src/csv.cpp -o manager
|
||||||
|
mkdir -p build
|
||||||
|
mv manager build/
|
||||||
|
clean:
|
||||||
|
rm -rf build
|
||||||
|
@ -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
|
|
|
2
build/books.csv
Normal file
2
build/books.csv
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
id,isbn,title,authors,year of publication,page length
|
||||||
|
"1","0984782850","Cracking the Coding Interview","Gayle Laakmann McDowell",2015,697
|
|
BIN
build/manager
Executable file
BIN
build/manager
Executable file
Binary file not shown.
@ -1,10 +1,11 @@
|
|||||||
#include "csv.h"
|
#include "csv.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
std::string strip_quotes(std::string input)
|
std::string strip_quotes(std::string input)
|
||||||
{
|
{
|
||||||
if (input[0] != '"')
|
if (input[0] != '"')
|
||||||
return input;
|
return input;
|
||||||
return input.substr(1, input.size() - 1);
|
return input.substr(1, input.size() - 2);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,11 +26,14 @@ std::vector<std::string> get_csv_as_vector(std::string input)
|
|||||||
{
|
{
|
||||||
std::vector<std::string> output;
|
std::vector<std::string> output;
|
||||||
int next_comma;
|
int next_comma;
|
||||||
|
std::string current_string;
|
||||||
|
|
||||||
while ((next_comma = get_next_comma(input)) != -1) {
|
while ((next_comma = get_next_comma(input)) != -1) {
|
||||||
output.push_back(input.substr(0, next_comma));
|
output.push_back(strip_quotes(input.substr(0, next_comma)));
|
||||||
input = strip_quotes(input.substr(next_comma + 1, input.size()));
|
|
||||||
|
input = input.substr(next_comma + 1, input.size());
|
||||||
}
|
}
|
||||||
|
output.push_back(input);
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
@ -6,22 +6,26 @@
|
|||||||
#include "csv.h"
|
#include "csv.h"
|
||||||
|
|
||||||
#define BOOK_FILENAME "books.csv"
|
#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<std::string> headers, std::vector<std::string> 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::string get_all_books()
|
||||||
{
|
{
|
||||||
std::ifstream file;
|
std::ifstream file;
|
||||||
int file_exists;
|
int file_exists;
|
||||||
std::string line, total_lines;
|
std::string line, total_lines;
|
||||||
book_t book;
|
std::vector<std::string> book_vec;
|
||||||
|
std::vector<std::string> header_vec;
|
||||||
|
|
||||||
file.open(BOOK_FILENAME);
|
file.open(BOOK_FILENAME);
|
||||||
|
|
||||||
@ -30,16 +34,19 @@ std::string get_all_books()
|
|||||||
return "No books saved!";
|
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<std::string> book_vec;
|
|
||||||
|
|
||||||
|
total_lines = "{";
|
||||||
while (std::getline(file, line)) {
|
while (std::getline(file, line)) {
|
||||||
book_vec = get_csv_as_vector(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()
|
std::string print_hello()
|
Loading…
x
Reference in New Issue
Block a user