-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
161 lines (134 loc) · 4.25 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
/*Author Muskaan Jain
Project Title: INVERTED SEARCH
DESCRIPTION:
*/
#include "inverted_search.h"
// Function pointer typedefs
typedef void (*DatabaseOperationFunc)(Flist *f_head, Wlist *head[]);
typedef void (*DisplayOperationFunc)(Wlist *head[]);
typedef void (*ExitOperationFunc)();
// Initialize the linked list heads for each letter
void initialize_head(Wlist *head[]) {
for (int i = 0; i < 27; i++) {
head[i] = create_wlist(); // Ensure create_wlist() initializes Wlist* correctly
}
}
// Menu option functions
void create_database_option(Flist *f_head, Wlist *head[]) {
create_database(f_head, head);
}
void display_database_option(Wlist *head[]) {
display_database(head);
}
void update_database_option(Flist *f_head, Wlist *head[]) {
update_database(head, &f_head);
}
// Function to search for a word in the inverted index
void search(Wlist *head[], char *word) {
int index = tolower(word[0]) - 'a';
if (index < 0 || index > 25) {
printf("Invalid word or word does not start with a letter.\n");
return;
}
Wlist *temp = head[index];
while (temp != NULL) {
if (strcmp(temp->word, word) == 0) {
printf("Word found: %s\n", word);
printf("Number of files containing the word: %d\n", temp->file_count);
Ltable *lt = temp->Tlink;
while (lt != NULL) {
printf("File: %s, Word Count: %d\n", lt->file_name, lt->word_count);
lt = lt->table_link;
}
return;
}
temp = temp->link;
}
printf("Word '%s' not found in the database.\n", word);
}
void search_option(Wlist *head[]) {
char search_word[WORD_SIZE];
printf("Enter the word to search: ");
if (scanf("%s", search_word) != 1) {
printf("Error reading input.\n");
// Clear input buffer to avoid infinite loop
while (getchar() != '\n');
return;
}
search(head, search_word);
}
void save_database_option(Wlist *head[]) {
save_database(head);
}
void exit_option() {
printf("Exiting...\n");
}
int main(int argc, char *argv[]) {
system("clear");
Wlist *head[27] = {NULL}; // Array of word list heads, one for each letter (a-z)
Flist *f_head = NULL;
initialize_head(head); // Initialize the head array
if (argc <= 1) {
printf("No input files provided.\n");
printf("Usage: ./Slist.exe f1.txt f2.txt...\n");
return 0;
}
file_validation_n_file_list(&f_head, argv);
if (f_head == NULL) {
printf("No files are available in the file linked list\n");
printf("Terminating process.\n");
return 1;
}
DatabaseOperationFunc db_operations[] = {
create_database_option,
update_database_option
};
DisplayOperationFunc display_operations[] = {
display_database_option,
search_option,
save_database_option
};
ExitOperationFunc exit_operation = exit_option;
int choice;
do {
printf("\nSelect your choice among the following options:\n");
printf("1. Create DATABASE\n");
printf("2. Display Database\n");
printf("3. Update DATABASE\n");
printf("4. Search\n");
printf("5. Save Database\n");
printf("6. Exit\n");
printf("Enter your choice: ");
if (scanf("%d", &choice) != 1) {
printf("Error reading input.\n");
while (getchar() != '\n'); // Clear input buffer
continue;
}
switch (choice) {
case 1:
db_operations[0](f_head, head);
break;
case 2:
display_operations[0](head);
break;
case 3:
db_operations[1](f_head, head);
break;
case 4:
display_operations[1](head);
break;
case 5:
display_operations[2](head);
break;
case 6:
exit_operation();
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 6);
// Free dynamically allocated memory here
// free_memory(head, f_head);
return 0;
}