-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_words.c
96 lines (89 loc) · 2.25 KB
/
count_words.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* count_words.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rdanyell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 16:18:54 by rdanyell #+# #+# */
/* Updated: 2022/06/16 16:49:53 by rdanyell ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_separator(char c)
{
if (c == ' ' || c == '\t' || c == '\n' || c == '\v'
|| c == '\f' || c == '\r' || c == '|' || c == '<' || c == '>')
return (1);
else
return (0);
}
void count_redirects(char *str, int *ind, int *num_words)
{
int i;
int words;
i = *ind;
words = *num_words;
if (str[i] == '<')
{
if (str[i + 1] && str[i + 1] == '<')
i++;
words++;
i++;
}
else if (str[i] == '>')
{
if (str[i + 1] && str[i + 1] == '>')
i++;
words++;
i++;
}
*num_words = words;
*ind = i;
}
void count_rest(char *str, int *ind, int *num_words)
{
int i;
int count_one;
int count_double;
count_one = 0;
count_double = 0;
i = *ind;
while (str[i])
{
if (ft_separator(str[i]) && count_one % 2 == 0 && count_double % 2 == 0)
{
(*num_words)++;
*ind = i;
return ;
}
if (str[i] == '\'' && count_double % 2 == 0)
count_one++;
if (str[i] == '\"' && count_one % 2 == 0)
count_double++;
i++;
}
(*num_words)++;
*ind = i;
}
int count_words(char *str, int i, int words)
{
while (str[i])
{
while (str[i] && ft_isspace(str[i]))
i++;
if (str[i] == '|')
{
words++;
i++;
}
else if (str[i] == '<' || str[i] == '>')
count_redirects(str, &i, &words);
else if (str[i + 1] && (!ft_strncmp(&str[i], "\'\'", 2)
|| !ft_strncmp(&str[i], "\"\"", 2)))
i += 2;
else
count_rest(str, &i, &words);
}
return (words);
}