From 073fd2f2ff451bb9867c447688b1b28b14e0c7fb Mon Sep 17 00:00:00 2001 From: Maelys Date: Sat, 28 Sep 2024 20:48:32 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=9A=A7=E3=80=8D=20test:=20testin?= =?UTF-8?q?g=20things,=20might=20broke.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 7 +++ includes/cub3d.h | 3 +- includes/mess_err.h | 12 ++++- includes/parsing.h | 50 +++++++++++++++--- map.cub | 35 +++++++------ src/cub3d.c | 34 +++++++++--- src/parsing/check_id_text.c | 76 ++------------------------- src/parsing/check_image.c | 99 +++++++++++++++++++++++++++++++++++ src/parsing/check_map.c | 37 +++---------- src/parsing/check_rgb.c | 71 ++++++++++++++++++++++--- src/parsing/color_utils.c | 69 ++++++++++++++++++++++++ src/parsing/find_player.c | 64 ++++++++++++++++++++++ src/parsing/memo_parsing | 18 ------- src/parsing/principal_pars.c | 74 +++++++++++++++++--------- src/utils/clear_free.c | 41 +++++++++++++++ src/utils/parse_utils.c | 32 +---------- src/utils/parse_utils_three.c | 98 ++++++++++++++++++++++++++++++++++ src/utils/parse_utils_two.c | 96 +++++++++++++++++++++++++++++++++ src/utils/utils.c | 77 +++++++++++++++++++++++++++ tamere.jpeg | 0 20 files changed, 778 insertions(+), 215 deletions(-) create mode 100644 src/parsing/check_image.c create mode 100644 src/parsing/color_utils.c create mode 100644 src/parsing/find_player.c delete mode 100644 src/parsing/memo_parsing create mode 100644 src/utils/clear_free.c create mode 100644 src/utils/parse_utils_three.c create mode 100644 src/utils/parse_utils_two.c create mode 100644 src/utils/utils.c create mode 100644 tamere.jpeg diff --git a/Makefile b/Makefile index 978d505..e836d8a 100644 --- a/Makefile +++ b/Makefile @@ -18,10 +18,17 @@ INCLUDE = -I $(I_DIR) -I $(LIBFT_DIR)/$(I_DIR) -I $(MACRO_DIR)/$(I_DIR) SRCS = src/utils/mess_error.c \ src/utils/parse_utils.c \ + src/utils/parse_utils_two.c \ + src/utils/parse_utils_three.c \ + src/utils/utils.c \ + src/utils/clear_free.c \ + src/parsing/check_image.c \ src/parsing/check_map.c \ src/parsing/check_arg.c \ src/parsing/check_id_text.c \ src/parsing/check_rgb.c \ + src/parsing/color_utils.c \ + src/parsing/find_player.c \ src/parsing/principal_pars.c \ src/cub3d.c diff --git a/includes/cub3d.h b/includes/cub3d.h index 14d4b4c..3d5a02f 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 17:01:05 by madumerg #+# #+# */ -/* Updated: 2024/09/15 20:43:25 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 15:55:04 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,5 +23,4 @@ # include # include "parsing.h" - #endif diff --git a/includes/mess_err.h b/includes/mess_err.h index bb5bf2e..38da5e6 100644 --- a/includes/mess_err.h +++ b/includes/mess_err.h @@ -6,10 +6,13 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 21:59:56 by madumerg #+# #+# */ -/* Updated: 2024/09/16 12:18:42 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 16:52:46 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ +//general +#define CRASH "Memory problems or open failed" + //for argurments #define ERR_ARGS "Wrong number of arguments" @@ -22,9 +25,14 @@ #define ERR_PLAYER "Incorrect number of players" #define WRONG_CHAR "Invalid character in the map" -//for permission +//for information map #define ERR_IMG "Problem with image" +#define WRONG_F_IMG "Wrong type of image" #define DUP_ACR "There is a duplicate acronym" #define DUP_IMG "There is a duplicate image" #define NOT_FOUND "Information searched is not complete" +#define ERR_COLOR "The number is not between 0 and 255" +#define DUP_COLOR "Impossible to have the same color on the floor and ceiling" +#define WRONG_F_RGB "Wrong format RGB" +#define INVALID_CHAR "Invalid character in format color" diff --git a/includes/parsing.h b/includes/parsing.h index e83c17e..435338c 100644 --- a/includes/parsing.h +++ b/includes/parsing.h @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/12 12:49:16 by madumerg #+# #+# */ -/* Updated: 2024/09/16 14:23:15 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 17:54:07 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,11 +23,14 @@ typedef struct s_pars { int f_color; int c_color; + int color; + char l_player; t_coord coor; char *no_png; char *so_png; char *we_png; char *ea_png; + char **map; } t_pars; //check_arg.c @@ -35,32 +38,63 @@ int check_err_arg(int ac, char **av); int check_format_file(char *file); //check_id_text.c -int check_texture(char *sign, char *path, t_pars *pars); int detect_info(char **tab, t_pars *pars); + +//check_image.c +int assign_img(char *sign, char *path, t_pars *pars); int check_dup_img(t_pars *pars); +int check_texture(char *sign, char *path, t_pars *pars); //check_map.c int err_not_close(char after, char before, char down, char up); int actual_char(char actual); int check_map_close(char **map); -int count_player(char **map); int check_char_map(char **map); //check_rgb.c -int check_format_rgb(char **tab); +int check_format_rgb(char *sign, char **tab, t_pars *pars); + +//color_utils.c +int create_argb(int r, int g, int b); +char *ft_strndup_color(char *src, int start, int end); +int check_char_color(char *tab); +int check_dup_color(t_pars *pars); + +//find_player.c +int search_letter(char **map, int i, char l); +int count_player(char **map, t_pars *pars, int ct); //principal_pars.c int verif_info_file(char *av, t_pars *pars); -int verif_all_map(char **map); -char **parse_map(char *map); +int verif_all_map(char **map, t_pars *pars); +char **parse_file(char *map, int ct, int i); //mess_error.c in utils int err_mess(char *str); //parse_utils.c char **info_map(char **file); -void convert_white_s(char *str); int verif_char(char c); -int count_tab(char **tab); int space_line(char *line); + +//parse_utils_two.c +int ft_count_line_map(char **map, int y); +void replace_nl(char *l); +int recup_map(char **old_map, int i, t_pars *pars); +int all_skip(char **map, t_pars *pars); + +//parse_utils_three.c +char **alloc_map(char **old_map, int size); +char *fill_line(char *l, int size); +int last_first_line(char **map); + +//utils.c +int count_tab(char **tab); +void convert_white_s(char *str); +int longest_line(char **line); +int count_line_file(int fd); + +//clear_free.c +void free_tab(char **tab); +void free_pars(t_pars *pars); #endif diff --git a/map.cub b/map.cub index 692671a..1bdb4fc 100644 --- a/map.cub +++ b/map.cub @@ -1,17 +1,20 @@ -NO ./no_texture.png -SO ./so_texture.png -WE ./we_texture.png -EA ./ea_texture.png -F 220 , 100, 0 -C 225, 30, 0 -11111 -1001S1 -11111 -11111 -11111 -11101 -11111 - 111 - 101 - 111 + + + + + + + + +NO ./no_texture.png + SO ./so_texture.png +WE ./we_texture.png +EA ./ea_texture.png +C 7,4,4 + + F 7,4,40 + +111111 +1000W1 +111111 diff --git a/src/cub3d.c b/src/cub3d.c index 5c6a003..05e241b 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */ -/* Updated: 2024/09/16 13:54:52 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 20:12:50 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,10 @@ t_pars init_pars(void) { t_pars new; - new.f_color = 0; - new.c_color = 0; + new.f_color = -1; + new.c_color = -1; + new.color = 0; + new.l_player = 0; new.coor.x = 0; new.coor.y = 0; new.no_png = NULL; @@ -27,14 +29,34 @@ t_pars init_pars(void) return (new); } +int basics_check(int ac, char **av, t_pars *pars) +{ + if (check_err_arg(ac, av) == 1) + return (1); + if (verif_info_file(av[1], pars) == 1) + return (1); + return (0); +} + int main(int ac, char **av) { t_pars pars; + int i; + i = 0; pars = init_pars(); - if (check_err_arg(ac, av) == 1) + if (basics_check(ac, av, &pars) == 1) return (1); - if (verif_info_file(av[1], &pars) == 1) - printf("main\n"); + printf ("f color %d\n", pars.f_color); + printf ("c color %d\n", pars.c_color); + printf ("x = %d\n", pars.coor.x); + printf ("y = %d\n", pars.coor.y); + printf ("no_png --> %s\n", pars.no_png); + printf ("so_png --> %s\n", pars.so_png); + printf ("we_png --> %s\n", pars.we_png); + printf ("ea_png --> %s\n", pars.ea_png); + while (pars.map[i]) + printf("|%s|\n", pars.map[i++]); + free_pars(&pars); return (0); } diff --git a/src/parsing/check_id_text.c b/src/parsing/check_id_text.c index 568c814..a5bf93d 100644 --- a/src/parsing/check_id_text.c +++ b/src/parsing/check_id_text.c @@ -6,78 +6,12 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/16 09:38:32 by madumerg #+# #+# */ -/* Updated: 2024/09/16 14:38:35 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 17:06:14 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -int check_dup_img(t_pars *pars) -{ - if (!pars->no_png || !pars->so_png || !pars->we_png || !pars->ea_png) - return (err_mess(NOT_FOUND)); - if (ft_strcmp(pars->no_png, pars->so_png) == 0 || \ - ft_strcmp(pars->no_png, pars->we_png) == 0 || \ - ft_strcmp(pars->no_png, pars->ea_png) == 0 || \ - ft_strcmp(pars->so_png, pars->we_png) == 0 || \ - ft_strcmp(pars->we_png, pars->ea_png) == 0) - return (err_mess(DUP_IMG)); - return (0); -} - -int assign_img_two(char *sign, char *path, t_pars *pars) -{ - if (ft_strcmp("WE", sign) == 0) - { - if (pars->we_png == NULL) - pars->we_png = path; - else - return (err_mess(DUP_ACR)); - } - else if (ft_strcmp("EA", sign) == 0) - { - if (pars->ea_png == NULL) - pars->ea_png = path; - else - return (err_mess(DUP_ACR)); - } - return (0); -} - -int assign_img(char *sign, char *path, t_pars *pars) -{ - if (ft_strcmp("NO", sign) == 0) - { - if (pars->no_png == NULL) - pars->no_png = path; - else - return (err_mess(DUP_ACR)); - } - else if (ft_strcmp("SO", sign) == 0) - { - if (pars->so_png == NULL) - pars->so_png = path; - else - return (err_mess(DUP_ACR)); - } - if (assign_img_two(sign, path, pars) == 1) - return (1); - return (0); -} - -int check_texture(char *sign, char *path, t_pars *pars) -{ - int fd; - - fd = -1; - fd = open(path, O_RDONLY, 0644); - if (fd < 0) - return (err_mess(ERR_IMG)); - if (assign_img(sign, path, pars) == 1) - return (1); - return (0); -} - int detect_info(char **tab, t_pars *pars) { if (ft_strcmp("NO", tab[0]) == 0 && count_tab(tab) == 2) @@ -88,10 +22,10 @@ int detect_info(char **tab, t_pars *pars) return (check_texture(tab[0], tab[1], pars)); else if (ft_strcmp("EA", tab[0]) == 0 && count_tab(tab) == 2) return (check_texture(tab[0], tab[1], pars)); - else if (ft_strcmp("F", tab[0]) == 0) - check_format_rgb(tab); - else if (ft_strcmp("C", tab[0]) == 0) - check_format_rgb(tab); + else if (ft_strcmp("F", tab[0]) == 0 && count_tab(tab) == 2) + return (check_format_rgb("F", tab, pars)); + else if (ft_strcmp("C", tab[0]) == 0 && count_tab(tab) == 2) + return (check_format_rgb("C", tab, pars)); else return (err_mess(NOT_FOUND)); return (0); diff --git a/src/parsing/check_image.c b/src/parsing/check_image.c new file mode 100644 index 0000000..c64790c --- /dev/null +++ b/src/parsing/check_image.c @@ -0,0 +1,99 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* check_image.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/19 10:44:07 by madumerg #+# #+# */ +/* Updated: 2024/09/28 20:14:47 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int check_dup_img(t_pars *pars) +{ + if (!pars->no_png || !pars->so_png || !pars->we_png || !pars->ea_png) + return (err_mess(NOT_FOUND)); + if (ft_strcmp(pars->no_png, pars->so_png) == 0 || \ + ft_strcmp(pars->no_png, pars->we_png) == 0 || \ + ft_strcmp(pars->no_png, pars->ea_png) == 0 || \ + ft_strcmp(pars->so_png, pars->we_png) == 0 || \ + ft_strcmp(pars->we_png, pars->ea_png) == 0) + return (err_mess(DUP_IMG)); + return (0); +} + +int assign_img_two(char *sign, char *path, t_pars *pars) +{ + if (ft_strcmp("WE", sign) == 0) + { + if (pars->we_png == NULL) + pars->we_png = path; + else + return (err_mess(DUP_ACR)); + } + else if (ft_strcmp("EA", sign) == 0) + { + if (pars->ea_png == NULL) + pars->ea_png = path; + else + return (err_mess(DUP_ACR)); + } + return (0); +} + +int assign_img(char *sign, char *path, t_pars *pars) +{ + if (ft_strcmp("NO", sign) == 0) + { + if (pars->no_png == NULL) + pars->no_png = path; + else + return (err_mess(DUP_ACR)); + } + else if (ft_strcmp("SO", sign) == 0) + { + if (pars->so_png == NULL) + pars->so_png = path; + else + return (err_mess(DUP_ACR)); + } + if (assign_img_two(sign, path, pars) == 1) + return (1); + return (0); +} + +int format_img(char *img) +{ + int len; + + len = ft_strlen(img) - 1; + if (img[len] == 'g' && img[len - 1] == 'n' && \ + img[len - 2] == 'p' && img[len - 3] == '.') + return (0); + return (1); +} + +int check_texture(char *sign, char *path, t_pars *pars) +{ + int fd; + + fd = -1; + fd = open(path, O_RDONLY, 0644); + if (fd < 0) + return (err_mess(ERR_IMG)); + if (format_img(path) == 1) + { + close(fd); + return (err_mess(WRONG_F_IMG)); + } + if (assign_img(sign, path, pars) == 1) + { + close(fd); + return (1); + } + close(fd); + return (0); +} diff --git a/src/parsing/check_map.c b/src/parsing/check_map.c index c7dce2f..fe28edd 100644 --- a/src/parsing/check_map.c +++ b/src/parsing/check_map.c @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 11:24:13 by madumerg #+# #+# */ -/* Updated: 2024/09/12 12:57:24 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 15:48:38 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,13 @@ int err_not_close(char after, char before, char down, char up) { - if (after == ' ' || after == '\0') + if (after == ' ' || after == '\0' || after == '\n') return (1); - else if (before == ' ' || before == '\0') + else if (before == ' ' || before == '\0' || before == '\n') return (1); - else if (down == ' ' || down == '\0') + else if (down == ' ' || down == '\0' || down == '\n') return (1); - else if (up == ' ' || up == '\0') + else if (up == ' ' || up == '\0' || up == '\n') return (1); return (0); } @@ -39,6 +39,8 @@ int check_map_close(char **map) int x; y = 0; + if (last_first_line(map) == 1) + return (1); while (map[y]) { x = 0; @@ -56,31 +58,6 @@ int check_map_close(char **map) return (0); } -int count_player(char **map) -{ - int y; - int x; - int cpt; - - cpt = 0; - y = 0; - while (map[y]) - { - x = 0; - while (map[y][x]) - { - if (map[y][x] == 'N' || map[y][x] == 'S' || \ - map[y][x] == 'E' || map[y][x] == 'W') - cpt++; - x++; - } - y++; - } - if (cpt != 1) - return (1); - return (0); -} - int check_char_map(char **map) { int y; diff --git a/src/parsing/check_rgb.c b/src/parsing/check_rgb.c index 2065b47..7dcd851 100644 --- a/src/parsing/check_rgb.c +++ b/src/parsing/check_rgb.c @@ -6,21 +6,78 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/16 09:36:07 by madumerg #+# #+# */ -/* Updated: 2024/09/16 14:59:30 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 17:08:01 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -int check_format_rgb(char **tab) +int check_nb_color(char *r, char *g, char *b) { - int i = 0; - while (tab[i]) - printf(".%s.\n", tab[i++]); + if (ft_strlen(r) > 3 || ft_strlen(g) > 3 || ft_strlen(b) > 3 || \ + ft_atoi(r) > 255 || ft_atoi(g) > 255 || ft_atoi(b) > 255) + return (-1); return (0); } -int create_argb(int a, int r, int g, int b) +int save_color(char *tab, t_pars *pars, int j) { - return (a << 24 | r << 16 | g << 8 | b); + char *r; + char *g; + char *b; + int i; + + i = 0; + while (tab[i] != ',') + i++; + r = ft_strndup_color(tab, 0, i); + i += 1; + j = i; + while (tab[i] != ',') + i++; + g = ft_strndup_color(tab, j, i); + i += 1; + j = i; + while (tab[i]) + i++; + b = ft_strndup_color(tab, j, i); + if (check_nb_color(r, g, b) == -1) + return (-1); + pars->color = create_argb(ft_atoi(r), ft_atoi(g), ft_atoi(b)); + return (0); +} + +int count_precise_char(char *str, char c) +{ + int ct; + int i; + + i = 0; + ct = 0; + while (str[i]) + { + if (str[i] == c) + ct++; + i++; + } + if (ct != 2) + return (1); + return (0); +} + +int check_format_rgb(char *sign, char **tab, t_pars *pars) +{ + if (count_tab(tab) > 2) + return (err_mess(NOT_FOUND)); + if (check_char_color(tab[1]) == 1) + return (err_mess(INVALID_CHAR)); + if (count_precise_char(tab[1], ',') == 1) + return (err_mess(WRONG_F_RGB)); + if (save_color(tab[1], pars, 0) == -1) + return (err_mess(ERR_COLOR)); + if (ft_strcmp(sign, "F") == 0) + pars->f_color = pars->color; + else if (ft_strcmp(sign, "C") == 0) + pars->c_color = pars->color; + return (0); } diff --git a/src/parsing/color_utils.c b/src/parsing/color_utils.c new file mode 100644 index 0000000..9fb785d --- /dev/null +++ b/src/parsing/color_utils.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/18 19:44:37 by madumerg #+# #+# */ +/* Updated: 2024/09/28 17:32:06 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int check_dup_color(t_pars *pars) +{ + if (pars->c_color < 0 || pars->f_color < 0) + return (err_mess(NOT_FOUND)); + if (pars->c_color == pars->f_color) + return (err_mess(DUP_COLOR)); + return (0); +} + +int create_argb(int r, int g, int b) +{ + return (r << 16 | g << 8 | b); +} + +char *ft_strndup_color(char *src, int start, int end) +{ + int size; + char *dest; + int i; + + size = start; + i = 0; + dest = ft_calloc(sizeof(char), ft_strlen(src) + 1); + if (dest == NULL) + return (NULL); + while (size < end) + { + dest[i] = src[size]; + size++; + i++; + } + dest[i] = '\0'; + return (dest); +} + +int valid_char_color(char c) +{ + if (c == ',' || (c >= '0' && c <= '9')) + return (0); + return (1); +} + +int check_char_color(char *tab) +{ + int i; + + i = 0; + while (tab[i]) + { + if (valid_char_color(tab[i]) == 1) + return (1); + i++; + } + return (0); +} diff --git a/src/parsing/find_player.c b/src/parsing/find_player.c new file mode 100644 index 0000000..1e9a9ab --- /dev/null +++ b/src/parsing/find_player.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* find_player.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/17 14:57:11 by madumerg #+# #+# */ +/* Updated: 2024/09/19 09:55:38 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int search_letter(char **map, int i, char l) +{ + int x; + int y; + + y = 0; + while (map[y]) + { + x = 0; + while (map[y][x]) + { + if (map[y][x] == l) + { + if (i == 0) + return (x); + else if (i == 1) + return (y); + } + x++; + } + y++; + } + return (0); +} + +int count_player(char **map, t_pars *pars, int ct) +{ + int y; + int x; + + y = 0; + while (map[y]) + { + x = 0; + while (map[y][x]) + { + if (map[y][x] == 'N' || map[y][x] == 'S' || \ + map[y][x] == 'E' || map[y][x] == 'W') + { + pars->l_player = map[y][x]; + ct++; + } + x++; + } + y++; + } + if (ct != 1) + return (1); + return (0); +} diff --git a/src/parsing/memo_parsing b/src/parsing/memo_parsing deleted file mode 100644 index 0b9fe59..0000000 --- a/src/parsing/memo_parsing +++ /dev/null @@ -1,18 +0,0 @@ - // convertir tt les whites spaces en simple espace sur les 6 1ere lignes - - //check si .png - WARNIIIIIINNNNNNNNNNNNNGGG TESTER LES MALLOC A NULL - - //faire search player - utiliser la ft pour couleur - - - no so - no we - no ea - - so we - so ea - - we ea - diff --git a/src/parsing/principal_pars.c b/src/parsing/principal_pars.c index f8042f8..4f575e9 100644 --- a/src/parsing/principal_pars.c +++ b/src/parsing/principal_pars.c @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/12 12:44:52 by madumerg #+# #+# */ -/* Updated: 2024/09/16 14:58:26 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 20:40:30 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,10 @@ int all_data_verif(char **tab, t_pars *pars) { if (detect_info(tab, pars) == 1) + { + free_tab(tab); return (1); + } return (0); } @@ -31,10 +34,13 @@ int check_info(char **l, t_pars *pars) while (l[i]) { tab = ft_split(l[i], ' '); + if (tab == NULL) + return (1); if (all_data_verif(tab, pars) == 1) return (1); i++; } + free_tab(tab); return (0); } @@ -43,49 +49,67 @@ int verif_info_file(char *av, t_pars *pars) char **map; char **f_part; - (void)pars; - map = parse_map(av); + map = parse_file(av, 0, 1); + if (map == NULL) + return (err_mess(CRASH)); f_part = info_map(map); - if (check_info(f_part, pars) == 1) + if (f_part == NULL) + { + free_tab(map); + return (err_mess(CRASH)); + } + if (check_info(f_part, pars) == 1 || check_dup_img(pars) == 1 || \ + check_dup_color(pars) == 1) + { + free_tab(map); + return (1); + } + if (all_skip(map, pars) == 1) return (1); -// if (check_dup_img(pars) == 1) -// return (1); -// if (verif_all_map(map) == 1) -// return (1); return (0); } -int verif_all_map(char **map) +int verif_all_map(char **map, t_pars *pars) { - if (check_char_map(map) == 1) + pars->map = alloc_map(map, longest_line(map)); + free_tab(map); + if (pars->map == NULL) + return (err_mess(CRASH)); + if (check_char_map(pars->map) == 1) return (err_mess(WRONG_CHAR)); - if (count_player(map) == 1) + if (count_player(pars->map, pars, 0) == 1) return (err_mess(ERR_PLAYER)); - if (check_map_close(map) == 1) + if (check_map_close(pars->map) == 1) return (err_mess(NOT_CLOSE)); + pars->coor.x = search_letter(pars->map, 0, pars->l_player); + pars->coor.y = search_letter(pars->map, 1, pars->l_player); return (0); } -char **parse_map(char *map) +char **parse_file(char *map, int ct, int i) { int fd; char **parse_map; - char *save; - char *join; fd = open(map, O_RDONLY); - save = get_next_line(fd); - join = ft_calloc(1, 1); - if (!join) + if (fd < 0) return (NULL); - while (save != NULL) + ct = count_line_file(fd); + fd = open(map, O_RDONLY); + if (fd < 0) + return (NULL); + parse_map = ft_calloc(sizeof(char *), ct + 1); + if (!parse_map) { - join = ft_strjoin(join, save); - free(save); - save = get_next_line(fd); + close(fd); + return (NULL); } - parse_map = ft_split(join, '\n'); - free(join); - close(fd); + parse_map[0] = get_next_line(fd); + while (i <= ct) + { + parse_map[i] = get_next_line(fd); + i++; + } + close (fd); return (parse_map); } diff --git a/src/utils/clear_free.c b/src/utils/clear_free.c new file mode 100644 index 0000000..7c7ef07 --- /dev/null +++ b/src/utils/clear_free.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* clear_free.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/28 17:37:10 by madumerg #+# #+# */ +/* Updated: 2024/09/28 17:43:25 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +void free_tab(char **tab) +{ + char **tmp; + + tmp = tab; + while (tmp && *tmp) + { + free(*tmp); + tmp++; + } + free(tab); + tab = NULL; +} + +void free_pars(t_pars *pars) +{ + if (pars->no_png) + free(pars->no_png); + if (pars->so_png) + free(pars->so_png); + if (pars->we_png) + free(pars->we_png); + if (pars->ea_png) + free(pars->ea_png); + if (pars->map) + free_tab(pars->map); +} diff --git a/src/utils/parse_utils.c b/src/utils/parse_utils.c index 3f173eb..62f5ae6 100644 --- a/src/utils/parse_utils.c +++ b/src/utils/parse_utils.c @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 14:20:54 by madumerg #+# #+# */ -/* Updated: 2024/09/16 14:55:36 by madumerg ### ########.fr */ +/* Updated: 2024/09/28 17:32:42 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ char **info_map(char **file) i = 0; j = 0; - f_part = ft_calloc(6, sizeof(char *)); + f_part = ft_calloc(6 + 1, sizeof(char *)); if (!f_part) return (NULL); while (i >= j) @@ -55,19 +55,6 @@ char **info_map(char **file) return (f_part); } -void convert_white_s(char *str) -{ - int i; - - i = 0; - while (str[i]) - { - if (str[i] >= 9 && str[i] <= 13) - str[i] = 32; - i++; - } -} - int verif_char(char c) { if (c != '1' && c != '0' && c != 'N' && c != 'S' && c != 'W' && \ @@ -75,18 +62,3 @@ int verif_char(char c) return (1); return (0); } - -int count_tab(char **tab) -{ - int i; - int ct; - - i = 0; - ct = 0; - while (tab[i]) - { - i++; - ct++; - } - return (ct); -} diff --git a/src/utils/parse_utils_three.c b/src/utils/parse_utils_three.c new file mode 100644 index 0000000..933c2aa --- /dev/null +++ b/src/utils/parse_utils_three.c @@ -0,0 +1,98 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_utils_three.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/28 15:40:12 by madumerg #+# #+# */ +/* Updated: 2024/09/28 17:32:55 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +char *fill_line(char *l, int size) +{ + char *tmp; + int i; + int j; + + i = 0; + j = 0; + tmp = ft_calloc(sizeof(char), size + 1); + if (!tmp) + return (NULL); + while (l[j]) + { + tmp[i] = l[j]; + i++; + j++; + } + if (i < size) + { + while (i <= size - 1) + { + tmp[i] = ' '; + i++; + } + } + tmp[i] = '\0'; + return (tmp); +} + +char **alloc_map(char **old_map, int size) +{ + int i; + int j; + char **tmp; + + i = 0; + j = 0; + tmp = ft_calloc(sizeof(char *), ft_count_line_map(old_map, 0) + 1); + if (!tmp) + return (NULL); + while (old_map[j]) + { + tmp[i] = fill_line(old_map[j], size); + if (tmp[i] == NULL) + return (NULL); + i++; + j++; + } + tmp[i] = NULL; + return (tmp); +} + +int line_char(char *l) +{ + int i; + + i = 0; + while (l[i]) + { + if (actual_char(l[i]) == 0) + return (1); + else + i++; + } + return (0); +} + +int last_first_line(char **map) +{ + int y; + int tmp; + + tmp = ft_count_line_map(map, 0); + y = 0; + while (map[y]) + { + if ((y == 0 && line_char(map[y]) == 1) || \ + (y == (tmp - 1) && line_char(map[y]) == 1)) + return (1); + else + y++; + } + return (0); +} diff --git a/src/utils/parse_utils_two.c b/src/utils/parse_utils_two.c new file mode 100644 index 0000000..a6e3933 --- /dev/null +++ b/src/utils/parse_utils_two.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_utils_two.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/17 15:12:28 by madumerg #+# #+# */ +/* Updated: 2024/09/28 20:44:38 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int ft_count_line_map(char **file, int y) +{ + while (file[y]) + y++; + return (y); +} + +void replace_nl(char *l) +{ + int i; + + i = 0; + while (l[i]) + { + if (l[i] == '\n') + l[i] = '\0'; + i++; + } +} + +int recup_map(char **old_map, int i, t_pars *pars) +{ + char **tmp; + int j; + + j = 0; + tmp = ft_calloc(sizeof(char *), ft_count_line_map(old_map, i)); + if (!tmp) + return (err_mess(CRASH)); + while (old_map[i]) + { + replace_nl(old_map[i]); + tmp[j] = old_map[i]; + j++; + i++; + } + if (verif_all_map(tmp, pars) == 1) + return (1); + return (0); +} + +int space_l_two(char *line) +{ + int i; + + i = 0; + while (line[i]) + { + if (line[i] == ' ' || (line[i] >= 9 && line[i] <= 13)) + i++; + else + break ; + } + if (line[i] != '\0') + return (1); + return (0); +} + +int all_skip(char **map, t_pars *pars) +{ + char **tmp; + int ct; + int i; + + ct = 0; + i = 0; + tmp = map; + while (tmp[i]) + { + if (space_line(tmp[i]) == 1) + ct++; + if (ct == 6) + break ; + i++; + } + i += 1; + while (tmp[i] && space_l_two(tmp[i]) != 1) + i++; + if (recup_map(map, i, pars) == 1) + return (1); + return (0); +} diff --git a/src/utils/utils.c b/src/utils/utils.c new file mode 100644 index 0000000..205a464 --- /dev/null +++ b/src/utils/utils.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/20 09:57:13 by madumerg #+# #+# */ +/* Updated: 2024/09/28 14:55:56 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +int count_tab(char **tab) +{ + int i; + int ct; + + i = 0; + ct = 0; + while (tab[i]) + { + i++; + ct++; + } + return (ct); +} + +void convert_white_s(char *str) +{ + int i; + + i = 0; + while (str[i]) + { + if (str[i] >= 9 && str[i] <= 13) + str[i] = 32; + i++; + } +} + +int longest_line(char **line) +{ + int i; + int stock; + int actual; + + stock = ft_strlen(line[0]); + i = 1; + actual = 0; + while (line[i]) + { + actual = ft_strlen(line[i]); + if (actual > stock) + stock = actual; + i++; + } + return (stock); +} + +int count_line_file(int fd) +{ + int i; + char *gnl; + + i = 0; + gnl = get_next_line(fd); + while (gnl) + { + free(gnl); + gnl = get_next_line(fd); + i++; + } + close (fd); + return (i); +} diff --git a/tamere.jpeg b/tamere.jpeg new file mode 100644 index 0000000..e69de29