15static bool is_not_number(
char *str)
17 return (*str !=
'\0' && *str !=
',');
28static t_result atoi_0_to_255(
char *str)
34 return ((
t_result){.value = -1, .err_msg = NULL});
35 while (*str !=
'\0' && ft_isdigit(*str) ==
true)
37 while (result == 0 && *str ==
'0' && *str !=
'\0')
40 return ((
t_result){.value = 0, .err_msg = NULL});
43 result = result * 10 + (*str -
'0');
45 return ((
t_result){.value = -2, .err_msg = NULL});
48 if (is_not_number(str) ==
true)
49 return ((
t_result){.value = -3, .err_msg = str});
50 return ((
t_result){.value = result, .err_msg = NULL});
53static int check_error_atoi(
t_result *result,
const char *rgb_str,
char *str)
55 if (result->value >= 0)
56 return (EXIT_SUCCESS);
57 if (result->value == -1)
58 result->err_msg = rgb_str;
59 if (result->value == -2)
60 result->err_msg = str;
61 return (EXIT_FAILURE);
74 static const char *rgb_str[3] = {
"R",
"G",
"B"};
78 while (str != NULL && *str !=
'\0' && type <= ENUM_B)
80 result[type] = atoi_0_to_255(str);
81 if (check_error_atoi(&result[type], rgb_str[type], str) != EXIT_SUCCESS)
82 return (result[type]);
84 str = ft_strchr(str,
',');
88 while (*str !=
'\0' && *str ==
' ')
92 return ((
t_result){.value = -1, .err_msg = rgb_str[type]});
94 return ((
t_result){.value = -4, .err_msg = --str});
95 return ((
t_result){(result[0].value << 16) + (result[1].value << 8)
96 + result[2].value, NULL});