Added function #43

Merged
asmeron merged 7 commits from YanTheKaller/libublsettings:master into master 6 months ago

@ -10,8 +10,13 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissin
-Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \
-fstack-clash-protection -fcf-protection")
add_library(${PROJECT_NAME} SHARED
libublsettings.c
libublsettings-config.c
libublsettings-dictionary.c
libublsettings-char.c
libublsettings-file.c
libublsettings.h)

@ -0,0 +1,684 @@
#include "libublsettings.h"
int yon_char_find_last(char *source, char find){
int size = strlen(source);
int i=size;
for (;source[i]!=find&&i>0;i--);
if (source[i]!=find)i=-1;
return i;
}
char *yon_char_append(const char *source, const char *append)
{
if (source && append)
{
int size = strlen(source) + strlen(append) + 1;
char *final = malloc(size);
memset(final, 0, size);
sprintf(final, "%s%s", source, append);
return final;
}
return NULL;
}
char *yon_char_append_c(const char *source, char append)
{
if (source)
{
size_t size = strlen(source) + 2;
char *final = malloc(size);
memset(final, 0, size);
memcpy(final,source,size-2);
final[size-2]=append;
return final;
}
return NULL;
}
char *yon_char_new(const char *chr)
{
if (chr){
char *newchar = malloc(strlen(chr) + 1);
memset(newchar, 0, strlen(chr) + 1);
memcpy(newchar, chr, strlen(chr));
return newchar;
} else
return NULL;
}
char *yon_char_unite(const char *source, ...){
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){
if (unite_char[0]!='\0'){
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;
}
return NULL;
}
char *yon_cut(char *source, int size, int startpos)
{
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;
}
int yon_char_check_begins_with(char *haystack, char *needle){
if (!yon_char_is_empty(haystack)&&!yon_char_is_empty(needle)&&strlen(haystack)>=strlen(needle)){
int size = strlen(needle);
for (int i=0;i<size;i++){
if (haystack[i]!=needle[i])
return 0;
}
return 1;
}
return 0;
}
char *yon_char_parsed_check_exist_begins_with(char **target, int size, char *compare){
for (int i=0;i<size;i++){
if (yon_char_check_begins_with(target[i],compare)){
return yon_char_new(target[i]);
}
}
return NULL;
}
config_str yon_char_wrap_to_lines(char *target, unsigned int line_count, unsigned int *final_size){
if (!yon_char_is_empty(target)&&line_count){
(*final_size)=0;
unsigned int spaces = yon_char_count(target," ");
float line_spaces = (float)spaces/line_count;
float left = spaces%line_count;
int processed=0;
int size;
config_str parsed = yon_char_parse(target,&size," ");
config_str final = NULL;
for (int i=0;i<line_count;i++){
char *final_str = NULL;
for (int j=0;j<(int)line_spaces+left?1:0;j++){
if (processed>spaces+1) break;
if (final_str){
char *temp = yon_char_unite(final_str," ",parsed[processed],NULL);
free(final_str);
final_str = temp;
} else {
final_str = parsed[processed];
}
processed++;
}
if (left) left--;
yon_char_parsed_add_or_create_if_exists(final,final_size,final_str);
}
return final;
}
else return NULL;
}
char *yon_char_divide(char *source, int dividepos)
{
if (source&&strlen(source)>=dividepos){
char *cut = malloc(dividepos + 1);
memset(cut, 0, dividepos + 1);
memcpy(cut, source, dividepos);
char *left = malloc(strlen(source) - strlen(cut));
memset(left, 0, strlen(source) - strlen(cut));
memcpy(left, source + dividepos + 1, (strlen(source) - dividepos));
memset(source, 0, strlen(source));
memcpy(source, left, strlen(left));
free(left);
return cut;
}
return NULL;
}
// int yon_char_find_count(char *source, char *find){
// char *working_string=yon_char_new(source);
// int i=0;
// int size=0;
// int pos=0;
// config_str rtn = yon_char_parse(working_string,&size,find);
// free(working_string);
// yon_char_parsed_free(rtn,size);
// return size-1;
// }
int yon_char_count(char *source, char *find){
char *working_string=yon_char_new(source);
int i=0;
int size=0;
char *cur=strstr(source,find);
for (i=0;cur;cur = strstr(cur,find)){
cur++;
i++;
}
return i;
}
char *yon_char_divide_search(char *source, char *dividepos, int delete_divider)
{
if (source&&dividepos){
char *cut = strstr(source, dividepos);
if (cut){
int leng = strlen(source) - strlen(cut);
cut = yon_char_divide(source, leng);
return cut;
}
}
return source;
}
char *yon_char_from_int(int int_to_convert)
{
int i = snprintf( NULL, 0, "%d", int_to_convert );
char *ch = malloc(i * sizeof(char) + 1);
memset(ch,0,i * sizeof(char) + 1);
sprintf(ch, "%d", int_to_convert);
return ch;
}
char *yon_char_from_float(float int_to_convert)
{
int i = 1;
float convert_check = (float)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_double(double int_to_convert)
{
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 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;
}
char *yon_char_replace(char *source, char *find, char*replace){
if (!strstr(replace,find)){
int source_size = strlen(source);
int replace_size = strlen(replace);
char *final=NULL;
char *temp=NULL;
while ((final=strstr(source,find))){
int final_size = strlen(final);
temp=malloc(source_size-final_size+replace_size);
memset(temp,0,source_size-final_size+replace_size);
memcpy(temp,source,source_size-final_size);
char *tmp = yon_char_append(temp,replace);
if (temp) free(temp);
temp = tmp;
source=yon_char_append(temp,final+strlen(find));
free(temp);
}
}
return source;
}
char *yon_char_replace_single(char *source, char *find, char *replace){
if (!yon_char_is_empty(source)&&!yon_char_is_empty(find)){
char *current = NULL;
if ((current=strstr(source,find))){
int size = sizeof(char)*(strlen(source)-strlen(find)+strlen(replace)+1);
int replace_pos = strlen(source)-strlen(find);
char *final = malloc(size);
memset(final,0,size);
memcpy(final,source,replace_pos);
memcpy(final+replace_pos,replace,strlen(replace));
if (strlen(source+replace_pos)>strlen(find)){
memcpy(final+strlen(final),source+strlen(find),strlen(source+strlen(find)));
}
return final;
}
}
return NULL;
}
char **yon_char_parse(char *parameters, int *size, char *divider){
*size=0;
if (parameters){
char **string=NULL;
char *paramline=yon_char_new(parameters);
char *param = strstr(paramline,divider);
for (;param;param=strstr(paramline,divider)){
string = yon_remalloc(string,sizeof(char*)*((*size)+1));
string[(*size)]=yon_char_divide(paramline,strlen(paramline)-strlen(param));
*size=(*size)+1;
}
string = yon_remalloc(string,sizeof(char*)*((*size)+1));
string[(*size)]=paramline;
(*size)+=1;
if(!string){
string = yon_char_parsed_new(size,parameters,NULL);
}
return string;
}
return NULL;
}
char **yon_char_parsed_rip(char **char_string, int *size, int item_to_delete) {
if (char_string && size && (*size) > 0 && (*size) > item_to_delete && item_to_delete >= 0) {
if ((*size)==1){
yon_char_parsed_free(char_string,*size);
return NULL;
}
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]);
new_index++;
} else {
free(char_string[i]);
}
}
free(char_string);
(*size) = (*size) - 1;
return new_char_parsed;
} else {
return NULL;
}
}
int yon_char_parsed_check_exist(char **parameters, int size, char *param){
if (parameters&&size&&!yon_char_is_empty(param)){
for (int i=0;i<size;i++){
if (parameters[i]){
if (!strcmp(parameters[i],param))
return i;
} else return -1;
}
}
return -1;
}
int yon_char_parsed_strstr(char **parameters, int size, char *param){
if (parameters){
for (int i=0;i<size;i++){
if (parameters[i]){
if (strstr(parameters[i],param))
return i;
} else return -1;
}
}
return -1;
}
int yon_char_parsed_check_repeats(char **parameters, int size, int *first_overlap, int *second_overlap){
if (parameters){
*first_overlap=0;
*second_overlap=0;
for ((*first_overlap)=0;(*first_overlap)<size;(*first_overlap)++){
for ((*second_overlap)=0;(*second_overlap)<size;(*second_overlap)++){
if (parameters[(*first_overlap)]&&parameters[(*second_overlap)]){
if (!strcmp(parameters[(*first_overlap)],parameters[(*second_overlap)])&&(*first_overlap)!=(*second_overlap))
return 1;
}
}
}
}
return 0;
}
int yon_char_parsed_includes_char_parsed (config_str source, config_str to_check, int source_size, int check_size){
int overall_found=0;
for (int i=0;i<source_size;i++){
int found=0;
for (int j=0;j<check_size;j++){
if (strcmp(source[i],to_check[j])==0&&found==0) { found=1; overall_found++; }
}
}
if (overall_found==check_size)
return 1;
else return 0;
}
config_str yon_char_parsed_new (int *size, ...){
va_list arglist;
config_str new_parsed=NULL;
new_parsed=new(char*);
int new_size=0;
va_start(arglist,size);
char *newparse = va_arg(arglist,char*);
while (newparse){
new_size++;
new_parsed=realloc(new_parsed,new_size*sizeof(char*));
new_parsed[new_size-1]=yon_char_new(newparse);
newparse=va_arg(arglist,char*);
}
va_end(arglist);
*size=new_size;
return new_parsed;
}
void yon_char_parsed_free(config_str source, int size){
if (source&&size>0){
for (int i=0;i<size;i++){
free(source[i]);
}
free(source);
}
}
config_str yon_char_parsed_copy(config_str to_copy, int size){
if (to_copy&&(*to_copy)&&size>0){
int final_size=0;
config_str final = yon_char_parsed_new(&final_size,to_copy[0],NULL);
for (int i=1;i<size;i++){
final = yon_char_parsed_append(final,&final_size,to_copy[i]);
}
return final;
}
}
config_str yon_char_parsed_append(config_str parsed, int *size, char *string){
config_str new_parsed=realloc(parsed,(*size+1)*sizeof(char*));
new_parsed[(*size)]=yon_char_new(string);
(*size)++;
return new_parsed;
}
void yon_char_parsed_append_strings (config_str array, int size, char *prepend){
if (array && size>0 && !yon_char_is_empty(prepend)){
for (int i=0;i<size;i++){
char *newstr = yon_char_append(array[i],prepend);
free(array[i]);
array[i]=newstr;
}
}
}
void yon_char_parsed_prepend_strings (config_str array, int size, char *prepend){
if (array && size>0 && !yon_char_is_empty(prepend)){
for (int i=0;i<size;i++){
char *newstr = yon_char_append(prepend,array[i]);
free(array[i]);
array[i]=newstr;
}
}
}
config_str yon_char_parsed_merge(config_str array1, int size1, config_str array2, int size2, int *final_size){
if (array1&&array2&&size1>0&&size2>0){
*final_size=0;
config_str parsed_final = yon_char_parsed_new(final_size,array1[0],NULL);
for (int i=1;i<size1;i++){
parsed_final = yon_char_parsed_append(parsed_final,final_size,array1[i]);
}
for (int i=0;i<size2;i++){
parsed_final = yon_char_parsed_append(parsed_final,final_size,array2[i]);
}
return parsed_final;
} else {
if (array1){
*final_size=size1;
return array1;
}
if (array2)
*final_size=size2;
return array2;
}
}
config_str yon_char_parsed_merge_no_repeats(config_str array1, int size1, config_str array2, int size2, int *final_size){
if (array1&&array2&&size1>0&&size2>0){
*final_size=0;
int new_size=0;
config_str parsed_final = yon_char_parsed_new(final_size,array1[0],NULL);
for (int i=1;i<size1;i++){
parsed_final = yon_char_parsed_append(parsed_final,final_size,array1[i]);
}
for (int i=0;i<size2;i++){
int found=0;
for (int j=0;j<size1;j++){
if (!strcmp(array1[j],array2[i])){
found=1;
break;
}
}
if (!found)
parsed_final= yon_char_parsed_append(parsed_final,final_size,array2[i]);
}
return parsed_final;
}
}
int yon_char_parsed_divide_full(config_str parsed,int size,int divide_pos){
if(parsed&&size>0){
for (int i=0;i<size;i++){
char *parsed_temp = yon_char_divide(parsed[i],divide_pos);
free(parsed[i]);
parsed[i]=parsed_temp;
}
return 1;
} return 0;
}
int yon_char_parsed_divide_search_full(config_str parsed,int size,char *divide_pos, int delete_divider){
if(parsed&&size>0){
for (int i=0;i<size;i++){
char *parsed_temp = yon_char_divide_search(parsed[i],divide_pos,delete_divider);
free(parsed[i]);
parsed[i]=parsed_temp;
}
return 1;
} return 0;
}
dictionary *yon_char_parsed_convert_to_dictionary(config_str parsed, int size){
if (parsed&&size>0){
dictionary *dict = NULL;
for (int i=0;i<size;i++){
yon_dictionary_add_or_create_if_exists_with_data(dict,NULL,parsed[i]);
}
return dict;
}
return NULL;
}
dictionary *yon_char_parsed_convert_copy_to_dictionary(config_str parsed, int size){
if (parsed&&size>0){
dictionary *dict = NULL;
for (int i=0;i<size;i++){
yon_dictionary_add_or_create_if_exists_with_data(dict,NULL,yon_char_new(parsed[i]));
}
return dict;
}
return NULL;
}
char *yon_char_parsed_to_string(config_str parsed, int size, char *divider_replace){
if (parsed && size>0){
char *final_string = "";
char *temp;
for (int i=0;i<size;i++){
if (!yon_char_is_empty(parsed[i])){
temp = yon_char_unite(final_string,yon_char_is_empty(final_string)?"":divider_replace,parsed[i],NULL);
if (!yon_char_is_empty(final_string)) free(final_string);
final_string=temp;
}
}
if (!yon_char_is_empty(final_string)){
return final_string;
} else return NULL;
} else if (size==0&&!parsed) return "";
}
char *yon_char_parsed_to_string_for_iters(config_str parsed, int size, char *divider_replace,int iterations){
if (parsed && size>0&& iterations>0){
char *final_string = "";
char *temp;
for (int i=0;i<iterations;i++){
if (!yon_char_is_empty(parsed[i])){
temp = yon_char_unite(final_string,yon_char_is_empty(final_string)?"":divider_replace,parsed[i],NULL);
if (!yon_char_is_empty(final_string)) free(final_string);
final_string=temp;
}
}
if (!yon_char_is_empty(final_string)){
return final_string;
} else return NULL;
} else if (size==0&&!parsed) return "";
}
config_str yon_char_parsed_cut(config_str parsed, int size, int pos){
if (size&&pos&&parsed&&size>pos){
config_str new_parsed = malloc(sizeof(char*)*pos);
for (int i=0;i<size;i++){
if (i<pos){
new_parsed[i]=parsed[i];
}
}
return new_parsed;
}
return NULL;
}
int yon_char_parsed_find_element(config_str parsed, int size, char *target){
if (parsed&&size){
for (int i=0;i<size;i++){
if (strstr(parsed[i],target)){
return i;
}
}
return -1;
}
return -1;
}
int yon_char_find_last_symbol_before_length(char *string, char target, int length){
if (yon_char_is_empty(string)||strlen(string)<length||length<1) return -1;
int last = -1;
for (int i=0;i<length;i++){
if (string[i]==target){
last = i;
}
}
return last;
}
config_str yon_char_wrap_to_length(char *target, unsigned int length, int *size){
(*size)=0;
if (!yon_char_is_empty(target)&&strlen(target)>length){
config_str final = NULL;
char *current = yon_char_new(target);
for (;strlen(current)>length;){
int last = yon_char_find_last_symbol_before_length(target,' ',length);
if (last>-1){
char *parsed = yon_char_divide(current,last);
yon_char_parsed_add_or_create_if_exists(final,size,parsed);
free(parsed);
}
}
return final;
}
return NULL;
}
int yon_ubl_check_root(){
if (getuid()==0) return 1;
else return 0;
}
char *yon_ubl_root_user_get(){
char *user=NULL;
if (yon_ubl_check_root()){
user=getenv("SUDO_USER");
if (user&&strcmp(user,"")!=0){
return user;
}else {
FILE *file = popen("getent passwd $PKEXEC_UID | cut -d: -f1","r");
user=malloc(4096);
memset(user,0,4096);
if (fgets(user,4096,file)){};
user=yon_char_divide_search(user,"\n",-1);
fclose(file);
if (user) return user;
}
}
return getlogin();
}
char *yon_ubl_user_get_home_directory(){
FILE *path = popen(get_home_dir_command,"r");
char *ret = malloc(4096);
memset(ret,0,4096);
if (fgets(ret,4096,path)){};
ret=yon_char_divide_search(ret,"\n",-1);
fclose(path);
return ret;
}
config_str yon_ubl_get_all_users(int *user_size){
struct passwd *user = NULL;
config_str user_list = NULL;
while ((user = getpwent())) {
if (!user_list)
user_list = yon_char_parsed_new(user_size,user->pw_name,NULL);
else {
user_list = yon_char_parsed_append(user_list,user_size,user->pw_name);
}
}
endpwent();
return user_list;
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,397 @@
#include "libublsettings.h"
dictionary *yon_dictionary_new()
{
dictionary *dict = malloc(sizeof(dictionary));
dict->data = NULL;
dict->key = NULL;
dict->next = NULL;
dict->prev = NULL;
dict->first = dict;
dict->data_type = DICTIONARY_OTHER_TYPE;
return dict;
}
dictionary *yon_dictinoary_copy(dictionary *dict){
dictionary *dct = yon_dictionary_new_with_data(dict->key,dict->data);
dct->data_type= dict->data_type;
return dct;
}
dictionary *yon_dictionary_copy_deep(dictionary *dict){
dictionary *dct = NULL;
dictionary *newone=NULL;
for_dictionaries(dct,dict){
yon_dictionary_add_or_create_if_exists_with_data(newone,dct->key,dct->data);
newone->data_type=dct->data_type;
}
return newone->first;
}
int yon_dictionary_set_data(dictionary *dict, void *data){
dict->data=data;
}
int yon_dictionary_set_key(dictionary *dict, char *key){
dict->key=key;
return 1;
}
int yon_dictionary_set(dictionary *dict, char *key, void *data){
dict->key=key;
dict->data=data;
return 1;
}
int yon_dictionary_empty(dictionary *dict){
dict->data=NULL;
dict->data_type=DICTIONARY_OTHER_TYPE;
return 1;
}
void yon_dictionary_switch_to_last(dictionary **dict)
{
dictionary *dct=NULL, *dact=*dict;
for_dictionaries(dct,dact);
}
dictionary *yon_dictionary_append(dictionary *targetdict)
{
targetdict = yon_dictionary_get_last(targetdict);
targetdict->next = yon_dictionary_new();
targetdict->next->prev = targetdict;
targetdict->next->first = targetdict->first;
targetdict->next->data_type = DICTIONARY_OTHER_TYPE;
return targetdict->next;
}
dictionary *yon_dictionary_get_last(dictionary *dict)
{
if (dict->next){
dictionary *dct = NULL;
for_dictionaries(dct,dict)
if (!dct->next)
return dct;
} else return dict;
}
dictionary *yon_dictionary_swap(dictionary *dict, int aim)
{
if (aim < 0)
{
if (dict->prev)
{
if (dict->prev->prev)
{
dictionary *next = dict->next, *prev = dict->prev, *preprev = prev->prev;
if (next)
{
preprev->next = dict;
dict->prev = preprev;
dict->next = prev;
prev->prev = dict;
prev->next = next;
next->prev = prev;
}
else
{
preprev->next = dict;
dict->prev = preprev;
dict->next = prev;
prev->prev = dict;
prev->next = NULL;
}
return prev;
}
else
{
dictionary *next = dict->next, *prev = dict->prev;
if (next)
{
yon_dictionary_make_first(dict);
dict->prev = NULL;
dict->next = prev;
prev->prev = dict;
prev->next = next;
next->prev = prev;
}
else
{
dict->prev = NULL;
dict->next = prev;
prev->prev = dict;
prev->next = NULL;
}
return prev;
}
}
}
else if (aim > 0)
{
if (dict->next)
{
if (dict->next->next)
{
dictionary *next = dict->next, *prev = dict->prev, *afnext = next->next;
if (prev)
{
prev->next = next;
next->prev = prev;
next->next = dict;
dict->prev = next;
dict->next = afnext;
afnext->prev = dict;
}
else
{
yon_dictionary_make_first(next);
next->prev = NULL;
next->next = dict;
dict->prev = next;
dict->next = afnext;
afnext->prev = dict;
}
return next;
}
else
{
dictionary *next = dict->next, *prev = dict->prev;
if (prev)
{
prev->next = next;
next->prev = prev;
next->next = dict;
dict->prev = next;
dict->next = NULL;
}
else
{
next->prev = NULL;
next->next = dict;
dict->prev = next;
dict->next = NULL;
}
}
}
}
}
void yon_dictionary_make_first(dictionary *dict)
{
for (dictionary *dct = dict->first; dct != NULL; dct = dct->next)
{
dct->first = dict;
}
}
void yon_dictionary_make_nth(dictionary *dict, int nth)
{
dictionary *dct = dict->first;
for (int i = 0; i < nth; i++)
{
if (dct == NULL)
return;
else
dct = dct->next;
}
yon_dictionary_rip(dict);
dictionary *prev = dct->prev;
prev->next = dict;
dict->prev = prev;
dict->next = dct;
dct->prev = dict;
}
dictionary *yon_dictionary_new_with_data(char *key, void *data)
{
dictionary *dct = yon_dictionary_new();
dct->key = yon_char_new(key);
dct->data = data;
dct->data_type = DICTIONARY_OTHER_TYPE;
return dct;
}
void *yon_dictionary_free(dictionary *dictionary_to_free){
if (dictionary_to_free){
free(dictionary_to_free->data);
free(dictionary_to_free->key);
free(dictionary_to_free);
return NULL;
}
}
void *yon_dictionary_free_all(dictionary *dictionary_to_free,void (*data_manipulation)(void*)){
if (dictionary_to_free){
dictionary *dict=NULL;
for_dictionaries(dict,dictionary_to_free){
if(data_manipulation)
data_manipulation(dict->data);
if(dict->prev)
free(dict->prev);
}
free(dict);
return NULL;
}
}
dictionary *yon_dictionary_append_with_data(dictionary *dict, char *key, void *data)
{
dictionary *dct = yon_dictionary_append(dict);
dct->key = yon_char_new(key);
dct->data = data;
dct->data_type = DICTIONARY_OTHER_TYPE;
return dct;
}
dictionary *yon_dictionary_connect(dictionary *old, dictionary *toconnect)
{
dictionary *dict = yon_dictionary_get_last(old);
dict->next = toconnect;
toconnect->prev = dict;
toconnect->first = dict->first;
return toconnect;
}
dictionary *yon_dictionary_merge(dictionary *dict1,dictionary *dict2){
dictionary *dct = NULL;
for_dictionaries(dct,dict2){
if (!yon_dictionary_get(&dict1,dct->key))
yon_dictionary_connect(dict1,dct);
}
return dict1;
}
dictionary *yon_dictionary_get(dictionary **dict, char *key)
{
dictionary *dct = *dict;
if (dct){
for (dictionary *pointer = dct->first; pointer != NULL; pointer = pointer->next){
if (pointer->key&&strcmp(pointer->key, key) == 0){
*dict = pointer;
return pointer;
}
}
}
return NULL;
}
dictionary *yon_dictionary_rip(dictionary *dict)
{
if (!dict->next&&!dict->prev) {
free(dict);
return NULL;
}
else if (!dict->next)
{
dictionary *prev = dict->prev;
free(dict);
if (prev)
{
prev->next = NULL;
return prev;
}
}
else if (!dict->prev)
{
dictionary *next = dict->next;
if (next)
{
yon_dictionary_make_first(next);
next->prev = NULL;
free(dict);
return next;
}
}
else
{
dictionary *next = dict->next, *prev = dict->prev;
next->prev = prev;
prev->next = next;
free(dict);
return next;
}
}
dictionary *yon_dictionary_get_nth(dictionary *dict, int place)
{
if (dict){
dict = dict->first;
int i = 0;
for (i = 0; i < place; i++)
if (dict->next)
dict = dict->next;
else
break;
if (i == place)
return dict;
else
return NULL;
} else return NULL;
}
dictionary *yon_dictionary_sort(dictionary *dict){
dictionary *first = dict->first;
dictionary *prev = first;
dictionary *current = first->next;
while (current){
if (current!=prev&&strcmp(current->key,prev->key)<0){
if (current->next)
current->next->prev=prev;
prev->next=current->next;
current->next=prev;
current->prev=prev->prev;
if (prev->prev)
prev->prev->next=current;
prev->prev=current;
if (prev==prev->first){
yon_dictionary_make_first(current);
}
dictionary *temp = prev;
prev = current;
current=temp;
}
prev=prev->next;
current = current->next;
}
return first;
}
int _yon_dictionary_check_loops(dictionary *_REG_DICT,dictionary *link){
if (_REG_DICT){
dictionary *dict;
for_dictionaries(dict,_REG_DICT){
if (dict->data==link){
return 1;
}
}
}
return 0;
}
dictionary *yon_dictionary_check_loops(dictionary *target){
dictionary *found = NULL;
dictionary *dict;
for_dictionaries(dict,target){
if (_yon_dictionary_check_loops(found,dict)){
return dict;
} else {
yon_dictionary_add_or_create_if_exists_with_data(found,NULL,dict);
}
}
yon_dictionary_free_all(found,NULL);
found = NULL;
dict=yon_dictionary_get_last(target);
for(;dict;dict=dict->prev){
if (_yon_dictionary_check_loops(found,dict)){
return dict;
} else {
yon_dictionary_add_or_create_if_exists_with_data(found,NULL,dict);
}
}
yon_dictionary_free_all(found,NULL);
return NULL;
}

@ -0,0 +1,174 @@
#include "libublsettings.h"
char *yon_file_path_proceed_spaces(char *path){
int size;
config_str parsed = yon_char_parse(path,&size,"/");
for (int i=0;i<size;i++){
if (!yon_char_is_empty(parsed[i])){
if (strstr(parsed[i]," ")){
char *temp = yon_char_unite("/",parsed[i],"/",NULL);;
free(parsed[i]);
parsed[i]=temp;
}
}
}
char *final = yon_char_parsed_to_string(parsed,size,"/");
yon_char_parsed_free(parsed,size);
char *temp = NULL;
if (path[0]=='/') temp = yon_char_append("/",final);
free(final);
final=temp;
return final;
}
config_str yon_file_open(char *file_path, int *size){
*size=0;
FILE *file = fopen(file_path,"r");
if (file){
char str_loaded[4098];
config_str final_string = NULL;
while (fgets(str_loaded,4098,file)){
final_string = final_string ? yon_char_parsed_append(final_string,size,str_loaded) : yon_char_parsed_new(size,str_loaded,NULL);
}
fclose(file);
return final_string;
}
}
int yon_file_save(char *file_path, char *text){
FILE *file = fopen(file_path,"w");
struct passwd *user = getpwnam(yon_ubl_root_user_get());
if (chown(file_path,user->pw_uid,user->pw_gid)){};
if (chmod(file_path,0755)){};
if (file){
fputs(text,file);
fclose(file);
return 1;
}
return 0;
}
int yon_file_create(char *path, char *name, int rules){
if (path&&name){
char *full_path = yon_char_unite(path,"/",name,NULL);
if (access(full_path,F_OK)){
FILE *fl = fopen(full_path,"w");
if (fl){
struct passwd *user = getpwnam(yon_ubl_root_user_get());
if (chown(path,user->pw_uid,user->pw_gid)){};
if (chmod(path,0755)){};
fclose(fl);
return 1;
} else {
return 0;
}
} else {
return -1;
}
} else {
return 0;
}
}
int yon_file_create_full_path(char *path, mode_t rules){
if (path){
if (access(path,F_OK)){
int size;
config_str parsed = yon_char_parse(path,&size,"/");
char *current = "";
for (int i=0;i<size;i++){
char *temp = yon_char_unite(current,strcmp(current,"/")?"/":"",parsed[i],NULL);
if (!yon_char_is_empty(temp)){
if (access(temp,F_OK)){
if (i!=size-1){
mkdir(temp,0755);
struct passwd *user = getpwnam(yon_ubl_root_user_get());
} else {
FILE *fl = fopen(temp,"w");
fclose(fl);
}
}
if (i>2){
struct passwd *user = getpwnam(yon_ubl_root_user_get());
int chown_success = chown(temp,user->pw_uid,user->pw_gid);
int chmod_success = chmod(temp,0755);
}
}
if (!yon_char_is_empty(current)) free(current);
current = temp;
}
return 1;
} else {
return 0;
}
}
return 0;
}
config_str yon_file_list_dirs (char *path, int *size){
(*size)=0;
if (!yon_char_is_empty(path)&&!access(path,F_OK)){
config_str final = NULL;
DIR *current_dir = opendir(path);
struct dirent* cur_dir;
while ((cur_dir=readdir(current_dir))){
if (cur_dir->d_type==DT_DIR &&(strcmp(cur_dir->d_name, ".") && strcmp(cur_dir->d_name, ".."))){
yon_char_parsed_add_or_create_if_exists(final,size,cur_dir->d_name);
}
}
closedir(current_dir);
return final;
}
return NULL;
}
config_str yon_file_ls(char *path, int *size){
(*size)=0;
if (!yon_char_is_empty(path)&&!access(path,F_OK)){
config_str final = NULL;
DIR *current_dir = opendir(path);
struct dirent* cur_dir;
while ((cur_dir=readdir(current_dir))){
if ((strcmp(cur_dir->d_name, ".") && strcmp(cur_dir->d_name, ".."))){
yon_char_parsed_add_or_create_if_exists(final,size,cur_dir->d_name);
}
}
closedir(current_dir);
return final;
}
return NULL;
}
int yon_file_is_directory(const char *path)
{
struct stat path_stat;
stat(path, &path_stat);
return S_ISDIR(path_stat.st_mode);
}
void yon_dir_remove(const char *path){
DIR *d = opendir(path);
struct dirent *dir;
if (d) {
while ((dir = readdir(d)) != NULL) {
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) {
continue;
}
char full_path[1024];
snprintf(full_path, sizeof(full_path), "%s/%s", path, dir->d_name);
if (dir->d_type == DT_DIR) {
yon_dir_remove(full_path);
} else {
unlink(full_path);
}
}
closedir(d);
rmdir(path);
}
}

