-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_database.c
66 lines (65 loc) · 2.22 KB
/
update_database.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
#include "inverted.h"
// function to update the database
int update_database(c_db *hash_t)
{
char newfile[100], c;
int index;
printf("Enter the file name to update : ");
scanf("%s", newfile); // reading the backedup file from user
FILE *fptr = fopen(newfile, "r");
// if check file is exist or not
if (fptr == NULL)
{
printf("File not found\n");
return -1;
}
// reading line by line from backup file untill fptr reaches EOF
while (fscanf(fptr, "%s", newfile) != EOF)
{
if (newfile[0] != '#')
return -1;
index = atoi(strtok(&newfile[1], ";")); // Extracting index from the line
m_node *m_new = malloc(sizeof(m_node)); // Create a new main node and updating the data
if (m_new == NULL)
return -1;
m_new->m_main_link = NULL;
strcpy(m_new->word, strtok(NULL, ";"));
m_new->file_count = atoi(strtok(NULL, ";"));
s_node *s_new = malloc(sizeof(s_node)); // Creating a new subword node and updating the data
s_node *stemp;
if (s_new == NULL)
return -1;
s_new->s_sub_link = NULL;
strcpy(s_new->file_name, strtok(NULL, ";"));
s_new->word_count = atoi(strtok(NULL, ";"));
m_new->m_sub_link = s_new;
stemp = s_new;
// loop to create more sub nodes based on file count
for (int i = 0; i < (m_new->file_count) - 1; i++)
{
s_node *s_new = malloc(sizeof(s_node));
if (s_new == NULL)
return -1;
s_new->s_sub_link = NULL;
strcpy(s_new->file_name, (strtok(NULL, ";")));
s_new->word_count = atoi(strtok(NULL, ";"));
stemp->s_sub_link = s_new;
stemp = s_new;
}
m_node *temp = hash_t[index].m_link; // inserting the new main node into the hashtable
if (temp == NULL)
{
hash_t[index].m_link = m_new;
}
else
{
while (temp->m_main_link)
{
temp = temp->m_main_link;
}
temp->m_main_link = m_new;
}
}
fclose(fptr);
return 1;
}