-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal.c
More file actions
748 lines (655 loc) · 20.2 KB
/
Copy pathjournal.c
File metadata and controls
748 lines (655 loc) · 20.2 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
#include <stdio.h>
#include <ncurses.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <errno.h>
#define MAX_LENGTH 2000
#define NUM_YEARS 24
#define NUM_MONTHS 12
#define JOURNAL_FOLDER "entries"
#define CURSOR_POS (getcury(stdscr) - 1) * COLS + getcurx(stdscr)
void get_entry(char *input, int max_length, char *folder);
void get_date(char *date, size_t buffer_size);
void get_year(char *year, size_t buffer_size);
void get_month(char *month, size_t buffer_size);
void remove_new_line(char *str);
bool create_directories(char *directory);
bool file_exists(char *filename);
int file_count(char *filepath);
int get_year_files(char *folder, char years[][8]);
int get_month_files(char *folder, char months[][24]);
int get_read_files(char *folder, char readfiles[][100]);
void read_file(FILE *to_read);
void update_entry(char *input, int max_length, char *folder);
int get_read_files_complete(char *readfile, char *folder);
int main(int argc, char *argv[])
{
char *options = "urwh";
char option = getopt(argc, argv, options);
if (option == '?')
{
printf("Invalid option. Valid options are: -r to read and -w to write.\n");
return 1;
}
if (getopt(argc, argv, options) != -1)
{
printf("Only one filter allowed.\n");
return 2;
}
if (argc != 2 && argc != 3)
{
printf("Usage: ./journal [flag] [directory]\nOtherwise, default folder is entries/\n-r to read, -w to write, -u to update and -h for help\n");
return 3;
}
// showing current date once
char date[80];
get_date(date, sizeof(date));
printf("Current date: %s\n", date);
char year[8];
get_year(year, sizeof(year));
char month[24];
get_month(month, sizeof(month));
char folder[80];
(argc == 3) ? strcpy(folder, argv[2]) : strcpy(folder, JOURNAL_FOLDER);
char input[MAX_LENGTH];
char readfile[100];
FILE *to_read = NULL;
switch (option)
{
// help flag
case 'h':
printf("Usage: ./journal [flag] [directory]\nOtherwise, default folder is entries/\n-r to read, -w to write, -u to update and -h for help\n");
return 0;
// write flag
case 'w':
char file[200];
char directory[100];
do
{
// reset file string on every iteration
memset(file, 0, sizeof(file));
memset(directory, 0, sizeof(directory));
// copy folder unto file
(argc == 3) ? strcpy(file, argv[2]) : strcpy(file, JOURNAL_FOLDER);
strcat(file, "/");
strcat(directory, file);
// getting current year
get_year(year, sizeof(year));
strcat(file, strcat(year, "/"));
strcat(directory, year);
// getting current month
get_month(month, sizeof(month));
strcat(directory, month);
strcat(file, strcat(month, "/"));
// directory does not have .txt
printf("%s\n", directory);
// getting title
char title[100];
printf("Give us a title: ");
fgets(title, 100, stdin);
// remove '\n'
remove_new_line(title);
if (strlen(title) == 0)
{
int filecount = file_count(file);
sprintf(title, "%03i", filecount);
}
strcat(title, ".txt");
strcat(file, title);
}
while (file_exists(file));
// file must not exist
// create directories if not present
printf("Creating directories in %s.\n", directory);
create_directories(directory);
printf("Writing into %s\n", file);
// getting valid entry and writing into file
get_entry(input, MAX_LENGTH, file);
if (strlen(input) == 0)
{
printf("Empty entry!\n");
return 1;
}
printf("Your entry was: %s\n", input);
printf("Writing into %s\n", file);
FILE *output = fopen(file, "w");
if (output == NULL)
{
printf("Could not open file %s\n", file);
return 1;
}
fputs(input, output);
fclose(output);
printf("Writing complete! Check %s!\n", file);
break;
// read flag
case 'r':
if (get_read_files_complete(readfile, folder) != 0)
{
return 1;
}
to_read = fopen(readfile, "r");
if (to_read == NULL)
{
printf("Failed to open %s.\n", readfile);
return 1;
}
read_file(to_read);
break;
// update flag
case 'u':
if (get_read_files_complete(readfile, folder) != 0)
{
return 1;
}
to_read = fopen(readfile, "r");
if (to_read == NULL)
{
printf("Failed to open %s.\n", readfile);
return 1;
}
size_t bytes_read = fread(input, sizeof(char), MAX_LENGTH - 1, to_read);
input[bytes_read] = '\0';
printf("Input length %li\n", strlen(input));
update_entry(input, MAX_LENGTH, readfile);
printf("Your entry was: %s\n", input);
fclose(to_read);
FILE *to_update = fopen(readfile, "w");
if (to_update == NULL)
{
printf("Something went wrong!\n");
return 1;
}
fputs(input, to_update);
printf("Writing complete! Check %s!\n", readfile);
fclose(to_update);
}
return 0;
}
// function to get input in a fashion similar to the terminal
void get_entry(char *input, int max_length, char *folder)
{
initscr();
cbreak();
// noecho allows for full control over input and its effects. changes made to screen/window can be controlled.
noecho();
keypad(stdscr, TRUE);
// initialization
int length = 0;
printw("Writing into %s. Getting some input %i/%i", folder, length, MAX_LENGTH - 1);
move(1, 0);
refresh();
// auto quits when length == MAX_LENGTH - 2 as input[MAX_LENGTH - 1] then must be '\0'
int ch;
while ((ch = getch()) != '\n' && length < MAX_LENGTH - 1)
{
if (ch == KEY_BACKSPACE)
{
// backspace interactions should only take place when cursor is not at start of string
if (length > 0 && CURSOR_POS > 0)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
// updating characters, essentially shifting them down left by 1
for (int i = CURSOR_POS - 1; i < length - 1; i++)
{
input[i] = input[i + 1];
}
length--;
input[length] = '\0';
// backspace moves cursor but not anymore with noecho();
// start of row shift back one row and to end of said row
// clearing and reprinting input helps with display management, meaning we only need to manage where the cursor should be as input is always correct.
if (x == 0)
{
clear();
printw("Writing into %s. Getting some input %i/%i", folder, length, MAX_LENGTH - 1);
move(1, 0);
printw("%s", input);
move(y - 1, COLS - 1);
}
// otherwise just shift back once
else
{
clear();
printw("Writing into %s. Getting some input %i/%i", folder, length, MAX_LENGTH - 1);
move(1, 0);
printw("%s", input);
move(y, x - 1);
}
}
}
else if (ch == KEY_LEFT)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
// left key interactions control only cursor. input display unaffected.
if (length > 0 && CURSOR_POS > 0)
{
if (x == 0)
{
move(y - 1, COLS - 1);
}
else
{
move(y, x - 1);
}
}
}
else if (ch == KEY_RIGHT)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
if (CURSOR_POS < length)
{
if (x == COLS - 1)
{
move(y + 1, 0);
}
else
{
move(y, x + 1);
}
}
}
// only care about character inputs
else if (ch < KEY_MIN || ch > KEY_MAX)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
// updating based on cursor position. characters are shifted down right by 1.
for (int i = length; i > CURSOR_POS; i--)
{
input[i] = input[i - 1];
}
// update
input[CURSOR_POS] = ch;
length++;
input[length] = '\0';
// as usual, clearing and reprinting input allows us to manage cursor position.
clear();
printw("Writing into %s. Getting some input %i/%i", folder, length, MAX_LENGTH - 1);
move(1, 0);
printw("%s", input);
if (x == COLS - 1)
{
move(y + 1, 0);
}
else
{
move(y, x + 1);
}
}
}
input[length] = '\0';
endwin();
}
// function to get date string
void get_date(char *date, size_t buffer_size)
{
memset(date, 0, buffer_size);
time_t rawtime;
struct tm *timeinfo; // pointer to tm struct defined in time.h
time(&rawtime); // probably read time into rawtime
timeinfo = localtime(&rawtime); // get localtime from values stored in rawtime
// format and writing timeinfo into buffer
strftime(date, buffer_size, "%Y-%B-%d", timeinfo);
}
// function to get year string
void get_year(char *year, size_t buffer_size)
{
memset(year, 0, buffer_size);
time_t rawtime;
struct tm *timeinfo; // pointer to tm struct defined in time.h
time(&rawtime); // probably read time into rawtime
timeinfo = localtime(&rawtime); // get localtime from values stored in rawtime
// format and writing timeinfo into buffer
strftime(year, buffer_size, "%Y", timeinfo);
}
// function to get month string
void get_month(char *month, size_t buffer_size)
{
memset(month, 0, buffer_size);
time_t rawtime;
struct tm *timeinfo; // pointer to tm struct defined in time.h
time(&rawtime); // probably read time into rawtime
timeinfo = localtime(&rawtime); // get localtime from values stored in rawtime
// format and writing timeinfo into buffer
strftime(month, buffer_size, "%B", timeinfo);
}
// function to remove new line from fgets
void remove_new_line(char *str)
{
size_t length = strcspn(str, "\n");
str[length] = '\0';
}
// function to check if a file exists
bool file_exists(char *filename)
{
if (access(filename, F_OK) != -1)
{
printf("%s exists!\n", filename);
return true;
}
return false;
}
// function to count files to use as file title in the event a title is not provided
int file_count(char *filepath)
{
DIR *dir = opendir(filepath);
if (dir == NULL)
{
printf("Failed to open directory\n");
return 0;
}
int filecount = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_REG)
{
filecount++;
}
}
closedir(dir);
return filecount;
}
// function to create directories by accessing the command line
bool create_directories(char *directory)
{
char command[100];
snprintf(command, sizeof(command), "mkdir -p %s", directory);
int status = system(command);
if (status == 0)
{
printf("Directories in %s created.\n", directory);
return true;
}
else
{
printf("Directories not created.\n");
return false;
}
}
// function to get year folders
int get_year_files(char *folder, char years[][8])
{
// years[NUM_YEARS][8];
DIR *directory = opendir(folder);
if (directory == NULL)
{
printf("Failed to open directory.\n");
return -1;
}
int foldercount = 0;
struct dirent *entry;
while ((entry = readdir(directory)) != NULL)
{
if (entry->d_type == DT_DIR)
{
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
{
strcpy(years[foldercount], entry->d_name);
foldercount++;
}
}
}
closedir(directory);
return foldercount;
}
// function to get month folders in a particular year
int get_month_files(char *folder, char months[][24])
{
// char months[NUM_MONTHS][24];
DIR *directory = opendir(folder);
if (directory == NULL)
{
printf("Failed to open directory.\n");
return -1;
}
int foldercount = 0;
struct dirent *entry;
while ((entry = readdir(directory)) != NULL)
{
if (entry->d_type == DT_DIR)
{
if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
{
strcpy(months[foldercount], entry->d_name);
foldercount++;
}
}
}
closedir(directory);
return foldercount;
}
// function to get files in a particular month
int get_read_files(char *folder, char readfiles[][100])
{
// char readfiles[100][100];
DIR *directory = opendir(folder);
if (directory == NULL)
{
printf("Failed to open directory.\n");
return -1;
}
int filecount = 0;
struct dirent *entry;
while ((entry = readdir(directory)) != NULL)
{
if (entry->d_type == DT_REG)
{
strcpy(readfiles[filecount], entry->d_name);
filecount++;
}
}
closedir(directory);
return filecount;
}
// function to output text in file
void read_file(FILE *to_read)
{
int ch;
while (!feof(to_read))
{
ch = fgetc(to_read);
if (ch != EOF)
{
putchar(ch);
}
}
printf("\nOutput complete.\n");
fclose(to_read);
}
// function to update an existing entry. similar to get_entry except for an initial print of the text and length initialization
void update_entry(char *input, int max_length, char *folder)
{
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
int length = strlen(input);
printw("Writing into %s. Getting some input %i/%i", folder, length, MAX_LENGTH - 1);
move(1, 0);
printw("%s", input);
refresh();
int ch;
while ((ch = getch()) != '\n' && length < MAX_LENGTH - 1)
{
if (ch == KEY_BACKSPACE)
{
if (length > 0 && CURSOR_POS > 0)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
for (int i = CURSOR_POS - 1; i < length - 1; i++)
{
input[i] = input[i + 1];
}
length--;
input[length] = '\0';
// backspace moves cursor but not anymore with noecho();
if (x == 0)
{
clear();
printw("Writing into %s. Getting some input %i/%i", folder, length, MAX_LENGTH - 1);
move(1, 0);
printw("%s", input);
move(y - 1, COLS - 1);
}
else
{
clear();
printw("Writing into %s. Getting some input %i/%i", folder, length, MAX_LENGTH - 1);
move(1, 0);
printw("%s", input);
move(y, x - 1);
}
}
}
else if (ch == KEY_LEFT)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
if (length > 0 && CURSOR_POS > 0)
{
if (x == 0)
{
move(y - 1, COLS - 1);
}
else
{
move(y, x - 1);
}
}
}
else if (ch == KEY_RIGHT)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
if (CURSOR_POS < length)
{
if (x == COLS - 1)
{
move(y + 1, 0);
}
else
{
move(y, x + 1);
}
}
}
else if (ch < KEY_MIN || ch > KEY_MAX)
{
int y, x;
y = getcury(stdscr);
x = getcurx(stdscr);
for (int i = length; i > CURSOR_POS; i--)
{
input[i] = input[i - 1];
}
input[CURSOR_POS] = ch;
length++;
input[length] = '\0';
clear();
printw("Writing into %s. Getting some input %i/%i", folder, length, MAX_LENGTH - 1);
move(1, 0);
printw("%s", input);
if (x == COLS - 1)
{
move(y + 1, 0);
}
else
{
move(y, x + 1);
}
}
}
input[length] = '\0';
endwin();
}
// function that combines the whole reading of files
int get_read_files_complete(char *readfile, char *folder)
{
char years[NUM_YEARS][8];
int year_files = get_year_files(folder, years);
if (year_files <= 0)
{
printf("No entries found in %s.\n", folder);
return 1;
}
strcpy(readfile, folder);
int year_option = -1;
while (year_option < 0 || year_option > year_files - 1)
{
printf("Choose which year you would like to view.\n");
for (int i = 0; i < year_files; i++)
{
printf("%i. %s\n", i + 1, years[i]);
}
scanf("%i", &year_option);
year_option -= 1;
}
printf("You chose %s\n", years[year_option]);
strcat(readfile, "/");
strcat(readfile, years[year_option]);
printf("%s\n", readfile);
char months[NUM_MONTHS][24];
int month_files = get_month_files(readfile, months);
if (month_files <= 0)
{
printf("No entries found in %s.\n", readfile);
return 1;
}
int month_option = -1;
while (month_option < 0 || month_option > month_files - 1)
{
printf("Choose which month you would like to view.\n");
for (int i = 0; i < month_files; i++)
{
printf("%i. %s\n", i + 1, months[i]);
}
scanf("%i", &month_option);
month_option -= 1;
}
printf("You chose %s\n", months[month_option]);
strcat(readfile, "/");
strcat(readfile, months[month_option]);
printf("%s\n", readfile);
char readfiles[100][100];
int filecount = get_read_files(readfile, readfiles);
if (filecount <= 0)
{
printf("No files found in %s.\n", readfile);
return 1;
}
int file_option = -1;
while (file_option < 0 || file_option > filecount - 1)
{
printf("Choose which file you would like to view.\n");
for (int i = 0; i < filecount; i++)
{
printf("%i. %s\n", i + 1, readfiles[i]);
}
scanf("%i", &file_option);
file_option -= 1;
}
printf("You chose %s\n", readfiles[file_option]);
strcat(readfile, "/");
strcat(readfile, readfiles[file_option]);
printf("%s\n", readfile);
return 0;
}