From e74aa0bd72041f1ab87d6bde613835434eb342e6 Mon Sep 17 00:00:00 2001 From: Ivan Yarcev Date: Tue, 9 Jan 2024 14:10:41 +0600 Subject: [PATCH 1/4] Added new function, changed config function --- source/CMakeLists.txt | 2 - source/libublsettings.c | 84 +++++++++++++++++++++++++++++------------ source/libublsettings.h | 4 +- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index e275a8f..5c48c66 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -9,8 +9,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissin -O2 -pipe -fno-plt -fexceptions \ -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \ -fstack-clash-protection -fcf-protection") - - add_library(${PROJECT_NAME} SHARED libublsettings.c diff --git a/source/libublsettings.c b/source/libublsettings.c index 2cb4200..6bd7b6e 100644 --- a/source/libublsettings.c +++ b/source/libublsettings.c @@ -214,15 +214,17 @@ dictionary *yon_dictionary_new_with_data(char *key, void *data) } void *yon_dictionary_free_all(dictionary *dictionary_to_free,void (*data_manipulation)(void*)){ - dictionary *dict=NULL; - for_dictionaries(dict,dictionary_to_free){ - if(data_manipulation) - data_manipulation(dict->data); - if(dict->prev) - free(dict->prev); + if (dictionary_to_free){ + dictionary *dict=NULL; + for_dictionaries(dict,dictionary_to_free){ + if(data_manipulation) + data_manipulation(dict->data); + if(dict->prev) + free(dict->prev); + } + free(dict); + return NULL; } - free(dict); - return NULL; } dictionary *yon_dictionary_append_with_data(dictionary *dict, char *key, void *data) @@ -711,6 +713,18 @@ dictionary *yon_char_parsed_convert_copy_to_dictionary(config_str parsed, int si return NULL; } +char *yon_char_parsed_to_string(config_str parsed, int size, char *divider_replace){ + if (parsed && size>0){ + char *final_string = ""; + for (int i=0;idata; - char *found = strstr(data,deleted); - int size=strlen(data)-strlen(found)+1; - char *new_data = malloc(size); - memset(new_data,0,size); - if (strlen(found)!=strlen(deleted)){ - memcpy(new_data,data,size-1); - new_data = yon_char_append(new_data,found+strlen(deleted)+1); - } else { - memcpy(new_data,data,size-2); - new_data = yon_char_append(new_data,found+strlen(deleted)); + if (dict){ + int size; + config_str parsed = yon_char_parse((char*)dict->data,&size,divider); + config_str final = NULL; + for(int i=0;idata); + dict->data=final_string; + return 1; + } else return 0; } - dict->data=(void*)(new_data); - dict->flag1=1; - return 1; + + } else return 0; +} + +int yon_config_append_element(char *key, char *append, char *divider){ + check_config{ + int found = 0; + yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key); + if (dict){ + yon_char_unite((char*)dict->data,divider,append,NULL); + return 1; + } + } else return 0; } @@ -1175,14 +1205,18 @@ int yon_config_save_registered(char *path){ for_dictionaries(dct,sections_add){ char *command = yon_dictionary_get_data(dct,char*); yon_launch(command); + yon_dictionary_free_all(sections_add,free); } if (sections_remove) for_dictionaries(dct,sections_remove){ char *command = yon_dictionary_get_data(dct,char*); yon_launch(command); + yon_dictionary_free_all(sections_remove,free); } - return 1; - } else return 1; + if (sections_add||sections_remove) + return 1; + else return 0; + } else return 0; } int yon_config_force_save_registered(char *path){ diff --git a/source/libublsettings.h b/source/libublsettings.h index 577a89c..3321fff 100644 --- a/source/libublsettings.h +++ b/source/libublsettings.h @@ -492,6 +492,8 @@ 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); +char *yon_char_parsed_to_string(config_str parsed, int size, char *divider_replace); + /**yon_ubl_check_root() * [EN] * @@ -653,7 +655,7 @@ int yon_config_remove_by_key(char *key); * [RU] * Удаляет элемент [deleted] из массива параметров с ключом [key] */ -int yon_config_remove_element(char *key, char *deleted); +int yon_config_remove_element(char *key, char *delete, char *divider); void yon_config_set_status(int status); -- 2.35.1 From b2bf0a60e61f9955f32b0452d30e3bb40d9052a3 Mon Sep 17 00:00:00 2001 From: Ivan Yarcev Date: Thu, 11 Jan 2024 10:33:15 +0600 Subject: [PATCH 2/4] Fixes for saving --- source/libublsettings.c | 198 ++++++++++++++++++++++++++-------------- source/libublsettings.h | 32 ++++++- 2 files changed, 160 insertions(+), 70 deletions(-) diff --git a/source/libublsettings.c b/source/libublsettings.c index 6bd7b6e..9da6273 100644 --- a/source/libublsettings.c +++ b/source/libublsettings.c @@ -213,6 +213,15 @@ dictionary *yon_dictionary_new_with_data(char *key, void *data) return dct; } +void *yon_dictionary_free(dictionary *dictionary_to_free){ + if (dictionary_to_free){ + free(dictionary_to_free->data); + free(dictionary_to_free->key); + free(dictionary_to_free); + return NULL; + } +} + void *yon_dictionary_free_all(dictionary *dictionary_to_free,void (*data_manipulation)(void*)){ if (dictionary_to_free){ dictionary *dict=NULL; @@ -891,7 +900,24 @@ dictionary *__yon_config_ignored = NULL; int yon_config_set_ignore(char *key){ - yon_dictionary_add_or_create_if_exists_with_data(__yon_config_ignored,key,NULL); + if (!yon_dictionary_get(&__yon_config_ignored,key)){ + yon_dictionary_add_or_create_if_exists_with_data(__yon_config_ignored,key,NULL); + } +} + +int yon_config_remove_ignore(char *key){ + dictionary *dict = yon_dictionary_get(&__yon_config_ignored,key); + if (dict) { + yon_dictionary_free(yon_dictionary_rip(dict)); + } +} + +int yon_config_get_status(char *key){ + dictionary *dict; + if (yon_dictionary_get((dictionary**)&__yon__config__strings,key)){ + return ((yon_config_parameter*)dict)->flag1; + } + else return 0; } int yon_config_check_ignore(char *key){ @@ -900,83 +926,114 @@ int yon_config_check_ignore(char *key){ else return 0; } -int yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...){ - if (__yon__config__strings){ - __yon__config__strings = yon_dictionary_free_all((dictionary*)__yon__config__strings,NULL); - } - va_list args; - va_start(args,parameter); - dictionary *sections = NULL; - yon_dictionary_add_or_create_if_exists_with_data(sections,section,parameter); - char *arg; - - while (arg=va_arg(args,char*)){ - char *key = va_arg(args,char*); - if (sections&&yon_dictionary_get(§ions,arg)) sections->data=(void*)yon_char_unite(yon_dictionary_get_data(sections,char*)," ",key,NULL); - else yon_dictionary_add_or_create_if_exists_with_data(sections,arg,key); - } - char *command=NULL; - dictionary *dict; - for_dictionaries(dict,sections){ - command = yon_char_unite(ubconfig_load_command, " default get ", dict->key," ", yon_dictionary_get_data(dict,char*),NULL); - FILE *output = popen(command, "r"); - int i = 0; - char str[4096]; - memset(str, 0, 4096); - while (fgets(str, 4096, output)) - { - if (strcmp(str, "") != 0&& strcmp(str,"(null)\n")!=0) +int yon_config_load_register_no_cleaning(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...){ + if (config_type!=YON_CONFIG_BOTH){ + va_list args; + va_start(args,parameter); + dictionary *sections = NULL; + yon_dictionary_add_or_create_if_exists_with_data(sections,section,parameter); + char *arg; + + while (arg=va_arg(args,char*)){ + char *key = va_arg(args,char*); + if (sections&&yon_dictionary_get(§ions,arg)) sections->data=(void*)yon_char_unite(yon_dictionary_get_data(sections,char*)," ",key,NULL); + else yon_dictionary_add_or_create_if_exists_with_data(sections,arg,key); + } + char *command=NULL; + dictionary *dict; + for_dictionaries(dict,sections){ + command = yon_char_unite(ubconfig_load_command,config_type==YON_CONFIG_GLOBAL ? " global get " : config_type==YON_CONFIG_LOCAL ? " system get " : " default get", dict->key," ", yon_dictionary_get_data(dict,char*),NULL); + FILE *output = popen(command, "r"); + int i = 0; + char str[4096]; + memset(str, 0, 4096); + while (fgets(str, 4096, output)) { - char *key = yon_char_divide_search(str,"=",-1); - char *final_str=yon_char_divide_search(str,"\n",-1); - - if (!yon_dictionary_get((dictionary**)&__yon__config__strings,key)){ - yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,yon_char_new(final_str)); + if (strcmp(str, "") != 0&& strcmp(str,"(null)\n")!=0) + { + char *key = yon_char_divide_search(str,"=",-1); + char *final_str=yon_char_divide_search(str,"\n",-1); + + if (!yon_dictionary_get((dictionary**)&__yon__config__strings,key)){ + yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,yon_char_new(final_str)); + } else { + __yon__config__strings->data=final_str; + } + __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; + __yon__config__strings->section=dict->key; } - __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; - __yon__config__strings->section=dict->key; } + free(command); + fclose(output); } - free(command); - fclose(output); - } - for_dictionaries(dict,sections){ - command = yon_char_unite(ubconfig_load_command,config_type==YON_CONFIG_GLOBAL ? " global get " : " system get ", dict->key," ", yon_dictionary_get_data(dict,char*),NULL); - FILE *output = popen(command, "r"); - int i = 0; - char str[4096]; - memset(str, 0, 4096); - while (fgets(str, 4096, output)) - { - if (strcmp(str, "") != 0&& strcmp(str,"(null)\n")!=0) + check_config + return 1; + else return 0; + } else return -1; +} + +int yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...){ + if (config_type!=YON_CONFIG_BOTH){ + if (__yon__config__strings){ + __yon__config__strings = yon_dictionary_free_all((dictionary*)__yon__config__strings,NULL); + } + va_list args; + va_start(args,parameter); + dictionary *sections = NULL; + yon_dictionary_add_or_create_if_exists_with_data(sections,section,parameter); + char *arg; + + while (arg=va_arg(args,char*)){ + char *key = va_arg(args,char*); + if (sections&&yon_dictionary_get(§ions,arg)) sections->data=(void*)yon_char_unite(yon_dictionary_get_data(sections,char*)," ",key,NULL); + else yon_dictionary_add_or_create_if_exists_with_data(sections,arg,key); + } + char *command=NULL; + dictionary *dict; + for_dictionaries(dict,sections){ + command = yon_char_unite(ubconfig_load_command,config_type==YON_CONFIG_GLOBAL ? " global get " : config_type==YON_CONFIG_LOCAL ? " system get " : " default get ", dict->key," ", yon_dictionary_get_data(dict,char*),NULL); + FILE *output = popen(command, "r"); + int i = 0; + char str[4096]; + memset(str, 0, 4096); + while (fgets(str, 4096, output)) { - char *key = yon_char_divide_search(str,"=",-1); - char *final_str=yon_char_divide_search(str,"\n",-1); - - if (!yon_dictionary_get((dictionary**)&__yon__config__strings,key)){ - yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,yon_char_new(final_str)); - } else { - __yon__config__strings->data=final_str; + if (strcmp(str, "") != 0&& strcmp(str,"(null)\n")!=0) + { + char *key = yon_char_divide_search(str,"=",-1); + char *final_str=yon_char_divide_search(str,"\n",-1); + + if (!yon_dictionary_get((dictionary**)&__yon__config__strings,key)){ + yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,yon_char_new(final_str)); + } else { + __yon__config__strings->data=final_str; + } + if (config_type==YON_CONFIG_DEFAULT){ + __yon__config__strings->flag1=-2; + yon_config_set_ignore(key); + } + __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; + __yon__config__strings->section=dict->key; } - __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; - __yon__config__strings->section=dict->key; } + free(command); + fclose(output); } - free(command); - fclose(output); - } - check_config - return 1; - else return 0; + check_config + return 1; + else return 0; + } else return -1; } int yon_config_remove_by_key(char *key){ check_config{ dictionary *dict = yon_dictionary_get((dictionary**)&__yon__config__strings,key); if (dict){ - ((yon_config_parameter*)dict)->flag1=-1; - dict->data=""; - return 1; + if (!yon_dictionary_get(&__yon_config_ignored,dict->key)){ + ((yon_config_parameter*)dict)->flag1=-1; + dict->data=""; + return 1; + } else return 0; }else return 0; } return 0; @@ -1117,6 +1174,11 @@ int yon_config_set(char *key, void *data){ yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key); dict->data=data; dict->flag1=1; + printf("%s has been changed\n",dict->key); + if (yon_dictionary_get(&__yon_config_ignored, dict->key)){ + __yon_config_ignored = yon_dictionary_rip(__yon_config_ignored); + printf("%s removed from ignored\n",dict->key); + } return 1; } else return 0; } @@ -1142,13 +1204,17 @@ 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_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,yon_char_new(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->data=yon_char_new(data); __yon__config__strings->flag1=1; + if (yon_dictionary_get(&__yon_config_ignored, __yon__config__strings->key)){ + __yon_config_ignored = yon_dictionary_rip(__yon_config_ignored); + printf("%s removed from ignored\n",__yon__config__strings->key); + } } } __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; diff --git a/source/libublsettings.h b/source/libublsettings.h index 3321fff..dd50505 100644 --- a/source/libublsettings.h +++ b/source/libublsettings.h @@ -265,6 +265,14 @@ dictionary *yon_dictionary_rip(dictionary *dict); */ dictionary *yon_dictionary_get_nth(dictionary *dict, int place); +/** void *yon_dictionary_free_all(dictionary *dictionary,void *data_manipulation) + * [EN] + * Frees memory of dictionary [dictionary_to_free]. + * [RU] + * Освобождает память элемента словаря [dictionary_to_free]. +*/ +void *yon_dictionary_free(dictionary *dictionary_to_free); + /** void *yon_dictionary_free_all(dictionary *dictionary,void *data_manipulation) * [EN] * Frees whole [dictionary] and activates [data_manipulation] function if not NULL with [dictionary]->data argument for each dictionary. @@ -592,7 +600,8 @@ config_str yon_dir_get_contents(char *dir_path, int *size); typedef enum { YON_CONFIG_LOCAL=0, YON_CONFIG_GLOBAL, - YON_CONFIG_BOTH + YON_CONFIG_BOTH, + YON_CONFIG_DEFAULT } YON_CONFIG_TYPE; /**yon_config_load(char *command, int *str_len) @@ -630,14 +639,29 @@ char *yon_config_get_parameter(config_str parameters, int size, char *param); int yon_config_set_ignore(char *key); +int yon_config_remove_ignore(char *key); + +int yon_config_get_status(char *key); + int yon_config_check_ignore(char *key); -/**yon_config_load_register(char *command) +/**yon_config_load_register_no_cleaning(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...) + * [EN] + * + * [RU] + * Считывает параметры [parameter] из раздела [section] ubconfig'а. + * ... - пара из [section], [parameter], позволяющих загружать параметры из разных разделов конфига, оканчивающихся NULL. + * Полученные данные парсятся и регистрируются в конфиг без удаления старых данных. +*/ +int yon_config_load_register_no_cleaning(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...); + +/**yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...) * [EN] * * [RU] - * Выполняет команду [command]. - * Полученные данные парсятся и регистрируются в конфиг. + * Считывает параметры [parameter] из раздела [section] ubconfig'а. + * ... - пара из [section], [parameter], позволяющих загружать параметры из разных разделов конфига, оканчивающихся NULL. + * Полученные данные парсятся и регистрируются в конфиг, удаляя старые значения. */ int yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...); -- 2.35.1 From f99aad5471dbc83ce34244dc8aa75542f47c6cf3 Mon Sep 17 00:00:00 2001 From: Ivan Yarcev Date: Thu, 11 Jan 2024 10:34:57 +0600 Subject: [PATCH 3/4] removed comments --- source/libublsettings.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/libublsettings.c b/source/libublsettings.c index 9da6273..1f7e06f 100644 --- a/source/libublsettings.c +++ b/source/libublsettings.c @@ -1174,10 +1174,8 @@ int yon_config_set(char *key, void *data){ yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key); dict->data=data; dict->flag1=1; - printf("%s has been changed\n",dict->key); if (yon_dictionary_get(&__yon_config_ignored, dict->key)){ __yon_config_ignored = yon_dictionary_rip(__yon_config_ignored); - printf("%s removed from ignored\n",dict->key); } return 1; } else return 0; @@ -1213,7 +1211,6 @@ void yon_config_register(char *key, char *config_section, void *data){ __yon__config__strings->flag1=1; if (yon_dictionary_get(&__yon_config_ignored, __yon__config__strings->key)){ __yon_config_ignored = yon_dictionary_rip(__yon_config_ignored); - printf("%s removed from ignored\n",__yon__config__strings->key); } } } -- 2.35.1 From 19735543ce6d8221c8bb52f7275a606e1706b2a2 Mon Sep 17 00:00:00 2001 From: Ivan Yarcev Date: Mon, 15 Jan 2024 10:00:53 +0600 Subject: [PATCH 4/4] Fixed unremoving of ignored parameters --- source/libublsettings.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/libublsettings.c b/source/libublsettings.c index 1f7e06f..b881423 100644 --- a/source/libublsettings.c +++ b/source/libublsettings.c @@ -908,7 +908,7 @@ int yon_config_set_ignore(char *key){ int yon_config_remove_ignore(char *key){ dictionary *dict = yon_dictionary_get(&__yon_config_ignored,key); if (dict) { - yon_dictionary_free(yon_dictionary_rip(dict)); + __yon_config_ignored=yon_dictionary_rip(dict); } } @@ -959,6 +959,11 @@ int yon_config_load_register_no_cleaning(YON_CONFIG_TYPE config_type,char *secti } else { __yon__config__strings->data=final_str; } + yon_config_remove_ignore(key); + if (config_type==YON_CONFIG_DEFAULT){ + __yon__config__strings->flag1=-2; + yon_config_set_ignore(key); + } __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; __yon__config__strings->section=dict->key; } @@ -1008,6 +1013,7 @@ int yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *par } else { __yon__config__strings->data=final_str; } + yon_config_remove_ignore(key); if (config_type==YON_CONFIG_DEFAULT){ __yon__config__strings->flag1=-2; yon_config_set_ignore(key); @@ -1210,7 +1216,7 @@ void yon_config_register(char *key, char *config_section, void *data){ __yon__config__strings->data=yon_char_new(data); __yon__config__strings->flag1=1; if (yon_dictionary_get(&__yon_config_ignored, __yon__config__strings->key)){ - __yon_config_ignored = yon_dictionary_rip(__yon_config_ignored); + yon_dictionary_rip(__yon_config_ignored); } } } -- 2.35.1