@ -0,0 +1,131 @@
#include "libublsettings.h"
// typedef struct yon_trash_collector_element{
// char *key;
// void *data;
// struct yon_trash_collector_element *next;
// struct yon_trash_collector_element *prev;
// struct yon_trash_collector_element *first;
// } yon_trash_collector_element;
// typedef struct yon_trash_collector_group{
// char *group_key;
// yon_trash_collector_element *data;
// struct yon_trash_collector_group *next;
// struct yon_trash_collector_group *prev;
// struct yon_trash_collector_group *first;
// } yon_trash_collector_group;
// yon_trash_collector_group *__yon_trash_collector__ = NULL;
// #define check_trash if (__yon_trash_collector__)
// #define trash_group(group_key) yon_dictionary_get(&__yon_trash_collector__,group_key)
// #define for_trash_group for(yon_trash_collector_group *gr = __yon_trash_collector__->first;gr;gr=gr->next)
// #define for_trash_element(group) for (yon_trash_collector_element *gr = group->first; gr;gr=gr->next)
// #define yon_malloc_reg(size,key,group) yon_var_create_register(size,key,group)
// yon_trash_collector_group *yon_trash_collector_group_new(){
// yon_trash_collector_group *group = malloc(sizeof(yon_trash_collector_group));
// group->data=NULL;
// group->first=group;
// group->group_key=NULL;
// group->next=NULL;
// group->prev=NULL;
// return group;
// }
// int yon_trash_collector_element_register(yon_trash_collector_element *element,char *group){
// if (!element||yon_char_is_empty(element->key)||yon_char_is_empty(group))
// return 0;
// check_trash{
// if (yon_dictionary_get((dictionary**)&__yon_trash_collector__,group)){
// if (yon_dictionary_get((dictionary**)&__yon_trash_collector__->data,element->key));
// } else {
// yon_trash_collector_group *last = (yon_trash_collector_group*)yon_dictionary_get_last((dictionary*)__yon_trash_collector__);
// __yon_trash_collector__->next = yon_trash_collector_group_new();
// __yon_trash_collector__->next->prev=__yon_trash_collector__;
// __yon_trash_collector__->next->first=__yon_trash_collector__->first;
// __yon_trash_collector__->next->data=element;
// }
// } else {
// __yon_trash_collector__ = yon_trash_collector_group_new();
// __yon_trash_collector__->data=element;
// }
// return 1;
// }
// void *yon_var_create_register(int size,char *key,char*group){
// void *allocated = malloc(size);
// }
// yon_trash_collector_element *yon_trash_collector_element_get(void *pointer,yon_trash_collector_group *group){
// for_trash_element(group){
// if (gr->data==pointer)
// return gr;
// }
// }
// int yon_trash_collector_group_has(char *group_key, void *pointer){
// yon_trash_collector_group *group = trash_group(group_key);
// yon_trash_collector_element *element = yon_trash_collector_element_get(pointer,&group);
// if (element)
// return 1;
// }
// yon_trash_collector_group *yon_trash_collector_shell_new(char *group_key){
// yon_trash_collector_group *trash = malloc(sizeof(yon_trash_collector_group));
// trash->group_key=yon_char_new(group_key);
// trash->first=NULL;
// trash->next=NULL;
// trash->prev=NULL;
// return trash;
// }
// yon_trash_collector_group *yon_trash_collector_new(int size, char *group_key){
// yon_trash_collector_group *trash = yon_trash_collector_shell_new(group_key);
// trash->data=malloc(size);
// return trash;
// }
// int yon_trash_collector_append(void *pointer, char group_key){
// if (yon_trash_collector_group_has(group_key,pointer)){
// }
// check_trash{
// yon_trash_collector_group *trash = yon_trash_collector_shell_new(group_key);
// trash->data=pointer;
// yon_dictionary_connect((dictionary*)__yon_trash_collector__,(dictionary*)trash);
// return 1;
// } else {
// __yon_trash_collector__ = yon_trash_collector_shell_new(group_key);
// __yon_trash_collector__->data=pointer;
// return 1;
// }
// }
// int yon_trash_collector_free(char *group_key){
// }
// void *yon_malloc(int size, char *group_key){
// void * data = yon_trash_collector_new(size,group_key)->data;
// return data;
// }
// void *yon_malloc0(int size, char *group_key){
// void * data = yon_trash_collector_new(size,group_key)->data;
// memset(data,0,size);
// return data;
// }
// void *yon_realloc(void *pointer, int size, char *group_key){
// void * data = yon_trash_collector_new(size,group_key)->data;
// memset(data,0,size);
// return data;
// }

File diff suppressed because it is too large Load Diff

@ -579,6 +579,8 @@ char *yon_char_parsed_to_string_for_iters(config_str parsed, int size, char *div
*/
config_str yon_char_parsed_cut(config_str parsed, int size, int pos);
int yon_char_parsed_find_element(config_str parsed, int size, char *target);
int yon_char_find_last_symbol_before_length(char *string, char target, int length);
/**yon_char_wrap_to_length(char *target, unsigned int length, int *size)
@ -625,6 +627,8 @@ config_str yon_ubl_get_all_users(int *user_size);
// parsing functions
int yon_password_get_min_len();
/** yon_size_convert_automatic(int bytes, int *size)
* [EN]
*

Loading…
Cancel
Save