Merge pull request 'Added new functions' (#89) from YanTheKaller/libublsettings:master into master

Reviewed-on: #89
pull/92/head^2 v1.82
Dmitry Razumov 1 month ago
commit 3bd71f9e74

@ -42,7 +42,7 @@ static yon_config_parameter *__yon__config__strings = NULL;
static yon_config_parameter *__yon__config__default__strings = NULL; static yon_config_parameter *__yon__config__default__strings = NULL;
dictionary *__yon_config_ignored = 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 check_default_config if(__yon__config__default__strings)
#define for_config yon_config_parameter *temp = NULL; for_dictionaries(temp,__yon__config__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) #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){ 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 *param_string = NULL;
char *parameter_wrapped = yon_config_parameter_wrap(parameter->key); 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); 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);
@ -1860,3 +1860,212 @@ void yon_config_to_default(){
yon_config_register(dict->key,dict->load_command,dict->data); 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,&parameters_size);
for (int i=0;i<parameters_size;i++){
if (!strcmp(parameters[i],"(null)\n")) continue;
yon_config_custom_import_string(target,parameters[i]);
}
return 1;
}
enum YON_CONFIG_SAVED_TYPE yon_config_custom_register(yon_config_custom **target, char *key,char *config_load, char *data){
if (yon_char_is_empty(key)||yon_char_is_empty(config_load)) return YON_CONFIG_SAVED_ERROR;
enum YON_CONFIG_SAVED_TYPE save_type = YON_CONFIG_SAVED_NEW;
yon_config_parameter *current = NULL;
if (*target){
current = (yon_config_parameter*)yon_dictionary_get((dictionary**)target,key);
if (!current &&*target&&!yon_char_is_empty(((yon_config_parameter*)*target)->first->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)&&current->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;i<params_size;i++){
int found = 0;
current_str=parameters[i];
yon_config_parameter *temp;
for_dictionaries(temp,((yon_config_parameter*)target)){
if (!strcmp(temp->key,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;
}

@ -1610,6 +1610,66 @@ int yon_config_move_after(char *parameter, char *target);
/// @brief Revert configuration to default values. /// @brief Revert configuration to default values.
void yon_config_to_default(); 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 // terminal-using functions
/**yon_launch_app_with_arguments(char *name, char *args) /**yon_launch_app_with_arguments(char *name, char *args)

Loading…
Cancel
Save