cleans up some code, adds some comments

This commit is contained in:
SuperNovaa41 2024-01-12 12:55:30 -05:00
parent 8994bc0365
commit db984d5e35

28
main.c
View File

@ -97,6 +97,12 @@ CURLcode perform_book_get(char isbn_buf[14], string* s)
return result; return result;
} }
/**
* void parse_json
* string* s - Pointer to the string struct
*
* Parses the JSON inside of s.buf and prints the information we're looking for
*/
void parse_json(string* s) void parse_json(string* s)
{ {
cJSON* json = cJSON_Parse(s->buf); cJSON* json = cJSON_Parse(s->buf);
@ -108,6 +114,9 @@ void parse_json(string* s)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/**
* if there are no results, or too many, we should exit
*/
cJSON* numFound = cJSON_GetObjectItemCaseSensitive(json, "numFound"); cJSON* numFound = cJSON_GetObjectItemCaseSensitive(json, "numFound");
if (0 == numFound->valueint) { if (0 == numFound->valueint) {
fprintf(stderr, "No ISBN found!\n"); fprintf(stderr, "No ISBN found!\n");
@ -123,18 +132,19 @@ void parse_json(string* s)
printf("Title: %s\n", cJSON_GetObjectItemCaseSensitive(child, "title")->valuestring); printf("Title: %s\n", cJSON_GetObjectItemCaseSensitive(child, "title")->valuestring);
/**
* The author value is a linked list, so we want to loop through each value
*/
cJSON* authors = cJSON_GetObjectItemCaseSensitive(child, "author_name"); cJSON* authors = cJSON_GetObjectItemCaseSensitive(child, "author_name");
cJSON* authorarr = authors->child;
printf("Author(s): "); printf("Author(s): ");
if (cJSON_IsArray(authors)) { while (NULL != authorarr->next) {
cJSON* authorarr = authors->child; printf("%s, ", authorarr->valuestring);
while (NULL != authorarr->next) { authorarr = authorarr->next;
printf("%s, ", authorarr->valuestring);
authorarr = authorarr->next;
}
printf("%s\n", authorarr->valuestring);
} else {
printf("Author(s): %s\n", authors->valuestring);
} }
printf("%s\n", authorarr->valuestring);
printf("(First) Year of Publication: %d\n", cJSON_GetObjectItemCaseSensitive(child, "first_publish_year")->valueint); printf("(First) Year of Publication: %d\n", cJSON_GetObjectItemCaseSensitive(child, "first_publish_year")->valueint);
printf("Page length: %d\n", cJSON_GetObjectItemCaseSensitive(child, "number_of_pages_median")->valueint); printf("Page length: %d\n", cJSON_GetObjectItemCaseSensitive(child, "number_of_pages_median")->valueint);