From 3543807548f82df00de372a659aa83a19be8d968 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 23 Jul 2025 13:31:58 +0200 Subject: [PATCH] fix: give error for trailing chars after map https://www.notion.so/Pas-d-erreur-detect-pour-une-map-avec-des-characters-apres-la-map-239551de06f480bc8f07e3fbe844ecd7?v=233551de06f480718417000cf26e3225&source=copy_link --- src/map/map_checker.c | 5 +++-- src/map/populate_map.c | 21 +++++++++++++++++++-- src/map/populate_map.h | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/map/map_checker.c b/src/map/map_checker.c index b029551..ed8ce08 100644 --- a/src/map/map_checker.c +++ b/src/map/map_checker.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */ -/* Updated: 2025/07/22 12:15:32 by kcolin ### ########.fr */ +/* Updated: 2025/07/24 12:20:17 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,7 +69,8 @@ bool check_cubfile(char *file, t_mapdata *map) return (close(fd), false); if (map->error[0]) return (close(fd), false); - populate_maps(map, fd); + if (!populate_maps(map, fd)) + return (close(fd), false); if (!check_walls(map)) return (close(fd), ft_strlcpy(map->error, "Map is malformed (invalid chars or missing walls)", ERRLEN), diff --git a/src/map/populate_map.c b/src/map/populate_map.c index d2f8f1c..e35240e 100644 --- a/src/map/populate_map.c +++ b/src/map/populate_map.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:44:02 by kcolin #+# #+# */ -/* Updated: 2025/07/21 15:23:47 by kcolin ### ########.fr */ +/* Updated: 2025/07/23 13:34:33 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,17 +52,34 @@ void add_map_line(const char *line, t_mapdata *map) map->mapflood = newmapflood; } -void populate_maps(t_mapdata *map, int fd) +/* +** return values: +** true: everything is fine +** false: error occured, caller should look at map->error +*/ +bool populate_maps(t_mapdata *map, int fd) { char *line; + bool end_reached; + bool retvalue; line = get_next_line(fd); + end_reached = false; + retvalue = true; while (line) { + if (line[0] != '\n' && end_reached) + { + ft_strlcpy(map->error, "Trailing chars after map", ERRLEN); + retvalue = false; + } if (line[0] != '\n') add_map_line(line, map); + if (line[0] == '\n') + end_reached = true; free(line); line = get_next_line(fd); } free(line); + return (retvalue); } diff --git a/src/map/populate_map.h b/src/map/populate_map.h index 828090c..50c434e 100644 --- a/src/map/populate_map.h +++ b/src/map/populate_map.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/21 15:24:31 by kcolin #+# #+# */ -/* Updated: 2025/07/21 15:25:02 by kcolin ### ########.fr */ +/* Updated: 2025/07/23 13:34:42 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,6 @@ # include "mapdata.h" -void populate_maps(t_mapdata *map, int fd); +bool populate_maps(t_mapdata *map, int fd); #endif // POPULATE_MAP_H