summaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authorrafa_99 <raroma09@gmail.com>2021-09-05 14:34:27 +0100
committerrafa_99 <raroma09@gmail.com>2021-09-05 14:34:27 +0100
commit694389dace8f39ac5add7f9b4305a172c6995fdf (patch)
treee479048c88405f5f7a26b025c928cf7e944c07fa /libs
parentf033fc79eab50c183fbd9e9e7d1c9ca342b7bd96 (diff)
Formatted Code using clang-format with GNU standard
Diffstat (limited to 'libs')
-rw-r--r--libs/curl.c42
-rw-r--r--libs/curl.h2
-rw-r--r--libs/json.c120
-rw-r--r--libs/json.h13
-rw-r--r--libs/string.c20
-rw-r--r--libs/string.h8
6 files changed, 106 insertions, 99 deletions
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);