1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
42-1st-piscine/ended/rush02/ex00/ft_parse_dict.c

114 lines
2.3 KiB
C
Raw Normal View History

2023-08-06 20:12:38 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_dict.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: julemart <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/30 18:30:17 by julemart #+# #+# */
/* Updated: 2022/07/31 15:20:02 by julemart ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
char **ft_cut_line2(char *str, char **tab, int count_line, int i)
{
int j;
int k;
while (str[i])
{
j = i;
while (str[i] != '\n' && str[i])
i++;
tab[count_line] = malloc(sizeof(char) * (i - j + 1));
k = 0;
while (str[j] != '\n' && str[j])
{
tab[count_line][k] = str[j];
j++;
k++;
}
if (k > 0)
tab[count_line][k] = '\0';
if (k > 0)
count_line++;
else
free(tab[count_line]);
i++;
}
return (tab);
}
char **ft_cut_line(char *str)
{
int i;
int count_line;
char **tab;
i = 0;
count_line = 1;
while (str[i])
{
if (str[i] == '\n' && str[i - 1] != '\n')
count_line++;
i++;
}
tab = malloc(sizeof(char *) * (count_line));
i = 0;
count_line = 0;
tab = ft_cut_line2(str, tab, count_line, i);
return (tab);
}
char **ft_dict_to_line(char *path)
{
int fd;
int size;
char tmp[1];
char *str;
char **tab_line;
size = 0;
fd = open(path, O_RDONLY);
if (fd == -1)
{
ft_dict_error();
return (NULL);
}
while (read(fd, tmp, 1))
size++;
close(fd);
str = malloc(sizeof(char) * (size + 1));
fd = open(path, O_RDONLY);
read(fd, str, size);
close(fd);
str[size] = '\0';
tab_line = ft_cut_line(str);
free(str);
return (tab_line);
}
int main(void)
{
char **tab_line;
int i;
i = 0;
tab_line = ft_dict_to_line("numbers.dict.txt");
while (tab_line[i])
{
printf("%s\n", tab_line[i]);
i++;
}
i = 0;
while (tab_line[i])
{
free(tab_line[i]);
i++;
}
free(tab_line);
return (0);
}