-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp_files.c
1356 lines (1198 loc) · 45.4 KB
/
p_files.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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* General Includes */
#include <dirent.h>
#include <errno.h>
#include <getopt.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* Windows includes */
#ifdef _WIN32
#endif
/* Local Includes */
#include "p_files.h"
#include "opp_files.h"
#include "tournament_files.h"
/* The number of bytes used to store an Entry in a player file.
* [short | opp_id]
* [3 doubles | glicko data]
* [4 chars | 2 game counts, day, and month]
* [3 shorts | year, tournament_id, and season_id] */
long int SIZE_OF_AN_ENTRY = (1 * sizeof(short)) + (3 * sizeof(double)) \
+ (4 * sizeof(char)) + (3 * sizeof(short));
/** Takes an Entry '*E' that contains the raw data read from a player file
* and fills out any fields that are not filled simply by reading bytes
* into variables (such as the 'is_competitor' field of the Entry struct
* which is represent as 1 bit in the 'day' field data in the file).
*
* \param '*E' an Entry that has been populated from a recent raw reading of a
* player file entry.
* \return void.
*/
void p_file_process_read_entry(Entry *E) {
/* If 'day' bitwise ANDed with 10000000 != 0, then this is a
* non-competitor entry */
if ( (E->day & (1 << ((sizeof(E->day) * 8) - 1))) != 0) {
E->is_competitor = 0;
/* Set leftmost bit to 0 so it becomes a normal char */
E->day = E->day & ~(1 << ((sizeof(E->day) * 8) - 1));
} else {
E->is_competitor = 1;
}
}
/** Initializes a new, fresh player file at '*file_path' with the info contained
* in the struct entry '*E'.
*
* \param '*E' a struct entry containing the len_name, and name, of the player
* we want to initialize a player file for.
* \param '*file_path' a file path to where we want the player file.
* \return 0 upon success, a negative integer upon failure.
*/
int p_file_initialize(Entry *E, const char *file_path) {
/* Open file for writing */
/* FILE *p_file = fopen(file_path, "wb+"); */
FILE *p_file = fopen(file_path, "w+b");
if (p_file == NULL) {
fprintf(stderr, "ERROR: p_file_initialize(): fopen(\"%s\"): %s\n", \
file_path, strerror(errno));
return -1;
}
int len_name = strlen(E->name);
if (1 != fwrite(&len_name, sizeof(char), 1, p_file)) {
fprintf(stderr, "ERROR: p_file_initialize(): fwrite(\"%s\"): %s\n", \
file_path, strerror(errno));
return -2;
}
if (strlen(E->name)
!= fwrite(E->name, sizeof(char), strlen(E->name), p_file)) {
fprintf(stderr, "Error initializing a player file \"%s\"" \
"(p_file_initialize): ", file_path);
perror("");
return -3;
}
/* Write the number of outcomes and tournaments attended this player has.
* Since we initializing the player file, both values must be 0 */
unsigned long lzero = 0;
if (1 != fwrite(&lzero, sizeof(long), 1, p_file)) {
fprintf(stderr, "Error initializing a player file \"%s\"" \
"(p_file_initialize): ", file_path);
perror("");
return -4;
}
if (1 != fwrite(&lzero, sizeof(long), 1, p_file)) {
fprintf(stderr, "Error initializing a player file \"%s\"" \
"(p_file_initialize): ", file_path);
perror("");
return -5;
}
fclose(p_file);
return 0;
}
/** Reads contents of a player file to a struct entry. Returns 0 upon success,
* and a negative number upon failure. Function expects that starter data
* has already been passed and that the FILE is on an entry.
*
* \param '*f' the file being read.
* \param '*E' the struct entry which will have the read entry's contents
* copied to.
* \return 0 upon success, or a negative number upon failure.
*/
int p_file_open_read_entry(const char *data_dir_file_path, FILE *f, \
struct entry *E) {
// Read opponent name id
if (1 != fread(&E->opp_id, sizeof(short), 1, f)) { return -1; }
if (1 != fread(&E->rating, sizeof(double), 1, f)) { return -2; }
if (1 != fread(&E->RD, sizeof(double), 1, f)) { return -3; }
if (1 != fread(&E->vol, sizeof(double), 1, f)) { return -4; }
if (1 != fread(&E->gc, sizeof(char), 1, f)) { return -5; }
if (1 != fread(&E->opp_gc, sizeof(char), 1, f)) { return -6; }
if (1 != fread(&E->day, sizeof(char), 1, f)) { return -7; }
if (1 != fread(&E->month, sizeof(char), 1, f)) { return -8; }
if (1 != fread(&E->year, sizeof(short), 1, f)) { return -9; }
if (1 != fread(&E->tournament_id, sizeof(short), 1, f)) { return -10; }
if (1 != fread(&E->season_id, sizeof(short), 1, f)) { return -11; }
/* Finalize the reading of the entry to ensure fields like the
* 'is_competitor' field are set properly */
p_file_process_read_entry(E);
/* Sets opp_name and len_opp_name of E to be according to opponent
* name E->opp_id */
int r;
if (0 != (r = opp_file_get_name_from_id(data_dir_file_path, E))) {
fprintf(stderr, "Error (%d) on opp_file_get_name_from_id() " \
"searching for id (%d)\n", r, E->opp_id);
return -12;
}
/* Sets t_name and len_t_name of E to be according to tournament
* name E->tournament_id */
if (0 != (r = t_file_get_tournament_name_from_id(data_dir_file_path, E))) {
fprintf(stderr, "Error (%d) on entry_get_tournament_name_from_id() " \
"searching for id (%d)\n", r, E->tournament_id);
return -13;
}
return 0;
}
/** Reads contents of a player file to a struct entry. Returns 0 upon success,
* and a negative number upon failure. Function expects that starter data
* has already been passed and that the FILE is on an entry
*
* \param '*f' the file being read
* \param '*E' the struct entry to store an entry found in the file too
* \param 'opp_id' the opp_id being searched for. The function will only
* read an entry that has the same opp_id as the one provided.
* \return 0 upon success, or a negative number upon failure.
*/
int p_file_open_read_next_opp_entry(const char *data_dir_file_path, FILE *f, \
struct entry *E, short opp_id) {
/* Set to starter data. No opp_id will ever be negative, just starts
* the loop */
if (1 != fread(&E->opp_id, sizeof(short), 1, f)) return -1;
while (E->opp_id != opp_id) {
/* If the entry isn't for the opp being searched for, skip to next
* entry */
if (0 != fseek(f, SIZE_OF_AN_ENTRY - sizeof(short), SEEK_CUR)) {
return -2;
}
if (1 != fread(&E->opp_id, sizeof(short), 1, f)) return -1;
}
if (1 != fread(&E->rating, sizeof(double), 1, f)) { return -3; }
if (1 != fread(&E->RD, sizeof(double), 1, f)) { return -4; }
if (1 != fread(&E->vol, sizeof(double), 1, f)) { return -5; }
if (1 != fread(&E->gc, sizeof(char), 1, f)) { return -6; }
if (1 != fread(&E->opp_gc, sizeof(char), 1, f)) { return -7; }
if (1 != fread(&E->day, sizeof(char), 1, f)) { return -8; }
if (1 != fread(&E->month, sizeof(char), 1, f)) { return -9; }
if (1 != fread(&E->year, sizeof(short), 1, f)) { return -10; }
if (1 != fread(&E->tournament_id, sizeof(short), 1, f)) { return -11; }
if (1 != fread(&E->season_id, sizeof(short), 1, f)) { return -12; }
/* Finalize the reading of the entry to ensure fields like the
* 'is_competitor' field are set properly */
p_file_process_read_entry(E);
/* Sets opp_name and len_opp_name of E to be according to opponent
* name E->opp_id */
int r;
if (0 != (r = opp_file_get_name_from_id(data_dir_file_path, E))) {
perror("opp_file_get_name_from_id (read_entry)");
return -12;
}
/* Sets t_name and len_t_name of E to be according to tournament
* name E->tournament_id */
if (0 != (r = t_file_get_tournament_name_from_id(data_dir_file_path, E))) {
fprintf(stderr, "Error: t_file_get_tournament_name_from_id (%d)", r);
return -13;
}
return 0;
}
/** Reads contents of a player file to a struct entry. Returns 0 upon success,
* and a negative number upon failure.Function expects that starter data has
* already been passed and that the FILE is on an entry
*
* Note: this function doesn't read the entire entry into the struct entry. It
* is the minimal version and reads only the glicko2 data plus the season id.
*
* \param '*f' the file being read
* \param '*E' the struct entry to store an entry found in the file too
* \return 0 upon success, or a negative number upon failure.
*/
int p_file_open_read_entry_minimal(FILE *f, struct entry *E) {
/* Skip over opp id */
if (0 != fseek(f, sizeof(short), SEEK_CUR)) return -1;
/* Read Glicko2 data */
if (1 != fread(&E->rating, sizeof(double), 1, f)) return -3;
if (1 != fread(&E->RD, sizeof(double), 1, f)) return -4;
if (1 != fread(&E->vol, sizeof(double), 1, f)) return -5;
/* Skip over game counts, date, and tournament id */
if (0 != fseek(f, sizeof(char) * 4 + sizeof(short) * 2 , SEEK_CUR)) {
return -1;
}
/* Read season id */
if (1 != fread(&E->season_id, sizeof(short), 1, f)) return -12;
return 0;
}
/** Reads contents of a player file to a struct entry. Returns 0 upon success,
* and a negative number upon failure.Function expects that starter data has
* already been passed and that the FILE is on an entry
*
* Note: this function doesn't read the entire entry into the
* struct entry. It is the absent version and reads only the glicko2 data,
* the date data, the tournament and season id.
*
* \param '*f' the file being read
* \param '*E' the struct entry to store an entry found in the file too
* \return 0 upon success, or a negative number upon failure.
*/
int p_file_open_read_entry_absent(const char *data_dir_file_path, FILE *f, \
struct entry *E) {
/* Skip over opp id */
if (0 != fseek(f, sizeof(short), SEEK_CUR)) { return -1; } //2
/* Read glicko2 data */
if (1 != fread(&E->rating, sizeof(double), 1, f)) { return -3; } //8 10
if (1 != fread(&E->RD, sizeof(double), 1, f)) { return -4; } // 8 18
if (1 != fread(&E->vol, sizeof(double), 1, f)) { return -5; } //8 26
/* Skip over game counts */
if (0 != fseek(f, sizeof(char) * 2, SEEK_CUR)) { return -1; } //2
/* Read date data */
if (1 != fread(&E->day, sizeof(char), 1, f)) { return -8; } //1 29
if (1 != fread(&E->month, sizeof(char), 1, f)) { return -9; } //1 30
if (1 != fread(&E->year, sizeof(short), 1, f)) { return -10; } //2 32
if (1 != fread(&E->tournament_id, sizeof(short), 1, f)) { return -11; } //2 32
if (1 != fread(&E->season_id, sizeof(short), 1, f)) { return -12; } //2 34
/* Finalize the reading of the entry to ensure fields like the
* 'is_competitor' field are set properly */
p_file_process_read_entry(E);
/* Sets t_name and len_t_name of E to be according to tournament
* name E->tournament_id */
int r;
if (0 != (r = t_file_get_tournament_name_from_id(data_dir_file_path, E))) {
fprintf(stderr, "Error: t_file_get_tournament_name_from_id (%d)", r);
return -13;
}
return 0;
}
/** Reads contents of a player file to a struct entry. Returns 0 upon success,
* and a negative number upon failure. Function expects that starter data
* has already been passed and that the FILE is on an entry
*
* \param '*f' the file being read
* \param '*E' the struct entry to store an entry found in the file too
* \return 0 upon success, or a negative number upon failure.
*/
int p_file_open_read_entry_tournament_id(FILE *f, struct entry *E) {
/* Skip all entry data up until tournament id */
if (0 != fseek(f, \
sizeof(short) + \
sizeof(double) + \
sizeof(double) + \
sizeof(double) + \
sizeof(char) + \
sizeof(char) + \
sizeof(char) + \
sizeof(char) + \
sizeof(short), SEEK_CUR)) { return -1; }
/* Read tournament id and seek to the end of the entry */
if (1 != fread(&E->tournament_id, sizeof(short), 1, f)) { return -2; }
if (0 != fseek(f, sizeof(short), SEEK_CUR)) { return -3; }
return 0;
}
/** Reads all the starter data in a player file leaving the FILE '*f' at
* a position where it can start reading entries.
*
* \param '*f' a player file opened in 'rb' mode.
* \return a negative int on failure, 0 upon success.
*/
int p_file_open_position_at_start_of_entries(FILE *f) {
char ln;
if (1 != fread(&ln, sizeof(char), 1, f)) return -1;
/* Skip past name and number of outcomes/tournaments attended */
long name_and_counts = ln * sizeof(char) + 2 * sizeof(long);
if (0 != fseek(f, name_and_counts, SEEK_CUR)) return -2;
return 0;
}
/** Reads all the starter data in a player file and seeks such the FILE
* '*f' is at a position where it can start appending a new entry.
*
* \param '*f' a player file opened in 'rb' mode.
* \return a negative int on failure, 0 upon success.
*/
int p_file_open_position_for_appending_entry(FILE *f) {
if (0 != fseek(f, 0, SEEK_END)) return -1;
return 0;
}
/** Reads the all entries in a player file, returns the number of unique
* opponents within, and alters '**ret_opp_id_list' to pointer to an
* array of shorts containing all the unique opponent ids.
*
* \param '*file_path' file path to a player file.
* \param '**ret_opp_id_list' pointer to a short pointer. The short pointer
* will be updated to memory address of a opp_id_list created in this
* function. It must be freed after.
* \return a long representing the number of unique opponents in this player
* file.
*/
long p_file_number_of_opponents(const char *data_dir_file_path, \
const char *file_path, short **ret_opp_id_list) {
int ret = 0;
FILE *p_file = fopen(file_path, "rb");
if (p_file == NULL) {
perror("fopen (p_file_number_of_opponents)");
ret = -1;
}
long opp_id_list_size = 64;
short *opp_id_list = (short *)malloc(sizeof(short) * opp_id_list_size);
short num_opp_ids = 0;
struct entry E;
p_file_open_position_at_start_of_entries(p_file);
/* While the function is still able to read entries from the old file */
while (0 == p_file_open_read_entry(data_dir_file_path, p_file, &E)) {
char already_in = 0;
int i = 0;
for (i = 0; i < num_opp_ids; i++) {
if (E.opp_id == opp_id_list[i]) {
already_in = 1;
break;
}
}
if (already_in == 0) {
/* If there is no space to add this opponent, reallocate */
if (num_opp_ids + 1 > opp_id_list_size) {
opp_id_list_size *= 2;
opp_id_list = (short *)\
realloc(opp_id_list, sizeof(short) * opp_id_list_size);
if (opp_id_list == NULL) {
perror("realloc (p_file_number_of_opponents)");
return 0;
}
}
opp_id_list[num_opp_ids] = E.opp_id;
num_opp_ids++;
}
}
fclose(p_file);
*ret_opp_id_list = opp_id_list;
/* If there were no errors, return the number of opponents */
if (ret == 0) ret = num_opp_ids;
return ret;
}
/** Takes a file path to a player file and returns the number of
* events this player has attended that weren't RD adjustments.
*
* \param '*file_path' the player file to read
* \return upon success, a positive integer representing the number
* of opponents this player has played.
*/
long p_file_number_of_events(const char *file_path) {
FILE *p_file = fopen(file_path, "rb");
if (p_file == NULL) {
perror("fopen (p_file_number_of_events)");
return -1;
}
unsigned long num_events = 0;
char ln;
if (1 != fread(&ln, sizeof(char), 1, p_file)) return -2;
/* Skip over player name and number of number of valid outcomes */
if (0 != fseek(p_file, ln + sizeof(long), SEEK_CUR)) { return -3; }
if (1 != fread(&num_events, sizeof(long), 1, p_file)) return -4;
fclose(p_file);
return num_events;
}
/** Reads a player file at the given file path and returns the number
* of entries contained in that file.
*
* \param '*file_path' the file path of the file to be read.
* \return 0 upon success, or a negative number upon failure.
*/
int p_file_get_number_of_entries(const char *data_dir_file_path, \
const char *file_path) {
FILE *base_file = fopen(file_path, "rb");
if (base_file == NULL) {
perror("fopen (p_file_get_number_of_entries)");
return -1;
}
// TODO: just do math, offset at start of entries, offset at end of file,
// substract and divide?
int entries = 0;
/* Read entry from old file */
struct entry *cur_entry = (struct entry *)malloc(sizeof(struct entry));
p_file_open_position_at_start_of_entries(base_file);
/* While the function is still able to read entries from the old file */
while (0 == p_file_open_read_entry(data_dir_file_path, base_file, cur_entry)) {
entries++;
}
free(cur_entry);
fclose(base_file);
return entries;
}
/** Reads a player file at the given file path and returns the number
* of entries contained in that file where the opponent is '*player2'.
*
* \param '*file_path' the file path of the file to be read.
* \param '*player2' the file name (not path) of a player file
* \return 0 upon success, or a negative number upon failure.
*/
long p_file_get_number_of_outcomes_against(const char *data_dir_file_path, \
const char *file_path, char *player2) {
FILE *base_file = fopen(file_path, "rb");
if (base_file == NULL) {
perror("fopen (p_file_get_number_of_outcomes_against)");
return -1;
}
int entries = 0;
/* Read entry from old file */
struct entry *cur_entry = (struct entry *)malloc(sizeof(struct entry));
p_file_open_position_at_start_of_entries(base_file);
/* While the function is still able to read entries from the old file */
while (0 == p_file_open_read_entry(data_dir_file_path, base_file, cur_entry)) {
if (0 == strcmp(cur_entry->opp_name, player2)) {
entries++;
}
}
free(cur_entry);
fclose(base_file);
return entries;
}
/** Reads a player file at the given file path and returns an array of longs
* that represent the number of times player1 has played that player in an
* event. The array follows the order of '*opp_id_list', where '*opp_id_list'
* is an unordered list of all the unique opp_ids in this player file.
*
* NOTE: the return value is calloc'd and as such, free() must be called
* on it once it is no longer being used.
*
* \param '*file_path' the file path of the file to be read.
* \param 'num_opp_ids' the number of unique opponents in this file
* \param '*opp_id_list' an array of shorts representing every unique
* opp_id in this player file.
* \return NULL upon failure, an array of longs (pointer) upon success.
*/
long *p_file_get_all_number_of_outcomes_against( \
const char *data_dir_file_path, const char *file_path, \
long num_opp_ids, short *opp_id_list) {
FILE *base_file = fopen(file_path, "rb");
if (base_file == NULL) {
perror("fopen (p_file_get_all_number_of_outcomes_against)");
return NULL;
}
long *outcomes = (long *)calloc(num_opp_ids, sizeof(long));
/* Read entry from old file */
struct entry E;
p_file_open_position_at_start_of_entries(base_file);
/* While the function is still able to read entries from the old file */
while (0 == p_file_open_read_entry(data_dir_file_path, base_file, &E)) {
int i = 0;
for (i = 0; i < num_opp_ids; i++) {
if (E.opp_id == opp_id_list[i]) {
break;
}
}
outcomes[i] += 1;
}
fclose(base_file);
return outcomes;
}
/** Returns the offset within a player file at which the last entry begins.
*
* \param '*file_path' a string of the file path of a player file for the
* function to find the offset of the last entry
* \return a long representing the offset within the file at which the last
* entry begins
*/
long p_file_get_last_entry_offset(const char *file_path) {
FILE *p_file = fopen(file_path, "rb");
if (p_file == NULL) {
perror("fopen (p_file_get_last_entry_offset)");
return 0;
}
/* Read to the end of the starter data in the file */
fseek(p_file, 0, SEEK_SET);
int ret = p_file_open_position_at_start_of_entries(p_file);
if (ret != 0) {
fprintf(stderr, \
"p_file_open_position_at_start_of_entries (p_file_get_last_entry_offset) returned %d", \
ret);
}
fseek(p_file, 0, SEEK_END);
long int last_entry_offset = ftell(p_file) - SIZE_OF_AN_ENTRY;
fclose(p_file);
return last_entry_offset;
}
/** Modifies a struct entry to be that of the last entry found in a player
* file.
*
* \param '*file_path' the file path of the player file to be read.
* \param '*ret' a struct entry pointer to have the data read into.
* \return 0 upon success, a negative int upon failure.
*/
int p_file_read_last_entry(const char *data_dir_file_path, \
const char *file_path, struct entry *ret) {
/* Open files for reading contents */
FILE *p_file = fopen(file_path, "rb");
if (p_file == NULL) {
perror("fopen (p_file_read_last_entry)");
/* If the file could not be read for any reason, return accordingly */
return -1;
}
/* Read the player's name from the file */
if (0 != p_file_open_read_start_from_file(p_file, ret)) {
perror("p_file_open_read_start_from_file (p_file_read_last_entry)");
return -2;
}
fseek(p_file, -SIZE_OF_AN_ENTRY, SEEK_END);
/* If reading the last entry failed */
int r = 0;
if (0 != (r = p_file_open_read_entry(data_dir_file_path, p_file, ret))) {
fprintf(stderr, "Error (%d) on p_file_open_read_entry()\n", r);
return -3;
}
fclose(p_file);
return 0;
}
/** Modifies a struct entry to be that of the last entry found in a player
* file. Note that this function doesn't read the entire entry into the
* struct entry. It is the minimal version and reads only the glicko2 data
* plus the season id.
*
* \param '*file_path' the file path of the player file to be read.
* \param '*ret' a struct entry pointer to have the data read into.
* \return 0 upon success, a negative int upon failure.
*/
int p_file_read_last_entry_minimal(const char *file_path, struct entry *ret) {
/* Open files for reading contents */
FILE *p_file = fopen(file_path, "rb");
if (p_file == NULL) {
perror("fopen (p_file_read_last_entry)");
/* If the file could not be read for any reason, return accordingly */
return -1;
}
fseek(p_file, -SIZE_OF_AN_ENTRY, SEEK_END);
/* If reading the last entry failed */
if (0 != p_file_open_read_entry_minimal(p_file, ret)) return -1;
fclose(p_file);
return 0;
}
/** Modifies a struct entry to be that of the last entry found in a player
* file. Note that this function doesn't read the entire entry into the
* struct entry. It is the absentee version and reads only the glicko2 data,
* the date, the tournament and season id.
*
* \param '*file_path' the file path of the player file to be read.
* \param '*ret' a struct entry pointer to have the data read into.
* \return 0 upon success, a negative int upon failure.
*/
int p_file_read_last_entry_absent(const char *data_dir_file_path, \
const char *file_path, struct entry *ret) {
/* Open files for reading contents */
FILE *p_file = fopen(file_path, "rb");
if (p_file == NULL) {
perror("fopen (p_file_read_last_entry)");
/* If the file could not be read for any reason, return accordingly */
return -1;
}
fseek(p_file, -SIZE_OF_AN_ENTRY, SEEK_END);
/* If reading the last entry failed */
if (0 != p_file_open_read_entry_absent(data_dir_file_path, p_file, ret)) {
return -1;
}
fclose(p_file);
return 0;
}
/** Modifies a struct entry to be that of the last entry found in a player
* file. Note that this function doesn't read the entire entry into the
* struct entry. It is the tournament_id version and reads only the
* tournament id.
*
* \param '*file_path' the file path of the player file to be read.
* \param '*ret' a struct entry pointer to have the data read into.
* \return 0 upon success, a negative int upon failure.
*/
int p_file_read_last_entry_tournament_id(const char *file_path, \
struct entry *ret) {
/* Open files for reading contents */
FILE *p_file = fopen(file_path, "rb");
if (p_file == NULL) {
perror("fopen (p_file_read_last_entry)");
/* If the file could not be read for any reason, return accordingly */
return -1;
}
fseek(p_file, -SIZE_OF_AN_ENTRY, SEEK_END);
/* If reading the last entry failed */
if (0 != p_file_open_read_entry_tournament_id(p_file, ret)) return -1;
fclose(p_file);
return 0;
}
/** Modifies a struct entry to be that of the last entry found in an open
* player file. Note that this function doesn't read the entire entry into the
* struct entry. It is the tournament_id version and reads only the
* tournament id.
*
* \param '*f' the open player file to be read.
* \param '*ret' a struct entry pointer to have the data read into.
* \return 0 upon success, a negative int upon failure.
*/
int p_file_open_read_last_entry_tournament_id(FILE *f, struct entry *ret) {
fseek(f, -SIZE_OF_AN_ENTRY, SEEK_END);
/* If reading the last entry failed */
if (0 != p_file_open_read_entry_tournament_id(f, ret)) return -1;
return 0;
}
/** Appends an RD adjustment entry to a given player file and return an int
* representing whether the function succeeded or not.
*
* NOTE: This is the 'id' version of this function, and it will expect
* that E->opp_id, and E->tournament_id have been correctly set.
*
* \param '*E' the struct entry to be appended.
* \param '*file_path' the file path of the player file.
* \return int that is 0 upon the function succeeding and negative upon
* any sort of failure.
*/
int p_file_append_adjustment_to_file_id(struct entry *E, const char *file_path) {
/* File guaranteed to exist as it was found by reading player
* directory contents */
/* Open file for appending */
FILE *p_file = fopen(file_path, "ab+");
if (p_file == NULL) {
fprintf(stderr, "Error opening file %s (p_file_append_adjustment_to_file_id): ", file_path);
perror("");
return -10;
}
/* Write length of opp name and opp name */
if (1 != fwrite(&E->opp_id, sizeof(short), 1, p_file)) { return -9; }
/* Write glicko data */
if (1 != fwrite(&E->rating, sizeof(double), 1, p_file)) { return -10; }
if (1 != fwrite(&E->RD, sizeof(double), 1, p_file)) { return -11; }
if (1 != fwrite(&E->vol, sizeof(double), 1, p_file)) { return -12; }
/* Write game counts */
if (1 != fwrite(&E->gc, sizeof(char), 1, p_file)) { return -13; }
if (1 != fwrite(&E->opp_gc, sizeof(char), 1, p_file)) { return -14; }
/* Write date data */
if (1 != fwrite(&E->day, sizeof(char), 1, p_file)) { return -15; }
if (1 != fwrite(&E->month, sizeof(char), 1, p_file)) { return -16; }
if (1 != fwrite(&E->year, sizeof(short), 1, p_file)) { return -17; }
if (1 != fwrite(&E->tournament_id, sizeof(short), 1, p_file)) { return -18; }
if (1 != fwrite(&E->season_id, sizeof(short), 1, p_file)) { return -19; }
fclose(p_file);
/* Guaranteed not to be a competitor entry */
return 0;
}
/** Appends an entry to a given player file and return an int representing
* whether the function succeeded or not.
*
* NOTE: This is the 'id' version of this function, and it will expect
* that E->opp_id, and E->tournament_id have been correctly set.
*
* \param '*E' the struct entry to be appended.
* \param '*file_path' the file path of the player file.
* \return int that is 0 upon the function succeeding and negative upon
* any sort of failure.
*/
int p_file_append_entry_to_file_id(struct entry *E, const char *file_path) {
/* If the file did not exist */
#ifdef __linux__
/* Check for read and write access */
int access_ret = access(file_path, R_OK | W_OK);
/* If there was an error with the _access() call */
if (access_ret == -1) {
if (errno == ENOENT) {
/* The file simply does not exist. We will handle this a couple
* lines further down, for now just don't exit like you would
* with other errors */
} else if (errno == EACCES) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"access(): File access denied: \"%s\"\n", file_path);
return -1;
} else if (errno == EINVAL) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"access(): Invalid parameter: \"%s\"\n", file_path);
return -1;
}
}
char pfile_exists = (access_ret == 0);
#elif _WIN32
/* Check for read and write access */
int access = _access(file_path, 06);
/* If there was an error with the _access() call */
if (access == -1) {
if (errno == EACCES) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"_access(): File access denied: \"%s\"\n", file_path);
return -1;
} else if (errno == EINVAL) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"_access(): Invalid parameter: \"%s\"\n", file_path);
return -1;
}
}
char pfile_exists = (access == 0);
#else
/* Check for read and write access */
int access_ret = access(file_path, R_OK | W_OK);
/* If there was an error with the _access() call */
if (access_ret == -1) {
if (errno == ENOENT) {
/* The file simply does not exist. We will handle this a couple
* lines further down, for now just don't exit like you would
* with other errors */
} else if (errno == EACCES) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"access(): File access denied: \"%s\"\n", file_path);
return -1;
} else if (errno == EINVAL) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"access(): Invalid parameter: \"%s\"\n", file_path);
return -1;
}
}
char pfile_exists = (access_ret == 0);
#endif
/* If the player file did not exist, create a new one */
if (!pfile_exists) {
if (0 != p_file_initialize(E, file_path)) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"Failed to initialize player: \"%s\"\n", file_path);
}
}
/* Entry that is used later to check if this entry is at a new tournament */
struct entry E2;
/* FILE *p_file = fopen(file_path, "ab+"); */
FILE *p_file = fopen(file_path, "a+b");
if (p_file == NULL) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"Could not open file to append new entry: \"%s\":", file_path);
perror("");
return -6;
}
unsigned long out_count = p_file_open_get_outcome_count(p_file);
if (0 != out_count) {
if (0 != p_file_open_read_last_entry_tournament_id(p_file, &E2)) {
return -7;
}
}
/* After getting what this function needed from the starter data,
* reposition the cursor so that the next write will append an entry */
if (0 != p_file_open_position_for_appending_entry(p_file)) {
return -8;
}
/* Write length of opp name and opp name */
if (1 != fwrite(&E->opp_id, sizeof(short), 1, p_file)) { return -8; }
/* Write glicko data */
size_t return_val_thing;
if (sizeof(double) != (return_val_thing = fwrite(&(E->rating), 1, sizeof(double), p_file))) {
return -9;
}
if (1 != fwrite(&E->RD, sizeof(double), 1, p_file)) {return -10; }
if (1 != fwrite(&E->vol, sizeof(double), 1, p_file)) { return -11; }
/* Write game counts */
if (1 != fwrite(&E->gc, sizeof(char), 1, p_file)) { return -12; }
if (1 != fwrite(&E->opp_gc, sizeof(char), 1, p_file)) { return -13; }
/* Write date data */
if (1 != fwrite(&E->day, sizeof(char), 1, p_file)) { return -14; }
if (1 != fwrite(&E->month, sizeof(char), 1, p_file)) { return -15; }
if (1 != fwrite(&E->year, sizeof(short), 1, p_file)) { return -16; }
if (1 != fwrite(&E->tournament_id, sizeof(short), 1, p_file)) {
return -17;
}
if (1 != fwrite(&E->season_id, sizeof(short), 1, p_file)) { return -18; }
fclose(p_file);
// TODO: make this its own function
/* If this entry is a competitor entry */
if ( (E->day & (1 << ((sizeof(E->day) * 8) - 1))) == 0) {
/* Update the number of outcome data in the player file */
FILE *p_file2 = fopen(file_path, "rb+");
if (p_file2 == NULL) {
fprintf(stderr, "Error opening file \"%s\" and updating " \
"number_of info (p_file_append_entry_to_file_id): ", \
file_path);
perror("");
return -19;
}
/* Read the starter data in the file */
char ln;
if (1 != fread(&ln, sizeof(char), 1, p_file2)) {
return -20;
}
/* Move to point in file where number of outcomes resides */
if (0 != fseek(p_file2, ln * sizeof(char), SEEK_CUR)) {
return -21;
}
unsigned long number_of_outcomes;
unsigned long number_of_tournaments_attended;
if (1 != fread(&number_of_outcomes, sizeof(long), 1, p_file2)) {
return -22;
}
if (1 != fread(&number_of_tournaments_attended, sizeof(long), 1, p_file2)) {
return -23;
}
number_of_outcomes++;
/* Move back to the point in file where number of outcomes resides */
if (0 != fseek(p_file2, -1 * 2 * ((long int) sizeof(long)), SEEK_CUR)) {
return -24;
}
if (1 != fwrite(&number_of_outcomes, sizeof(long), 1, p_file2)) {
return -25;
}
/* Only increase number of tournaments attended if this outcome is
* at a new tournament */
if (E2.tournament_id != E->tournament_id || \
0 == out_count) {
number_of_tournaments_attended++;
if (1 != fwrite(&number_of_tournaments_attended, sizeof(long), 1, p_file2)) {
return -26;
}
}
fclose(p_file2);
}
return 0;
}
/** Appends an entry to a given player file and return an int representing
* whether the function succeeded or not.
*
* \param '*E' the struct entry to be appended.
* \param '*file_path' the file path of the player file.
* \return int that is 0 upon the function succeeding and negative upon
* any sort of failure.
*/
int p_file_append_entry_to_file(struct entry *E, \
const char *data_dir_file_path, const char *file_path) {
/* If the file did not exist */
#ifdef __linux__
char pfile_exists = access(file_path, R_OK) != -1;
#elif _WIN32
char pfile_exists = access(file_path, 0) != -1;
#else
char pfile_exists = access(file_path, R_OK) != -1;
#endif
/* If the player file did not exist, create a new one */
if (!pfile_exists) {
if (0 != p_file_initialize(E, file_path)) {
fprintf(stderr, "ERROR: p_file_append_entry_to_file_id(): " \
"Failed to initialize player file: \"%s\"\n", file_path);
}
}
/* Entry that is used later to check if this entry is at a new tournament */
struct entry E2;
unsigned long out_count = p_file_get_outcome_count(file_path);
if (0 != out_count) {
if (0 != p_file_read_last_entry_tournament_id(file_path, &E2)) return -6;
}
int ret;
/* If the opponent file does not already contain an id for this opponent */
if (-1 == (ret = opp_file_contains_opponent(data_dir_file_path, E->opp_name))) {
/* Add the new opponent to the opponent file. This also corrects
* the opp_id if it is incorrect */
if (0 != (ret = opp_file_add_new_opponent(data_dir_file_path, E))) {
fprintf(stderr, "Error (%d) on opp_file_add_new_opponent(E, %s)\n", ret, file_path);
return -7;
}
/* If there was an error */
} else if (ret < -1) {
fprintf(stderr, "Error (%d) on opp_file_contains_opponent(%s, %s)\n", ret, E->opp_name, file_path);
return -8;
/* If the opponent file does contain an id for this opponent */
} else {
/* Fix the opp_id in case it wasn't set */
E->opp_id = (unsigned short) ret;
}
/* If the tournament file does not already contain an id for this tournament */
if (-1 == (ret = t_file_contains_tournament(data_dir_file_path, E->t_name))) {
/* Add the new tournament to the tournament file. This also corrects
* the t_id if it is incorrect */
if (0 != t_file_add_new_tournament(data_dir_file_path, E)) return -9;
/* If there was an error */
} else if (ret < -1) {
return -10;
/* If the tournament file does contain an id for this tournament */