-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_new_len_handling.c
137 lines (123 loc) · 2.96 KB
/
get_new_len_handling.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
#include "main.h"
ssize_t get_new_len(char *line);
ssize_t handle_semicolon_case(char *line, size_t index, char next);
ssize_t handle_initial_semicolon_case(char *line, size_t index, char next);
int should_insert_space(char previous, char current, char next);
void update_new_len(char *line, ssize_t *new_len);
/**
*get_new_len - Gets the new length of a line partitioned
* by ";", "||", "&&&", or "#".
*@line: The line to check.
*
*Return: The new length of the line.
*
*Description: Cuts short lines containing '#' comments with '\0'.
*/
ssize_t get_new_len(char *line)
{
size_t i;
ssize_t new_len = 0;
char current, next;
for (i = 0; line[i]; i++)
{
current = line[i];
next = line[i + 1];
if (current == '#')
{
if (i == 0 || line[i - 1] == ' ')
{
line[i] = '\0';
break;
}
}
else if (i != 0)
{
if (current == ';')
{
if (should_insert_space(line[i - 1], current, next))
new_len += 2;
else
{
if (line[i - 1] != ' ')
new_len++;
if (next != ' ')
new_len++;
}
}
else
update_new_len(&line[i], &new_len);
}
else if (current == ';')
{
if (i != 0 && line[i - 1] != ' ')
new_len++;
if (next != ' ' && next != ';')
new_len++;
}
new_len++;
}
return (new_len);
}
/**
*handle_semicolon_case - Handles the new length
*calculation for semicolon cases.
*@line: The line to check.
*@index: The current index in the line.
*@next: The next character.
*
*Return: The additional length to add.
*
*Description: Handles the new length calculation for cases
* involving semicolons.
* It checks for specific conditions and returns
* the additional length to add.
*/
ssize_t handle_semicolon_case(char *line, size_t index, char next)
{
ssize_t additional_len = 0;
if (next != ' ')
additional_len++;
if (line[index - 1] != ' ')
additional_len++;
return (additional_len + 2);
}
/**
*handle_initial_semicolon_case - Handles the new
* length calculation for initial semicolon case.
*@line: The line to check.
*@index: The current index in the line.
*@next: The next character.
*
*Return: The additional length to add.
*
*Description: Handles the new length calculation
* for the initial semicolon case.
* It checks for specific conditions
* and returns the additional length to add.
*/
ssize_t handle_initial_semicolon_case(char *line, size_t index, char next)
{
ssize_t additional_len = 0;
if (line[index - 1] != ' ')
additional_len++;
if (next != ' ' && next != ';')
additional_len++;
return (additional_len);
}
/**
*update_new_len - Updates the new length based on specific conditions.
*@line: The line to check.
*@new_len: Pointer to the new length.
*
*Description: Handles the new length calculation for specific conditions
* in the line.
*/
void update_new_len(char *line, ssize_t *new_len)
{
if (line[0] == ';' && line[1] == ';')
*
new_len += 2;
else if (line[0] == ';' || line[0] == '|' || line[0] == '&')
*
new_len += 1;
}