Archived
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.
FDF/maps_reader.c

130 lines
2.5 KiB
C
Raw Normal View History

2023-12-12 13:28:15 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* maps_reader.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 05:19:39 by adjoly #+# #+# */
2023-12-18 17:19:34 +01:00
/* Updated: 2023/12/18 17:18:43 by adjoly ### ########.fr */
2023-12-12 13:28:15 +01:00
/* */
/* ************************************************************************** */
#include "fdf.h"
2023-12-18 17:19:34 +01:00
#include "libft/libft.h"
#include <stddef.h>
#include <stdio.h>
void ft_putstr(char *s){int i = 0;while(s[i]){write(1, &s[i], 1);i++;}}
2023-12-12 13:28:15 +01:00
size_t ft_countline_fd(int fd)
{
size_t i;
ssize_t rd;
size_t count;
char *buf;
i = 0;
count = 1;
2023-12-18 17:19:34 +01:00
buf = ft_calloc(1, 1);
2023-12-12 13:28:15 +01:00
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);
}
2023-12-18 17:19:34 +01:00
size_t ft_countline(char **map)
{
size_t i;
while (map[i])
i++;
return (i);
}
2023-12-12 13:28:15 +01:00
int ft_read_map(int fd, char **map)
{
size_t i;
2023-12-18 17:19:34 +01:00
i = 0;
2023-12-12 13:28:15 +01:00
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;
2023-12-18 17:19:34 +01:00
int line_nb;
2023-12-12 13:28:15 +01:00
fd = open(file, O_RDONLY);
2023-12-18 17:19:34 +01:00
line_nb = ft_countline_fd(fd);
map = ft_calloc(sizeof(char), line_nb);
2023-12-12 13:28:15 +01:00
close(fd);
2023-12-18 17:19:34 +01:00
printf("linenb %d", line_nb);
2023-12-12 13:28:15 +01:00
fd = open(file, O_RDONLY);
ft_read_map(fd, map);
return (map);
}
2023-12-18 17:19:34 +01:00
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++;
}
}