Compare commits
4 Commits
2052137673
...
4851e19159
Author | SHA1 | Date | |
---|---|---|---|
![]() |
4851e19159 | ||
![]() |
3d6f78e29a | ||
![]() |
3513858264 | ||
![]() |
a2d4e27d73 |
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
|||||||
CC=gcc
|
CC=gcc
|
||||||
CFLAGS= -c -g -Wall
|
CFLAGS= -c -g -Wall
|
||||||
|
|
||||||
LDLIBS = -lcurl -lcjson -lsqlite3
|
LDLIBS = -lcurl -lcjson -lsqlite3 -lm
|
||||||
|
|
||||||
TARGET := isbn-lookup
|
TARGET := isbn-lookup
|
||||||
|
|
||||||
|
107
src/db.c
107
src/db.c
@ -1,7 +1,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <sqlite3.h>
|
#include <sqlite3.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#include "include/db.h"
|
#include "include/db.h"
|
||||||
|
|
||||||
@ -40,7 +42,7 @@ void setup_db(sqlite3* db)
|
|||||||
err = sqlite3_exec(db,
|
err = sqlite3_exec(db,
|
||||||
"CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \
|
"CREATE TABLE IF NOT EXISTS books (id UNSIGNED INT PRIMARY KEY, \
|
||||||
title TEXT, subtitle TEXT, isbn TEXT, authors TEXT, subjects TEXT, \
|
title TEXT, subtitle TEXT, isbn TEXT, authors TEXT, subjects TEXT, \
|
||||||
langs TEXT, date_added TEXT, date_completed TEXT, pub_date TEXT, \
|
langs TEXT, date_added TEXT, pub_date TEXT, \
|
||||||
image_code TEXT, year_of_pub YEAR, page_len UNSIGNED INT);",
|
image_code TEXT, year_of_pub YEAR, page_len UNSIGNED INT);",
|
||||||
0, 0, &err_buf);
|
0, 0, &err_buf);
|
||||||
if (err != SQLITE_OK) {
|
if (err != SQLITE_OK) {
|
||||||
@ -53,18 +55,113 @@ void setup_db(sqlite3* db)
|
|||||||
sqlite3_close(db);
|
sqlite3_close(db);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define INSERT_STR "INSERT INTO books (id, title, subtitle, isbn, authors, subjects, langs, date_added, date_completed, pub_date, image_code, year_of_pub, page_len) VALUES("
|
static void add_str_entry(char** dst, char** src, bool last)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
len = strlen(*src) + strlen(*dst) + 2 + 1 + (last ? 0 : 2);
|
||||||
|
|
||||||
|
*dst = realloc(*dst, sizeof(char) * len);
|
||||||
|
|
||||||
|
*dst = strcat(*dst, "\"");
|
||||||
|
*dst = strcat(*dst, *src);
|
||||||
|
*dst = strcat(*dst, "\"");
|
||||||
|
if (!last)
|
||||||
|
*dst = strcat(*dst, ", ");
|
||||||
|
}
|
||||||
|
|
||||||
|
static void add_int_entry(char** dst, int src, bool last)
|
||||||
|
{
|
||||||
|
size_t len_int, len;
|
||||||
|
char* num;
|
||||||
|
|
||||||
|
len_int = (src == 0) ? 1 : floor(log10(abs(src))) + 1;
|
||||||
|
|
||||||
|
num = malloc(sizeof(char) * (len_int + 1));
|
||||||
|
snprintf(num, len_int + 1, "%d", src);
|
||||||
|
|
||||||
|
len = strlen(*dst) + len_int + 1 + (last ? 0 : 2);
|
||||||
|
*dst = realloc(*dst, sizeof(char) * len);
|
||||||
|
*dst = strcat(*dst, num);
|
||||||
|
|
||||||
|
if (!last)
|
||||||
|
strcat(*dst, ", ");
|
||||||
|
|
||||||
|
free(num);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void string_cat_vals(char** dst, book_t* book, int id)
|
||||||
|
{
|
||||||
|
add_int_entry(dst, id, false);
|
||||||
|
add_str_entry(dst, &(book->title), false);
|
||||||
|
add_str_entry(dst, &(book->subtitle), false);
|
||||||
|
add_str_entry(dst, &(book->isbn), false);
|
||||||
|
add_str_entry(dst, &(book->authors), false);
|
||||||
|
add_str_entry(dst, &(book->subjects), false);
|
||||||
|
add_str_entry(dst, &(book->langs), false);
|
||||||
|
add_str_entry(dst, &(book->date_added), false);
|
||||||
|
add_str_entry(dst, &(book->pub_date), false);
|
||||||
|
add_str_entry(dst, &(book->image_code), false);
|
||||||
|
add_int_entry(dst, book->page_len, true);
|
||||||
|
|
||||||
|
*dst = realloc(*dst, sizeof(char) * (strlen(*dst) + 3));
|
||||||
|
strcat(*dst, ");");
|
||||||
|
}
|
||||||
|
|
||||||
|
#define INSERT_STR "INSERT INTO books (id, title, subtitle, isbn, authors, subjects, langs, date_added, pub_date, image_code, page_len) VALUES("
|
||||||
|
|
||||||
void add_to_db(book_t* book, sqlite3* db)
|
void add_to_db(book_t* book, sqlite3* db)
|
||||||
{
|
{
|
||||||
int id;
|
int id, err;
|
||||||
char* cmd;
|
char* cmd;
|
||||||
|
char* err_msg = 0;
|
||||||
|
|
||||||
open_db(&db);
|
open_db(&db);
|
||||||
|
|
||||||
id = get_num_rows(db) + 1;
|
id = get_num_rows(db) + 4;
|
||||||
|
|
||||||
cmd = malloc(sizeof(char) * (strlen(INSERT_STR) + 1));
|
cmd = malloc(sizeof(char) * (strlen(INSERT_STR) + 1));
|
||||||
snprintf(cmd, strlen(INSERT_STR), "%s", INSERT_STR);
|
snprintf(cmd, strlen(INSERT_STR) + 1, "%s", INSERT_STR);
|
||||||
|
|
||||||
|
string_cat_vals(&cmd, book, id);
|
||||||
|
|
||||||
|
printf("%s\n", cmd);
|
||||||
|
|
||||||
|
err = sqlite3_exec(db, cmd, 0, 0, &err_msg);
|
||||||
|
if (err != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "sqlite3_exec: %s\n", err_msg);
|
||||||
|
sqlite3_free(err_msg);
|
||||||
|
sqlite3_close(db);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
|
free(cmd);
|
||||||
|
}
|
||||||
|
#undef INSERT_STR
|
||||||
|
|
||||||
|
#define DEL_STR "DELETE FROM books WHERE id = "
|
||||||
|
void remove_from_db(int id, sqlite3* db)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
char *cmd, *err_msg;
|
||||||
|
|
||||||
|
open_db(&db);
|
||||||
|
|
||||||
|
cmd = malloc(sizeof(char) * (strlen(DEL_STR) + 1));
|
||||||
|
snprintf(cmd, strlen(DEL_STR), "%s", DEL_STR);
|
||||||
|
|
||||||
|
add_int_entry(&cmd, id, true);
|
||||||
|
|
||||||
|
err = sqlite3_exec(db, cmd, 0, 0, &err_msg);
|
||||||
|
if (err != SQLITE_OK) {
|
||||||
|
fprintf(stderr, "sqlite3_exec: %s\n", err_msg);
|
||||||
|
sqlite3_free(err_msg);
|
||||||
|
sqlite3_close(db);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
sqlite3_close(db);
|
||||||
|
free(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef DEL_STR
|
||||||
|
@ -9,11 +9,14 @@ typedef struct {
|
|||||||
char *date_added, *pub_date;
|
char *date_added, *pub_date;
|
||||||
char *image_code;
|
char *image_code;
|
||||||
|
|
||||||
int year_of_pub, page_len;
|
int page_len;
|
||||||
} book_t;
|
} book_t;
|
||||||
|
|
||||||
void setup_db(sqlite3* db);
|
void setup_db(sqlite3* db);
|
||||||
|
|
||||||
void add_to_db(book_t* book, sqlite3* db);
|
void add_to_db(book_t* book, sqlite3* db);
|
||||||
|
|
||||||
|
void remove_from_db(int id, sqlite3* db);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
24
src/main.c
24
src/main.c
@ -4,6 +4,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "include/curl.h"
|
#include "include/curl.h"
|
||||||
|
#include "include/db.h"
|
||||||
#include "include/json.h"
|
#include "include/json.h"
|
||||||
|
|
||||||
struct flags {
|
struct flags {
|
||||||
@ -69,6 +70,7 @@ int main(int argc, char* argv[])
|
|||||||
init_flags(&flags);
|
init_flags(&flags);
|
||||||
argp_parse(&argp, argc, argv, 0, 0, &flags);
|
argp_parse(&argp, argc, argv, 0, 0, &flags);
|
||||||
|
|
||||||
|
/*
|
||||||
//book_api_call("9780762437818", &resp);
|
//book_api_call("9780762437818", &resp);
|
||||||
book_api_call(flags.isbn, &resp);
|
book_api_call(flags.isbn, &resp);
|
||||||
|
|
||||||
@ -81,8 +83,28 @@ int main(int argc, char* argv[])
|
|||||||
if (flags.json)
|
if (flags.json)
|
||||||
json_out(&query);
|
json_out(&query);
|
||||||
else
|
else
|
||||||
// db out;
|
; // db out;
|
||||||
|
*/
|
||||||
|
|
||||||
|
book_t book;
|
||||||
|
|
||||||
|
book.title = "hi";
|
||||||
|
book.subtitle = "test";
|
||||||
|
book.isbn = "1234";
|
||||||
|
book.authors = "test";
|
||||||
|
book.subjects = "test";
|
||||||
|
book.langs = "eng";
|
||||||
|
book.date_added = "test";
|
||||||
|
book.pub_date = "test";
|
||||||
|
book.image_code = "test";
|
||||||
|
|
||||||
|
book.page_len = 1;
|
||||||
|
|
||||||
|
sqlite3* db;
|
||||||
|
|
||||||
|
setup_db(db);
|
||||||
|
|
||||||
|
add_to_db(&book, db);
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user