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

149 lines
3.0 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-23 09:25:01 +01:00
/* Updated: 2023/12/23 08:29:30 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 <stdio.h>
2023-12-23 09:25:01 +01:00
2023-12-18 17:19:34 +01:00
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);
2023-12-23 09:25:01 +01:00
map[i + 1] = NULL;
2023-12-12 13:28:15 +01:00
return (i);
}
2023-12-23 09:25:01 +01:00
char **ft_getmap(const char *file)
2023-12-12 13:28:15 +01:00
{
char **map;
int fd;
2023-12-18 17:19:34 +01:00
int line_nb;
2023-12-12 13:28:15 +01:00
2023-12-23 09:25:01 +01:00
printf("mais çá fait quelque chose au moins\n");
2023-12-12 13:28:15 +01:00
fd = open(file, O_RDONLY);
2023-12-23 09:25:01 +01:00
printf("file opened");
2023-12-18 17:19:34 +01:00
line_nb = ft_countline_fd(fd);
2023-12-12 13:28:15 +01:00
close(fd);
2023-12-23 09:25:01 +01:00
map = ft_calloc(sizeof(char), line_nb);
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
2023-12-20 14:14:35 +01:00
t_map ft_split_height_color(char *tmp)
2023-12-18 17:19:34 +01:00
{
2023-12-20 14:14:35 +01:00
t_map height_color;
2023-12-18 17:19:34 +01:00
int i;
2023-12-20 14:14:35 +01:00
height_color.y = ft_atoi(tmp);
while (tmp[i] && tmp[i] != ',')
i++;
if (tmp[i] == ',')
2023-12-18 17:19:34 +01:00
{
i++;
2023-12-20 14:14:35 +01:00
height_color.color = ft_strdup(&tmp[i]);
return (height_color);
}
else
{
height_color.color = ft_strdup("0xFFFFFF");
return (height_color);
2023-12-18 17:19:34 +01:00
}
}
t_map **ft_parse_map(char **mapfile)
{
int z;
2023-12-20 14:14:35 +01:00
int x;
2023-12-18 17:19:34 +01:00
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);
2023-12-20 14:14:35 +01:00
x = 0;
while (tmp[x])
{
parsed_map[z][x] = ft_split_height_color(tmp[x]);
x++;
}
2023-12-18 17:19:34 +01:00
}
return (parsed_map);
}
int main(int ac, char **av)
{
char **map;
int i = 0;
2023-12-23 09:25:01 +01:00
printf("%s", av[1]);
2023-12-18 17:19:34 +01:00
map = ft_getmap(av[1]);
2023-12-23 09:25:01 +01:00
(void)ac;
2023-12-18 17:19:34 +01:00
ft_putstr("map read");
while (map[i])
{
ft_putstr(map[i]);
2023-12-23 09:25:01 +01:00
free(map[i]);
2023-12-18 17:19:34 +01:00
i++;
}
2023-12-23 09:25:01 +01:00
free(map);
2023-12-18 17:19:34 +01:00
}
2023-12-23 09:25:01 +01:00
//cc maps_reader.c fdf.h get_next_line/get_next_line.c get_next_line/get_next_line.h get_next_line/get_next_line_utils.c libft/libft.a