cleans up the outputting a bit
This commit is contained in:
parent
16fd5277b8
commit
a88232f48c
69
src/main.c
69
src/main.c
@ -12,6 +12,11 @@
|
|||||||
|
|
||||||
#define MAX_BUF_LEN 1024
|
#define MAX_BUF_LEN 1024
|
||||||
|
|
||||||
|
enum book_options {
|
||||||
|
PRINT_TO_STDIN,
|
||||||
|
WRITE_TO_FILE
|
||||||
|
};
|
||||||
|
|
||||||
void print_book(book_t* book)
|
void print_book(book_t* book)
|
||||||
{
|
{
|
||||||
printf("ISBN: %s\n", book->isbn);
|
printf("ISBN: %s\n", book->isbn);
|
||||||
@ -46,12 +51,23 @@ void write_to_file(book_t* book)
|
|||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void do_output(book_t* book, enum book_options output_type)
|
||||||
|
{
|
||||||
|
if (WRITE_TO_FILE == output_type) {
|
||||||
|
write_to_file(book);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
print_book(book);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
char isbn_buf[14]; // 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
|
||||||
|
char isbn_buf[14];
|
||||||
char options[2];
|
char options[2];
|
||||||
CURLcode res;
|
CURLcode res;
|
||||||
book_t new_book;
|
book_t new_book;
|
||||||
|
enum book_options output_type;
|
||||||
|
|
||||||
if (3 != argc) {
|
if (3 != argc) {
|
||||||
printf("Usage: isbn [isbn] [options]\n");
|
printf("Usage: isbn [isbn] [options]\n");
|
||||||
@ -65,35 +81,32 @@ int main(int argc, char* argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
// We must initialize cURL
|
||||||
* We must initialize cURL
|
|
||||||
*/
|
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
|
|
||||||
/**
|
// Grab the ISBN from argv
|
||||||
* Grab the ISBN from argv
|
|
||||||
*/
|
|
||||||
snprintf(isbn_buf, 14, "%s", argv[1]);
|
snprintf(isbn_buf, 14, "%s", argv[1]);
|
||||||
|
|
||||||
/**
|
// Grab the formatting options from argv
|
||||||
* Grab the formatting options from argv
|
|
||||||
*/
|
|
||||||
snprintf(options, 2, "%s", argv[2]);
|
snprintf(options, 2, "%s", argv[2]);
|
||||||
if (!(('w' == options[0]) || ('r' == options[0]))) {
|
switch(options[0]) {
|
||||||
fprintf(stderr, "Invalid option submitted!\n");
|
case 'w':
|
||||||
return EXIT_FAILURE;
|
output_type = WRITE_TO_FILE;
|
||||||
|
break;
|
||||||
|
case 'r':
|
||||||
|
output_type = PRINT_TO_STDIN;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "Invalid option provided!\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
// Setup the output string
|
||||||
* Setup the output string
|
|
||||||
*/
|
|
||||||
string get_output;
|
string get_output;
|
||||||
init_string(&get_output);
|
init_string(&get_output);
|
||||||
|
|
||||||
/**
|
// Perform the get request
|
||||||
* Perform the get request
|
|
||||||
*/
|
|
||||||
res = perform_book_get(isbn_buf, &get_output);
|
res = perform_book_get(isbn_buf, &get_output);
|
||||||
if (0 != res) {
|
if (0 != res) {
|
||||||
fprintf(stderr, "Failed to perform the get request!\n");
|
fprintf(stderr, "Failed to perform the get request!\n");
|
||||||
@ -101,24 +114,16 @@ int main(int argc, 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);
|
||||||
|
|
||||||
/**
|
// We need to free this string
|
||||||
* We need to free this string
|
|
||||||
*/
|
|
||||||
free(get_output.buf);
|
free(get_output.buf);
|
||||||
|
|
||||||
/**
|
// NOW we either print or save the book
|
||||||
* NOW we either print or save the book
|
do_output(&new_book, output_type);
|
||||||
*/
|
|
||||||
if (options[0] == 'w')
|
|
||||||
write_to_file(&new_book);
|
|
||||||
else if (options[0] == 'r')
|
|
||||||
print_book(&new_book);
|
|
||||||
|
|
||||||
|
// Need to tree this string
|
||||||
free(new_book.authors);
|
free(new_book.authors);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user