-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_numbers.c
More file actions
128 lines (117 loc) · 2.35 KB
/
extract_numbers.c
File metadata and controls
128 lines (117 loc) · 2.35 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* extract_numbers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kikiz <kikiz@student.42istanbul.com.tr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 16:11:14 by kikiz #+# #+# */
/* Updated: 2025/04/07 17:51:33 by kikiz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void ft_freestr(char **lst)
{
int i;
if (!lst)
return ;
i = 0;
while (lst[i])
{
free(lst[i]);
i++;
}
free(lst);
}
t_stack *extract_numbers(char *str)
{
t_stack *stack_a;
char **tmp;
long int num;
int i;
stack_a = NULL;
tmp = ft_split(str, ' ');
i = 0;
if (!is_correct_input(tmp))
exit_error(tmp, str);
while (tmp[i])
{
num = ft_atol(tmp[i]);
if (num > 2147483647 || num < -2147483648)
{
free_stack(&stack_a);
exit_error(tmp, str);
}
stack_add_bottom(&stack_a, new_node(num));
i++;
}
ft_freestr(tmp);
free(str);
return (stack_a);
}
char *ft_strdup(const char *s1)
{
int i;
int j;
char *ptr;
i = 0;
j = 0;
while (s1[i] != '\0')
{
i++;
}
ptr = malloc(sizeof(char) * (i + 1));
if (!ptr)
return (0);
while (s1[j] != '\0')
{
ptr[j] = s1[j];
j++;
}
ptr[j] = '\0';
return (ptr);
}
char *ft_strjoin(char *s1, char *s2)
{
char *ptr;
int i;
int j;
int size;
i = 0;
j = 0;
size = ft_strlen(s1) + ft_strlen(s2) + 1;
ptr = malloc(sizeof(char) * size);
if (!ptr)
return (0);
while (s1[i] != '\0')
{
ptr[i] = s1[i];
i++;
}
while (s2[j] != '\0')
{
ptr[i + j] = s2[j];
j++;
}
ptr[i + j] = '\0';
return (ptr);
}
char *all_args(int argc, char **argv)
{
int i;
char *result;
char *tmp;
char *tmp2;
i = 1;
result = ft_strdup("");
while (i < argc)
{
tmp2 = result;
tmp = ft_strjoin(argv[i], " ");
result = ft_strjoin(tmp2, tmp);
free(tmp);
free(tmp2);
i++;
}
return (result);
}