#include "philos_utils.h"

void philos_array_string_remove_char(char*** array, char* str_remove,  int size) {
    for (int index = 0; index < size; index++) {
       (*array)[index] = yon_char_divide_search((*array)[index], str_remove, -1);
    }

}

void philos_set_pow_size_memory(char* str_find, int* array_size, char** array_size_pow) {
    char* STR_KB = array_size_pow[0];
    char* STR_MB = array_size_pow[1];
    char* STR_GB = array_size_pow[2];
    char* STR_TB = array_size_pow[3];
    size_t length = strlen(str_find);
    if (strstr(str_find,"K") != NULL) {
        (*array_size) = 0;
        str_find = yon_char_divide(str_find, length-1);
        str_find = yon_char_unite(str_find, " ", STR_KB, NULL);
    }
    else if (strstr(str_find,"M") != NULL) {
        (*array_size) = 1;
        str_find = yon_char_divide(str_find, length-1);
        str_find = yon_char_unite(str_find," ", STR_MB, NULL);
    }
    else if (strstr(str_find,"G") != NULL) {
        (*array_size) = 2;
        str_find = yon_char_divide(str_find, length-1);
        str_find = yon_char_unite(str_find," ", STR_GB, NULL);
    }
    else if (strstr(str_find,"T") != NULL) {
        (*array_size) = 3;
        str_find = yon_char_divide(str_find, length-1);
        
        str_find = yon_char_unite(str_find," ", STR_TB, NULL);
    }
    else if (strstr(str_find,"%%") != NULL) {
        (*array_size) = -1;
        str_find = yon_char_divide(str_find, length-1);
    }
    else if (strstr(str_find,"-") != NULL) {
        (*array_size) = -1;
    }
    else {
        (*array_size) = 0;
    }
     
}

void philos_set_size_memory_integer_char(char* str_find, char*** array_data, int index) {
    char* simv_del_array[10] = {"K","M","G","T","k","m","g","t"," ","%"};
    for (size_t i = 0; i < 10; i++) {
        if (strstr(str_find, simv_del_array[i])) {
            str_find =  yon_char_divide_search(str_find, simv_del_array[i], -1);
        }
    }
    if (strstr(str_find, " ") != NULL) {
        char* mem_s = yon_char_new(str_find);
        yon_char_divide_search(mem_s, " ", -1);
        *array_data = yon_char_parsed_append(*array_data, &index, mem_s); 
    }
    if (strstr(str_find, "-") != NULL) {
        *array_data = yon_char_parsed_append(*array_data, &index, "-");
    }
    else {
        if (str_find[0] == '\"') {
            yon_char_divide(str_find, 0);
            size_t length = strlen(str_find);
            str_find = yon_char_divide(str_find, length-2);
        }
        if (strstr(str_find, " ") == NULL) {
            *array_data = yon_char_parsed_append(*array_data, &index, str_find);
        }
         
    } 
}

void philos_free_string_array(char ***array, int size) {
    if ((*array) == NULL) {
        return;
    }
    for (int i = 0; i < size; i++){
        free((*array)[i]);
    }
    if (size!= 0) {
        free(*array);
        (*array) = NULL;
    } 
}

void philos_free_string_array_n3(char ****array, int size) {
    if ((*array) == NULL || size == 0) {
        return;
    }
    int index_to_l2 = 0;
    for (int i = 0; i < size; i++){
        index_to_l2 = 0;
        if ((*array)[i]!=NULL) {
            while ((*array)[i][index_to_l2] != NULL) {
                if ((*array)[i][index_to_l2] != NULL) {
                    free((*array)[i][index_to_l2]);
                    index_to_l2++;
                }
                
            }
        }
        free((*array)[i]);
        
    }
    if (size!= 0) {
        free(*array);
        (*array) = NULL;
    } 
}

