-
Notifications
You must be signed in to change notification settings - Fork 0
/
readWriteData.c
557 lines (483 loc) · 16.7 KB
/
readWriteData.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/********************************************************************
*
* readWriteData.c
*
* This program is responsible for reading, writing
* modifying and creating new computer dating application's
* database file. The Database file is created as a binary file.
*
* Created by Jiajia Bai (Jia) ID 63070503404
* Sonia Gautam (Sonia) ID 63070503410
* Tamonwan Boonpa (Nice) ID 63070503418
* Theeranont Ruchiyakorn (Peak) ID 63070503420
*
* 18 April 2021
*
********************************************************************
*/
#include "datingApp.h"
#include "abstractHashTable.h"
#define BUCKET_COUNT 4993 /* the number of buckets in the hash table */
/* Adapted with permission from function by Aj. Sally
* in file hashTester.c
*/
/* Robust hash function that uses bitwise operations to
* modify string values. Adapted from Kyle Loudon,
* "Mastering Algorithms with C"
*/
unsigned int bitwiseOpHash(char* key)
{
unsigned int result = 0;
unsigned int tmp = 0;
int size = hashTableSize();
int i = 0;
for (i = 0; i < strlen(key); i++)
{
/* shift up four bits then add in next char */
result = (result << 4) + key[i];
if (tmp = (result & 0xf0000000)) /* if high bit is set */
{
/* XOR result with down shifted tmp */
result = result ^ (tmp >> 24);
/* then XOR with tmp itself */
result = result ^ tmp;
}
}
result = result % size; /* make it fit in the table size */
return result;
}
/* public function */
/**************************************************************
*
* This function checks if the database file exists or not.
* If not, the new database file will be created and number
* of people in the file (zero at initial) will be written.
*
* No argument.
*
* This function returns 'status' which is 1 when the
* database file is found, otherwise 0.
*
*************************************************************/
int checkFile()
{
FILE* pNew = NULL; /* File pointer - will be used to access file */
int status = 0; /* to store return value */
int fileStatus = 0; /* to store return value from access function */
int peopleCount = 0; /* to store number of people at the beginning */
/* check if the database file exists or not*/
fileStatus = access(FILENAME,F_OK);
if (fileStatus == 0)
{
status = 1;
}
else
{
printf("\n\n* * * * * * * * * * * * * * * * * * * * * * * ");
printf("\n\n\t - Database file not found -\n");
/* create new database file */
pNew = fopen(FILENAME,"wb");
if (pNew == NULL)
{
printf("\n\tError creating new database file!\n");
exit(0);
}
/* write number of people(0) in the file */
if (fwrite(&peopleCount,sizeof(int),1,pNew) != 1)
{
printf("\n\tError writing number of people in database file!\n");
exit(4);
}
if (fclose(pNew))
{
printf("\n\tError occurred while closing the file!\n");
exit(3);
}
printf("\t Created new database file!!\n");
printf("\n* * * * * * * * * * * * * * * * * * * * * * * \n\n");
}
return status;
}
/* public function */
/**************************************************************
*
* This function reads the number of people in the database
* file and returns it to the calling function.
*
* No argument.
*
* This function returns 'peopleCount' which is the number
* of people in the database file.
*
*************************************************************/
int readToCount()
{
FILE* pDatabase = NULL; /* File pointer - will be used to access file */
int peopleCount = 0; /* to get number of people in the file */
pDatabase = fopen(FILENAME,"rb");
if (pDatabase == NULL)
{
printf("\n\tError occurred while opening the file!\n");
exit(1);
}
/* read number of people in the file */
if (fread(&peopleCount,sizeof(int),1,pDatabase) != 1)
{
printf("\n\tError occurred while reading the file!\n");
exit(2);
}
if (fclose(pDatabase))
{
printf("\n\tError occurred while closing the file!\n");
exit(3);
}
return peopleCount;
}
/* public function */
/**************************************************************
*
* This function reads all data from the file and store each
* users's data in the array of structure.
*
* userData is array of pointer PERSON_T structure to
* store each user's data.
*
*************************************************************/
void readData(PERSON_T** userData)
{
FILE* pDatabase = NULL; /* File pointer - will be used to access file */
PERSON_T* pEntry = NULL; /* pointer to structure for each user */
int personCount = 0; /* to get number of people in the file */
int indexCount = 0; /* to increment index number */
int i = 0; /* loop variable */
pDatabase = fopen(FILENAME,"rb");
if (pDatabase == NULL)
{
printf("\n\tError occurred while opening the file!\n");
exit(1);
}
/* read number of people in the file */
if (fread(&personCount,sizeof(int),1,pDatabase) != 1)
{
printf("\n\tError occurred while reading the file!\n");
exit(2);
}
/* read each person's information stored in the file */
for (i = 0; i < personCount; i++)
{
pEntry = calloc(1,sizeof(PERSON_T));
if (pEntry == NULL)
{
printf("\n\tMemory allocation failure - exiting!\n");
exit(0);
}
if (fread(pEntry,sizeof(PERSON_T),1,pDatabase))
{
userData[indexCount] = pEntry;
indexCount++;
}
else
{
printf("\n\tError occurred while reading the file!\n");
exit(2);
}
}
if (fclose(pDatabase))
{
printf("\n\tError occurred while closing the file!\n");
exit(3);
}
}
/* public function */
/**************************************************************
*
* This function calls other functions to check the file
* status. If the file and user's data exist, it calls the
* responsible function to read the user's data and add user
* to the tree and insert user's data into the hash table.
*
* No argument.
*
*************************************************************/
void readFile()
{
PERSON_T** userData = NULL; /* array of pointer PERSON_T structure */
/* to store each user's data */
int personCount = 0; /* to get number of people in the file */
int fileStatus = 0; /* to store return value from access function */
int pCollision = 0; /* store collision status */
int result = 0; /* to store return value from other function */
int i = 0; /* loop variable */
/* check if the database file exists or not */
fileStatus = checkFile();
/* get the number of people in the file */
personCount = readToCount();
/* initialize the hash table */
result = hashTableInit(BUCKET_COUNT,&bitwiseOpHash);
if (result == 0)
{
printf("\n\tError occurred while initializing the hash table!\n");
exit(0);
}
/* check if there's the data in the file or not */
if ((fileStatus == 1) && (personCount > 0))
{
userData = calloc(personCount,sizeof(PERSON_T*));
if (userData == NULL)
{
printf("\n\tMemory allocation failure - exiting!\n");
exit(0);
}
/* read all user's data */
readData(userData);
for (i = 0; i < personCount; i++)
{
/* add each user to appropriate list */
addNewPerson(userData[i]);
/* insert each user's data into hash table */
/* user's username is the key */
result = hashTableInsert(userData[i]->username,userData[i],&pCollision);
if (result == 0)
{
printf("\n\tError occurred while inserting %s's information into hash table!\n",userData[i]->name);
}
}
free(userData);
}
}
/* public function */
/**************************************************************
*
* This function adds new data into the database file and
* updates number of people in the file at the top of
* the file. This function also insert new user's data
* into the hash table.
*
* Arguments
* pNew - a pointer to structure storing
* new user's data
* peopleCount - the number of people in the file
*
*************************************************************/
void addNewData(PERSON_T* pNew, int personCount)
{
FILE* pDatabase = NULL; /* File pointer - will be used to access file */
int pCollision = 0; /* to store collision status */
int result = 0; /* to store return value from other function */
pDatabase = fopen(FILENAME,"rb+");
if (pDatabase == NULL)
{
printf("\n\tError occurred while opening the file!\n");
exit(1);
}
/* move to the end of the file */
fseek(pDatabase,0,SEEK_END);
/* write new user's data */
if (fwrite(pNew,sizeof(PERSON_T),1,pDatabase) != 1)
{
printf("\n\tError occurred while writing new data to the file!\n");
exit(4);
}
/* move to the beginning of the file */
fseek(pDatabase,0,SEEK_SET);
/* write number of people in the file */
if (fwrite(&personCount,sizeof(int),1,pDatabase) != 1)
{
printf("\n\tError occurred while writing number of person to the file!\n");
exit(4);
}
else
{
if (fclose(pDatabase))
{
printf("\n\tError occurred while closing the file!\n");
exit(3);
}
}
/* insert new user's data into hash table */
/* user's username is the key */
result = hashTableInsert(pNew->username,pNew,&pCollision);
if (result == 0)
{
printf("\n\tError occurred while inserting %s's information into hash table!\n",pNew->name);
}
}
/* private function */
/**************************************************************
*
* This function writes modified version data into the file
* by using the username to compare people in the file to
* find the person whose data has been modified.
*
* Arguments
* data - array of pointer PERSON_T structure
* storing each user's data.
* pPerson - pointer to structure storing currently
* logged in user's updated information
* personCount - the number of people in the file
*
*************************************************************/
void addByUsername(PERSON_T** data, PERSON_T* pPerson, int personCount)
{
FILE* pDatabase = NULL; /* File pointer - will be used to access file */
int i = 0; /* loop variable */
pDatabase = fopen(FILENAME,"rb+");
if (pDatabase == NULL)
{
printf("\n\tError occurred while opening the file!\n");
exit(1);
}
for (i = 0; i < personCount; i++)
{
/* move to the end of the file */
fseek(pDatabase,0,SEEK_END);
if (strcmp(pPerson->username,data[i]->username) == 0)
{
/* write updated version of current user's data */
if (fwrite(pPerson,sizeof(PERSON_T),1,pDatabase) != 1)
{
printf("\n\tError occurred while writing new data to the file!\n");
exit(4);
}
}
else
{
/* write other user's data */
if (fwrite(data[i],sizeof(PERSON_T),1,pDatabase) != 1)
{
printf("\n\tError occurred while writing new data to the file!\n");
exit(4);
}
}
}
if (fclose(pDatabase))
{
printf("\n\tError occurred while closing the file!\n");
exit(3);
}
}
/* private function */
/**************************************************************
*
* This function writes modified version data into the file
* by using the email to compare people in the file to find
* the person whose username has been modified.
*
* Arguments
* data - array of pointer PERSON_T structure
* storing each user's data.
* pPerson - pointer to structure storing currently
* logged in user's updated information
* personCount - the number of people in the file
*
*************************************************************/
void addByEmail(PERSON_T** data, PERSON_T* pPerson, int personCount)
{
FILE* pDatabase = NULL; /* File pointer - will be used to access file */
int i = 0; /* loop variable */
pDatabase = fopen(FILENAME,"rb+");
if (pDatabase == NULL)
{
printf("\n\tError occurred while opening the file!\n");
exit(1);
}
for (i = 0; i < personCount; i++)
{
/* move to the end of the file */
fseek(pDatabase,0,SEEK_END);
if (strcmp(pPerson->email,data[i]->email) == 0)
{
/* write updated version of current user's data */
if (fwrite(pPerson,sizeof(PERSON_T),1,pDatabase) != 1)
{
printf("\n\tError occurred while writing new data to the file!\n");
exit(4);
}
}
else
{
/* write other user's data */
if (fwrite(data[i],sizeof(PERSON_T),1,pDatabase) != 1)
{
printf("\n\tError occurred while writing new data to the file!\n");
exit(4);
}
}
}
if (fclose(pDatabase))
{
printf("\n\tError occurred while closing the file!\n");
exit(3);
}
}
/* public function */
/**************************************************************
*
* This function writes the number of people in the database
* file and call responsible functions to write the modified
* data into the file.
*
* Arguments
* pPerson - pointer to structure storing currently
* logged in user's updated information
* type - to indicate how data should be rewritten
*
* if type = 1, username is not modified, so username
* can be used to find user's old data
* and replace with new data.
*
* if type = 2, username has been modified, so username
* can't be used to find user's old data
* since the program won't be able to find
* person in the file by new username. Thus,
* the program will use email to find that
* person instead of username.
*
*************************************************************/
void addPersonalData(PERSON_T* pPerson, int type)
{
PERSON_T** data = NULL; /* array of pointer PERSON_T structure */
/* to store each user's data */
FILE* pDatabase = NULL; /* File pointer - will be used to access file */
int personCount = 0; /* to get number of people in the file */
/* get the number of people in the file */
personCount = readToCount();
data = calloc(personCount,sizeof(PERSON_T*));
if (data == NULL)
{
printf("\n\tMemory allocation failure - exiting!\n");
exit(0);
}
/* read all user's data */
readData(data);
pDatabase = fopen(FILENAME,"wb");
if (pDatabase == NULL)
{
printf("\n\tError occurred while opening the file!\n");
exit(1);
}
/* write number of people in the file */
if (fwrite(&personCount,sizeof(int),1,pDatabase) != 1)
{
printf("\n\tError occurred while writing number of person to the file!\n");
exit(4);
}
if (fclose(pDatabase))
{
printf("\n\tError occurred while closing the file!\n");
exit(3);
}
/* if username hasn't been modified */
if (type == 1)
{
addByUsername(data,pPerson,personCount);
}
else
{
/* username has been modified */
/* email will be used to find person instead */
addByEmail(data,pPerson,personCount);
}
free(data);
printf("\n\tSuccessfully updated the information\n\n");
}