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

97 lines
2.0 KiB
C
Raw Permalink 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 #+# #+# */
2024-01-06 11:10:54 +01:00
/* Updated: 2024/01/06 08:18:22 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);
}
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);
}
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);
}
2024-01-06 11:10:54 +01:00
t_map **ft_get_map(char *file)
2023-12-18 17:19:34 +01:00
{
2024-01-06 11:10:54 +01:00
t_map **parsed_map;
char **map_read;
2024-01-04 16:36:32 +01:00
int fd;
2024-01-06 11:10:54 +01:00
int map_size;
2024-01-04 16:36:32 +01:00
fd = open(file, O_RDONLY);
2024-01-06 11:10:54 +01:00
map_size = ft_countline_fd(fd);
close (fd);
2024-01-04 16:36:32 +01:00
fd = open(file, O_RDONLY);
2024-01-06 11:10:54 +01:00
map_read = ft_read_map(fd, map_size);
close (fd);
return (parsed_map);
2024-01-04 16:36:32 +01:00
}
int main(int ac, char **av)
{
2024-01-06 11:10:54 +01:00
return (0);
2023-12-18 17:19:34 +01:00
}