#include #include #include static int ft_check_digits(const char *word) { size_t i; i = 0; while (ft_isdigit(word[i])) i++; if (i != ft_strlen(word)) return (-1); return (0); } static int ft_get_f_color(char *line, char **words, t_cub *clist) { char **num; if (!(*words) || ft_strcmp(*words, "F") || !words[1] || words[2]) return (-1); if (!(num = ft_split(words[1], ','))) return (-1); if (!num[0] || !num[1] || !num[2] || num[3] || ft_check_digits(num[0]) || ft_check_digits(num[1]) || ft_check_digits(num[2]) || ft_atoi(num[0]) > 255 || ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255) { ft_free_words(num, NULL); return (-1); } clist->f_color = ft_atoi(num[0]); clist->f_color *= 1000; clist->f_color += ft_atoi(num[1]); clist->f_color *= 1000; clist->f_color += ft_atoi(num[2]); ft_free_words(num, NULL); ft_free_words(words, line); return (0); } static int ft_get_c_color(char *line, char **words, t_cub *clist) { char **num; if (!(*words) || ft_strcmp(*words, "C") || !words[1] || words[2]) return (-1); if (!(num = ft_split(words[1], ','))) return (-1); if (!num[0] || !num[1] || !num[2] || num[3] || ft_check_digits(num[0]) || ft_check_digits(num[1]) || ft_check_digits(num[2]) || ft_atoi(num[0]) > 255 || ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255) { ft_free_words(num, NULL); return (-1); } clist->c_color = ft_atoi(num[0]); clist->c_color *= 1000; clist->c_color += ft_atoi(num[1]); clist->c_color *= 1000; clist->c_color += ft_atoi(num[2]); ft_free_words(num, NULL); ft_free_words(words, line); return (0); } int ft_get_colors(int fd, t_cub *clist) { char *line; char **words; if (get_next_line(fd, &line) <= 0 || !(words = ft_split(line, ' '))) { ft_memdel(line); return (ft_map_error(8, clist)); } if (ft_get_f_color(line, words, clist) < 0) { ft_free_words(words, line); return (ft_map_error(8, clist)); } if (get_next_line(fd, &line) <= 0 || !(words = ft_split(line, ' '))) { ft_memdel(line); return (ft_map_error(9, clist)); } if (ft_get_c_color(line, words, clist) < 0) { ft_free_words(words, line); return (ft_map_error(9, clist)); } return (0); }