void philos_free_int_array(int **array, int size) {
    if (size!= 0) {
        free(*array);
        *array = NULL;
    }
}
void philos_free_int_array_n2(int ***array, int size) {
    if ((*array) == NULL || size == 0) {
        return;
    }
    for (int i = 0; i < size; i++){
        free((*array)[i]);
    }
    if (size!= 0) {
        free(*array);
        (*array) = NULL;
    } 
}

config_str philos_list_user(int* size, int flag_lim) {
    char* str_uid_min = "UID_MIN";
    char* str_uid_max = "UID_MAX";
    unsigned short uid_min = philos_read_uid_min_max(file_source_login_min_max, str_uid_min);
    unsigned short uid_max = philos_read_uid_min_max(file_source_login_min_max, str_uid_max);
    config_str str_users = malloc(1);
    while (1) {
        errno = 0;
        struct passwd* entry = getpwent();
        if (!entry) {
            if (errno) {
                return str_users;
            }
            break;
        }
        if (flag_lim == 1) {
            if ((entry->pw_uid >= uid_min && entry->pw_uid < uid_max) || entry->pw_uid == 0) {
                str_users = yon_char_parsed_append(str_users, size, entry->pw_name);
            }
        }
        else {
            str_users = yon_char_parsed_append(str_users, size, entry->pw_name);
        }
        
    }
    endpwent();
    return str_users;
}

unsigned short philos_read_uid_min_max(char* filename, char* search) {
    int uid = 0;
    char* remove_tab = "\t";
    char* remove_space = " ";
    char* search_uid_min = "UID_MIN";
    int buff_size = 255;
    char* line = g_malloc0(buff_size);
    char* search_true = yon_char_get_augumented("SYS_", search);
    FILE *fp = fopen(filename, "r");
    if(fp) {
        while((fgets(line, buff_size, fp)) != NULL) {
            try{
                if (yon_char_find_count(line, search) != 0 && yon_char_find_count(line, search_true) == 0) {
                    line = philos_str_remove(line, search);
                    line = philos_str_remove(line, remove_space);
                    line = philos_str_remove(line, remove_tab);
                    uid = atoi(line);
                    
                }
            }
            catch (...) {
                if (yon_char_find_count(search, search_uid_min) != 0){
                    uid = 1000;
                }
                else{
                    uid = 65534;
                }
            }
        }
    }
    else{
        if (yon_char_find_count(search, search_uid_min) != 0) {
            uid = 1000;
        }
        else{
            uid = 65534;
        }
    }
    fclose(fp);
    free(line);
    free(search_true);
    return uid;

}

config_str philos_list_group(int* size, int flag_lim) {
    char* str_uid_min = "UID_MIN";
    char* str_uid_max = "UID_MAX";
    unsigned short uid_min = philos_read_uid_min_max(file_source_login_min_max, str_uid_min);
    unsigned short uid_max = philos_read_uid_min_max(file_source_login_min_max, str_uid_max);
    config_str str_users = malloc(1);
    char *cmd = "cat /etc/group";
    int size_groups = 0;
    size_t pw_gid = 0;
    char **responce = yon_config_load(cmd, &size_groups);
    for (int i = 0; i< size_groups; i++) {
        char* str_data = yon_char_new(responce[i]);
        char* str_group = yon_char_divide_search(str_data,":x:",-1);
        char* num = yon_char_divide_search(str_data, ":", -1);
        pw_gid = atoll(yon_char_divide_search(str_data, ":", -1));
        if (flag_lim == 1) {
            if ((pw_gid >= uid_min && pw_gid < uid_max) || pw_gid == 0) {
                str_users = yon_char_parsed_append(str_users, size, str_group);
            }
        }
        else {
            str_users = yon_char_parsed_append(str_users, size, str_group);
        }
    }
    endpwent();
    return str_users;
}

