-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_line.c
59 lines (53 loc) · 865 Bytes
/
parse_line.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
54
55
56
57
58
59
#include "push_swap.h"
void parse_line(char *str, t_two_stacks **stacks)
{
int32_t i;
int64_t val;
i = check_for_parse(str, stacks);
--i;
while (i >= 0)
{
i = skip_nums(i, str);
val = atol(&str[i]);
--i;
if (val > INT32_MAX || val < INT32_MIN)
error_handle(stacks);
(*stacks)->a = push((*stacks)->a, (int32_t)val, 0, 0);
i = skip_spaces(i, str);
}
}
int32_t check_for_parse(char *str, t_two_stacks **stacks)
{
int32_t i;
i = 0;
while (str[i])
{
if (str[i] > '9' || str[i] < '0')
{
if (str[i] == '-' || str[i] == ' ')
{
++i;
continue ;
}
error_handle(stacks);
}
++i;
}
return (i);
}
int32_t skip_nums(int32_t i, char *str)
{
while (str[i])
{
if (str[i - 1] == ' ' || i == 0)
break ;
--i;
}
return (i);
}
int32_t skip_spaces(int32_t i, char *str)
{
while (str[i] == ' ')
--i;
return (i);
}