Repository navigation
Expand file tree
/
Copy pathdisplay_save.c
More file actions
173 lines (146 loc) · 5.13 KB
/
Copy pathdisplay_save.c
File metadata and controls
173 lines (146 loc) · 5.13 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
#include "invert.h"
/*--------------------------------------------------------------------
* Function : display_database
* Description : Displays complete inverted search database.
*
* Parameters :
* arr -> Hash table array
*
* Return :
* SUCCESS
*
* Logic :
* - Traverse each index of hash table
* - Display:
* -> Word
* -> Number of files
* -> File names
* -> Word occurrence count
*------------------------------------------------------------------*/
int display_database(hash_t *arr)
{
/* Display table heading */
printf("\n------------------ DATABASE ------------------\n");
printf("%-10s | %-20s | %-10s | %-20s | %-10s\n",
"HashIdx",
"Word",
"FileCount",
"FileName",
"WordCount");
printf("-------------------------------------------------------------------------------\n");
/*--------------------------------------------------------------
* Traverse entire hash table
*-------------------------------------------------------------*/
for (int i = 0; i < HASH_SIZE; i++)
{
/* Point to main node list */
main_node_t *mtemp = arr[i].link;
/* Traverse main nodes */
while (mtemp)
{
/*------------------------------------------------------
* Print first sub node along with main node details
*-----------------------------------------------------*/
printf("%-10d | %-20s | %-10d | %-20s | %-10d\n",
i,
mtemp->word,
mtemp->file_count,
mtemp->sub_link->filename,
mtemp->sub_link->word_count);
/*------------------------------------------------------
* Traverse remaining sub nodes
*-----------------------------------------------------*/
sub_node_t *stemp = mtemp->sub_link->link;
while (stemp)
{
printf("%-10s | %-20s | %-10s | %-20s | %-10d\n",
"",
"",
"",
stemp->filename,
stemp->word_count);
stemp = stemp->link;
}
/* Move to next main node */
mtemp = mtemp->link;
}
}
printf("-------------------------------------------------------------------------------\n");
return SUCCESS;
}
/*--------------------------------------------------------------------
* Function : save_database
* Description : Saves inverted search database into text file.
*
* Parameters :
* arr -> Hash table array
*
* Return :
* SUCCESS / FAILURE
*
* File Format :
* #index;word;filecount;filename;wordcount;#
*------------------------------------------------------------------*/
int save_database(hash_t *arr)
{
char fname[FILE_SIZE];
/*--------------------------------------------------------------
* Get output file name from user
*-------------------------------------------------------------*/
printf("Enter file name to save database(.txt): ");
scanf("%s", fname);
/*--------------------------------------------------------------
* Validate .txt extension
*-------------------------------------------------------------*/
if (check_txt_ext(fname) == FAILURE)
{
printf("Error: File must be a .txt file\n");
return FAILURE;
}
/* Open file in write mode */
FILE *fp = fopen(fname, "w");
/* File opening failure */
if (fp == NULL)
{
perror("fopen");
return FAILURE;
}
/*--------------------------------------------------------------
* Traverse entire hash table
*-------------------------------------------------------------*/
for (int i = 0; i < HASH_SIZE; i++)
{
main_node_t *mtemp = arr[i].link;
/* Traverse main node list */
while (mtemp)
{
/*------------------------------------------------------
* Write main node details
*-----------------------------------------------------*/
fprintf(fp,
"#%d;%s;%d;",
i,
mtemp->word,
mtemp->file_count);
/*------------------------------------------------------
* Traverse sub node list
*-----------------------------------------------------*/
sub_node_t *stemp = mtemp->sub_link;
while (stemp)
{
fprintf(fp,
"%s;%d;",
stemp->filename,
stemp->word_count);
stemp = stemp->link;
}
/* End of one database entry */
fprintf(fp, "#\n");
/* Move to next main node */
mtemp = mtemp->link;
}
}
/* Close file */
fclose(fp);
return SUCCESS;
}