Compare commits
No commits in common. "6d81d205a048dd5b5e7d59b5085722bb443fe5b1" and "8e78e89f3bcc655424aee74a410066ed106230a2" have entirely different histories.
6d81d205a0
...
8e78e89f3b
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
build/
|
isbn
|
||||||
|
books.csv
|
||||||
|
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
|||||||
all: src/*.c
|
all: src/*.c
|
||||||
gcc src/main.c src/json.c src/curl.c src/db.c -lsqlite3 -lcurl -lcjson -o isbn -Wall
|
gcc src/main.c src/json.c src/curl.c src/csv.c -lcurl -lcjson -o isbn -Wall
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
mv isbn build
|
mv isbn build
|
||||||
clean:
|
clean:
|
||||||
|
126
src/csv.c
Normal file
126
src/csv.c
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
#include <cjson/cJSON.h>
|
||||||
|
#include <curl/curl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "curl.h"
|
||||||
|
#include "json.h"
|
||||||
|
#include "csv.h"
|
||||||
|
|
||||||
|
#define MAX_BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
int get_next_id()
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
char buffer[MAX_BUFFER_SIZE];
|
||||||
|
FILE* csv;
|
||||||
|
char id[MAX_BUFFER_SIZE];
|
||||||
|
|
||||||
|
csv = fopen(FILE_NAME, "r");
|
||||||
|
|
||||||
|
while (fgets(buffer, sizeof(buffer), csv));
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while(buffer[i] != ',')
|
||||||
|
i++;
|
||||||
|
|
||||||
|
strncpy(id, buffer, i);
|
||||||
|
|
||||||
|
return atoi(id) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_line(char** line, int new_id)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char* buffer;
|
||||||
|
for (i = 0; i < MAX_BUFFER_SIZE; i++) {
|
||||||
|
if ((*line)[i] == ',')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == MAX_BUFFER_SIZE - 1) {
|
||||||
|
fprintf(stderr, "There was an error in %s file!\n", FILE_NAME);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = malloc(sizeof(char) * MAX_BUFFER_SIZE);
|
||||||
|
strncpy(buffer, (*line) + i, MAX_BUFFER_SIZE);
|
||||||
|
|
||||||
|
snprintf(*line, MAX_BUFFER_SIZE, "%d%s", new_id, buffer);
|
||||||
|
free(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_line_from_file(int id_to_remove)
|
||||||
|
{
|
||||||
|
int file_exists, line_count;
|
||||||
|
FILE* csv;
|
||||||
|
FILE* new_csv;
|
||||||
|
char* line;
|
||||||
|
size_t line_size = MAX_BUFFER_SIZE;
|
||||||
|
|
||||||
|
file_exists = access(FILE_NAME, F_OK);
|
||||||
|
if (0 != file_exists) {
|
||||||
|
fprintf(stderr, "%s does not exist!\n", FILE_NAME);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
csv = fopen(FILE_NAME, "r");
|
||||||
|
if (NULL == csv) {
|
||||||
|
fprintf(stderr, "Failed to open %s!\n", FILE_NAME);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
new_csv = fopen("temp.csv", "w");
|
||||||
|
|
||||||
|
line_count = 0;
|
||||||
|
while(getline(&line, &line_size, csv) != -1) {
|
||||||
|
if (id_to_remove > line_count) {
|
||||||
|
fprintf(new_csv, "%s", line);
|
||||||
|
} else if (id_to_remove < line_count) {
|
||||||
|
update_line(&line, line_count - 1);
|
||||||
|
fprintf(new_csv, "%s", line);
|
||||||
|
}
|
||||||
|
line_count++;
|
||||||
|
}
|
||||||
|
fclose(new_csv);
|
||||||
|
fclose(csv);
|
||||||
|
|
||||||
|
remove(FILE_NAME);
|
||||||
|
rename("temp.csv", FILE_NAME); // new csv is now the original file, without that line
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_to_file(book_t* book)
|
||||||
|
{
|
||||||
|
FILE* file;
|
||||||
|
int file_exists;
|
||||||
|
int book_id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* We want to check if the file exists
|
||||||
|
* if it doesnt, we create a new one
|
||||||
|
* otherwise, we write to the existing one
|
||||||
|
*/
|
||||||
|
file_exists = access(FILE_NAME, F_OK);
|
||||||
|
if (0 != file_exists) {
|
||||||
|
file = fopen(FILE_NAME, "w");
|
||||||
|
// write the csv headers to the file since we're making it
|
||||||
|
fprintf(file, "id,isbn,title,authors,imageurl,year of publication,page length\n");
|
||||||
|
book_id = 1;
|
||||||
|
} else {
|
||||||
|
file = fopen(FILE_NAME, "a");
|
||||||
|
book_id = get_next_id();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NULL == file) {
|
||||||
|
fprintf(stderr, "Failed to open %s!\n", FILE_NAME);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// now we write the information
|
||||||
|
fprintf(file, "%d,\"%s\",\"%s\",\"%s\",\"%s\",%d,%d\n",
|
||||||
|
book_id, book->isbn, book->title, book->authors, book->image_url, book->year_of_publication, book->page_len);
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
}
|
38
src/csv.h
Normal file
38
src/csv.h
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#ifndef CSV_H
|
||||||
|
#define CSV_H
|
||||||
|
|
||||||
|
#define FILE_NAME "books.csv"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* int get_next_id
|
||||||
|
*
|
||||||
|
* Returns the next available ID so that we know what to assign to the book
|
||||||
|
*/
|
||||||
|
int get_next_id();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* void write_to_file
|
||||||
|
* boot_t* book - A pointer to the book information struct
|
||||||
|
*
|
||||||
|
* Writes the book information to a CSV file
|
||||||
|
*/
|
||||||
|
void write_to_file(book_t* book);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* void remove_line_from_file
|
||||||
|
* int id_to_remove - The book ID that we don't want anymore
|
||||||
|
*
|
||||||
|
* Removes a book from the CSV file
|
||||||
|
*/
|
||||||
|
void remove_line_from_file(int id_to_remove);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* void update_line
|
||||||
|
* char** line - Pointer to the book entry string
|
||||||
|
* int new_id - The new ID that should be placed into this book entry
|
||||||
|
*
|
||||||
|
* Takes a book entry and changes the ID to the given one
|
||||||
|
*/
|
||||||
|
void update_line(char** line, int new_id);
|
||||||
|
|
||||||
|
#endif
|
124
src/db.c
124
src/db.c
@ -1,124 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <sqlite3.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <cjson/cJSON.h>
|
|
||||||
#include <curl/curl.h>
|
|
||||||
|
|
||||||
|
|
||||||
#include "curl.h"
|
|
||||||
#include "json.h"
|
|
||||||
#include "db.h"
|
|
||||||
|
|
||||||
void do_db_entry(enum DB_OPTIONS option, ...)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
sqlite3* db;
|
|
||||||
va_list args;
|
|
||||||
|
|
||||||
rc = sqlite3_open("books.db", &db);
|
|
||||||
if (rc != SQLITE_OK) {
|
|
||||||
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
|
||||||
sqlite3_close(db);
|
|
||||||
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
setup_db(db);
|
|
||||||
|
|
||||||
va_start(args, option);
|
|
||||||
switch(option) {
|
|
||||||
case ADD:
|
|
||||||
add_to_db(va_arg(args, book_t*), db);
|
|
||||||
break;
|
|
||||||
case REMOVE:
|
|
||||||
remove_from_db(va_arg(args, int), db);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
fprintf(stderr, "Invalid db command given!\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
va_end(args);
|
|
||||||
|
|
||||||
sqlite3_close(db);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setup_db(sqlite3* db)
|
|
||||||
{
|
|
||||||
int rc;
|
|
||||||
char* err_msg = 0;
|
|
||||||
|
|
||||||
rc = sqlite3_exec(db,
|
|
||||||
"CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \
|
|
||||||
isbn TEXT, title TEXT, authors TEXT, imageurl TEXT, year_of_publication YEAR, \
|
|
||||||
page_length UNSIGNED INT, subjects TEXT, date_added TEXT, date_completed TEXT, \
|
|
||||||
progress UNSIGNED TINYINT, publication_date TEXT, subtitle TEXT);",
|
|
||||||
0, 0, &err_msg);
|
|
||||||
|
|
||||||
if (rc != SQLITE_OK) {
|
|
||||||
fprintf(stderr, "SQL error: %s\n", err_msg);
|
|
||||||
|
|
||||||
sqlite3_free(err_msg);
|
|
||||||
sqlite3_close(db);
|
|
||||||
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_to_db(book_t* book, sqlite3* db)
|
|
||||||
{
|
|
||||||
char* sql;
|
|
||||||
int asp_err, rc, entries = 0;
|
|
||||||
char* err_msg = 0;
|
|
||||||
|
|
||||||
sqlite3_stmt* getmsg;
|
|
||||||
sqlite3_prepare(db, "SELECT * FROM BOOKS;", -1, &getmsg, NULL);
|
|
||||||
while (sqlite3_step(getmsg) == SQLITE_ROW)
|
|
||||||
entries++;
|
|
||||||
sqlite3_finalize(getmsg);
|
|
||||||
|
|
||||||
entries++; // new id!!
|
|
||||||
|
|
||||||
asp_err = asprintf(&sql, "INSERT INTO books \
|
|
||||||
(id, isbn, title, authors, imageurl, year_of_publication, \
|
|
||||||
page_length, subjects, date_added, publication_date, subtitle) \
|
|
||||||
VALUES(%d, \"%s\", \"%s\", \"%s\", \"%s\", %d, %d, \"%s\", \"%s\", \"%s\", \"%s\");",
|
|
||||||
entries, book->isbn, book->title, book->authors, book->image_url, book->year_of_publication,
|
|
||||||
book->page_len, book->subjects, book->date_added, book->publication_date, book->subtitle);
|
|
||||||
|
|
||||||
if (-1 == asp_err) {
|
|
||||||
fprintf(stderr, "asprintf failed!\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
|
|
||||||
if (rc != SQLITE_OK) {
|
|
||||||
fprintf(stderr, "SQL error: %s\n", err_msg);
|
|
||||||
sqlite3_free(err_msg);
|
|
||||||
sqlite3_close(db);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void remove_from_db(int id, sqlite3* db)
|
|
||||||
{
|
|
||||||
char* sql;
|
|
||||||
int asp_err, rc;
|
|
||||||
char* err_msg = 0;
|
|
||||||
|
|
||||||
asp_err = asprintf(&sql, "DELETE FROM books WHERE id = %d;", id);
|
|
||||||
if (-1 == asp_err) {
|
|
||||||
fprintf(stderr, "asprintf failed!\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
|
|
||||||
if (rc != SQLITE_OK) {
|
|
||||||
fprintf(stderr, "SQL error: %s\n", err_msg);
|
|
||||||
sqlite3_free(err_msg);
|
|
||||||
sqlite3_close(db);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
47
src/db.h
47
src/db.h
@ -1,47 +0,0 @@
|
|||||||
#ifndef DH_H
|
|
||||||
#define DH_H
|
|
||||||
|
|
||||||
enum DB_OPTIONS {
|
|
||||||
ADD,
|
|
||||||
REMOVE
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* void do_db_entry
|
|
||||||
*
|
|
||||||
* enum DB_OPTIONS option - The type of transaction being made to the DB
|
|
||||||
* VA args
|
|
||||||
* - Expects book_t if option is ADD
|
|
||||||
* - Expects int if option is REMOVE
|
|
||||||
*
|
|
||||||
* Handles the whole process of interacting with the database
|
|
||||||
*/
|
|
||||||
void do_db_entry(enum DB_OPTIONS option, ...);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* void setup_db
|
|
||||||
* sqlite3* db - The database
|
|
||||||
*
|
|
||||||
* Just creates the database if it doesn't exist.
|
|
||||||
*/
|
|
||||||
void setup_db(sqlite3* db);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* void add_to_db
|
|
||||||
* book_t* book - The struct full of book information
|
|
||||||
* sqlite3* db - The database
|
|
||||||
*
|
|
||||||
* Adds the book information to the database
|
|
||||||
*/
|
|
||||||
void add_to_db(book_t* book, sqlite3* db);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* void remove_from_db
|
|
||||||
* int id - The id of the book to remove
|
|
||||||
* sqlite3* db - The database
|
|
||||||
*
|
|
||||||
* Removes the given ID (and its associated book) from the database.
|
|
||||||
*/
|
|
||||||
void remove_from_db(int id, sqlite3* db);
|
|
||||||
|
|
||||||
#endif
|
|
80
src/json.c
80
src/json.c
@ -3,7 +3,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
|
||||||
|
|
||||||
#include "curl.h"
|
#include "curl.h"
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
@ -18,26 +17,26 @@ void check_valid_query(cJSON* numfound)
|
|||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_list(cJSON* bookinfo, char* in_str, char* json_value)
|
void get_authors(cJSON* bookinfo, char* authors)
|
||||||
{
|
{
|
||||||
char* temp_str;
|
char* temp_author;
|
||||||
size_t new_len;
|
size_t new_len;
|
||||||
cJSON* str_arr = cJSON_GetObjectItemCaseSensitive(bookinfo, json_value)->child;
|
cJSON* author_arr = cJSON_GetObjectItemCaseSensitive(bookinfo, "author_name")->child;
|
||||||
|
|
||||||
snprintf(in_str, strlen(str_arr->valuestring) + 1, "%s", str_arr->valuestring);
|
snprintf(authors, strlen(author_arr->valuestring) + 1, "%s", author_arr->valuestring);
|
||||||
str_arr = str_arr->next;
|
author_arr = author_arr->next;
|
||||||
|
|
||||||
while (NULL != str_arr) {
|
while (NULL != author_arr) {
|
||||||
// The plus 1 is for the \0, the plus 2 is for the ", "
|
// The plus 1 is for the \0, the plus 2 is for the ", "
|
||||||
new_len = strlen(in_str) + strlen(str_arr->valuestring) + 1 + 2;
|
new_len = strlen(authors) + strlen(author_arr->valuestring) + 1 + 2;
|
||||||
|
|
||||||
temp_str = malloc(sizeof(char) * new_len);
|
temp_author = malloc(sizeof(char) * new_len);
|
||||||
snprintf(temp_str, new_len, "%s, %s", in_str, str_arr->valuestring);
|
snprintf(temp_author, new_len, "%s, %s", authors, author_arr->valuestring);
|
||||||
memcpy(in_str, temp_str, new_len);
|
memcpy(authors, temp_author, new_len);
|
||||||
|
|
||||||
free(temp_str);
|
free(temp_author);
|
||||||
|
|
||||||
str_arr = str_arr->next;
|
author_arr = author_arr->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,57 +45,21 @@ void get_image_link(cJSON* bookinfo, book_t* book)
|
|||||||
{
|
{
|
||||||
int image_id = cJSON_GetObjectItemCaseSensitive(bookinfo, "cover_i")->valueint;
|
int image_id = cJSON_GetObjectItemCaseSensitive(bookinfo, "cover_i")->valueint;
|
||||||
|
|
||||||
|
|
||||||
const char* begin = "https://covers.openlibrary.org/b/id/";
|
const char* begin = "https://covers.openlibrary.org/b/id/";
|
||||||
const char* end = "-L.jpg";
|
const char* end = "-L.jpg";
|
||||||
|
|
||||||
book->image_url = malloc(sizeof(char) * MAX_BUF_LEN);
|
book->image_url = malloc(sizeof(char) * MAX_BUF_LEN);
|
||||||
|
|
||||||
sprintf(book->image_url, "%s%d%s", begin, image_id, end);
|
sprintf(book->image_url, "%s%d%s", begin, image_id, end);
|
||||||
}
|
|
||||||
|
|
||||||
void set_list_values(cJSON* bookinfo, book_t* book)
|
|
||||||
{
|
|
||||||
char authors[MAX_BUF_LEN];
|
|
||||||
char subjects[MAX_BUF_LEN];
|
|
||||||
|
|
||||||
get_list(bookinfo, authors, "author_name");
|
|
||||||
get_list(bookinfo, subjects, "subject");
|
|
||||||
|
|
||||||
// Need to malloc, because we need to copy authors into the book struct
|
|
||||||
book->authors = (char*) malloc(sizeof(char) * (strlen(authors) + 1));
|
|
||||||
memcpy(book->authors, authors, strlen(authors) + 1);
|
|
||||||
|
|
||||||
book->subjects = (char*) malloc(sizeof(char) * (strlen(subjects) + 1));
|
|
||||||
memcpy(book->subjects, subjects, strlen(subjects) + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_actual_values(cJSON* bookinfo, book_t* book, char* isbn)
|
|
||||||
{
|
|
||||||
book->isbn = isbn;
|
|
||||||
book->title = cJSON_GetObjectItemCaseSensitive(bookinfo, "title")->valuestring;
|
|
||||||
book->year_of_publication = cJSON_GetObjectItemCaseSensitive(bookinfo, "first_publish_year")->valueint;
|
|
||||||
book->page_len = cJSON_GetObjectItemCaseSensitive(bookinfo, "number_of_pages_median")->valueint;
|
|
||||||
book->subtitle = cJSON_GetObjectItemCaseSensitive(bookinfo, "subtitle")->valuestring;
|
|
||||||
book->publication_date = cJSON_GetObjectItemCaseSensitive(bookinfo, "publish_date")->child->valuestring;
|
|
||||||
|
|
||||||
get_image_link(bookinfo, book);
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_current_date(book_t* book)
|
|
||||||
{
|
|
||||||
char date[MAX_BUF_LEN];
|
|
||||||
|
|
||||||
time_t t;
|
|
||||||
time(&t);
|
|
||||||
|
|
||||||
sprintf(date, "%s", ctime(&t));
|
|
||||||
|
|
||||||
book->date_added = (char*) malloc(sizeof(char) * (strlen(date) + 1));
|
|
||||||
memcpy(book->date_added, date, strlen(date) + 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_json(string* s, char* isbn, book_t* book)
|
void parse_json(string* s, char* isbn, book_t* book)
|
||||||
{
|
{
|
||||||
|
char authors[MAX_BUF_LEN];
|
||||||
|
|
||||||
cJSON* json = cJSON_Parse(s->buf);
|
cJSON* json = cJSON_Parse(s->buf);
|
||||||
if (NULL == json) {
|
if (NULL == json) {
|
||||||
const char* error_ptr = cJSON_GetErrorPtr();
|
const char* error_ptr = cJSON_GetErrorPtr();
|
||||||
@ -110,7 +73,14 @@ void parse_json(string* s, char* isbn, book_t* book)
|
|||||||
|
|
||||||
cJSON* bookinfo = cJSON_GetObjectItemCaseSensitive(json, "docs")->child;
|
cJSON* bookinfo = cJSON_GetObjectItemCaseSensitive(json, "docs")->child;
|
||||||
|
|
||||||
set_current_date(book);
|
book->isbn = isbn;
|
||||||
set_actual_values(bookinfo, book, isbn);
|
book->title = cJSON_GetObjectItemCaseSensitive(bookinfo, "title")->valuestring;
|
||||||
set_list_values(bookinfo, book);
|
book->year_of_publication = cJSON_GetObjectItemCaseSensitive(bookinfo, "first_publish_year")->valueint;
|
||||||
|
book->page_len = cJSON_GetObjectItemCaseSensitive(bookinfo, "number_of_pages_median")->valueint;
|
||||||
|
|
||||||
|
get_authors(bookinfo, authors);
|
||||||
|
get_image_link(bookinfo, book);
|
||||||
|
// Need to malloc, because we need to copy authors into the book struct
|
||||||
|
book->authors = (char*) malloc(sizeof(char) * (strlen(authors) + 1));
|
||||||
|
memcpy(book->authors, authors, strlen(authors) + 1);
|
||||||
}
|
}
|
||||||
|
@ -10,11 +10,6 @@ typedef struct book_t {
|
|||||||
char* image_url;
|
char* image_url;
|
||||||
int year_of_publication;
|
int year_of_publication;
|
||||||
int page_len;
|
int page_len;
|
||||||
char* subjects;
|
|
||||||
char* date_added;
|
|
||||||
char* publication_date;
|
|
||||||
char* subtitle;
|
|
||||||
|
|
||||||
} book_t;
|
} book_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
28
src/main.c
28
src/main.c
@ -3,15 +3,18 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sqlite3.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "curl.h"
|
#include "curl.h"
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
#include "db.h"
|
#include "csv.h"
|
||||||
|
|
||||||
#define MAX_BUF_LEN 1024
|
#define MAX_BUF_LEN 1024
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: we need to check the csv file for duplicates
|
||||||
|
* TODO: allow us to remove a book from the csv and update the ids
|
||||||
|
*/
|
||||||
void do_ISBN_get(char* argv[])
|
void do_ISBN_get(char* argv[])
|
||||||
{
|
{
|
||||||
// want to hold a max of 14 so we can hold up to ISBN13s
|
// want to hold a max of 14 so we can hold up to ISBN13s
|
||||||
@ -46,7 +49,7 @@ void do_ISBN_get(char* argv[])
|
|||||||
// Now we want to parse the JSON input
|
// Now we want to parse the JSON input
|
||||||
parse_json(&get_output, isbn_buf, &new_book);
|
parse_json(&get_output, isbn_buf, &new_book);
|
||||||
|
|
||||||
do_db_entry(ADD, &new_book);
|
write_to_file(&new_book);
|
||||||
|
|
||||||
// we need to free these strings
|
// we need to free these strings
|
||||||
free(get_output.buf);
|
free(get_output.buf);
|
||||||
@ -54,9 +57,11 @@ void do_ISBN_get(char* argv[])
|
|||||||
free(new_book.image_url);
|
free(new_book.image_url);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_help_menu(char* program)
|
void process_args(char* argv[])
|
||||||
{
|
{
|
||||||
printf("%s - An ISBN lookup tool.\n", program);
|
int id;
|
||||||
|
if (0 == strcmp(argv[1], "--help")) {
|
||||||
|
printf("%s - An ISBN lookup tool.\n", argv[0]);
|
||||||
printf("Author: Nathan Singer\n");
|
printf("Author: Nathan Singer\n");
|
||||||
|
|
||||||
puts("\n");
|
puts("\n");
|
||||||
@ -64,13 +69,6 @@ void print_help_menu(char* program)
|
|||||||
puts("--help - Shows this message.");
|
puts("--help - Shows this message.");
|
||||||
puts("[isbn] -- Attempts to download a book from the given ISBN-10 or ISBN-13 input.");
|
puts("[isbn] -- Attempts to download a book from the given ISBN-10 or ISBN-13 input.");
|
||||||
puts("remove [id] -- Removes a book with the given ID from the book database.");
|
puts("remove [id] -- Removes a book with the given ID from the book database.");
|
||||||
}
|
|
||||||
|
|
||||||
void process_args(char* argv[])
|
|
||||||
{
|
|
||||||
int id;
|
|
||||||
if (0 == strcmp(argv[1], "--help")) {
|
|
||||||
print_help_menu(argv[0]);
|
|
||||||
} else if (0 == strcmp(argv[1], "remove")) {
|
} else if (0 == strcmp(argv[1], "remove")) {
|
||||||
if (NULL == argv[2]) {
|
if (NULL == argv[2]) {
|
||||||
printf("Not enough arguments! Try typing %s --help\n", argv[0]);
|
printf("Not enough arguments! Try typing %s --help\n", argv[0]);
|
||||||
@ -83,13 +81,16 @@ void process_args(char* argv[])
|
|||||||
printf("Invalid book ID given!\n");
|
printf("Invalid book ID given!\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
do_db_entry(REMOVE, id);
|
|
||||||
|
remove_line_from_file(id);
|
||||||
} else {
|
} else {
|
||||||
// lets assume its an ISBN and let the other functions fail if its not
|
// lets assume its an ISBN and let the other functions fail if its not
|
||||||
do_ISBN_get(argv);
|
do_ISBN_get(argv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
if (1 == argc) {
|
if (1 == argc) {
|
||||||
@ -99,6 +100,5 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
process_args(argv);
|
process_args(argv);
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user