From 8ef0d67724cbdcb6a736e61602eee8aa29a396a8 Mon Sep 17 00:00:00 2001 From: Ivan Yarcev Date: Wed, 27 Mar 2024 18:11:22 +0600 Subject: [PATCH] Added new functions; Edits of some char functions --- source/libublsettings.c | 64 +++++++++++++++++++++++++---------------- source/libublsettings.h | 2 ++ 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/source/libublsettings.c b/source/libublsettings.c index 0f8b94a..62b9ec0 100644 --- a/source/libublsettings.c +++ b/source/libublsettings.c @@ -345,13 +345,9 @@ char *yon_char_append(char *source, char *append) int size = strlen(source) + strlen(append) + 1; char *final = malloc(size); memset(final, 0, size); - // if (strstr(source, "%%")) - // sprintf(final, source, append); - // else - sprintf(final, "%s%s", source, append); + sprintf(final, "%s%s", source, append); return final; } - else return NULL; } @@ -367,27 +363,37 @@ char *yon_char_new(char *chr) } char *yon_char_unite(char *source, ...){ - va_list arglist; - char *new_char=NULL; - char *unite_char=NULL; - new_char=yon_char_new(source); - va_start(arglist,source); - unite_char = va_arg(arglist,char*); - while(unite_char){ - new_char = yon_char_append(new_char,unite_char); + if (source){ + va_list arglist; + char *new_char=NULL; + char *unite_char=NULL; + new_char=yon_char_new(source); + va_start(arglist,source); unite_char = va_arg(arglist,char*); + while(unite_char){ + char *temp = yon_char_append(new_char,unite_char); + free(new_char); + new_char=temp; + unite_char = va_arg(arglist,char*); + } + va_end(arglist); + return new_char; } - va_end(arglist); - return new_char; + return NULL; } char *yon_cut(char *source, int size, int startpos) { - char *cut = NULL; - cut = malloc(size + 1); - memset(cut, 0, size + 1); - memcpy(cut, source + startpos, size); - return cut; + if (source){ + if (strlen(source+startpos)>=size){ + char *cut = NULL; + cut = malloc(size + 1); + memset(cut, 0, size + 1); + memcpy(cut, source + startpos, size); + return cut; + } + } + return NULL; } char *yon_char_divide(char *source, int dividepos) @@ -401,6 +407,7 @@ char *yon_char_divide(char *source, int dividepos) memcpy(left, source + dividepos + 1, (strlen(source) - dividepos)); memset(source, 0, strlen(source)); memcpy(source, left, strlen(left)); + free(left); return cut; } return NULL; @@ -412,6 +419,7 @@ int yon_char_find_count(char *source, char *find){ int size=0; int pos=0; config_str rtn = yon_char_parse(working_string,&size,"\n"); + free(working_string); for (int j=0;j