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, ...);