From 35def97b907c3489ee16422ceb371aba4251ec45 Mon Sep 17 00:00:00 2001 From: Ivan Dmitrievich Yartsev Date: Mon, 30 Mar 2026 15:28:21 +0600 Subject: [PATCH] Added new functions --- source/libublsettings-config.c | 213 ++++++++++++++++++++++++++++++++- source/libublsettings.h | 60 ++++++++++ 2 files changed, 271 insertions(+), 2 deletions(-) diff --git a/source/libublsettings-config.c b/source/libublsettings-config.c index 5a1a9b0..ef09515 100644 --- a/source/libublsettings-config.c +++ b/source/libublsettings-config.c @@ -42,7 +42,7 @@ static yon_config_parameter *__yon__config__strings = NULL; static yon_config_parameter *__yon__config__default__strings = NULL; dictionary *__yon_config_ignored = NULL; -#define check_config if(__yon__config__strings&&__yon__config__strings->data_type==DICTIONARY_CHAR_TYPE) +#define check_config if(__yon__config__strings) #define check_default_config if(__yon__config__default__strings) #define for_config yon_config_parameter *temp = NULL; for_dictionaries(temp,__yon__config__strings) #define for_default_config dictionary *temp = NULL; for_dictionaries(temp,(dictionary*)__yon__config__default__strings) @@ -1206,7 +1206,7 @@ char *yon_config_parameter_wrap(char *parameter_key){ } char *yon_config_parameter_to_string(yon_config_parameter *parameter, int insert_section){ - if (parameter){ + if (parameter&&!yon_char_is_empty(parameter->key)){ char *param_string = NULL; char *parameter_wrapped = yon_config_parameter_wrap(parameter->key); param_string = yon_char_unite(insert_section?parameter->section:"",insert_section?" ":"", parameter_wrapped,parameter->flag1==-1?NULL:"","=\'",yon_char_return_if_exist(parameter->data,""),"\'",NULL); @@ -1859,4 +1859,213 @@ void yon_config_to_default(){ for_dictionaries(dict,__yon__config__default__strings){ yon_config_register(dict->key,dict->load_command,dict->data); } +} + +// Custom config + + +#define yon_config_custom_parameter_add_or_create_if_exists_with_data(dict,key,value) {if (dict){\ + yon_config_parameter *dct = (yon_config_parameter *)yon_dictionary_get((dictionary**)&dict,key);\ + if (dct) {\ + dict=(yon_config_custom*)dct;\ + ((yon_config_parameter *)dict)->data=value;\ + }\ + else{\ + dict=(yon_config_custom*)yon_config_parameter_append_with_data((yon_config_parameter *)dict,key,value);\ + }\ + }\ + else dict=(yon_config_custom*)yon_config_parameter_new_with_data(key,value);} + +yon_config_custom *yon_config_custom_new(){ + yon_config_custom *custom = malloc(sizeof(yon_config_custom)); + memset(custom,0,sizeof(yon_config_custom)); + ((yon_config_parameter*)custom)->first = (yon_config_parameter*)custom; + return custom; +} + +int yon_config_custom_import_string(yon_config_custom **target, char *string){ + if (!strcmp(string,"(null)\n")) return 0; + char *section = NULL; + yon_char_remove_last_symbol(string,'\n'); + char *value = yon_char_new(string); + char *parameter = yon_char_divide_search(value,"=",-1); + if (parameter[0]=='['){ + section = yon_char_divide_search(parameter," ",-1); + free(yon_char_divide(section,yon_char_find_last(section,'/'))); + yon_char_remove_last_symbol(section,']'); + } + yon_char_remove_brackets(value); + + char *get_parameter = yon_char_unite("[",section,"]"," ", value,NULL); + char *get_command = ubconfig_load_command_full("system",get_parameter); + + + char *cur_data = config(parameter); + + if (value){ + yon_config_custom_register(target,parameter,get_command,value); + // yon_config_set_section(parameter,section); + } +} + +int yon_config_custom_load_config(yon_config_custom **target, ...){ + + char *current; + va_list args; + va_start(args,target); + + int commands_size; + config_str commands = NULL; + while ((current=va_arg(args,char*))){ + yon_char_parsed_add_or_create_if_exists(commands,&commands_size,current); + } + + char *final_command = yon_config_command_get_full(commands,commands_size); + yon_config_set_last_command(final_command); + if (yon_char_is_empty(final_command)) return 0; + int parameters_size; + config_str parameters = yon_config_load(final_command,¶meters_size); + for (int i=0;ifirst->key)) current=((yon_config_parameter*)target)->first; + if (!current){ + yon_config_custom_parameter_add_or_create_if_exists_with_data(*target,key,data); + current = (yon_config_parameter*)yon_dictionary_get_last((dictionary *)*target); + } else if (!yon_char_is_empty(data)&¤t->data&&!strcmp((char*)current->data,(char*)data)){ + save_type=YON_CONFIG_SAVED_EXIST; + return save_type; + } else { + current->data = yon_char_new(data); + } + } else { + yon_config_custom_parameter_add_or_create_if_exists_with_data(*target,key,data); + current = (yon_config_parameter*)*target; + } + + config_load = yon_char_new(config_load); + + current->data_type=DICTIONARY_CHAR_TYPE; + current->load_command = config_load; + current->section = yon_config_command_get_section(config_load); + + if (data){ + current->flag1=1; + } else { + current->flag1 = 0; + } + switch(current->flag1){ + case -1: save_type = YON_CONFIG_SAVED_REMOVED; break; + case 1: save_type = YON_CONFIG_SAVED_CHANGED; break; + default: save_type = YON_CONFIG_SAVED_NEW; break; + } + return save_type; +} + +int yon_config_custom_clean(yon_config_custom *target){ + if (target){ + yon_dictionary_free_all((dictionary*)target, NULL); + return 1; + } + return 0; +} + +config_str yon_config_get_save_parameters_by_custom_config(yon_config_custom *target, int *size, config_str parameters, int params_size){ + va_list list; + (*size)=0; + int removed_size; + config_str removed = NULL; + int updated_size; + config_str updated = NULL; + config_str final = NULL; + char *current_str = NULL; + for (int i=0;ikey,current_str)){ + found = 1; + if (((yon_config_parameter*)temp)->flag1!=-2){ + char *action = NULL; + config_str *current=NULL; + int *current_size=NULL; + switch (((yon_config_parameter*)temp)->flag1){ + case -1: + action = "remove"; + current = &removed; + current_size = &removed_size; + break; + case 1: + action = "set"; + current = &updated; + current_size = &updated_size; + break; + default: + continue; + } + int position = yon_char_parsed_find_element(*current,*current_size,((yon_config_parameter*)temp)->section); + if (position>=0){ + char *string = yon_char_unite((*current)[position]," ",yon_config_parameter_to_string((yon_config_parameter*)temp,0),NULL); + free((*current)[position]); + (*current)[position]=string; + + } else { + char *string = yon_char_unite(action," ",yon_config_parameter_to_string((yon_config_parameter*)temp,1),NULL); + yon_char_parsed_add_or_create_if_exists(*current,current_size,string); + } + } + } + } + if (!found){ + if(removed&&temp){ + int position = yon_char_parsed_find_element(removed,removed_size,((yon_config_parameter*)temp)->section); + if (position>=0){ + char *string = yon_char_unite((removed)[position]," ",yon_config_parameter_to_string((yon_config_parameter*)temp,0),NULL); + free(removed[position]); + removed[position]=string; + + } + } else if (temp){ + char *string = yon_char_unite("remove"," ",yon_config_parameter_to_string((yon_config_parameter*)temp,1),NULL); + yon_char_parsed_add_or_create_if_exists(removed,&removed_size,string); + } + } + } + if (updated&&removed){ + final = yon_char_parsed_merge(updated,updated_size,removed,removed_size,size); + } else if (updated&&!removed){ + final=updated; + *size=updated_size; + } else if (!updated&&removed){ + final=removed; + *size=removed_size; + } + return final; +} + +int yon_config_custom_swap(yon_config_custom **target){ + yon_config_clean(); + yon_config_parameter *temp = __yon__config__strings; + __yon__config__strings = (yon_config_parameter *)*target; + *target = (yon_config_custom *)__yon__config__strings; +} + +int yon_config_custom_apply(yon_config_custom **target){ + yon_config_clean(); + __yon__config__strings = ((yon_config_parameter *)*target)->first; + *target=NULL; } \ No newline at end of file diff --git a/source/libublsettings.h b/source/libublsettings.h index 0002fb0..d1f87d3 100644 --- a/source/libublsettings.h +++ b/source/libublsettings.h @@ -1610,6 +1610,66 @@ int yon_config_move_after(char *parameter, char *target); /// @brief Revert configuration to default values. void yon_config_to_default(); +struct _yon_config_parameter{ + char *key; + void *data; + struct yon_config_parameter *next; + struct yon_config_parameter *prev; + struct yon_config_parameter *first; + DICT_TYPE data_type; + int flag1; + char *section; + int ignore; + char *save_command; + char *load_command; + int compare_ignore; + char *regex_mask_pattern; +}; + +typedef struct { + struct _yon_config_parameter param; +} yon_config_custom; + +/// @brief Create a custom configuration storage +/// @return A newly allocated custom configuration storage +yon_config_custom *yon_config_custom_new(); + +/// @brief Load custom configuration +/// @param ... NULL-terminated List of commands. +/// @return 0 if failed, id for specific action type otherwise +int yon_config_custom_load_config(yon_config_custom **target, ...); + +/// @brief Import string to custom config +/// @param config_type configuration type +/// @param string parameter string. A line from ubconfig command output +/// @return 1 if successful, 0 otherwise. +int yon_config_custom_import_string(yon_config_custom **target, char *string); + +/// @brief Register new or change existing parameter inside custom configuration +/// @param key key for new or existing parameter +/// @param config_load command to load a parameter from the system configuration +/// @param data parameter value +/// @return the type of action performed by the configuration system with the parameter +enum YON_CONFIG_SAVED_TYPE yon_config_custom_register(yon_config_custom **target, char *key,char *config_load, char *data); + +/// @brief Clear custom configuration parameters +/// @return 1 if any parameters were removed, 0 if configuratino was empty +int yon_config_custom_clean(yon_config_custom *target); + +/// @brief Get a list with pairs of parmaeter=value strings for saving from custom config +/// @param size Pointer for final size of list +/// @param parameters list of parameters to request from custom config +/// @param params_size size of requested parameters list +/// @return +config_str yon_config_get_save_parameters_by_custom_config(yon_config_custom *target, int *size, config_str parameters, int params_size); + +/// @brief Swap internal and custom config +/// @return TRUE if swapped successfully, FALSE otherwise +int yon_config_custom_swap(yon_config_custom **target); + +/// @brief Apply custom config to internal config. Remove all parameters and insert custom configuration; +/// @return TRUE if applied successfully, FALSE otherwise +int yon_config_custom_apply(yon_config_custom **target); // terminal-using functions /**yon_launch_app_with_arguments(char *name, char *args) -- 2.35.1