From ab4439ea510121b2356237416ff47ff9e65f23ff Mon Sep 17 00:00:00 2001 From: Maelys Date: Mon, 11 Nov 2024 15:20:51 +0100 Subject: [PATCH] fix_parsing --- includes/parsing.h | 9 +++++-- maps/g_cahuete.cub | 4 +-- maps/g_crazy_cat.cub | 46 +++++++++++++++++++++++++++----- maps/w_char_color.cub | 2 +- maps/w_db_ac.cub | 4 +-- maps/w_form_color.cub | 2 +- src/parsing/check_id_text.c | 7 ++--- src/parsing/check_rgb.c | 50 ++++++++++++++++++++++------------- src/parsing/color_utils.c | 18 ++++++++----- src/parsing/color_utils_two.c | 48 +++++++++++++++++++++++++++++++++ src/utils/utils.c | 20 +++++++++++++- 11 files changed, 167 insertions(+), 43 deletions(-) create mode 100644 src/parsing/color_utils_two.c diff --git a/includes/parsing.h b/includes/parsing.h index 24f80d8..8de6467 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/11/06 12:50:01 by adjoly ### ########.fr */ +/* Updated: 2024/11/11 15:12:35 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,7 +67,11 @@ 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_char_color(char *str); + +//color_utils_two.c +int total_len(char **tab); +char *multi_join(char **tab, int ct); //find_player.c int search_letter(char **map, int i, char l); @@ -102,6 +106,7 @@ int count_tab(char **tab); void convert_white_s(char *str); int longest_line(char **line); int count_line_file(int fd); +char *ft_strcat(char *dest, char *src); //clear_free.c void free_tab(char **tab); diff --git a/maps/g_cahuete.cub b/maps/g_cahuete.cub index 9144e81..f5e3998 100644 --- a/maps/g_cahuete.cub +++ b/maps/g_cahuete.cub @@ -3,8 +3,8 @@ SO ./img/cahuete2.png WE ./img/cahuete3.png EA ./img/cahuete4.png -C 255,153,102 -F 132,132,218 +C 255 , 153,102 +F 132,132,218 11111111111 100000N0001 diff --git a/maps/g_crazy_cat.cub b/maps/g_crazy_cat.cub index bfe3c52..f6a53b7 100644 --- a/maps/g_crazy_cat.cub +++ b/maps/g_crazy_cat.cub @@ -6,9 +6,43 @@ EA ./img/suki.png C 255,153,102 F 132,132,218 -11111111111 -100000N0001 -11100000111 -10000000101 -10101010101 -11111111111 +11111111111111111111 +10000000000000000001 +10111111111111111001 +10000000000000000001 +10111111111111111001 +10000000000000000001 +10111111111111111001 +10000000000000000001 +10111111111111111001 +10000000000000000001 +11111111111111111111 +11111111111111111111 +11111111111111111111 +10000000000000000001 +10111111111111111001 +10000000000000000001 +10111111111111111001 +10000000000000000001 +11111111111111111111 +11111111111111111111 +11111111111111111111 +10000000000000000001 +10111111111111111001 +10000000000000000001 +10111111111111111001 +10000000000000000001 +10111111111111111001 +10000000000000000001 +10111111111111111001 +10000000000000000001 +11111111111111111111 +11111111111111111111 +11111111111111111111 +10000000000000000001 +10111111111111111001 +10000000000000000N01 +10111111111111111001 +10000000000000000001 +11111111111111111111 +11111111111111111111 diff --git a/maps/w_char_color.cub b/maps/w_char_color.cub index 3a62ac3..05b2af7 100644 --- a/maps/w_char_color.cub +++ b/maps/w_char_color.cub @@ -3,7 +3,7 @@ SO ./img/suki.png WE ./img/cahuete.png EA ./img/kanel.png -C 75,rere7,9, +C 75,rere 7 , 9, F 50,0,250 11111 diff --git a/maps/w_db_ac.cub b/maps/w_db_ac.cub index ef3efdb..30a3bfe 100644 --- a/maps/w_db_ac.cub +++ b/maps/w_db_ac.cub @@ -1,9 +1,9 @@ NO ./img/flo.png -NO ./img/suki.png +SO ./img/suki.png WE ./img/cahuete.png EA ./img/kanel.png -C 9,77,56 +F 9,77,56 F 50,0,250 11111 diff --git a/maps/w_form_color.cub b/maps/w_form_color.cub index ebf70f3..66cf0f9 100644 --- a/maps/w_form_color.cub +++ b/maps/w_form_color.cub @@ -3,7 +3,7 @@ SO ./img/suki.png WE ./img/cahuete.png EA ./img/kanel.png -C 256,1874700,200 +C 256 ,1874700 ,200 F 50,0,250 11111 diff --git a/src/parsing/check_id_text.c b/src/parsing/check_id_text.c index 2def421..babfd5f 100644 --- a/src/parsing/check_id_text.c +++ b/src/parsing/check_id_text.c @@ -6,11 +6,12 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/16 09:38:32 by madumerg #+# #+# */ -/* Updated: 2024/10/05 15:51:44 by madumerg ### ########.fr */ +/* Updated: 2024/11/11 13:41:46 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ #include "parsing.h" +#include int detect_info(char **tab, t_pars *pars) { @@ -22,9 +23,9 @@ int detect_info(char **tab, t_pars *pars) return (check_texture(tab[0], tab[1], pars)); else if (ft_strncmp("EA", tab[0], 2) == 0 && count_tab(tab) == 2) return (check_texture(tab[0], tab[1], pars)); - else if (ft_strncmp("F", tab[0], 1) == 0 && count_tab(tab) == 2) + else if (ft_strncmp("F", tab[0], 1) == 0) return (check_format_rgb("F", tab, pars)); - else if (ft_strncmp("C", tab[0], 1) == 0 && count_tab(tab) == 2) + else if (ft_strncmp("C", tab[0], 1) == 0) return (check_format_rgb("C", tab, pars)); else return (err_mess(NOT_FOUND)); diff --git a/src/parsing/check_rgb.c b/src/parsing/check_rgb.c index 0b7d653..7f1998a 100644 --- a/src/parsing/check_rgb.c +++ b/src/parsing/check_rgb.c @@ -6,10 +6,11 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/16 09:36:07 by madumerg #+# #+# */ -/* Updated: 2024/11/10 11:58:40 by madumerg ### ########.fr */ +/* Updated: 2024/11/11 15:13:02 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" #include "parsing.h" int check_nb_color(char *r, char *g, char *b) @@ -20,6 +21,7 @@ int check_nb_color(char *r, char *g, char *b) free(r); free(g); free(b); + err_mess(ERR_COLOR); return (-1); } return (0); @@ -31,9 +33,10 @@ int save_color(char *tab, t_pars *pars, int j, int i) char *g; char *b; + i++; while (tab[i] != ',') i++; - r = ft_strndup_color(tab, 0, i); + r = ft_strndup_color(tab, 1, i); i += 1; j = i; while (tab[i] != ',') @@ -53,46 +56,57 @@ int save_color(char *tab, t_pars *pars, int j, int i) return (0); } -int count_precise_char(char *str, char c) +int count_precise_char(char *tab, char c) { int ct; int i; i = 0; ct = 0; - while (str[i]) + while (tab[i]) { - if (str[i] == c) + if (tab[i] == c) ct++; i++; } if (ct != 2) - return (1); + return (err_mess(WRONG_F_RGB)); return (0); } +void assign_color(t_pars *pars, char *sign) +{ + if (ft_strcmp(sign, "F") == 0) + pars->f_color = pars->color; + if (ft_strcmp(sign, "C") == 0) + pars->c_color = pars->color; +} + int check_format_rgb(char *sign, char **tab, t_pars *pars) { static int ct_f; static int ct_c; + char *str; + str = multi_join(tab, count_tab(tab)); + if (!str) + return (err_mess(CRASH)); if (ft_strcmp(sign, "F") == 0) ct_f++; if (ft_strcmp(sign, "C") == 0) ct_c++; - 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, 0) == -1) - return (err_mess(ERR_COLOR)); + if (check_char_color(str) == 1 || count_precise_char(str, ',') == 1 \ + || save_color(str, pars, 0, 0) == -1) + { + free(str); + return (1); + } if (ct_f > 1 || ct_c > 1) + { + free(str); return (err_mess(DUP_ACR)); - if (ft_strcmp(sign, "F") == 0) - pars->f_color = pars->color; - if (ft_strcmp(sign, "C") == 0) - pars->c_color = pars->color; + } + assign_color(pars, sign); + free(str); return (0); } diff --git a/src/parsing/color_utils.c b/src/parsing/color_utils.c index f4522ea..658a06b 100644 --- a/src/parsing/color_utils.c +++ b/src/parsing/color_utils.c @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/18 19:44:37 by madumerg #+# #+# */ -/* Updated: 2024/10/05 17:52:43 by madumerg ### ########.fr */ +/* Updated: 2024/11/11 15:09:04 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,20 +40,24 @@ char *ft_strndup_color(char *src, int start, int end) int valid_char_color(char c) { - if (c == ',' || (c >= '0' && c <= '9')) + if (c == ',' || (c >= '0' && c <= '9') || c == '\t' \ + || c == ' ' || c == '\0') return (0); return (1); } -int check_char_color(char *tab) +int check_char_color(char *str) { int i; - i = 0; - while (tab[i]) + if (str[0] == 'C' || str[0] == 'F') + i = 1; + else + i = 0; + while (str[i]) { - if (valid_char_color(tab[i]) == 1) - return (1); + if (valid_char_color(str[i]) == 1) + return (err_mess(INVALID_CHAR)); i++; } return (0); diff --git a/src/parsing/color_utils_two.c b/src/parsing/color_utils_two.c new file mode 100644 index 0000000..543e710 --- /dev/null +++ b/src/parsing/color_utils_two.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* color_utils_two.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: madumerg +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/11 15:11:11 by madumerg #+# #+# */ +/* Updated: 2024/11/11 15:12:20 by madumerg ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" + +int total_len(char **tab) +{ + int len; + int i; + + i = 0; + len = 0; + while (tab[i]) + { + len += ft_strlen(tab[i]); + i++; + } + return (len); +} + +char *multi_join(char **tab, int ct) +{ + char *str; + int t_len; + int i; + + i = 0; + t_len = total_len(tab); + str = malloc(t_len + 1); + if (!str) + return (NULL); + str[0] = '\0'; + while (i < ct) + { + ft_strcat(str, tab[i]); + i++; + } + return (str); +} diff --git a/src/utils/utils.c b/src/utils/utils.c index c19b374..078cd34 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/20 09:57:13 by madumerg #+# #+# */ -/* Updated: 2024/10/05 18:33:01 by madumerg ### ########.fr */ +/* Updated: 2024/11/11 15:09:41 by madumerg ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,3 +77,21 @@ int count_line_file(int fd) close (fd); return (i); } + +char *ft_strcat(char *dest, char *src) +{ + int i; + int j; + + i = 0; + while (dest[i] != '\0') + i++; + j = 0; + while (src[j] != '\0') + { + dest[j + i] = src[j]; + j++; + } + dest[i + j] = '\0'; + return (dest); +}