-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappdata.c
244 lines (224 loc) · 6.37 KB
/
appdata.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include "common.h"
#include "appdata.h"
void appdata_save(int all) {
// Save quiz data if admin
if (currentuser.type==1 || all) {
save_quizdata();
}
//Save userdata
if (all) {
// Save all users data
for (int i = 0; i < no_of_currentusers; ++i) {
save_userdata(i);
}
}
else {
save_userdata(currentuser.ID);
}
}
void appdata_read() {
FILE *datafile;
FILE *userdata;
// Read common appdata
printf("%sSomeone is writting to the appdata!%s\nIf this error persists try deleting all appdata lock files and restart Aquisys.\n", red, normal);
while(checklock(0,0));
clearscr();
// Open the datafile
datafile = fopen(".appdata.dat", "rb");
// Check if open
if (datafile == NULL) {
printf("Is this the first time you have started the quiz application?\n[Answer 'y' for yes and 'n' for no]\n");
char com;
scanf("%c", &com);
clearBuf();
if (com=='y'||com=='Y') {
//Make default appdata
for (int i = 0; i < max_tags; i++) {
strcpy(taglist[i],"");
}
strcpy(userlist[0].username,"admin");
strcpy(userlist[0].password,"1");
userlist[0].type=1;
strcpy(userlist[1].username,"student");
strcpy(userlist[1].password,"1");
userlist[1].type=0;
quizlist.no_of_quizes = 0;
no_of_currentusers = 2;
appdata_save(1);
clearscr();
//Then try again
appdata_read();
}
else if (com=='n'||com=='N') {
printf("%sError!\nAppdata not found\n%sIf this error persists try deleting all appdata files and restart Aquisys.\n", red, normal);
exit(1);
}
else {
printf("%sInvalid option!\nPlease try again,%s", red, normal);
appdata_read();
}
}
else {
// Read common data
fread(&quizlist.no_of_quizes,sizeof(int),1,datafile);
fread(&quizlist.quiz, sizeof(struct Quiz), quizlist.no_of_quizes, datafile);
for (int i = 0; i < quizlist.no_of_quizes; ++i) {
for (int j = 0; j < quizlist.quiz[i].no_of_questions; ++j) {
fread(question[i][j], sizeof(struct Question), max_alternative_q, datafile);
}
}
fread(&no_of_currentusers,sizeof(int),1,datafile);
fread(taglist,sizeof(taglist),1,datafile);
fclose(datafile);
// Read all users data
for (int i = 0; i < no_of_currentusers; ++i) {
read_userdata(i);
}
}
}
void save_userdata(int i) {
FILE *userdata;
//Open userdata
char udatafile[100];
sprintf(udatafile, ".appdata-%d.dat", i);
printf("%sSomeone is writting to the appdata!%s\nIf this error persists try deleting all appdata lock files and restart Aquisys.\n", red, normal);
while (checklock(1,i));
clearscr();
lock(1,i);
userdata = fopen(udatafile, "wb");
// Check if open
if (userdata == NULL) {
printf("\n%sError!\nCould not open userdata%s\nIf this error persists try deleting all appdata files and restart Aquisys.\n", red, normal);
exit (1);
}
fwrite(&userlist[i], sizeof(struct User), 1, userdata);
fwrite(quizes_attempted[i], sizeof(struct Quizes_attempted), quizlist.no_of_quizes, userdata);
if (userlist[i].type==0) {
//If it is student save responces
for (int j = 0; j < quizlist.no_of_quizes; ++j) {
for (int k = 0; k < quizlist.quiz[j].no_of_questions; ++k)
{
fwrite(response[i][j][k], sizeof(struct Response), max_alternative_q, userdata);
}
}
}
fclose(userdata);
unlock(1,i);
}
void read_userdata(int i) {
FILE *userdata;
//Open userdata
char udatafile[100];
sprintf(udatafile, ".appdata-%d.dat", i);
printf("%sSomeone is writting to the appdata!%s\nIf this error persists try deleting all appdata lock files and restart Aquisys.\n", red, normal);
while(checklock(1,i));
clearscr();
userdata = fopen(udatafile, "rb");
// Check if open
if (userdata == NULL) {
printf("\n%sError!\nCould not open userdata%s\nIf this error persists try deleting all appdata files and restart Aquisys.\n", red, normal);
exit (1);
}
fread(&userlist[i], sizeof(struct User), 1, userdata);
fread(quizes_attempted[i], sizeof(struct Quizes_attempted), quizlist.no_of_quizes, userdata);
if (userlist[i].type==0) {
//If it is student read responces
for (int j = 0; j < quizlist.no_of_quizes; ++j) {
for (int k = 0; k < quizlist.quiz[j].no_of_questions; ++k)
{
fread(response[i][j][k], sizeof(struct Response), max_alternative_q, userdata);
}
}
}
fclose(userdata);
}
int checklock(int type, int i) {
// Check if type is common data
if (type==0) {
FILE *lock;
// Check for lock
lock = fopen(".appdata.dat.lock", "rb");
if (lock == NULL) {
return 0;
}
else {
fclose(lock);
return 1;
}
}
// Check if type is student
if (type==1) {
FILE *lock;
// Check for lock
char udatafilelock[100];
sprintf(udatafilelock, ".appdata-%d.dat.lock", i);
lock = fopen(udatafilelock, "rb");
if (lock == NULL) {
return 0;
}
else {
fclose(lock);
return 1;
}
}
}
void lock(int type, int i) {
// Check if type is common data
if (type==0) {
FILE *lock;
// Make lock
lock=fopen(".appdata.dat.lock", "wb");
fclose(lock);
}
// Check if type is student
if (type==1) {
FILE *lock;
// Make lock
char udatafilelock[100];
sprintf(udatafilelock, ".appdata-%d.dat.lock", i);
lock=fopen(udatafilelock, "wb");
fclose(lock);
}
}
void unlock(int type, int i) {
// Check if type is common data
if (type==0) {
// Delete lock
remove(".appdata.dat.lock");
}
// Check if type is student
if (type==1) {
// Delete lock
char udatafilelock[100];
sprintf(udatafilelock, ".appdata-%d.dat.lock", i);
remove(udatafilelock);
}
}
void save_quizdata() {
FILE *datafile;
printf("%sSomeone is writting to the appdata!%s\nIf this error persists try deleting all appdata lock files and restart Aquisys.\n", red, normal);
while(checklock(0,0));
clearscr();
lock(0,0);
//Open the datafile
datafile = fopen(".appdata.dat", "wb");
//Check if open
if (datafile == NULL)
{
printf("\n%sError!\nCould not open appdata%s\nIf this error persists try deleting all appdata files and restart Aquisys.\n", red, normal);
exit (1);
}
printf("Saving data...\n");
// Save common data
fwrite(&quizlist.no_of_quizes,sizeof(int),1,datafile);
fwrite(&quizlist.quiz, sizeof(struct Quiz), quizlist.no_of_quizes, datafile);
for (int i = 0; i < quizlist.no_of_quizes; ++i) {
for (int j = 0; j < quizlist.quiz[i].no_of_questions; ++j) {
fwrite(question[i][j], sizeof(struct Question), max_alternative_q, datafile);
}
}
fwrite(&no_of_currentusers,sizeof(int),1,datafile);
fwrite(taglist,sizeof(taglist),1,datafile);
fclose(datafile);
unlock(0,0);
}