From c4c5511654ae428984f713f030f333544b68fc19 Mon Sep 17 00:00:00 2001 From: k1rch Date: Thu, 4 May 2023 01:45:02 +0700 Subject: [PATCH 1/2] =?UTF-8?q?feature:=20#1=20=D0=B7=D0=B0=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=91=D0=BD=20=D0=BF=D0=BE=D0=BB=D0=BD=D1=8B=D0=B9=20?= =?UTF-8?q?=D0=BE=D0=B1=D1=85=D0=BE=D0=B4=20=D0=B2=D1=81=D0=B5=D1=85=20?= =?UTF-8?q?=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=BE=D0=B2=20=D1=85?= =?UTF-8?q?=D0=B5=D1=88-=D1=82=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helpers/helpers_common.c | 30 ++++++++++++---------------- helpers/helpers_hash_table.c | 23 ++++++++++++++++----- helpers/helpers_logger.c | 2 +- include/helpers/helpers_hash_table.h | 10 +++++++--- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/helpers/helpers_common.c b/helpers/helpers_common.c index 000a2b7..2b1fcce 100644 --- a/helpers/helpers_common.c +++ b/helpers/helpers_common.c @@ -310,24 +310,20 @@ static void handle_event(queue_entry_t event, thread_argument_t * arg) return; } DIR * thread_directory = NULL; - for (int i = 0; i < arg->hash_table->size; i++) + + hash_item_t * hi = arg->hash_table->node; + while (hi != NULL) { - if (NULL != hash_table_search_item(arg->hash_table, i, NULL)) + thread_directory = opendir(hi->value); + if (thread_directory && !backup_helpers_find_file(cur_event_filename, thread_directory)) { - thread_directory = opendir(arg->hash_table->table[i]->value); - if (NULL != thread_directory) - { - if (false == backup_helpers_find_file(cur_event_filename, thread_directory)) - { - continue; - } - else - { - // path to file founded successfully, refresh - arg->path_to_dir = arg->hash_table->table[i]->value; - break; - } - } + hi = hi->previous; + continue; + } + else if (thread_directory) + { + arg->path_to_dir = hi->value; + break; } } cur_event_file_or_dir = HELPERS_FILE; @@ -609,4 +605,4 @@ char * parser_read_conf() while (fread(buffer, sizeof(char), HELPERS_FILE_STROKE_MAX_LEN * lines, cp)); return buffer; -} \ No newline at end of file +} diff --git a/helpers/helpers_hash_table.c b/helpers/helpers_hash_table.c index bedd3f8..483759c 100644 --- a/helpers/helpers_hash_table.c +++ b/helpers/helpers_hash_table.c @@ -33,11 +33,11 @@ static hash_item_t * __hash_item_create(char * value) hash_item_t * hash_item = (hash_item_t *)malloc(sizeof(hash_item_t)); memset(hash_item, 0, sizeof(hash_item_t)); - hash_item->value = (char *)malloc(sizeof(char) * value_len); + hash_item->value = (char *)malloc(sizeof(char) * value_len); memset(hash_item->value, 0, sizeof(char) * value_len); strncpy(hash_item->value, value, value_len); - hash_t key = __hash_key_generator(value); + hash_t key = __hash_key_generator(value); if (key >= 0 && key <= 1.1) { printf("Error: This path to file is too big !\n"); @@ -45,7 +45,9 @@ static hash_item_t * __hash_item_create(char * value) } else { - hash_item->key = key; + hash_item->key = key; + hash_item->previous = NULL; + return hash_item; } } @@ -62,6 +64,7 @@ hash_table_t * hash_table_create(hash_table_size_t size) { this->table[i] = NULL; } + this->node = NULL; return this; } @@ -118,9 +121,19 @@ unsigned int hash_table_insert_item(hash_table_t * this, char * value) } hash_item_t ** head = &this->table[key]; - if (!(*head)) + if (head && !(*head)) { *head = item; + if (NULL == this->node) + { + this->node = (void *)*head; + } + else + { + item->previous = this->node; + this->node = (void *)item; + } + ++this->count; } else if ((*head)->key == key) @@ -175,4 +188,4 @@ int test1() printf("[1] = %d:%s\n", HI->key, HI->value); hash_table_free(HT); } -#endif \ No newline at end of file +#endif diff --git a/helpers/helpers_logger.c b/helpers/helpers_logger.c index e9531e0..14953b2 100644 --- a/helpers/helpers_logger.c +++ b/helpers/helpers_logger.c @@ -31,4 +31,4 @@ int logger(char * message) fclose(fptr); return LOGGER_NORMAL_EXIT; -} \ No newline at end of file +} diff --git a/include/helpers/helpers_hash_table.h b/include/helpers/helpers_hash_table.h index aa23407..1708824 100644 --- a/include/helpers/helpers_hash_table.h +++ b/include/helpers/helpers_hash_table.h @@ -16,13 +16,17 @@ typedef enum typedef struct hash_item_s { - hash_t key; - char * value; + hash_t key; + char * value; + + void * previous; } hash_item_t; typedef struct hash_table_s { hash_item_t ** table; + hash_item_t * node; + hash_table_size_t size; int count; } hash_table_t; @@ -32,4 +36,4 @@ unsigned int hash_table_insert_item(hash_table_t * this, char * value); hash_table_t * hash_table_create(hash_table_size_t size); void hash_table_free(hash_table_t * this); -#endif // HELPERS_HASH_TABLE_H \ No newline at end of file +#endif // HELPERS_HASH_TABLE_H From 2466ed1cf4f0765afeadb32b4312107053ce70f1 Mon Sep 17 00:00:00 2001 From: k1rch Date: Fri, 5 May 2023 01:51:03 +0700 Subject: [PATCH 2/2] =?UTF-8?q?enhancement:=20#2=20=D0=B8=D0=BD=D0=B8?= =?UTF-8?q?=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8F=20?= =?UTF-8?q?=D1=81=D0=B2=D1=8F=D0=B7=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=81=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D0=BA=D0=B0=20'=D0=B2=D0=BF=D0=B5=D1=80=D1=91?= =?UTF-8?q?=D0=B4'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helpers/helpers_common.c | 2 +- helpers/helpers_hash_table.c | 8 +++++--- include/helpers/helpers_hash_table.h | 1 + 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/helpers/helpers_common.c b/helpers/helpers_common.c index 2b1fcce..a4d66f1 100644 --- a/helpers/helpers_common.c +++ b/helpers/helpers_common.c @@ -316,7 +316,7 @@ static void handle_event(queue_entry_t event, thread_argument_t * arg) { thread_directory = opendir(hi->value); if (thread_directory && !backup_helpers_find_file(cur_event_filename, thread_directory)) - { + { hi = hi->previous; continue; } diff --git a/helpers/helpers_hash_table.c b/helpers/helpers_hash_table.c index 483759c..0511529 100644 --- a/helpers/helpers_hash_table.c +++ b/helpers/helpers_hash_table.c @@ -33,11 +33,11 @@ static hash_item_t * __hash_item_create(char * value) hash_item_t * hash_item = (hash_item_t *)malloc(sizeof(hash_item_t)); memset(hash_item, 0, sizeof(hash_item_t)); - hash_item->value = (char *)malloc(sizeof(char) * value_len); + hash_item->value = (char *)malloc(sizeof(char) * value_len); memset(hash_item->value, 0, sizeof(char) * value_len); strncpy(hash_item->value, value, value_len); - hash_t key = __hash_key_generator(value); + hash_t key = __hash_key_generator(value); if (key >= 0 && key <= 1.1) { printf("Error: This path to file is too big !\n"); @@ -45,8 +45,9 @@ static hash_item_t * __hash_item_create(char * value) } else { - hash_item->key = key; + hash_item->key = key; hash_item->previous = NULL; + hash_item->next = NULL; return hash_item; } @@ -130,6 +131,7 @@ unsigned int hash_table_insert_item(hash_table_t * this, char * value) } else { + this->node->next = (void *)item; item->previous = this->node; this->node = (void *)item; } diff --git a/include/helpers/helpers_hash_table.h b/include/helpers/helpers_hash_table.h index 1708824..affdaa5 100644 --- a/include/helpers/helpers_hash_table.h +++ b/include/helpers/helpers_hash_table.h @@ -20,6 +20,7 @@ typedef struct hash_item_s char * value; void * previous; + void * next; } hash_item_t; typedef struct hash_table_s