/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* env1.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ /* Updated: 2023/04/18 13:57:38 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "env_private.h" void add_sort(t_list **head, t_env *var) { t_list *current; t_env *last; current = *head; while (current->next != NULL && ft_strcmp(var->key, ((t_env *)(current->content))->key) > 0) current = current->next; if (current->content == NULL) current->content = var; else { last = NULL; swap_env_3((void **)&last, ¤t->content, (void **)&var); while (current->next != NULL) { current = current->next; swap_env(¤t->content, (void **)&last); } } if (current->next == NULL) { current->next = ft_calloc(1, sizeof(t_list)); if (!current->next) return ; } } char *get_value_by_key(char *key, t_list **head) { t_list *current; current = *head; while (current->next != NULL) { if (ft_strcmp(((t_env *)current->content)->key, key) == 0) return (((t_env *)current->content)->value); current = current->next; } return (NULL); } int set_value_by_key(char *key, char *value, t_list **head) { t_list *current; current = *head; while (current->next != NULL) { if (ft_strcmp(((t_env *)current->content)->key, key) == 0) { free(((t_env *)current->content)->value); ((t_env *)current->content)->value = value; return (0); } current = current->next; } return (1); } int create_value_by_key(char *key, char *value, t_list **head) { t_env *content; if (set_value_by_key(key, value, head) == 0) { free(key); return (0); } content = ft_calloc(1, sizeof(t_env)); if (content == NULL) return (1); content->key = key; content->value = value; add_sort(head, content); return (0); } t_list **init_env(char **env) { t_list **head; int i; t_env *var; head = ft_calloc(1, sizeof(t_list *)); if (head == NULL) return (NULL); *head = ft_calloc(1, sizeof(t_list)); if (*head == NULL) { free(head); return (NULL); } i = -1; while (env[++i]) { var = ft_calloc(1, sizeof(t_env)); if (var == NULL) return (NULL); var->key = get_key(env[i]); var->value = get_value(env[i]); add_sort(head, var); } return (head); }