-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
3807 lines (3297 loc) · 134 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
#include <stdbool.h>
#include <math.h>
#include <ctype.h>
#include "struct_init.h"
#include "struct_file.h"
#include "cs_tools.h"
#define _POSIX_C_SOURCE 200809L // or another appropriate value
// Constants
#define ZERO_CHAR "0"
#define ZERO_INT 0
#define ZERO 0
#define ONE_INT 1
#define FIVE_INT 5
#define SIX_INT 6
#define SEVEN_INT 7
#define ZERO_DOUBLE 0.0
#define NONE "none"
#define TRUE_CHAR "true"
#define FALSE_CHAR "false"
/* CORE FUNTIONS */
void get_date();
void linux_path();
void start_up();
void check_for_count();
void main_menu();
void available_options(int options);
void main_options(int selection_int);
void selection_dialogs(int dialog);
void create_course();
void setup_data();
void produce_course();
void update_counts();
void course_listpath();
void add_course_tolist(const char *file_path, const char *file_pathtwo, const char *course_filepath);
void select_course();
void study_course();
void calculate_learning();
void add_repetition();
void spacing_multiplier();
void interval_time();
void engram_stability();
void engram_retrievability();
void process_date();
void save_progress();
void change_topic_questions();
void study_incrementer();
void study_decrementer();
void clear_lists();
void study_lines();
void study_dates();
void study_false();
void study_true();
void study_not_done();
void study_not_zero();
void study_hud();
/* PREDICTION FUNCTIONS */
void predict_main();
void avg_diff();
void collect_first_studies();
int add_firsts_collect(int topic_number, int index);
void y_max_firsts();
void y_max_sort_firsts();
void find_high_y();
void prepare_past_studies();
void initialize_one_past_study(int index, int repetition);
int past_study_initialization(int index, int repetition);
void simulate_past_studies();
void x_max_repeats();
bool sort_xrep_counts(bool repsort_correct);
void x_max_to_sort();
void add_to_xmax();
void x_max_count_reps();
void x_max_repeat_sort();
bool x_insert_sort(bool xsort_correct);
void collect_non_studied();
int non_study_getter(int topicNumber, int loopIndex);
void generate_projected_studies();
void prediction_lists_clear();
void predict_studies();
void collect_study_x();
void gensims_all_getter(int index);
void find_y_at_x();
void rep_setter();
void goal_setter();
void collect_study_y();
void y_to_gen_sims_all(int index);
void reduce_new();
void get_reduced_new(int count);
void get_reduced_projected(int count);
void sim_calculate_learning();
void sim_interval_time();
void sim_add_repetition();
void sim_process_date();
/* WEEKLY SCHEDULE FUNCTIONS */
void check_for_week();
bool skip_checker(date_time *simDate);
void weekly_schedule();
void weekly_dialog();
void toggle_day_function(int dayOption);
/* FILE PATHS FUNCTIONS */
void init_file_path(int choice);
void free_file_path(int choice);
// Memory Leaks:
// Once done converting, go through and collect the list of string pointers
// that I can to put into a global variable list, to decrease the number
// of memory leaks.
// All unfreed "char *var" strings are memory leaks. must free them after not being used.
// Initialize some lists
// const int ZERO = 0;
// struct st_fstudy_counts first_page;
// fstudy_counts->page = &first_page;
// ((struct st_fstudy_counts*)fstudy_counts->page)->counts = ZERO;
// Figure out if I can even use this in a separate file like I want.
struct st_file_strings
{
// comment a done after each variable once I
// have the code store that string into this
// struct. That way I can reduce the amount of
// code by checking off a list using 1 as a check.
// mark. Once all have been done, remove the check
// mark comments from here.
// These must be initialized after the
// username is obtained.
// They must be freed upon exit of the program.
// These are all full file paths.
/* In course_list_path() */
// struct choice 1 along with it's bak file
char *txt_course_listfile; //1
// Back up file made in case power outage while file being written, but doesnt complete. It can happen.
char *bak_course_listfile;
/* In check_for_count() */
// struct choice 2
char *course_countfile;
/* In check_for_week() */
// struct choice 3
char *week_file; // I use sc extension for schedule
// This one must get initialized at the same time as
// globals.course_name
// It must be freed upon exit of the course, and the program.
/* I think it is in select_course */
// struct choice 4
char *txt_course_namefile;
// Back up file made in case power outage while file being written, but doesnt complete. It can happen.
char *bak_course_namefile;
};
extern struct st_file_strings file_strings;
// 36 functions complete
/********************** Core program **********************************/
int main(void)
{
get_date();
linux_path();
start_up();
}
void get_date()
{
/*
Remember to free(globals.the_date); when you're done using it to prevent memory leaks.
*/
time_t now = time(NULL);
struct tm *local_time = localtime(&now);
globals.the_date = malloc(11 * sizeof(char)); // Allocate memory
if (globals.the_date != NULL) {
strftime(globals.the_date, 11, "%m/%d/%Y", local_time);
}
// Add error handling for malloc failure if you want
}
void linux_path()
{
const int ONE = 1;
bool skip_all = false;
// Get the username, then store it locally
char *username = getenv("USER");
if (username == NULL)
{
perror("getenv() error");
// return EXIT_FAILURE;
skip_all = true;
}
char *local_username = malloc(strlen(username) + 1);
if (skip_all != true)
{
if (local_username == NULL)
{
perror("malloc() error");
// return EXIT_FAILURE;
}
strcpy(local_username, username);
// Set up required variables, and allocate memory for the whole file name
char *filename1 = "/home/";
char *filename2 = "/Documents/GlideCLI";
char *path_string;
int len = strlen(filename1) + strlen(local_username) + strlen(filename2) + ONE;
path_string = malloc(len);
if (path_string == NULL)
{
perror("malloc() error");
free(local_username);
// return EXIT_FAILURE;
}
// Combine the parts into the filename
sprintf(path_string, "%s%s%s", filename1, local_username, filename2);
// Make the directory if it does not exist
struct stat st = {0};
if (stat(path_string, &st) == -1)
{
mkdir(path_string, 0700);
}
// Store the path into the global struct
globals.directory_path = path_string;
globals.os_switch = true;
}
free(local_username);
}
void start_up()
{
while (true)
{
study_decrementer();
check_for_count();
check_for_week();
main_menu();
study_incrementer();
clear_lists();
}
}
void check_for_count()
{
// const int ZERO = 0;
const int OPTION_TWO = 2; // this is the initialization option needed to get the file path
struct st_string *c_course_count = malloc(sizeof(struct st_string));
if (c_course_count == NULL)
{
perror("malloc() error");
exit(EXIT_FAILURE);
}
int i_course_count = ZERO;
// Initialize the file path
init_file_path(OPTION_TWO);
if (file_strings.course_countfile == NULL)
{
perror("No path for course_countfile"); // You might want to handle this error differently
free(c_course_count);
exit(EXIT_FAILURE);
}
// Try to read the text from the file
if ((c_course_count->string = cs_read_all_text(file_strings.course_countfile)) != NULL)
{
i_course_count = atoi(c_course_count->string);
globals.course_count = i_course_count;
}
else
{
// If reading fails, initialize the file and try again
cs_write_all_text(file_strings.course_countfile, "0");
free(c_course_count->string); // Ensure the previous string is deallocated
c_course_count->string = cs_read_all_text(file_strings.course_countfile);
if (c_course_count->string == NULL)
{
perror("Failed to read course_countfile after initializing it");
free(c_course_count);
exit(EXIT_FAILURE);
}
i_course_count = atoi(c_course_count->string);
globals.course_count = i_course_count;
}
// Free up what's no longer needed
free(c_course_count->string);
free(c_course_count);
}
void main_menu()
{
available_options(globals.course_count > 0);
}
void available_options(int options)
{
int selectionInt;
char *selection_string;
globals.made_select = false;
do
{
if (options == 0)
{
selection_dialogs(1);
}
else if (options == 1)
{
selection_dialogs(2);
}
selection_string = cs_read_line();
// Check that the user's input is valid
if (!isdigit(selection_string[0]))
{
printf("\nInvalid Input. Try again:\n");
continue;
}
selectionInt = atoi(selection_string);
free(selection_string);
if ((options == 0 && (selectionInt == 1 || selectionInt == 2)) ||
(options == 1 && (selectionInt >= 1 && selectionInt <= 5)))
{
main_options(selectionInt);
globals.made_select = true;
}
else
{
printf("\nInvalid Selection. Try again:\n");
}
} while (globals.made_select == false);
}
void main_options(int selection_int)
{
// Clear console and set flag for selected option for cases 1-5
if (selection_int >= 1 && selection_int <= 5)
{
cs_console_clear();
globals.made_select = true;
}
switch (selection_int)
{
case 1:
printf("\nWeekly Schedule Selected.\n");
weekly_schedule();
break;
case 2:
printf("\nCreate new course selected.\n");
create_course();
break;
case 3:
printf("\nCOURSES:\n");
study_incrementer();
clear_lists();
select_course();
study_course();
break;
case 4:
printf("Current date set at: %s", globals.the_date);
printf("\nEnter a date, in your region's formatting, to force the program to use it:");
printf("\nExample formatting provided...\n"); // You could continue your example here
globals.the_date = cs_read_line();
break;
case 5:
printf("\nGood Bye");
exit(0);
break;
default:
printf("\nInvalid option selected.\n");
globals.made_select = false;
break;
}
}
void print_line_breaks(int n) {
for (int i = 0; i < n; ++i) {
printf("\n");
}
}
void clear_and_print_date() {
cs_console_clear();
printf("\nDate: %s", globals.the_date);
}
void selection_dialogs(int dialog)
{
// FIXME: ChatGPT cut many of the options out of the switch
// const int ZERO = 0;
switch (dialog)
{
case 1:
// For AvailableOptions()
// Option 1 if no courses available
cs_console_clear();
printf("\n\n\n1: Exit the program");
printf("\n2: Create a new course\n");
printf("\n\n\n\nEnter an option from the menu: ");
break;
case 2:
// For AvailableOptions()
// Option 2 if courses are available
cs_console_clear();
printf("\nDate: %s", globals.the_date);
printf("\n\n\n1: Weekly Schedule");
printf("\n2: Create a new course");
printf("\n3: Study a course");
printf("\n4: Force GlieCLI to use a different date");
printf("\n5: Exit the program\n");
printf("\n\n\n\nSelect an option from the menu: ");
break;
case 3:
// For StudyCourse()
cs_console_clear();
printf("\n\n\n\nNothing left to study for current topic today.");
printf("\nEnter m to quit back to menu, or any other key to exit.");
globals.response = cs_read_line();
if (globals.response == "m")
{
cs_console_clear();
globals.made_select = false;
globals.new_left = ZERO;
globals.current_left = ZERO;
globals.late_left = ZERO;
return;
}
else
{
cs_console_clear();
exit(ZERO);
}
break;
case 4:
// For CreateCourse()
cs_console_clear();
printf("\n\n\n\n\n\nWhat is the name of the course? ");
globals.course_name = cs_read_line();
printf("\n\n\n\n\n\nHow many chapters are in the text book? ");
globals.course_chapters = cs_read_line();
creation_vars.chapters_int = atoi(globals.course_chapters);
break;
case 5:
// For SetupData()
printf("\n\n\n\n\n\nHow many sub-sections are in chapter %d: ", creation_vars.current_chapter); // Chapters are used here to make it easier to set the course up.
creation_vars.subsection_string = cs_read_line();
creation_vars.subsection_counter = atoi(creation_vars.subsection_string);
break;
case 6:
// For SetupData()
printf("\n\n\n\n\n\nHow many topics are in section %d.%d: ", creation_vars.current_chapter, creation_vars.current_subsection); // Chapters are used here to make it easier to set the course up.
creation_vars.topic_count_string = cs_read_line();
break;
case 7:
// For SetupData()
printf("\n\n\n\n\n\nEnter the quantity of questions for section %s: ", creation_vars.new_top_name);
creation_vars.p_count_string = cs_read_line();
break;
case 8:
// For UpdateCounts()
cs_console_clear();
printf("\nFinished updating CourseCount.txt");
cs_read_line();
cs_console_clear();
break;
case 9:
// For SelectCourse()
printf("\n\nEnter a Course ID: ");
break;
case 10:
// For StudyCourse()
study_hud();
break;
case 11:
// For StudyCourse()
printf("\n\n\nQuantity answered correctly, or option choice: ");
break;
case 12:
// For StudyCourse()
study_hud();
printf("\nInvalid Input:");
printf("\n\n\nvalue exceeds number of problems or questions, \nor it is less than zero.");
printf("\n\n\nQuantity answered correctly, or option choice: ");
break;
case 13:
// For WeeklySchedule()
weekly_dialog();
break;
}
}
void create_course()
{
// const int ZERO_INT = 0;
char *ZERO_CH = "0";
const int FOUR = 4;
// increment the course count after the course file containing the topic names
// is created AND/OR updated.
creation_vars.chapter_loop = ZERO_INT;
creation_vars.current_chapter = ZERO_INT;
creation_vars.chapters_int = ZERO_INT;
creation_vars.topic_counter = ZERO_INT;
creation_vars.topic_count_string = ZERO_CH;
creation_vars.topic_loop = ZERO_INT;
creation_vars.subsection_counter = ZERO_INT;
creation_vars.subsection_string = ZERO_CH;
creation_vars.sub_loop = ZERO_INT;
creation_vars.topic_id = ZERO_INT;
globals.topic_count = ZERO_INT;
creation_vars.current_subsection = ZERO_INT;
selection_dialogs(FOUR);
setup_data();
produce_course();
update_counts();
}
void setup_data()
{
// Constants
// const int ZERO_INT = 0;
// const int ONE_INT = 1;
// const int FIVE_INT = 5;
// const int SIX_INT = 6;
// const int SEVEN_INT = 7;
// const double ZERO_DOUBLE = 0.0;
// Initialization
struct st_node *topics = malloc(sizeof(struct st_node));
topics->total_nodes = ZERO_INT;
topics = cs_node_intializer(topics);
// For CreateCourse()
while (creation_vars.chapter_loop < creation_vars.chapters_int)
{
creation_vars.current_chapter = creation_vars.chapter_loop + ONE_INT;
selection_dialogs(FIVE_INT);
while (creation_vars.sub_loop < creation_vars.subsection_counter)
{
creation_vars.current_subsection = creation_vars.sub_loop + ONE_INT;
selection_dialogs(SIX_INT);
creation_vars.topic_counter = atoi(creation_vars.topic_count_string);
globals.topic_count += creation_vars.topic_counter;
while (creation_vars.topic_loop < creation_vars.topic_counter)
{
// Add a new topic node and initialize its page
topics = cs_add(topics);
struct st_topic_model *new_topic = malloc(sizeof(struct st_topic_model));
topics->page = new_topic;
// Setup essential data
if (creation_vars.topic_loop == ZERO_INT)
new_topic->top_id = ZERO_INT;
creation_vars.current_topic = creation_vars.topic_loop + ONE_INT;
sprintf(creation_vars.topic_string, "%d", creation_vars.current_topic);
// Create Top_Name string
char top_name[256]; // Ensure that the size is appropriate
snprintf(top_name, sizeof(top_name), "%d.%d.%s",
creation_vars.current_chapter,
creation_vars.current_subsection,
creation_vars.topic_string);
new_topic->top_name = strdup(top_name);
selection_dialogs(SEVEN_INT);
creation_vars.problem_count = atof(creation_vars.p_count_string);
new_topic->course_id = globals.course_count + ONE_INT;
new_topic->top_studied = false;
new_topic->next_date = strdup("NONE");
new_topic->first_date = strdup("NONE");
new_topic->num_problems = creation_vars.problem_count;
new_topic->num_correct = ZERO_DOUBLE;
new_topic->top_difficulty = ZERO_DOUBLE;
new_topic->top_repetition = ZERO_INT;
new_topic->interval_remaining = ZERO_DOUBLE;
new_topic->interval_length = ZERO_DOUBLE;
new_topic->engram_stability = ZERO_DOUBLE;
new_topic->engram_retrievability = ZERO_DOUBLE;
// Loop management
++creation_vars.topic_loop;
creation_vars.check = creation_vars.topic_loop;
if (creation_vars.check <= creation_vars.topic_counter)
{
++creation_vars.topic_id;
new_topic->top_id = creation_vars.topic_id;
}
}
// Reset topic-related variables
creation_vars.topic_counter = ZERO_INT;
creation_vars.topic_loop = ZERO_INT;
++creation_vars.sub_loop;
}
// Reset subsection-related variables
creation_vars.subsection_counter = ZERO_INT;
creation_vars.sub_loop = ZERO_INT;
++creation_vars.chapter_loop;
}
}
void produce_course()
{
const int ZERO = 0;
const int ONE = 1;
struct st_node *output = malloc(sizeof(struct st_node));
output->total_nodes = ZERO;
output = cs_node_intializer(output);
int total_topics = topics->total_nodes;
int node_quantity = total_topics - ONE;
int index = ZERO;
// Build out the node chain
while (index < node_quantity)
{
output = cs_add(output);
++index;
}
output = cs_element_at(output, ZERO);
// Add pages to nodes
index = ZERO;
while (index < total_topics)
{
struct st_string *temp = malloc(sizeof(struct st_string));
output->page = temp;
topics = cs_element_at(topics, index);
char *comma = ",";
char str_top_id[32], str_course_id[32], str_top_studied[32], str_num_problems[64], str_num_correct[64],
str_top_difficulty[64], str_top_repetition[32], str_interval_remaining[64], str_interval_length[64],
str_engram_stability[64], str_engram_retrievability[64];
// Convert to string
sprintf(str_top_id, "%d", ((struct st_topic_model*)topics->page)->top_id);
sprintf(str_course_id, "%d", ((struct st_topic_model*)topics->page)->course_id);
sprintf(str_top_studied, "%d", ((struct st_topic_model*)topics->page)->top_studied);
sprintf(str_num_problems, "%f", ((struct st_topic_model*)topics->page)->num_problems);
sprintf(str_num_correct, "%f", ((struct st_topic_model*)topics->page)->num_correct);
sprintf(str_top_difficulty, "%f", ((struct st_topic_model*)topics->page)->top_difficulty);
sprintf(str_top_repetition, "%d", ((struct st_topic_model*)topics->page)->top_repetition);
sprintf(str_interval_remaining, "%f", ((struct st_topic_model*)topics->page)->interval_remaining);
sprintf(str_interval_length, "%f", ((struct st_topic_model*)topics->page)->interval_length);
sprintf(str_engram_stability, "%f", ((struct st_topic_model*)topics->page)->engram_stability);
sprintf(str_engram_retrievability, "%f", ((struct st_topic_model*)topics->page)->engram_retrievability);
// Concatenate
int len = 1280; // Estimated buffer size, adjust if needed
char *result = malloc(len);
snprintf(result, len, "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",
str_top_id, str_course_id, ((struct st_topic_model*)topics->page)->top_name,
str_top_studied, ((struct st_topic_model*)topics->page)->next_date,
((struct st_topic_model*)topics->page)->first_date, str_num_problems,
str_num_correct, str_top_difficulty, str_top_repetition,
str_interval_remaining, str_interval_length, str_engram_stability,
str_engram_retrievability);
temp->string = result;
++index;
if (index < output->total_nodes)
{
output = cs_element_at(output, index);
}
}
output = cs_element_at(output, ZERO);
// Write to File
char *file_path;
int len = strlen(globals.directory_path) + strlen("/") + strlen(globals.course_name) + strlen(".txt") + ONE;
file_path = malloc(len);
sprintf(file_path, "%s/%s.txt", globals.directory_path, globals.course_name);
cs_write_all_lines(file_path, output, output->total_nodes);
char *list_file;
len = strlen(globals.directory_path) + strlen("/CourseList.txt") + ONE;
list_file = malloc(len);
sprintf(list_file, "%s/CourseList.txt", globals.directory_path);
// Free memory
index = ZERO;
while (index < output->total_nodes)
{
output = cs_element_at(output, index);
free(((struct st_string*)output->page)->string);
free(output->page);
++index;
}
cs_clear_nodes(output);
free(output);
}
void update_counts()
{
const int ONE = 1;
const int EIGHT = 8;
// Declare and initialize variables
int new_id = globals.course_count;
++new_id;
char new_id_str[12]; // Max string length for int32 is 11 chars + null terminator
sprintf(new_id_str, "%d", new_id);
char *path;
char *course_count_str;
char *list_contents;
// Create the new list entry
int len = strlen(new_id_str) + 1 + strlen(globals.course_name) + strlen(".txt") + 1 + strlen(creation_vars.file_path) + 1;
list_contents = malloc(len);
snprintf(list_contents, len, "%s,%s.txt,%s", new_id_str, globals.course_name, creation_vars.file_path);
struct st_node *content_list = malloc(sizeof(struct st_node));
struct st_string *temp_string = malloc(sizeof(struct st_string));
temp_string->string = list_contents;
content_list->page = temp_string;
content_list->index = 0;
content_list->total_nodes = ONE;
// Check for file existence and write accordingly
if (cs_file_exists(creation_vars.list_file))
course_listpath();
else
cs_write_all_lines(creation_vars.list_file, content_list, ONE);
// Create the path to the CourseCount.txt file
len = strlen(globals.directory_path) + 1 + strlen("CourseCount.txt") + 1;
path = malloc(len);
snprintf(path, len, "%s/%s", globals.directory_path, "CourseCount.txt");
// Update course count
course_count_str = cs_read_all_text(path);
globals.course_count = atoi(course_count_str);
free(course_count_str); // Don't forget to free memory if cs_read_all_text allocates memory
++globals.course_count;
len = snprintf(NULL, 0, "%d", globals.course_count); // Find out the length of the string
course_count_str = malloc(len + 1);
snprintf(course_count_str, len + 1, "%d", globals.course_count);
cs_write_all_text(path, course_count_str);
// Cleanup
free(path);
free(list_contents);
free(temp_string);
free(content_list);
free(course_count_str);
// Go to the selection dialog
selection_dialogs(EIGHT);
}
void course_listpath()
{
const int OPTION_ONE = 1; // this is the initialization option needed to get the file path
init_file_path(OPTION_ONE);
add_course_tolist(file_strings->txt_course_listfile, file_strings->bak_course_listfile, file_strings->txt_course_namefile);
}
// This one may need to be redesigned, because it looks like it just re-writes the info
// back to the file that it already had.
// Just double check later. It's probably fine.
void add_course_tolist(const char *file_path, const char *file_pathtwo, const char *course_filepath)
{
const int ZERO = 0;
const int ONE = 1;
int course_id = globals.course_count;
++course_id;
// Initialize linked list and temporary string
struct st_node *lines = malloc(sizeof(struct st_node));
lines->total_nodes = ZERO;
lines = cs_node_intializer(lines); // Initialize the linked list
struct st_string *temp_string;
// Read existing lines from the file if it exists
if (cs_file_exists(file_path)) {
lines = cs_ral_tolist(file_path);
}
// Prepare the string to be added to the list
char course_id_str[20]; // Assuming course_id would not exceed this length
snprintf(course_id_str, sizeof(course_id_str), "%d", course_id);
int len = strlen(course_id_str) + strlen(globals.course_name) + strlen(course_filepath) + 5; // Additional 5 characters for two commas, ".txt", and a null terminator
temp_string = malloc(len);
snprintf(temp_string, len, "%d,%s.txt,%s", course_id, globals.course_name, course_filepath);
// Add the new line to the linked list
lines = cs_element_at(lines, lines->total_nodes - 1); // Move to the last element
lines = cs_add(lines); // Add a new element at the end
lines->page = temp_string;
// Write to files
lines = cs_element_at(lines, ZERO); // Move to the first element
cs_write_all_lines(file_pathtwo, lines, lines->total_nodes);
cs_write_all_lines(file_path, lines, lines->total_nodes);
// Free memory
int index = ZERO;
while (index < lines->total_nodes)
{
lines = cs_element_at(lines, index);
free(lines->page); // Free the string
++index;
}
cs_clear_nodes(lines); // Clear all nodes
}
// 9-11-2023
// Continue from the next function.
// Need to use other PC.
// This one is too slow while ffmpeg is being used
// Clean this function up.
// DOES NEED THE 5 PROTOCOLS
void select_course() {
// Constants
const int ZERO = 0;
const int ONE = 1;
const int NINE = 9;
const int OPTION_FOUR = 4;
// Variables
int selection_int = ZERO;
bool valid_input = false;
char *selection_string;
// Initialize complete_list
struct st_node *complete_list = malloc(sizeof(struct st_node));
complete_list->total_nodes = ZERO;
complete_list = cs_node_intializer(complete_list);
// Read all lines and convert to list
struct st_node *lines = cs_ral_tolist(file_strings.txt_course_listfile);
// Build out the node chain and add pages to nodes
int total_nodes = lines->total_nodes;
int index = ZERO;
while (index < total_nodes) {
complete_list = cs_add(complete_list);
lines = cs_element_at(lines, index);
struct st_node *entries = cs_list_split(lines->page, ",");
struct st_course_list_model *new_list = malloc(sizeof(struct st_course_list_model));
complete_list->page = new_list;
// Populate new_list with data from entries
entries = cs_element_at(entries, ZERO);
new_list->course_id = atoi(entries->page);
entries = cs_element_at(entries, ONE);
new_list->course_name = entries->page;
entries = cs_element_at(entries, TWO);
new_list->file_path = entries->page;
complete_list = cs_element_at(complete_list, ++index);
}
// Display the courses
index = ZERO;
complete_list = cs_element_at(complete_list, ZERO);
while (index < total_nodes) {
printf("Course ID: %d - Course Name: %s\n",
((struct st_course_list_model*)complete_list->page)->course_id,
((struct st_course_list_model*)complete_list->page)->course_name);
complete_list = cs_element_at(complete_list, ++index);
}
// Selection Loop
while (!valid_input) {
selection_dialogs(NINE);
selection_string = cs_read_line();
selection_int = cs_to_int(selection_string);
valid_input = (selection_int != NULL);
if (valid_input) {
--selection_int;
int test_var = selection_int + ONE;
if (test_var >= ONE && test_var <= total_nodes) {
complete_list = cs_element_at(complete_list, selection_int);
if (test_var == ((struct st_course_list_model*)complete_list->page)->course_id) {
globals.file_path = ((struct st_course_list_model*)complete_list->page)->file_path;
globals.course_name = ((struct st_course_list_model*)complete_list->page)->course_name;
free_file_path(OPTION_FOUR);
init_file_path(OPTION_FOUR);
} else {
printf("Invalid selection.\n");
valid_input = false;
}
} else {
printf("Invalid selection.\n");
valid_input = false;
}
} else {
printf("Invalid selection.\n");
}
}
// Clean-up
index = ZERO;
// Free memory for entries and new_list within the loop
while (index < complete_list->total_nodes) {
complete_list = cs_element_at(complete_list, index);
free((struct st_course_list_model*)complete_list->page); // Free new_list
free(entries); // Free entries
++index;
}
// Free memory for lines
index = ZERO;
while (index < lines->total_nodes) {
lines = cs_element_at(lines, index);
free((struct st_string*)lines->page);
++index;
}
// Free memory for complete_list and lines
free(complete_list);
free(lines);
}
// Take note of the functions in c# source for locations of clearing of lists, and
// which lists are cleared.
void study_course()
{
const int ZERO = 0;
const int THREE = 3;
predict_vars.lock_goals = false;
predict_vars.until_new = ZERO;
study_vars.index = ZERO;
study_vars.today = globals.the_date;
study_vars.file_path = globals.file_path;
study_vars.line_count = ZERO;
study_lines();
if (study_vars.line_count > ZERO)
{
if (topics_list->total_nodes > ZERO)
{
study_dates();
if (to_study->total_nodes != ZERO)
{
study_not_zero();
while (globals.problems_done != true)