You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libublsettings/source/libublsettings.c

2115 lines
70 KiB

#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) return NULL;
else if (!dict->next)
{
dictionary *prev = dict->prev;
if (prev)
{
prev->next = NULL;
return prev;
}
else
return dict;
}
else if (!dict->prev)
{
dictionary *next = dict->next;
if (next)
{
yon_dictionary_make_first(next);
next->prev = NULL;
return next;
}
else
return dict;
}
else
{
dictionary *next = dict->next, *prev = dict->prev;
next->prev = prev;
prev->next = next;
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;
}
// 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(char *source, 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(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(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){
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;
}
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,"\n");
free(working_string);
for (int j=0;j<size;j++){
if(strstr(rtn[j],find))
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 = 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_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)>0&&(*size)>item_to_delete&&item_to_delete>=0){
char **new_char_parsed=NULL;
new_char_parsed=malloc(sizeof(char*)*((*size)-1));
int flag = 0;
for (int i=0;i < (*size);i++){
if (i==item_to_delete) {
flag = 1;
}
if (flag == 0) {
new_char_parsed[i]=yon_char_new(char_string[i]);
}
else if (flag == 1 && i!=item_to_delete) {
new_char_parsed[i-1]=yon_char_new(char_string[i]);
}
}
(*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;i<size;i++){
if (parameters[i]){
if (!strcmp(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++){
int new_size=0;
parsed_final = yon_char_parsed_append(parsed_final,&new_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 {
*final_size=size1;
return array1;
}
}
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++){
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 "";
}
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_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);
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);
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;
}
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_find_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;i<found_size;i++){
int c;
for (c=0;c<strlen(found_files[i]);c++){
if (found_files[i][c]!=lpart[c]){
break;
}
if (c==strlen(lpart)) break;
}
if (found){
for (int l=c;l<strlen(found_files[i]);l++){
if (found_files[i][l]==rpart[l]) rfound++;
else rfound=0;
}
if (rfound==strlen(rpart)) {
yon_char_parsed_add_or_create_if_exists(files,size,found_files[i]);
}
}
}
free(found_files);
}
}
}
//config functions
typedef struct yon_config_parameter
{
char *key;
void *data;
struct yon_config_parameter *next;
struct yon_config_parameter *prev;
struct yon_config_parameter *first;
DICT_TYPE data_type;
int flag1;
char *section;
int ignore;
char *save_command;
char *load_command;
} yon_config_parameter;
yon_config_parameter *yon_config_parameter_new_with_data(char *key, void *data){
yon_config_parameter *param = yon_remalloc(NULL,sizeof(yon_config_parameter));
param->data=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;
}
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(&sections,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));
}
dictionary *dicte = yon_dictionary_get((dictionary**)&__yon__config__strings,key);
if (!dicte){
yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,yon_char_new(final_str));
} else {
if (strcmp((char*)dicte->data,final_str)){
__yon__config__strings->data=final_str;
} else {
continue;
}
}
__yon__config__strings->flag1=0;
yon_config_remove_ignore(key);
if (config_type==YON_CONFIG_DEFAULT){
__yon__config__strings->flag1=-2;
yon_config_set_ignore(key);
}
__yon__config__strings->data_type=DICTIONARY_CHAR_TYPE;
__yon__config__strings->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 (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 (place<size){
parsed[place]=yon_char_new(parameter);
}
char *final = yon_char_parsed_to_string(parsed,size," ");
yon_char_parsed_free(parsed,size);
return final;
}
int yon_config_command_prepare(config_str *commands, int *commands_size,char *command){
if (!yon_char_is_empty(command)){
int parsed_size;
int done=0;
config_str parsed = yon_char_parse(command,&parsed_size," ");
if (parsed_size>5){
for (int j=5;j<parsed_size;j++){
int new_size;
config_str new_element = yon_char_parsed_new(&new_size,parsed[0],parsed[1],parsed[2],parsed[3],parsed[4],parsed[j],NULL);
*commands = yon_char_parsed_append(*commands,commands_size,yon_char_parsed_to_string(new_element,new_size," "));
done=1;
}
}
return done;
}
return 0;
}
int yon_config_load_config(YON_CONFIG_TYPE config_type, ...){
if (config_type!=YON_CONFIG_BOTH){
va_list args;
va_start(args,config_type);
char *current = NULL;
int command_size=0;
config_str command=NULL;
while ((current=va_arg(args,char*))){
yon_config_command_prepare(&command,&command_size,current);
}
for (int i=0;i<command_size;i++){
int parsed_size;
config_str parsed = yon_config_load(command[i],&parsed_size);
if (parsed_size>0){
for (int j=0;j<parsed_size;j++){
if (!yon_char_is_empty(parsed[j])&&strcmp(parsed[j],"(null)\n")){
if (parsed[j][strlen(parsed[j])-1]=='\n') parsed[j][strlen(parsed[j])-1]='\0';
char *current_value = yon_char_new(parsed[j]);
char *key = yon_char_divide_search(current_value,"=",-1);
yon_char_remove_brackets(current_value);
char *current_command = yon_char_new(command[i]);
current_command = yon_config_replace_parameter(current_command,key,5);
yon_config_register(key,current_command,current_value);
if (config_type==YON_CONFIG_DEFAULT){
yon_config_set_status(key,-2);
}
}
}
}
yon_char_parsed_free(parsed,parsed_size);
}
}
}
int yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...){
if (config_type!=YON_CONFIG_BOTH){
if (__yon__config__strings){
__yon__config__strings = yon_dictionary_free_all((dictionary*)__yon__config__strings,NULL);
}
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(&sections,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 (!yon_char_is_empty(str)&& 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));
}
dictionary *dicte = yon_dictionary_get((dictionary**)&__yon__config__strings,key);
if (!dicte){
yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,yon_char_new(final_str));
} else {
if (strcmp((char*)__yon__config__strings->data,final_str)){
__yon__config__strings->data=final_str;
} else {
continue;
}
}
__yon__config__strings->flag1=0;
yon_config_remove_ignore(key);
if (config_type==YON_CONFIG_DEFAULT){
__yon__config__strings->flag1=-2;
yon_config_set_ignore(key);
}
__yon__config__strings->data_type=DICTIONARY_CHAR_TYPE;
__yon__config__strings->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;i<size;i++){
if (!strcmp(parsed[i],delete_target)){
parsed = yon_char_parsed_rip(parsed,&size,i--);
found = 1;
}
}
char *final_string = NULL;
if (size>0) 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;
}
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){
key=yon_char_new(key);
config_load=yon_char_new(config_load);
if (!__yon__config__strings||!yon_dictionary_get((dictionary**)&__yon__config__strings,key)){
{
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[4]);
yon_char_parsed_free(section,size);
}
else if (yon_dictionary_get((dictionary**)&__yon__config__strings,key)){
if (data!=__yon__config__strings->data&&strcmp(__yon__config__strings->data,data)){
__yon__config__strings->data=yon_char_new(data);
__yon__config__strings->flag1=1;
__yon__config__strings->data_type=DICTIONARY_CHAR_TYPE;
__yon__config__strings->load_command=config_load;
if (yon_dictionary_get(&__yon_config_ignored, __yon__config__strings->key)){
yon_dictionary_rip(__yon_config_ignored);
}
int size=0;
config_str section = yon_char_parse(config_load,&size," ");
__yon__config__strings->section=yon_char_new(section[4]);
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;i<params_size;i++){
current_str=parameters[i];
for_config{
if (!strcmp(temp->key,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;i<params_size;i++){
current_str=parameters[i];
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:
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 (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;
config_str removed = NULL;
int updated_size;
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(&parameters_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," ");
if (path&&!strcmp(path,"")){
if (size>4&&!strcmp(parsed[1],"--source")){
free(parsed[1]);
parsed[1]=yon_char_new("");
free(parsed[2]);
parsed[2]=yon_char_new("");
}
} else if (path){
if (size>4&&!strcmp(parsed[1],"--source")){
free(parsed[2]);
parsed[2]=yon_char_new(path);
}
}
if (section){
if (size>4){
free(parsed[4]);
parsed[4]=yon_char_new(section);
}
}
if (parameter){
if (size>5){
free(parsed[5]);
parsed[5] = 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(&sections_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(&sections_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(&sections_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(&sections_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;
}
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);
}
return final_string;
}
}
int yon_file_save(char *file_path, char *text){
FILE *file = fopen(file_path,"w");
if (file){
fputs(text,file);
fclose(file);
return 1;
}
return 0;
}
int yon_file_create(char *path, char *name, int rules){
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){
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)){
FILE *fl = fopen(path,"w");
if (fl){
chmod(path,rules);
fclose(fl);
return 1;
} else {
return 0;
}
} else {
return -1;
}
} else {
return 0;
}
}
// 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_group{
// char *group_key;
// void *data;
// struct yon_trash_collector_group *next;
// struct yon_trash_collector_group *prev;
// struct yon_trash_collector_group *first;
// } yon_trash_collector_group;
// 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;
// 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)
// 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;
// }