/
cli.c
152 lines (135 loc) · 3.58 KB
/
cli.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/** @file cli.c
* @brief implementation of cli.h
*
* @author Frederik Dudzik (doodzik)
* @bug No known bugs.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cli.h"
#include "util.h"
#include "student.h"
#include "cli_student.h"
#include "student_list.h"
int is_function(char function[], char str[])
{
if(strlen(str) > 99 || strlen(function) > 99) return 1;
char* str2 = concat(function, "\n");
int i = (strncmp(str, str2, 100) == 0);
free(str2);
return i;
}
int cli_push(StudentList ** sl, int* sl_length)
{
Student *s, *tmp;
tmp = newStudentCli(&s);
// TODO get to know why lastName is empty
if (tmp == 0L) {
printf("please varify your input\n");
}else{
push(sl, s);
++*sl_length;
}
return 0;
}
int cli_unshift(StudentList ** sl, int* sl_length)
{
// TODO: unshift to return status code
Student *s, *tmp;
tmp = newStudentCli(&s);
if (tmp == 0L) {
printf("please varify your input\n");
}else{
unshift(sl, s);
++*sl_length;
}
return 0;
}
int cli_print_all(StudentList* sl)
{
// TODO: this to return status code
printStudentList(sl);
return 0;
}
int cli_length(int* sl_length)
{
printf("\nLength: %d\n", *sl_length);
return 0;
}
int cli_delete_all(StudentList* sl, int* sl_length)
{
if(*sl_length > 0) destroyList(&sl);
*sl_length = 0;
return 0;
}
int cli_get_student_id()
{
char stundent_id[10];
printf("enter student id:\n");
fgets(stundent_id, sizeof(stundent_id), stdin);
size_t idLn = strlen(stundent_id) - 1;
if (stundent_id[idLn] == '\n')
stundent_id[idLn] = '\0';
else flushStdin();
return atoi(stundent_id);
}
int cli_delete(StudentList** sl, int* sl_length)
{
int result = destroy(sl, cli_get_student_id());
*sl_length = *sl_length - result;
return 0;
}
int cli_find_id(StudentList* sl)
{
Student* s = studentFindById(sl, cli_get_student_id());
if (s == 0L)
printf("NO SUCH STUDENT\n");
else
printStudent(s);
return 0;
}
int cli_query(StudentList* sl)
{
printf("IF YOU DONT CARE FOR AN ATTRIBUTE JUST CLICK ENTER\n");
printf("ALL SPECIFIED VALUES CHECK IF THE STUDENT CONTAINS THE GIVEN VALUE WITHIN ITS VALUE\n\n");
Student* s;
newStudentCliWithoutNullCheck(&s);
queryStudentList(sl, s);
free(s);
return 0;
}
int cli_sort_studiengang(StudentList* sl)
{
sortStudiengang(sl);
return 0;
}
int cli_sort_martikel(StudentList* sl)
{
sortMatrikel(sl);
return 0;
}
void print_possible_functions()
{
printf("\n");
printf("What function do you want to run? Type the name and confirm with enter \n");
printf("PUSH, UNSHIFT, PRINT_ALL, LENGTH, DELETE, DELETE_ALL, FIND_ID, QUERY, SORT_MARTRIKEL, SORT_STUDIENGANG, QUIT\n");
printf("\n");
}
int cli_function(char str[], StudentList** sl, int* sl_length)
{
printf("\n");
if(is_function("PUSH", str)) return cli_push(sl, sl_length);
if(is_function("UNSHIFT", str)) return cli_unshift(sl, sl_length);
if(is_function("PRINT_ALL", str)) return cli_print_all(*sl);
if(is_function("LENGTH", str)) return cli_length(sl_length);
if(is_function("DELETE", str)) return cli_delete(sl, sl_length);
if(is_function("DELETE_ALL", str)) return cli_delete_all(*sl, sl_length);
if(is_function("FIND_ID", str)) return cli_find_id(*sl);
if(is_function("QUERY", str)) return cli_query(*sl);
if(is_function("SORT_STUDIENGANG", str)) return cli_sort_studiengang(*sl);
if(is_function("SORT_MARTRIKEL", str)) return cli_sort_martikel(*sl);
if(is_function("QUIT", str)) return 1;
printf("No such Function\n");
return 0;
}