-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiary.h
More file actions
948 lines (901 loc) · 26.4 KB
/
diary.h
File metadata and controls
948 lines (901 loc) · 26.4 KB
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
// preprocessor declarations
#define listMaxIndex 50
#define maxListLength 100
#define SUBLEN 100 +2
#define DIARY "diaryinfo.bin"
// variables to store file names (doesnt have to be global)
char mainfile[MAXFILENAMELEN];
char listfile[MAXFILENAMELEN];
// global typedefs and structures
typedef struct _info{
char subject[SUBLEN];
int d, m, y;
} info;
// global variables
int databaseLength = 0, databaseCapacity = 4; // main array
int textLength = 0, textCapacity = 50; // main body informations
char* text; // main body
int listLast = 0, listLength[listMaxIndex]; // length of list indx
char list[listMaxIndex][maxListLength]; // list array
enum _color_codes highlight_color, normal_color;
bool highlight_mode = 0;
info* database;
// taken from todolist
int dayNum[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// function prototypes
int isTagged();
void removeTag(int tag_type);
void serializeDiary();
void deserializeDiary();
void addInfo(char subject[SUBLEN], int d, int m, int y);
int removeInfo(int indx);
void listEntries();
void userScan();
void userListScan();
void searchDiary();
bool diarySubstringCheck(char* large, char* small);
void scan_entry(int indx);
void edit_list(int indx);
void add_list();
void remove_list(int indx);
void remove_nth_listend_tag_from_body(int indx, int n);
void print_list_only();
void serialize_list_only(FILE* fp);
bool check_list_endtag(char* s);
void remove_list_endtag(char* s);
int frontTagged(FILE* fp, FILE* ivan);
void readFromFile(int indx);
void readListFromFile(FILE* ivan);
int diary(void)
{
database = (info*) malloc(databaseCapacity * sizeof(info));
text = (char*) malloc(textCapacity * sizeof(char));
normal_color = not_selected;
highlight_color = selected;
while(1)
{
deserializeDiary();
char command = diary_menu();
color(normal_color);
if(command == '7') break;
if(command == '1')
{
printf("Today is %2.2d/%2.2d/%4.4d\n", date, month, year);
printf("Date (format dd/mm/yyyy): ");
int d, m, y;
scanf("%d / %d / %d", &d, &m, &y);
fflush(stdin);
if(d < 1 || m < 1 || ((m != 2 && d > dayNum[m-1]) || (m == 2 && isLeapYear(y) && d > 29) || (m == 2 && d > 28)) || m > 12)
{
printf("Invalid time!\n");
system("pause");
continue;
}
char subject[SUBLEN] = {};
printf("Subject: ");
fgets(subject, SUBLEN, stdin);
addInfo(subject, d, m, y);
printf("------------------>> ENTRY NO %d <<--------------------\n", databaseLength);
serializeDiary();
userScan();
mainfile[0] = listfile[0] = 0;
num_to_string(mainfile, databaseLength-1);
strcpy(listfile, mainfile);
strcat(mainfile, ".txt");
strcat(listfile, "list.txt");
FILE* fp = fopen(mainfile, "w");
fprintf(fp,"%s", text);
fclose(fp);
fp = fopen(listfile, "w");
for(int i = 0 ; i < listLast-1 ; i++)
fprintf(fp,"%s\n", list[i]);
fprintf(fp, "%s", list[listLast-1]);
fclose(fp);
printf("\n");
}
if(command == '2')
{
printf("Give the number to load : ");
int indx;
scanf("%d", &indx);
fflush(stdin);
if(indx < 1 || indx > databaseLength)
{
printf("<ENTRY DOESNT EXIST>\n");
system("pause");
continue;
}
indx--;
printf("Date: %2.2d/%2.2d/%4.4d\n", database[indx].d, database[indx].m, database[indx].y);
printf("Subject: %s", database[indx].subject);
printf("------------------------------------------------\n");
readFromFile(indx);
printf("\n");
}
if(command == '3')
{
printf("Give the number to delete : ");
int indx;
scanf("%d", &indx);
fflush(stdin);
if(indx < 1 || indx > databaseLength)
{
printf("<ENTRY DOESNT EXIST>\n");
system("pause");
continue;
}
indx--;
removeInfo(indx);
printf("<SUCCESSFULLY REMOVED THIS ENTRY>\n");
}
if(command == '4')
listEntries();
if(command == '5')
{
printf("Which entry would you like to edit? Ans: ");
int indx;
scanf("%d", &indx);
fflush(stdin);
if(indx < 1 || indx > databaseLength)
{
printf("<ENTRY DOESNT EXIST>\n");
system("pause");
continue;
}
indx--;
edit_list(indx);
}
if(command == '6')
searchDiary();
system("pause");
}
free(text);
free(database);
return 0;
}
int isTagged()
{
char checker[5] = {};
for(int i = textLength-3 ; i < textLength ; i++)
checker[i-textLength+3] = text[i];
if(!strcmp(checker, "<h>"))
return 1;
if(!strcmp(checker, "<l>"))
return 3;
for(int i = textLength-4 ; i < textLength ; i++)
checker[i-textLength+4] = text[i];
if(!strcmp(checker, "</h>"))
return 2;
if(!strcmp(checker, "</l>"))
return 4;
return 0;
}
void removeTag(int tag_type)
{
if(tag_type == 1 || tag_type == 2)
{
for(int i = textLength-3 ; i < textLength ; i++)
text[i] = 0;
textLength -= 3;
}
else if(tag_type == 3 || tag_type == 4)
{
for(int i = textLength-4 ; i < textLength ; i++)
text[i] = 0;
textLength -= 4;
}
}
void serializeDiary()
{
FILE* fp = fopen(DIARY, "wb");
for(int i = 0 ; i < databaseLength ; i++)
fwrite(&database[i], sizeof(database[0]), 1, fp);
fclose(fp);
}
void deserializeDiary()
{
databaseCapacity = 4;
databaseLength = 0;
FILE* fp = fopen(DIARY, "rb");
if(fp == NULL)
return;
while(1)
{
info temp;
int check = fread(&temp, sizeof(temp), 1, fp);
if(check == 0)
break;
addInfo(temp.subject, temp.d, temp.m, temp.y);
}
fclose(fp);
}
void addInfo(char subject[SUBLEN], int d, int m, int y)
{
strcpy(database[databaseLength].subject, subject);
database[databaseLength].d = d;
database[databaseLength].m = m;
database[databaseLength].y = y;
databaseLength++;
if(databaseLength == databaseCapacity)
{
databaseCapacity *= 2;
database = (info*) realloc(database, databaseCapacity * sizeof(info));
}
}
int removeInfo(int indx)
{
databaseLength--;
char entry_name[MAXFILENAMELEN] = {};
char list_name[MAXFILENAMELEN] = {};
num_to_string(entry_name, indx);
strcpy(list_name, entry_name);
strcat(entry_name, ".txt");
strcat(list_name, "list.txt");
remove(entry_name);
remove(list_name);
char temp_name[MAXFILENAMELEN] = {};
char temp_list_name[MAXFILENAMELEN] = {};
num_to_string(temp_name, databaseLength);
strcpy(temp_list_name, temp_name);
strcat(temp_name, ".txt");
strcat(temp_list_name, "list.txt");
rename(temp_name, entry_name);
rename(temp_list_name, list_name);
database[indx] = database[databaseLength];
serializeDiary();
return 0;
}
void searchDiary()
{
printf("Now we will insert the date range.\n "
"Input a negative value to disable this filter\n");
printf("Input starting date (dd/mm/yyyy): ");
int sd, sm, sy; // start day-month-year
int ed, em, ey; // ending day-month-year
bool dateFilter, subFilter, bodyFilter;
scanf("%d / %d / %d", &sd, &sm, &sy);
fflush(stdin);
if(sd == 0 || sm == 0 || ((sm != 2 && sd > dayNum[sm-1]) || (sm == 2 && isLeapYear(sy) && sd > 29) || (sm == 2 && sd > 28)) || sm > 12)
{
printf("Invalid time!\n");
system("pause");
return;
}
if(sd > 0 && sm > 0 && sy > 0)
{
printf("Input ending date (dd/mm/yyyy): ");
scanf("%d / %d / %d", &ed, &em, &ey);
fflush(stdin);
if(ed < 1 || em < 1 || ((em != 2 && ed > dayNum[em-1]) || (em == 2 && isLeapYear(ey) && ed > 29) || (em == 2 && ed > 28)) || em > 12)
{
printf("Invalid time!\n");
system("pause");
return;
}
dateFilter = 1;
}
else dateFilter = 0;
char sub_search[SUBLEN] = {}, body_search[SUBLEN * 5] = {};
printf("Enter Subject Filter.\nBlank input disables this filter.\n");
printf("Subject (can be a substring) : ");
fgets(sub_search, SUBLEN, stdin);
fflush(stdin);
printf("Enter Body Filter.\nBlank input disables this filter.\n");
printf("Body substring: ");
fgets(body_search, 5 * SUBLEN, stdin);
fflush(stdin);
int* results = (int*) calloc(databaseLength +1 , sizeof(int));
int r = 0;
system("cls");
printf("<SEARCH STARTED!>\n"
"Please wait patiently as this can take a moment\n\n");
int start;
int end;
if(dateFilter)
{
start = days_gone(sm, sd);
if(start == 366) start = 59;
end = days_gone(em, ed);
if(end == 366) end = 59;
}
if(strlen(sub_search) == 1) subFilter = 0;
else subFilter = 1;
if(strlen(body_search) == 1) bodyFilter = 0;
else bodyFilter = 1;
for(int i = 0 ; i < databaseLength ; i++)
{
int current = days_gone(database[i].m, database[i].d);
if(current == 366) current = 59;
if(!dateFilter || (database[i].y > sy || (database[i].y == sy && current >= start))
&& (database[i].y < ey || (database[i].y == ey && current <= end)))
{
if(!subFilter || diarySubstringCheck(database[i].subject, sub_search))
{
scan_entry(i);
bool flag = 0;
if(!bodyFilter || diarySubstringCheck(text, body_search)) flag = 1;
for(int j = 0 ; j < listLast && !flag ; j++)
if(diarySubstringCheck(list[j], body_search))
flag = 1;
if(flag)
{
results[r] = i;
r++;
}
}
}
}
printf("FOUND: %d matches\n\n", r);
for(int i = 0 ; i < r ; i++)
{
printf("Entry no : %d\n"
"Date: %2.2d/%2.2d/%4.4d\n"
"Subject: %s\n\n", results[i]+1, database[results[i]].d, database[results[i]].m, database[results[i]].y, database[results[i]].subject);
}
free(results);
}
bool diarySubstringCheck(char* large, char* small)
{
int slen = strlen(small);
int llen = strlen(large);
for(int i = 0 ; i < llen ; i++)
{
for(int j = 0 ; j < slen ; j++)
{
char s = *(small+j);
char l = *(large+i+j);
while(1)
{
if(j == slen)
return true;
if(i == llen)
return false;
if(!(s == '<' || l == '<' || s == '\n' || l == '\n'))
break;
// skip newlines
while(s == '\n' && j < slen)
{
j++;
s = *(small+j);
}
while(l == '\n' && i < llen)
{
i++;
l = *(large+i+j);
}
// skip tags
while(s == '<' && j < slen)
{
if(*(small+j+1) == 'l' && *(small+j+2) == '>') j += 3;
if(*(small+j+1) == 'h' && *(small+j+2) == '>') j += 3;
if(*(small+j+1) == '/' && *(small+j+2) == 'l' && *(small+j+3) == '>') j += 4;
if(*(small+j+1) == '/' && *(small+j+2) == 'h' && *(small+j+3) == '>') j += 4;
if(j >= slen) return true;
s = *(small+j);
}
while(l == '<' && i < llen)
{
if(*(large+j+i+1) == 'l' && *(large+j+i+2) == '>') i += 3;
if(*(large+j+i+1) == 'h' && *(large+j+i+2) == '>') i += 3;
if(*(large+j+i+1) == '/' && *(large+j+i+2) == 'l' && *(large+j+i+3) == '>') i += 4;
if(*(large+j+i+1) == '/' && *(large+j+i+2) == 'h' && *(large+j+i+3) == '>') i += 4;
if(i >= llen) return false;
l = *(large+i+j);
}
}
if(isalpha(s)) s = tolower(s);
if(isalpha(l)) l = tolower(l);
if(l != s)
break;
if(j >= slen-1)
return true;
}
}
return false;
}
void scan_entry(int indx)
{
char ch;
textCapacity = 50;
text = (char*) realloc(text, textCapacity * sizeof(char));
textLength = 0;
listLast = 0;
// opening the file
mainfile[0] = listfile[0] = 0;
num_to_string(mainfile, indx);
num_to_string(listfile, indx);
strcat(mainfile, ".txt");
strcat(listfile, "list.txt");
FILE* fp = fopen(mainfile, "r");
while(1)
{
if(fscanf(fp, "%c", &ch) == EOF) break;
if(textLength == textCapacity - 1)
{
textCapacity *= 2;
text = (char*) realloc(text, textCapacity * sizeof(char));
}
text[textLength] = ch;
textLength++;
}
fclose(fp);
fp = fopen(listfile, "r");
memset(listLength, 0, listMaxIndex * sizeof(int));
while(1)
{
if(fgets(list[listLast], maxListLength, fp) == NULL) break;
listLength[listLast] = strlen(list[listLast]);
listLast++;
}
fclose(fp);
}
void edit_list(int indx)
{
listfile[0] = 0;
num_to_string(listfile, indx);
strcat(listfile, "list.txt");
FILE* fp = fopen(listfile, "r");
listLast = 0;
memset(listLength, 0, listMaxIndex * sizeof(int));
bool end_of_file = 0;
while(!end_of_file)
{
while(!end_of_file)
{
char ch;
if(fscanf(fp, "%c", &ch) == EOF)
{
end_of_file = 1;
break;
}
if(ch == '\n') break;
list[listLast][listLength[listLast]] = ch;
listLength[listLast]++;
list[listLast][listLength[listLast]] = 0;
}
listLast++;
}
fclose(fp);
if(listLast == 1)
{
printf("Sorry, this entry does not have any list to edit\n"
"You can only edit list if the entry has atleast one list point\n");
return;
}
char input = 0;
while(input != escape)
{
fp = fopen(listfile, "w");
system("cls");
printf("Press esc to exit\n\n");
printf("CURRENT LIST:-\n");
print_list_only();
printf("\n\nWhat would you like to do? Add or remove? (A/R) : ");
input = _getch();
printf("%c \n", input);
if(input == 'A') add_list();
else if(input == 'R') remove_list(indx);
serialize_list_only(fp);
fclose(fp);
}
}
void add_list()
{
printf("Give a float value that is inbetween the two list numbers\n"
"where you want your new list entry to be in\n"
"(For example to add a list between 1 and 2, give 1.5) : ");
double input;
scanf("%lf", &input);
fflush(stdin);
input -= 1;
printf("Give the new list entry: \n");
fgets(list[listLast], maxListLength, stdin);
list[listLast][strlen(list[listLast]) -1] = 0; //discarding the \n
if(input + 1 > listLast)
{
strcat(list[listLast], "</l>");
remove_list_endtag(list[listLast-1]);
}
listLength[listLast] = strlen(list[listLast]);
listLast++;
for(int i = listLast-1 ; i > input+1 && i > 0 ; i--)
{
swap_string(list[i], list[i-1]);
int temp = listLength[i];
listLength[i] = listLength[i-1];
listLength[i-1] = temp;
}
}
void remove_list(int indx)
{
printf("Give the list number you want to remove: ");
int input;
scanf("%d", &input);
fflush(stdin);
input--;
if(input < 0 || input >= listLast)
{
printf("<LIST ELEMENT DOESNT EXIST>\n");
system("pause");
return;
}
// first - last - middle
if(input == 0 && check_list_endtag(list[input]))
{
if(input != listLast-1 && !check_list_endtag(list[input+1]))
{
strcat(list[input+1], "</l>");
listLength[input+1] += 4;
}
else remove_nth_listend_tag_from_body(indx , 1);
}
else if(input == listLast-1 && check_list_endtag(list[input]))
{
if(!check_list_endtag(list[input-1]))
{
strcat(list[input-1], "</l>");
listLength[input-1] += 4;
}
else
{
int count = 0;
for(int i = 0 ; i <= input ; i++)
if(check_list_endtag(list[i])) count++;
remove_nth_listend_tag_from_body(indx, count);
}
}
else if(check_list_endtag(list[input]))
{
if(!check_list_endtag(list[input+1]))
{
strcat(list[input+1], "</l>");
listLength[input+1] += 4;
}
else if(!check_list_endtag(list[input-1]))
{
strcat(list[input-1], "</l>");
listLength[input-1] += 4;
}
else
{
int count = 0;
for(int i = 0 ; i <= input ; i++)
if(check_list_endtag(list[i])) count++;
remove_nth_listend_tag_from_body(indx, count);
}
}
listLength[input] = 0;
list[input][0] = 0;
listLast--;
for(int i = input ; i < listLast ; i++)
{
swap_string(list[i], list[i+1]);
int temp = listLength[i];
listLength[i] = listLength[i+1];
listLength[i+1] = temp;
}
}
void remove_nth_listend_tag_from_body(int indx, int n)
{
mainfile[0] = 0;
num_to_string(mainfile, indx);
strcat(mainfile, ".txt");
FILE* fp = fopen(mainfile, "r");
int count = 0;
textCapacity = 50;
text = (char*) realloc(text, textCapacity * sizeof(char));
text[0] = 0;
textLength = 0;
while(1)
{
char temp[500] = {};
int templen;
if(fgets(temp, 500, fp) == NULL) break;
templen = strlen(temp);
if(templen >= 3 && temp[0] == '<' && temp[1] == 'l' && temp[2] == '>') count++;
if(count == n)
{
for(int i = 0 ; i < templen ; i++)
temp[i] = temp[i+3];
}
if(templen + textLength >= textCapacity)
{
textCapacity *= 2;
text = (char*) realloc(text, textCapacity * sizeof(char));
}
templen = strlen(temp);
textLength += templen;
strcat(text, temp);
}
fclose(fp);
fp = fopen(mainfile, "w");
fprintf(fp, "%s", text);
fclose(fp);
textLength = 0;
}
void print_list_only()// remove tags
{
for(int i = 0 ; i < listLast ; i++)
{
if(check_list_endtag(list[i]))
{
if(i+1 < 10)
printf("%d. %s\b\b\b\b \b\b\b\b\n", i+1, list[i]);
else
printf("%d. %s\b\b\b\b\b \b\b\b\b\b\n", i+1, list[i]);
}
else printf("%d. %s\n", i+1, list[i]);
}
}
void serialize_list_only(FILE* fp)
{
for(int i = 0 ; i < listLast-1 ; i++)
fprintf(fp, "%s\n", list[i]);
fprintf(fp, "%s", list[listLast-1]);
}
bool check_list_endtag(char* s)
{
int len = strlen(s);
if(s[len-4] == '<' && s[len-3] == '/' && s[len-2] == 'l' && s[len-1] == '>')
return 1;
return 0;
}
void remove_list_endtag(char* s)
{
int len = strlen(s);
s[len-4] = 0;
}
void listEntries()
{
if(databaseLength == 0)
{
printf("THERE ARE NO ENTRIES!\n");
return;
}
printf("The following entries are present:\n\n");
for(int i = 0 ; i < databaseLength ; i++)
{
printf("Entry no %d\n"
"Date: %2.2d/%2.2d/%4.4d\n"
"Subject: %s\n", i+1, database[i].d, database[i].m, database[i].y, database[i].subject);
}
}
void userScan()
{
char ch;
textCapacity = 50;
text = (char*) realloc(text, textCapacity * sizeof(char));
textLength = 0;
listLast = 0;
memset(listLength, 0, listMaxIndex * sizeof(int));
int tag;
while((ch = _getch()) != escape)
{
if(ch == backspace)
{
tag = isTagged();
if(tag == 1 || tag == 2)
{
highlight_mode = !highlight_mode;
if(highlight_mode)
color(highlight_color);
else color(normal_color);
}
removeTag(tag); // incase there is a tag
printf("\b \b");
if(textLength != 0)
{
textLength--;
text[textLength] = 0;
}
}
else if(ch == stilde) // pressed highlight
{
highlight_mode = !highlight_mode;
tag = isTagged();
if(tag == 1) // if someone spammed highlightmode
removeTag(tag);
else if(highlight_mode == 1)
{
strcat(text, "<h>");
textLength += 3;
}
else
{
strcat(text, "</h>");
textLength += 4;
}
if(highlight_mode)
color(highlight_color);
else color(normal_color);
}
else if(ch == tilde && listLast < listMaxIndex) // pressed list mode
{
if(text[textLength-1] == '\n')
{
strcat(text, "<l>");
textLength += 3;
}
else
{
strcat(text, "\n<l>");
printf("\n");
textLength += 4;
}
int temp = listLast;
color(highlight_color);
printf("%d. ", listLast+1);
userListScan();
if(temp == listLast)
removeTag(isTagged());
}
else
{
if(ch == enter)
ch = '\n';
printf("%c", ch);
text[textLength] = ch;
textLength++;
text[textLength] = 0;
if(textLength == textCapacity-1)
{
textCapacity *= 2;
text = (char*) realloc(text, textCapacity * sizeof(char));
}
}
}
color(normal_color);
highlight_mode = 0;
}
void userListScan()
{
char ch;
bool flag = 0;
while((ch = _getch()) != tilde)
{
flag = 1;
if(ch == backspace)
{
if(listLength[listLast] != 0)
{
printf("\b \b");
listLength[listLast]--;
list[listLast][listLength[listLast]] = 0;
}
}
else if(ch == enter && listLast < listMaxIndex)
{
listLast++;
printf("\n%d. ", listLast+1);
}
else if(ch == enter)
{
listLast++;
break;
}
else
{
printf("%c", ch);
list[listLast][listLength[listLast]] = ch;
listLength[listLast]++;
list[listLast][listLength[listLast]] = 0;
if(listLength[listLast] == maxListLength) ch = enter;
}
}
for(int i = 0 ; i < maxListLength ; i++)
printf("\b");
for(int i = 0 ; i < maxListLength ; i++)
printf(" ");
for(int i = 0 ; i < maxListLength ; i++)
printf("\b");
if(listLast < listMaxIndex) listLength[listLast] = 0;
if(flag == 0) return;
strcat(list[listLast-1], "</l>");
listLength[listLast-1] += 4;
if(highlight_mode == 0)
color(normal_color);
}
int frontTagged(FILE* fp, FILE* ivan)
{
char tag[5] = "<";
for(int i = 1 ; i < 3 ; i++)
fscanf(fp, "%c", &tag[i]);
if(!strcmp(tag, "<h>"))
{
highlight_mode = 1;
color(highlight_color);
return 1;
}
else if(!strcmp(tag, "<l>"))
{
readListFromFile(ivan);
return 1;
}
fscanf(fp, "%c", &tag[3]);
if(!strcmp(tag, "</h>"))
{
highlight_mode = 0;
color(normal_color);
return 1;
}
fseek(fp, -4, SEEK_CUR);
return 0;
}
void readFromFile(int indx)
{
listLast = 0;
mainfile[0] = listfile[0] = 0;
num_to_string(mainfile, indx);
num_to_string(listfile, indx);
strcat(mainfile, ".txt");
strcat(listfile, "list.txt");
FILE* fp = fopen(mainfile, "r");
FILE* ivan = fopen(listfile, "r");
char ch;
textCapacity = 50;
text = (char*) realloc(text, textCapacity * sizeof(char));
textLength = 0;
memset(listLength, 0, listMaxIndex * sizeof(int));
while(1)
{
if(fscanf(fp, "%c", &ch) == EOF) break;
if(ch == '<')
frontTagged(fp, ivan);
else
{
if(textLength == textCapacity - 1)
{
textCapacity *= 2;
text = (char*) realloc(text, textCapacity * sizeof(char));
}
printf("%c", ch);
text[textLength] = ch;
textLength++;
}
}
text[textLength] = 0;
fclose(fp);
fclose(ivan);
}
void readListFromFile(FILE* ivan)
{
color(highlight_color);
while(1)
{
while(1)
{
char ch;
if(fscanf(ivan, "%c", &ch) == EOF) break;
if(ch == '\n') break;
list[listLast][listLength[listLast]] = ch;
listLength[listLast]++;
list[listLast][listLength[listLast]] = 0;
}
int flag = 0;
if(list[listLast][listLength[listLast] - 1] == '>') flag++;
if(list[listLast][listLength[listLast] - 2] == 'l') flag++;
if(list[listLast][listLength[listLast] - 3] == '/') flag++;
if(list[listLast][listLength[listLast] - 4] == '<') flag++;
listLast++;
if(flag == 4)
{
if(listLast < 10)
printf("%d. %s\b\b\b\b \b\b\b\b\n", listLast, list[listLast-1]);
else
printf("%d. %s\b\b\b\b\b \b\b\b\b\b\n", listLast, list[listLast-1]);
if(highlight_mode == 0)
color(normal_color);
break;
}
else
{
printf("%d. %s\n", listLast, list[listLast-1]);
}
}
}
// swap between modes frequently
// among us sus- text art
// finish these following functions one by one
// use memset to reset the listlength!
// backspace to previous line
// days gone used in the search (fix the leap year)