From 56b396a87007f621387170d9e13b20ccf476764b Mon Sep 17 00:00:00 2001 From: Ivan Yarcev Date: Wed, 13 Dec 2023 16:43:59 +0600 Subject: [PATCH] Added various functions: for dictionary, for configuration, for string arrays, etc --- source/CMakeLists.txt | 2 - source/ublsettings.c | 162 +++++++++++++++++++++++++++++++++++++----- source/ublsettings.h | 55 ++++++++++++-- 3 files changed, 196 insertions(+), 23 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 20dde0d..ec142f3 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -26,8 +26,6 @@ if(WEBKIT_LIBRARIES_FOUND) endif() -#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always -std=c++2a") -#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always -lm") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always \ -O2 -pipe -fno-plt -fexceptions \ -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \ diff --git a/source/ublsettings.c b/source/ublsettings.c index c417f0a..211df08 100644 --- a/source/ublsettings.c +++ b/source/ublsettings.c @@ -242,15 +242,24 @@ dictionary *yon_dictionary_connect(dictionary *old, dictionary *toconnect) return toconnect; } +dictionary *yon_dictionary_merge(dictionary *dict1,dictionary *dict2){ + dictionary *dct = NULL; + for_dictionaries(dct,dict2){ + if (!yon_dictionary_get(&dict1,dct->key)) + yon_dictionary_connect(dict1,dct); + } + return dict1; +} + dictionary *yon_dictionary_get(dictionary **dict, char *key) { dictionary *dct = *dict; - for (dictionary *pointer = dct->first; pointer != NULL; pointer = pointer->next) - { - if (strcmp(pointer->key, key) == 0) - { - *dict = pointer; - return pointer; + if (dct){ + for (dictionary *pointer = dct->first; pointer != NULL; pointer = pointer->next){ + if (pointer->key&&strcmp(pointer->key, key) == 0){ + *dict = pointer; + return pointer; + } } } return NULL; @@ -395,7 +404,6 @@ int yon_char_find_count(char *source, char *find){ if(strstr(rtn[j],find)) i++; } - printf("%d\n",i); return i; } @@ -579,14 +587,14 @@ void yon_char_parsed_free(config_str source, int size){ free(source); } -void yon_char_parsed_copy(config_str *source, config_str *to_copy){ - if (source&&!*source&&to_copy){ - int size=0; - config_str new_char = yon_char_parsed_new(&size,(*to_copy)[0]); - for (int i=0;(*to_copy)[i];i++){ - new_char = yon_char_parsed_append(new_char,&size,yon_char_new((*to_copy)[i])); +config_str yon_char_parsed_copy(config_str to_copy, int size){ + if (to_copy&&(*to_copy)&&size>0){ + int final_size=0; + config_str final = yon_char_parsed_new(&final_size,to_copy[0],NULL); + for (int i=1;i0&&size2>0){ + *final_size=0; + config_str parsed_final = yon_char_parsed_new(final_size,array1[0],NULL); + for (int i=1;i0&&size2>0){ + *final_size=0; + int new_size=0; + config_str parsed_final = yon_char_parsed_new(final_size,array1[0],NULL); + for (int i=1;i0){ + for (int i=0;i0){ + for (int i=0;i0){ + dictionary *dict = NULL; + for (int i=0;i0){ + dictionary *dict = NULL; + for (int i=0;idata_type==DICTIONARY_CHAR_TYPE) -#define for_config dictionary temp = NULL; for_dictionary(temp,(dictionary*)__yon__config__strings) +#define for_config dictionary *temp = NULL; for_dictionaries(temp,(dictionary*)__yon__config__strings) #define yon_config_parameter_add_or_create_if_exists_with_data(dict,key,data) {if (!dict) dict=yon_config_parameter_new_with_data(key,data); \ else dict=yon_config_parameter_append_with_data(dict,key,data);} @@ -905,6 +995,14 @@ int yon_config_remove_element(char *key, char *deleted){ } else return 0; } +void yon_config_set_status(int status){ + check_config{ + for_config{ + ((yon_config_parameter*)temp)->flag1=status; + } + } +} + void *yon_config_get_by_key(char *key){ check_config{ dictionary *dict = NULL; @@ -917,6 +1015,18 @@ void *yon_config_get_by_key(char *key){ return NULL; } +char *yon_config_get_section_for_key(char *key){ + check_config{ + for_config{ + if (!yon_char_is_empty(temp->key)){ + if (!strcmp(temp->key,key)){ + return yon_char_new(((yon_config_parameter*)temp)->section); + } + } + } + } +} + void *yon_config_get_all_by_key(char *key, int *size){ check_config{ config_str ret_data=NULL; @@ -932,6 +1042,19 @@ void *yon_config_get_all_by_key(char *key, int *size){ } } +config_str yon_config_get_all_keys(int *size){ + check_config{ + *size=0; + config_str final = NULL; + for_config{ + if (!final) final = yon_char_parsed_new(size,temp->key,NULL); + else final = yon_char_parsed_append(final,size,temp->key); + } + return final; + } + return NULL; +} + int yon_config_set(char *key, void *data){ check_config{ yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key); @@ -963,10 +1086,15 @@ int yon_config_clean(){ void yon_config_register(char *key, char *config_section, void *data){ if (!__yon__config__strings||!yon_dictionary_get((dictionary**)&__yon__config__strings,key)){ yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,data); + __yon__config__strings->flag1=1; + } + else if (yon_dictionary_get((dictionary**)&__yon__config__strings,key)){ + if (data!=__yon__config__strings->data&&strcmp(__yon__config__strings->data,data)){ + __yon__config__strings->data=data; + __yon__config__strings->flag1=1; + } } - else if (yon_dictionary_get((dictionary**)&__yon__config__strings,key)) __yon__config__strings->data=data; __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; - __yon__config__strings->flag1=1; __yon__config__strings->section=yon_char_new(config_section); } diff --git a/source/ublsettings.h b/source/ublsettings.h index f05415b..ee573fc 100644 --- a/source/ublsettings.h +++ b/source/ublsettings.h @@ -238,6 +238,8 @@ dictionary *yon_dictionary_append_with_data(dictionary *dict, char *key, void *d */ dictionary *yon_dictionary_connect(dictionary *old, dictionary *toconnect); +dictionary *yon_dictionary_merge(dictionary *dict1,dictionary *dict2); + /**yon_dictionary_get(dictionary **dict, char *key) * [EN] * @@ -378,6 +380,9 @@ char *yon_char_replace(char *source, char *find, char*replace); */ char **yon_char_parse(char *parameters, int *size, char *divider); +#define yon_char_parsed_add_or_create_if_exists(parsed,size,data) {if (!parsed) parsed = yon_char_parsed_new(size,data,NULL);\ + else parsed = yon_char_parsed_append(parsed,size,data);} + /**yon_char_parsed_rip(char **char_string, int *size, int item_to_delete) * [EN] * @@ -435,13 +440,13 @@ config_str yon_char_parsed_new (int *size, ...); void yon_char_parsed_free(config_str source, int size); -/**yon_char_parsed_copy(config_str *source, config_str *to_copy) +/**config_str yon_char_parsed_copy(config_str to_copy, int size) * [EN] * * [RU] - * Копирует массив строк [to_copy] в [source] + * Возвращает копию массива строк [to_copy] размера [size] */ -void yon_char_parsed_copy(config_str *source, config_str *to_copy); +config_str yon_char_parsed_copy(config_str to_copy, int size); /**config_str yon_char_parsed_append(config_str parsed, int *size, char *string) * [EN] @@ -451,6 +456,36 @@ void yon_char_parsed_copy(config_str *source, config_str *to_copy); */ config_str yon_char_parsed_append(config_str parsed, int *size, char *string); +/**yon_char_parsed_merge(config_str array1, int size1, config_str array2, int size2, int *final_size) + * [EN] + * + * [RU] + * Объединяет два массива строк в один. + * [array1] - первый массив строк размера [size1] + * [array2] - второй массив строк размера [size2] + * [final_size] - указатель на целочисленную переменную в которую должен вернуться размер нового массива +*/ +config_str yon_char_parsed_merge(config_str array1, int size1, config_str array2, int size2, int *final_size); + +/**yon_char_parsed_merge_no_repeats(config_str array1, int size1, config_str array2, int size2, int *final_size) + * [EN] + * + * [RU] + * Объединяет два массива строк в один с предотвращением дублей. + * [array1] - первый массив строк размера [size1] + * [array2] - второй массив строк размера [size2] + * [final_size] - указатель на целочисленную переменную в которую должен вернуться размер нового массива +*/ +config_str yon_char_parsed_merge_no_repeats(config_str array1, int size1, config_str array2, int size2, int *final_size); + +int yon_char_parsed_divide_full(config_str parsed,int size,int divide_pos); + +int yon_char_parsed_divide_search_full(config_str parsed,int size,char *divide_pos, int delete_divider); + +dictionary *yon_char_parsed_convert_to_dictionary(config_str parsed, int size); + +dictionary *yon_char_parsed_convert_copy_to_dictionary(config_str parsed, int size); + /**yon_ubl_check_root() * [EN] * @@ -610,6 +645,8 @@ int yon_config_remove_by_key(char *key); */ int yon_config_remove_element(char *key, char *deleted); +void yon_config_set_status(int status); + /**yon_config_get_key_by_key(char *data) * [EN] * @@ -619,6 +656,8 @@ int yon_config_remove_element(char *key, char *deleted); */ void *yon_config_get_by_key(char *key); +char *yon_config_get_section_for_key(char *key); + /**yon_config_get_all_by_key(char *data) * [EN] * @@ -628,6 +667,14 @@ void *yon_config_get_by_key(char *key); */ void *yon_config_get_all_by_key(char *key, int *size); +/**yon_config_get_all_keys(int *size) + * [EN] + * + * [RU] + * Возвращает массив с ключами всех параметров внутреннего конфига +*/ +config_str yon_config_get_all_keys(int *size); + /**yon_config_set(char *key, void *data) * [EN] * @@ -683,7 +730,7 @@ int yon_config_force_save_registered(char *path); * Возвращает массив со всеми параметрами конфига, оканчивающаяся NULL * [size] - указатель, в который выгружается длина массива */ -config_str yon_config_get_all(); +config_str yon_config_get_all(int *size); // terminal-using functions