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.
so_long/get_map.c

83 lines
1.9 KiB
C
Raw Permalink Normal View History

2024-01-08 13:33:55 +01:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/08 13:13:18 by adjoly #+# #+# */
2024-01-28 18:26:25 +01:00
/* Updated: 2024/01/27 15:45:48 by adjoly ### ########.fr */
2024-01-08 13:33:55 +01:00
/* */
/* ************************************************************************** */
2024-01-24 13:45:01 +01:00
#include "printf/ft_printf.h"
2024-01-08 13:33:55 +01:00
#include "so_long.h"
2024-01-24 13:45:01 +01:00
#include <stddef.h>
2024-01-08 13:33:55 +01:00
2024-01-24 13:45:01 +01:00
size_t ft_filesize(char *file_name)
2024-01-08 13:33:55 +01:00
{
2024-01-09 16:12:58 +01:00
size_t i;
2024-01-22 14:13:58 +01:00
int fd;
2024-01-24 13:45:01 +01:00
ssize_t rd;
char *buf;
2024-01-08 13:33:55 +01:00
2024-01-22 14:13:58 +01:00
fd = open(file_name, O_RDONLY);
2024-01-24 13:45:01 +01:00
buf = ft_calloc(1, 1);
if (!buf)
2024-01-22 14:13:58 +01:00
return (0);
2024-01-09 16:12:58 +01:00
i = 0;
2024-01-24 13:45:01 +01:00
while (buf && i < SIZE_MAX)
2024-01-08 13:33:55 +01:00
{
2024-01-09 16:12:58 +01:00
rd = read(fd, buf, 1);
2024-01-24 13:45:01 +01:00
if (rd <= 0)
2024-01-09 16:12:58 +01:00
break ;
i++;
2024-01-08 13:33:55 +01:00
}
2024-01-19 15:41:43 +01:00
free(buf);
2024-01-24 13:45:01 +01:00
close(fd);
return (i);
2024-01-08 13:33:55 +01:00
}
2024-01-24 13:45:01 +01:00
char **ft_getmap(int fd, char **map_read, char *buf, char *tmp)
2024-01-08 13:33:55 +01:00
{
2024-01-24 13:45:01 +01:00
ssize_t rd;
2024-01-09 16:12:58 +01:00
size_t i;
i = 0;
2024-01-24 13:45:01 +01:00
while (buf && i < SIZE_MAX)
2024-01-09 16:12:58 +01:00
{
2024-01-24 13:45:01 +01:00
rd = read(fd, buf, 1);
if (rd <= 0)
2024-01-09 16:12:58 +01:00
break ;
2024-01-24 13:45:01 +01:00
tmp[i] = buf[0];
2024-01-09 16:12:58 +01:00
i++;
}
close(fd);
2024-01-24 13:45:01 +01:00
tmp[i] = '\0';
map_read = ft_split(tmp, '\n');
return (map_read);
}
2024-01-28 18:26:25 +01:00
char **ft_read_map(char *file_name, char **map_read)
2024-01-24 13:45:01 +01:00
{
int fd;
char *tmp;
char *buf;
size_t filesize;
filesize = ft_filesize(file_name);
if (filesize == 0)
return (NULL);
tmp = ft_calloc(filesize + 1, 1);
if (!tmp)
return (NULL);
buf = ft_calloc(1, 1);
if (!buf)
return (NULL);
fd = open(file_name, O_RDONLY);
map_read = ft_getmap(fd, map_read, buf, tmp);
free(buf);
free(tmp);
2024-01-09 16:12:58 +01:00
return (map_read);
2024-01-08 13:33:55 +01:00
}