moved env functions to libft

This commit is contained in:
Tanguy Maze
2019-12-25 11:45:05 +01:00
parent 2362474fd6
commit 8a421600f4
18 changed files with 320 additions and 203 deletions

31
libft/srcs/ft_envaddend.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envaddend.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 15:02:33 by tmaze #+# #+# */
/* Updated: 2019/12/09 13:00:42 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_env *ft_envaddend(t_env **alst, t_env *new)
{
t_env *tmp;
if (new == NULL)
return (NULL);
if (*alst == NULL)
*alst = new;
else
{
tmp = *alst;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
return (*alst);
}

32
libft/srcs/ft_envclean.c Normal file
View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envclean.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/06 09:53:25 by tmaze #+# #+# */
/* Updated: 2019/12/06 09:53:47 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_envclean(t_env **env)
{
t_env **it;
t_env *tmp;
it = env;
while (*it)
{
if (ft_strequ((*it)->val, ""))
{
tmp = *it;
*it = tmp->next;
ft_envdelelem(&tmp);
}
else
it = &((*it)->next);
}
}

25
libft/srcs/ft_envdel.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envdel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 14:51:20 by tmaze #+# #+# */
/* Updated: 2019/12/09 13:00:46 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_envdel(t_env **env)
{
t_env *tmp;
while (*env)
{
tmp = *env;
*env = (*env)->next;
ft_envdelelem(&tmp);
}
}

View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envdelelem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 14:43:45 by tmaze #+# #+# */
/* Updated: 2019/12/09 13:00:45 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_envdelelem(t_env **elem)
{
if (elem)
{
ft_strdel(&((*elem)->key));
ft_strdel(&((*elem)->val));
ft_memdel((void**)elem);
}
}

View File

@@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envgetelem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 16:46:17 by tmaze #+# #+# */
/* Updated: 2019/12/05 16:47:00 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_env *ft_envgetelem(char *key, t_env *env)
{
t_env *it;
it = env;
while (it && !ft_strequ(it->key, key))
it = it->next;
return (it);
}

29
libft/srcs/ft_envnew.c Normal file
View File

@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 14:54:55 by tmaze #+# #+# */
/* Updated: 2019/12/05 17:12:59 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_env *ft_envnew(char *env)
{
t_env *ret;
int i;
i = 0;
ret = NULL;
while (env[i] && env[i] != '=')
i++;
if (env[i] == '=' && (ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
if ((ret->val = ft_strsub(env, i + 1, ft_strlen(env) - i - 1)) == NULL
|| (ret->key = ft_strsub(env, 0, i)) == NULL)
ft_envdelelem(&ret);
return (ret);
}

28
libft/srcs/ft_envtochar.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envtochar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 17:28:25 by tmaze #+# #+# */
/* Updated: 2019/12/09 13:00:44 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_envtochar(t_env *env)
{
int size;
char *ret;
size = ft_strlen(env->key) + ft_strlen(env->val) + 1;
if ((ret = ft_strnew(size)) != NULL)
{
ft_strlcpy(ret, env->key, size);
ft_strlcat(ret, "=", size);
ft_strlcat(ret, env->val, size);
}
return (ret);
}

41
libft/srcs/ft_envtotab.c Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envtotab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 17:34:22 by tmaze #+# #+# */
/* Updated: 2019/12/05 17:34:41 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char **ft_envtotab(t_env *env)
{
t_env *it;
char *new;
char **ret;
int i;
i = 0;
ret = NULL;
it = env;
while (it && (++i))
it = it->next;
if ((ret = (char**)ft_memalloc(sizeof(char*) * (i + 1))) == NULL)
return (NULL);
it = env;
i = 0;
while (it)
{
if ((new = ft_envtochar(it)) == NULL)
ft_del_words_tables(&ret);
if (new == NULL)
break ;
ret[i++] = new;
it = it->next;
}
return (ret);
}

28
libft/srcs/ft_envupdate.c Normal file
View File

@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_envupdate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/05 19:10:38 by tmaze #+# #+# */
/* Updated: 2019/12/05 19:10:52 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_envupdate(char *key, t_env *env, char *val)
{
t_env *elem;
char *new;
new = NULL;
if ((elem = ft_envgetelem(key, env)) != NULL)
if ((new = ft_strdup(val)) != NULL)
{
ft_strdel(&elem->val);
elem->val = new;
}
return (new);
}

View File

@@ -1,4 +1,4 @@
/* ************************************************************************** */
cf/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/07 17:00:34 by tmaze #+# #+# */
/* Updated: 2018/04/10 14:15:23 by tmaze ### ########.fr */
/* Updated: 2019/12/09 13:00:48 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */