-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_handler.c
More file actions
229 lines (199 loc) · 5.07 KB
/
Copy pathinput_handler.c
File metadata and controls
229 lines (199 loc) · 5.07 KB
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
/**
* @file input_handler.c
* @author Max Nonfried (nonfried@students.zcu.cz)
* @brief Zajistuje vstup od uzivatele, ktery nacita z konzole a podle prikazu vola danou funkci.
* @version 1.0
* @date 2022-02-
*/
#include "definitions.h"
/**
* @brief Zajistuje nacitani prikazu od uzivatele.
*
* @param fs_name Jmeno souboru, kde je fs.
*/
void listening(char *fs_name) {
char *input;
while (1) {
input = get_line();
if (strcmp(input, "exit\n") == 0) {
free(input);
break;
}
parse_input(input, fs_name);
free(input);
}
}
/**
* @brief Odstrani znak nove radky ze vstupu a nahradi ho znakem '\0'.
*
* @param arg_input String, kde se ma znak '\n' nahradit '\0'.
*/
void delete_nl_char(char *arg_input) {
int32_t i = 0;
while(arg_input[i] != '\n') {
i++;
}
arg_input[i] = '\0';
}
/**
* @brief Rozdeli vstup od uzivatele. Delici znak je mezera. Podle prikazu zavola danou
* funkci, ktera prikaz vykona.
*
* @param arg_input Vstup od uzivatele.
* @param fs_name Nazev souboru, kde je fs.
*/
void parse_input(char *arg_input, char *fs_name) {
char **input;
int num_of_splits;
delete_nl_char(arg_input);
input = str_split(arg_input, ' ', &num_of_splits); // obsahuje \n
//printf("Vstup: %s", input[0]);
/* podminky pro prikazy */
if (strcmp(input[0], "cp") == 0) {
cp(input);
}
else if (strcmp(input[0], "mv") == 0) {
mv(input);
}
else if (strcmp(input[0], "rm") == 0) {
rm(input);
}
else if (strcmp(input[0], "mkdir") == 0) { // zde
mkdir(input);
}
else if (strcmp(input[0], "rmdir") == 0) {
rmdir(input);
}
else if (strcmp(input[0], "ls") == 0) {
ls(input);
}
else if (strcmp(input[0], "cat") == 0) {
cat(input);
}
else if (strcmp(input[0], "cd") == 0) { // zde
cd(input);
}
else if (strcmp(input[0], "pwd") == 0) { // zde
pwd();
}
else if (strcmp(input[0], "info") == 0) {
info(input);
}
else if (strcmp(input[0], "incp") == 0) { // zde
incp(input);
}
else if (strcmp(input[0], "outcp") == 0) {
outcp(input);
}
else if (strcmp(input[0], "load") == 0) {
load(input, fs_name);
}
else if (strcmp(input[0], "format") == 0) {
format(input, fs_name);
}
else if (strcmp(input[0], "xcp") == 0) {
xcp(input);
}
else if (strcmp(input[0], "short") == 0) {
short_func(input);
}
else {
printf("Command doesn't found.\n");
}
free(input);
}
/**
* @brief Nacte vstup z konzole.
*
* @return char* Text z konzole.
*/
char * get_line() {
char *line = malloc(100), *linep = line;
size_t lenmax = 100, len = lenmax;
int c;
if(line == NULL) {
return NULL;
}
for(;;) {
c = fgetc(stdin);
if(c == EOF) {
break;
}
if(--len == 0) {
len = lenmax;
char * linen = realloc(linep, lenmax *= 2);
if(linen == NULL) {
free(linep);
return NULL;
}
line = linen + (line - linep);
linep = linen;
}
if((*line++ = c) == '\n') {
break;
}
}
*line = '\0';
return linep;
}
/**
* @brief Rozdeli string na casti podle deliciho znaku.
*
* neobvykle pripady priklad, delim '/'
* "hola" --> numsplits = 1
* "/hola" --> numsplits = 2
* "hola/" --> numsplits = 2
* "/" --> numsplits = 2
* @param str Srting, ktery se ma rozdelit.
* @param delim Delici znak.
* @param numSplits Pocet rozdelni.
* @return char** Pole stringu.
*/
char** str_split(char *str, char delim, int *numSplits) {
char** ret;
int retLen = 0;
char* c;
if ((str == NULL) || (delim == '\0')) {
ret = NULL;
retLen = -1;
}
else {
retLen = 0;
c = str;
do {
if (*c == delim) {
retLen++;
}
c++;
} while (*c != '\0');
ret = malloc((retLen + 1) * sizeof(*ret));
ret[retLen] = NULL;
c = str;
retLen = 1;
ret[0] = str;
do {
if (*c == delim) {
ret[retLen++] = &c[1];
*c = '\0';
}
c++;
} while (*c != '\0');
}
if (numSplits != NULL) {
*numSplits = retLen;
}
return ret;
}
/**
* @brief Vrati delku souboru v bytech.
*
* @param fptr Soubor, u ktereho chceme zjistit delku.
* @return Delku souboru v bytech.
*/
int32_t get_file_lenght(FILE *fptr) {
int32_t file_lenght = 0;
fseek(fptr, 0, SEEK_END);
file_lenght = ftell(fptr);
rewind(fptr);
return file_lenght;
}