-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpansions_quotes_2.c
129 lines (119 loc) · 3.18 KB
/
expansions_quotes_2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expansions_quotes_2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngasco <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/14 12:00:38 by ngasco #+# #+# */
/* Updated: 2022/03/14 12:00:52 by ngasco ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/* Three cases: quoted string, quoted string but empty, unquoted string */
char *ft_write_str_to_node(t_qdata *t_qdata, int end)
{
if (end - t_qdata->start > 1)
return (ft_create_quoted_token(t_qdata->raw_input,
t_qdata->start, end - t_qdata->start));
else
{
if (t_qdata->raw_input[t_qdata->start] == '\''
|| t_qdata->raw_input[t_qdata->start] == '\"')
return ((ft_create_quoted_token_empty(t_qdata->raw_input,
t_qdata->start)));
else
return (ft_create_unquoted_token(t_qdata->raw_input, t_qdata->start,
end - t_qdata->start));
}
}
/* a) Actual token text is written into node ignoring quotes */
char *ft_create_quoted_token(char *input, int start, int len)
{
char *result;
int result_len;
int i;
int y;
i = start;
result_len = len;
y = 0;
if (input[start] == '\"' || input[start] == '\'')
i++;
if (input[start + len] == '\"' || input[start + len] == '\'')
{
if (input[start] == '\"' || input[start] == '\'')
result_len--;
}
result = (char *)malloc(sizeof(char) * (result_len + 1));
while (result_len != 0)
{
result[y] = input[i];
i++;
y++;
result_len--;
}
result[y] = '\0';
return (result);
}
/* b) Case where empty set of quotes is given "" */
char *ft_create_quoted_token_empty(char *input, int start)
{
char *result;
int i;
int y;
i = start;
y = 0;
result = (char *)malloc(sizeof(char) * (ft_strlen(input) - start - 2 + 1));
while (i < start)
{
result[y] = input[i];
i++;
y++;
}
result[y] = '\0';
return (result);
}
/* c) Case where no quote is present */
char *ft_create_unquoted_token(char *input, int start, int len)
{
char *result;
int result_len;
int i;
int y;
i = start;
result_len = len;
y = 0;
result = (char *)malloc(sizeof(char) * (result_len + 1));
while (result_len != 0)
{
result[y] = input[i];
i++;
y++;
result_len--;
}
result[y] = '\0';
return (result);
}
/* Iterate all nodes of linked lists and concatenate them in a string */
char *ft_convert_list_to_str(t_qdata *t_qdata)
{
struct s_qnode *curr;
char *result;
int i;
i = 0;
result = NULL;
curr = t_qdata->quotes_list;
while (1)
{
if (result == NULL)
result = ft_strdup(curr->str);
else
result = ft_strcat(result, curr->str);
if (curr->next == NULL)
break ;
else
curr = curr->next;
i++;
}
return (result);
}