42_C_PISCINE/Rush/Rush02/ex00/dictionary_checker.c

115 lines
2.8 KiB
C
Raw Normal View History

2023-05-17 12:45:25 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dictionary_checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/31 20:31:53 by cchauvet #+# #+# */
/* Updated: 2022/07/31 20:36:31 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
char *ft_doubleline(char *str)
{
int i;
int j;
char *tab;
i = 0;
j = 0;
tab = malloc(sizeof(*str) * ft_strlen(str + 1));
while (str[i] != '\0')
{
tab[j] = str[i];
i++;
j++;
if (str[i] == '\n' && str[i + 1] == '\n')
while (str[i + 1] == '\n' && str[i])
i++;
}
tab[i] = '\0';
return (tab);
}
t_number *ft_dictionnary_reader(char *path)
{
char *str;
char **lines;
int i;
t_number *tab;
str = ft_filereader(path);
str = ft_doubleline(str);
lines = ft_split(str, "\n");
tab = malloc((ft_lines_counter(lines) + 1) * sizeof(*tab));
i = -1;
while (++i < ft_lines_counter(lines))
{
tab[i].nbr = malloc(sizeof(*(tab[i].nbr)) * ft_strlen(ft_atonbr(lines[i])));
tab[i].apc = malloc(sizeof(*(tab[i].apc)) * ft_strlen(ft_atoapc(lines[i])));
if (ft_strlen(ft_atonbr(lines[i])) * ft_strlen(ft_atoapc(lines[i])) == 0)
tab[i].nbr = "";
else
{
if (ft_print_dicterror(lines[i]) == 0)
return (tab = NULL);
tab[i].nbr = ft_atonbr(lines[i]);
tab[i].apc = ft_atoapc(lines[i]);
}
}
tab[i].nbr = "";
tab[i].apc = "";
return (tab);
}
int ft_dict_is_valid(char *path)
{
t_number *my_dict;
t_number *other_dict;
int i;
int j;
my_dict = ft_dictionnary_reader(PATH);
other_dict = ft_dictionnary_reader(path);
if (my_dict == NULL || other_dict == NULL)
return (0);
i = 0;
while (my_dict[i].nbr[0] != '\0')
{
j = 0;
while (other_dict[j].nbr[0] != '\0')
{
if (ft_strcmp(my_dict[i].nbr, other_dict[i].nbr) == 0)
break;
j++;
}
if (ft_strcmp(my_dict[i].nbr, other_dict[i].nbr) != 0)
{
free(other_dict);
free(my_dict);
return (0);
}
i++;
}
free(other_dict);
free(my_dict);
return (1);
}
/*
int main()
{
int i;
char *path;
printf("%d", ft_dict_is_valide("./numbers_test.dict"));
path = "numbers_test.dict";
i = 0;
while (ft_dictionnary_reader(path)[++i].nbr[0] != '\0')
printf("%s : %s\n", ft_dictionnary_reader(path)[i].nbr, ft_dictionnary_reader(PATH)[i].apc);
}
*/