#include "libublsettings.h" // dictionary functions 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; } // char functions 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_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; } 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;ispaces+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&÷pos){ 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 = 1; float convert_check = (float)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, "%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)){ 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)); memcpy(temp,source,strlen(source)-strlen(final)); temp=yon_char_append(temp,replace); source=yon_char_append(temp,final+strlen(find)); } } } return source; } 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){ for (int i=0;i0){ for (int i=0;i0){ int final_size=0; config_str final = yon_char_parsed_new(&final_size,to_copy[0],NULL); for (int i=1;i0 && !yon_char_is_empty(prepend)){ for (int i=0;i0 && !yon_char_is_empty(prepend)){ for (int i=0;i0&&size2>0){ *final_size=0; config_str parsed_final = yon_char_parsed_new(final_size,array1[0],NULL); for (int i=1;i0&&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;i0){ for (int i=0;i0){ for (int i=0;i0){ dictionary *dict = NULL; for (int i=0;i0){ dictionary *dict = NULL; for (int i=0;i0){ char *final_string = ""; char *temp; for (int i=0;i0&& iterations>0){ char *final_string = ""; char *temp; for (int i=0;ipos){ config_str new_parsed = malloc(sizeof(char*)*pos); for (int i=0;ilength){ 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; } // parsing functions float yon_size_convert_automatic(int bytes, int *size){ float byte_float=bytes; for (*size=-1;byte_float>1024;*size=*size+1){ byte_float=byte_float/1024; } if (*size==-1) { *size=0; byte_float=byte_float/1024; } return byte_float; } int yon_get_size_get_from_letter(char size){ switch (size){ case 'K': return 0; break; case 'M': return 1; break; case 'G': return 2; break; case 'T': return 0; break; default: return -1; break; } } char *yon_size_get_mod(int size){ char *size_modifier = NULL; switch (size){ case 0: size_modifier = "K"; break; case 1: size_modifier = "M"; break; case 2: size_modifier = "G"; break; case 3: size_modifier = "T"; break; } return size_modifier; } config_str yon_dir_get_contents(char *dir_path, int *size){ config_str dir = NULL; *size=0; if (!access(dir_path,F_OK)){ DIR *directory = opendir(dir_path); struct dirent *de; while ((de = readdir(directory))){ 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); } return dir; } config_str yon_dir_get_by_mask(char *path, char *mask, int *size){ (*size)=0; if (yon_char_count(mask,"*")<=1){ char *lpart; char *rpart=yon_char_new(mask); 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); if(found_size>0){ int found = 1; int rfound = 0; for (int i=0;idata=data; param->data_type=DICTIONARY_CHAR_TYPE; param->first=param; param->flag1=1; param->key=yon_char_new(key); param->next=NULL; param->prev=NULL; param->section=NULL; param->ignore=0; param->save_command=NULL; param->load_command=NULL; return param; } yon_config_parameter *yon_config_parameter_append_with_data(yon_config_parameter *dict, char *key, void *data){ yon_config_parameter *param = yon_config_parameter_new_with_data(key,data); param->first=dict->first; dict = (yon_config_parameter*)yon_dictionary_get_last((dictionary*)dict); (param->prev)=dict; dict->next=param; return param; } static yon_config_parameter *__yon__config__strings = NULL; static yon_config_parameter *__yon__config__default__strings = NULL; dictionary *__yon_config_ignored = NULL; #define check_config if(__yon__config__strings&&__yon__config__strings->data_type==DICTIONARY_CHAR_TYPE) #define check_default_config if(__yon__config__default__strings) #define for_config yon_config_parameter *temp = NULL; for_dictionaries(temp,__yon__config__strings) #define for_default_config dictionary *temp = NULL; for_dictionaries(temp,(dictionary*)__yon__config__default__strings) #define yon_config_parameter_add_or_create_if_exists_with_data(dict,key,value) {if (dict){\ yon_config_parameter *dct = (yon_config_parameter *)yon_dictionary_get((dictionary**)&dict,key);\ if (dct) {\ dict=dct;\ dict->data=value;\ }\ else{\ dict=yon_config_parameter_append_with_data(dict,key,value);\ }\ }\ else dict=yon_config_parameter_new_with_data(key,value);} char *yon_config_get_type_path(YON_CONFIG_TYPE type){ switch (type){ case YON_CONFIG_DEFAULT: return "default"; break; case YON_CONFIG_BOTH: return ""; break; case YON_CONFIG_GLOBAL: return "global"; break; case YON_CONFIG_LOCAL: return "system"; break; } return NULL; } char *yon_config_get_all_info(){ check_config{ int size; config_str parsed = yon_config_get_all(&size); char *full = yon_char_parsed_to_string(parsed,size,"\n"); yon_char_parsed_free(parsed,size); return full; } return NULL; } void yon_config_parameter_set_load_command(char *key, char *command){ dictionary *found = yon_dictionary_get((dictionary**)&__yon__config__strings,key); if (found){ ((yon_config_parameter*)found)->load_command=yon_char_new(command); } } void yon_config_parameter_set_save_command(char *key, char *command){ dictionary *found = yon_dictionary_get((dictionary**)&__yon__config__strings,key); if (found){ ((yon_config_parameter*)found)->save_command=yon_char_new(command); } } char *yon_config_parameter_get_load_command(char *key){ dictionary *found = yon_dictionary_get((dictionary**)&__yon__config__strings,key); if (found){ return ((yon_config_parameter*)found)->load_command; } return NULL; } char *yon_config_parameter_get_save_command(char *key){ dictionary *found = yon_dictionary_get((dictionary**)&__yon__config__strings,key); if (found){ return ((yon_config_parameter*)found)->save_command; } return NULL; } int yon_config_set_ignore(char *key){ if (!yon_dictionary_get(&__yon_config_ignored,key)){ yon_dictionary_add_or_create_if_exists_with_data(__yon_config_ignored,key,NULL); } } int yon_config_remove_ignore(char *key){ dictionary *dict = yon_dictionary_get(&__yon_config_ignored,key); if (dict) { __yon_config_ignored=yon_dictionary_rip(dict); } } int yon_config_get_status(char *key){ dictionary *dict; if ((dict=yon_dictionary_get((dictionary**)&__yon__config__strings,key))){ return ((yon_config_parameter*)dict)->flag1; } else return 0; } int yon_config_check_ignore(char *key){ dictionary *dict = yon_dictionary_get(&__yon_config_ignored,key); if (dict) return 1; else return 0; } int yon_config_load_register_no_cleaning(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...){ if (config_type!=YON_CONFIG_BOTH){ va_list args; va_start(args,parameter); dictionary *sections = NULL; yon_dictionary_add_or_create_if_exists_with_data(sections,section,parameter); char *arg; while (arg=va_arg(args,char*)){ char *key = va_arg(args,char*); if (sections&&yon_dictionary_get(§ions,arg)) sections->data=(void*)yon_char_unite(yon_dictionary_get_data(sections,char*)," ",key,NULL); else yon_dictionary_add_or_create_if_exists_with_data(sections,arg,key); } char *command=NULL; dictionary *dict; for_dictionaries(dict,sections){ command = yon_char_unite(ubconfig_load_command_old,config_type==YON_CONFIG_GLOBAL ? " global get " : config_type==YON_CONFIG_LOCAL ? " system get " : " default get", dict->key," ", yon_dictionary_get_data(dict,char*),NULL); FILE *output = popen(command, "r"); int i = 0; char str[4096]; memset(str, 0, 4096); while (fgets(str, 4096, output)) { if (strcmp(str, "") != 0&& strcmp(str,"(null)\n")!=0) { char *key = yon_char_divide_search(str,"=",-1); char *final_str=yon_char_divide_search(str,"\n",-1); if ((final_str[0]=='\''&&final_str[strlen(final_str)-1]=='\'')||(final_str[0]=='\"'&&final_str[strlen(final_str)-1]=='\"')){ final_str[strlen(final_str)-1] = '\0'; free(yon_char_divide(final_str,0)); } yon_config_parameter *cur_config = (config_type!=YON_CONFIG_DEFAULT? __yon__config__strings:__yon__config__default__strings); dictionary *dicte = yon_dictionary_get((dictionary**)&cur_config,key); if (!dicte){ yon_config_parameter_add_or_create_if_exists_with_data(cur_config,key,yon_char_new(final_str)); } else { if (strcmp((char*)dicte->data,final_str)){ cur_config->data=final_str; } else { continue; } } cur_config->flag1=0; yon_config_remove_ignore(key); if (config_type==YON_CONFIG_DEFAULT){ cur_config->flag1=-2; yon_config_set_ignore(key); } cur_config->data_type=DICTIONARY_CHAR_TYPE; cur_config->section=dict->key; } } free(command); fclose(output); } check_config return 1; else return 0; } else return -1; } int yon_config_parse_parameter(char *parameter,char **key, char **value){ (*key)=NULL; (*value)=NULL; check_config{ char *copied = yon_char_new(parameter); char *key_temp = yon_char_divide_search(copied,"=",-1); if (key_temp!=parameter){ char *final_str=yon_char_divide_search(copied,"\n",-1); if ((final_str[0]=='\''&&final_str[strlen(final_str)-1]=='\'')||(final_str[0]=='\"'&&final_str[strlen(final_str)-1]=='\"')){ final_str[strlen(final_str)-1] = '\0'; free(yon_char_divide(final_str,0)); } (*key)=key_temp; (*value)=final_str; return 1; } } return 0; } int yon_char_remove_brackets(char *string){ int done=0; if (strlen(string)>2){ if (string[0]=='\''||string[0]=='\"'){ done=1; free(yon_char_divide(string,0));} if (string[strlen(string)-1]=='\''|| string[strlen(string)-1]=='\"') { done=1; string[strlen(string)-1] = '\0'; } } return done; } char *yon_config_replace_parameter(char *command, char *parameter, int place){ if (!yon_char_is_empty(command)); int size=0; config_str parsed = yon_char_parse(command,&size," "); if (placeget_place+2){ for (int j=get_place+2;j0){ for (int j=0;jdata=(void*)yon_char_unite(yon_dictionary_get_data(sections,char*)," ",key,NULL); else yon_dictionary_add_or_create_if_exists_with_data(sections,arg,key); } char *command=NULL; dictionary *dict; for_dictionaries(dict,sections){ command = yon_char_unite(ubconfig_load_command_old,config_type==YON_CONFIG_GLOBAL ? " global get " : config_type==YON_CONFIG_LOCAL ? " system get " : " default get", dict->key," ", yon_dictionary_get_data(dict,char*),NULL); FILE *output = popen(command, "r"); int i = 0; char str[4096]; memset(str, 0, 4096); while (fgets(str, 4096, output)) { if (strcmp(str, "") != 0&& strcmp(str,"(null)\n")!=0) { char *key = yon_char_divide_search(str,"=",-1); char *final_str=yon_char_divide_search(str,"\n",-1); if ((final_str[0]=='\''&&final_str[strlen(final_str)-1]=='\'')||(final_str[0]=='\"'&&final_str[strlen(final_str)-1]=='\"')){ final_str[strlen(final_str)-1] = '\0'; free(yon_char_divide(final_str,0)); } yon_config_parameter *cur_config = (config_type!=YON_CONFIG_DEFAULT? __yon__config__strings:__yon__config__default__strings); dictionary *dicte = yon_dictionary_get((dictionary**)&cur_config,key); if (!dicte){ yon_config_parameter_add_or_create_if_exists_with_data(cur_config,key,yon_char_new(final_str)); } else { if (strcmp((char*)dicte->data,final_str)){ cur_config->data=final_str; } else { continue; } } cur_config->flag1=0; yon_config_remove_ignore(key); if (config_type==YON_CONFIG_DEFAULT){ cur_config->flag1=-2; yon_config_set_ignore(key); } cur_config->data_type=DICTIONARY_CHAR_TYPE; cur_config->section=dict->key; } } free(command); fclose(output); } check_config return 1; else return 0; } else return -1; } int yon_config_remove_by_key(char *key){ check_config{ dictionary *dict = yon_dictionary_get((dictionary**)&__yon__config__strings,key); if (dict){ if (!yon_dictionary_get(&__yon_config_ignored,dict->key)){ ((yon_config_parameter*)dict)->flag1=-1; dict->data=""; return 1; } else return 0; }else return 0; } return 0; } int yon_config_remove_element(char *key, char *delete_target, char *divider){ check_config{ int found = 0; yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key); if (dict){ int size; config_str parsed = yon_char_parse((char*)dict->data,&size,divider); config_str final = NULL; for(int i=0;i0) final_string = yon_char_parsed_to_string(parsed,size,divider); else { final_string = malloc(sizeof(char)*1); final_string[0]='\0'; } if (final_string){ free(dict->data); dict->data=final_string; return 1; } else return 0; } } else return 0; } int yon_config_append_element(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){ yon_char_unite((char*)dict->data,divider,append,NULL); return 1; } } else return 0; } int yon_config_set_status(char *key, int status){ check_config{ if (!yon_char_is_empty(key)){ if(yon_dictionary_get((dictionary**)&__yon__config__strings,key)){ ((yon_config_parameter*)__yon__config__strings)->flag1=status; return 1; } } } return 0; } void yon_config_set_status_full(int status){ check_config{ for_config{ ((yon_config_parameter*)temp)->flag1=status; } } } void *yon_config_get_by_key(char *key){ check_config{ dictionary *dict = NULL; for_dictionaries(dict, (dictionary*)__yon__config__strings){ if (strcmp(dict->key,key)==0&&((yon_config_parameter*)dict)->flag1!=-1){ return dict->data; } } } return NULL; } char *yon_config_get_section_for_key(char *key){ check_config{ for_config{ if (!yon_char_is_empty(temp->key)){ if (!strcmp(temp->key,key)){ return yon_char_new(((yon_config_parameter*)temp)->section); } } } } } void *yon_config_get_all_by_key(char *key, int *size){ (*size)=0; check_config{ config_str ret_data=NULL; dictionary *dict = NULL; for_dictionaries(dict, (dictionary*)__yon__config__strings){ if (strstr(dict->key,key)&&((yon_config_parameter*)dict)->flag1!=-1) { char *ret_string = yon_char_unite(dict->key,"=",(char*)dict->data,NULL); if (ret_data) ret_data = yon_char_parsed_append(ret_data,size,ret_string); else ret_data = yon_char_parsed_new(size,ret_string,NULL); } } return ret_data; } return NULL; } config_str yon_config_get_selection_by_key(int *size, ...){ (*size)=0; check_config{ va_list list; va_start(list,size); config_str ret_data=NULL; char *arg = NULL; while ((arg = va_arg(list,char*))){ char *cur = yon_config_get_by_key(arg); char *parameter_string=NULL; if (!yon_char_is_empty(cur)){ parameter_string = yon_char_unite(arg,"='",cur,"'",NULL); yon_char_parsed_add_or_create_if_exists(ret_data,size,parameter_string); free(parameter_string); } } return ret_data; } return NULL; } config_str yon_config_get_selection_by_key_no_ignored(int *size, ...){ (*size)=0; check_config{ va_list list; va_start(list,size); config_str ret_data=NULL; char *arg = NULL; while ((arg = va_arg(list,char*))){ char *cur = yon_config_get_by_key(arg); char *parameter_string=NULL; if (!yon_char_is_empty(cur)&&!yon_config_check_ignore(cur)){ parameter_string = yon_char_unite(arg,"='",cur,"'",NULL); yon_char_parsed_add_or_create_if_exists(ret_data,size,parameter_string); free(parameter_string); } } return ret_data; } return NULL; } void *yon_config_get_all_by_key_no_ignored(char *key, int *size){ check_config{ (*size)=0; config_str ret_data=NULL; dictionary *dict = NULL; for_dictionaries(dict, (dictionary*)__yon__config__strings){ if (strstr(dict->key,key)&&((yon_config_parameter*)dict)->flag1!=-1&&yon_config_check_ignore(dict->key)==0) { char *ret_string = yon_char_unite(dict->key,"=",(char*)dict->data,NULL); if (ret_data) ret_data = yon_char_parsed_append(ret_data,size,ret_string); else ret_data = yon_char_parsed_new(size,ret_string,NULL); } } return ret_data; } } config_str yon_config_get_all_keys(int *size){ check_config{ *size=0; config_str final = NULL; for_config{ if (!final) final = yon_char_parsed_new(size,temp->key,NULL); else final = yon_char_parsed_append(final,size,temp->key); } return final; } return NULL; } config_str yon_config_get_all_keys_no_ignored(int *size){ check_config{ *size=0; config_str final = NULL; for_config{ if (!yon_config_check_ignore(temp->key)){ if (!final) final = yon_char_parsed_new(size,temp->key,NULL); else final = yon_char_parsed_append(final,size,temp->key); } } return final; } return NULL; } int yon_config_set(char *key, void *data){ check_config{ yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key); dict->data=data; dict->flag1=1; if (yon_dictionary_get(&__yon_config_ignored, dict->key)){ __yon_config_ignored = yon_dictionary_rip(__yon_config_ignored); } return 1; } else return 0; } int yon_config_append(char *key, char *data, char *divider){ check_config{ yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key); if (dict){ if (strcmp(((char*)dict->data),"")!=0){ char *str = (char*)dict->data; dict->data=(void*)(yon_char_unite(yon_char_is_empty(str)?"":str,yon_char_is_empty(str)?"":divider,data,NULL)); } else dict->data=(void*)yon_char_new(data); dict->flag1=1; return 1; } else return 0; } else return 0; } int yon_config_clean(){ check_config{ __yon__config__strings = (yon_config_parameter*)yon_dictionary_free_all((dictionary*)__yon__config__strings, NULL); return 1; } else return 0; } void yon_config_register(char *key, char *config_load, void *data){ if (!yon_char_is_empty(key)){ key=yon_char_new(key); config_load=yon_char_new(config_load); yon_config_parameter *current = NULL; if (data){ if (!__yon__config__strings||!(current=(yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key))){ { yon_config_parameter *cur_default = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__default__strings,key); if ((cur_default&&cur_default->data&&!strcmp((char*)cur_default->data,(char*)data))){ yon_config_remove_by_key(current->key); return; } if (__yon__config__strings){ char *data_copy = yon_char_new(data); yon_config_parameter* dict=NULL; for_dictionaries(dict,__yon__config__strings){ if (!dict->next){ __yon__config__strings=dict; } } __yon__config__strings=yon_config_parameter_append_with_data(__yon__config__strings,key,data_copy); } else { char *data_copy = yon_char_new(data); __yon__config__strings=yon_config_parameter_new_with_data(key,yon_char_new(data)); } } __yon__config__strings = (yon_config_parameter*)yon_dictionary_get_last((dictionary*)__yon__config__strings); __yon__config__strings->flag1=1; __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; __yon__config__strings->load_command=config_load; int size=0; config_str section = yon_char_parse(config_load,&size," "); __yon__config__strings->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } else if ((current = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key))){ current->data=yon_char_new(data); current->flag1=1; current->data_type=DICTIONARY_CHAR_TYPE; current->load_command=config_load; if (yon_dictionary_get(&__yon_config_ignored, current->key)){ __yon_config_ignored = yon_dictionary_rip(__yon_config_ignored); } int size=0; config_str section = yon_char_parse(config_load,&size," "); current->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } } else if ((current = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key))) { current->data=NULL; current->flag1=0; current->data_type=DICTIONARY_CHAR_TYPE; current->load_command=config_load; int size=0; config_str section = yon_char_parse(config_load,&size," "); current->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } else { if (!__yon__config__strings||!(current=(yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key))){ { if (__yon__config__strings){ yon_config_parameter* dict=NULL; for_dictionaries(dict,__yon__config__strings){ if (!dict->next){ __yon__config__strings=dict; } } __yon__config__strings=yon_config_parameter_append_with_data(__yon__config__strings,key,NULL); } else { __yon__config__strings=yon_config_parameter_new_with_data(key,NULL); } } yon_config_set_ignore(key); __yon__config__strings = (yon_config_parameter*)yon_dictionary_get_last((dictionary*)__yon__config__strings); __yon__config__strings->flag1=0; __yon__config__strings->data_type=DICTIONARY_CHAR_TYPE; __yon__config__strings->load_command=config_load; int size=0; config_str section = yon_char_parse(config_load,&size," "); __yon__config__strings->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } else if ((current = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key))){ if ((current->data&&data!=__yon__config__strings->data&&strcmp(__yon__config__strings->data,data))||!current->data){ yon_config_set_ignore(key); current->data=NULL; current->flag1=0; current->data_type=DICTIONARY_CHAR_TYPE; current->load_command=config_load; if (yon_dictionary_get(&__yon_config_ignored, current->key)){ __yon_config_ignored= yon_dictionary_rip(__yon_config_ignored); } int size=0; config_str section = yon_char_parse(config_load,&size," "); current->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } } } } } void yon_config_register_default(char *key, char *config_load, void *data){ if (!yon_char_is_empty(key)){ key=yon_char_new(key); config_load=yon_char_new(config_load); yon_config_parameter *current = NULL; if (data){ if (!__yon__config__default__strings||!(current=(yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__default__strings,key))){ { if (__yon__config__default__strings){ char *data_copy = yon_char_new(data); yon_config_parameter* dict=NULL; for_dictionaries(dict,__yon__config__default__strings){ if (!dict->next){ __yon__config__default__strings=dict; } } __yon__config__default__strings=yon_config_parameter_append_with_data(__yon__config__default__strings,key,data_copy); } else { char *data_copy = yon_char_new(data); __yon__config__strings=yon_config_parameter_new_with_data(key,yon_char_new(data)); } } __yon__config__default__strings = (yon_config_parameter*)yon_dictionary_get_last((dictionary*)__yon__config__default__strings); __yon__config__default__strings->flag1=1; __yon__config__default__strings->data_type=DICTIONARY_CHAR_TYPE; __yon__config__default__strings->load_command=config_load; int size=0; config_str section = yon_char_parse(config_load,&size," "); __yon__config__default__strings->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } else if ((current = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__default__strings,key))){ yon_config_parameter *cur_default = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__default__strings,current->key); if ((current->data&&data!=__yon__config__default__strings->data&&strcmp(__yon__config__default__strings->data,data))||!current->data){ current->data=yon_char_new(data); current->flag1=1; current->data_type=DICTIONARY_CHAR_TYPE; current->load_command=config_load; if (yon_dictionary_get(&__yon_config_ignored, current->key)){ __yon_config_ignored = yon_dictionary_rip(__yon_config_ignored); } int size=0; config_str section = yon_char_parse(config_load,&size," "); current->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); if ((cur_default&&strcmp((char*)cur_default->data,(char*)current->data))){ yon_config_remove_by_key(current->key); } } } } else if ((current = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__default__strings,key))) { current->data=NULL; current->flag1=0; current->data_type=DICTIONARY_CHAR_TYPE; current->load_command=config_load; int size=0; config_str section = yon_char_parse(config_load,&size," "); current->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } else { if (!__yon__config__default__strings||!(current=(yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__default__strings,key))){ { if (__yon__config__default__strings){ yon_config_parameter* dict=NULL; for_dictionaries(dict,__yon__config__default__strings){ if (!dict->next){ __yon__config__default__strings=dict; } } __yon__config__default__strings=yon_config_parameter_append_with_data(__yon__config__default__strings,key,NULL); } else { __yon__config__default__strings=yon_config_parameter_new_with_data(key,NULL); } } yon_config_set_ignore(key); __yon__config__default__strings = (yon_config_parameter*)yon_dictionary_get_last((dictionary*)__yon__config__default__strings); __yon__config__default__strings->flag1=0; __yon__config__default__strings->data_type=DICTIONARY_CHAR_TYPE; __yon__config__default__strings->load_command=config_load; int size=0; config_str section = yon_char_parse(config_load,&size," "); __yon__config__default__strings->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } else if ((current = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__default__strings,key))){ if ((current->data&&data!=__yon__config__default__strings->data&&strcmp(__yon__config__default__strings->data,data))||!current->data){ yon_config_set_ignore(key); current->data=NULL; current->flag1=0; current->data_type=DICTIONARY_CHAR_TYPE; current->load_command=config_load; if (yon_dictionary_get(&__yon_config_ignored, current->key)){ __yon_config_ignored= yon_dictionary_rip(__yon_config_ignored); } int size=0; config_str section = yon_char_parse(config_load,&size," "); current->section=yon_char_new(section[yon_char_parsed_check_exist(section,size,"get")+1]); yon_char_parsed_free(section,size); } } } } } config_str yon_config_load(char *command, int *str_len){ FILE *output = popen(command, "r"); char **output_strings = NULL; output_strings = malloc(sizeof(char)); int i = 0; char str[4096]; memset(str, 0, 4096); while (fgets(str, 4096, output)) { if (strcmp(str, "") != 0) { output_strings = realloc(output_strings, sizeof(char *) * (i + 1)); output_strings[i] = NULL; output_strings[i] = yon_char_new(str); memset(str, 0, 4096); i++; } } if (output) fclose(output); if (i>0){ *str_len = i; return output_strings; } else{ *str_len=-1; return NULL; } } config_str yon_config_load_file(FILE *file, int *str_len) { FILE *output = file; char **output_strings = NULL; int i = 0; char str[4096]; output_strings = malloc(sizeof(char *)); if (!output_strings) { *str_len = -1; return NULL; } while (fgets(str, sizeof(str), output)) { if (strcmp(str, "") != 0) { output_strings[i] = yon_char_new(str); if (!output_strings[i]) { *str_len = -1; for (int j = 0; j < i; j++) { free(output_strings[j]); } free(output_strings); return NULL; } i++; output_strings = realloc(output_strings, sizeof(char *) * (i + 1)); if (!output_strings) { *str_len = -1; for (int j = 0; j < i; j++) { free(output_strings[j]); } free(output_strings); return NULL; } } } *str_len = i; return output_strings; } char *yon_config_parameter_to_string(yon_config_parameter *parameter, int insert_section){ if (parameter){ char *param_string = NULL; param_string = yon_char_unite(insert_section?parameter->section:"",insert_section?" ":"", parameter->key,parameter->flag1==-1?NULL:"","=\'",parameter->data,"\'",NULL); return param_string; } return NULL; } config_str yon_config_get_load_parameters_by_list(int *size, config_str parameters, int params_size){ va_list list; (*size)=0; config_str updated = NULL; int final_size; config_str final = NULL; char *current_str = NULL; for (int i=0;ikey,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); free((final)[position]); (final)[position]=string; } else { char *string = yon_char_unite(temp->section," ",current_str,NULL); yon_char_parsed_add_or_create_if_exists(final,size,string); } } } } return final; } config_str yon_config_get_save_parameters_by_list(int *size, config_str parameters, int params_size){ va_list list; (*size)=0; int removed_size; config_str removed = NULL; int updated_size; config_str updated = NULL; config_str final = NULL; char *current_str = NULL; for (int i=0;ikey,current_str)){ found = 1; if (((yon_config_parameter*)temp)->flag1!=-2){ char *action = NULL; config_str *current=NULL; int *current_size=NULL; switch (((yon_config_parameter*)temp)->flag1){ case -1: action = "remove"; current = &removed; current_size = &removed_size; break; case 1: case 0: action = "set"; current = &updated; current_size = &updated_size; break; } int position = yon_char_parsed_find_element(*current,*current_size,((yon_config_parameter*)temp)->section); if (position>=0){ char *string = yon_char_unite((*current)[position]," ",yon_config_parameter_to_string((yon_config_parameter*)temp,0),NULL); free((*current)[position]); (*current)[position]=string; } else { char *string = yon_char_unite(action," ",yon_config_parameter_to_string((yon_config_parameter*)temp,1),NULL); yon_char_parsed_add_or_create_if_exists(*current,current_size,string); } } } } if (!found){ if(removed){ int position = yon_char_parsed_find_element(removed,removed_size,((yon_config_parameter*)temp)->section); if (position>=0){ char *string = yon_char_unite((removed)[position]," ",yon_config_parameter_to_string((yon_config_parameter*)temp,0),NULL); free(removed[position]); removed[position]=string; } } else { char *string = yon_char_unite("remove"," ",parameters[i],NULL); yon_char_parsed_add_or_create_if_exists(removed,&removed_size,string); } } } if (updated&&removed){ final = yon_char_parsed_merge(updated,updated_size,removed,removed_size,size); } else if (updated&&!removed){ final=updated; *size=updated_size; } else if (!updated&&removed){ final=removed; *size=removed_size; } return final; } config_str yon_config_get_save_parameters_by_key(int *size, char *parameter,...){ va_list list; (*size)=0; va_start(list,parameter); int removed_size; config_str removed = NULL; int updated_size; config_str updated = NULL; config_str final = NULL; char *current_str = NULL; yon_va_while(list,char*,current_str){ for_config{ if (!strcmp(temp->key,current_str)){ if (((yon_config_parameter*)temp)->flag1!=-2){ char *action = NULL; config_str *current=NULL; int *current_size=NULL; switch (((yon_config_parameter*)temp)->flag1){ case -1: action = "remove"; current = &removed; current_size = &removed_size; break; case 1: action = "set"; current = &updated; current_size = &updated_size; break; } int position = yon_char_parsed_find_element(*current,*current_size,((yon_config_parameter*)temp)->section); if (position>=0){ char *string = yon_char_unite((*current)[position]," ",yon_config_parameter_to_string((yon_config_parameter*)temp,0),NULL); free((*current)[position]); (*current)[position]=string; } else { char *string = yon_char_unite(action," ",yon_config_parameter_to_string((yon_config_parameter*)temp,1),NULL); yon_char_parsed_add_or_create_if_exists(*current,current_size,string); } } } } } final = yon_char_parsed_merge(updated,updated_size,removed,removed_size,size); return final; } config_str yon_config_get_save_parameters(int *size){ (*size)=0; int removed_size=0; config_str removed = NULL; int updated_size=0; config_str updated = NULL; config_str final = NULL; for_config{ if (((yon_config_parameter*)temp)->flag1!=0&&((yon_config_parameter*)temp)->flag1!=-2){ char *action = NULL; config_str *current=NULL; int *current_size=NULL; switch (((yon_config_parameter*)temp)->flag1){ case -1: action = "remove"; current = &removed; current_size = &removed_size; break; case 1: action = "set"; current = &updated; current_size = &updated_size; break; } int position = yon_char_parsed_find_element(*current,*current_size,((yon_config_parameter*)temp)->section); if (position>=0){ char *string = yon_char_unite((*current)[position]," ",yon_config_parameter_to_string((yon_config_parameter*)temp,0),NULL); free((*current)[position]); (*current)[position]=string; } else { char *string = yon_char_unite(action," ",yon_config_parameter_to_string((yon_config_parameter*)temp,1),NULL); yon_char_parsed_add_or_create_if_exists(*current,current_size,string); } } } final = yon_char_parsed_merge(updated,updated_size,removed,removed_size,size); return final; } char *yon_config_save_simple(YON_CONFIG_TYPE target, char *path){ int parameters_size=0; config_str parameters=yon_config_get_save_parameters(¶meters_size); if (parameters){ yon_char_parsed_prepend_strings(parameters,parameters_size,ubconfig_set_command(path)); char *final_command = yon_char_parsed_to_string(parameters,parameters_size,";"); printf("%s\n",final_command); FILE *file = popen(final_command,"r"); if (file){ int file_size=0; config_str file_output = yon_config_load_file(file,&file_size); if (file_output){ char *final_string = yon_char_parsed_to_string(file_output,file_size,""); if (!yon_char_is_empty(final_string)){ return final_string; } } fclose(file); } } return NULL; } char *yon_config_parameter_prepare_command(char *command, char *path, char *section, char *parameter){ if (!yon_char_is_empty(command)){ int size=0; config_str parsed = yon_char_parse(command,&size," "); int source_pos=yon_char_parsed_check_exist(parsed,size,"--source"); int get_pos = yon_char_parsed_check_exist(parsed,size,"get"); if (source_pos==-1) { source_pos = yon_char_parsed_check_exist(parsed,size,"--target"); get_pos = yon_char_parsed_check_exist(parsed,size,"set"); } if (path&&!strcmp(path,"")){ if (source_pos>-1&&size>source_pos+1){ free(parsed[source_pos]); parsed[source_pos]=yon_char_new(""); free(parsed[source_pos+1]); parsed[source_pos+1]=yon_char_new(""); } } else if (path){ if (source_pos>-1&&size>source_pos+1){ free(parsed[source_pos+1]); parsed[source_pos+1]=yon_char_new(path); } } if (section){ if (size>get_pos+1){ free(parsed[get_pos+1]); parsed[get_pos+1]=yon_char_new(section); } } if (parameter){ if (size>=get_pos+2){ free(parsed[get_pos+2]); parsed[get_pos+2] = yon_char_new(parameter); } } char *final = yon_char_parsed_to_string(parsed,size," "); yon_char_parsed_free(parsed,size); return final; } return NULL; } int yon_config_save_registered(char *path){ check_config{ dictionary *dct; dictionary *sections_add=NULL; dictionary *sections_remove=NULL; for_dictionaries(dct,(dictionary*)__yon__config__strings){ if (((yon_config_parameter*)dct)->flag1==1){ if (dct->data&&!yon_char_is_empty(yon_dictionary_get_data(dct,char*))){ ((yon_config_parameter*)dct)->flag1=0; if (sections_add&&yon_dictionary_get(§ions_add,((yon_config_parameter*)dct)->section)) sections_add->data=(void*)yon_char_unite(yon_dictionary_get_data(sections_add,char*)," ",dct->key,"=\'",yon_dictionary_get_data(dct,char*),"\'",NULL); else yon_dictionary_add_or_create_if_exists_with_data(sections_add,((yon_config_parameter*)dct)->section,yon_char_unite (ubconfig_save_command,path ? yon_char_append(" --target ",path):"", " set ", ((yon_config_parameter*)dct)->section, " ",dct->key,"=\'",yon_dictionary_get_data(dct,char*),"\'",NULL)); } } else if (((yon_config_parameter*)dct)->flag1==-1){ if (dct->data&&yon_char_is_empty(yon_dictionary_get_data(dct,char*))){ ((yon_config_parameter*)dct)->flag1=0; if (sections_remove&&yon_dictionary_get(§ions_remove,((yon_config_parameter*)dct)->section)) sections_remove->data=(void*)yon_char_unite(yon_dictionary_get_data(sections_remove,char*)," ",dct->key,NULL); else yon_dictionary_add_or_create_if_exists_with_data(sections_remove,((yon_config_parameter*)dct)->section,yon_char_unite (ubconfig_save_command,path ? yon_char_append(" --target ",path):"", " remove ", ((yon_config_parameter*)dct)->section, " ",dct->key,NULL)); yon_dictionary_rip(dct); } } } if (sections_add) for_dictionaries(dct,sections_add){ char *command = yon_dictionary_get_data(dct,char*); yon_launch(command); printf("%s\n",command); } yon_dictionary_free_all(sections_add,free); if (sections_remove) for_dictionaries(dct,sections_remove){ char *command = yon_dictionary_get_data(dct,char*); yon_launch(command); printf("%s\n",command); } yon_dictionary_free_all(sections_remove,free); if (sections_add||sections_remove) return 1; else return 0; } else return 0; } int yon_config_force_save_registered(char *path){ check_config{ dictionary *dct; dictionary *sections_add=NULL; dictionary *sections_remove=NULL; for_dictionaries(dct,(dictionary*)__yon__config__strings){ if (dct->data&&strcmp(yon_dictionary_get_data(dct,char*),"")!=0){ if (((yon_config_parameter*)dct)->flag1==1||((yon_config_parameter*)dct)->flag1==0){ if (sections_add&&yon_dictionary_get(§ions_add,((yon_config_parameter*)dct)->section)) sections_add->data=(void*)yon_char_unite(yon_dictionary_get_data(sections_add,char*)," ",dct->key,"=\'",yon_dictionary_get_data(dct,char*),"\'",NULL); else yon_dictionary_add_or_create_if_exists_with_data(sections_add,((yon_config_parameter*)dct)->section,yon_char_unite (ubconfig_save_command,path ? yon_char_append(" --target ",path):"", " set ", ((yon_config_parameter*)dct)->section, " ",dct->key,"=\'",yon_dictionary_get_data(dct,char*),"\'",NULL)); } else if (((yon_config_parameter*)dct)->flag1==-1){ if (sections_remove&&yon_dictionary_get(§ions_remove,((yon_config_parameter*)dct)->section)) sections_remove->data=(void*)yon_char_unite(yon_dictionary_get_data(sections_remove,char*)," ",dct->key,NULL); else yon_dictionary_add_or_create_if_exists_with_data(sections_remove,((yon_config_parameter*)dct)->section,yon_char_unite (ubconfig_save_command,path ? yon_char_append(" --target ",path):"", " remove ", ((yon_config_parameter*)dct)->section, " ",dct->key,NULL)); } } } if (sections_add) for_dictionaries(dct,sections_add){ char *command = yon_dictionary_get_data(dct,char*); yon_launch(command); } if (sections_remove) for_dictionaries(dct,sections_remove){ char *command = yon_dictionary_get_data(dct,char*); yon_launch(command); } return 1; } else return 1; } config_str yon_config_get_all(int *size){ check_config{ *size = 1; config_str conf = NULL; dictionary *dict = NULL; for_dictionaries(dict,(dictionary*)__yon__config__strings){ conf = yon_remalloc(conf,sizeof(char*)*(*size)); conf[(*size)-1] = yon_char_unite(dict->key,"=",(char*)dict->data,NULL); (*size)++; } conf = yon_remalloc(conf,sizeof(char*)*(*size)); conf[*size-1] = NULL; (*size)=(*size)-1; return conf; } else return NULL; } config_str yon_config_get_all_no_ignored(int *size){ check_config{ *size = 1; config_str conf = NULL; dictionary *dict = NULL; for_dictionaries(dict,(dictionary*)__yon__config__strings){ if (yon_config_check_ignore(dict->key)==0){ conf = yon_remalloc(conf,sizeof(char*)*(*size)); conf[(*size)-1] = yon_char_unite(dict->key,"=",(char*)dict->data,NULL); (*size)++; } } conf = yon_remalloc(conf,sizeof(char*)*(*size)); conf[*size-1] = NULL; (*size)=(*size)-1; return conf; } else return NULL; } char *yon_config_get_parameter(config_str parameters, int size, char *param) { if (param[0]==' ') yon_char_divide_search(param," ",-1); param=yon_char_divide_search(yon_char_new(param)," ",-1); char *str = NULL; for (int j = 0; j < size; j++) { char *name = yon_char_divide_search(yon_char_new(parameters[j]), "=", 1); if (name) { if (strcmp(name, param) == 0) { str = yon_char_divide_search(yon_char_new(parameters[j]), "\n", 1); if (strcmp(str, "") != 0 && strcmp(str, "(null)") != 0) return str; else return NULL; } } } return NULL; } int yon_config_change_key(char *target, char *key){ if (!yon_char_is_empty(target)&&!yon_char_is_empty(key)){ dictionary *dict = yon_dictionary_get((dictionary**)&__yon__config__strings,target); if (!dict) return 0; free(dict->key); dict->key = yon_char_new(key); } return 0; } char *yon_file_path_proceed_spaces(char *path){ int size; config_str parsed = yon_char_parse(path,&size,"/"); for (int i=0;ipw_uid,user->pw_gid)){}; chmod(file_path,0644); 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)){}; chmod(full_path,rules); fclose(fl); return 1; } else { return 0; } } else { return -1; } } else { return 0; } } int yon_file_create_full_path(char *path, int rules){ if (path){ if (access(path,F_OK)){ int size; config_str parsed = yon_char_parse(path,&size,"/"); char *current = ""; for (int i=0;ipw_uid,user->pw_gid); } } 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); } } // terminal-using functions int yon_launch_app_with_arguments(char *name, char *args) { char *path = yon_char_unite("/usr/bin/", name, " ", args,NULL); pthread_t thread_id; char *command = NULL; command = path; pthread_create(&thread_id, NULL, (void *)yon_launch, command); }; void yon_launch(char *command) { int ansver = system(command); } // // Trash collector functions // 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; // }