GCC Code Coverage Report


Directory: ./
File: src/line-editing/my_getline.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 45 55 81.8%
Functions: 6 6 100.0%
Branches: 17 30 56.7%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the getline functions
6 */
7 /**
8 * @file my_getline.c
9 * @brief The file containing the getline functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Insert a character at the index
16 * @param str The string
17 * @param c The character to insert
18 * @param pos The position of the cursor
19 * @param str2 The second string
20 * @return <b>void</b>
21 */
22 10904 void insert_index(char *str, char c, int *pos, char **str2)
23 {
24
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10904 times.
10904 if (pos[0] != pos[1]) {
25 *str2 = my_malloc_strdup(&str[pos[0]]);
26 }
27 10904 str[pos[0]] = c;
28 10904 str[pos[0] + 1] = 0;
29 10904 pos[1]++;
30 10904 }
31
32 /**
33 * @brief Display the two strings
34 * @param pos The position of the cursor
35 * @param str The string
36 * @param str2 The second string
37 * @return <b>void</b>
38 */
39 10904 static void print_two_strings(char *str, char *str2, int *pos, char c)
40 {
41
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10904 times.
10904 if (pos[0] == pos[1])
42 return;
43
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10904 times.
10904 for (int i = 0; i < my_strlen(str2); i++)
44 my_fprintf(0, " ");
45
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10904 times.
10904 for (int i = 0; i < my_strlen(str2); i++)
46 my_fprintf(0, "\b");
47 10904 my_fprintf(0, "%c", c);
48 10904 my_fprintf(0, "%s", str2);
49
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10904 times.
10904 for (int i = 0; i < my_strlen(str2); i++)
50 my_fprintf(0, "\b");
51
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10904 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
10904 if (str2 != NULL && str2[0] != 0)
52 my_strcat(str, str2);
53 }
54
55 /**
56 * @brief Check if there are an arrow key and execute them
57 * @param pos The position of the cursor
58 * @param str The string
59 * @param str2 The second string
60 * @return <b>void</b>
61 */
62 10904 static void condition(char c, int *pos, char *str, char **str2)
63 {
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10904 times.
10904 if (c == '\033')
65 move_cursor(pos, str, str2);
66 else {
67 10904 insert_index(str, c, pos, str2);
68 10904 print_two_strings(str, *str2, pos, c);
69 10904 pos[0]++;
70 }
71 10904 }
72
73 /**
74 * @brief Check if there are instructions to do
75 * with the character (controls, backspace...)
76 * @param c The character
77 * @param pos The position of the cursor
78 * @param str The string
79 * @param str2 The second string
80 * @return <b>int</b> <u>1</u> if the character is a control character,
81 * <u>0</u> otherwise
82 */
83 10904 static int instruction_to_do(char c, int *pos, char *str, char **str2)
84 {
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10904 times.
10904 if (ctrl(c, pos, str, str2) == 0)
86 return 1;
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10904 times.
10904 if (backspace(c, pos, str, str2) == 0)
88 return 1;
89 10904 return 0;
90 }
91
92 /**
93 * @brief Stop the getline function
94 * @param line The line to store the command
95 * @param str The string
96 * @param type The type of the stop
97 * @return <b>int</b> <u>0</u> if success, <u>-1</u> if error
98 */
99 1027 static int stop_getline(char **line, char *str, int type)
100 {
101
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 1011 times.
1027 if (type) {
102 16 *line = NULL;
103 16 return -1;
104 }
105 1011 *line = my_strdup(str);
106 1011 return 0;
107 }
108
109 /**
110 * @brief Get the line from the stream
111 * @param line The line to store the command
112 * @param stream The stream
113 * @return <b>int</b> <u>0</u> if success, <u>-1</u> if error
114 */
115 1027 int my_getline(char **line, FILE *stream)
116 {
117 1027 char c = 0;
118 1027 char *str2 = NULL;
119 1027 char str[2049] = {0};
120 1027 int *pos = CALLOC(2, sizeof(int));
121 1027 int instruction = 0;
122
123 1027 disable_buffer();
124 while (1) {
125 10920 c = getchar();
126
3/4
✓ Branch 0 taken 10904 times.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10904 times.
10920 if (c == -1 || c == CTRL_D)
127 16 return stop_getline(line, str, 1);
128 10904 instruction = instruction_to_do(c, pos, str, &str2);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10904 times.
10904 if (instruction)
130 continue;
131 10904 condition(c, pos, str, &str2);
132
2/2
✓ Branch 0 taken 1011 times.
✓ Branch 1 taken 9893 times.
10904 if (c == '\n')
133 1011 return stop_getline(line, str, 0);
134 }
135 }
136