-
Notifications
You must be signed in to change notification settings - Fork 0
/
grading_table.c
92 lines (70 loc) · 1.76 KB
/
grading_table.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include "grading_table.h"
#include <unistd.h>
grading_table_ptr prev;
int read_bytes;
void delete_list(grading_table_ptr head)
{
if(head != NULL)
{
struct grading_entry_t* temp = head->next_entry;
free(head);
delete_list(temp);
}
}
void traverse_list(grading_table_ptr head, void (*visitor)(grading_table_ptr current))
{
if(head != NULL)
{
(*visitor)(head);
traverse_list(head->next_entry, (*visitor));
}
}
int reduce_list(grading_table_ptr head, int (*visitor)(grading_table_ptr current, int val), int start_val)
{
static int final_val = 0;
if(head != NULL)
{
final_val = (*visitor)(head, start_val);
reduce_list(head->next_entry, (*visitor), final_val);
}
return final_val;
}
grading_table_ptr read_list(FILE* fp)
{
uint16_t tasknr;
uint16_t points;
uint32_t matnr;
struct grading_entry_t* prev = 0;
struct grading_entry_t* head = 0;
uint32_t* buf = malloc(sizeof(uint32_t));
while ((read_bytes = read(fileno(fp), buf, 4)) != 0)
{
matnr = *buf;
uint16_t* buf2 = malloc(sizeof(uint16_t));
read_bytes = read(fileno(fp), buf2, 2);
tasknr = *buf2;
read_bytes = read(fileno(fp), buf2, 2);
points = *buf2;
free(buf2);
struct grading_entry_t* pt = malloc(sizeof(struct grading_entry_t));
struct grading_entry_t entry = {matnr, tasknr, points};
*pt = entry;
if(head == 0)
{
head = pt;
}
if(prev == 0)
{
prev = pt;
}
else
{
(*prev).next_entry = pt;
prev = pt;
}
}
return head;
}