isbn-lookup/main.c

125 lines
2.4 KiB
C
Raw Normal View History

2024-01-12 12:15:17 -05:00
#include <curl/curl.h>
#include <cjson/cJSON.h>
2024-01-13 10:38:03 -05:00
#include <stdio.h>
2024-01-12 12:15:17 -05:00
#include <stdlib.h>
#include <string.h>
2024-01-13 10:38:03 -05:00
#include <unistd.h>
2024-01-12 12:15:17 -05:00
#include "curl.h"
#include "json.h"
2024-01-12 12:15:17 -05:00
2024-01-13 10:38:03 -05:00
#define FILE_NAME "books.csv"
#define MAX_BUF_LEN 1024
2024-01-12 12:15:17 -05:00
void print_book(book_t* book)
2024-01-12 12:15:17 -05:00
{
printf("ISBN: %s\n", book->isbn);
printf("Title: %s\n", book->title);
printf("Author(s): %s\n", book->authors);
printf("(First) Year of Publication: %d\n", book->year_of_publication);
printf("Page length: %d\n", book->page_len);
2024-01-13 10:38:03 -05:00
}
void write_to_file(book_t* book)
2024-01-13 10:38:03 -05:00
{
FILE* file;
int file_exists;
/**
* 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");
fprintf(file, "isbn,title,authors,year of publication,page length\n");
} else {
file = fopen(FILE_NAME, "a");
}
// now we write the information
fprintf(file, "\"%s\",\"%s\",\"%s\",%d,%d\n",
book->isbn, book->title, book->authors, book->year_of_publication, book->page_len);
2024-01-13 10:38:03 -05:00
fclose(file);
}
2024-01-12 12:15:17 -05:00
int main(int argc, char* argv[])
{
2024-01-12 12:29:09 -05:00
char isbn_buf[14]; // want to hold a max of 14 so we can hold up to ISBN13s
2024-01-13 10:38:03 -05:00
char options[2];
2024-01-12 12:15:17 -05:00
CURLcode res;
book_t new_book;
2024-01-12 12:15:17 -05:00
2024-01-13 10:38:03 -05:00
if (3 != argc) {
printf("Usage: isbn [isbn] [options]\n");
2024-01-12 12:15:17 -05:00
return EXIT_FAILURE;
}
2024-01-12 12:50:10 -05:00
size_t input_len = strlen(argv[1]);
if (!(13 == input_len || 10 == input_len)) {
fprintf(stderr, "Invalid ISBN submitted!");
return EXIT_FAILURE;
}
2024-01-12 12:15:17 -05:00
/**
* We must initialize cURL
*/
curl_global_init(CURL_GLOBAL_ALL);
/**
* Grab the ISBN from argv
*/
snprintf(isbn_buf, 14, "%s", argv[1]);
2024-01-13 10:38:03 -05:00
/**
* Grab the formatting options from argv
*/
snprintf(options, 2, "%s", argv[2]);
if (!(('w' == options[0]) || ('r' == options[0]))) {
fprintf(stderr, "Invalid option submitted!\n");
return EXIT_FAILURE;
}
2024-01-13 10:38:03 -05:00
2024-01-12 12:15:17 -05:00
/**
* Setup the output string
*/
string get_output;
init_string(&get_output);
/**
* Perform the get request
2024-01-13 10:38:03 -05:00
*/
res = perform_book_get(isbn_buf, &get_output);
2024-01-12 12:15:17 -05:00
if (0 != res) {
fprintf(stderr, "Failed to perform the get request!\n");
return EXIT_FAILURE;
}
2024-01-12 12:15:17 -05:00
/**
* Now we want to parse the JSON input
*/
parse_json(&get_output, isbn_buf, &new_book);
2024-01-12 12:15:17 -05:00
/**
* We need to free this string
*/
free(get_output.buf);
/**
* NOW we either print or save the book
*/
if (options[0] == 'w')
write_to_file(&new_book);
else if (options[0] == 'r')
print_book(&new_book);
2024-01-12 12:15:17 -05:00
return 0;
}