Cub3D
Loading...
Searching...
No Matches
parse_fc_utils.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* parse_fc_utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/11/16 18:17:19 by kamitsui #+# #+# */
9/* Updated: 2024/12/18 03:04:12 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
15static bool is_not_number(char *str)
16{
17 return (*str != '\0' && *str != ',');
18}
19
28static t_result atoi_0_to_255(char *str)
29{
30 int result;
31
32 result = 0;
33 if (*str == ',')
34 return ((t_result){.value = -1, .err_msg = NULL});
35 while (*str != '\0' && ft_isdigit(*str) == true)
36 {
37 while (result == 0 && *str == '0' && *str != '\0')
38 str++;
39 if (*str == '\0')
40 return ((t_result){.value = 0, .err_msg = NULL});
41 if (*str == ',')
42 str--;
43 result = result * 10 + (*str - '0');
44 if (result > 255)
45 return ((t_result){.value = -2, .err_msg = NULL});
46 str++;
47 }
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});
51}
52
53static int check_error_atoi(t_result *result, const char *rgb_str, char *str)
54{
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);
62}
63
71t_result get_rgb_color(char *str)
72{
73 t_result result[3];
74 static const char *rgb_str[3] = {"R", "G", "B"};
75 t_type_rgb type;
76
77 type = ENUM_R;
78 while (str != NULL && *str != '\0' && type <= ENUM_B)
79 {
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]);
83 type++;
84 str = ft_strchr(str, ',');
85 if (str == NULL)
86 break ;
87 str++;
88 while (*str != '\0' && *str == ' ')
89 str++;
90 }
91 if (type <= ENUM_B)
92 return ((t_result){.value = -1, .err_msg = rgb_str[type]});
93 if (str != NULL)
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});
97}
98 //debug_get_rgb_color(debug_fd, type, rgb, "get_rgb_color()");