From 93395705c40265c1893efd550b0f8404158eb86c Mon Sep 17 00:00:00 2001 From: Mole Shang <135e2@135e2.dev> Date: Mon, 7 Aug 2023 15:36:19 +0800 Subject: utils: move to a separate folder --- src/extractors/bilibili.c | 2 +- src/extractors/bilibili.h | 2 +- src/process_url.c | 2 +- src/process_url.h | 2 +- src/utils.c | 216 ---------------------------------------------- src/utils.h | 63 -------------- src/utils/utils.c | 216 ++++++++++++++++++++++++++++++++++++++++++++++ src/utils/utils.h | 63 ++++++++++++++ 8 files changed, 283 insertions(+), 283 deletions(-) delete mode 100644 src/utils.c delete mode 100644 src/utils.h create mode 100644 src/utils/utils.c create mode 100644 src/utils/utils.h (limited to 'src') diff --git a/src/extractors/bilibili.c b/src/extractors/bilibili.c index 4748409..6520a59 100644 --- a/src/extractors/bilibili.c +++ b/src/extractors/bilibili.c @@ -12,7 +12,7 @@ #include "../logger.h" #include "../process_url.h" -#include "../utils.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 5d609b0..890ddab 100644 --- a/src/extractors/bilibili.h +++ b/src/extractors/bilibili.h @@ -1,7 +1,7 @@ #ifndef BILIBILI_H_ #define BILIBILI_H_ -#include "../utils.h" +#include "../utils/utils.h" #include "extractor.h" #include diff --git a/src/process_url.c b/src/process_url.c index 881375f..f99c717 100644 --- a/src/process_url.c +++ b/src/process_url.c @@ -1,4 +1,4 @@ -#include "utils.h" +#include "utils/utils.h" #include #include #include diff --git a/src/process_url.h b/src/process_url.h index de81729..25caef6 100644 --- a/src/process_url.h +++ b/src/process_url.h @@ -6,7 +6,7 @@ #include #include "constants.h" -#include "utils.h" +#include "utils/utils.h" #define ERRTOSTRING(err) curl_easy_strerror(err) #define logerr(X) \ diff --git a/src/utils.c b/src/utils.c deleted file mode 100644 index ee41735..0000000 --- a/src/utils.c +++ /dev/null @@ -1,216 +0,0 @@ -#include -#include -#include -#include -#include - -#include "logger.h" -#include "utils.h" - -int regex_match(const char *subject, str_array_t patterns, - str_array_t *results) { - pcre2_code *re; - - int errornumber; - int rc = PCRE2_ERROR_NOMATCH; - - PCRE2_SIZE subject_len = strlen(subject); - PCRE2_SIZE offset = 0, erroroffset; - PCRE2_SIZE *ovector; - - pcre2_match_data *match_data; - for (unsigned short i = 0; i < patterns.n; i++) { - DEBUG_PRINT("Gets pattern: %s\n", patterns.str[i]); - re = pcre2_compile((PCRE2_SPTR)patterns.str[i], PCRE2_ZERO_TERMINATED, 0, - &errornumber, &erroroffset, NULL); - - if (re == NULL) { - PCRE2_UCHAR buffer[256]; - pcre2_get_error_message(errornumber, buffer, sizeof(buffer)); - LOG("PCRE2", "compilation failed at offset %d: %s\n", (int)erroroffset, - buffer); - return 1; - } - - match_data = pcre2_match_data_create_from_pattern(re, NULL); - - unsigned char i = 0; - while (offset < subject_len && - (rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)subject_len, - offset, 0, match_data, NULL)) > 0) { - - // results->str = realloc(results->str, sizeof(char *) * (rc + - // results->n)); - resize_str_array(results, rc + i); - - ovector = pcre2_get_ovector_pointer(match_data); - DEBUG_PRINT("Get %d captures.\n", rc - 1); - DEBUG_PRINT("Match succeeded at offset %d.\n", (int)ovector[0]); - - for (unsigned short j = 1; j < rc; j++) { - PCRE2_SIZE substring_length = ovector[2 * j + 1] - ovector[2 * j]; - PCRE2_SPTR substring = (PCRE2_SPTR)subject + ovector[2 * j]; - /* Here we need to manually control the str array, - * as PCRE2_SPTR == const unsigned char - * (which cannot be directly casted) */ - results->str[j] = malloc(substring_length + 1); - sprintf(results->str[j], "%.*s", (int)substring_length, substring); - DEBUG_PRINT("index: %2d, substring_length: %d\n", j, - (int)substring_length); - } - offset = ovector[1]; - DEBUG_PRINT("offset: %zu, subject_len: %zu\n", offset, subject_len); - i++; - } - pcre2_match_data_free(match_data); - pcre2_code_free(re); - if (rc <= 0) { - - switch (rc) { - case PCRE2_ERROR_NOMATCH: - DEBUG_PRINT("No match found.\n"); - return 0; - break; - case 0: - LOG("PCRE2", - "ovector was not big enough for all the captured substrings\n"); - break; - default: - LOG("PCRE2", "Matching error %d\n", rc); - return 1; - } - } - } - return 0; -} - -int repchr(char *str, char t, char r) { - int c = 0; - for (size_t i = 0; str[i] != '\0'; i++) { - if (str[i] == t) { - str[i] = r; - c++; - } - } - 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) { - free(p); - p = NULL; - } -} diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index 79c58ca..0000000 --- a/src/utils.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef UTILS_H_ -#define UTILS_H_ - -#include - -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; - -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 diff --git a/src/utils/utils.c b/src/utils/utils.c new file mode 100644 index 0000000..475f5e0 --- /dev/null +++ b/src/utils/utils.c @@ -0,0 +1,216 @@ +#include +#include +#include +#include +#include + +#include "../logger.h" +#include "utils.h" + +int regex_match(const char *subject, str_array_t patterns, + str_array_t *results) { + pcre2_code *re; + + int errornumber; + int rc = PCRE2_ERROR_NOMATCH; + + PCRE2_SIZE subject_len = strlen(subject); + PCRE2_SIZE offset = 0, erroroffset; + PCRE2_SIZE *ovector; + + pcre2_match_data *match_data; + for (unsigned short i = 0; i < patterns.n; i++) { + DEBUG_PRINT("Gets pattern: %s\n", patterns.str[i]); + re = pcre2_compile((PCRE2_SPTR)patterns.str[i], PCRE2_ZERO_TERMINATED, 0, + &errornumber, &erroroffset, NULL); + + if (re == NULL) { + PCRE2_UCHAR buffer[256]; + pcre2_get_error_message(errornumber, buffer, sizeof(buffer)); + LOG("PCRE2", "compilation failed at offset %d: %s\n", (int)erroroffset, + buffer); + return 1; + } + + match_data = pcre2_match_data_create_from_pattern(re, NULL); + + unsigned char i = 0; + while (offset < subject_len && + (rc = pcre2_match(re, (PCRE2_SPTR)subject, (PCRE2_SIZE)subject_len, + offset, 0, match_data, NULL)) > 0) { + + // results->str = realloc(results->str, sizeof(char *) * (rc + + // results->n)); + resize_str_array(results, rc + i); + + ovector = pcre2_get_ovector_pointer(match_data); + DEBUG_PRINT("Get %d captures.\n", rc - 1); + DEBUG_PRINT("Match succeeded at offset %d.\n", (int)ovector[0]); + + for (unsigned short j = 1; j < rc; j++) { + PCRE2_SIZE substring_length = ovector[2 * j + 1] - ovector[2 * j]; + PCRE2_SPTR substring = (PCRE2_SPTR)subject + ovector[2 * j]; + /* Here we need to manually control the str array, + * as PCRE2_SPTR == const unsigned char + * (which cannot be directly casted) */ + results->str[j] = malloc(substring_length + 1); + sprintf(results->str[j], "%.*s", (int)substring_length, substring); + DEBUG_PRINT("index: %2d, substring_length: %d\n", j, + (int)substring_length); + } + offset = ovector[1]; + DEBUG_PRINT("offset: %zu, subject_len: %zu\n", offset, subject_len); + i++; + } + pcre2_match_data_free(match_data); + pcre2_code_free(re); + if (rc <= 0) { + + switch (rc) { + case PCRE2_ERROR_NOMATCH: + DEBUG_PRINT("No match found.\n"); + return 0; + break; + case 0: + LOG("PCRE2", + "ovector was not big enough for all the captured substrings\n"); + break; + default: + LOG("PCRE2", "Matching error %d\n", rc); + return 1; + } + } + } + return 0; +} + +int repchr(char *str, char t, char r) { + int c = 0; + for (size_t i = 0; str[i] != '\0'; i++) { + if (str[i] == t) { + str[i] = r; + c++; + } + } + 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) { + free(p); + p = NULL; + } +} diff --git a/src/utils/utils.h b/src/utils/utils.h new file mode 100644 index 0000000..79c58ca --- /dev/null +++ b/src/utils/utils.h @@ -0,0 +1,63 @@ +#ifndef UTILS_H_ +#define UTILS_H_ + +#include + +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; + +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 -- cgit v1.2.3