-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_tokens.c
More file actions
98 lines (88 loc) · 2.1 KB
/
parse_tokens.c
File metadata and controls
98 lines (88 loc) · 2.1 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_tokens.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: miissa <miissa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/31 07:16:51 by miissa #+# #+# */
/* Updated: 2026/01/12 12:39:55 by miissa ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int is_space(char c)
{
return (c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v'
|| c == '\f');
}
static char *dup_ws_as_space(const char *s)
{
char *dup;
size_t i;
if (!s)
return (NULL);
dup = (char *)ft_calloc(ft_strlen(s) + 1, 1);
if (!dup)
return (NULL);
i = 0;
while (s[i])
{
if (is_space(s[i]))
dup[i] = ' ';
else
dup[i] = s[i];
i++;
}
return (dup);
}
void ps_free_split(char **parts)
{
int i;
if (!parts)
return ;
i = 0;
while (parts[i])
free(parts[i++]);
free(parts);
}
char **ps_split_ws(const char *s)
{
char *dup;
char **parts;
dup = dup_ws_as_space(s);
if (!dup)
return (NULL);
parts = ft_split(dup, ' ');
free(dup);
if (!parts || !parts[0])
{
ps_free_split(parts);
return (NULL);
}
return (parts);
}
int ps_parse_token(const char *s, int *out)
{
long long val;
int i;
int sign;
if (!s || !*s)
return (0);
i = 0;
sign = 1;
if (s[i] == '+' || s[i] == '-')
if (s[i++] == '-')
sign = -1;
if (!ft_isdigit(s[i]))
return (0);
val = 0;
while (ft_isdigit(s[i]))
val = val * 10 + (s[i++] - '0');
if (s[i] != '\0')
return (0);
val *= sign;
if (val < INT_MIN || val > INT_MAX)
return (0);
*out = (int)val;
return (1);
}