119 lines
2.5 KiB
C
119 lines
2.5 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* spacer.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/02/15 13:35:50 by cchauvet #+# #+# */
|
|
/* Updated: 2023/02/16 16:26:15 by cchauvet ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libftx/libftx.h"
|
|
#include "minishell.h"
|
|
#include "utils/utils.h"
|
|
|
|
static char *ft_spacer_after(const char *str)
|
|
{
|
|
char *out;
|
|
char *temp;
|
|
size_t i;
|
|
|
|
out = ft_strdup(str);
|
|
if (out == NULL)
|
|
return (NULL);
|
|
i = 1;
|
|
while (out[i] != '\0')
|
|
{
|
|
while (ft_is_in_quote(out, i))
|
|
i++;
|
|
if (ft_is_in("><|", out[i - 1]))
|
|
{
|
|
while (out[i] == out[i - 1])
|
|
i++;
|
|
temp = ft_strreplace(out, " ", i, i);
|
|
free(out);
|
|
out = temp;
|
|
if (out == NULL)
|
|
return (NULL);
|
|
}
|
|
if (out[i] != '\0')
|
|
i++;
|
|
}
|
|
return (out);
|
|
}
|
|
|
|
static char *ft_spacer_before(const char *str)
|
|
{
|
|
char *out;
|
|
char *temp;
|
|
size_t i;
|
|
|
|
out = ft_strdup(str);
|
|
if (out == NULL)
|
|
return (NULL);
|
|
i = 0;
|
|
while (out[i + 1] != '\0')
|
|
{
|
|
while (ft_is_in_quote(out, i))
|
|
i++;
|
|
if (ft_is_in("><|", out[i + 1]))
|
|
{
|
|
while (out[i] == ' ')
|
|
i++;
|
|
while (out[i] == out[i + 1])
|
|
i++;
|
|
temp = ft_strreplace(out, " ", i + 1, i + 1);
|
|
free(out);
|
|
out = temp;
|
|
if (out == NULL)
|
|
return (NULL);
|
|
}
|
|
i++;
|
|
}
|
|
return (out);
|
|
}
|
|
|
|
static void ft_space_simplifier(char *str)
|
|
{
|
|
size_t i;
|
|
size_t y;
|
|
|
|
i = 0;
|
|
while (str[i] != '\0')
|
|
{
|
|
if (ft_is_in_quote(str, i))
|
|
i++;
|
|
y = 0;
|
|
while (str[y + i] == ' ')
|
|
y++;
|
|
if (y > 1)
|
|
{
|
|
ft_strshift(str + i, -y + 1);
|
|
y--;
|
|
}
|
|
i++;
|
|
}
|
|
}
|
|
|
|
char *ft_normalizer(char *str)
|
|
{
|
|
char *out;
|
|
char *temp;
|
|
|
|
if (ft_str_is_empty(str))
|
|
return (ft_strdup(" "));
|
|
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);
|
|
if (out[ft_strlen(out) - 1] == ' ')
|
|
out[ft_strlen(out) - 1] = '\0';
|
|
return (out);
|
|
}
|