130 lines
3.1 KiB
C
130 lines
3.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* pars_map.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/11/12 16:16:49 by lfirmin #+# #+# */
|
|
/* Updated: 2024/11/16 18:41:22 by lfirmin ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../include/so_long.h"
|
|
|
|
int ft_count_lines(char *file)
|
|
{
|
|
char c;
|
|
int fd;
|
|
int ret;
|
|
int count;
|
|
int last;
|
|
|
|
fd = open(file, O_RDONLY);
|
|
if (fd < 0)
|
|
return (-1);
|
|
count = 0;
|
|
last = '\n';
|
|
ret = read(fd, &c, 1);
|
|
while (ret > 0)
|
|
{
|
|
if (c == '\n')
|
|
count++;
|
|
last = c;
|
|
ret = read(fd, &c, 1);
|
|
}
|
|
if (last != '\n')
|
|
count++;
|
|
return (close(fd), count);
|
|
}
|
|
|
|
char *add_newline_if_needed(char *line)
|
|
{
|
|
size_t len;
|
|
char *new_line;
|
|
|
|
if (!line)
|
|
return (NULL);
|
|
len = strlen(line);
|
|
if (len > 0 && line[len - 1] != '\n')
|
|
{
|
|
new_line = malloc(len + 2);
|
|
if (!new_line)
|
|
return (NULL);
|
|
strcpy(new_line, line);
|
|
new_line[len] = '\n';
|
|
new_line[len + 1] = '\0';
|
|
free(line);
|
|
return (new_line);
|
|
}
|
|
return (line);
|
|
}
|
|
|
|
int get_map(char *file, t_data *data)
|
|
{
|
|
int fd;
|
|
int i;
|
|
int nb_lines;
|
|
|
|
nb_lines = ft_count_lines(file);
|
|
if (nb_lines <= 0)
|
|
return (ft_printf_fd(2, "Error : The map is empty\n"), 1);
|
|
data->map = malloc(sizeof(char *) * (nb_lines + 1));
|
|
if (!data->map)
|
|
return (1);
|
|
i = 0;
|
|
fd = open(file, O_RDONLY);
|
|
if (fd == -1)
|
|
return (ft_printf_fd(2, "Error : opening file %s\n", file), 1);
|
|
if (get_map_2(i, nb_lines, data, fd) == 1)
|
|
return (close(fd), 1);
|
|
return (close(fd), 0);
|
|
}
|
|
|
|
int check_close(t_data *data)
|
|
{
|
|
int i;
|
|
int j;
|
|
|
|
i = 1;
|
|
if (check_first_last_lines(data, 0) == 1)
|
|
return (1);
|
|
while (data->map[i + 1])
|
|
{
|
|
j = 1;
|
|
if (data->map[i][0] != '1')
|
|
return (ft_printf_fd(2, "Error : Map not closed\n"), 1);
|
|
while (data->map[i][j + 1])
|
|
j++;
|
|
if (data->map[i][j - 1] != '1')
|
|
return (ft_printf_fd(2, "Error : Map not closed\n"), 1);
|
|
i++;
|
|
}
|
|
return (check_first_last_lines(data, i));
|
|
}
|
|
|
|
int check_first_last_lines(t_data *data, int i)
|
|
{
|
|
int j;
|
|
|
|
j = 0;
|
|
while (data->map[0][j + 1] && data->map[0][j + 1] != '\n')
|
|
{
|
|
if (data->map[0][j] != '1')
|
|
return (ft_printf_fd(2, "Error : Map not closed\n"), 1);
|
|
j++;
|
|
}
|
|
if (data->map[0][j] != '1')
|
|
return (ft_printf_fd(2, "Error : Map not closed\n"), 1);
|
|
j = 0;
|
|
while (data->map[i][j + 1] && data->map[i][j + 1] != '\n')
|
|
{
|
|
if (data->map[i][j] != '1')
|
|
return (ft_printf_fd(2, "Error : Map not closed\n"), 1);
|
|
j++;
|
|
}
|
|
if (data->map[i][j] != '1')
|
|
return (ft_printf_fd(2, "Error : Map not closeds\n"), 1);
|
|
return (0);
|
|
}
|