char* philos_str_size_pow_byte(GtkWidget *combo_box_text) {
    int menu_id = gtk_combo_box_get_active(GTK_COMBO_BOX(combo_box_text));
    if (menu_id == 0) {
        return "K";
    }
    else if (menu_id == 1) {
        return "M";
    }
    else if (menu_id == 2) {
        return "G";
    }
    else if (menu_id == 3) {
        return "T";
    }
    else {
        return " ";
    }
}

char* philos_str_remove(char *str, const char *sub) {
    size_t len = strlen(sub);
    if (len > 0) {
        char *p = str;
        size_t size = 0;
        while ((p = strstr(p, sub)) != NULL) {
            size = (size == 0) ? (p - str) + strlen(p + len) + 1 : size - len;
            memmove(p, p + len, size - (p - str));
        }
    }
    return str;
}


void philos_split_size_memory(char* str_value, int* size, char* pow_memory) {
    (*size) = atoi(yon_char_divide_search(pow_memory, " ", -1));
}

char* philos_format_cfg_str_size_memory(char* str_key, int value, int pow_size_memory) {
    if (value == -1 || value == -3 || pow_size_memory == -3) {
        return yon_char_new("-");
    }
    char* str_value = yon_char_from_int(value);
    if (value == 0) {
        return str_value;
    }
    if (pow_size_memory==0) {
        return yon_char_unite(str_key, str_value, "K" ,NULL);
    }
    else if (pow_size_memory==1) {
        return yon_char_unite(str_key,  str_value, "M" ,NULL);
    }
    else if (pow_size_memory==2){
        return yon_char_unite(str_key,  str_value, "G" ,NULL);
    }
    else if (pow_size_memory== 3) {
        return yon_char_unite(str_key,  str_value, "T" ,NULL);
    }
    else if (pow_size_memory== -1) {
        return yon_char_unite(str_key,  str_value, "%" ,NULL);
    }
    else if (pow_size_memory== -3) {
        return yon_char_new("-");
    }
    else {
        return yon_char_new("-");
    }
}

char** philos_str_split(char *parameters, int *size, char *divider) {
    char** array_split = NULL;
    char* ch= NULL;
    parameters = yon_char_new(parameters);
    ch = strtok(parameters, divider);
    if (ch != NULL) {
        array_split = yon_char_parsed_append(array_split, size, ch);
        while (ch != NULL) {
            ch = strtok(NULL, divider);
            array_split = yon_char_parsed_append(array_split, size, ch);
        }
    }
    (*size) -= 1;
    return array_split;
}

