Added new config and char functions #83

Merged
asmeron merged 9 commits from YanTheKaller/libublsettings:master into master 1 month ago

@ -48,6 +48,24 @@ char *yon_char_append_element(char *source,char *target, char *divider){
return yon_char_new(source);
}
char *yon_char_append_elements(char *source,char *target, char *divider){
if (!yon_char_is_empty(target)&&!yon_char_is_empty(divider)){
if (yon_char_is_empty(source)) return yon_char_new(target);
int size=0;
int target_size=0;
config_str parsed = yon_char_parse(source,&size,divider);
config_str target_parsed = yon_char_parse(target,&target_size,divider);
for (int i=0;i<target_size;i++){
if (yon_char_parsed_check_exist(parsed,size,target_parsed[i])==-1){
yon_char_parsed_add_or_create_if_exists(parsed,&size,target_parsed[i]);
}
}
yon_char_parsed_free(target_parsed,target_size);
return yon_char_parsed_to_string(parsed,size,divider);
}
return yon_char_new(source);
}
char *yon_char_force_append_element(char *source,char *target, char *divider){
if (yon_char_is_empty(target)){
target = "";
@ -171,7 +189,7 @@ int yon_char_check_elements(char *target, char *element, char *divider){
}
yon_char_parsed_free(parsed,size);
yon_char_parsed_free(parsed_element,element_size);
return 0;
return 1;
}
char *yon_char_swap_element(const char *source, const char *a, const char *b, const char *divider){

@ -339,9 +339,20 @@ char *yon_config_command_get_full(config_str command, int command_size){
}
if (final_size>0){
char *final_command = yon_char_parsed_to_string(final,final_size," -- get ");
char *temp = yon_char_replace(final_command,"ubconfig","ubconfig -ea");
int pos = yon_char_check_element(final_command,"ubconfig", " ");
if (pos&&yon_char_check_element(final_command,"-ea"," ")==0){
int size=0;
config_str parsed = yon_char_parse(final_command,&size," ");
pos = yon_char_parsed_check_exist(parsed,size,"ubconfig");
char *tmp = yon_char_unite(parsed[pos]," -ea",NULL);
free(parsed[pos]);
parsed[pos] = tmp;
free(final_command);
final_command = temp;
final_command = yon_char_parsed_to_string(parsed,size," ");
}
// char *temp = yon_char_replace(final_command,"ubconfig","ubconfig -ea");
// free(final_command);
// final_command = temp;
yon_char_parsed_free(final,final_size);
return final_command;
}
@ -595,6 +606,43 @@ int yon_config_remove_by_key(char *key){
return 0;
}
int yon_config_remove_by_list(config_str keys, size_t size){
int result = 0;
check_config{
for (size_t i = 0;i<size;i++){
dictionary *dict = yon_dictionary_get((dictionary**)&__yon__config__strings,keys[i]);
if (dict){
yon_config_remove_ignore(dict->key);
((yon_config_parameter*)dict)->flag1=-1;
dict->data="";
result=1;
}
}
}
return result;
}
int yon_config_remove_by_args(char *key, ...){
int result = 0;
check_config{
va_list args;
va_start(args,key);
char *cur_key = key;
while (!yon_char_is_empty(cur_key)){
dictionary *dict = yon_dictionary_get((dictionary**)&__yon__config__strings,cur_key);
if (dict){
yon_config_remove_ignore(dict->key);
((yon_config_parameter*)dict)->flag1=-1;
dict->data="";
result=1;
}
cur_key = va_arg(args,char*);
}
va_end(args);
}
return result;
}
int yon_config_clear_by_key(const char *key){
check_config{
dictionary *dict = yon_dictionary_get((dictionary**)&__yon__config__strings,(char*)key);
@ -665,6 +713,25 @@ int yon_config_append_element(char *key, char *append, char *divider){
return 0;
}
int yon_config_append_elements(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){
if (!yon_char_check_elements((char*)dict->data,append,divider)){
char *temp = yon_char_append_elements((char*)dict->data,append,divider);
if (!yon_char_is_empty((char*)dict->data)) free(dict->data);
dict->data = temp;
dict->flag1 = 1;
yon_config_remove_ignore(key);
return 1;
}
}
}
return 0;
}
int yon_config_force_append_element(char *key, char *append, char *divider){
check_config{
int found = 0;
@ -1135,13 +1202,17 @@ config_str yon_config_get_load_parameters_by_list(int *size, config_str paramete
if (!strcmp(temp->key,current_str)){
int position = yon_char_parsed_find_element(final,*size,((yon_config_parameter*)temp)->section);
if (position>=0){
char *string = yon_char_unite((final)[position]," ",current_str,NULL);
char *quoted = yon_config_parameter_set_quotes(current_str);
char *string = yon_char_unite((final)[position]," ",quoted,NULL);
free((final)[position]);
(final)[position]=string;
free(quoted);
} else {
char *string = yon_char_unite(temp->section," ",current_str,NULL);
char *quoted = yon_config_parameter_set_quotes(current_str);
char *string = yon_char_unite(temp->section," ",quoted,NULL);
yon_char_parsed_add_or_create_if_exists(final,size,string);
free(quoted);
}
}
}
@ -1352,6 +1423,104 @@ char *yon_config_save_simple(YON_CONFIG_TYPE target, char *path){
return NULL;
}
int yon_config_save_list_simple(config_str parameters_keys, size_t size, char *path){
if (yon_char_is_empty(path)) return 0;
check_config{
int parameters_size=0;
config_str parameters = yon_config_get_save_parameters_by_list(&parameters_size,parameters_keys,size);
if (parameters&&parameters_size){
char *command_parameters = yon_char_parsed_to_string(parameters,parameters_size," -- ");
char *command = yon_char_unite(ubconfig_dull_command,"--target ",path," ", command_parameters,NULL);
return !!system(command);
}
}
return 0;
}
int yon_config_save_args_simple(char *path, ...){
if (yon_char_is_empty(path)) return 0;
check_config{
va_list args;
va_start(args,path);
int size = 0;
config_str params = NULL;
char *cur = NULL;
while ((cur = va_arg(args,char*))){
yon_char_parsed_add_or_create_if_exists(params,&size,cur);
}
int parameters_size=0;
config_str parameters = yon_config_get_save_parameters_by_list(&parameters_size,params,size);
yon_char_parsed_free(params,size);
if (parameters&&parameters_size){
char *command_parameters = yon_char_parsed_to_string(parameters,parameters_size," -- ");
char *command = yon_char_unite(ubconfig_dull_command,"--target ",path," ", command_parameters,NULL);
return !!system(command);
}
}
return 0;
}
int yon_config_update_by_key(char *key){
if (yon_char_is_empty(key)) return 0;
check_config{
dictionary *current = yon_dictionary_get((dictionary**)&__yon__config__strings,key);
if (!current) return 0;
if (current->data) free(current->data);
current->data = NULL;
int size=0;
char *command = yon_config_parameter_get_load_command(key);
config_str new_param = yon_config_load(command,&size);
if (size&&!yon_char_is_empty(new_param[0])&&strcmp(new_param[0],"(null)\n")){
yon_char_remove_last_symbol(new_param[0],'\n');
current->data = yon_char_new(new_param[0]);
((yon_config_parameter*)current)->flag1=0;
yon_config_set_status(key,0);
return 1;
}
}
return 0;
}
int yon_config_update_by_list(config_str keys, size_t size){
if (!size||!keys) return 0;
check_config{
int params_size=0;
config_str parameters = yon_config_get_load_parameters_by_list(&params_size,keys,size);
if (params_size&&parameters){
char *path = yon_config_command_get_path(config_last_load_command);
char *parameters_str = yon_char_parsed_to_string(parameters,params_size," ");
char *command = yon_char_unite(ubconfig_load_command(path), parameters_str, NULL);
yon_config_load_config(YON_CONFIG_CUSTOM,command,NULL);
}
}
return 0;
}
int yon_config_update_by_args(char *key, ...){
check_config{
int size=0;
config_str keys = NULL;
yon_char_parsed_add_or_create_if_exists(keys,&size,key);
va_list args;
va_start(args,key);
char *cur_key = NULL;
while((cur_key = va_arg(args,char*))){
yon_char_parsed_add_or_create_if_exists(keys,&size,cur_key);
}
int params_size=0;
config_str parameters = yon_config_get_load_parameters_by_list(&params_size,keys,size);
if (params_size&&parameters){
char *path = yon_config_command_get_path(config_last_load_command);
char *parameters_str = yon_char_parsed_to_string(parameters,params_size," -- get ");
char *command = yon_char_unite(ubconfig_load_command(path), parameters_str, NULL);
yon_config_load_config(YON_CONFIG_CUSTOM,command,NULL);
}
}
return 0;
}
char *yon_config_parameter_prepare_command(char *command, char *path, char *section, char *parameter){
if (!yon_char_is_empty(command)){
int size=0;

@ -140,6 +140,17 @@ config_str yon_file_ls(char *path, int *size){
return NULL;
}
char *yon_file_get_parent(const char *path){
if (yon_char_is_empty(path)|| !strstr(path,"/")) return NULL;
char *parent_path = NULL;
int size;
config_str parsed = yon_char_parse(path,&size,"/");
free(parsed[size-1]);
parsed[size-1] = NULL;
parent_path = yon_char_parsed_to_string_full(parsed,size,"/");
return parent_path;
}
int yon_file_is_directory(const char *path)
{
struct stat path_stat;

@ -68,7 +68,7 @@ void yon_locale_set(struct yon_locale *target,char *locale_name){
void yon_locale_init(){
int size;
config_str locales_list = yon_dir_get_contents(locales_path,&size);
config_str locales_list = yon_file_ls(locales_path,&size);
locales_hash_table = yon_hash_new(size*2,yon_str_hash);
for (int i=0;i<size;i++){
if (!strcmp(locales_list[i],".")||!strcmp(locales_list[i],"..")) continue;

@ -128,7 +128,7 @@ config_str yon_dir_get_by_mask(char *path, char *mask, int *size){
lpart = yon_char_divide_search(rpart,"*",-1);
config_str files = NULL;
int found_size;
config_str found_files = yon_dir_get_contents(path,&found_size);
config_str found_files = yon_file_ls(path,&found_size);
if(found_size>0){
int found = 1;
int rfound = 0;

@ -646,6 +646,13 @@ char *yon_char_append_c(const char *source, char append);
*/
char *yon_char_append_element(char *source,char *target, char *divider);
/// @brief Append string of elements with another string of elements
/// @param source sSource string of elements.
/// @param target Appended string of elements.
/// @param separator String, separating elements within parameter's value
/// @return A newly allocated string of elements
char *yon_char_append_elements(char *source,char *target, char *separator);
char *yon_char_force_append_element(char *source,char *target, char *divider);
/**
* @brief Remove substring from string.
@ -1107,89 +1114,69 @@ double yon_size_long_convert_to_mod(double size, char mod);
double yon_size_long_convert_automatic(unsigned long bytes, char *size);
/**yon_file_path_proceed_spaces(char *path)
* [EN]
*
* [RU]
* Оборачивает все пробелы в пути для правильной обработки
*/
/// @brief Wrap all spaces in paths for proper handling in commands
/// @return A newly allocated string or NULL if path was empty.
char *yon_file_path_proceed_spaces(char *path);
/**yon_file_open(char *file_path, int *size)
* [EN]
*
* [RU]
* Открывает файл [file_path], возвращает содержимое в виде массива строк размера [size]
*/
/// @brief Read file and return its contents as string array, separated by new line symbols.
/// @param file_path Path to file.
/// @param size Size of returned list.
/// @return A newly allocated array of strings containing the entire contents of the file.
config_str yon_file_open(char *file_path, int *size);
/**yon_file_save(char *file_path, char *text)
* [EN]
*
* [RU]
* Сохранить текст [text] в файл по пути [file_path]. Если файла не существует, он создаётся
*/
/// @brief Save string to file
/// @param file_path File where text will be saved.
/// @param text Text to save.
/// @return TRUE if successful, FALSE otherwise
int yon_file_save(char *file_path, char *text);
/**yon_file_create(char *path, char *name, int rules)
* [EN]
*
* [RU]
* Создать файл с названием [name], находящимся по пути [path]
* С правами доступа [rules] (от 0000 до 0777)
*/
/// @brief Create a file for path.
/// @param path Path to create file.
/// @param name File name.
/// @param rules Rules for all created directories and file (from 0000 to 0777).
/// @return TRUE if file were successfully created, FALSE if file exist/invalid path/error occured while creating file.
int yon_file_create(char *path, char *name, int rules);
/**yon_file_create_full_path(char *path, char *name, int rules)
* [EN]
*
* [RU]
* Создать файл по пути [path]
* С правами доступа [rules] (от 0000 до 0777)
*/
/// @brief Create a file and any missing directories for path.
/// @param rules Rules for all created directories and file (from 0000 to 0777).
/// @return TRUE if file were successfully created, FALSE if file exist/invalid path/error occured while creating file.
int yon_file_create_full_path(char *path, mode_t rules);
/**yon_file_ls (char *path, int *size)
* [EN]
*
* [RU]
* Возвращает массив названий директорий размера [size], находящихся внутри директории [path].
*/
/// @brief Get a list of directory names for path.
/// @param size Size of returned list.
/// @return A newly allocated string array or NULL.
config_str yon_file_list_dirs (char *path, int *size);
/**
* [EN]
*
* [RU]
* Возвращает массив строк размера [size] с именами всех файлов и папок из директории [path]
*/
/// @brief Get a list of file/directory names for path.
/// @param path Path string.
/// @param size Size of returned list.
/// @return A newly allocated string array or NULL.
config_str yon_file_ls(char *path, int *size);
/**yon_file_is_directory(const char *path)
* [EN]
*
* [RU]
* Проверяет указывает ли путь [path] на директорию
*/
/// @brief Get parent directory from path.
/// @return Newly allocated string with parent path or NULL.
char *yon_file_get_parent(const char *path);
/// @brief Check if path points to directory.
/// @return TRUE if path is pointing to directory, 0 otherwise.
int yon_file_is_directory(const char *path);
/**yon_dir_remove(const char *path)
* [EN]
*
* [RU]
* Проверяет указывает ли путь [path] на директорию и удаляет её
*/
/// @brief Removes directory for path. If path is not directory, nothing happens.
void yon_dir_remove(const char *path);
/** yon_dir_get_contents(char *dir_path, int *size)
* [EN]
* [DEPRECATED]
*
* [RU]
* [УСТАРЕЛО]
* Проверяет существует ли папка [dir_path] и
* возвращает список всех вложенных файлов и папок,
* передавая в [size] длину списка.
*/
config_str yon_dir_get_contents(char *dir_path, int *size);
__attribute__((deprecated("Use yon_file_ls() instead"))) config_str yon_dir_get_contents(char *dir_path, int *size);
config_str yon_dir_get_by_mask(char *path, char *mask, int *size);
@ -1333,25 +1320,36 @@ int yon_config_load_register_no_cleaning(YON_CONFIG_TYPE config_type,char *secti
[[ deprecated ( "Use yon_config_load_config instead" ) ]]
int yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...);
/// @brief Set configuration parameter ignore status. Determines whether the parameter will be compared with the value from the configuration or the old value will be ignored when saving.
/// @param key Key of parameter.
/// @param status New status
void yon_config_compare_ignore_set(char *key, int status);
/// @brief Get configuration parameter ignore status
/// @param key Key of parameter.
/// @return TRUE if parameter is ignored, FALSE otherwise
int yon_config_compare_ignore_get(char *key);
/**yon_config_remove_by_key(char *key)
* [EN]
*
* [RU]
* Удаляет параметр конфига по ключу [key]
*/
/// @brief Mark parameter from configuration for remove. It will be removed from target configuration at save.
/// @param key Key of parameter.
/// @return TRUE if parameter was successfully removed, FALSE otherwise.
int yon_config_remove_by_key(char *key);
/**yon_config_remove_by_key(char *key)
* [EN]
*
* [RU]
* Очищает параметр конфига по ключу [key].
* Параметр удаляется как при полной очистке всего конфига.
*/
/// @brief Mark list of parameters from configuration for remove. It will be removed from target configuration at save.
/// @param keys string array of parameters removing parameter keys.
/// @param size size of string array.
/// @return TRUE if parameter was successfully removed, FALSE otherwise.
int yon_config_remove_by_list(config_str keys, size_t size);
/// @brief Mark list of parameters from configuration for remove. It will be removed from target configuration at save.
/// @param key First of parameter keys to remove.
/// @param ... A null-terminated list of remaining parameter keys.
/// @return TRUE if parameter was successfully removed, FALSE otherwise.
int yon_config_remove_by_args(char *key, ...);
/// @brief Clear a configuration parameter. Remove it completely, as if it had never been added - a parameter removed this way will not be compared when saving.
/// @param key Key of parameter.
/// @return TRUE if parameter was successfully removed, FALSE otherwise.
int yon_config_clear_by_key(const char *key);
/// @brief Remove substring from registered parameter value string
@ -1375,6 +1373,13 @@ int yon_config_remove_elements(char *key, char *delete_target, char *separator);
/// @return 1 if element was successfully added, 0 otherwise
int yon_config_append_element(char *key, char *append, char *separator);
/// @brief Append registered parameter value string of elements with another string of elements
/// @param key parameter key
/// @param append string to append
/// @param separator string, separating elements within parameter's value
/// @return 1 if element was successfully added, 0 otherwise
int yon_config_append_elements(char *key, char *append, char *divider);
/// @brief Append parameter value string event if appended string already exists in parameter value or empty
/// @param key parameter key
/// @param append string to append
@ -1517,6 +1522,37 @@ enum YON_CONFIG_SAVED_TYPE yon_config_register_default(char *key,char *config_lo
/// @return
char *yon_config_save_simple(YON_CONFIG_TYPE target, char *path);
/// @brief Save list of parameters into configuration.
/// @param parameters_keys List of parameter keys.
/// @param size size of parameter keys list.
/// @param path Path to configuration .ini.
/// @return TRUE if successful, FALSE otherwise.
int yon_config_save_list_simple(config_str parameters_keys, size_t size, char *path);
/// @brief Save list of parameters into configuration.
/// @param path Path to configuration .ini.
/// @param ... A NULL-terminated list of parameter keys.
/// @return TRUE if successful, FALSE otherwise.
int yon_config_save_args_simple(char *path, ...);
/// @brief Update parameter's value from configuration.
/// @param key Parameter's key.
/// @return TRUE if parameter successfully updated, FALSE if parameter doesn't registered in configuration
int yon_config_update_by_key(char *key);
/// @brief Update parameter's value from configuration.
/// @param key Parameters key list.
/// @param size size of parameters keys list.
/// @return TRUE if parameter successfully updated, FALSE if parameter doesn't registered in configuration
int yon_config_update_by_list(config_str keys, size_t size);
/// @brief Update parameters value from configuration.
/// @param key First parameter's key.
/// @param ... NULL-terminated list of parameter keys.
/// @return TRUE if parameter successfully updated, FALSE if parameter doesn't registered in configuration
int yon_config_update_by_args(char *key, ...);
/**int yon_config_force_save_registered(char *path, char *section)
* [EN]
* Force config to save at [path] config ignoring parameter save status.

Loading…
Cancel
Save