42_minishell/format/format.c

131 lines
2.7 KiB
C
Raw Normal View History

2023-02-15 14:52:27 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2023-03-28 09:55:08 -04:00
/* format.c :+: :+: :+: */
2023-02-15 14:52:27 -05:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 13:35:50 by cchauvet #+# #+# */
/* Updated: 2023/03/31 15:22:21 by alouis-j ### ########.fr */
2023-02-15 14:52:27 -05:00
/* */
/* ************************************************************************** */
#include "./format_private.h"
2023-02-15 14:52:27 -05:00
2023-03-29 13:07:57 -04:00
static int ft_replace(char **str, size_t i)
{
char *temp;
temp = ft_strreplace(*str, " ", i, i);
free(*str);
if (temp == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
*str = temp;
return (0);
}
2023-02-15 14:52:27 -05:00
static char *ft_spacer_after(const char *str)
{
char *out;
size_t i;
out = ft_strdup(str);
if (out == NULL)
return (NULL);
i = 1;
while (out[i] != '\0')
{
2023-03-13 14:28:46 -04:00
while (ft_is_in_quote(out, i - 1))
2023-02-16 07:53:49 -05:00
i++;
if (out[i - 1] == '\0' || out[i] == '\0')
2023-03-13 14:28:46 -04:00
break ;
2023-02-15 14:52:27 -05:00
if (ft_is_in("><|", out[i - 1]))
{
2023-02-24 09:51:12 -05:00
while (out[i] == out[i - 1])
2023-02-15 14:52:27 -05:00
i++;
2023-03-29 13:07:57 -04:00
if (ft_replace(&out, i))
2023-02-15 14:52:27 -05:00
return (NULL);
}
2023-02-28 09:21:47 -05:00
if (out[i] != '\0')
i++;
2023-02-15 14:52:27 -05:00
}
return (out);
}
static char *ft_spacer_before(const char *str)
{
char *out;
2023-03-29 13:07:57 -04:00
ssize_t i;
2023-02-15 14:52:27 -05:00
out = ft_strdup(str);
if (out == NULL)
return (NULL);
2023-03-29 13:07:57 -04:00
i = -1;
while (out[++i] != '\0')
2023-02-15 14:52:27 -05:00
{
while (ft_is_in_quote(out, i + 1))
2023-02-16 07:53:49 -05:00
i++;
2023-03-13 14:28:46 -04:00
if (out[i] == '\0')
break ;
2023-02-15 14:52:27 -05:00
if (ft_is_in("><|", out[i + 1]))
{
while (out[i] == ' ')
i++;
while (out[i] == out[i + 1])
i++;
2023-03-29 13:07:57 -04:00
if (ft_replace(&out, i + 1))
2023-02-15 14:52:27 -05:00
return (NULL);
}
}
return (out);
}
static void ft_space_simplifier(char *str)
{
size_t i;
size_t y;
i = 0;
while (str[i] != '\0')
{
2023-02-16 07:53:49 -05:00
if (ft_is_in_quote(str, i))
i++;
2023-03-13 14:28:46 -04:00
if (str[i] != '\0')
break ;
2023-02-15 14:52:27 -05:00
y = 0;
while (str[y + i] == ' ')
y++;
if (y > 1)
{
ft_strshift(str + i, -y + 1);
y--;
}
i++;
}
}
2023-03-13 10:34:43 -04:00
char *ft_formater(t_data *data, const char *str)
2023-02-15 14:52:27 -05:00
{
char *out;
char *temp;
2023-02-16 10:29:46 -05:00
if (ft_str_is_empty(str))
return (ft_strdup(" "));
2023-02-15 14:52:27 -05:00
temp = ft_spacer_after(str);
if (temp == NULL)
return (NULL);
out = ft_spacer_before(temp);
free(temp);
if (out == NULL)
return (NULL);
ft_space_simplifier(out);
2023-02-16 10:29:46 -05:00
if (out[ft_strlen(out) - 1] == ' ')
out[ft_strlen(out) - 1] = '\0';
2023-03-13 10:34:43 -04:00
temp = ft_env_filler(data, out);
free(out);
return (temp);
2023-02-15 14:52:27 -05:00
}