void philos_array_str_copy(char*** source, char*** copy) {
    int index = 0;
    if (copy == NULL || source == NULL) {
        return;
    }
    while (1) {
        if ((*copy)[index] != NULL) {
            (*source) = yon_char_parsed_append((*source), &index, yon_char_new((*copy)[index]));
        }
        else {
            break;
        }
    }
}
void philos_array_int_copy(int** source, int** copy) {
    int * new_int = g_malloc0(sizeof(int)*2);
    if ((*copy)[0] != -2) {
        new_int[0] = (*copy)[0];
        new_int[1] = -2;
        int i = 2;
        for (i=1;(*copy)[i]!=-2;i++) {
            yon_int_array_append(&new_int,(*copy)[i]);
        }
        *source=new_int;
    }
    else {
        new_int[0] = (*copy)[0];
        *source=new_int;
    }
    
}
char** philos_pars_terminal_systemd_cgls(char* CMD_GET_SLICE_SERVICE, char* str_find, int* size_array_data) {
    int size = 0;
    char** terminal_print = yon_config_load(CMD_GET_SLICE_SERVICE, &size);
    char** array_data = NULL;
    for (int index = 0; index < size; index++) {
        if (strstr(terminal_print[index], str_find) != NULL) {
            yon_char_divide_search(terminal_print[index],"─",-1);
            terminal_print[index] = yon_char_divide_search(terminal_print[index]," ", -1);
            yon_char_divide(terminal_print[index],1);
            array_data = yon_char_parsed_append(array_data, size_array_data, terminal_print[index]);
            
        }
    }
    return array_data;
}
char* philos_pard_array_add_cmd(char* cmd, temp_config* _config, char* key, int* array_io, int* array_io_pow_size, char** disk, int size) {
    char* split_simvol = g_malloc0(sizeof(char)*2);
    int flag_format = 0;
    char* cmd_old = yon_char_new(cmd);
    char* cmd_new = "";
    if (disk && size && array_io && array_io_pow_size) {
        int index_find = 0;
        for (int index = 0; index < size; index++) {
            if (array_io_pow_size[index] >= 0 && array_io[index]>= 0 && !strstr(disk[index], "-")) {
                char* num_and_pow_size = philos_format_cfg_str_size_memory(" ", array_io[index], array_io_pow_size[index]);
                if (!strstr(num_and_pow_size, "-")) {
                    cmd_new = yon_char_unite(cmd_new, 
                        split_simvol,
                        disk[index],
                        num_and_pow_size, NULL);
                    split_simvol[0] = ',';
                    split_simvol[1] = '\0';
                    flag_format = 1;
                }
                free(num_and_pow_size);
            }   
        }
    }
    free(split_simvol);
    if (flag_format) {
        if (strlen(cmd_new)>2) {
            if (strlen(cmd_old) > 3) {
                cmd = yon_char_unite(cmd_old, ",", key, cmd_new, NULL);
            }
            else {
                cmd = yon_char_unite(key, cmd_new, NULL);
            }
            
        }
        return cmd;
    }
    else {
        return cmd_old;
    }
    
}


void philos_temp_config_init(temp_config* _config) {
    if (_config->size_disk != 0) {
        philos_free_string_array(&_config->disk_read, _config->size_disk);
        philos_free_int_array(&_config->i_o_limit_read, _config->size_disk);
        philos_free_int_array(&_config->i_o_limit_read_size, _config->size_disk); 
    }
    else if (_config->size_disk != 0) {
        philos_free_string_array(&_config->disk_write, _config->size_disk);
        philos_free_int_array(&_config->i_o_limit_write, _config->size_disk);
        philos_free_int_array(&_config->i_o_limit_write_size, _config->size_disk);
    }
    _config->disk_read = NULL;
    _config->disk_write = NULL;
    _config->i_o_limit_read = NULL;
    _config->i_o_limit_write = NULL;
    _config->i_o_limit_read_size = NULL;
    _config->i_o_limit_write_size = NULL;
    _config->size_disk = 0;
    _config->size_disk = 0;
}


int find_null_array(temp_config* _config) {
    int index = 0;
    for (index = 0; (_config->disk_read[index]!=NULL && _config->disk_write[index]!=NULL); index++) {
        if (strcmp(_config->disk_read[index], "-") == 0 && strcmp(_config->disk_write[index], "-") == 0) {
            break;
        }
    }
    return index;
    

}
int* philos_int_append(int* array, int* size, int value) {
    array = yon_remalloc(array, (*size+1)*sizeof(int));
    array[(*size)] = value;
    (*size)++;
    return array;
    
}

int* remove_element_int_array(int* array, int* size, int item_to_delete) {
    int *new_int_parsed=NULL;
    new_int_parsed=malloc(sizeof(int)*((*size)-1));
    int flag = 0;
    for (int i=0;i < (*size);i++){
        if (i==item_to_delete) {
            flag = 1;
        }
        if (flag == 0) {
            memcpy(&(new_int_parsed[i]),&(array[i]),sizeof(int));
        }
        else if (flag == 1 && i!=item_to_delete) {
            memcpy(&(new_int_parsed[i-1]),&(array[i]),sizeof(int));
        }
    }
    (*size)=(*size)-1;
    return new_int_parsed;
}

int** remove_element_int_array_n3(int** array, int* size, int item_to_delete) {
    int **new_int_parsed=NULL;
    new_int_parsed=malloc(sizeof(int*)*((*size)-1));
    int flag = 0;
    for (int i=0;i < (*size);i++){
        if (i==item_to_delete) {
            flag = 1;
        }
        if (flag == 0) {
            philos_array_int_copy(&new_int_parsed[i],&array[i]);
        }
        else if (flag == 1 && i!=item_to_delete) {
            philos_array_int_copy(&new_int_parsed[i-1],&array[i]);
        }
    }
    (*size)=(*size)-1;
    return new_int_parsed;
}
char* philos_get_size_bite(GtkWidget* chk_button, GtkWidget* spin, GtkWidget* combo_box_text) {
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(chk_button))) {
        char* size_prifics = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(combo_box_text));
        int size_bite = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
        char* str_size_bute = yon_char_unite(yon_char_from_int(size_bite), " ", size_prifics, NULL);
        return str_size_bute;
    }
    else {
        char* str = (char*)malloc(sizeof(char*)*2);
        str[0] = '-';
        str[1] = '\0';
        return str;
    }
}
void philos_fill_combo_box_text(GtkWidget *cbt, config_str list_data, int size) {
    for (int index = 0; index < size; index++) {
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(cbt), list_data[index]);
    }
}

void philos_set_spin_adjustment(GtkWidget *check, GtkWidget *spin, GtkWidget *combo, size_t value) {
    gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
    //guint value_spin = gtk_spin_button_get_digits(GTK_SPIN_BUTTON(spin));
    guint value_spin = atoi((char*)gtk_entry_get_text(GTK_ENTRY(spin)));
    if (active != -1) {
        GtkAdjustment* adjustment = NULL;
        adjustment = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(spin));
        gtk_adjustment_set_lower(adjustment, 0.0);
        gtk_adjustment_set_page_increment(adjustment, 1.0);
        if (combo == NULL) {
            gtk_adjustment_set_upper(adjustment, (value*100));
            if ((value*100)<value_spin) {
                gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), (value*100));
            }
        }
        else {
            int index = gtk_combo_box_get_active(GTK_COMBO_BOX(combo));
            if (index == 0) {
                gtk_adjustment_set_upper(adjustment, value);
                if (value<value_spin) {
                    gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), value);
                }
            }
            else if (index == -1) {}
            else {
                size_t pow_mem_size = (size_t)get_size_pow_memory(value, index);
                gtk_adjustment_set_upper(adjustment, pow_mem_size);
                if (pow_mem_size<value_spin) {
                    //gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spin), (int)pow_mem_size);
                }
            }
             
        }
    }
}
void philos_set_active_widgets(GtkWidget *check, GtkWidget *spin, GtkWidget *combo) {
    gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
    size_t value_spin = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spin));
    gtk_widget_set_sensitive(spin, active);
    if (combo) {
        if (!active) {
            gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
        }
        gtk_widget_set_sensitive(combo, active);
    }     
}

size_t get_resurs_total(char* cmd) {
    int size = 0;
    size_t size_memory = 0;
    char **responce = yon_config_load(cmd, &size);
    for (int index = 0; index < size; index++ ) {
        char* mem_size_kb = yon_char_divide_search(responce[index], "\n", -1);
        size_memory = atoll(mem_size_kb);
        free(mem_size_kb);
    }
    philos_free_string_array(&responce, size);
    return size_memory;
}

float get_size_pow_memory(size_t size_memory, int size) {
    float res = size_memory;
    for (size_t index = 0; index < size; index++) {
        res = res/1024;
    }
    return res;
}

