Merge pull request 'Added new functions; Edits of some char functions' (#21) from YanTheKaller/libublsettings:master into master

Reviewed-on: #21
master v1.17
Dmitry Razumov 2 years ago
commit 9e9f49684b

@ -335,6 +335,7 @@ int yon_char_find_last(char *source, char find){
int size = strlen(source); int size = strlen(source);
int i=size; int i=size;
for (;source[i]!=find&&i>0;i--); for (;source[i]!=find&&i>0;i--);
if (source[i]!=find)i=-1;
return i; return i;
} }
@ -345,13 +346,9 @@ char *yon_char_append(char *source, char *append)
int size = strlen(source) + strlen(append) + 1; int size = strlen(source) + strlen(append) + 1;
char *final = malloc(size); char *final = malloc(size);
memset(final, 0, 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; return final;
} }
else
return NULL; return NULL;
} }
@ -367,6 +364,7 @@ char *yon_char_new(char *chr)
} }
char *yon_char_unite(char *source, ...){ char *yon_char_unite(char *source, ...){
if (source){
va_list arglist; va_list arglist;
char *new_char=NULL; char *new_char=NULL;
char *unite_char=NULL; char *unite_char=NULL;
@ -374,20 +372,29 @@ char *yon_char_unite(char *source, ...){
va_start(arglist,source); va_start(arglist,source);
unite_char = va_arg(arglist,char*); unite_char = va_arg(arglist,char*);
while(unite_char){ while(unite_char){
new_char = yon_char_append(new_char,unite_char); char *temp = yon_char_append(new_char,unite_char);
free(new_char);
new_char=temp;
unite_char = va_arg(arglist,char*); unite_char = va_arg(arglist,char*);
} }
va_end(arglist); va_end(arglist);
return new_char; return new_char;
}
return NULL;
} }
char *yon_cut(char *source, int size, int startpos) char *yon_cut(char *source, int size, int startpos)
{ {
if (source){
if (strlen(source+startpos)>=size){
char *cut = NULL; char *cut = NULL;
cut = malloc(size + 1); cut = malloc(size + 1);
memset(cut, 0, size + 1); memset(cut, 0, size + 1);
memcpy(cut, source + startpos, size); memcpy(cut, source + startpos, size);
return cut; return cut;
}
}
return NULL;
} }
char *yon_char_divide(char *source, int dividepos) char *yon_char_divide(char *source, int dividepos)
@ -401,6 +408,7 @@ char *yon_char_divide(char *source, int dividepos)
memcpy(left, source + dividepos + 1, (strlen(source) - dividepos)); memcpy(left, source + dividepos + 1, (strlen(source) - dividepos));
memset(source, 0, strlen(source)); memset(source, 0, strlen(source));
memcpy(source, left, strlen(left)); memcpy(source, left, strlen(left));
free(left);
return cut; return cut;
} }
return NULL; return NULL;
@ -412,6 +420,7 @@ int yon_char_find_count(char *source, char *find){
int size=0; int size=0;
int pos=0; int pos=0;
config_str rtn = yon_char_parse(working_string,&size,"\n"); config_str rtn = yon_char_parse(working_string,&size,"\n");
free(working_string);
for (int j=0;j<size;j++){ for (int j=0;j<size;j++){
if(strstr(rtn[j],find)) if(strstr(rtn[j],find))
i++; i++;
@ -423,11 +432,11 @@ char *yon_char_divide_search(char *source, char *dividepos, int delete_divider)
{ {
if (source&&dividepos){ if (source&&dividepos){
char *cut = strstr(source, dividepos); char *cut = strstr(source, dividepos);
if (cut) if (cut){
{
int leng = strlen(source) - strlen(cut); int leng = strlen(source) - strlen(cut);
cut = yon_char_divide(source, leng); cut = yon_char_divide(source, leng);
return cut; return cut;
} }
} }
return source; return source;
@ -477,8 +486,6 @@ char *yon_char_from_long(long int_to_convert)
char *yon_char_replace(char *source, char *find, char*replace){ char *yon_char_replace(char *source, char *find, char*replace){
if (!strstr(replace,find)){ if (!strstr(replace,find)){
char *final=NULL; char *final=NULL;
char *temp=NULL; char *temp=NULL;
if(!strstr(replace,find)){ if(!strstr(replace,find)){
@ -1927,6 +1934,16 @@ config_str yon_file_open(char *file_path, int *size){
} }
} }
int yon_file_save(char *file_path, char *text){
FILE *file = fopen(file_path,"w");
if (file){
fputs(text,file);
fclose(file);
return 1;
}
return 0;
}
int yon_file_create(char *path, char *name, int rules){ int yon_file_create(char *path, char *name, int rules){
if (path&&name){ if (path&&name){
char *full_path = yon_char_unite(path,"/",name,NULL); char *full_path = yon_char_unite(path,"/",name,NULL);

@ -574,6 +574,8 @@ char *yon_file_path_proceed_spaces(char *path);
*/ */
config_str yon_file_open(char *file_path, int *size); config_str yon_file_open(char *file_path, int *size);
int yon_file_save(char *file_path, char *text);
/**yon_file_create(char *path, char *name, int rules) /**yon_file_create(char *path, char *name, int rules)
* [EN] * [EN]
* *

Loading…
Cancel
Save