-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_database.c
126 lines (106 loc) · 2.23 KB
/
create_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
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
#include "inverted.h"
int create_DB(Slist *file_head, main_node_t **head)
{
if(file_head==NULL)
return FAILURE;
while (file_head != NULL)
{
read_datafile (file_head, head,file_head->f_name);
file_head = file_head->link;
}
}
void read_datafile(Slist *Shead, main_node_t **mhead, char *f_name)
{
int exitloop=1,key,flag=0;
char buffer[BUFF_SIZE];
FILE* fptr = fopen (f_name, "r");
fseek (fptr, 0, SEEK_SET);
while(exitloop)
{
//printf("first while\n");
exitloop=fscanf(fptr,"%s",buffer);
// printf("%s\n",buffer);
if(exitloop>0)
{
key=toupper(buffer[0])-65;
// printf("%d\n",key);
if(mhead[key]==NULL)
{
// printf("when nukk\n");
main_node_t *new=malloc(sizeof(main_node_t));
strcpy(new->word,buffer);
new->link=NULL;
new->sub_link=NULL;
new->f_count=1;
sub_node_t *snew=malloc(sizeof(sub_node_t));
strcpy(snew->f_name,f_name);
snew->w_count=1;
snew->link=NULL;
new->sub_link=snew;
mhead[key]=new;
}
else
{
// printf("when not null\n");
main_node_t *temp=mhead[key],*prev;
while(temp!=NULL)
{
// printf("inside while\n");
if(strcmp(temp->word,buffer)==0)
{
sub_node_t *temp1=temp->sub_link,*prev1;
while(temp1!=NULL)
{
prev1=temp1;
if(strcmp(temp1->f_name,f_name)==0)
{
temp1->w_count++;
flag=1;
break;
}
temp1=temp1->link;
}
// printf("outside 2nd while\n");
if(flag==0)
{
sub_node_t *snew=malloc(sizeof(sub_node_t));
strcpy(snew->f_name,f_name);
snew->w_count=1;
snew->link=NULL;
temp->f_count++;
prev1->link=snew;
break;
}
else
{
break;
}
}
prev=temp;
temp=temp->link;
}
// printf("outside while\n");
if(temp==NULL)
{
main_node_t *new=malloc(sizeof(main_node_t));
strcpy(new->word,buffer);
new->link=NULL;
new->sub_link=NULL;
new->f_count=1;
sub_node_t *snew=malloc(sizeof(sub_node_t));
strcpy(snew->f_name,f_name);
snew->w_count=1;
snew->link=NULL;
new->sub_link=snew;
prev->link=new;
}
// printf("at the end\n");
}
}
else
{
break;
}
}
// printf("one file complte\n");
}