Changes CSV to DB
Updates all of the appropriate functions, updates the release version of the isbn-lookup
This commit is contained in:
parent
5ec581f0bc
commit
444f49e85d
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
all: api/*.cpp
|
||||
g++ api/main.cpp api/csv.cpp api/isbn-interaction.cpp -o manager
|
||||
g++ api/main.cpp api/isbn-interaction.cpp -lsqlite3 -o manager
|
||||
mkdir -p bin
|
||||
mv manager bin/
|
||||
clean:
|
||||
|
39
api/csv.cpp
39
api/csv.cpp
@ -1,39 +0,0 @@
|
||||
#include "csv.h"
|
||||
#include <iostream>
|
||||
|
||||
std::string strip_quotes(std::string input)
|
||||
{
|
||||
if (input[0] != '"')
|
||||
return input;
|
||||
return input.substr(1, input.size() - 2);
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
std::string current_string;
|
||||
|
||||
while ((next_comma = get_next_comma(input)) != -1) {
|
||||
output.push_back(strip_quotes(input.substr(0, next_comma)));
|
||||
|
||||
input = input.substr(next_comma + 1, input.size());
|
||||
}
|
||||
output.push_back(input);
|
||||
|
||||
return output;
|
||||
}
|
15
api/csv.h
15
api/csv.h
@ -1,15 +0,0 @@
|
||||
#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
|
@ -1,15 +1,13 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <sqlite3.h>
|
||||
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "isbn-interaction.h"
|
||||
#include "csv.h"
|
||||
|
||||
#define BOOK_FILENAME "books.csv"
|
||||
|
||||
std::string book_vec_to_json(std::vector<std::string> headers, std::vector<std::string> book)
|
||||
{ int i;
|
||||
@ -23,38 +21,63 @@ std::string book_vec_to_json(std::vector<std::string> headers, std::vector<std::
|
||||
out.pop_back();
|
||||
|
||||
return out + "}";
|
||||
}
|
||||
|
||||
std::vector<std::string> get_db_headers(sqlite3* db)
|
||||
{
|
||||
int i;
|
||||
std::vector<std::string> res;
|
||||
sqlite3_stmt* getmsgs;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
std::string get_all_books()
|
||||
{
|
||||
std::ifstream file;
|
||||
int file_exists;
|
||||
std::string line, total_lines;
|
||||
int rc, i;
|
||||
sqlite3* db;
|
||||
char* text;
|
||||
std::string total_lines;
|
||||
std::vector<std::string> book_vec;
|
||||
std::vector<std::string> header_vec;
|
||||
|
||||
std::ifstream file("books.db");
|
||||
if (!file.is_open())
|
||||
return "No books found!\n";
|
||||
file.close();
|
||||
|
||||
file.open(BOOK_FILENAME);
|
||||
rc = sqlite3_open("books.db", &db);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
|
||||
if (!file.good()) {
|
||||
fprintf(stderr, "Theres no books available!\n");
|
||||
return "No books saved!";
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::getline(file, line);
|
||||
total_lines += "{\"books\": [";
|
||||
|
||||
// this contains the headers so that we can fill the json file
|
||||
header_vec = get_csv_as_vector(line);
|
||||
|
||||
total_lines = "{ \"books\": [";
|
||||
while (std::getline(file, line)) {
|
||||
book_vec = get_csv_as_vector(line);
|
||||
header_vec = get_db_headers(db);
|
||||
|
||||
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));
|
||||
}
|
||||
total_lines += book_vec_to_json(header_vec, book_vec) + ",";
|
||||
}
|
||||
sqlite3_finalize(getmsgs);
|
||||
sqlite3_close(db);
|
||||
|
||||
// remove the trailing comma
|
||||
// remove trailing comma
|
||||
total_lines.pop_back();
|
||||
|
||||
|
||||
|
@ -2,8 +2,6 @@
|
||||
#include <crow/app.h>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
#include "csv.h"
|
||||
#include "isbn-interaction.h"
|
||||
|
||||
std::string print_hello()
|
||||
@ -37,5 +35,4 @@ int main()
|
||||
|
||||
|
||||
app.port(18080).run();
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ cd ../
|
||||
mkdir -p bin
|
||||
cd bin
|
||||
|
||||
wget https://github.com/SuperNovaa41/isbn-lookup/releases/download/0.0.4/isbn
|
||||
wget https://github.com/SuperNovaa41/isbn-lookup/releases/download/0.0.5/isbn
|
||||
chmod +x isbn
|
||||
|
||||
cd ../
|
||||
|
Loading…
x
Reference in New Issue
Block a user