From ed9ad979be88a230eb873e8914a2425f84f09b06 Mon Sep 17 00:00:00 2001 From: rafa_99 Date: Sat, 4 Sep 2021 17:50:20 +0100 Subject: Updated Main and Some Memory Errors --- include/utils.h | 3 +++ queryt.c | 28 ++++++++++++++++++++++--- src/utils.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/video.c | 13 +++++------- 4 files changed, 96 insertions(+), 11 deletions(-) diff --git a/include/utils.h b/include/utils.h index 987b7fe..4444279 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,7 +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); diff --git a/queryt.c b/queryt.c index a91f234..8f198b0 100644 --- a/queryt.c +++ b/queryt.c @@ -5,7 +5,7 @@ #include #include "include/utils.h" #include "include/video.h" -#include "libs/curl.h" +#include "libs/json.h" void processOptions(int *options, char **args); @@ -82,14 +82,36 @@ void processOptions(int *options, char **args) if ( json != NULL && strlen(json) > 0 ) { - json_object *queryRoot = json_tokener_parse(json), *handler; + 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); + + 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); + + for ( int i = 0; i < videoCount; i++ ) + { + freeVideo(&videos[i]); + } + + free(videos); + } + else + { + json_object_put(queryRoot); + } } else { - json_object_put(queryRoot); + json_object_put(queryRoot); } } else diff --git a/src/utils.c b/src/utils.c index a76e8d9..db8eaf3 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,9 +1,11 @@ #include #include #include +#include #include "utils.h" #include "../libs/curl.h" #include "../libs/string.h" +#include "../include/video.h" int tokenCount(char *string, char *delimiter) { @@ -171,3 +173,64 @@ char* extractQueryJSON(char *youtubeurl) } return json; } + +int checkNumber(char *num) +{ + int number = 0, charCheck = 1; + + if( num != NULL && strlen(num) > 0 && strlen(num) < 3 ) + { + for( int i = 0; i < strlen(num) && charCheck; i++ ) + { + if( !isdigit(num[i]) ) + { + charCheck--; + } + } + + number = ( charCheck ) ? atoi(num) : 0; + } + + return number; +} + +void printVideoInfo(char *format, Video *videos, int numberOfVideos) +{ + if ( format != NULL && strlen(format) > 0 ) + { + for ( int i = 0; i < numberOfVideos; i++ ) + { + for ( int j = 0; j < strlen(format); j++ ) + { + switch(format[j]) + { + case 'a': + printf("%s", videos[i].author); + break; + case 'd': + printf("%s", videos[i].duration); + break; + case 'i': + printf("%s", videos[i].id); + break; + case 't': + printf("%s", videos[i].title); + break; + default: + if( !isalpha(format[j]) ) + { + printf("%c", format[j]); + } + } + } + printf("\n"); + } + } + else + { + for( int i = 0; i < numberOfVideos; i++ ) + { + printf("%s - %s %s %s\n", videos[i].title, videos[i].author, videos[i].duration, videos[i].id); + } + } +} diff --git a/src/video.c b/src/video.c index 9f29ed9..f24476b 100644 --- a/src/video.c +++ b/src/video.c @@ -9,31 +9,28 @@ Video createVideo(char *title, char *author, char *id, char *duration) if (title != NULL && strlen(title) > 0) { - video.title = (char *) calloc(TITLE + 1, sizeof(char)); + video.title = (char *) calloc(strlen(title) + 1, sizeof(char)); strcpy(video.title, title); } if (author != NULL && strlen(author) > 0) { - video.author = (char *) calloc(AUTHOR + 1, sizeof(char)); + video.author = (char *) calloc(strlen(author) + 1, sizeof(char)); strcpy(video.author, author); } if (id != NULL && strlen(id) > 0) { - video.id = (char *) calloc(VIDEOID + 1, sizeof(char)); + video.id = (char *) calloc(strlen(id) + 1, sizeof(char)); strcpy(video.id, id); } if (duration != NULL && strlen(duration) > 0) { - video.duration = (char *) calloc(DURATION + 1, sizeof(char)); + video.duration = (char *) calloc(strlen(duration) + 1, sizeof(char)); strcpy(video.duration, duration); } - else - { - video.duration = (char *) calloc(1, sizeof(char)); - } + return video; } -- cgit v1.2.3