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 #+# #+# */
|
2024-01-04 16:36:32 +01:00
|
|
|
/* Updated: 2024/01/04 16:22:01 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"
|
2024-01-04 16:36:32 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stddef.h>
|
2023-12-18 17:19:34 +01:00
|
|
|
#include <stdio.h>
|
2024-01-04 16:36:32 +01:00
|
|
|
#include "printf/ft_printf.h"
|
2023-12-23 09:25:01 +01:00
|
|
|
|
2024-01-04 16:36:32 +01:00
|
|
|
// void ft_putstr(char *s){int i = 0;while(s[i]){write(1, &s[i], 1);i++;}}
|
|
|
|
|
|
|
|
void ft_free(void **map)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (map[i])
|
|
|
|
{
|
|
|
|
free(map[i]);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
free(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
t_map ft_split_height_color(char *tmp)
|
|
|
|
{
|
|
|
|
t_map height_color;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
height_color.y = ft_atoi(tmp);
|
|
|
|
while (tmp[i] && tmp[i] != ',')
|
|
|
|
i++;
|
|
|
|
if (tmp[i] == ',')
|
|
|
|
height_color.color = ft_strdup(&tmp[i + 1]);
|
|
|
|
else
|
|
|
|
height_color.color = ft_strdup("0xFFFFFF");
|
|
|
|
return (height_color);
|
|
|
|
}
|
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);
|
2024-01-04 16:36:32 +01:00
|
|
|
while (i < ULONG_MAX)
|
2023-12-12 13:28:15 +01:00
|
|
|
{
|
|
|
|
rd = read(fd, buf, 1);
|
2024-01-04 16:36:32 +01:00
|
|
|
if (rd == -1 || rd == 0)
|
2023-12-12 13:28:15 +01:00
|
|
|
break ;
|
|
|
|
else if (buf[0] == '\n')
|
|
|
|
count++;
|
|
|
|
i++;
|
|
|
|
}
|
2024-01-04 16:36:32 +01:00
|
|
|
free(buf);
|
2023-12-12 13:28:15 +01:00
|
|
|
if (i == 0)
|
|
|
|
return (-1);
|
|
|
|
return (count);
|
|
|
|
}
|
|
|
|
|
2023-12-18 17:19:34 +01:00
|
|
|
size_t ft_countline(char **map)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
i = 0;
|
2024-01-04 16:36:32 +01:00
|
|
|
while (map[i])
|
2023-12-12 13:28:15 +01:00
|
|
|
i++;
|
|
|
|
return (i);
|
|
|
|
}
|
|
|
|
|
2024-01-04 16:36:32 +01:00
|
|
|
t_map **ft_parse_map(char **mapfile, size_t line_count)
|
2023-12-18 17:19:34 +01:00
|
|
|
{
|
|
|
|
int z;
|
2023-12-20 14:14:35 +01:00
|
|
|
int x;
|
2023-12-18 17:19:34 +01:00
|
|
|
char **tmp;
|
|
|
|
t_map **parsed_map;
|
2024-01-04 16:36:32 +01:00
|
|
|
size_t i;
|
2023-12-18 17:19:34 +01:00
|
|
|
|
|
|
|
z = 0;
|
2024-01-04 16:36:32 +01:00
|
|
|
parsed_map = ft_calloc(sizeof(t_map *), line_count);
|
2023-12-18 17:19:34 +01:00
|
|
|
while (mapfile[z])
|
|
|
|
{
|
|
|
|
tmp = ft_split(mapfile[z], 32);
|
2024-01-04 16:36:32 +01:00
|
|
|
i = ft_countline(tmp);
|
|
|
|
parsed_map[z] = ft_calloc(i, sizeof(t_map *));
|
2023-12-20 14:14:35 +01:00
|
|
|
x = 0;
|
|
|
|
while (tmp[x])
|
|
|
|
{
|
|
|
|
parsed_map[z][x] = ft_split_height_color(tmp[x]);
|
|
|
|
x++;
|
|
|
|
}
|
2024-01-04 16:36:32 +01:00
|
|
|
parsed_map[z][x] = NULL;
|
|
|
|
z++;
|
2023-12-18 17:19:34 +01:00
|
|
|
}
|
2024-01-04 16:36:32 +01:00
|
|
|
parsed_map[z] = NULL;
|
2023-12-18 17:19:34 +01:00
|
|
|
return (parsed_map);
|
|
|
|
}
|
|
|
|
|
2024-01-04 16:36:32 +01:00
|
|
|
char **ft_read_map(int fd, size_t map_size)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
char **map;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
map = ft_calloc(sizeof(char *), map_size + 1);
|
|
|
|
while (i < map_size)
|
|
|
|
{
|
|
|
|
map[i] = get_next_line(fd);
|
|
|
|
if (map[i] == NULL)
|
|
|
|
break ;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return (map);
|
|
|
|
}
|
|
|
|
|
|
|
|
t_map **ft_getmap(char *file)
|
2023-12-18 17:19:34 +01:00
|
|
|
{
|
2024-01-04 16:36:32 +01:00
|
|
|
t_map **map_parsed;
|
2023-12-18 17:19:34 +01:00
|
|
|
char **map;
|
2024-01-04 16:36:32 +01:00
|
|
|
int fd;
|
|
|
|
size_t line_nb;
|
|
|
|
|
|
|
|
fd = open(file, O_RDONLY);
|
|
|
|
line_nb = ft_countline_fd(fd);
|
|
|
|
close(fd);
|
|
|
|
fd = open(file, O_RDONLY);
|
|
|
|
map = ft_read_map(fd, line_nb);
|
|
|
|
close(fd);
|
|
|
|
map_parsed = ft_parse_map(map, line_nb);
|
|
|
|
ft_free((void **)map);
|
|
|
|
return (map_parsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
|
|
|
t_map **map;
|
2023-12-18 17:19:34 +01:00
|
|
|
int i = 0;
|
2024-01-04 16:36:32 +01:00
|
|
|
char *file_name;
|
|
|
|
|
|
|
|
file_name = ft_strdup(av[1]);
|
|
|
|
map = ft_getmap(file_name);
|
2023-12-23 09:25:01 +01:00
|
|
|
(void)ac;
|
2024-01-04 16:36:32 +01:00
|
|
|
ft_putstr("map read\n");
|
2023-12-18 17:19:34 +01:00
|
|
|
while (map[i])
|
|
|
|
{
|
2024-01-04 16:36:32 +01:00
|
|
|
ft_printf("%s", map[i]->color);
|
|
|
|
ft_putchar('\n');
|
|
|
|
ft_putnbr(map[i]->y);
|
|
|
|
ft_putchar('\n');
|
2023-12-18 17:19:34 +01:00
|
|
|
i++;
|
|
|
|
}
|
2024-01-04 16:36:32 +01:00
|
|
|
ft_free((void **)map);
|
|
|
|
return(0);
|
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
|