void philos_set_active_widgets_device_io(GtkWidget* combo_to_l2,GtkWidget *check, GtkWidget *spin, GtkWidget *combo) {
    int menu_id = gtk_combo_box_get_active(GTK_COMBO_BOX(combo_to_l2));
    if (menu_id != -1) {
        gboolean active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
        if (active == 0) {
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), 0);
        }
        else if (gtk_spin_button_get_digits(GTK_SPIN_BUTTON(spin))>0) {
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check),1);
        }
        gtk_widget_set_sensitive(spin, active);
        gtk_widget_set_sensitive(combo, active);
    }
    else {
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), 0);
        gtk_widget_set_sensitive(spin, 0);
        gtk_widget_set_sensitive(combo, 0);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), 0);
    }
    philos_set_spin_adjustment(check, spin, combo, 12582912);
}
char *yon_char_get_augumented(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;
}
dictionary *yon_dictionary_create_with_data_connected(dictionary *dict, char *key, void *data) {
    dictionary *dct = yon_dictionary_create_conneced(dict);
    dct->key = yon_char_new(key);
    dct->data = data;
    dct->data_type = DICTIONARY_OTHER_TYPE;
    return dct;
}
char **yon_char_parsed_shrink(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;
}
char*** yon_char_parsed_shrink_n3(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) {
            yon_char_parsed_copy(&new_char_parsed[i],&char_string[i]);
        }
        else if (flag == 1 && i!=item_to_delete) {
            yon_char_parsed_copy(&new_char_parsed[i-1], &char_string[i]);
        }
    }
    (*size)=(*size)-1;
    return new_char_parsed;
}
dictionary *yon_dictionary_create_empty() {
    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;
}
#ifdef VTE_TERMINAL
void yon_terminal_integrated_launch(GtkWidget *terminal, char* command, void *endwork_function, void* endwork_function_argument){
    char **commands=new_arr(char*,2);
    gchar **envp = g_get_environ();
    commands[0]=(gchar *)g_strdup(g_environ_getenv(envp, "SHELL"));
    commands[1]=NULL;
    char **env=new_arr(char*,2);
    env[0]="";
    env[1]=NULL;
    vte_terminal_set_size(VTE_TERMINAL(terminal),10,15);
    VtePty *pty = vte_pty_new_sync(VTE_PTY_DEFAULT,NULL,NULL);
    vte_terminal_set_pty(VTE_TERMINAL(terminal),pty);
    char *install_command=yon_char_unite("tput cup 0 0 && tput ed; ",command, "; exit 0\n",NULL); 
    if(endwork_function)
        g_signal_connect(G_OBJECT(terminal), "child-exited", G_CALLBACK(endwork_function), endwork_function_argument);
     vte_terminal_spawn_async(VTE_TERMINAL(terminal),
        VTE_PTY_DEFAULT,
        NULL,   
        commands,   
        NULL,  
        0,      
        NULL, NULL,
        NULL,   
        -1, 
        NULL, 
        child_ready,    
        install_command); 
    vte_pty_spawn_async(pty,
        NULL,        
        commands,      
        NULL,         
        0,            
        NULL, NULL,   
        NULL,         
        -1,           
        NULL,         
        NULL,           
        NULL);        
        vte_terminal_set_scrollback_lines(VTE_TERMINAL(terminal), -1);
        vte_terminal_set_scroll_on_output(VTE_TERMINAL(terminal), TRUE);
        vte_terminal_set_scroll_on_keystroke(VTE_TERMINAL(terminal), TRUE);
        gtk_widget_show_all(terminal);
}
static void child_ready(VteTerminal *terminal, GPid pid, GError *error, gpointer user_data)
{
    if (!terminal) return;
    if (pid == -1) printf("Error\n\n\n");
    else vte_terminal_feed_child(VTE_TERMINAL(terminal),(char*)user_data,strlen((char*)user_data));
}
#endif
dictionary *yon_dictionary_create_conneced(dictionary *targetdict)
{
    targetdict = yon_dictionary_get_last(targetdict);
    targetdict->next = yon_dictionary_create_empty();
    targetdict->next->prev = targetdict;
    targetdict->next->first = targetdict->first;
    targetdict->next->data_type = DICTIONARY_OTHER_TYPE;
    return targetdict->next;
}
void yon_int_array_append(int **source, int append){
    int size=0;
    for (size=0;(*source)[size]!=-2;size++);
    *source = realloc(*source,(size+2)*sizeof(int));
    (*source)[size] = append;
    (*source)[size+1] = -2;
}

