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/ublsettings.c

1303 lines
41 KiB

#include "ublsettings.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);
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_all(dictionary *dictionary_to_free,void (*data_manipulation)(void*)){
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--);
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);
// if (strstr(source, "%%"))
// sprintf(final, source, append);
// else
sprintf(final, "%s%s", source, append);
return final;
}
else
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, ...){
va_list arglist;
char *new_char=NULL;
char *unite_char=NULL;
new_char=yon_char_new(source);
va_start(arglist,source);
unite_char = va_arg(arglist,char*);
while(unite_char){
new_char = yon_char_append(new_char,unite_char);
unite_char = va_arg(arglist,char*);
}
va_end(arglist);
return new_char;
}
char *yon_cut(char *source, int size, int startpos)
{
char *cut = NULL;
cut = malloc(size + 1);
memset(cut, 0, size + 1);
memcpy(cut, source + startpos, size);
return cut;
}
char *yon_char_divide(char *source, int dividepos)
{
if (source){
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));
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");
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){
if (parameters){
char **string=NULL;
*size=0;
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;
return string;
}
return NULL;
}
char **yon_char_parsed_rip(char **char_string, int *size, int item_to_delete){
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;
}
int yon_char_parsed_check_exist(char **parameters, int size, char *param){
for (int i=0;i<size;i++){
if (parameters[i]){
if (strstr(parameters[i],param))
return i;
} else return -1;
}
return -1;
}
int yon_char_parsed_check_repeats(char **parameters, int size, int *first_overlap, int *second_overlap){
if (parameters){
*first_overlap=0;
*second_overlap=0;
for ((*first_overlap)=0;(*first_overlap)<size;(*first_overlap)++){
for ((*second_overlap)=0;(*second_overlap)<size;(*second_overlap)++){
if (parameters[(*first_overlap)]&&parameters[(*second_overlap)]){
if (!strcmp(parameters[(*first_overlap)],parameters[(*second_overlap)])&&(*first_overlap)!=(*second_overlap))
return 1;
}
}
}
}
return 0;
}
int yon_char_parsed_includes_char_parsed (config_str source, config_str to_check, int source_size, int check_size){
int overall_found=0;
for (int i=0;i<source_size;i++){
int found=0;
for (int j=0;j<check_size;j++){
if (strcmp(source[i],to_check[j])==0&&found==0) { found=1; overall_found++; }
}
}
if (overall_found==check_size)
return 1;
else return 0;
}
config_str yon_char_parsed_new (int *size, ...){
va_list arglist;
config_str new_parsed=NULL;
new_parsed=new(char*);
int new_size=0;
va_start(arglist,size);
char *newparse = va_arg(arglist,char*);
while (newparse){
new_size++;
new_parsed=realloc(new_parsed,new_size*sizeof(char*));
new_parsed[new_size-1]=yon_char_new(newparse);
newparse=va_arg(arglist,char*);
}
va_end(arglist);
*size=new_size;
return new_parsed;
}
void yon_char_parsed_free(config_str source, int size){
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;
}
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;
}
}
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;
}
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);
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);
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;
}
// apps *yon_apps_scan_and_parse_desktops(int *sizef)
// {
// int size = 0;
// struct apps *applist;
// {
// DIR *directory = opendir(DesktopPath);
// struct dirent *de;
// while ((de = readdir(directory)))
// {
// FILE *file;
// char *path = yon_char_append(DesktopPath, de->d_name);
// file = fopen(path, "r");
// if (strlen(de->d_name) > 9)
// {
// char *extension = strstr(path, ".");
// if (extension != NULL)
// {
// if (strcmp(extension, ".desktop") == 0)
// {
// apps tempapp;
// GKeyFile *gfile = g_key_file_new();
// GError *err = NULL;
// g_key_file_load_from_file(gfile, path, G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
// char *Type = g_key_file_get_string(gfile, "Desktop Entry", "Type", &err);
// if (err)
// {
// printf("%s\n", err->message);
// }
// if (strcmp(Type, "Application") == 0)
// tempapp.Type = 1;
// else if (strcmp(Type, "pyApplication") == 0)
// tempapp.Type = 2;
// else
// continue;
// tempapp.Name = g_key_file_get_locale_string(gfile, "Desktop Entry", "Name", setlocale(LC_ALL, NULL), NULL);
// if (tempapp.Name == NULL)
// continue;
// tempapp.Categories = g_key_file_get_string(gfile, "Desktop Entry", "Categories", NULL);
// if (tempapp.Categories == NULL)
// continue;
// tempapp.Exec = g_key_file_get_string(gfile, "Desktop Entry", "Exec", NULL);
// if (tempapp.Exec == NULL)
// continue;
// tempapp.Icon = g_key_file_get_string(gfile, "Desktop Entry", "Icon", NULL);
// if (tempapp.Icon == NULL)
// continue;
// tempapp.Pluggable = g_key_file_get_boolean(gfile, "Desktop Entry", "Pluggable", NULL);
// if (!tempapp.Pluggable)
// tempapp.Pluggable = g_key_file_get_boolean(gfile, "Desktop Entry", "X-XfcePluggable", NULL);
// if (tempapp.Pluggable)
// tempapp.DualPluggable = g_key_file_get_boolean(gfile, "Desktop Entry", "X-UBLPluggable", NULL);
// if (g_key_file_get_boolean(gfile, "Desktop Entry", "X-UBL-SettingsManager-Hidden", NULL) == 0)
// if (size == 0)
// {
// applist = (apps *)malloc(size + 1 * sizeof(apps));
// applist[0].Name = yon_char_new(tempapp.Name);
// applist[0].Categories = yon_char_new(tempapp.Categories);
// applist[0].Exec = yon_char_new(tempapp.Exec);
// applist[0].Icon = yon_char_new(tempapp.Icon);
// applist[0].Type = tempapp.Type;
// applist[0].Pluggable = tempapp.Pluggable;
// applist[0].DualPluggable = tempapp.DualPluggable;
// size++;
// }
// else
// {
// applist = (apps *)realloc(applist, (size + 1) * sizeof(apps));
// applist[size].Name = yon_char_new(tempapp.Name);
// applist[size].Categories = yon_char_new(tempapp.Categories);
// applist[size].Exec = yon_char_new(tempapp.Exec);
// applist[size].Icon = yon_char_new(tempapp.Icon);
// applist[size].Pluggable = tempapp.Pluggable;
// applist[size].DualPluggable = tempapp.DualPluggable;
// applist[size].Type = tempapp.Type;
// size++;
// }
// }
// }
// }
// }
// }
// *sizef = size;
// return applist;
// };
// void yon_apps_sort(apps *applist, int size)
// {
// apps tmp;
// if (size > 2)
// {
// for (int i = 1; i < size; i++)
// {
// for (int j = 1; j < size; j++)
// {
// if (strcmp(applist[j].Name, applist[j - 1].Name) < 0)
// {
// tmp = applist[j];
// applist[j] = applist[j - 1];
// applist[j - 1] = tmp;
// };
// }
// };
// }
// };
// apps *yon_apps_get_by_name(apps *applist, char *name, int size)
// {
// for (int i = 0; i < size; i++)
// {
// if (strcmp(applist[i].Name, name) == 0)
// return &applist[i];
// }
// return NULL;
// };
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 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;
} 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=0;
param->key=yon_char_new(key);
param->next=NULL;
param->prev=NULL;
param->section=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;
(param->prev)=(yon_config_parameter*)yon_dictionary_get_last((dictionary*)dict);
dict->next=param;
return param;
}
static yon_config_parameter *__yon__config__strings = NULL;
#define check_config if(__yon__config__strings&&__yon__config__strings->data_type==DICTIONARY_CHAR_TYPE)
#define for_config dictionary *temp = NULL; for_dictionaries(temp,(dictionary*)__yon__config__strings)
#define yon_config_parameter_add_or_create_if_exists_with_data(dict,key,data) {if (!dict) dict=yon_config_parameter_new_with_data(key,data); \
else dict=yon_config_parameter_append_with_data(dict,key,data);}
int yon_config_load_register(YON_CONFIG_TYPE config_type,char *section,char *parameter, ...){
if (__yon__config__strings){
__yon__config__strings = yon_dictionary_free_all((dictionary*)__yon__config__strings,NULL);
}
va_list args;
va_start(args,parameter);
char *arg;
dictionary *sections = NULL;
{
if (sections&&yon_dictionary_get(&sections,section)) sections->data=(void*)yon_char_unite(yon_dictionary_get_data(sections,char*)," ",parameter,NULL);
else yon_dictionary_add_or_create_if_exists_with_data(sections,section,parameter);
}
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,config_type==YON_CONFIG_GLOBAL ? " global get " : " system get ", dict->key," ", yon_dictionary_get_data(dict,char*),NULL);
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&& strcmp(str,"(null)\n")!=0)
{
char *key = yon_char_divide_search(str,"=",-1);
char *final_str=yon_char_divide_search(str,"\n",-1);
yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,yon_char_new(final_str));
__yon__config__strings->data_type=DICTIONARY_CHAR_TYPE;
__yon__config__strings->section=dict->key;
}
}
}
check_config
return 1;
else return 0;
}
int yon_config_remove_by_key(char *key){
check_config{
dictionary *dict = yon_dictionary_get((dictionary**)&__yon__config__strings,key);
if (dict){
((yon_config_parameter*)dict)->flag1=-1;
return 1;
}else return 0;
}
return 0;
}
int yon_config_remove_element(char *key, char *deleted){
check_config{
yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key);
char *data = (char*)dict->data;
char *found = strstr(data,deleted);
int size=strlen(data)-strlen(found)+1;
char *new_data = malloc(size);
memset(new_data,0,size);
if (strlen(found)!=strlen(deleted)){
memcpy(new_data,data,size-1);
new_data = yon_char_append(new_data,found+strlen(deleted)+1);
} else {
memcpy(new_data,data,size-2);
new_data = yon_char_append(new_data,found+strlen(deleted));
}
dict->data=(void*)(new_data);
dict->flag1=1;
return 1;
} else return 0;
}
void yon_config_set_status(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){
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;
}
}
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;
}
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;
return 1;
} else return 0;
}
int yon_config_append(char *key, char *data){
check_config{
yon_config_parameter *dict = (yon_config_parameter*)yon_dictionary_get((dictionary**)&__yon__config__strings,key);
if (strcmp(((char*)dict->data),"")!=0)
dict->data=(void*)(yon_char_unite((char*)dict->data," ",data,NULL));
else dict->data=(void*)data;
dict->flag1=1;
return 1;
} 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_section, void *data){
if (!__yon__config__strings||!yon_dictionary_get((dictionary**)&__yon__config__strings,key)){
yon_config_parameter_add_or_create_if_exists_with_data(__yon__config__strings,key,data);
__yon__config__strings->flag1=1;
}
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=data;
__yon__config__strings->flag1=1;
}
}
__yon__config__strings->data_type=DICTIONARY_CHAR_TYPE;
__yon__config__strings->section=yon_char_new(config_section);
}
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 (i>0){
*str_len = i;
return output_strings;
} else{
*str_len=-1;
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 (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){
((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);
}
if (sections_remove)
for_dictionaries(dct,sections_remove){
char *command = yon_dictionary_get_data(dct,char*);
yon_launch(command);
}
return 1;
} else return 1;
}
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;
}
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;
}
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_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)
{
system(command);
}