diff options
-rw-r--r-- | include/utils.h | 14 | ||||
-rw-r--r-- | include/video.h | 7 | ||||
-rw-r--r-- | libs/curl.c | 42 | ||||
-rw-r--r-- | libs/curl.h | 2 | ||||
-rw-r--r-- | libs/json.c | 120 | ||||
-rw-r--r-- | libs/json.h | 13 | ||||
-rw-r--r-- | libs/string.c | 20 | ||||
-rw-r--r-- | libs/string.h | 8 | ||||
-rw-r--r-- | queryt.c | 143 | ||||
-rw-r--r-- | src/utils.c | 153 | ||||
-rw-r--r-- | src/video.c | 41 |
11 files changed, 297 insertions, 266 deletions
diff --git a/include/utils.h b/include/utils.h index 4444279..89326d9 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,10 +1,10 @@ #pragma once #include "video.h" -int tokenCount(char *string, char *delimiter); -char** tokenizer(char *string, char *delimiter); -void freeTokens(char **tokens, int size); -char* queryNormalizer(char *query); -char* extractQueryJSON(char *youtubeurl); -int checkNumber(char *num); -void printVideoInfo(char *format, Video *videos, int numberOfVideos); +int tokenCount (char *string, char *delimiter); +char **tokenizer (char *string, char *delimiter); +void freeTokens (char **tokens, int size); +char *queryNormalizer (char *query); +char *extractQueryJSON (char *youtubeurl); +int checkNumber (char *num); +void printVideoInfo (char *format, Video *videos, int numberOfVideos); diff --git a/include/video.h b/include/video.h index 9ac136d..7649e3f 100644 --- a/include/video.h +++ b/include/video.h @@ -2,9 +2,8 @@ typedef struct video { - char *title, *author, *id, *duration; + char *title, *author, *id, *duration; } Video; - -Video createVideo(char *title, char *author, char *id, char *duration); -void freeVideo(Video *v); +Video createVideo (char *title, char *author, char *id, char *duration); +void freeVideo (Video *v); diff --git a/libs/curl.c b/libs/curl.c index bf6cb93..03509d9 100644 --- a/libs/curl.c +++ b/libs/curl.c @@ -1,42 +1,44 @@ +#include "curl.h" +#include "string.h" +#include <curl/curl.h> +#include <curl/easy.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <curl/curl.h> -#include <curl/easy.h> -#include "curl.h" -#include "string.h" -size_t downloadCallback(char *ptr, size_t size, size_t nmemb, String *output) +size_t +downloadCallback (char *ptr, size_t size, size_t nmemb, String *output) { output->length += nmemb; - output->string = realloc(output->string, output->length + 1); - strcat(output->string, ptr); - return size*nmemb; + output->string = realloc (output->string, output->length + 1); + strcat (output->string, ptr); + return size * nmemb; } -char* downloadPage(char *page) +char * +downloadPage (char *page) { char *str = NULL; - if ( page != NULL && strlen(page) > 0 ) + if (page != NULL && strlen (page) > 0) { // Initializing cURL and Temporary String Structure CURL *curl; - String s = newString(""); - curl = curl_easy_init(); + String s = newString (""); + curl = curl_easy_init (); // Saving cURL'ed webpage into String Structure - curl_easy_setopt(curl, CURLOPT_URL, page); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); - curl_easy_perform(curl); + curl_easy_setopt (curl, CURLOPT_URL, page); + curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, downloadCallback); + curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s); + curl_easy_perform (curl); - str = (char *) calloc(s.length + 1, sizeof(char)); - strcpy(str, s.string); + str = (char *)calloc (s.length + 1, sizeof (char)); + strcpy (str, s.string); // Cleanup - curl_easy_cleanup(curl); - freeString(&s); + curl_easy_cleanup (curl); + freeString (&s); } return str; diff --git a/libs/curl.h b/libs/curl.h index 3aa59a2..4c76319 100644 --- a/libs/curl.h +++ b/libs/curl.h @@ -1,3 +1,3 @@ #pragma once -char* downloadPage(char *page); +char *downloadPage (char *page); diff --git a/libs/json.c b/libs/json.c index eb92cce..03d8aab 100644 --- a/libs/json.c +++ b/libs/json.c @@ -1,58 +1,64 @@ +#include "json.h" +#include "../include/video.h" +#include <json-c/json.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <json-c/json.h> -#include "json.h" -#include "../include/video.h" -json_object* jsonParseString(char* stringedJSON) +json_object * +jsonParseString (char *stringedJSON) { - return ( stringedJSON != NULL && strlen(stringedJSON) > 0 ) ? json_tokener_parse(stringedJSON) : NULL; + return (stringedJSON != NULL && strlen (stringedJSON) > 0) + ? json_tokener_parse (stringedJSON) + : NULL; } -json_object* objectPathNavigator(json_object* object, char path[][MAXLENGTH], int depth) +json_object * +objectPathNavigator (json_object *object, char path[][MAXLENGTH], int depth) { - json_object *navigator = ( json_object_get_type(object) == json_type_array ) ? - json_object_array_get_idx(object, atoi(path[0])) : - json_object_object_get(object, path[0]); + json_object *navigator + = (json_object_get_type (object) == json_type_array) + ? json_object_array_get_idx (object, atoi (path[0])) + : json_object_object_get (object, path[0]); - for( int i = 1; i < depth; i++ ) + for (int i = 1; i < depth; i++) { - navigator = ( json_object_get_type(navigator) == json_type_array ) ? - json_object_array_get_idx(navigator, atoi(path[i])) : - json_object_object_get(navigator, path[i]); + navigator = (json_object_get_type (navigator) == json_type_array) + ? json_object_array_get_idx (navigator, atoi (path[i])) + : json_object_object_get (navigator, path[i]); } return navigator; } -json_object* navigateToVideos(json_object* jsonRoot) +json_object * +navigateToVideos (json_object *jsonRoot) { - char path[][MAXLENGTH] = { - "contents", + char path[][MAXLENGTH] = { "contents", "twoColumnSearchResultsRenderer", "primaryContents", "sectionListRenderer", "contents", "0", "itemSectionRenderer", - "contents" - }; + "contents" }; - return objectPathNavigator(jsonRoot, path, sizeof(path)/sizeof(path[0])); + return objectPathNavigator (jsonRoot, path, + sizeof (path) / sizeof (path[0])); } -int videoCounter(json_object* contents) +int +videoCounter (json_object *contents) { int counter = 0; json_object *iterator; - for ( int i = 0; i < json_object_array_length(contents) ; i++) + for (int i = 0; i < json_object_array_length (contents); i++) { - iterator = json_object_array_get_idx(contents, i); - iterator = json_object_object_get(iterator, "videoRenderer"); + iterator = json_object_array_get_idx (contents, i); + iterator = json_object_object_get (iterator, "videoRenderer"); - if ( json_object_get_type(iterator) != json_type_null ) + if (json_object_get_type (iterator) != json_type_null) { counter++; } @@ -61,61 +67,57 @@ int videoCounter(json_object* contents) return counter; } -Video* generateVideos(json_object* contents) +Video * +generateVideos (json_object *contents) { - char idPath[][MAXLENGTH] = { - "videoId" - }; + char idPath[][MAXLENGTH] = { "videoId" }; - char titlePath[][MAXLENGTH] = { - "title", - "runs", - "0", - "text" - }; + char titlePath[][MAXLENGTH] = { "title", "runs", "0", "text" }; - char authorPath[][MAXLENGTH] = { - "ownerText", - "runs", - "0", - "text" - }; + char authorPath[][MAXLENGTH] = { "ownerText", "runs", "0", "text" }; - char lengthPath[][MAXLENGTH] = { - "lengthText", - "simpleText" - }; + char lengthPath[][MAXLENGTH] = { "lengthText", "simpleText" }; - Video *videos = (Video *) calloc(videoCounter(contents), sizeof(Video)); + Video *videos = (Video *)calloc (videoCounter (contents), sizeof (Video)); - for ( int i = 0, j = 0; i < json_object_array_length(contents); i++ ) + for (int i = 0, j = 0; i < json_object_array_length (contents); i++) { // Initializing Vars For Each Video Structure - char title[TITLE + 1], author[AUTHOR + 1], id[VIDEOID + 1], duration[DURATION + 1]; - json_object *iterator = json_object_array_get_idx(contents, i), *dataHolder; + char title[TITLE + 1], author[AUTHOR + 1], id[VIDEOID + 1], + duration[DURATION + 1]; + json_object *iterator = json_object_array_get_idx (contents, i), + *dataHolder; // Grabbing the information from each path of the JSON - iterator = json_object_object_get(iterator, "videoRenderer"); - if ( json_object_get_type(iterator) != json_type_null ) + iterator = json_object_object_get (iterator, "videoRenderer"); + if (json_object_get_type (iterator) != json_type_null) { // ID Path - dataHolder = objectPathNavigator(iterator, idPath, sizeof(idPath)/sizeof(idPath[0])); - strcpy(id, json_object_get_string(dataHolder)); + dataHolder = objectPathNavigator ( + iterator, idPath, sizeof (idPath) / sizeof (idPath[0])); + strcpy (id, json_object_get_string (dataHolder)); // Title Path - dataHolder = objectPathNavigator(iterator, titlePath, sizeof(titlePath)/sizeof(titlePath[0])); - strcpy(title, json_object_get_string(dataHolder)); + dataHolder = objectPathNavigator ( + iterator, titlePath, sizeof (titlePath) / sizeof (titlePath[0])); + strcpy (title, json_object_get_string (dataHolder)); // Author Path - dataHolder = objectPathNavigator(iterator, authorPath, sizeof(authorPath)/sizeof(authorPath[0])); - strcpy(author, json_object_get_string(dataHolder)); + dataHolder = objectPathNavigator (iterator, authorPath, + sizeof (authorPath) + / sizeof (authorPath[0])); + strcpy (author, json_object_get_string (dataHolder)); // Video Length Path - dataHolder = objectPathNavigator(iterator, lengthPath, sizeof(lengthPath)/sizeof(lengthPath[0])); - ( json_object_get_type(dataHolder) != json_type_null ) ? strcpy(duration, json_object_get_string(dataHolder)) : strcpy(duration, "LIVE NOW") ; + dataHolder = objectPathNavigator (iterator, lengthPath, + sizeof (lengthPath) + / sizeof (lengthPath[0])); + (json_object_get_type (dataHolder) != json_type_null) + ? strcpy (duration, json_object_get_string (dataHolder)) + : strcpy (duration, "LIVE NOW"); // Creating Videos Data - videos[j++] = createVideo(title, author, id, duration); + videos[j++] = createVideo (title, author, id, duration); } } diff --git a/libs/json.h b/libs/json.h index 86cf7bf..7c9c10d 100644 --- a/libs/json.h +++ b/libs/json.h @@ -1,14 +1,15 @@ #pragma once -#include <json-c/json.h> #include "../include/video.h" +#include <json-c/json.h> #define TITLE 100 #define AUTHOR 120 #define VIDEOID 11 #define DURATION 8 #define MAXLENGTH 256 -json_object* jsonParseString(char* stringedJSON); -json_object* objectPathNavigator(json_object* object, char path[][MAXLENGTH], int depth); -json_object* navigateToVideos(json_object* jsonRoot); -int videoCounter(json_object* contents); -Video* generateVideos(json_object* contents); +json_object *jsonParseString (char *stringedJSON); +json_object *objectPathNavigator (json_object *object, char path[][MAXLENGTH], + int depth); +json_object *navigateToVideos (json_object *jsonRoot); +int videoCounter (json_object *contents); +Video *generateVideos (json_object *contents); diff --git a/libs/string.c b/libs/string.c index 9917e4f..8c2eab2 100644 --- a/libs/string.c +++ b/libs/string.c @@ -1,28 +1,30 @@ +#include "string.h" #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "string.h" -String newString(char *string) +String +newString (char *string) { String s; - if (string != NULL && strlen(string) > 0) + if (string != NULL && strlen (string) > 0) { - s.length = strlen(string); - s.string = (char *)calloc(s.length + 1, sizeof(char)); - strcpy(s.string, string); + s.length = strlen (string); + s.string = (char *)calloc (s.length + 1, sizeof (char)); + strcpy (s.string, string); } else { s.length = 0; - s.string = (char *)calloc(1, sizeof(char)); + s.string = (char *)calloc (1, sizeof (char)); } return s; } -void freeString(String *s) +void +freeString (String *s) { s->length = 0; - free(s->string); + free (s->string); } diff --git a/libs/string.h b/libs/string.h index 84a1519..d692095 100644 --- a/libs/string.h +++ b/libs/string.h @@ -2,9 +2,9 @@ typedef struct string { - char *string; - int length; + char *string; + int length; } String; -String newString(char *string); -void freeString(String *s); +String newString (char *string); +void freeString (String *s); @@ -1,136 +1,143 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <getopt.h> -#include <json-c/json.h> #include "include/utils.h" #include "include/video.h" #include "libs/json.h" +#include <getopt.h> +#include <json-c/json.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> #define VERSION "1.0" #define SEARCHLINK "https://www.youtube.com/results?search_query=" -void processOptions(int *options, char **args, int argc); +void processOptions (int *options, char **args, int argc); -int main(int argc, char **argv) +int +main (int argc, char **argv) { int o = 0, opts[] = { 0, 0, 0, 0, 0 }; - char **args = (char **) calloc(3, sizeof(char *)); - for ( int i = 0; i < 3; i++ ) + char **args = (char **)calloc (3, sizeof (char *)); + for (int i = 0; i < 3; i++) { - args[i] = (char *) calloc(1, sizeof(char *)); + args[i] = (char *)calloc (1, sizeof (char *)); } - static struct option long_options[] = { - {"format", required_argument, 0, 'f'}, - {"help", no_argument, 0, 'h'}, - {"max", required_argument, 0, 'm'}, - {"search", required_argument, 0, 's'}, - {"version", no_argument, 0, 'v'}, - {0, 0, 0, 0} - }; + static struct option long_options[] + = { { "format", required_argument, 0, 'f' }, + { "help", no_argument, 0, 'h' }, + { "max", required_argument, 0, 'm' }, + { "search", required_argument, 0, 's' }, + { "version", no_argument, 0, 'v' }, + { 0, 0, 0, 0 } }; - while ((o = getopt_long(argc, argv, "f:hm:s:v", long_options, NULL)) != -1) + while ((o = getopt_long (argc, argv, "f:hm:s:v", long_options, NULL)) != -1) { - switch(o) + switch (o) { case 'f': opts[0]++; - args[0] = (char *) realloc(args[0], strlen(optarg) + 1); - strcpy(args[0], optarg); + args[0] = (char *)realloc (args[0], strlen (optarg) + 1); + strcpy (args[0], optarg); break; case 'h': opts[1]++; break; case 'm': opts[2]++; - args[1] = (char *) realloc(args[1], strlen(optarg) + 1); - strcpy(args[1], optarg); + args[1] = (char *)realloc (args[1], strlen (optarg) + 1); + strcpy (args[1], optarg); break; case 's': opts[3]++; - args[2] = (char *) realloc(args[2], strlen(optarg) + 1); - strcpy(args[2], optarg); + args[2] = (char *)realloc (args[2], strlen (optarg) + 1); + strcpy (args[2], optarg); break; case 'v': opts[4]++; } } - processOptions(opts, args, argc); + processOptions (opts, args, argc); - for ( int i = 0; i < 3; i++ ) + for (int i = 0; i < 3; i++) { - free(args[i]); + free (args[i]); } - free(args); + free (args); return 0; } -void processOptions(int *options, char **args, int argc) +void +processOptions (int *options, char **args, int argc) { - if ( !options[1] && options[3] && !options[4] ) + if (!options[1] && options[3] && !options[4]) { - char *query = queryNormalizer(args[2]); - if ( query != NULL ) + char *query = queryNormalizer (args[2]); + if (query != NULL) { - char *link = (char *) calloc(strlen(SEARCHLINK) + strlen(query) + 1, sizeof(char)); - strcpy(link, SEARCHLINK); - strcat(link, query); + char *link = (char *)calloc ( + strlen (SEARCHLINK) + strlen (query) + 1, sizeof (char)); + strcpy (link, SEARCHLINK); + strcat (link, query); - char *json = extractQueryJSON(link); - free(link); - free(query); + char *json = extractQueryJSON (link); + free (link); + free (query); - if ( json != NULL && strlen(json) > 0 ) + if (json != NULL && strlen (json) > 0) { - json_object *queryRoot = jsonParseString(json); - free(json); - if ( queryRoot != NULL && json_object_get_type(queryRoot) != json_type_null ) + json_object *queryRoot = jsonParseString (json); + free (json); + if (queryRoot != NULL + && json_object_get_type (queryRoot) != json_type_null) { - json_object *handler = navigateToVideos(queryRoot); - int videoCount = videoCounter(handler); + json_object *handler = navigateToVideos (queryRoot); + int videoCount = videoCounter (handler); - if ( videoCount > 0 ) + if (videoCount > 0) { - Video *videos = generateVideos(handler); - json_object_put(queryRoot); - int printedVideos = ( options[2] && checkNumber(args[1]) > 0) ? checkNumber(args[1]) : videoCount; - char *format = ( options[0] ) ? args[0] : ""; - printVideoInfo(format, videos, printedVideos); + Video *videos = generateVideos (handler); + json_object_put (queryRoot); + int printedVideos + = (options[2] && checkNumber (args[1]) > 0) + ? checkNumber (args[1]) + : videoCount; + char *format = (options[0]) ? args[0] : ""; + printVideoInfo (format, videos, printedVideos); - for ( int i = 0; i < videoCount; i++ ) + for (int i = 0; i < videoCount; i++) { - freeVideo(&videos[i]); + freeVideo (&videos[i]); } - free(videos); + free (videos); } else { - json_object_put(queryRoot); + json_object_put (queryRoot); } } else { - json_object_put(queryRoot); + json_object_put (queryRoot); } } else { - free(json); + free (json); } - } } else { - if ( options[1] || (options[3] && options[4]) || argc == 1) + if (options[1] || (options[3] && options[4]) || argc == 1) { - printf("Usage: queryt -s [SEARCH]... [OPTIONS]...\n" + printf ( + "Usage: queryt -s [SEARCH]... [OPTIONS]...\n" "API Keyless YouTube querying tool.\n\n" - " -f, --format uses format modifiers to customize the output\n" + " -f, --format uses format modifiers to " + "customize the output\n" " a author;\n" " d duration;\n" " i video id;\n" @@ -138,21 +145,23 @@ void processOptions(int *options, char **args, int argc) " R return;\n" " T tab;\n" " -h, --help display this help and exit\n" - " -m, --max defines the max number of displayed results\n" + " -m, --max defines the max number of displayed " + "results\n" " -s, --search search content on youtube\n" - " -v, --version output version information and exit\n\n" + " -v, --version output version information and " + "exit\n\n" "Examples:\n" " queryt -s \"Never Gonna Give You Up\" -m 5 -f iTaTdRt\n\n"); } else { - if ( options[4] ) + if (options[4]) { - printf("queryt-%s\n", VERSION); + printf ("queryt-%s\n", VERSION); } else { - puts("-s is required!"); + puts ("-s is required!"); } } } diff --git a/src/utils.c b/src/utils.c index 45735ad..8600406 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,86 +1,91 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <ctype.h> #include "utils.h" +#include "../include/video.h" #include "../libs/curl.h" #include "../libs/string.h" -#include "../include/video.h" +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> -int tokenCount(char *string, char *delimiter) +int +tokenCount (char *string, char *delimiter) { int totalCount = 0; if (string != NULL && delimiter != NULL) { - char *auxString = (char *)calloc(strlen(string) + 1, sizeof(char)); - strcpy(auxString, string); + char *auxString = (char *)calloc (strlen (string) + 1, sizeof (char)); + strcpy (auxString, string); - char *token = strtok(auxString, delimiter); + char *token = strtok (auxString, delimiter); while (token != NULL) { totalCount++; - token = strtok(NULL, delimiter); + token = strtok (NULL, delimiter); } - free(auxString); + free (auxString); } return totalCount; } -char **tokenizer(char *string, char *delimiter) +char ** +tokenizer (char *string, char *delimiter) { char **tokens = NULL; if (string != NULL && delimiter != NULL) { - char *auxString = (char *)calloc(strlen(string) + 1, sizeof(char)); - strcpy(auxString, string); + char *auxString = (char *)calloc (strlen (string) + 1, sizeof (char)); + strcpy (auxString, string); - tokens = (char **)calloc(tokenCount(auxString, delimiter), sizeof(char *)); - char *tempToken = strtok(auxString, delimiter); + tokens = (char **)calloc (tokenCount (auxString, delimiter), + sizeof (char *)); + char *tempToken = strtok (auxString, delimiter); for (int i = 0; tempToken != NULL; i++) { - tokens[i] = (char *)calloc(strlen(tempToken) + 1, sizeof(char)); - strcpy(tokens[i], tempToken); - tempToken = strtok(NULL, delimiter); + tokens[i] = (char *)calloc (strlen (tempToken) + 1, sizeof (char)); + strcpy (tokens[i], tempToken); + tempToken = strtok (NULL, delimiter); } - free(auxString); + free (auxString); } else { - tokens = (char **)calloc(1, sizeof(char)); + tokens = (char **)calloc (1, sizeof (char)); } return tokens; } -void freeTokens(char **tokens, int size) +void +freeTokens (char **tokens, int size) { - if ( size > 0 ) + if (size > 0) { - for ( int i = 0; i < size; i++ ) + for (int i = 0; i < size; i++) { - free(tokens[i]); + free (tokens[i]); } } - free(tokens); + free (tokens); } -char *queryNormalizer(char *query) +char * +queryNormalizer (char *query) { char *norm = NULL; - if (query != NULL && strlen(query) > 0) + if (query != NULL && strlen (query) > 0) { // Declaring Initial Query Size - int querySize = strlen(query); + int querySize = strlen (query); // Calculating Normalized Query Size Allocation - for (int i = 0; i < strlen(query); i++) + for (int i = 0; i < strlen (query); i++) { if (query[i] == '+' || query[i] == '#' || query[i] == '&') { @@ -89,10 +94,10 @@ char *queryNormalizer(char *query) } // Allocating Memory - norm = (char *)calloc(querySize + 1, sizeof(char)); + norm = (char *)calloc (querySize + 1, sizeof (char)); // Creating New Normalized Query - for (int i = 0, added = 0; i < strlen(query); i++) + for (int i = 0, added = 0; i < strlen (query); i++) { switch (query[i]) { @@ -138,105 +143,115 @@ char *queryNormalizer(char *query) return norm; } -char* extractQueryJSON(char *youtubeurl) +char * +extractQueryJSON (char *youtubeurl) { char *json = NULL; - if (youtubeurl != NULL && strlen(youtubeurl) > 0) + if (youtubeurl != NULL && strlen (youtubeurl) > 0) { - char *htmlPage = downloadPage(youtubeurl); - if ( htmlPage != NULL && strlen(htmlPage) > 0 ) + char *htmlPage = downloadPage (youtubeurl); + if (htmlPage != NULL && strlen (htmlPage) > 0) { // Setting Up Vars - char jsonVar[] = "ytInitialData", needlessHTML[] = ";</script><script", **tokens = tokenizer(htmlPage, " "); - int numberOfTokens = tokenCount(htmlPage, " "); - json = (char *)calloc(1, sizeof(char)); + char jsonVar[] = "ytInitialData", + needlessHTML[] = ";</script><script", + **tokens = tokenizer (htmlPage, " "); + int numberOfTokens = tokenCount (htmlPage, " "); + json = (char *)calloc (1, sizeof (char)); // Extracting JSON String - for (int i = 0, j = 1; i < numberOfTokens && strstr(tokens[j-1], needlessHTML) == NULL; i++) + for (int i = 0, j = 1; + i < numberOfTokens + && strstr (tokens[j - 1], needlessHTML) == NULL; + i++) { - if ( strcmp(tokens[i], jsonVar) == 0 ) + if (strcmp (tokens[i], jsonVar) == 0) { - for ( j = i + 2; strstr(tokens[j-1], needlessHTML) == NULL; j++ ) + for (j = i + 2; strstr (tokens[j - 1], needlessHTML) == NULL; + j++) { - json = realloc(json, (strlen(json) + strlen(tokens[j]) + 2)); - strcat(json, tokens[j]); - strcat(json, " "); + json = realloc ( + json, (strlen (json) + strlen (tokens[j]) + 2)); + strcat (json, tokens[j]); + strcat (json, " "); } } } - json[strlen(json) - strlen(needlessHTML)] = '\0'; - freeTokens(tokens, numberOfTokens); + json[strlen (json) - strlen (needlessHTML)] = '\0'; + freeTokens (tokens, numberOfTokens); } - free(htmlPage); + free (htmlPage); } return json; } -int checkNumber(char *num) +int +checkNumber (char *num) { int number = 0, charCheck = 1; - if( num != NULL && strlen(num) > 0 && strlen(num) < 3 ) + if (num != NULL && strlen (num) > 0 && strlen (num) < 3) { - for( int i = 0; i < strlen(num) && charCheck; i++ ) + for (int i = 0; i < strlen (num) && charCheck; i++) { - if( !isdigit(num[i]) ) + if (!isdigit (num[i])) { charCheck--; } } - number = ( charCheck ) ? atoi(num) : 0; + number = (charCheck) ? atoi (num) : 0; } return number; } -void printVideoInfo(char *format, Video *videos, int numberOfVideos) +void +printVideoInfo (char *format, Video *videos, int numberOfVideos) { - if ( format != NULL && strlen(format) > 0 ) + if (format != NULL && strlen (format) > 0) { - for ( int i = 0; i < numberOfVideos; i++ ) + for (int i = 0; i < numberOfVideos; i++) { - for ( int j = 0; j < strlen(format); j++ ) + for (int j = 0; j < strlen (format); j++) { - switch(format[j]) + switch (format[j]) { case 'a': - printf("%s", videos[i].author); + printf ("%s", videos[i].author); break; case 'd': - printf("%s", videos[i].duration); + printf ("%s", videos[i].duration); break; case 'i': - printf("%s", videos[i].id); + printf ("%s", videos[i].id); break; case 't': - printf("%s", videos[i].title); + printf ("%s", videos[i].title); break; case 'R': - printf("\n"); + printf ("\n"); break; case 'T': - printf("\t"); + printf ("\t"); break; default: - if( !isalpha(format[j]) ) + if (!isalpha (format[j])) { - printf("%c", format[j]); + printf ("%c", format[j]); } } } - printf("\n"); + printf ("\n"); } } else { - for( int i = 0; i < numberOfVideos; i++ ) + for (int i = 0; i < numberOfVideos; i++) { - printf("%s\t%s\n", videos[i].id, videos[i].title); + printf ("%s\t%s\n", videos[i].id, videos[i].title); } } } diff --git a/src/video.c b/src/video.c index f24476b..0e09437 100644 --- a/src/video.c +++ b/src/video.c @@ -1,44 +1,45 @@ +#include "../include/video.h" #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "../include/video.h" -Video createVideo(char *title, char *author, char *id, char *duration) +Video +createVideo (char *title, char *author, char *id, char *duration) { Video video; - if (title != NULL && strlen(title) > 0) + if (title != NULL && strlen (title) > 0) { - video.title = (char *) calloc(strlen(title) + 1, sizeof(char)); - strcpy(video.title, title); + video.title = (char *)calloc (strlen (title) + 1, sizeof (char)); + strcpy (video.title, title); } - if (author != NULL && strlen(author) > 0) + if (author != NULL && strlen (author) > 0) { - video.author = (char *) calloc(strlen(author) + 1, sizeof(char)); - strcpy(video.author, author); + video.author = (char *)calloc (strlen (author) + 1, sizeof (char)); + strcpy (video.author, author); } - if (id != NULL && strlen(id) > 0) + if (id != NULL && strlen (id) > 0) { - video.id = (char *) calloc(strlen(id) + 1, sizeof(char)); - strcpy(video.id, id); + video.id = (char *)calloc (strlen (id) + 1, sizeof (char)); + strcpy (video.id, id); } - if (duration != NULL && strlen(duration) > 0) + if (duration != NULL && strlen (duration) > 0) { - video.duration = (char *) calloc(strlen(duration) + 1, sizeof(char)); - strcpy(video.duration, duration); + video.duration = (char *)calloc (strlen (duration) + 1, sizeof (char)); + strcpy (video.duration, duration); } - return video; } -void freeVideo(Video *v) +void +freeVideo (Video *v) { - free(v->title); - free(v->author); - free(v->id); - free(v->duration); + free (v->title); + free (v->author); + free (v->id); + free (v->duration); } |