-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrtol.c
More file actions
75 lines (68 loc) · 2.02 KB
/
strtol.c
File metadata and controls
75 lines (68 loc) · 2.02 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strtol.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: macheuk- <macheuk-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/16 04:58:40 by macheuk- #+# #+# */
/* Updated: 2025/06/16 06:18:42 by macheuk- ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
char *skip_whitespace_and_sign(char *str, int *sign)
{
while (*str == ' ' || *str == '\t')
str++;
*sign = 1;
if (*str == '-')
{
*sign = -1;
str++;
}
else if (*str == '+')
str++;
return (str);
}
// Helper: Convert a single hex digit to int
int hex_digit(char c)
{
if (c >= '0' && c <= '9')
return (c - '0');
if (c >= 'a' && c <= 'f')
return (c - 'a' + 10);
if (c >= 'A' && c <= 'F')
return (c - 'A' + 10);
return (-1);
}
// Helper: Parse number part
long parse_number(char *str, int base, char **end)
{
long result;
int val;
result = 0;
val = hex_digit(*str);
while ((*str >= '0' && *str <= '9') || (base == 16 && (val != -1)))
{
if (*str >= '0' && *str <= '9')
result = result * base + (*str - '0');
else if (base == 16 && hex_digit(*str) != -1)
result = result * base + hex_digit(*str);
str++;
val = hex_digit(*str);
}
if (end)
*end = str;
return (result);
}
long ft_strtol(char *str, char **endptr, int base)
{
long result;
char *end;
int sign;
str = skip_whitespace_and_sign(str, &sign);
result = parse_number(str, base, &end);
if (endptr)
*endptr = (char *)end;
return (result * sign);
}