intial commit
This commit is contained in:
commit
34274812b8
BIN
Library Manager.pdf
Normal file
BIN
Library Manager.pdf
Normal file
Binary file not shown.
2
books.csv
Normal file
2
books.csv
Normal file
@ -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
|
|
35
csv.cpp
Normal file
35
csv.cpp
Normal file
@ -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<std::string> get_csv_as_vector(std::string input)
|
||||||
|
{
|
||||||
|
std::vector<std::string> 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;
|
||||||
|
}
|
15
csv.h
Normal file
15
csv.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef CSV_H
|
||||||
|
#define CSV_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
std::string strip_quotes(std::string input);
|
||||||
|
|
||||||
|
int get_next_comma(std::string input);
|
||||||
|
|
||||||
|
std::vector<std::string> get_csv_as_vector(std::string input);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
64
main.cpp
Normal file
64
main.cpp
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include <crow.h>
|
||||||
|
#include <crow/app.h>
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
#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<std::string> 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();
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user