diff --git a/source/libublsettings.c b/source/libublsettings.c index 29e8a0a..a566551 100644 --- a/source/libublsettings.c +++ b/source/libublsettings.c @@ -229,7 +229,6 @@ void *yon_dictionary_free_all(dictionary *dictionary_to_free,void (*data_manipul if(data_manipulation) data_manipulation(dict->data); if(dict->prev) - free(dict->prev->key); free(dict->prev); } free(dict); @@ -408,7 +407,7 @@ int yon_char_find_last(char *source, char find){ return i; } -char *yon_char_append(char *source, char *append) +char *yon_char_append(const char *source, const char *append) { if (source && append) { @@ -421,7 +420,7 @@ char *yon_char_append(char *source, char *append) return NULL; } -char *yon_char_new(char *chr) +char *yon_char_new(const char *chr) { if (chr){ char *newchar = malloc(strlen(chr) + 1); @@ -432,7 +431,7 @@ char *yon_char_new(char *chr) return NULL; } -char *yon_char_unite(char *source, ...){ +char *yon_char_unite(const char *source, ...){ if (source){ va_list arglist; char *new_char=NULL; @@ -555,28 +554,28 @@ char *yon_char_from_float(float int_to_convert) char *yon_char_from_double(double int_to_convert) { - int negative = int_to_convert < 0 ? 1 : 0; - int digits_before_decimal = (int_to_convert == 0) ? 1 : (int)log10(fabs(int_to_convert)) + 1; - int total_length = digits_before_decimal + 1 + 2 + negative + 1; - char *ch = malloc(total_length * sizeof(char)); - if (!ch) { - return NULL; + int i = 1; + double convert_check = (double)int_to_convert; + for (i = 1; convert_check >= 10; i++) + { + convert_check = convert_check / 10; } - + char *ch = malloc((i + 9)* sizeof(char)); + memset(ch,0,(i + 9)* sizeof(char)); sprintf(ch, "%.2f", int_to_convert); return ch; } char *yon_char_from_long(long int_to_convert) { - int negative = int_to_convert < 0 ? 1 : 0; - int digits = (int_to_convert == 0) ? 1 : (int)log10(labs(int_to_convert)) + 1; - int total_length = digits + negative + 1; - char *ch = malloc(total_length * sizeof(char)); - if (!ch) { - return NULL; + int i = 1; + double convert_check = (double)int_to_convert; + for (i = 1; convert_check >= 10; i++) + { + convert_check = convert_check / 10; } - + char *ch = malloc(i * sizeof(char) + 1); + memset(ch,0,i * sizeof(char) + 1); sprintf(ch, "%ld", int_to_convert); return ch; } @@ -585,6 +584,7 @@ char *yon_char_replace(char *source, char *find, char*replace){ if (!strstr(replace,find)){ char *final=NULL; char *temp=NULL; + if(!strstr(replace,find)){ while ((final=strstr(source,find))){ temp=malloc(strlen(source)-strlen(final)+strlen(replace)); memset(temp,0,strlen(source)-strlen(final)+strlen(replace)); @@ -592,6 +592,8 @@ char *yon_char_replace(char *source, char *find, char*replace){ temp=yon_char_append(temp,replace); source=yon_char_append(temp,final+strlen(find)); } + + } } return source; } @@ -619,23 +621,16 @@ char **yon_char_parse(char *parameters, int *size, char *divider){ } char **yon_char_parsed_rip(char **char_string, int *size, int item_to_delete) { - if (char_string && size && (*size) > 0 && item_to_delete >= 0 && item_to_delete < (*size)) { + if (char_string && size && (*size) > 0 && (*size) > item_to_delete && item_to_delete >= 0) { char **new_char_parsed = malloc(sizeof(char*) * ((*size) - 1)); if (!new_char_parsed) { return NULL; } - + int new_index = 0; for (int i = 0; i < (*size); i++) { if (i != item_to_delete) { new_char_parsed[new_index] = yon_char_new(char_string[i]); - if (!new_char_parsed[new_index]) { - for (int j = 0; j < new_index; j++) { - free(new_char_parsed[j]); - } - free(new_char_parsed); - return NULL; - } new_index++; } else { free(char_string[i]); @@ -975,8 +970,8 @@ config_str yon_dir_get_contents(char *dir_path, int *size){ DIR *directory = opendir(dir_path); struct dirent *de; while ((de = readdir(directory))){ - if (dir){ dir = yon_char_parsed_append(dir,size,de->d_name); - } else dir = yon_char_parsed_new(size,de->d_name,NULL); + if (dir){ dir = yon_char_parsed_append(dir,size,yon_char_new(de->d_name)); + } else dir = yon_char_parsed_new(size,yon_char_new(de->d_name),NULL); } closedir(directory); } @@ -1284,16 +1279,10 @@ int yon_config_command_prepare(config_str *commands, int *commands_size,char *co for (int j=5;j0){ *str_len = i; return output_strings; diff --git a/source/libublsettings.h b/source/libublsettings.h index 00e5e0d..dd0bde1 100644 --- a/source/libublsettings.h +++ b/source/libublsettings.h @@ -13,7 +13,6 @@ #include #include #include -#include #define DesktopPath "/usr/share/applications/" @@ -314,21 +313,21 @@ int yon_char_find_last(char *source, char find); * * creates new char string by combining two char strings. */ -char *yon_char_append(char *source, char *append); +char *yon_char_append(const char *source, const char *append); /**[EN] * * creates new char string by copying another char. */ -char *yon_char_new(char *chr); +char *yon_char_new(const char *chr); -/**yon_char_unite(char *source, ...) +/**yon_char_unite(const char *source, ...) * [En] * * [RU] * Объединяет строку [source] со всеми строками, написанными в [...]. Последний элемент должен быть NULL */ -char *yon_char_unite(char *source, ...); +char *yon_char_unite(const char *source, ...); /**yon_cut(char *source, int size, int startpos) * [EN]