/**[EN]
 * int yon_config_save(char *command)
 * Saves config with [command]
 * [RU]
*/
int philos_config_save(char *command)
{   
    FILE *output = popen(command, "r");
    return 1;
}

void philos_array_int_pars_to(int** array, int to) {
    int flag = 0;
    int i = 0;
    for (i = 0; (*array)[i] != -2; i++) {
        if ((*array)[i]==-3) {
            (*array)[i] = to;
            flag = 1;
            break;
        }
    }
}

void philos_array_char_pars_to(char*** array, char* to) {
    int flag = 0;
    int i = 0;
    for (i = 0; (*array)[i] != NULL; i++) {
        if (strcmp((*array)[i], "-") == 0) {
            (*array)[i] = yon_char_new(to);
            flag = 1;
            break;
        }
    }
}

char** philos_char_parsed_append(char** parsed, int *size, char *string) {
    parsed = yon_char_parsed_append(parsed, size, string); 
    (*size)--;
    return parsed;
}

void philos_set_size_memory_integer(char* str_find, size_t* array_data) {
    if (strstr(str_find, " ")) {
        yon_char_divide_search(str_find, " ", -1);
    }
    char* simv_del_array[9] = {"K","M","G","T","k","m","g","t","%"};
    for (size_t i = 0; i < 9; i++) {
        if (strstr(str_find, simv_del_array[i])) {
            str_find =  yon_char_divide_search(str_find, simv_del_array[i], -1);
        }
    }
    if (strstr(str_find, "-")==NULL) {
        (*array_data) = atoll(str_find); 
    }
    else {
       (*array_data) = -1; 
    }
}

me_time time_convert(size_t seconds) {
    me_time t;
    if (seconds != -1) {
        double weeks = seconds / (7*24*60*60);
        double days = seconds / (24*60*60) - 7*weeks;
        double hours = seconds / (60*60) - 7*24*weeks - 24*days;
        double minutes = seconds / 60 - 7*24*60*weeks - 24*60*days - 60*hours;
        if (weeks>0) {
            t.weeks = (int)weeks;
        }
        else {
            t.weeks = 0;
        }
        if (days>0) {
            t.days = (int)days;
        }
        else {
            t.days = 0;
        }
        if (hours>0) {
            t.hours = (int)hours;
        }
        else {
            t.hours = 0;
        }
        if (minutes>0) {
            t.minutes = (int)minutes;
        }
        else {
            t.minutes = 0;
        }
        t.str_time = NULL;
    }
    else {
        t.weeks = 0;
        t.days = 0;
        t.hours = 0;
        t.hours = 0;
        t.str_time = NULL;
    }
    
    
    return t; 
}

char* fill_tree_view_id(int id) {
    if (id < 0) {
        return yon_char_new("AUTO");
    }
    else {
        return yon_char_from_int(id);
    }
}


char *philos_char_from_size_t(size_t 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 = g_malloc0(i * sizeof(char) + 1);
    sprintf(ch, "%d", int_to_convert);
    return ch;
}
size_t philos_convert_memory(char* num_memory_pow) {
    float num_f = atof(yon_char_new(num_memory_pow));
    if (strstr(num_memory_pow,"K") != NULL) {
        return (size_t)(num_f);
    }
    else if (strstr(num_memory_pow,"M") != NULL) {
        return (size_t)(num_f*1024);
    }
    else if (strstr(num_memory_pow,"G") != NULL) {
        return (size_t)(num_f*1024*1024);
    }
    else if (strstr(num_memory_pow,"T") != NULL) {
        return (size_t)(num_f*1024*1024*1024);
    }
    return 0;
}

