-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
191 lines (176 loc) · 5.24 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
/*
*
* Mathew McHugh
* C Program that manages a directory of notes
* Allows for opening and reading specific notes
* As well as the creation of new notes
*
*/
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "linkedList.h"
#include "util.h"
#include <stdlib.h>
char promp_user();
void execute_action(char choice, char* user);
struct Node *create_list();
void print_file();
void create_Note(char *user);
void delete_Note();
char *make_filename();
void populate_Note(FILE* fp, char *filename, char* author, char *time);
void edit_Note();
int main(void)
{
int choice;
char user[MAX_USER_NAME_LENGTH];
printf(" ----- Welcomes to the Note Record System -----\n\n");
printf("Enter your username: ");
fgets(user, MAX_USER_NAME_LENGTH, stdin);
do{
printf("-------------------------------------------------------------------------\n");
choice = promp_user();
execute_action(choice, user);
}while (choice != EXIT_VAL);
return(0);
}
/* get a char that represents the option the user has selected */
char promp_user(){
char choice[2];
printf("Options: \n 1: Create a new Note\n 2: View an existing Note\n 3: Delete an old Note\n 4: Edit an existing Note\n 5: Exit\n\n");
printf("Enter the number of your selection: ");
fgets(choice, 3, stdin);
return choice[0];
}
/* carries out the action that the user requests */
void execute_action(char choice, char *user){
struct Node *head = NULL;
printf("\n");
head = create_list();
switch(choice){
case '1':
create_Note(user);
break;
case '2':
printList(head);
print_file();
break;
case '3':
printList(head);
delete_Note();
break;
case '4':
printList(head);
edit_Note();
break;
case EXIT_VAL:
break;
default:
printf("Invalid option: Enter a number 1-5\n");
break;
}
}
/* creates a list of all .txt files in the directory */
struct Node *create_list(){
DIR *d;
struct dirent *dir;
struct Node* head = NULL;
d = opendir(".");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
if(strstr(dir->d_name, FILE_EXTENSION)){
append(&head, dir->d_name);
}
}
closedir(d);
}
return head;
}
/* prints out the contents of a file */
void print_file(){
char filename[MAX_NAME_LENGTH];
char line[BUFFER_SIZE];
FILE *fp;
printf("Type the full name without extension of the Note you would like to view: ");
fgets(filename, MAX_NAME_LENGTH, stdin);
strcpy(filename, string_to_filename(filename));
fp = fopen(filename, "r");
while(fp == NULL){
printf("Enter a valid filename: ");
fgets(filename, MAX_NAME_LENGTH, stdin);
strcpy(filename, string_to_filename(filename));
fp = fopen(filename, "r");
}
system("cls");
while(fgets(line, BUFFER_SIZE, fp) != NULL)
printf("%s", line);
if(feof(fp))
printf("\n\n");
else
printf("\nError\n");
fclose(fp);
}
/* creates a new .txt file with the header created based on filename, username, and time created */
void create_Note(char *user){
time_t mytime;
char *totalTime;
FILE *fp;
mytime = time(NULL);
char *filename = (char*)malloc(sizeof(char)*MAX_NAME_LENGTH);
printf("Type a custom Note name, or press <enter> for the default name: ");
fgets(filename, MAX_NAME_LENGTH, stdin);
if(!strcmp(filename, "\n")){
totalTime = ctime(&mytime);
parse_time(filename, totalTime);
}
else{
strcpy(filename, string_to_filename(filename));
}
fp = fopen(filename, "w");
if(fp == NULL)
printf("Failed to create file.\n");
else{
populate_Note(fp, filename, user, ctime(&mytime));
printf("File %s successfully created.\n", filename);
}
fclose(fp);
}
/* writes header into file */
void populate_Note(FILE* fp, char *filename, char* author, char *time){
fprintf(fp,"Title: %s\nAuthor: %sCreated: %s\nBody:", filename, author, time);
}
/* permanently deletes file */
void delete_Note(){
char filename[MAX_NAME_LENGTH];
printf("Enter the name without extension of the Note you would like to delete: ");
fgets(filename, MAX_NAME_LENGTH, stdin);
strcpy(filename, string_to_filename(filename));
if(remove(filename) == 0)
printf("File %s successfully deleted.\n", filename);
else
printf("Could not delete file %s.\n", filename);
}
/* opens file in gvim
REQUIRES GVIM BE INSTALLED */
void edit_Note(){
char filename[MAX_NAME_LENGTH];
char command[MAX_NAME_LENGTH + 5];
int ch;
FILE *fp = NULL;
while ((ch = getchar()) != '\n' && ch != EOF){}
do{
printf("Enter the name without extension of the Note you would like to edit or quit to exit: ");
fgets(filename, MAX_NAME_LENGTH, stdin);
if(!strcmp(filename, "quit\n"))
return;
strcpy(filename, string_to_filename(filename));
fp = fopen(filename, "r");
}while(fp == NULL);
strcpy(command, "gvim ");
strcat(command, filename);
system(command);
}