diff options
-rw-r--r-- | src/extractors/bilibili.c | 1 | ||||
-rw-r--r-- | src/extractors/bilibili.h | 5 | ||||
-rw-r--r-- | src/process_url.c | 3 | ||||
-rw-r--r-- | src/utils/types.c | 120 | ||||
-rw-r--r-- | src/utils/types.h | 56 | ||||
-rw-r--r-- | src/utils/utils.c | 112 | ||||
-rw-r--r-- | src/utils/utils.h | 52 |
7 files changed, 182 insertions, 167 deletions
diff --git a/src/extractors/bilibili.c b/src/extractors/bilibili.c index 6520a59..7eaa0bc 100644 --- a/src/extractors/bilibili.c +++ b/src/extractors/bilibili.c @@ -12,7 +12,6 @@ #include "../logger.h" #include "../process_url.h" -#include "../utils/utils.h" #include "../utils/ffmpeg.h" #include "bilibili.h" #include "extractor.h" diff --git a/src/extractors/bilibili.h b/src/extractors/bilibili.h index 890ddab..2a21707 100644 --- a/src/extractors/bilibili.h +++ b/src/extractors/bilibili.h @@ -1,10 +1,11 @@ #ifndef BILIBILI_H_ #define BILIBILI_H_ -#include "../utils/utils.h" -#include "extractor.h" #include <stddef.h> +#include "../utils/types.h" +#include "extractor.h" + #define BILIBILI_API "https://api.bilibili.com/x/player/playurl?" #define BILIBILI_BANGUMI_API "https://api.bilibili.com/pgc/player/web/playurl?" #define BILIBILI_TOKEN_API "https://api.bilibili.com/x/player/playurl/token?" diff --git a/src/process_url.c b/src/process_url.c index f99c717..bba3c53 100644 --- a/src/process_url.c +++ b/src/process_url.c @@ -1,4 +1,3 @@ -#include "utils/utils.h" #include <curl/curl.h> #include <curl/easy.h> #include <curl/header.h> @@ -21,6 +20,8 @@ #include "extractors/extractor.h" #include "logger.h" #include "process_url.h" +#include "utils/utils.h" +#include "utils/types.h" /* NOTICE: the global curl_conf pointer will only stay valid during downloading, * otherwise, ALWAYS point it to NULL. */ diff --git a/src/utils/types.c b/src/utils/types.c new file mode 100644 index 0000000..ebedd0e --- /dev/null +++ b/src/utils/types.c @@ -0,0 +1,120 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "../logger.h" +#include "types.h" +#include "utils.h" + +generic_array_t create_array(size_t elem_size, size_t n) { + generic_array_t array; + array.data = n ? malloc(elem_size * n) : NULL; + array.elem_size = elem_size; + array.n = n; + return array; +} + +void free_array(generic_array_t *array) { + free_and_nullify(array->data); + array->n = 0; +} + +void resize_array(generic_array_t *array, size_t new_size) { + array->data = realloc(array->data, array->elem_size * new_size); + array->n = new_size; +} + +void *get_element(generic_array_t *array, size_t index) { + if (index >= array->n) { + return NULL; // Out of bounds + } + return (char *)array->data + index * array->elem_size; +} + +/* A more specific impl, specially for string (char *) */ + +str_array_t create_str_array(size_t n) { + str_array_t array; + array.str = n ? malloc(n * sizeof(char *)) : NULL; + array.n = n; + for (size_t i = 0; i < n; i++) { + array.str[i] = NULL; + } + return array; +} + +void free_str_array(str_array_t *array) { + for (size_t i = 0; i < array->n; i++) { + free(array->str[i]); + } + free_and_nullify(array->str); + array->n = 0; +} + +void resize_str_array(str_array_t *array, size_t new_size) { + array->str = realloc(array->str, sizeof(char *) * new_size); + for (size_t i = array->n; i < new_size; i++) { + array->str[i] = NULL; + } + array->n = new_size; +} + +int set_str_element(str_array_t *array, size_t index, const char *value) { + if (index >= array->n) { + return 1; // Out of bounds + } + array->str[index] = malloc(strlen(value) + 1); + strcpy(array->str[index], value); + return 0; +} + +const char *get_str_element(str_array_t *array, size_t index) { + if (index >= array->n) { + return NULL; // Out of bounds + } + return array->str[index]; +} + +queue_t create_queue(void) { + queue_t queue; + queue.front = queue.rear = NULL; + return queue; +} + +int is_empty_queue(queue_t *queue) { return queue->front == NULL; } + +void enqueue(queue_t *queue, data_t data) { + node_t *node = malloc(sizeof(node_t)); + node->data = data; + node->next = NULL; + if (queue->rear == NULL) { + queue->rear = queue->front = node; + } else { + queue->rear->next = node; + queue->rear = node; + } +} + +data_t dequeue(queue_t *queue) { + if (is_empty_queue(queue)) { + DEBUG_PRINT("Queue is empty.\n"); + return NULL; + } + + node_t *temp = queue->front; + data_t data = temp->data; + queue->front = temp->next; + free_and_nullify(temp); + + if (queue->front == NULL) { + queue->rear = NULL; + } + + return data; +} + +void free_queue(queue_t *queue) { + while (!is_empty_queue(queue)) { + dequeue(queue); + } +} diff --git a/src/utils/types.h b/src/utils/types.h new file mode 100644 index 0000000..b70e6d4 --- /dev/null +++ b/src/utils/types.h @@ -0,0 +1,56 @@ +#ifndef TYPES_H_ +#define TYPES_H_ + +#include <stddef.h> +typedef void *data_t; + +typedef struct str_array { + char **str; + size_t n; +} str_array_t; + +typedef struct generic_array { + void *data; + size_t elem_size; + size_t n; +} generic_array_t; + +typedef struct node { + data_t data; + struct node *next; +} node_t; + +typedef struct queue { + node_t *front; + node_t *rear; +} queue_t; + +generic_array_t create_array(size_t elem_size, size_t n); + +void free_array(generic_array_t *array); + +void resize_array(generic_array_t *array, size_t new_size); + +void *get_element(generic_array_t *array, size_t index); + +str_array_t create_str_array(size_t n); + +void free_str_array(str_array_t *array); + +void resize_str_array(str_array_t *array, size_t new_size); + +int set_str_element(str_array_t *array, size_t index, const char *value); + +const char *get_str_element(str_array_t *array, size_t index); + +queue_t create_queue(void); + +int is_empty_queue(queue_t *queue); + +void enqueue(queue_t *queue, data_t data); + +data_t dequeue(queue_t *queue); + +void free_queue(queue_t *queue); + +#endif diff --git a/src/utils/utils.c b/src/utils/utils.c index 475f5e0..16d39d9 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -95,118 +95,6 @@ int repchr(char *str, char t, char r) { return c; } -generic_array_t create_array(size_t elem_size, size_t n) { - generic_array_t array; - array.data = n ? malloc(elem_size * n) : NULL; - array.elem_size = elem_size; - array.n = n; - return array; -} - -void free_array(generic_array_t *array) { - free_and_nullify(array->data); - array->n = 0; -} - -void resize_array(generic_array_t *array, size_t new_size) { - array->data = realloc(array->data, array->elem_size * new_size); - array->n = new_size; -} - -void *get_element(generic_array_t *array, size_t index) { - if (index >= array->n) { - return NULL; // Out of bounds - } - return (char *)array->data + index * array->elem_size; -} - -/* A more specific impl, specially for string (char *) */ - -str_array_t create_str_array(size_t n) { - str_array_t array; - array.str = n ? malloc(n * sizeof(char *)) : NULL; - array.n = n; - for (size_t i = 0; i < n; i++) { - array.str[i] = NULL; - } - return array; -} - -void free_str_array(str_array_t *array) { - for (size_t i = 0; i < array->n; i++) { - free(array->str[i]); - } - free_and_nullify(array->str); - array->n = 0; -} - -void resize_str_array(str_array_t *array, size_t new_size) { - array->str = realloc(array->str, sizeof(char *) * new_size); - for (size_t i = array->n; i < new_size; i++) { - array->str[i] = NULL; - } - array->n = new_size; -} - -int set_str_element(str_array_t *array, size_t index, const char *value) { - if (index >= array->n) { - return 1; // Out of bounds - } - array->str[index] = malloc(strlen(value) + 1); - strcpy(array->str[index], value); - return 0; -} - -const char *get_str_element(str_array_t *array, size_t index) { - if (index >= array->n) { - return NULL; // Out of bounds - } - return array->str[index]; -} - -queue_t create_queue(void) { - queue_t queue; - queue.front = queue.rear = NULL; - return queue; -} - -int is_empty_queue(queue_t *queue) { return queue->front == NULL; } - -void enqueue(queue_t *queue, data_t data) { - node_t *node = malloc(sizeof(node_t)); - node->data = data; - node->next = NULL; - if (queue->rear == NULL) { - queue->rear = queue->front = node; - } else { - queue->rear->next = node; - queue->rear = node; - } -} - -data_t dequeue(queue_t *queue) { - if (is_empty_queue(queue)) { - DEBUG_PRINT("Queue is empty.\n"); - return NULL; - } - - node_t *temp = queue->front; - data_t data = temp->data; - queue->front = temp->next; - free_and_nullify(temp); - - if (queue->front == NULL) { - queue->rear = NULL; - } - - return data; -} - -void free_queue(queue_t *queue) { - while (!is_empty_queue(queue)) { - dequeue(queue); - } -} void free_and_nullify(void *p) { if (p) { diff --git a/src/utils/utils.h b/src/utils/utils.h index 79c58ca..d7e45dc 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -2,62 +2,12 @@ #define UTILS_H_ #include <stddef.h> - -typedef void *data_t; - -typedef struct str_array { - char **str; - size_t n; -} str_array_t; - -typedef struct generic_array { - void *data; - size_t elem_size; - size_t n; -} generic_array_t; - -typedef struct node { - data_t data; - struct node *next; -} node_t; - -typedef struct queue { - node_t *front; - node_t *rear; -} queue_t; +#include "types.h" int regex_match(const char *, str_array_t, str_array_t *); int repchr(char *str, char t, char r); -generic_array_t create_array(size_t elem_size, size_t n); - -void free_array(generic_array_t *array); - -void resize_array(generic_array_t *array, size_t new_size); - -void *get_element(generic_array_t *array, size_t index); - void free_and_nullify(void *p); -str_array_t create_str_array(size_t n); - -void free_str_array(str_array_t *array); - -void resize_str_array(str_array_t *array, size_t new_size); - -int set_str_element(str_array_t *array, size_t index, const char *value); - -const char *get_str_element(str_array_t *array, size_t index); - -queue_t create_queue(void); - -int is_empty_queue(queue_t *queue); - -void enqueue(queue_t *queue, data_t data); - -data_t dequeue(queue_t *queue); - -void free_queue(queue_t *queue); - #endif |