-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsrmformatter.c
187 lines (157 loc) · 4.02 KB
/
hsrmformatter.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
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "hsrmformatter.h"
char *klausur = " - Klausur";
char *cons = " ";
char *delete_whitespace(char *text)
{
while(isspace(*text) && *text != '\0')
text++;
return text;
}
void write_data(char* into, char** text)
{
int k=0;
char *from = *text ;
while( !(isspace(*(from)) && isspace(*(from+1))) )
into[k++] = *(from++);
*text = from;
}
char *find_end_of_module(char *text)
{
// lolol this is so shitty
while((*text != 'E' && *(text+1) != 'N' && *(text+2) != 'D' &&
*(text+3) != 'O' && *(text+4) != 'F') && *text != '\0')
text++;
return text+11;
}
module *create_module(char **table_string)
{
char *text = *table_string;
module *m = malloc(sizeof(module));
// printf("Starting to create module.\n");
text = delete_whitespace(text);
write_data(m->course_number,&text);
// printf("Number written. %s\n", m->course_number);
text = delete_whitespace(text);
write_data(m->course_desc,&text);
// printf("Description written.\n");
text = delete_whitespace(text);
if(*text == 'W' || *text == 'S')
{
write_data(m->sem,&text);
text = delete_whitespace(text);
// printf("Semester written.\n");
}
if(*text == '0' && isdigit(*(text+1)))
{
write_data(m->termin,&text);
text = delete_whitespace(text);
// printf("Termin written.\n");
}
if(isdigit(*text) && *text != '0' && !isdigit(*(text+1)))
{
write_data(m->grade,&text);
text = delete_whitespace(text);
// printf("Grade written. %s\n",m->grade);
}
if(!isdigit(*text))
{
write_data(m->status,&text);
// printf("Status written. %s\n",m->status);
}
text = delete_whitespace(text);
write_data(m->credit_points,&text);
text = find_end_of_module(text);
*table_string = text;
// printf("Finishing up, table_string should now be at %c and the address: %x\n", *text, *table_string);
return m;
}
modarray sort_data(char *table_string)
{
module *m,*old;
int j=0,first=1;
modarray modarr;
while(*table_string)
{
old = m;
m = create_module(&table_string);
j++;
if(first)
{
modarr.modulepointer = m;
first = 0;
continue;
}
old->next = m;
}
modarr.length = j;
return modarr;
}
void print_module(module m, module *prev)
{
char *str = calloc(255,sizeof(char));
if(prev != NULL)
{
size_t i,len=strlen(prev->course_desc);
for (i = 0; i < len; i++)
if(m.course_desc[i] != prev->course_desc[i])
break;
if(i == len)
{
int k = 1;
strcat(str, cons);
while(m.course_desc[i] != '\0')
{
str[k] = m.course_desc[i];
i++;
k++;
}
str[k] = '\0';
if(k == 1)
strcat(str, klausur);
} else {
strcpy(str, m.course_desc);
printf("| | | | | |\n");
}
} else {
strcpy(str, m.course_desc);
}
for (int i = 0; i < 254; ++i) {
/* super ugly hack to kill the umlaut ä in the course title. */
if(str[i] == -61 && str[i + 1] == -92)
str[i] = 'a', str[i + 1] = 'e';
/* super ugly hack to kill the umlaut ü in the course title. */
if(str[i] == -61 && str[i + 1] == -68)
str[i] = 'u', str[i + 1] = 'e';
}
printf("| %4s | %-25.25s | %4s | %-8.8s | %5.5s |\n",
m.course_number, str, m.grade, m.sem,
m.credit_points);
free(str);
}
void print_all(module *m)
{
module *old;
printf(".------+---------------------------+------+----------+-------.\n");
printf("| Nr | Modul | Note | Semester | CrPt |\n");
printf("|------+---------------------------+------+----------+-------|\n");
while(m != NULL)
{
print_module(*m,old);
old = m;
m = m->next;
}
printf("`------+---------------------------+------+----------+-------´\n");
}
int main(int argc, char** argv)
{
// FILE *fp = fopen(*argv,"r");
char *tab_data = *(argv+1);
//print_tbl_hd();
modarray ma = sort_data(tab_data);
print_all(ma.modulepointer);
return 0;
}