/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* maps_reader.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/12 05:19:39 by adjoly #+# #+# */ /* Updated: 2023/12/18 17:18:43 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "fdf.h" #include "libft/libft.h" #include #include void ft_putstr(char *s){int i = 0;while(s[i]){write(1, &s[i], 1);i++;}} size_t ft_countline_fd(int fd) { size_t i; ssize_t rd; size_t count; char *buf; i = 0; count = 1; buf = ft_calloc(1, 1); while (i <= ULONG_MAX) { rd = read(fd, buf, 1); if (rd == -1) break ; else if (buf[0] == '\n') count++; i++; } if (i == 0) return (-1); return (count); } size_t ft_countline(char **map) { size_t i; while (map[i]) i++; return (i); } int ft_read_map(int fd, char **map) { size_t i; i = 0; while (map[i] && i <= ULONG_MAX) { map[i] = get_next_line(fd); i++; } if (i == 0 && map[i] == NULL) return (-1); close(fd); return (i); } char **ft_getmap(char *file) { char **map; int fd; int line_nb; fd = open(file, O_RDONLY); line_nb = ft_countline_fd(fd); map = ft_calloc(sizeof(char), line_nb); close(fd); printf("linenb %d", line_nb); fd = open(file, O_RDONLY); ft_read_map(fd, map); return (map); } t_map *ft_split_height_color(char *tmp) { t_map *height_color; int i; while (tmp[i]) { i++; } return (); } t_map **ft_parse_map(char **mapfile) { int z; int line_count; char **tmp; t_map **parsed_map; z = 0; line_count = ft_countline(mapfile); parsed_map = ft_calloc(sizeof(t_map), line_count); while (mapfile[z]) { tmp = ft_split(mapfile[z], 32); parsed_map[z] = ft_split_height_color(tmp); } return (parsed_map); } int main(int ac, char **av) { (void)ac; char **map; int i = 0; map = ft_getmap(av[1]); ft_putstr("map read"); while (map[i]) { ft_putstr(map[i]); i++; } }