-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneral_utils.c
258 lines (234 loc) · 4.54 KB
/
general_utils.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* PS_LLutils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrizakov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/08 18:48:26 by mrizakov #+# #+# */
/* Updated: 2023/04/08 18:48:28 by mrizakov ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
void ft_putstr_fd(char *s, int fd)
{
while (*s)
{
ft_putchar_fd(*s, fd);
s++;
}
}
int ft_isdigit(int c)
{
if (c >= 48 && c <= 57)
{
}
else
{
return (0);
}
return (1);
}
int ft_isdigit2(const char *str)
{
int i;
i = 0;
while(str[i])
{
if(!ft_isdigit(str[i]))
return (-1);
i++;
}
return(0);
}
char **ft_free(char **strs, int j)
{
while (j-- > 0)
free(strs[j]);
free(strs);
return (NULL);
}
int ft_word_count(const char *str, char c)
{
int i;
int trigger;
i = 0;
trigger = 0;
while (*str)
{
if (*str != c && trigger == 0)
{
trigger = 1;
i++;
}
else if (*str == c)
trigger = 0;
str++;
}
return (i);
}
int ft_size_word(const char *s, char c, int i)
{
int word_size;
word_size = 0;
while (s[i] != c && s[i])
{
word_size++;
i++;
}
return (word_size);
}
size_t ft_strlen(const char *str)
{
size_t counter;
counter = 0;
while (str[counter] != '\0')
{
counter++;
}
return (counter);
}
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *substr;
unsigned int i;
unsigned int str_len;
unsigned int correct_size;
i = 0;
str_len = ft_strlen((char *)s);
correct_size = 1;
if (start > str_len || len == 0)
correct_size = 1;
else if (len <= str_len - start)
correct_size = len + 1;
else if (len > str_len - start)
correct_size = str_len - start + 1;
substr = malloc(correct_size * sizeof(char));
if (!substr)
return ((void *) NULL);
while (i != correct_size - 1)
{
substr[i] = s[start + i];
i++;
}
substr[i] = '\0';
return (substr);
}
char **ft_split(char const *s, char c)
{
char **array;
unsigned int word_count;
unsigned int i;
unsigned int len;
unsigned int y;
word_count = ft_word_count(s, c);
y = -1;
i = 0;
array = (char **)malloc((word_count + 1) * sizeof(char *));
if (!array)
return (NULL);
while (++y < word_count)
{
while (s[i] == c)
i++;
len = ft_size_word(s, c, i);
array[y] = ft_substr(s, i, len);
if (!array[y])
ft_free(array, y);
i = i + len;
}
array[y] = 0;
return (array);
}
//
//char **ft_free_split(char **strs)
//{
// int i;
//
// i = 0;
// while (strs[i] != (void *)0)
// free(strs[i]);
// free(strs);
// return (NULL);
//}
void clean_ptrs(char **double_ptr)
{
char **tmp;
tmp = double_ptr;
while (*tmp)
{
free(*tmp);
tmp++;
}
free(double_ptr);
}
long long ft_long_long_atoi(const char *str)
{
unsigned int str_c;
long long result;
int positive;
str_c = 0;
positive = 1;
result = 0;
while (str[str_c] == 32 || (str[str_c] >= 9
&& str[str_c] <= 13))
str_c++;
if (str[str_c] == 45)
{
positive = -1;
str_c++;
}
else if (str[str_c] == 43)
str_c++;
while (str[str_c] != '\0')
{
while (str[str_c] >= 48 && str[str_c] <= 57)
{
result = result * 10 + str[str_c] - '0';
str++;
}
}
return (result * positive);
}
int ft_number_checker(const char *str)
{
unsigned int str_c;
long long result;
int positive;
str_c = 0;
positive = 1;
result = 0;
while (str[str_c] == 32 || (str[str_c] >= 9
&& str[str_c] <= 13))
str_c++;
if (str[str_c] == 45)
{
positive = -1;
str_c++;
}
else if (str[str_c] == 43)
str_c++;
while (str[str_c] != '\0')
{
if (str[str_c] < '0' || str[str_c] > '9')
{
ft_putstr_fd("Number checker says Not a number!", 1);
return (1);
}
while (str[str_c] >= 48 && str[str_c] <= 57)
{
result = result * 10 + str[str_c] - '0';
str++;
}
}
if (result * positive > INT_MAX || result * positive < INT_MIN)
{
ft_putstr_fd("Number checker says Value outside of INT range! Try again", 1);
return (1);
}
return (0);
}