-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb_atox_check_bonus.c
53 lines (47 loc) · 1.83 KB
/
b_atox_check_bonus.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* b_atox_check_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkawaguc <nkawaguc@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/28 12:17:12 by nkawaguc #+# #+# */
/* Updated: 2024/10/29 00:24:14 by nkawaguc ### ########.fr */
/* */
/* ************************************************************************** */
#include "b_fdf_bonus.h"
static void error_atox_check(char *str);
static void space_skip(const char *str, int *i);
void atox_check(const char *str, int i)
{
int num;
space_skip(str, &i);
if (str[i] != '0' || (str[i + 1] != 'x' && str[i + 1] != 'X'))
error_atox_check(MSG_ATOX_ERROR_PREFIX);
i += 2;
num = 0;
while (ft_isdigit(str[i]) || ('a' <= str[i] && str[i] <= 'f')
|| ('A' <= str[i] && str[i] <= 'F'))
{
if (ft_isdigit(str[i]))
num = num * 16 + str[i++] - '0';
else if ('a' <= str[i] && str[i] <= 'f')
num = num * 16 + str[i++] - 'a' + 10;
else
num = num * 16 + str[i++] - 'A' + 10;
if (num > COLOR_MAX)
error_atox_check(MSG_ATOX_ERROR_LARGE);
}
if (str[i] != '\0')
error_atox_check(MSG_ATOX_ERROR_NOT_HEX);
}
static void error_atox_check(char *str)
{
ft_putendl_fd(str, STDERR_FILENO);
exit(EXIT_FAILURE);
}
static void space_skip(const char *str, int *i)
{
while ((9 <= str[*i] && str[*i] <= 13) || str[*i] == ' ')
(*i)++;
}